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