]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxSlider.cxx
Feature #1774
[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     // 1) Creation of the components of the widget
156     // Any top level sub-widget must have the panel returned by panel
157     // for parent
158     mwxSlider = new wxSlider( panel, 
159                               -1, 0 , 0, 500 , 
160                               wxDefaultPosition, 
161                               wxSize(sizeX,sizeY), 
162                               wxorientation  | wxlabels );
163
164     //    mwxSlider->SetInitialSize(wxSize(sizeX,sizeY));
165
166     //  mwxSlider->SetTickFreq(100,0);
167     mwxSlider->SetRange(min,max);
168     mwxSlider->SetValue(value);
169
170     //    RefreshLabels();
171
172     // Connecting events to callbacks
173     Connect( mwxSlider->GetId(), 
174              wxEVT_SCROLL_THUMBRELEASE, 
175              (wxObjectEventFunction) 
176              (void (wxPanel::*)(wxScrollEvent&))
177              &SliderWidget::OnSliderRelease ); 
178
179     Connect( mwxSlider->GetId(),  
180              wxEVT_SCROLL_THUMBTRACK, 
181              (wxObjectEventFunction) 
182              (void (wxPanel::*)(wxScrollEvent&))
183               &SliderWidget::OnSliderTrack ); 
184     
185     Connect( mwxSlider->GetId(),  
186              wxEVT_SCROLL_CHANGED, 
187              (wxObjectEventFunction) 
188              (void (wxPanel::*)(wxScrollEvent&))
189               &SliderWidget::OnSliderTrack );
190                
191     // If asked : creation of the other little slider which allows to change 
192     // the resolution of the main slider
193     if (changeresolution==true){
194       // has panel for parent too
195       mResolutionOfSlider = new wxSlider(panel,
196                                          -1,5,1,10,
197                                          wxDefaultPosition,
198                                          wxSize(25,45),
199                                          wxSL_VERTICAL | 
200                                          wxSL_AUTOTICKS |
201                                          wxSL_LEFT  );
202    
203
204       mResolutionOfSlider->SetRange(1,8);
205       mResolutionOfSlider->SetValue(5);
206       // Is wxEVT_COMMAND_SLIDER_UPDATED event 
207       // is connected to the method OnResolutionOfSlider
208       Connect(mResolutionOfSlider->GetId(),
209               wxEVT_COMMAND_SLIDER_UPDATED,
210               (wxObjectEventFunction) 
211               (void (wxPanel::*)(wxScrollEvent&))
212               &SliderWidget::OnResolutionOfSlider ); 
213     }
214     //---------------------------------------------------------------------
215
216     //---------------------------------------------------------------------
217     // 2) Insertion of the components in the window
218     
219     // We use a FlexGridSizer
220     wxFlexGridSizer *sizer;
221
222
223     if (orientation==0) 
224       {
225         // HORIZONTAL
226         if (mResolutionOfSlider!=NULL) 
227           {
228             sizer       = new wxFlexGridSizer(2);
229             if (title!=_T(""))
230               {
231                 sizer->Add( new wxStaticText(panel,-1, title ) ); 
232                 sizer->Add( new wxStaticText(panel,-1, _T("") ) ); 
233               }
234           } 
235         else 
236           {
237             sizer       = new wxFlexGridSizer(1);
238             if (title!=_T(""))
239               {
240                 sizer->Add( new wxStaticText(panel,-1, title ) ); 
241               }
242           }
243         sizer->Add( mwxSlider,1,wxGROW ); 
244         sizer->AddGrowableCol(0);
245         if (mResolutionOfSlider!=NULL) 
246           {
247             sizer->Add( mResolutionOfSlider ); 
248           }
249       } 
250     else 
251       {
252         // VERTICAL 
253         sizer   = new wxFlexGridSizer(1);
254         if (labels==true)  // with lable
255           {
256             label_vertical = new wxStaticText(panel,-1,_T(""));
257             label_min = new wxStaticText(panel,-1,_T(""));
258             label_max = new wxStaticText(panel,-1,_T(""));
259             if (title!=_T(""))
260               {
261                 sizer->Add( new wxStaticText(panel,-1, title ) );
262                 sizer->AddGrowableRow(3);
263               } 
264             else 
265               {
266                 sizer->AddGrowableRow(2);
267               }
268             sizer->Add( label_vertical );
269             sizer->Add( label_min );
270             sizer->Add( mwxSlider,1,wxGROW );
271             sizer->Add( label_max );
272             if (mResolutionOfSlider!=NULL) 
273               {
274                 sizer->Add( mResolutionOfSlider ); 
275               }
276           } 
277         else 
278           {
279             if (title!=_T(""))
280               {
281                 sizer->Add( new wxStaticText(panel,-1, title ) );
282                 sizer->AddGrowableRow(1);
283               } 
284             else 
285               {
286                 sizer->AddGrowableRow(0);
287               }
288             sizer->Add( mwxSlider,1,wxGROW );
289             if (mResolutionOfSlider!=NULL) 
290               {
291                 sizer->Add( mResolutionOfSlider ); 
292               }
293           }
294       }
295     // Initialize the labels 
296     RefreshLabels();  
297     // Insert the sizer in the main panel and refresh the layout
298     panel->SetSizer(sizer);
299   }
300   //-------------------------------------------------------------------------
301   
302         
303         
304   //-------------------------------------------------------------------------
305   SliderWidget::~SliderWidget()
306   {
307   }
308   //-------------------------------------------------------------------------
309
310
311   //-------------------------------------------------------------------------
312   void SliderWidget::OnResolutionOfSlider(wxScrollEvent& event)
313   {
314     int value  = mwxSlider->GetValue();
315     int delta  = (int) (pow((double) 4 ,(double) mResolutionOfSlider->GetValue() ));
316     int minTmp = value - delta/2;
317     int maxTmp = value + delta/2;
318     if (minTmp<min)
319       {
320         minTmp = min;
321         //              maxTmp = delta;
322       }
323     if (maxTmp>max)
324       {
325         maxTmp = max;
326       }
327     mwxSlider->SetRange(minTmp,maxTmp);
328     RefreshLabels();
329   }
330   //-------------------------------------------------------------------------
331
332
333
334   //-------------------------------------------------------------------------
335   void SliderWidget::OnSliderTrack(wxScrollEvent& event)
336   {
337     if(reactiveOnTrack)
338     {
339     // When user releases the slider 
340     // we update the output of the box
341     
342     mBox->bbSetOutputOut( mwxSlider->GetValue() );
343     mBox->bbSetInputIn( mwxSlider->GetValue() );
344     // and signal that the output has changed
345     mBox->bbSignalOutputModification(std::string("Out"));    
346     }
347     RefreshLabels();
348   }
349   //-------------------------------------------------------------------------
350
351
352   //-------------------------------------------------------------------------
353   void SliderWidget::OnSliderRelease(wxScrollEvent& event)
354   {
355     // When user releases the slider 
356     // we update the output of the box
357     mBox->bbSetOutputOut( mwxSlider->GetValue() );
358     mBox->bbSetInputIn( mwxSlider->GetValue() );
359     // and signal that the output has changed
360     mBox->bbSignalOutputModification(std::string("Out"));
361   }
362   //-------------------------------------------------------------------------
363   
364
365   //-------------------------------------------------------------------------
366   void SliderWidget::RefreshLabels()
367   {
368     wxString strValue;
369     if (label_vertical!=NULL)
370       {
371         strValue.Printf( _T("%d"), mwxSlider->GetValue() );
372         label_vertical->SetLabel(strValue);
373       }
374     if (label_min!=NULL)
375       {
376         strValue.Printf( _T("%d"), mwxSlider->GetMin() );
377         label_min->SetLabel(strValue);
378       }
379     if (label_max!=NULL)
380       {
381         strValue.Printf( _T("%d"), mwxSlider->GetMax() );
382         label_max->SetLabel(strValue);
383       }  
384   }
385   //-------------------------------------------------------------------------
386   
387
388   //-------------------------------------------------------------------------
389   void SliderWidget::SetRange(int min, int max)
390   {
391     this->min = min;
392     this->max = max;
393     mwxSlider->SetRange(min,max);
394     RefreshLabels();
395   }
396         
397         
398         //-------------------------------------------------------------------------
399         void SliderWidget::SetReactiveOnTrack(bool ok)
400         {
401                 reactiveOnTrack = ok;
402         }
403         
404   //-------------------------------------------------------------------------
405
406  
407
408
409   //--------------------------------------------------------------------------
410   //-------------------------------------------------------------------------
411   // WxBlackBox implementation
412   //--------------------------------------------------------------------------
413   //--------------------------------------------------------------------------
414
415   //--------------------------------------------------------------------------
416   BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WxBlackBox);
417   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,Slider);
418   
419         //-----------------------------------------------------------------     
420         void Slider::bbUserSetDefaultValues()
421         {
422                 bbSetInputIn(0);
423                 bbSetInputMin(0);
424                 bbSetInputMax(500);
425                 bbSetOutputOut(0);
426                 bbSetInputOrientation("HORIZONTAL");
427                 bbSetInputChangeResolution(false);
428                 bbSetInputLabel(true);
429                 bbSetInputReactiveOnTrack(false);    
430         }
431         
432         //-----------------------------------------------------------------     
433         void Slider::bbUserInitializeProcessing()
434         {
435         }
436         
437         //-----------------------------------------------------------------     
438         void Slider::bbUserFinalizeProcessing()
439         {
440         }       
441   
442   //--------------------------------------------------------------------------
443   void Slider::Process() 
444   {
445     bbtkDebugMessage("process",3,
446                      "Slider "<<bbGetName()<<" input="
447                      <<bbGetInputIn()<<std::endl);
448
449 // desperate try // JPR
450       if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() ||  bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )       
451       {    
452                   ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
453       }
454
455           ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
456           
457     bbSetOutputOut( bbGetInputIn() );
458     if (bbGetOutputWidget()!=0)
459       {
460         ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
461       }
462   }
463
464 //--------------------------------------------------------------------------
465   void Slider::CreateWidget(wxWindow* parent)
466   {
467
468     int orientation=0;
469     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true)  { orientation=0; }
470     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)    { orientation=1; }
471     
472     
473     //    std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
474     SliderWidget *w =  new SliderWidget(this, 
475                                         parent, //bbGetWxParent(),
476                                         orientation , 
477                                         bbGetInputChangeResolution(), 
478                                         bbGetInputLabel(), 
479                                         bbtk::std2wx( bbGetInputTitle() ),
480                                         bbGetInputMin(), 
481                                         bbGetInputMax(),
482                                         bbGetInputIn(),
483                                         bbGetInputReactiveOnTrack()
484                                         ); 
485     //    std::cout << "w = "<<w<<std::endl;
486     //  w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
487           
488     bbSetOutputWidget( w );
489   }
490   
491
492 } //namespace bbwx
493
494 #endif // _USE_WXWIDGETS_ 
495
496