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