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