2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
28 /*=========================================================================
30 Module: $RCSfile: bbwxSlider.cxx,v $
32 Date: $Date: 2012/11/16 08:52:14 $
33 Version: $Revision: 1.27 $
34 =========================================================================*/
38 * \brief Short description in one line
45 #ifdef _USE_WXWIDGETS_
47 #include "bbwxSlider.h"
48 #include "bbwxPackage.h"
49 #include "bbtkUtilities.h"
53 //--------------------------------------------------------------------------
54 // The widget created by the box
55 class SliderWidget : public wxPanel
58 /// Ctor with the two first params the parent window and the creator box
59 /// which must be passed to the WxBlackBoxWidget constructor.
60 /// The other params initialize the widget
61 SliderWidget(Slider* box, wxWindow *parent,
63 bool changeresolution,
73 /// Called when the slider is moved
74 void OnSliderTrack(wxScrollEvent& event);
75 /// Called when the slider is released
76 void OnSliderRelease(wxScrollEvent& event);
77 /// Called when the little slider which controls the resolution
78 /// of the main slider is moved (if activated)
79 void OnResolutionOfSlider(wxScrollEvent& event);
82 void SetValue(int v) { mwxSlider->SetValue(v); }
83 int GetValue() { return mwxSlider->GetValue(); }
84 int GetMin() {return min;}
85 int GetMax() {return max;}
86 void SetRange(int min, int max);
87 // Update the texts which display the min/max/current values of the slider
90 void SetReactiveOnTrack(bool ok);
96 wxSlider *mResolutionOfSlider;
100 wxStaticText *label_vertical;
101 wxStaticText *label_min;
102 wxStaticText *label_max;
104 //------------------------------------------------------------------------
105 //------------------------------------------------------------------------
106 //------------------------------------------------------------------------
110 //-------------------------------------------------------------------------
111 SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
113 bool changeresolution,
119 bool reactiveontrack)
121 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
125 reactiveOnTrack(reactiveontrack)
127 wxPanel * panel = this;
131 label_vertical = NULL;
132 mResolutionOfSlider = NULL;
136 long wxorientation=0;
141 wxorientation = wxSL_HORIZONTAL;
144 wxlabels = wxSL_LABELS;
151 wxorientation = wxSL_VERTICAL;
154 //---------------------------------------------------------------------
155 // 1) Creation of the components of the widget
156 // Any top level sub-widget must have the panel returned by panel
158 mwxSlider = new wxSlider( panel,
161 wxDefaultSize, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
164 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
166 // mwxSlider->SetTickFreq(100,0);
167 mwxSlider->SetRange(min,max);
168 mwxSlider->SetValue(value);
172 // Connecting events to callbacks
173 Connect( mwxSlider->GetId(),
174 wxEVT_SCROLL_THUMBRELEASE,
175 (wxObjectEventFunction)
176 (void (wxPanel::*)(wxScrollEvent&))
177 &SliderWidget::OnSliderRelease );
179 Connect( mwxSlider->GetId(),
180 wxEVT_SCROLL_THUMBTRACK,
181 (wxObjectEventFunction)
182 (void (wxPanel::*)(wxScrollEvent&))
183 &SliderWidget::OnSliderTrack );
185 Connect( mwxSlider->GetId(),
186 wxEVT_SCROLL_CHANGED,
187 (wxObjectEventFunction)
188 (void (wxPanel::*)(wxScrollEvent&))
189 &SliderWidget::OnSliderTrack );
191 // If asked : creation of the other little slider which allows to change
192 // the resolution of the main slider
193 if (changeresolution==true){
194 // has panel for parent too
195 mResolutionOfSlider = new wxSlider(panel,
204 mResolutionOfSlider->SetRange(1,8);
205 mResolutionOfSlider->SetValue(5);
206 // Is wxEVT_COMMAND_SLIDER_UPDATED event
207 // is connected to the method OnResolutionOfSlider
208 Connect(mResolutionOfSlider->GetId(),
209 wxEVT_COMMAND_SLIDER_UPDATED,
210 (wxObjectEventFunction)
211 (void (wxPanel::*)(wxScrollEvent&))
212 &SliderWidget::OnResolutionOfSlider );
214 //---------------------------------------------------------------------
216 //---------------------------------------------------------------------
217 // 2) Insertion of the components in the window
219 // We use a FlexGridSizer
220 wxFlexGridSizer *sizer;
226 if (mResolutionOfSlider!=NULL)
228 sizer = new wxFlexGridSizer(2);
231 sizer->Add( new wxStaticText(panel,-1, title ) );
232 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
237 sizer = new wxFlexGridSizer(1);
240 sizer->Add( new wxStaticText(panel,-1, title ) );
243 sizer->Add( mwxSlider,1,wxGROW );
244 sizer->AddGrowableCol(0);
245 if (mResolutionOfSlider!=NULL)
247 sizer->Add( mResolutionOfSlider );
253 sizer = new wxFlexGridSizer(1);
254 if (labels==true) // with lable
256 label_vertical = new wxStaticText(panel,-1,_T(""));
257 label_min = new wxStaticText(panel,-1,_T(""));
258 label_max = new wxStaticText(panel,-1,_T(""));
261 sizer->Add( new wxStaticText(panel,-1, title ) );
262 sizer->AddGrowableRow(3);
266 sizer->AddGrowableRow(2);
268 sizer->Add( label_vertical );
269 sizer->Add( label_min );
270 sizer->Add( mwxSlider,1,wxGROW );
271 sizer->Add( label_max );
272 if (mResolutionOfSlider!=NULL)
274 sizer->Add( mResolutionOfSlider );
281 sizer->Add( new wxStaticText(panel,-1, title ) );
282 sizer->AddGrowableRow(1);
286 sizer->AddGrowableRow(0);
288 sizer->Add( mwxSlider,1,wxGROW );
289 if (mResolutionOfSlider!=NULL)
291 sizer->Add( mResolutionOfSlider );
295 // Initialize the labels
297 // Insert the sizer in the main panel and refresh the layout
298 panel->SetSizer(sizer);
300 //-------------------------------------------------------------------------
304 //-------------------------------------------------------------------------
305 SliderWidget::~SliderWidget()
308 //-------------------------------------------------------------------------
311 //-------------------------------------------------------------------------
312 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
314 int value = mwxSlider->GetValue();
315 int delta = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
316 int minTmp = value - delta/2;
317 int maxTmp = value + delta/2;
327 mwxSlider->SetRange(minTmp,maxTmp);
330 //-------------------------------------------------------------------------
334 //-------------------------------------------------------------------------
335 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
339 // When user releases the slider
340 // we update the output of the box
342 mBox->bbSetOutputOut( mwxSlider->GetValue() );
343 mBox->bbSetInputIn( mwxSlider->GetValue() );
344 // and signal that the output has changed
345 mBox->bbSignalOutputModification(std::string("Out"));
349 //-------------------------------------------------------------------------
352 //-------------------------------------------------------------------------
353 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
355 // When user releases the slider
356 // we update the output of the box
357 mBox->bbSetOutputOut( mwxSlider->GetValue() );
358 mBox->bbSetInputIn( mwxSlider->GetValue() );
359 // and signal that the output has changed
360 mBox->bbSignalOutputModification(std::string("Out"));
362 //-------------------------------------------------------------------------
365 //-------------------------------------------------------------------------
366 void SliderWidget::RefreshLabels()
369 if (label_vertical!=NULL)
371 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
372 label_vertical->SetLabel(strValue);
376 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
377 label_min->SetLabel(strValue);
381 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
382 label_max->SetLabel(strValue);
385 //-------------------------------------------------------------------------
388 //-------------------------------------------------------------------------
389 void SliderWidget::SetRange(int min, int max)
393 mwxSlider->SetRange(min,max);
398 //-------------------------------------------------------------------------
399 void SliderWidget::SetReactiveOnTrack(bool ok)
401 reactiveOnTrack = ok;
404 //-------------------------------------------------------------------------
409 //--------------------------------------------------------------------------
410 //-------------------------------------------------------------------------
411 // WxBlackBox implementation
412 //--------------------------------------------------------------------------
413 //--------------------------------------------------------------------------
415 //--------------------------------------------------------------------------
416 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
417 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
419 //-----------------------------------------------------------------
420 void Slider::bbUserSetDefaultValues()
426 bbSetInputOrientation("HORIZONTAL");
427 bbSetInputChangeResolution(false);
428 bbSetInputLabel(true);
429 bbSetInputReactiveOnTrack(false);
432 //-----------------------------------------------------------------
433 void Slider::bbUserInitializeProcessing()
437 //-----------------------------------------------------------------
438 void Slider::bbUserFinalizeProcessing()
442 //--------------------------------------------------------------------------
443 void Slider::Process()
445 bbtkDebugMessage("process",3,
446 "Slider "<<bbGetName()<<" input="
447 <<bbGetInputIn()<<std::endl);
449 // desperate try // JPR
450 if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() || bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )
452 ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
455 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
457 bbSetOutputOut( bbGetInputIn() );
458 if (bbGetOutputWidget()!=0)
460 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
464 //--------------------------------------------------------------------------
465 void Slider::CreateWidget(wxWindow* parent)
468 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
469 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
470 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
471 SliderWidget *w = new SliderWidget(this,
472 parent, //bbGetWxParent(),
474 bbGetInputChangeResolution(),
476 bbtk::std2wx( bbGetInputTitle() ),
480 bbGetInputReactiveOnTrack()
482 // std::cout << "w = "<<w<<std::endl;
483 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
484 bbSetOutputWidget( w );
490 #endif // _USE_WXWIDGETS_