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;
155 wxSize sizeSliderWidget(25,25);
156 // int widthSliderWidget = wxDefaultSize;
158 //---------------------------------------------------------------------
159 // 1) Creation of the components of the widget
160 // Any top level sub-widget must have the panel returned by panel
162 mwxSlider = new wxSlider( panel,
165 sizeSliderWidget, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
168 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
170 // mwxSlider->SetTickFreq(100,0);
171 mwxSlider->SetRange(min,max);
172 mwxSlider->SetValue(value);
176 // Connecting events to callbacks
177 Connect( mwxSlider->GetId(),
178 wxEVT_SCROLL_THUMBRELEASE,
179 (wxObjectEventFunction)
180 (void (wxPanel::*)(wxScrollEvent&))
181 &SliderWidget::OnSliderRelease );
183 Connect( mwxSlider->GetId(),
184 wxEVT_SCROLL_THUMBTRACK,
185 (wxObjectEventFunction)
186 (void (wxPanel::*)(wxScrollEvent&))
187 &SliderWidget::OnSliderTrack );
189 Connect( mwxSlider->GetId(),
190 wxEVT_SCROLL_CHANGED,
191 (wxObjectEventFunction)
192 (void (wxPanel::*)(wxScrollEvent&))
193 &SliderWidget::OnSliderTrack );
195 // If asked : creation of the other little slider which allows to change
196 // the resolution of the main slider
197 if (changeresolution==true){
198 // has panel for parent too
199 mResolutionOfSlider = new wxSlider(panel,
208 mResolutionOfSlider->SetRange(1,8);
209 mResolutionOfSlider->SetValue(5);
210 // Is wxEVT_COMMAND_SLIDER_UPDATED event
211 // is connected to the method OnResolutionOfSlider
212 Connect(mResolutionOfSlider->GetId(),
213 wxEVT_COMMAND_SLIDER_UPDATED,
214 (wxObjectEventFunction)
215 (void (wxPanel::*)(wxScrollEvent&))
216 &SliderWidget::OnResolutionOfSlider );
218 //---------------------------------------------------------------------
220 //---------------------------------------------------------------------
221 // 2) Insertion of the components in the window
223 // We use a FlexGridSizer
224 wxFlexGridSizer *sizer;
230 if (mResolutionOfSlider!=NULL)
232 sizer = new wxFlexGridSizer(2);
235 sizer->Add( new wxStaticText(panel,-1, title ) );
236 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
241 sizer = new wxFlexGridSizer(1);
244 sizer->Add( new wxStaticText(panel,-1, title ) );
247 sizer->Add( mwxSlider,1,wxGROW );
248 sizer->AddGrowableCol(0);
249 if (mResolutionOfSlider!=NULL)
251 sizer->Add( mResolutionOfSlider );
257 sizer = new wxFlexGridSizer(1);
258 if (labels==true) // with lable
260 label_vertical = new wxStaticText(panel,-1,_T(""));
261 label_min = new wxStaticText(panel,-1,_T(""));
262 label_max = new wxStaticText(panel,-1,_T(""));
265 sizer->Add( new wxStaticText(panel,-1, title ) );
266 sizer->AddGrowableRow(3);
270 sizer->AddGrowableRow(2);
272 sizer->Add( label_vertical );
273 sizer->Add( label_min );
274 sizer->Add( mwxSlider,1,wxGROW );
275 sizer->Add( label_max );
276 if (mResolutionOfSlider!=NULL)
278 sizer->Add( mResolutionOfSlider );
285 sizer->Add( new wxStaticText(panel,-1, title ) );
286 sizer->AddGrowableRow(1);
290 sizer->AddGrowableRow(0);
292 sizer->Add( mwxSlider,1,wxGROW );
293 if (mResolutionOfSlider!=NULL)
295 sizer->Add( mResolutionOfSlider );
299 // Initialize the labels
301 // Insert the sizer in the main panel and refresh the layout
302 panel->SetSizer(sizer);
304 //-------------------------------------------------------------------------
308 //-------------------------------------------------------------------------
309 SliderWidget::~SliderWidget()
312 //-------------------------------------------------------------------------
315 //-------------------------------------------------------------------------
316 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
318 int value = mwxSlider->GetValue();
319 int delta = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
320 int minTmp = value - delta/2;
321 int maxTmp = value + delta/2;
331 mwxSlider->SetRange(minTmp,maxTmp);
334 //-------------------------------------------------------------------------
338 //-------------------------------------------------------------------------
339 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
343 // When user releases the slider
344 // we update the output of the box
346 mBox->bbSetOutputOut( mwxSlider->GetValue() );
347 mBox->bbSetInputIn( mwxSlider->GetValue() );
348 // and signal that the output has changed
349 mBox->bbSignalOutputModification(std::string("Out"));
353 //-------------------------------------------------------------------------
356 //-------------------------------------------------------------------------
357 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
359 // When user releases the slider
360 // we update the output of the box
361 mBox->bbSetOutputOut( mwxSlider->GetValue() );
362 mBox->bbSetInputIn( mwxSlider->GetValue() );
363 // and signal that the output has changed
364 mBox->bbSignalOutputModification(std::string("Out"));
366 //-------------------------------------------------------------------------
369 //-------------------------------------------------------------------------
370 void SliderWidget::RefreshLabels()
373 if (label_vertical!=NULL)
375 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
376 label_vertical->SetLabel(strValue);
380 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
381 label_min->SetLabel(strValue);
385 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
386 label_max->SetLabel(strValue);
389 //-------------------------------------------------------------------------
392 //-------------------------------------------------------------------------
393 void SliderWidget::SetRange(int min, int max)
397 mwxSlider->SetRange(min,max);
402 //-------------------------------------------------------------------------
403 void SliderWidget::SetReactiveOnTrack(bool ok)
405 reactiveOnTrack = ok;
408 //-------------------------------------------------------------------------
413 //--------------------------------------------------------------------------
414 //-------------------------------------------------------------------------
415 // WxBlackBox implementation
416 //--------------------------------------------------------------------------
417 //--------------------------------------------------------------------------
419 //--------------------------------------------------------------------------
420 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
421 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
423 //-----------------------------------------------------------------
424 void Slider::bbUserSetDefaultValues()
430 bbSetInputOrientation("HORIZONTAL");
431 bbSetInputChangeResolution(false);
432 bbSetInputLabel(true);
433 bbSetInputReactiveOnTrack(false);
436 //-----------------------------------------------------------------
437 void Slider::bbUserInitializeProcessing()
441 //-----------------------------------------------------------------
442 void Slider::bbUserFinalizeProcessing()
446 //--------------------------------------------------------------------------
447 void Slider::Process()
449 bbtkDebugMessage("process",3,
450 "Slider "<<bbGetName()<<" input="
451 <<bbGetInputIn()<<std::endl);
453 if (bbGetOutputWidget()!=NULL)
455 // desperate try // JPR
456 if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() || bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )
458 ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
461 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
463 bbSetOutputOut( bbGetInputIn() );
464 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
465 } // if bbGetOutputWidget
468 //--------------------------------------------------------------------------
469 void Slider::CreateWidget(wxWindow* parent)
472 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
473 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
474 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
475 SliderWidget *w = new SliderWidget(this,
476 parent, //bbGetWxParent(),
478 bbGetInputChangeResolution(),
480 bbtk::std2wx( bbGetInputTitle() ),
484 bbGetInputReactiveOnTrack()
486 // std::cout << "w = "<<w<<std::endl;
487 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
488 bbSetOutputOut( bbGetInputIn() );
489 bbSetOutputWidget( w );
495 #endif // _USE_WXWIDGETS_