]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
e2d9ac08dd10636453ce59e6460a581036f26f74
[bbtk.git] / packages / wx / src / bbwxCommandButton.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbwxCommandButton.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:52:14 $
33   Version:   $Revision: 1.16 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41
42 #ifdef _USE_WXWIDGETS_
43
44
45 #include "bbwxCommandButton.h"
46 #include "bbwxPackage.h"
47 #include "bbtkInterpreter.h"
48 #include "bbtkExecuter.h"
49
50
51
52 namespace bbwx
53 {
54
55
56    //--------------------------------------------------------------------------
57   class CommandButtonWidget : public wxPanel
58   {
59   public:
60     CommandButtonWidget(CommandButton* box, wxWindow *parent, 
61                         wxString title);
62     ~CommandButtonWidget();
63     void OnCommandButton( wxEvent& );
64     void SetLabel(wxString title);
65     void SetColour(wxColour color);
66         
67   private:
68     CommandButton* mBox;
69     wxButton    *mwxCommandButton;
70   }; 
71    
72   //--------------------------------------------------------------------------
73   //--------------------------------------------------------------------------
74
75   CommandButtonWidget::CommandButtonWidget(CommandButton* box,
76                                            wxWindow *parent, 
77                                            wxString title )
78     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
79       mBox(box)
80   { 
81
82     wxPanel *panel      = this;
83
84     mwxCommandButton = new wxButton( panel, -1, title);
85     Connect( mwxCommandButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
86              (wxObjectEventFunction) 
87              (void (wxPanel::*)(wxEvent&))
88              &CommandButtonWidget::OnCommandButton ); 
89     
90     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
91     sizer -> Add( mwxCommandButton,1,wxGROW | wxALL,10 ); 
92     sizer       -> AddGrowableCol(0);
93     
94     panel       -> SetSizer(sizer);
95     panel       -> SetAutoLayout(true);
96     panel       -> Layout();
97     
98   }
99   
100   CommandButtonWidget::~CommandButtonWidget()
101   {
102   }
103   
104   
105   void CommandButtonWidget::OnCommandButton( wxEvent& )
106   {
107     // Look for the interpreter or the executer if no interpreter
108     bbtk::Interpreter::Pointer I;
109     bbtk::VirtualExec::Pointer E;
110       if (mBox->bbGetParent() != 0) 
111         {
112           bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(mBox->bbGetParent()->bbGetDescriptor())->GetFactory();
113           if ((f != 0)&&
114               (f->GetExecuter()))
115             {
116               E = f->GetExecuter();
117               I = E->GetInterpreter();
118             }
119         }
120       if (I==0) 
121         {
122           //      bbtkError("CommandButton::DoProcess() : could not find interpreter");
123           if (E==0) 
124             {
125               // If no executer : create a totally independant interpreter
126               I = bbtk::Interpreter::New();
127             }
128           else 
129             {
130               // If executer : create an interpreter using E
131               I = bbtk::Interpreter::New(E);
132             }
133         }
134       
135       std::string commandstr(mBox->bbGetInputIn());
136     
137     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
138     unsigned int i;
139     bool ok=true;
140     int pos1=0,pos2;
141     pos2 = commandstr.find(";",pos1);
142     std::string ccommand;
143     while (ok==true)
144       {
145         if (pos2==-1) 
146           {
147             ok=false;
148             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
149           } 
150         else 
151           {
152             ccommand=commandstr.substr(pos1,pos2-pos1);
153           }
154         for ( i=0 ; i < ccommand.length() ; i++)
155           {
156             if (ccommand[i]==39) // '
157               {
158                 ccommand[i]=34;  // "
159               }
160           }             
161         I->InterpretLine( ccommand );
162         pos1=pos2+1;
163         pos2 = commandstr.find(";",pos2+1);
164         
165       }
166    
167     mBox->UpdateLabel();
168     mBox->UpdateColour();
169     mBox->bbSignalOutputModification();
170   }
171
172   
173   //--------------------------------------------------------------------------
174   
175   void CommandButtonWidget::SetLabel(wxString title)
176   {
177     mwxCommandButton->SetLabel(title);
178   }
179   //--------------------------------------------------------------------------
180   
181   void CommandButtonWidget::SetColour(wxColour color)
182   {
183     mwxCommandButton->SetBackgroundColour(color);
184   }
185
186
187
188   //--------------------------------------------------------------------------
189   //-------------------------------------------------------------------------
190   //--------------------------------------------------------------------------
191   //-------------------------------------------------------------------------- 
192
193   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,CommandButton);
194   BBTK_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
195
196 //-----------------------------------------------------------------     
197         void CommandButton::bbUserSetDefaultValues()
198         {
199                 bbSetInputIn("");
200                 bbSetInputLabel("");
201                 std::vector<double> lstColour;
202                 lstColour.push_back(0.75);
203                 lstColour.push_back(0.75);
204                 lstColour.push_back(0.75);
205                 bbSetInputColour(lstColour);
206                 bbSetOutputWidget(0);
207         }
208         
209         //-----------------------------------------------------------------     
210         void CommandButton::bbUserInitializeProcessing()
211         {
212         }
213         
214         //-----------------------------------------------------------------     
215         void CommandButton::bbUserFinalizeProcessing()
216         {
217         }
218         
219   
220   void CommandButton::Process() 
221   { 
222     CommandButtonWidget* w = (CommandButtonWidget*)bbGetOutputWidget();
223     if (w) 
224       {
225         UpdateColour();
226         UpdateLabel();
227       }
228   }
229   
230   void CommandButton::UpdateColour()
231   {
232     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
233     if  ( (bbGetInputColour()[0]==-1) && 
234           (bbGetInputColour()[1]==-1) &&
235           (bbGetInputColour()[2]==-1) )
236       {
237         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
238       } 
239     else 
240       {
241       int r=(int) (255*bbGetInputColour()[0]);
242       int g=(int) (255*bbGetInputColour()[1]);
243       int b=(int) (255*bbGetInputColour()[2]);
244       wxwidget->SetColour( wxColour(r,g,b) );
245     }
246
247   } 
248         
249   void CommandButton::UpdateLabel()
250   {
251     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
252     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
253   }   
254  
255   /** 
256    * \brief  Create wxWidget .  
257    * 
258    * 
259    */  
260   void CommandButton::CreateWidget(wxWindow* parent) 
261   { 
262     bbSetOutputWidget
263       ( new CommandButtonWidget ( this, //bbGetWxParent(), 
264                                                                  parent,
265                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
266     UpdateColour();
267   } 
268  
269  
270 }  //namespace bbwx 
271  
272 #endif  // _USE_WXWIDGETS_ 
273