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