]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
*** empty log message ***
[bbtk.git] / packages / wx / src / bbwxCommandButton.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbwxCommandButton.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/12 08:55:23 $
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 
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   void CommandButton::bbUserConstructor() 
192   { 
193     bbSetInputIn("");
194     bbSetInputLabel("");
195     std::vector<double> lstColour;
196     lstColour.push_back(0.75);
197     lstColour.push_back(0.75);
198     lstColour.push_back(0.75);
199     bbSetInputColour(lstColour);
200     bbSetOutputWidget(0);
201   }
202   
203   
204   void CommandButton::Process() 
205   { 
206     CommandButtonWidget* w = (CommandButtonWidget*)bbGetOutputWidget();
207     if (w) 
208       {
209         UpdateColour();
210         UpdateLabel();
211       }
212   }
213   
214   void CommandButton::UpdateColour()
215   {
216     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
217     if  ( (bbGetInputColour()[0]==-1) && 
218           (bbGetInputColour()[1]==-1) &&
219           (bbGetInputColour()[2]==-1) )
220       {
221         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
222       } 
223     else 
224       {
225       int r=(int) (255*bbGetInputColour()[0]);
226       int g=(int) (255*bbGetInputColour()[1]);
227       int b=(int) (255*bbGetInputColour()[2]);
228       wxwidget->SetColour( wxColour(r,g,b) );
229     }
230
231   } 
232   void CommandButton::UpdateLabel()
233   {
234     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
235     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
236   }   
237  
238   /** 
239    * \brief  Create wxWidget .  
240    * 
241    * 
242    */  
243   void CommandButton::CreateWidget(wxWindow* parent) 
244   { 
245     bbSetOutputWidget
246       ( new CommandButtonWidget ( this, //bbGetWxParent(), 
247                                                                  parent,
248                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
249     UpdateColour();
250   } 
251  
252  
253 }  //namespace bbwx 
254  
255 #endif  // _USE_WXWIDGETS_ 
256