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