]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxSlider.cxx
Clean code
[bbtk.git] / packages / wx / src / bbwxSlider.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbwxSlider.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:52:14 $
33   Version:   $Revision: 1.27 $
34 =========================================================================*/
35
36 /**
37  * \file 
38  * \brief Short description in one line
39  *
40  * Long 
41  * description
42  *  
43  */
44
45 #ifdef _USE_WXWIDGETS_
46
47 #include "bbwxSlider.h"
48 #include "bbwxPackage.h"
49 #include "bbtkUtilities.h"
50
51 namespace bbwx
52 {
53   //--------------------------------------------------------------------------
54   // The widget created by the box 
55   class SliderWidget : public wxPanel 
56   {
57   public:
58     /// Ctor with the two first params the parent window and the creator box
59     /// which must be passed to the WxBlackBoxWidget constructor.
60     /// The other params initialize the widget 
61     SliderWidget(Slider* box, wxWindow *parent,
62                  int orientation,
63                  bool changeresolution,
64                  bool label, 
65                  wxString title,
66                  int vmin,
67                  int vmax,
68                  int value,
69                  bool track);
70     /// Dtor
71     ~SliderWidget();
72     /// Events callbacks
73     /// Called when the slider is moved
74     void OnSliderTrack(wxScrollEvent& event);
75     /// Called when the slider is released
76     void OnSliderRelease(wxScrollEvent& event);
77     /// Called when the little slider which controls the resolution
78     /// of the main slider is moved (if activated)
79     void OnResolutionOfSlider(wxScrollEvent& event);
80
81     // Accessors
82     void SetValue(int v) { mwxSlider->SetValue(v); }
83     int  GetValue() { return mwxSlider->GetValue(); }
84     int GetMin() {return min;}
85     int GetMax() {return max;}    
86     void SetRange(int min, int max);
87     // Update the texts which display the min/max/current values of the slider
88     void RefreshLabels();
89
90         void SetReactiveOnTrack(bool ok);
91
92           
93   private:
94     Slider*      mBox;
95     wxSlider     *mwxSlider;
96     wxSlider     *mResolutionOfSlider;
97     int          min;
98     int          max;
99     bool         reactiveOnTrack;
100     wxStaticText *label_vertical;
101     wxStaticText *label_min;
102     wxStaticText *label_max;
103   };
104   //------------------------------------------------------------------------
105   //------------------------------------------------------------------------
106   //------------------------------------------------------------------------
107
108   
109     
110   //-------------------------------------------------------------------------
111   SliderWidget::SliderWidget(Slider* box, wxWindow *parent,
112                              int orientation,
113                              bool changeresolution,
114                              bool labels,
115                              wxString title,
116                              int vmin,
117                              int vmax,
118                              int value,
119                              bool reactiveontrack)
120     :  
121     wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
122     mBox(box),
123     min(vmin),
124     max(vmax),
125     reactiveOnTrack(reactiveontrack)
126   {
127     wxPanel * panel = this;
128     
129     label_min           = NULL;
130     label_max           = NULL;
131     label_vertical      = NULL;
132     mResolutionOfSlider = NULL;
133
134     int sizeX,sizeY;
135     long wxlabels=0;
136     long wxorientation=0;
137     if (orientation==0) 
138       {
139         sizeX                   = 2; 
140         sizeY                   = 40;
141         wxorientation           = wxSL_HORIZONTAL;
142         if (labels==true)
143           {
144             wxlabels = wxSL_LABELS;
145           }
146       } 
147     else 
148       {
149         sizeX           = 20;
150         sizeY           = 2;
151         wxorientation   = wxSL_VERTICAL;
152       }
153     
154       
155     wxSize  sizeSliderWidget(25,25);
156 //  int  widthSliderWidget = wxDefaultSize;
157       
158     //---------------------------------------------------------------------
159     // 1) Creation of the components of the widget
160     // Any top level sub-widget must have the panel returned by panel
161     // for parent
162     mwxSlider = new wxSlider( panel, 
163                               -1, 0 , 0, 500 , 
164                               wxDefaultPosition, 
165                   sizeSliderWidget, wxSL_HORIZONTAL|wxSL_LABELS, wxDefaultValidator);
166
167
168     //    mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
169
170     //  mwxSlider->SetTickFreq(100,0);
171     mwxSlider->SetRange(min,max);
172     mwxSlider->SetValue(value);
173
174     //    RefreshLabels();
175
176     // Connecting events to callbacks
177     Connect( mwxSlider->GetId(), 
178              wxEVT_SCROLL_THUMBRELEASE, 
179              (wxObjectEventFunction) 
180              (void (wxPanel::*)(wxScrollEvent&))
181              &SliderWidget::OnSliderRelease ); 
182
183     Connect( mwxSlider->GetId(),  
184              wxEVT_SCROLL_THUMBTRACK, 
185              (wxObjectEventFunction) 
186              (void (wxPanel::*)(wxScrollEvent&))
187               &SliderWidget::OnSliderTrack ); 
188     
189     Connect( mwxSlider->GetId(),  
190              wxEVT_SCROLL_CHANGED, 
191              (wxObjectEventFunction) 
192              (void (wxPanel::*)(wxScrollEvent&))
193               &SliderWidget::OnSliderTrack );
194                
195     // If asked : creation of the other little slider which allows to change 
196     // the resolution of the main slider
197     if (changeresolution==true){
198       // has panel for parent too
199       mResolutionOfSlider = new wxSlider(panel,
200                                          -1,5,1,10,
201                                          wxDefaultPosition,
202                                          wxSize(25,45),
203                                          wxSL_VERTICAL | 
204                                          wxSL_AUTOTICKS |
205                                          wxSL_LEFT  );
206    
207
208       mResolutionOfSlider->SetRange(1,8);
209       mResolutionOfSlider->SetValue(5);
210       // Is wxEVT_COMMAND_SLIDER_UPDATED event 
211       // is connected to the method OnResolutionOfSlider
212       Connect(mResolutionOfSlider->GetId(),
213               wxEVT_COMMAND_SLIDER_UPDATED,
214               (wxObjectEventFunction) 
215               (void (wxPanel::*)(wxScrollEvent&))
216               &SliderWidget::OnResolutionOfSlider ); 
217     }
218     //---------------------------------------------------------------------
219
220     //---------------------------------------------------------------------
221     // 2) Insertion of the components in the window
222     
223     // We use a FlexGridSizer
224     wxFlexGridSizer *sizer;
225
226
227     if (orientation==0) 
228       {
229         // HORIZONTAL
230         if (mResolutionOfSlider!=NULL) 
231           {
232             sizer       = new wxFlexGridSizer(2);
233             if (title!=_T(""))
234               {
235                 sizer->Add( new wxStaticText(panel,-1, title ) ); 
236                 sizer->Add( new wxStaticText(panel,-1, _T("") ) ); 
237               }
238           } 
239         else 
240           {
241             sizer       = new wxFlexGridSizer(1);
242             if (title!=_T(""))
243               {
244                 sizer->Add( new wxStaticText(panel,-1, title ) ); 
245               }
246           }
247         sizer->Add( mwxSlider,1,wxGROW ); 
248         sizer->AddGrowableCol(0);
249         if (mResolutionOfSlider!=NULL) 
250           {
251             sizer->Add( mResolutionOfSlider ); 
252           }
253       } 
254     else 
255       {
256         // VERTICAL 
257         sizer   = new wxFlexGridSizer(1);
258         if (labels==true)  // with lable
259           {
260             label_vertical = new wxStaticText(panel,-1,_T(""));
261             label_min = new wxStaticText(panel,-1,_T(""));
262             label_max = new wxStaticText(panel,-1,_T(""));
263             if (title!=_T(""))
264               {
265                 sizer->Add( new wxStaticText(panel,-1, title ) );
266                 sizer->AddGrowableRow(3);
267               } 
268             else 
269               {
270                 sizer->AddGrowableRow(2);
271               }
272             sizer->Add( label_vertical );
273             sizer->Add( label_min );
274             sizer->Add( mwxSlider,1,wxGROW );
275             sizer->Add( label_max );
276             if (mResolutionOfSlider!=NULL) 
277               {
278                 sizer->Add( mResolutionOfSlider ); 
279               }
280           } 
281         else 
282           {
283             if (title!=_T(""))
284               {
285                 sizer->Add( new wxStaticText(panel,-1, title ) );
286                 sizer->AddGrowableRow(1);
287               } 
288             else 
289               {
290                 sizer->AddGrowableRow(0);
291               }
292             sizer->Add( mwxSlider,1,wxGROW );
293             if (mResolutionOfSlider!=NULL) 
294               {
295                 sizer->Add( mResolutionOfSlider ); 
296               }
297           }
298       }
299     // Initialize the labels 
300     RefreshLabels();  
301     // Insert the sizer in the main panel and refresh the layout
302     panel->SetSizer(sizer);
303   }
304         
305   //-------------------------------------------------------------------------
306   SliderWidget::~SliderWidget()
307   {
308   }
309
310   //-------------------------------------------------------------------------
311   void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
312   {
313     int value  = mwxSlider->GetValue();
314     int delta  = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
315     int minTmp = value - delta/2;
316     int maxTmp = value + delta/2;
317     if (minTmp<min)
318     {
319         minTmp = min;
320         //              maxTmp = delta;
321     }
322     if (maxTmp>max)
323     {
324         maxTmp = max;
325     }
326     mwxSlider->SetRange(minTmp,maxTmp);
327     RefreshLabels();
328   }
329
330 //-------------------------------------------------------------------------
331   void SliderWidget::OnSliderTrack(wxScrollEvent& event)
332   {
333     if(reactiveOnTrack)
334     {
335     // When user releases the slider 
336     // we update the output of the box
337     
338     mBox->bbSetOutputOut( mwxSlider->GetValue() );
339     mBox->bbSetInputIn( mwxSlider->GetValue() );
340     // and signal that the output has changed
341     mBox->bbSignalOutputModification(std::string("Out"));    
342     }
343     RefreshLabels();
344   }
345
346 //-------------------------------------------------------------------------
347   void SliderWidget::OnSliderRelease(wxScrollEvent& event)
348   {
349     // When user releases the slider 
350     // we update the output of the box
351     mBox->bbSetOutputOut( mwxSlider->GetValue() );
352     mBox->bbSetInputIn( mwxSlider->GetValue() );
353     // and signal that the output has changed
354     mBox->bbSignalOutputModification(std::string("Out"));
355   }
356   //-------------------------------------------------------------------------
357   
358
359   //-------------------------------------------------------------------------
360   void SliderWidget::RefreshLabels()
361   {
362     wxString strValue;
363     if (label_vertical!=NULL)
364       {
365         strValue.Printf( _T("%d"), mwxSlider->GetValue() );
366         label_vertical->SetLabel(strValue);
367       }
368     if (label_min!=NULL)
369       {
370         strValue.Printf( _T("%d"), mwxSlider->GetMin() );
371         label_min->SetLabel(strValue);
372       }
373     if (label_max!=NULL)
374       {
375         strValue.Printf( _T("%d"), mwxSlider->GetMax() );
376         label_max->SetLabel(strValue);
377       }  
378   }
379   //-------------------------------------------------------------------------
380   
381
382   //-------------------------------------------------------------------------
383   void SliderWidget::SetRange(int min, int max)
384   {
385     this->min = min;
386     this->max = max;
387     mwxSlider->SetRange(min,max);
388     RefreshLabels();
389   }
390         
391         
392         //-------------------------------------------------------------------------
393         void SliderWidget::SetReactiveOnTrack(bool ok)
394         {
395                 reactiveOnTrack = ok;
396         }
397         
398   //-------------------------------------------------------------------------
399
400  
401
402
403   //--------------------------------------------------------------------------
404   //-------------------------------------------------------------------------
405   // WxBlackBox implementation
406   //--------------------------------------------------------------------------
407   //--------------------------------------------------------------------------
408
409   //--------------------------------------------------------------------------
410   BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
411   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
412   
413         //-----------------------------------------------------------------     
414         void Slider::bbUserSetDefaultValues()
415         {
416                 bbSetInputIn(0);
417                 bbSetInputMin(0);
418                 bbSetInputMax(500);
419                 bbSetOutputOut(0);
420                 bbSetInputOrientation("HORIZONTAL");
421                 bbSetInputChangeResolution(false);
422                 bbSetInputLabel(true);
423                 bbSetInputReactiveOnTrack(false);    
424         }
425         
426         //-----------------------------------------------------------------     
427         void Slider::bbUserInitializeProcessing()
428         {
429         }
430         
431         //-----------------------------------------------------------------     
432         void Slider::bbUserFinalizeProcessing()
433         {
434         }       
435   
436   //--------------------------------------------------------------------------
437   void Slider::Process() 
438   {
439     bbtkDebugMessage("process",3,
440                      "Slider "<<bbGetName()<<" input="
441                      <<bbGetInputIn()<<std::endl);
442     
443     if (bbGetOutputWidget()!=NULL)
444         {
445 // desperate try // JPR
446         if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() ||  bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )       
447         {    
448                         ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
449         }
450
451                 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
452           
453         bbSetOutputOut( bbGetInputIn() );
454                 ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
455         } // if bbGetOutputWidget
456   }
457
458 //--------------------------------------------------------------------------
459   void Slider::CreateWidget(wxWindow* parent)
460   {
461     int orientation=0;
462     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true)  { orientation=0; }
463     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)    { orientation=1; }
464     //    std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
465     SliderWidget *w =  new SliderWidget(this, 
466                                         parent, //bbGetWxParent(),
467                                         orientation , 
468                                         bbGetInputChangeResolution(), 
469                                         bbGetInputLabel(), 
470                                         bbtk::std2wx( bbGetInputTitle() ),
471                                         bbGetInputMin(), 
472                                         bbGetInputMax(),
473                                         bbGetInputIn(),
474                                         bbGetInputReactiveOnTrack()
475                                         ); 
476     //    std::cout << "w = "<<w<<std::endl;
477     //  w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );       
478         bbSetOutputOut( bbGetInputIn() );
479     bbSetOutputWidget( w );
480   }
481   
482
483 } //namespace bbwx
484
485 #endif // _USE_WXWIDGETS_ 
486
487