]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxCommandButton.cxx
*** empty log message ***
[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/19 14:58:14 $
7   Version:   $Revision: 1.5 $
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
31
32
33 namespace bbwx
34 {
35   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx,CommandButton);
36   
37   CommandButtonWidget::CommandButtonWidget(CommandButton* box,
38                                            wxWindow *parent, 
39                                            wxString title )
40     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
41       mBox(box)
42   { 
43
44     wxPanel *panel      = this;
45
46     mwxCommandButton = new wxButton( panel, -1, title);
47     Connect( mwxCommandButton->GetId(),   wxEVT_COMMAND_BUTTON_CLICKED , 
48              (wxObjectEventFunction) 
49              (void (wxPanel::*)(wxEvent&))
50              &CommandButtonWidget::OnCommandButton ); 
51     
52     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
53     sizer -> Add( mwxCommandButton,1,wxGROW ); 
54     sizer       -> AddGrowableCol(0);
55     
56     panel       -> SetSizer(sizer);
57     panel       -> SetAutoLayout(true);
58     panel       -> Layout();
59     
60   }
61   
62   CommandButtonWidget::~CommandButtonWidget()
63   {
64   }
65   
66   
67   void CommandButtonWidget::OnCommandButton( wxEvent& )
68   {
69     // Look for the interpreter
70       bbtk::Interpreter* I = 0;
71       if (mBox->bbGetParent() != 0) 
72         {
73           bbtk::Factory* f = 
74             ((bbtk::ComplexBlackBoxDescriptor*)mBox->bbGetParent()
75              ->bbGetDescriptor())->GetFactory();
76           if ((f != 0)&&
77               (f->GetExecuter()))
78             {
79               I = f->GetExecuter()->GetInterpreter();
80             }
81         }
82       if (I==0) 
83         {
84           //      bbtkError("ExecBbiCommand::DoProcess() : could not find interpreter");
85           I = new bbtk::Interpreter();
86         }
87       
88       std::string commandstr(mBox->bbGetInputIn());
89     
90     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
91     int i;
92     bool ok=true;
93     int pos1=0,pos2;
94     pos2 = commandstr.find(";",pos1);
95     std::string ccommand;
96     while (ok==true)
97       {
98         if (pos2==-1) 
99           {
100             ok=false;
101             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
102           } 
103         else 
104           {
105             ccommand=commandstr.substr(pos1,pos2-pos1);
106           }
107         for ( i=0 ; i < ccommand.length() ; i++)
108           {
109             if (ccommand[i]==39) // '
110               {
111                 ccommand[i]=34;  // "
112               }
113           }             
114         I->InterpretLine( ccommand );
115         pos1=pos2+1;
116         pos2 = commandstr.find(";",pos2+1);
117         
118       }
119    
120     mBox->UpdateLabel();
121     mBox->UpdateColour();
122     mBox->bbSignalOutputModification();
123   }
124
125   
126   //--------------------------------------------------------------------------
127   
128   void CommandButtonWidget::SetLabel(wxString title)
129   {
130     mwxCommandButton->SetLabel(title);
131   }
132   //--------------------------------------------------------------------------
133   
134   void CommandButtonWidget::SetColour(wxColour color)
135   {
136     mwxCommandButton->SetBackgroundColour(color);
137   }
138
139
140
141   //--------------------------------------------------------------------------
142   //-------------------------------------------------------------------------
143   //--------------------------------------------------------------------------
144   //-------------------------------------------------------------------------- 
145
146
147   BBTK_USER_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
148
149   void CommandButton::bbUserConstructor() 
150   { 
151     bbSetInputIn("");
152     bbSetInputLabel("");
153     std::vector<double> lstColour;
154     lstColour.push_back(-1);
155     lstColour.push_back(-1);
156     lstColour.push_back(-1);
157     bbSetInputColour(lstColour);
158   }
159   
160   
161   void CommandButton::Process() 
162   { 
163     UpdateColour();
164     UpdateLabel();
165   }
166   
167   void CommandButton::UpdateColour()
168   {
169     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
170     if  ( (bbGetInputColour()[0]==-1) && 
171           (bbGetInputColour()[1]==-1) &&
172           (bbGetInputColour()[2]==-1) )
173       {
174         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
175       } 
176     else 
177       {
178       int r=(int) (255*bbGetInputColour()[0]);
179       int g=(int) (255*bbGetInputColour()[1]);
180       int b=(int) (255*bbGetInputColour()[2]);
181       wxwidget->SetColour( wxColour(r,g,b) );
182     }
183
184   } 
185   void CommandButton::UpdateLabel()
186   {
187     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
188     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
189   }   
190  
191   /** 
192    * \brief  Create wxWidget .  
193    * 
194    * 
195    */  
196   void CommandButton::CreateWidget() 
197   { 
198     bbSetOutputWidget
199       ( new CommandButtonWidget ( this, bbGetWxParent(), 
200                                   bbtk::std2wx(bbGetInputLabel()) ) );
201     UpdateColour();
202   } 
203  
204  
205 }  //namespace bbwx 
206  
207 #endif  // _USE_WXWIDGETS_ 
208