]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxSlider.cxx
#3483 adaptor from string to long and long to string
[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         
307         
308   //-------------------------------------------------------------------------
309   SliderWidget::~SliderWidget()
310   {
311   }
312   //-------------------------------------------------------------------------
313
314
315   //-------------------------------------------------------------------------
316   void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
317   {
318     int value  = mwxSlider->GetValue();
319     int delta  = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
320     int minTmp = value - delta/2;
321     int maxTmp = value + delta/2;
322     if (minTmp<min)
323       {
324         minTmp = min;
325         //              maxTmp = delta;
326       }
327     if (maxTmp>max)
328       {
329         maxTmp = max;
330       }
331     mwxSlider->SetRange(minTmp,maxTmp);
332     RefreshLabels();
333   }
334   //-------------------------------------------------------------------------
335
336
337
338   //-------------------------------------------------------------------------
339   void SliderWidget::OnSliderTrack(wxScrollEvent& event)
340   {
341     if(reactiveOnTrack)
342     {
343     // When user releases the slider 
344     // we update the output of the box
345     
346     mBox->bbSetOutputOut( mwxSlider->GetValue() );
347     mBox->bbSetInputIn( mwxSlider->GetValue() );
348     // and signal that the output has changed
349     mBox->bbSignalOutputModification(std::string("Out"));    
350     }
351     RefreshLabels();
352   }
353   //-------------------------------------------------------------------------
354
355
356   //-------------------------------------------------------------------------
357   void SliderWidget::OnSliderRelease(wxScrollEvent& event)
358   {
359     // When user releases the slider 
360     // we update the output of the box
361     mBox->bbSetOutputOut( mwxSlider->GetValue() );
362     mBox->bbSetInputIn( mwxSlider->GetValue() );
363     // and signal that the output has changed
364     mBox->bbSignalOutputModification(std::string("Out"));
365   }
366   //-------------------------------------------------------------------------
367   
368
369   //-------------------------------------------------------------------------
370   void SliderWidget::RefreshLabels()
371   {
372     wxString strValue;
373     if (label_vertical!=NULL)
374       {
375         strValue.Printf( _T("%d"), mwxSlider->GetValue() );
376         label_vertical->SetLabel(strValue);
377       }
378     if (label_min!=NULL)
379       {
380         strValue.Printf( _T("%d"), mwxSlider->GetMin() );
381         label_min->SetLabel(strValue);
382       }
383     if (label_max!=NULL)
384       {
385         strValue.Printf( _T("%d"), mwxSlider->GetMax() );
386         label_max->SetLabel(strValue);
387       }  
388   }
389   //-------------------------------------------------------------------------
390   
391
392   //-------------------------------------------------------------------------
393   void SliderWidget::SetRange(int min, int max)
394   {
395     this->min = min;
396     this->max = max;
397     mwxSlider->SetRange(min,max);
398     RefreshLabels();
399   }
400         
401         
402         //-------------------------------------------------------------------------
403         void SliderWidget::SetReactiveOnTrack(bool ok)
404         {
405                 reactiveOnTrack = ok;
406         }
407         
408   //-------------------------------------------------------------------------
409
410  
411
412
413   //--------------------------------------------------------------------------
414   //-------------------------------------------------------------------------
415   // WxBlackBox implementation
416   //--------------------------------------------------------------------------
417   //--------------------------------------------------------------------------
418
419   //--------------------------------------------------------------------------
420   BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
421   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
422   
423         //-----------------------------------------------------------------     
424         void Slider::bbUserSetDefaultValues()
425         {
426                 bbSetInputIn(0);
427                 bbSetInputMin(0);
428                 bbSetInputMax(500);
429                 bbSetOutputOut(0);
430                 bbSetInputOrientation("HORIZONTAL");
431                 bbSetInputChangeResolution(false);
432                 bbSetInputLabel(true);
433                 bbSetInputReactiveOnTrack(false);    
434         }
435         
436         //-----------------------------------------------------------------     
437         void Slider::bbUserInitializeProcessing()
438         {
439         }
440         
441         //-----------------------------------------------------------------     
442         void Slider::bbUserFinalizeProcessing()
443         {
444         }       
445   
446   //--------------------------------------------------------------------------
447   void Slider::Process() 
448   {
449     bbtkDebugMessage("process",3,
450                      "Slider "<<bbGetName()<<" input="
451                      <<bbGetInputIn()<<std::endl);
452
453 // desperate try // JPR
454       if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() ||  bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )       
455       {    
456                   ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
457       }
458
459           ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
460           
461     bbSetOutputOut( bbGetInputIn() );
462     if (bbGetOutputWidget()!=0)
463       {
464         ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
465       }
466   }
467
468 //--------------------------------------------------------------------------
469   void Slider::CreateWidget(wxWindow* parent)
470   {
471     int orientation=0;
472     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true)  { orientation=0; }
473     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)    { orientation=1; }
474     //    std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
475     SliderWidget *w =  new SliderWidget(this, 
476                                         parent, //bbGetWxParent(),
477                                         orientation , 
478                                         bbGetInputChangeResolution(), 
479                                         bbGetInputLabel(), 
480                                         bbtk::std2wx( bbGetInputTitle() ),
481                                         bbGetInputMin(), 
482                                         bbGetInputMax(),
483                                         bbGetInputIn(),
484                                         bbGetInputReactiveOnTrack()
485                                         ); 
486     //    std::cout << "w = "<<w<<std::endl;
487     //  w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );       
488     bbSetOutputWidget( w );
489   }
490   
491
492 } //namespace bbwx
493
494 #endif // _USE_WXWIDGETS_ 
495
496