1 /*=========================================================================
3 Module: $RCSfile: bbwxSlider.cxx,v $
5 Date: $Date: 2012/10/24 15:04:30 $
6 Version: $Revision: 1.26 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 * \brief Short description in one line
40 #ifdef _USE_WXWIDGETS_
42 #include "bbwxSlider.h"
43 #include "bbwxPackage.h"
44 #include "bbtkUtilities.h"
48 //--------------------------------------------------------------------------
49 // The widget created by the box
50 class SliderWidget : public wxPanel
53 /// Ctor with the two first params the parent window and the creator box
54 /// which must be passed to the WxBlackBoxWidget constructor.
55 /// The other params initialize the widget
56 SliderWidget(Slider* box, wxWindow *parent,
58 bool changeresolution,
68 /// Called when the slider is moved
69 void OnSliderTrack(wxScrollEvent& event);
70 /// Called when the slider is released
71 void OnSliderRelease(wxScrollEvent& event);
72 /// Called when the little slider which controls the resolution
73 /// of the main slider is moved (if activated)
74 void OnResolutionOfSlider(wxScrollEvent& event);
77 void SetValue(int v) { mwxSlider->SetValue(v); }
78 int GetValue() { return mwxSlider->GetValue(); }
79 int GetMin() {return min;}
80 int GetMax() {return max;}
81 void SetRange(int min, int max);
82 // Update the texts which display the min/max/current values of the slider
85 void SetReactiveOnTrack(bool ok);
91 wxSlider *mResolutionOfSlider;
95 wxStaticText *label_vertical;
96 wxStaticText *label_min;
97 wxStaticText *label_max;
99 //------------------------------------------------------------------------
100 //------------------------------------------------------------------------
101 //------------------------------------------------------------------------
105 //-------------------------------------------------------------------------
106 SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
108 bool changeresolution,
114 bool reactiveontrack)
116 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
120 reactiveOnTrack(reactiveontrack)
122 wxPanel * panel = this;
126 label_vertical = NULL;
127 mResolutionOfSlider = NULL;
131 long wxorientation=0;
136 wxorientation = wxSL_HORIZONTAL;
139 wxlabels = wxSL_LABELS;
146 wxorientation = wxSL_VERTICAL;
149 //---------------------------------------------------------------------
150 // 1) Creation of the components of the widget
151 // Any top level sub-widget must have the panel returned by panel
153 mwxSlider = new wxSlider( panel,
157 wxorientation | wxlabels );
159 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
161 // mwxSlider->SetTickFreq(100,0);
162 mwxSlider->SetRange(min,max);
163 mwxSlider->SetValue(value);
167 // Connecting events to callbacks
168 Connect( mwxSlider->GetId(),
169 wxEVT_SCROLL_THUMBRELEASE,
170 (wxObjectEventFunction)
171 (void (wxPanel::*)(wxScrollEvent&))
172 &SliderWidget::OnSliderRelease );
174 Connect( mwxSlider->GetId(),
175 wxEVT_SCROLL_THUMBTRACK,
176 (wxObjectEventFunction)
177 (void (wxPanel::*)(wxScrollEvent&))
178 &SliderWidget::OnSliderTrack );
180 Connect( mwxSlider->GetId(),
181 wxEVT_SCROLL_CHANGED,
182 (wxObjectEventFunction)
183 (void (wxPanel::*)(wxScrollEvent&))
184 &SliderWidget::OnSliderTrack );
186 // If asked : creation of the other little slider which allows to change
187 // the resolution of the main slider
188 if (changeresolution==true){
189 // has panel for parent too
190 mResolutionOfSlider = new wxSlider(panel,
199 mResolutionOfSlider->SetRange(1,8);
200 mResolutionOfSlider->SetValue(5);
201 // Is wxEVT_COMMAND_SLIDER_UPDATED event
202 // is connected to the method OnResolutionOfSlider
203 Connect(mResolutionOfSlider->GetId(),
204 wxEVT_COMMAND_SLIDER_UPDATED,
205 (wxObjectEventFunction)
206 (void (wxPanel::*)(wxScrollEvent&))
207 &SliderWidget::OnResolutionOfSlider );
209 //---------------------------------------------------------------------
211 //---------------------------------------------------------------------
212 // 2) Insertion of the components in the window
214 // We use a FlexGridSizer
215 wxFlexGridSizer *sizer;
221 if (mResolutionOfSlider!=NULL)
223 sizer = new wxFlexGridSizer(2);
226 sizer->Add( new wxStaticText(panel,-1, title ) );
227 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
232 sizer = new wxFlexGridSizer(1);
235 sizer->Add( new wxStaticText(panel,-1, title ) );
238 sizer->Add( mwxSlider,1,wxGROW );
239 sizer->AddGrowableCol(0);
240 if (mResolutionOfSlider!=NULL)
242 sizer->Add( mResolutionOfSlider );
248 sizer = new wxFlexGridSizer(1);
249 if (labels==true) // with lable
251 label_vertical = new wxStaticText(panel,-1,_T(""));
252 label_min = new wxStaticText(panel,-1,_T(""));
253 label_max = new wxStaticText(panel,-1,_T(""));
256 sizer->Add( new wxStaticText(panel,-1, title ) );
257 sizer->AddGrowableRow(3);
261 sizer->AddGrowableRow(2);
263 sizer->Add( label_vertical );
264 sizer->Add( label_min );
265 sizer->Add( mwxSlider,1,wxGROW );
266 sizer->Add( label_max );
267 if (mResolutionOfSlider!=NULL)
269 sizer->Add( mResolutionOfSlider );
276 sizer->Add( new wxStaticText(panel,-1, title ) );
277 sizer->AddGrowableRow(1);
281 sizer->AddGrowableRow(0);
283 sizer->Add( mwxSlider,1,wxGROW );
284 if (mResolutionOfSlider!=NULL)
286 sizer->Add( mResolutionOfSlider );
290 // Initialize the labels
292 // Insert the sizer in the main panel and refresh the layout
293 panel->SetSizer(sizer);
295 //-------------------------------------------------------------------------
299 //-------------------------------------------------------------------------
300 SliderWidget::~SliderWidget()
303 //-------------------------------------------------------------------------
306 //-------------------------------------------------------------------------
307 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
309 int value = mwxSlider->GetValue();
310 int delta = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
311 int minTmp = value - delta/2;
312 int maxTmp = value + delta/2;
322 mwxSlider->SetRange(minTmp,maxTmp);
325 //-------------------------------------------------------------------------
329 //-------------------------------------------------------------------------
330 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
334 // When user releases the slider
335 // we update the output of the box
337 mBox->bbSetOutputOut( mwxSlider->GetValue() );
338 mBox->bbSetInputIn( mwxSlider->GetValue() );
339 // and signal that the output has changed
340 mBox->bbSignalOutputModification(std::string("Out"));
344 //-------------------------------------------------------------------------
347 //-------------------------------------------------------------------------
348 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
350 // When user releases the slider
351 // we update the output of the box
352 mBox->bbSetOutputOut( mwxSlider->GetValue() );
353 mBox->bbSetInputIn( mwxSlider->GetValue() );
354 // and signal that the output has changed
355 mBox->bbSignalOutputModification(std::string("Out"));
357 //-------------------------------------------------------------------------
360 //-------------------------------------------------------------------------
361 void SliderWidget::RefreshLabels()
364 if (label_vertical!=NULL)
366 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
367 label_vertical->SetLabel(strValue);
371 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
372 label_min->SetLabel(strValue);
376 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
377 label_max->SetLabel(strValue);
380 //-------------------------------------------------------------------------
383 //-------------------------------------------------------------------------
384 void SliderWidget::SetRange(int min, int max)
388 mwxSlider->SetRange(min,max);
393 //-------------------------------------------------------------------------
394 void SliderWidget::SetReactiveOnTrack(bool ok)
396 reactiveOnTrack = ok;
399 //-------------------------------------------------------------------------
404 //--------------------------------------------------------------------------
405 //-------------------------------------------------------------------------
406 // WxBlackBox implementation
407 //--------------------------------------------------------------------------
408 //--------------------------------------------------------------------------
410 //--------------------------------------------------------------------------
411 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
412 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
414 //-----------------------------------------------------------------
415 void Slider::bbUserSetDefaultValues()
421 bbSetInputOrientation("HORIZONTAL");
422 bbSetInputChangeResolution(false);
423 bbSetInputLabel(true);
424 bbSetInputReactiveOnTrack(false);
427 //-----------------------------------------------------------------
428 void Slider::bbUserInitializeProcessing()
432 //-----------------------------------------------------------------
433 void Slider::bbUserFinalizeProcessing()
437 //--------------------------------------------------------------------------
438 void Slider::Process()
440 bbtkDebugMessage("process",3,
441 "Slider "<<bbGetName()<<" input="
442 <<bbGetInputIn()<<std::endl);
444 // desperate try // JPR
445 if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() || bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )
447 ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
450 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
452 bbSetOutputOut( bbGetInputIn() );
453 if (bbGetOutputWidget()!=0)
455 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
459 //--------------------------------------------------------------------------
460 void Slider::CreateWidget(wxWindow* parent)
464 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
465 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
468 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
469 SliderWidget *w = new SliderWidget(this,
470 parent, //bbGetWxParent(),
472 bbGetInputChangeResolution(),
474 bbtk::std2wx( bbGetInputTitle() ),
478 bbGetInputReactiveOnTrack()
480 // std::cout << "w = "<<w<<std::endl;
481 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
483 bbSetOutputWidget( w );
489 #endif // _USE_WXWIDGETS_