]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
Clean Code
[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), mBox(box)
79     : wxPanel( parent, -1, wxDefaultPosition, wxSize(20,-1), wxTAB_TRAVERSAL), mBox(box)
80   {
81     wxPanel *panel      = this;
82     mwxCommandButton = new wxButton( panel, -1, title);
83     Connect( mwxCommandButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
84              (wxObjectEventFunction) 
85              (void (wxPanel::*)(wxEvent&))
86              &CommandButtonWidget::OnCommandButton ); 
87     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
88     sizer -> Add( mwxCommandButton,1,wxGROW | wxALL,10 ); 
89     sizer       -> AddGrowableCol(0);
90     panel       -> SetSizer(sizer);
91     panel       -> SetAutoLayout(true);
92     panel       -> Layout();
93   }
94   
95   CommandButtonWidget::~CommandButtonWidget()
96   {
97   }
98   
99   void CommandButtonWidget::OnCommandButton( wxEvent& )
100   {
101     // Look for the interpreter or the executer if no interpreter
102     bbtk::Interpreter::Pointer I;
103     bbtk::VirtualExec::Pointer E;
104       if (mBox->bbGetParent() != 0) 
105         {
106           bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(mBox->bbGetParent()->bbGetDescriptor())->GetFactory();
107           if ((f != 0)&&
108               (f->GetExecuter()))
109             {
110               E = f->GetExecuter();
111               I = E->GetInterpreter();
112             }
113         }
114       if (I==0) 
115         {
116           //      bbtkError("CommandButton::DoProcess() : could not find interpreter");
117           if (E==0) 
118             {
119               // If no executer : create a totally independant interpreter
120               I = bbtk::Interpreter::New();
121             } else {
122               // If executer : create an interpreter using E
123               I = bbtk::Interpreter::New(E);
124             }
125         }
126       
127       std::string commandstr(mBox->bbGetInputIn());
128     
129     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
130     unsigned int i;
131     bool ok=true;
132     int pos1=0,pos2;
133     pos2 = commandstr.find(";",pos1);
134     std::string ccommand;
135     while (ok==true)
136       {
137         if (pos2==-1) 
138           {
139             ok=false;
140             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
141           }  else  {
142             ccommand=commandstr.substr(pos1,pos2-pos1);
143           }
144         for ( i=0 ; i < ccommand.length() ; i++)
145           {
146             if (ccommand[i]==39) // '
147             {
148                         ccommand[i]=34;  // "
149             }
150           }             
151         I->InterpretLine( ccommand );
152         pos1=pos2+1;
153         pos2 = commandstr.find(";",pos2+1);     
154       }
155     mBox->UpdateLabel();
156     mBox->UpdateColour();
157     mBox->bbSignalOutputModification();
158   }
159
160   //--------------------------------------------------------------------------  
161   void CommandButtonWidget::SetLabel(wxString title)
162   {
163     mwxCommandButton->SetLabel(title);
164   }
165
166   //--------------------------------------------------------------------------
167   void CommandButtonWidget::SetColour(wxColour color)
168   {
169     mwxCommandButton->SetBackgroundColour(color);
170         mwxCommandButton->Refresh();
171         mwxCommandButton->ClearBackground();
172   }
173
174
175
176   //--------------------------------------------------------------------------
177   //-------------------------------------------------------------------------
178   //--------------------------------------------------------------------------
179   //-------------------------------------------------------------------------- 
180
181   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,CommandButton);
182   BBTK_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
183
184 //-----------------------------------------------------------------     
185         void CommandButton::bbUserSetDefaultValues()
186         {
187                 bbSetInputIn("");
188                 bbSetInputLabel("");
189                 std::vector<double> lstColour;
190                 lstColour.push_back(0.75);
191                 lstColour.push_back(0.75);
192                 lstColour.push_back(0.75);
193                 bbSetInputColour(lstColour);
194                 bbSetOutputWidget(0);
195         }
196         
197         //-----------------------------------------------------------------     
198         void CommandButton::bbUserInitializeProcessing()
199         {
200         }
201         
202         //-----------------------------------------------------------------     
203         void CommandButton::bbUserFinalizeProcessing()
204         {
205         }
206         
207   
208   void CommandButton::Process() 
209   { 
210     CommandButtonWidget* w = (CommandButtonWidget*)bbGetOutputWidget();
211     if (w) 
212       {
213         UpdateColour();
214         UpdateLabel();
215       }
216   }
217   
218   void CommandButton::UpdateColour()
219   {
220     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
221     if  ( (bbGetInputColour()[0]==-1) && 
222           (bbGetInputColour()[1]==-1) &&
223           (bbGetInputColour()[2]==-1) )
224       {
225         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
226       }  else  {
227       int r=(int) (255*bbGetInputColour()[0]);
228       int g=(int) (255*bbGetInputColour()[1]);
229       int b=(int) (255*bbGetInputColour()[2]);
230       wxwidget->SetColour( wxColour(r,g,b) );
231     }
232   } 
233         
234   void CommandButton::UpdateLabel()
235   {
236     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
237     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
238   }   
239  
240   /** 
241    * \brief  Create wxWidget .  
242    * 
243    * 
244    */  
245   void CommandButton::CreateWidget(wxWindow* parent) 
246   { 
247     bbSetOutputWidget
248       ( new CommandButtonWidget ( this, //bbGetWxParent(), 
249                                                                  parent,
250                                                                  bbtk::std2wx(bbGetInputLabel()) ) );
251     UpdateColour();
252   } 
253  
254  
255 }  //namespace bbwx 
256  
257 #endif  // _USE_WXWIDGETS_ 
258