1 /*=========================================================================
3 Module: $RCSfile: bbwxSlider.cxx,v $
5 Date: $Date: 2008/11/24 15:45:51 $
6 Version: $Revision: 1.15 $
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"
49 //-------------------------------------------------------------------------
50 SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
52 bool changeresolution,
60 wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
64 reactiveOnTrack(reactiveontrack)
66 wxPanel * panel = this;
70 label_vertical = NULL;
71 mResolutionOfSlider = NULL;
80 wxorientation = wxSL_HORIZONTAL;
83 wxlabels = wxSL_LABELS;
90 wxorientation = wxSL_VERTICAL;
93 //---------------------------------------------------------------------
94 // 1) Creation of the components of the widget
95 // Any top level sub-widget must have the panel returned by panel
97 mwxSlider = new wxSlider( panel,
101 wxorientation | wxlabels );
103 // mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
105 // mwxSlider->SetTickFreq(100,0);
106 mwxSlider->SetRange(min,max);
107 mwxSlider->SetValue(value);
111 // Connecting events to callbacks
112 Connect( mwxSlider->GetId(),
113 wxEVT_SCROLL_THUMBRELEASE,
115 (wxObjectEventFunction)
116 (void (wxPanel::*)(wxScrollEvent&))
117 &SliderWidget::OnSliderRelease );
119 Connect( mwxSlider->GetId(),
120 wxEVT_SCROLL_THUMBTRACK,
122 (wxObjectEventFunction)
123 (void (wxPanel::*)(wxScrollEvent&))
124 &SliderWidget::OnSliderTrack );
126 Connect( mwxSlider->GetId(),
127 wxEVT_SCROLL_CHANGED,
129 (wxObjectEventFunction)
130 (void (wxPanel::*)(wxScrollEvent&))
131 &SliderWidget::OnSliderTrack );
133 // If asked : creation of the other little slider which allows to change
134 // the resolution of the main slider
135 if (changeresolution==true){
136 // has panel for parent too
137 mResolutionOfSlider = new wxSlider(panel,
146 mResolutionOfSlider->SetRange(1,8);
147 mResolutionOfSlider->SetValue(5);
148 // Is wxEVT_COMMAND_SLIDER_UPDATED event
149 // is connected to the method OnResolutionOfSlider
150 Connect(mResolutionOfSlider->GetId(),
151 wxEVT_COMMAND_SLIDER_UPDATED,
152 (wxObjectEventFunction)
153 (void (wxPanel::*)(wxScrollEvent&))
154 &SliderWidget::OnResolutionOfSlider );
156 //---------------------------------------------------------------------
158 //---------------------------------------------------------------------
159 // 2) Insertion of the components in the window
161 // We use a FlexGridSizer
162 wxFlexGridSizer *sizer;
168 if (mResolutionOfSlider!=NULL)
170 sizer = new wxFlexGridSizer(2);
173 sizer -> Add( new wxStaticText(panel,-1, title ) );
174 sizer -> Add( new wxStaticText(panel,-1, _T("") ) );
179 sizer = new wxFlexGridSizer(1);
182 sizer -> Add( new wxStaticText(panel,-1, title ) );
185 sizer -> Add( mwxSlider,1,wxGROW );
186 sizer -> AddGrowableCol(0);
187 if (mResolutionOfSlider!=NULL)
189 sizer -> Add( mResolutionOfSlider );
195 sizer = new wxFlexGridSizer(1);
196 if (labels==true) // with lable
198 label_vertical = new wxStaticText(panel,-1,_T(""));
199 label_min = new wxStaticText(panel,-1,_T(""));
200 label_max = new wxStaticText(panel,-1,_T(""));
203 sizer -> Add( new wxStaticText(panel,-1, title ) );
204 sizer -> AddGrowableRow(3);
208 sizer -> AddGrowableRow(2);
210 sizer -> Add( label_vertical );
211 sizer -> Add( label_min );
212 sizer -> Add( mwxSlider,1,wxGROW );
213 sizer -> Add( label_max );
214 if (mResolutionOfSlider!=NULL)
216 sizer -> Add( mResolutionOfSlider );
223 sizer -> Add( new wxStaticText(panel,-1, title ) );
224 sizer -> AddGrowableRow(1);
228 sizer -> AddGrowableRow(0);
230 sizer -> Add( mwxSlider,1,wxGROW );
231 if (mResolutionOfSlider!=NULL)
233 sizer -> Add( mResolutionOfSlider );
237 // Initialize the labels
239 // Insert the sizer in the main panel and refresh the layout
240 panel->SetSizer(sizer);
242 //-------------------------------------------------------------------------
245 //-------------------------------------------------------------------------
246 SliderWidget::~SliderWidget()
249 //-------------------------------------------------------------------------
252 //-------------------------------------------------------------------------
253 void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
255 int value = mwxSlider->GetValue();
256 int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
257 int minTmp = value - delta/2;
258 int maxTmp = value + delta/2;
268 mwxSlider->SetRange(minTmp,maxTmp);
271 //-------------------------------------------------------------------------
275 //-------------------------------------------------------------------------
276 void SliderWidget::OnSliderTrack(wxScrollEvent& event)
280 // When user releases the slider
281 // we update the output of the box
283 mBox->bbSetOutputOut( mwxSlider->GetValue() );
284 mBox->bbSetInputIn( mwxSlider->GetValue() );
285 // and signal that the output has changed
286 mBox->bbSignalOutputModification("Out");
290 //-------------------------------------------------------------------------
293 //-------------------------------------------------------------------------
294 void SliderWidget::OnSliderRelease(wxScrollEvent& event)
296 // When user releases the slider
297 // we update the output of the box
298 mBox->bbSetOutputOut( mwxSlider->GetValue() );
299 mBox->bbSetInputIn( mwxSlider->GetValue() );
300 // and signal that the output has changed
301 mBox->bbSignalOutputModification("Out");
303 //-------------------------------------------------------------------------
306 //-------------------------------------------------------------------------
307 void SliderWidget::RefreshLabels()
310 if (label_vertical!=NULL)
312 strValue.Printf( _T("%d"), mwxSlider->GetValue() );
313 label_vertical->SetLabel(strValue);
317 strValue.Printf( _T("%d"), mwxSlider->GetMin() );
318 label_min->SetLabel(strValue);
322 strValue.Printf( _T("%d"), mwxSlider->GetMax() );
323 label_max->SetLabel(strValue);
326 //-------------------------------------------------------------------------
329 //-------------------------------------------------------------------------
330 void SliderWidget::SetRange(int min, int max)
334 mwxSlider->SetRange(min,max);
337 //-------------------------------------------------------------------------
342 //--------------------------------------------------------------------------
343 //-------------------------------------------------------------------------
344 // WxBlackBox implementation
345 //--------------------------------------------------------------------------
346 //--------------------------------------------------------------------------
348 //--------------------------------------------------------------------------
349 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
350 BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
352 //--------------------------------------------------------------------------
353 void Slider::bbUserConstructor()
359 bbSetInputOrientation("HORIZONTAL");
360 bbSetInputChangeResolution(false);
361 bbSetInputLabel(true);
362 bbSetInputReactiveOnTrack(0);
365 //--------------------------------------------------------------------------
366 void Slider::Process()
368 bbSetOutputOut( bbGetInputIn() );
371 void Slider::CreateWidget(wxWindow* parent)
375 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) { orientation=0; }
376 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true) { orientation=1; }
379 // std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
380 SliderWidget *w = new SliderWidget(this,
381 parent, //bbGetWxParent(),
383 bbGetInputChangeResolution(),
385 bbtk::std2wx( bbGetInputTitle() ),
389 bbGetInputReactiveOnTrack()
391 // std::cout << "w = "<<w<<std::endl;
392 // w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
394 bbSetOutputWidget( w );
400 #endif // _USE_WXWIDGETS_