]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
5ad78e5b8110918aa62c4093daf11e9b7f730cf2
[bbtk.git] / packages / wx / src / bbwxCommandButton.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbwxCommandButton.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/05/15 14:58:03 $
6   Version:   $Revision: 1.15 $
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 
34  */
35
36
37 #ifdef _USE_WXWIDGETS_
38
39
40 #include "bbwxCommandButton.h"
41 #include "bbwxPackage.h"
42 #include "bbtkInterpreter.h"
43 #include "bbtkExecuter.h"
44
45
46
47 namespace bbwx
48 {
49
50
51    //--------------------------------------------------------------------------
52   class CommandButtonWidget : public wxPanel
53   {
54   public:
55     CommandButtonWidget(CommandButton* box, wxWindow *parent, 
56                         wxString title);
57     ~CommandButtonWidget();
58     void OnCommandButton( wxEvent& );
59     void SetLabel(wxString title);
60     void SetColour(wxColour color);
61         
62   private:
63     CommandButton* mBox;
64     wxButton    *mwxCommandButton;
65   }; 
66    
67   //--------------------------------------------------------------------------
68   //--------------------------------------------------------------------------
69
70   CommandButtonWidget::CommandButtonWidget(CommandButton* box,
71                                            wxWindow *parent, 
72                                            wxString title )
73     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
74       mBox(box)
75   { 
76
77     wxPanel *panel      = this;
78
79     mwxCommandButton = new wxButton( panel, -1, title);
80     Connect( mwxCommandButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
81              (wxObjectEventFunction) 
82              (void (wxPanel::*)(wxEvent&))
83              &CommandButtonWidget::OnCommandButton ); 
84     
85     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
86     sizer -> Add( mwxCommandButton,1,wxGROW | wxALL,10 ); 
87     sizer       -> AddGrowableCol(0);
88     
89     panel       -> SetSizer(sizer);
90     panel       -> SetAutoLayout(true);
91     panel       -> Layout();
92     
93   }
94   
95   CommandButtonWidget::~CommandButtonWidget()
96   {
97   }
98   
99   
100   void CommandButtonWidget::OnCommandButton( wxEvent& )
101   {
102     // Look for the interpreter or the executer if no interpreter
103     bbtk::Interpreter::Pointer I;
104     bbtk::VirtualExec::Pointer E;
105       if (mBox->bbGetParent() != 0) 
106         {
107           bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(mBox->bbGetParent()->bbGetDescriptor())->GetFactory();
108           if ((f != 0)&&
109               (f->GetExecuter()))
110             {
111               E = f->GetExecuter();
112               I = E->GetInterpreter();
113             }
114         }
115       if (I==0) 
116         {
117           //      bbtkError("CommandButton::DoProcess() : could not find interpreter");
118           if (E==0) 
119             {
120               // If no executer : create a totally independant interpreter
121               I = bbtk::Interpreter::New();
122             }
123           else 
124             {
125               // If executer : create an interpreter using E
126               I = bbtk::Interpreter::New(E);
127             }
128         }
129       
130       std::string commandstr(mBox->bbGetInputIn());
131     
132     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
133     unsigned int i;
134     bool ok=true;
135     int pos1=0,pos2;
136     pos2 = commandstr.find(";",pos1);
137     std::string ccommand;
138     while (ok==true)
139       {
140         if (pos2==-1) 
141           {
142             ok=false;
143             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
144           } 
145         else 
146           {
147             ccommand=commandstr.substr(pos1,pos2-pos1);
148           }
149         for ( i=0 ; i < ccommand.length() ; i++)
150           {
151             if (ccommand[i]==39) // '
152               {
153                 ccommand[i]=34;  // "
154               }
155           }             
156         I->InterpretLine( ccommand );
157         pos1=pos2+1;
158         pos2 = commandstr.find(";",pos2+1);
159         
160       }
161    
162     mBox->UpdateLabel();
163     mBox->UpdateColour();
164     mBox->bbSignalOutputModification();
165   }
166
167   
168   //--------------------------------------------------------------------------
169   
170   void CommandButtonWidget::SetLabel(wxString title)
171   {
172     mwxCommandButton->SetLabel(title);
173   }
174   //--------------------------------------------------------------------------
175   
176   void CommandButtonWidget::SetColour(wxColour color)
177   {
178     mwxCommandButton->SetBackgroundColour(color);
179   }
180
181
182
183   //--------------------------------------------------------------------------
184   //-------------------------------------------------------------------------
185   //--------------------------------------------------------------------------
186   //-------------------------------------------------------------------------- 
187
188   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,CommandButton);
189   BBTK_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
190
191 //-----------------------------------------------------------------     
192         void CommandButton::bbUserSetDefaultValues()
193         {
194                 bbSetInputIn("");
195                 bbSetInputLabel("");
196                 std::vector<double> lstColour;
197                 lstColour.push_back(0.75);
198                 lstColour.push_back(0.75);
199                 lstColour.push_back(0.75);
200                 bbSetInputColour(lstColour);
201                 bbSetOutputWidget(0);
202         }
203         
204         //-----------------------------------------------------------------     
205         void CommandButton::bbUserInitializeProcessing()
206         {
207         }
208         
209         //-----------------------------------------------------------------     
210         void CommandButton::bbUserFinalizeProcessing()
211         {
212         }
213         
214   
215   void CommandButton::Process() 
216   { 
217     CommandButtonWidget* w = (CommandButtonWidget*)bbGetOutputWidget();
218     if (w) 
219       {
220         UpdateColour();
221         UpdateLabel();
222       }
223   }
224   
225   void CommandButton::UpdateColour()
226   {
227     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
228     if  ( (bbGetInputColour()[0]==-1) && 
229           (bbGetInputColour()[1]==-1) &&
230           (bbGetInputColour()[2]==-1) )
231       {
232         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
233       } 
234     else 
235       {
236       int r=(int) (255*bbGetInputColour()[0]);
237       int g=(int) (255*bbGetInputColour()[1]);
238       int b=(int) (255*bbGetInputColour()[2]);
239       wxwidget->SetColour( wxColour(r,g,b) );
240     }
241
242   } 
243         
244   void CommandButton::UpdateLabel()
245   {
246     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
247     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
248   }   
249  
250   /** 
251    * \brief  Create wxWidget .  
252    * 
253    * 
254    */  
255   void CommandButton::CreateWidget(wxWindow* parent) 
256   { 
257     bbSetOutputWidget
258       ( new CommandButtonWidget ( this, //bbGetWxParent(), 
259                                                                  parent,
260                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
261     UpdateColour();
262   } 
263  
264  
265 }  //namespace bbwx 
266  
267 #endif  // _USE_WXWIDGETS_ 
268