]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
- Finished the Transcriptor
[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/03/26 14:47:37 $
7   Version:   $Revision: 1.6 $
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 ); 
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
71       bbtk::Interpreter* I = 0;
72       if (mBox->bbGetParent() != 0) 
73         {
74           bbtk::Factory* f = 
75             ((bbtk::ComplexBlackBoxDescriptor*)mBox->bbGetParent()
76              ->bbGetDescriptor())->GetFactory();
77           if ((f != 0)&&
78               (f->GetExecuter()))
79             {
80               I = f->GetExecuter()->GetInterpreter();
81             }
82         }
83       if (I==0) 
84         {
85           //      bbtkError("ExecBbiCommand::DoProcess() : could not find interpreter");
86           I = new bbtk::Interpreter();
87         }
88       
89       std::string commandstr(mBox->bbGetInputIn());
90     
91     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
92     int i;
93     bool ok=true;
94     int pos1=0,pos2;
95     pos2 = commandstr.find(";",pos1);
96     std::string ccommand;
97     while (ok==true)
98       {
99         if (pos2==-1) 
100           {
101             ok=false;
102             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
103           } 
104         else 
105           {
106             ccommand=commandstr.substr(pos1,pos2-pos1);
107           }
108         for ( i=0 ; i < ccommand.length() ; i++)
109           {
110             if (ccommand[i]==39) // '
111               {
112                 ccommand[i]=34;  // "
113               }
114           }             
115         I->InterpretLine( ccommand );
116         pos1=pos2+1;
117         pos2 = commandstr.find(";",pos2+1);
118         
119       }
120    
121     mBox->UpdateLabel();
122     mBox->UpdateColour();
123     mBox->bbSignalOutputModification();
124   }
125
126   
127   //--------------------------------------------------------------------------
128   
129   void CommandButtonWidget::SetLabel(wxString title)
130   {
131     mwxCommandButton->SetLabel(title);
132   }
133   //--------------------------------------------------------------------------
134   
135   void CommandButtonWidget::SetColour(wxColour color)
136   {
137     mwxCommandButton->SetBackgroundColour(color);
138   }
139
140
141
142   //--------------------------------------------------------------------------
143   //-------------------------------------------------------------------------
144   //--------------------------------------------------------------------------
145   //-------------------------------------------------------------------------- 
146
147
148   BBTK_USER_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
149
150   void CommandButton::bbUserConstructor() 
151   { 
152     bbSetInputIn("");
153     bbSetInputLabel("");
154     std::vector<double> lstColour;
155     lstColour.push_back(-1);
156     lstColour.push_back(-1);
157     lstColour.push_back(-1);
158     bbSetInputColour(lstColour);
159   }
160   
161   
162   void CommandButton::Process() 
163   { 
164     UpdateColour();
165     UpdateLabel();
166   }
167   
168   void CommandButton::UpdateColour()
169   {
170     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
171     if  ( (bbGetInputColour()[0]==-1) && 
172           (bbGetInputColour()[1]==-1) &&
173           (bbGetInputColour()[2]==-1) )
174       {
175         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
176       } 
177     else 
178       {
179       int r=(int) (255*bbGetInputColour()[0]);
180       int g=(int) (255*bbGetInputColour()[1]);
181       int b=(int) (255*bbGetInputColour()[2]);
182       wxwidget->SetColour( wxColour(r,g,b) );
183     }
184
185   } 
186   void CommandButton::UpdateLabel()
187   {
188     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
189     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
190   }   
191  
192   /** 
193    * \brief  Create wxWidget .  
194    * 
195    * 
196    */  
197   void CommandButton::CreateWidget() 
198   { 
199     bbSetOutputWidget
200       ( new CommandButtonWidget ( this, bbGetWxParent(), 
201                                   bbtk::std2wx(bbGetInputLabel()) ) );
202     UpdateColour();
203   } 
204  
205  
206 }  //namespace bbwx 
207  
208 #endif  // _USE_WXWIDGETS_ 
209