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);
305 //-------------------------------------------------------------------------
306 SliderWidget::~SliderWidget()
310 //-------------------------------------------------------------------------
311 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
313 int value = mwxSlider->GetValue();
314 int delta = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
315 int minTmp = value - delta/2;
316 int maxTmp = value + delta/2;
326 mwxSlider->SetRange(minTmp,maxTmp);
330 //-------------------------------------------------------------------------
331 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
335 // When user releases the slider
336 // we update the output of the box
338 mBox->bbSetOutputOut( mwxSlider->GetValue() );
339 mBox->bbSetInputIn( mwxSlider->GetValue() );
340 // and signal that the output has changed
341 mBox->bbSignalOutputModification(std::string("Out"));
346 //-------------------------------------------------------------------------
347 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
349 // When user releases the slider
350 // we update the output of the box
351 mBox->bbSetOutputOut( mwxSlider->GetValue() );
352 mBox->bbSetInputIn( mwxSlider->GetValue() );
353 // and signal that the output has changed
354 mBox->bbSignalOutputModification(std::string("Out"));
356 //-------------------------------------------------------------------------
359 //-------------------------------------------------------------------------
360 void SliderWidget::RefreshLabels()
363 if (label_vertical!=NULL)
365 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
366 label_vertical->SetLabel(strValue);
370 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
371 label_min->SetLabel(strValue);
375 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
376 label_max->SetLabel(strValue);
379 //-------------------------------------------------------------------------
382 //-------------------------------------------------------------------------
383 void SliderWidget::SetRange(int min, int max)
387 mwxSlider->SetRange(min,max);
392 //-------------------------------------------------------------------------
393 void SliderWidget::SetReactiveOnTrack(bool ok)
395 reactiveOnTrack = ok;
398 //-------------------------------------------------------------------------
403 //--------------------------------------------------------------------------
404 //-------------------------------------------------------------------------
405 // WxBlackBox implementation
406 //--------------------------------------------------------------------------
407 //--------------------------------------------------------------------------
409 //--------------------------------------------------------------------------
410 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
411 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
413 //-----------------------------------------------------------------
414 void Slider::bbUserSetDefaultValues()
420 bbSetInputOrientation("HORIZONTAL");
421 bbSetInputChangeResolution(false);
422 bbSetInputLabel(true);
423 bbSetInputReactiveOnTrack(false);
426 //-----------------------------------------------------------------
427 void Slider::bbUserInitializeProcessing()
431 //-----------------------------------------------------------------
432 void Slider::bbUserFinalizeProcessing()
436 //--------------------------------------------------------------------------
437 void Slider::Process()
439 bbtkDebugMessage("process",3,
440 "Slider "<<bbGetName()<<" input="
441 <<bbGetInputIn()<<std::endl);
443 if (bbGetOutputWidget()!=NULL)
445 // desperate try // JPR
446 if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() || bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )
448 ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
451 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
453 bbSetOutputOut( bbGetInputIn() );
454 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
455 } // if bbGetOutputWidget
458 //--------------------------------------------------------------------------
459 void Slider::CreateWidget(wxWindow* parent)
462 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
463 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
464 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
465 SliderWidget *w = new SliderWidget(this,
466 parent, //bbGetWxParent(),
468 bbGetInputChangeResolution(),
470 bbtk::std2wx( bbGetInputTitle() ),
474 bbGetInputReactiveOnTrack()
476 // std::cout << "w = "<<w<<std::endl;
477 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
478 bbSetOutputOut( bbGetInputIn() );
479 bbSetOutputWidget( w );
485 #endif // _USE_WXWIDGETS_