1 /*=========================================================================
3 Module: $RCSfile: bbwxSlider.cxx,v $
5 Date: $Date: 2008/12/11 15:30:14 $
6 Version: $Revision: 1.16 $
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 int GetValue() { return mwxSlider->GetValue(); }
78 void SetRange(int min, int max);
79 // Update the texts which display the min/max/current values of the slider
85 wxSlider *mResolutionOfSlider;
89 wxStaticText *label_vertical;
90 wxStaticText *label_min;
91 wxStaticText *label_max;
93 //------------------------------------------------------------------------
94 //------------------------------------------------------------------------
95 //------------------------------------------------------------------------
99 //-------------------------------------------------------------------------
100 SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
102 bool changeresolution,
110 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
114 reactiveOnTrack(reactiveontrack)
116 wxPanel * panel = this;
120 label_vertical = NULL;
121 mResolutionOfSlider = NULL;
125 long wxorientation=0;
130 wxorientation = wxSL_HORIZONTAL;
133 wxlabels = wxSL_LABELS;
140 wxorientation = wxSL_VERTICAL;
143 //---------------------------------------------------------------------
144 // 1) Creation of the components of the widget
145 // Any top level sub-widget must have the panel returned by panel
147 mwxSlider = new wxSlider( panel,
151 wxorientation | wxlabels );
153 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
155 // mwxSlider->SetTickFreq(100,0);
156 mwxSlider->SetRange(min,max);
157 mwxSlider->SetValue(value);
161 // Connecting events to callbacks
162 Connect( mwxSlider->GetId(),
163 wxEVT_SCROLL_THUMBRELEASE,
165 (wxObjectEventFunction)
166 (void (wxPanel::*)(wxScrollEvent&))
167 &SliderWidget::OnSliderRelease );
169 Connect( mwxSlider->GetId(),
170 wxEVT_SCROLL_THUMBTRACK,
172 (wxObjectEventFunction)
173 (void (wxPanel::*)(wxScrollEvent&))
174 &SliderWidget::OnSliderTrack );
176 Connect( mwxSlider->GetId(),
177 wxEVT_SCROLL_CHANGED,
179 (wxObjectEventFunction)
180 (void (wxPanel::*)(wxScrollEvent&))
181 &SliderWidget::OnSliderTrack );
183 // If asked : creation of the other little slider which allows to change
184 // the resolution of the main slider
185 if (changeresolution==true){
186 // has panel for parent too
187 mResolutionOfSlider = new wxSlider(panel,
196 mResolutionOfSlider->SetRange(1,8);
197 mResolutionOfSlider->SetValue(5);
198 // Is wxEVT_COMMAND_SLIDER_UPDATED event
199 // is connected to the method OnResolutionOfSlider
200 Connect(mResolutionOfSlider->GetId(),
201 wxEVT_COMMAND_SLIDER_UPDATED,
202 (wxObjectEventFunction)
203 (void (wxPanel::*)(wxScrollEvent&))
204 &SliderWidget::OnResolutionOfSlider );
206 //---------------------------------------------------------------------
208 //---------------------------------------------------------------------
209 // 2) Insertion of the components in the window
211 // We use a FlexGridSizer
212 wxFlexGridSizer *sizer;
218 if (mResolutionOfSlider!=NULL)
220 sizer = new wxFlexGridSizer(2);
223 sizer -> Add( new wxStaticText(panel,-1, title ) );
224 sizer -> Add( new wxStaticText(panel,-1, _T("") ) );
229 sizer = new wxFlexGridSizer(1);
232 sizer -> Add( new wxStaticText(panel,-1, title ) );
235 sizer -> Add( mwxSlider,1,wxGROW );
236 sizer -> AddGrowableCol(0);
237 if (mResolutionOfSlider!=NULL)
239 sizer -> Add( mResolutionOfSlider );
245 sizer = new wxFlexGridSizer(1);
246 if (labels==true) // with lable
248 label_vertical = new wxStaticText(panel,-1,_T(""));
249 label_min = new wxStaticText(panel,-1,_T(""));
250 label_max = new wxStaticText(panel,-1,_T(""));
253 sizer -> Add( new wxStaticText(panel,-1, title ) );
254 sizer -> AddGrowableRow(3);
258 sizer -> AddGrowableRow(2);
260 sizer -> Add( label_vertical );
261 sizer -> Add( label_min );
262 sizer -> Add( mwxSlider,1,wxGROW );
263 sizer -> Add( label_max );
264 if (mResolutionOfSlider!=NULL)
266 sizer -> Add( mResolutionOfSlider );
273 sizer -> Add( new wxStaticText(panel,-1, title ) );
274 sizer -> AddGrowableRow(1);
278 sizer -> AddGrowableRow(0);
280 sizer -> Add( mwxSlider,1,wxGROW );
281 if (mResolutionOfSlider!=NULL)
283 sizer -> Add( mResolutionOfSlider );
287 // Initialize the labels
289 // Insert the sizer in the main panel and refresh the layout
290 panel->SetSizer(sizer);
292 //-------------------------------------------------------------------------
295 //-------------------------------------------------------------------------
296 SliderWidget::~SliderWidget()
299 //-------------------------------------------------------------------------
302 //-------------------------------------------------------------------------
303 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
305 int value = mwxSlider->GetValue();
306 int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
307 int minTmp = value - delta/2;
308 int maxTmp = value + delta/2;
318 mwxSlider->SetRange(minTmp,maxTmp);
321 //-------------------------------------------------------------------------
325 //-------------------------------------------------------------------------
326 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
330 // When user releases the slider
331 // we update the output of the box
333 mBox->bbSetOutputOut( mwxSlider->GetValue() );
334 mBox->bbSetInputIn( mwxSlider->GetValue() );
335 // and signal that the output has changed
336 mBox->bbSignalOutputModification("Out");
340 //-------------------------------------------------------------------------
343 //-------------------------------------------------------------------------
344 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
346 // When user releases the slider
347 // we update the output of the box
348 mBox->bbSetOutputOut( mwxSlider->GetValue() );
349 mBox->bbSetInputIn( mwxSlider->GetValue() );
350 // and signal that the output has changed
351 mBox->bbSignalOutputModification("Out");
353 //-------------------------------------------------------------------------
356 //-------------------------------------------------------------------------
357 void SliderWidget::RefreshLabels()
360 if (label_vertical!=NULL)
362 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
363 label_vertical->SetLabel(strValue);
367 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
368 label_min->SetLabel(strValue);
372 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
373 label_max->SetLabel(strValue);
376 //-------------------------------------------------------------------------
379 //-------------------------------------------------------------------------
380 void SliderWidget::SetRange(int min, int max)
384 mwxSlider->SetRange(min,max);
387 //-------------------------------------------------------------------------
392 //--------------------------------------------------------------------------
393 //-------------------------------------------------------------------------
394 // WxBlackBox implementation
395 //--------------------------------------------------------------------------
396 //--------------------------------------------------------------------------
398 //--------------------------------------------------------------------------
399 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
400 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
402 //--------------------------------------------------------------------------
403 void Slider::bbUserConstructor()
409 bbSetInputOrientation("HORIZONTAL");
410 bbSetInputChangeResolution(false);
411 bbSetInputLabel(true);
412 bbSetInputReactiveOnTrack(0);
415 //--------------------------------------------------------------------------
416 void Slider::Process()
418 bbSetOutputOut( bbGetInputIn() );
421 void Slider::CreateWidget(wxWindow* parent)
425 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
426 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
429 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
430 SliderWidget *w = new SliderWidget(this,
431 parent, //bbGetWxParent(),
433 bbGetInputChangeResolution(),
435 bbtk::std2wx( bbGetInputTitle() ),
439 bbGetInputReactiveOnTrack()
441 // std::cout << "w = "<<w<<std::endl;
442 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
444 bbSetOutputWidget( w );
450 #endif // _USE_WXWIDGETS_