]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
5cff6c8615f250d69752a41eb5720c7115466a55
[bbtk.git] / packages / wx / src / bbwxCommandButton.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   bbtk
4   Module:    $RCSfile: bbwxCommandButton.cxx,v $
5   Language:  C++
6   Date:      $Date: 2008/06/26 07:37:11 $
7   Version:   $Revision: 1.9 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de 
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even 
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 /**
19  *  \file 
20  *  \brief 
21  */
22
23
24 #ifdef _USE_WXWIDGETS_
25
26
27 #include "bbwxCommandButton.h"
28 #include "bbwxPackage.h"
29 #include "bbtkInterpreter.h"
30 #include "bbtkExecuter.h"
31
32
33
34 namespace bbwx
35 {
36   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,CommandButton);
37   
38   CommandButtonWidget::CommandButtonWidget(CommandButton* box,
39                                            wxWindow *parent, 
40                                            wxString title )
41     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
42       mBox(box)
43   { 
44
45     wxPanel *panel      = this;
46
47     mwxCommandButton = new wxButton( panel, -1, title);
48     Connect( mwxCommandButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
49              (wxObjectEventFunction) 
50              (void (wxPanel::*)(wxEvent&))
51              &CommandButtonWidget::OnCommandButton ); 
52     
53     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
54     sizer -> Add( mwxCommandButton,1,wxGROW | wxALL,10 ); 
55     sizer       -> AddGrowableCol(0);
56     
57     panel       -> SetSizer(sizer);
58     panel       -> SetAutoLayout(true);
59     panel       -> Layout();
60     
61   }
62   
63   CommandButtonWidget::~CommandButtonWidget()
64   {
65   }
66   
67   
68   void CommandButtonWidget::OnCommandButton( wxEvent& )
69   {
70     // Look for the interpreter or the executer if no interpreter
71     bbtk::Interpreter::Pointer I;
72     bbtk::VirtualExec::Pointer E;
73       if (mBox->bbGetParent() != 0) 
74         {
75           bbtk::Factory::Pointer f = boost::dynamic_pointer_cast<bbtk::ComplexBlackBoxDescriptor>(mBox->bbGetParent()->bbGetDescriptor())->GetFactory();
76           if ((f != 0)&&
77               (f->GetExecuter()))
78             {
79               E = f->GetExecuter();
80               I = E->GetInterpreter();
81             }
82         }
83       if (I==0) 
84         {
85           //      bbtkError("CommandButton::DoProcess() : could not find interpreter");
86           if (E==0) 
87             {
88               // If no executer : create a totally independant interpreter
89               I = bbtk::Interpreter::New();
90             }
91           else 
92             {
93               // If executer : create an interpreter using E
94               I = bbtk::Interpreter::New(E);
95             }
96         }
97       
98       std::string commandstr(mBox->bbGetInputIn());
99     
100     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
101     int i;
102     bool ok=true;
103     int pos1=0,pos2;
104     pos2 = commandstr.find(";",pos1);
105     std::string ccommand;
106     while (ok==true)
107       {
108         if (pos2==-1) 
109           {
110             ok=false;
111             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
112           } 
113         else 
114           {
115             ccommand=commandstr.substr(pos1,pos2-pos1);
116           }
117         for ( i=0 ; i < ccommand.length() ; i++)
118           {
119             if (ccommand[i]==39) // '
120               {
121                 ccommand[i]=34;  // "
122               }
123           }             
124         I->InterpretLine( ccommand );
125         pos1=pos2+1;
126         pos2 = commandstr.find(";",pos2+1);
127         
128       }
129    
130     mBox->UpdateLabel();
131     mBox->UpdateColour();
132     mBox->bbSignalOutputModification();
133   }
134
135   
136   //--------------------------------------------------------------------------
137   
138   void CommandButtonWidget::SetLabel(wxString title)
139   {
140     mwxCommandButton->SetLabel(title);
141   }
142   //--------------------------------------------------------------------------
143   
144   void CommandButtonWidget::SetColour(wxColour color)
145   {
146     mwxCommandButton->SetBackgroundColour(color);
147   }
148
149
150
151   //--------------------------------------------------------------------------
152   //-------------------------------------------------------------------------
153   //--------------------------------------------------------------------------
154   //-------------------------------------------------------------------------- 
155
156
157   BBTK_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
158
159   void CommandButton::bbUserConstructor() 
160   { 
161     bbSetInputIn("");
162     bbSetInputLabel("");
163     std::vector<double> lstColour;
164     lstColour.push_back(0.75);
165     lstColour.push_back(0.75);
166     lstColour.push_back(0.75);
167     bbSetInputColour(lstColour);
168   }
169   
170   
171   void CommandButton::Process() 
172   { 
173     UpdateColour();
174     UpdateLabel();
175   }
176   
177   void CommandButton::UpdateColour()
178   {
179     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
180     if  ( (bbGetInputColour()[0]==-1) && 
181           (bbGetInputColour()[1]==-1) &&
182           (bbGetInputColour()[2]==-1) )
183       {
184         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
185       } 
186     else 
187       {
188       int r=(int) (255*bbGetInputColour()[0]);
189       int g=(int) (255*bbGetInputColour()[1]);
190       int b=(int) (255*bbGetInputColour()[2]);
191       wxwidget->SetColour( wxColour(r,g,b) );
192     }
193
194   } 
195   void CommandButton::UpdateLabel()
196   {
197     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
198     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
199   }   
200  
201   /** 
202    * \brief  Create wxWidget .  
203    * 
204    * 
205    */  
206   void CommandButton::CreateWidget() 
207   { 
208     bbSetOutputWidget
209       ( new CommandButtonWidget ( this, bbGetWxParent(), 
210                                   bbtk::std2wx(bbGetInputLabel()) ) );
211     UpdateColour();
212   } 
213  
214  
215 }  //namespace bbwx 
216  
217 #endif  // _USE_WXWIDGETS_ 
218