1 /*=========================================================================
3 Module: $RCSfile: bbwxSlider.cxx,v $
5 Date: $Date: 2009/04/16 14:17:50 $
6 Version: $Revision: 1.22 $
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 void SetRange(int min, int max);
80 // Update the texts which display the min/max/current values of the slider
86 wxSlider *mResolutionOfSlider;
90 wxStaticText *label_vertical;
91 wxStaticText *label_min;
92 wxStaticText *label_max;
94 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
96 //------------------------------------------------------------------------
100 //-------------------------------------------------------------------------
101 SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
103 bool changeresolution,
109 bool reactiveontrack)
111 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
115 reactiveOnTrack(reactiveontrack)
117 wxPanel * panel = this;
121 label_vertical = NULL;
122 mResolutionOfSlider = NULL;
126 long wxorientation=0;
131 wxorientation = wxSL_HORIZONTAL;
134 wxlabels = wxSL_LABELS;
141 wxorientation = wxSL_VERTICAL;
144 //---------------------------------------------------------------------
145 // 1) Creation of the components of the widget
146 // Any top level sub-widget must have the panel returned by panel
148 mwxSlider = new wxSlider( panel,
152 wxorientation | wxlabels );
154 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
156 // mwxSlider->SetTickFreq(100,0);
157 mwxSlider->SetRange(min,max);
158 mwxSlider->SetValue(value);
162 // Connecting events to callbacks
163 Connect( mwxSlider->GetId(),
164 wxEVT_SCROLL_THUMBRELEASE,
165 (wxObjectEventFunction)
166 (void (wxPanel::*)(wxScrollEvent&))
167 &SliderWidget::OnSliderRelease );
169 Connect( mwxSlider->GetId(),
170 wxEVT_SCROLL_THUMBTRACK,
171 (wxObjectEventFunction)
172 (void (wxPanel::*)(wxScrollEvent&))
173 &SliderWidget::OnSliderTrack );
175 Connect( mwxSlider->GetId(),
176 wxEVT_SCROLL_CHANGED,
177 (wxObjectEventFunction)
178 (void (wxPanel::*)(wxScrollEvent&))
179 &SliderWidget::OnSliderTrack );
181 // If asked : creation of the other little slider which allows to change
182 // the resolution of the main slider
183 if (changeresolution==true){
184 // has panel for parent too
185 mResolutionOfSlider = new wxSlider(panel,
194 mResolutionOfSlider->SetRange(1,8);
195 mResolutionOfSlider->SetValue(5);
196 // Is wxEVT_COMMAND_SLIDER_UPDATED event
197 // is connected to the method OnResolutionOfSlider
198 Connect(mResolutionOfSlider->GetId(),
199 wxEVT_COMMAND_SLIDER_UPDATED,
200 (wxObjectEventFunction)
201 (void (wxPanel::*)(wxScrollEvent&))
202 &SliderWidget::OnResolutionOfSlider );
204 //---------------------------------------------------------------------
206 //---------------------------------------------------------------------
207 // 2) Insertion of the components in the window
209 // We use a FlexGridSizer
210 wxFlexGridSizer *sizer;
216 if (mResolutionOfSlider!=NULL)
218 sizer = new wxFlexGridSizer(2);
221 sizer->Add( new wxStaticText(panel,-1, title ) );
222 sizer->Add( new wxStaticText(panel,-1, _T("") ) );
227 sizer = new wxFlexGridSizer(1);
230 sizer->Add( new wxStaticText(panel,-1, title ) );
233 sizer->Add( mwxSlider,1,wxGROW );
234 sizer->AddGrowableCol(0);
235 if (mResolutionOfSlider!=NULL)
237 sizer->Add( mResolutionOfSlider );
243 sizer = new wxFlexGridSizer(1);
244 if (labels==true) // with lable
246 label_vertical = new wxStaticText(panel,-1,_T(""));
247 label_min = new wxStaticText(panel,-1,_T(""));
248 label_max = new wxStaticText(panel,-1,_T(""));
251 sizer->Add( new wxStaticText(panel,-1, title ) );
252 sizer->AddGrowableRow(3);
256 sizer->AddGrowableRow(2);
258 sizer->Add( label_vertical );
259 sizer->Add( label_min );
260 sizer->Add( mwxSlider,1,wxGROW );
261 sizer->Add( label_max );
262 if (mResolutionOfSlider!=NULL)
264 sizer->Add( mResolutionOfSlider );
271 sizer->Add( new wxStaticText(panel,-1, title ) );
272 sizer->AddGrowableRow(1);
276 sizer->AddGrowableRow(0);
278 sizer->Add( mwxSlider,1,wxGROW );
279 if (mResolutionOfSlider!=NULL)
281 sizer->Add( mResolutionOfSlider );
285 // Initialize the labels
287 // Insert the sizer in the main panel and refresh the layout
288 panel->SetSizer(sizer);
290 //-------------------------------------------------------------------------
293 //-------------------------------------------------------------------------
294 SliderWidget::~SliderWidget()
297 //-------------------------------------------------------------------------
300 //-------------------------------------------------------------------------
301 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
303 int value = mwxSlider->GetValue();
304 int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
305 int minTmp = value - delta/2;
306 int maxTmp = value + delta/2;
316 mwxSlider->SetRange(minTmp,maxTmp);
319 //-------------------------------------------------------------------------
323 //-------------------------------------------------------------------------
324 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
328 // When user releases the slider
329 // we update the output of the box
331 mBox->bbSetOutputOut( mwxSlider->GetValue() );
332 mBox->bbSetInputIn( mwxSlider->GetValue() );
333 // and signal that the output has changed
334 mBox->bbSignalOutputModification(std::string("Out"));
338 //-------------------------------------------------------------------------
341 //-------------------------------------------------------------------------
342 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
344 // When user releases the slider
345 // 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"));
351 //-------------------------------------------------------------------------
354 //-------------------------------------------------------------------------
355 void SliderWidget::RefreshLabels()
358 if (label_vertical!=NULL)
360 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
361 label_vertical->SetLabel(strValue);
365 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
366 label_min->SetLabel(strValue);
370 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
371 label_max->SetLabel(strValue);
374 //-------------------------------------------------------------------------
377 //-------------------------------------------------------------------------
378 void SliderWidget::SetRange(int min, int max)
382 mwxSlider->SetRange(min,max);
385 //-------------------------------------------------------------------------
390 //--------------------------------------------------------------------------
391 //-------------------------------------------------------------------------
392 // WxBlackBox implementation
393 //--------------------------------------------------------------------------
394 //--------------------------------------------------------------------------
396 //--------------------------------------------------------------------------
397 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
398 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
400 //--------------------------------------------------------------------------
401 void Slider::bbUserConstructor()
407 bbSetInputOrientation("HORIZONTAL");
408 bbSetInputChangeResolution(false);
409 bbSetInputLabel(true);
410 bbSetInputReactiveOnTrack(false);
413 //--------------------------------------------------------------------------
414 void Slider::Process()
416 bbtkDebugMessage("process",3,
417 "Slider "<<bbGetName()<<" input="
418 <<bbGetInputIn()<<std::endl);
420 bbSetOutputOut( bbGetInputIn() );
421 if (bbGetOutputWidget()!=0)
423 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
427 void Slider::CreateWidget(wxWindow* parent)
431 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
432 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
435 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
436 SliderWidget *w = new SliderWidget(this,
437 parent, //bbGetWxParent(),
439 bbGetInputChangeResolution(),
441 bbtk::std2wx( bbGetInputTitle() ),
445 bbGetInputReactiveOnTrack()
447 // std::cout << "w = "<<w<<std::endl;
448 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
450 bbSetOutputWidget( w );
456 #endif // _USE_WXWIDGETS_