]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxSlider.cxx
Add CheckBox
[bbtk.git] / packages / wx / src / bbwxSlider.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbwxSlider.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/01/13 09:57:13 $
6   Version:   $Revision: 1.18 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  * \file 
33  * \brief Short description in one line
34  *
35  * Long 
36  * description
37  *  
38  */
39
40 #ifdef _USE_WXWIDGETS_
41
42 #include "bbwxSlider.h"
43 #include "bbwxPackage.h"
44 #include "bbtkUtilities.h"
45
46 namespace bbwx
47 {
48   //--------------------------------------------------------------------------
49   // The widget created by the box 
50   class SliderWidget : public wxPanel 
51   {
52   public:
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,
57                  int orientation,
58                  bool changeresolution,
59                  bool label, 
60                  wxString title,
61                  int vmin,
62                  int vmax,
63                  int value,
64          int track);
65     /// Dtor
66     ~SliderWidget();
67     /// Events callbacks
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);
75
76     // Accessors
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
80     void RefreshLabels();
81         
82   private:
83     Slider*    mBox;
84     wxSlider   *mwxSlider;
85     wxSlider   *mResolutionOfSlider;
86     int        min;
87     int        max;
88     int        reactiveOnTrack;
89     wxStaticText        *label_vertical;
90     wxStaticText        *label_min;
91     wxStaticText        *label_max;
92   };
93   //------------------------------------------------------------------------
94   //------------------------------------------------------------------------
95   //------------------------------------------------------------------------
96
97   
98     
99   //-------------------------------------------------------------------------
100   SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
101                              int orientation,
102                              bool changeresolution,
103                              bool labels,
104                              wxString title,
105                              int vmin,
106                              int vmax,
107                              int value,
108                              int reactiveontrack)
109     :  
110     wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
111     mBox(box),
112     min(vmin),
113     max(vmax),
114     reactiveOnTrack(reactiveontrack)
115   {
116     wxPanel * panel = this;
117     
118     label_min           = NULL;
119     label_max           = NULL;
120     label_vertical      = NULL;
121     mResolutionOfSlider = NULL;
122
123     int sizeX,sizeY;
124     long wxlabels=0;
125     long wxorientation=0;
126     if (orientation==0) 
127       {
128         sizeX                   = 2;
129         sizeY                   = 40;
130         wxorientation   = wxSL_HORIZONTAL;
131         if (labels==true)
132           {
133             wxlabels = wxSL_LABELS;
134           }
135       } 
136     else 
137       {
138         sizeX                   = 20;
139         sizeY                   = 2;
140         wxorientation   = wxSL_VERTICAL;
141       }
142     
143     //---------------------------------------------------------------------
144     // 1) Creation of the components of the widget
145     // Any top level sub-widget must have the panel returned by panel
146     // for parent
147     mwxSlider = new wxSlider( panel, 
148                               -1, 0 , 0, 500 , 
149                               wxDefaultPosition, 
150                               wxSize(sizeX,sizeY), 
151                               wxorientation  | wxlabels );
152
153     //    mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
154
155     //  mwxSlider->SetTickFreq(100,0);
156     mwxSlider->SetRange(min,max);
157     mwxSlider->SetValue(value);
158
159     //    RefreshLabels();
160
161     // Connecting events to callbacks
162     Connect( mwxSlider->GetId(), 
163              wxEVT_SCROLL_THUMBRELEASE, 
164              (wxObjectEventFunction) 
165              (void (wxPanel::*)(wxScrollEvent&))
166              &SliderWidget::OnSliderRelease ); 
167
168     Connect( mwxSlider->GetId(),  
169              wxEVT_SCROLL_THUMBTRACK, 
170              (wxObjectEventFunction) 
171              (void (wxPanel::*)(wxScrollEvent&))
172               &SliderWidget::OnSliderTrack ); 
173     
174     Connect( mwxSlider->GetId(),  
175              wxEVT_SCROLL_CHANGED, 
176              (wxObjectEventFunction) 
177              (void (wxPanel::*)(wxScrollEvent&))
178               &SliderWidget::OnSliderTrack );
179                
180     // If asked : creation of the other little slider which allows to change 
181     // the resolution of the main slider
182     if (changeresolution==true){
183       // has panel for parent too
184       mResolutionOfSlider = new wxSlider(panel,
185                                          -1,5,1,10,
186                                          wxDefaultPosition,
187                                          wxSize(25,45),
188                                          wxSL_VERTICAL | 
189                                          wxSL_AUTOTICKS |
190                                          wxSL_LEFT  );
191    
192
193       mResolutionOfSlider->SetRange(1,8);
194       mResolutionOfSlider->SetValue(5);
195       // Is wxEVT_COMMAND_SLIDER_UPDATED event 
196       // is connected to the method OnResolutionOfSlider
197       Connect(mResolutionOfSlider->GetId(),
198               wxEVT_COMMAND_SLIDER_UPDATED,
199               (wxObjectEventFunction) 
200               (void (wxPanel::*)(wxScrollEvent&))
201               &SliderWidget::OnResolutionOfSlider ); 
202     }
203     //---------------------------------------------------------------------
204
205     //---------------------------------------------------------------------
206     // 2) Insertion of the components in the window
207     
208     // We use a FlexGridSizer
209     wxFlexGridSizer *sizer;
210
211
212     if (orientation==0) 
213       {
214         // HORIZONTAL
215         if (mResolutionOfSlider!=NULL) 
216           {
217             sizer       = new wxFlexGridSizer(2);
218             if (title!=_T(""))
219               {
220                 sizer   -> Add( new wxStaticText(panel,-1, title ) ); 
221                 sizer   -> Add( new wxStaticText(panel,-1, _T("") ) ); 
222               }
223           } 
224         else 
225           {
226             sizer       = new wxFlexGridSizer(1);
227             if (title!=_T(""))
228               {
229                 sizer   -> Add( new wxStaticText(panel,-1, title ) ); 
230               }
231           }
232         sizer   -> Add( mwxSlider,1,wxGROW ); 
233         sizer   -> AddGrowableCol(0);
234         if (mResolutionOfSlider!=NULL) 
235           {
236             sizer       -> Add( mResolutionOfSlider ); 
237           }
238       } 
239     else 
240       {
241         // VERTICAL 
242         sizer   = new wxFlexGridSizer(1);
243         if (labels==true)  // with lable
244           {
245             label_vertical = new wxStaticText(panel,-1,_T(""));
246             label_min = new wxStaticText(panel,-1,_T(""));
247             label_max = new wxStaticText(panel,-1,_T(""));
248             if (title!=_T(""))
249               {
250                 sizer   -> Add( new wxStaticText(panel,-1, title ) );
251                 sizer   -> AddGrowableRow(3);
252               } 
253             else 
254               {
255                 sizer   -> AddGrowableRow(2);
256               }
257             sizer       -> Add( label_vertical );
258             sizer       -> Add( label_min );
259             sizer       -> Add( mwxSlider,1,wxGROW );
260             sizer       -> Add( label_max );
261             if (mResolutionOfSlider!=NULL) 
262               {
263                 sizer   -> Add( mResolutionOfSlider ); 
264               }
265           } 
266         else 
267           {
268             if (title!=_T(""))
269               {
270                 sizer   -> Add( new wxStaticText(panel,-1, title ) );
271                 sizer   -> AddGrowableRow(1);
272               } 
273             else 
274               {
275                 sizer   -> AddGrowableRow(0);
276               }
277             sizer       -> Add( mwxSlider,1,wxGROW );
278             if (mResolutionOfSlider!=NULL) 
279               {
280                 sizer   -> Add( mResolutionOfSlider ); 
281               }
282           }
283       }
284     // Initialize the labels 
285     RefreshLabels();  
286     // Insert the sizer in the main panel and refresh the layout
287     panel->SetSizer(sizer);
288   }
289   //-------------------------------------------------------------------------
290   
291
292   //-------------------------------------------------------------------------
293   SliderWidget::~SliderWidget()
294   {
295   }
296   //-------------------------------------------------------------------------
297
298
299   //-------------------------------------------------------------------------
300   void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
301   {
302     int value = mwxSlider->GetValue();
303     int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
304     int minTmp   = value - delta/2;
305     int maxTmp   = value + delta/2;
306     if (minTmp<min)
307       {
308         minTmp = min;
309         //              maxTmp = delta;
310       }
311     if (maxTmp>max)
312       {
313         maxTmp = max;
314       }
315     mwxSlider->SetRange(minTmp,maxTmp);
316     RefreshLabels();
317   }
318   //-------------------------------------------------------------------------
319
320
321
322   //-------------------------------------------------------------------------
323   void SliderWidget::OnSliderTrack(wxScrollEvent& event)
324   {
325     if(reactiveOnTrack)
326     {
327     // When user releases the slider 
328     // we update the output of the box
329     
330     mBox->bbSetOutputOut( mwxSlider->GetValue() );
331     mBox->bbSetInputIn( mwxSlider->GetValue() );
332     // and signal that the output has changed
333         mBox->bbSignalOutputModification(std::string("Out"));    
334     }
335     RefreshLabels();
336   }
337   //-------------------------------------------------------------------------
338
339
340   //-------------------------------------------------------------------------
341   void SliderWidget::OnSliderRelease(wxScrollEvent& event)
342   {
343     // When user releases the slider 
344     // we update the output of the box
345     mBox->bbSetOutputOut( mwxSlider->GetValue() );
346     mBox->bbSetInputIn( mwxSlider->GetValue() );
347     // and signal that the output has changed
348         mBox->bbSignalOutputModification(std::string("Out"));
349   }
350   //-------------------------------------------------------------------------
351   
352
353   //-------------------------------------------------------------------------
354   void SliderWidget::RefreshLabels()
355   {
356     wxString strValue;
357     if (label_vertical!=NULL)
358       {
359         strValue.Printf( _T("%d"), mwxSlider->GetValue() );
360         label_vertical->SetLabel(strValue);
361       }
362     if (label_min!=NULL)
363       {
364         strValue.Printf( _T("%d"), mwxSlider->GetMin() );
365         label_min->SetLabel(strValue);
366       }
367     if (label_max!=NULL)
368       {
369         strValue.Printf( _T("%d"), mwxSlider->GetMax() );
370         label_max->SetLabel(strValue);
371       }  
372   }
373   //-------------------------------------------------------------------------
374   
375
376   //-------------------------------------------------------------------------
377   void SliderWidget::SetRange(int min, int max)
378   {
379     this->min = min;
380     this->max = max;
381     mwxSlider->SetRange(min,max);
382     RefreshLabels();
383   }
384   //-------------------------------------------------------------------------
385
386  
387
388
389   //--------------------------------------------------------------------------
390   //-------------------------------------------------------------------------
391   // WxBlackBox implementation
392   //--------------------------------------------------------------------------
393   //--------------------------------------------------------------------------
394
395   //--------------------------------------------------------------------------
396   BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
397   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
398   
399   //--------------------------------------------------------------------------
400   void Slider::bbUserConstructor() 
401   { 
402     bbSetInputIn(0);
403     bbSetInputMin(0);
404     bbSetInputMax(500);
405     bbSetOutputOut(0);
406     bbSetInputOrientation("HORIZONTAL");
407     bbSetInputChangeResolution(false);
408     bbSetInputLabel(true);
409     bbSetInputReactiveOnTrack(0);    
410   }
411   
412   //--------------------------------------------------------------------------
413   void Slider::Process() 
414   {
415     bbSetOutputOut( bbGetInputIn() );
416   }
417
418   void Slider::CreateWidget(wxWindow* parent)
419   {
420
421     int orientation=0;
422     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true)  { orientation=0; }
423     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)    { orientation=1; }
424     
425     
426     //    std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
427     SliderWidget *w =  new SliderWidget(this, 
428                                         parent, //bbGetWxParent(),
429                                         orientation , 
430                                         bbGetInputChangeResolution(), 
431                                         bbGetInputLabel(), 
432                                         bbtk::std2wx( bbGetInputTitle() ),
433                                         bbGetInputMin(), 
434                                         bbGetInputMax(),
435                                         bbGetInputIn(),
436                                         bbGetInputReactiveOnTrack()
437                                         ); 
438     //    std::cout << "w = "<<w<<std::endl;
439     //  w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
440           
441     bbSetOutputWidget( w );
442   }
443   
444
445 } //namespace bbwx
446
447 #endif // _USE_WXWIDGETS_ 
448
449