]> 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/02/15 12:47:17 $
7   Version:   $Revision: 1.1 $
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     std::string commandstr(mBox->bbGetInputIn());
70     
71     //  bbtk::Interpreter::mGlobalInterpreter->InterpretLine( commandstr );
72     int i;
73     bool ok=true;
74     int pos1=0,pos2;
75     pos2 = commandstr.find(";",pos1);
76     std::string ccommand;
77     while (ok==true)
78       {
79         if (pos2==-1) 
80           {
81             ok=false;
82             ccommand=commandstr.substr(pos1,commandstr.length()-pos1 );
83           } 
84         else 
85           {
86             ccommand=commandstr.substr(pos1,pos2-pos1);
87           }
88         for ( i=0 ; i < ccommand.length() ; i++)
89           {
90             if (ccommand[i]==39)
91               {
92                 ccommand[i]=34;
93               }
94           }             
95         bool insideComment = false; // for multiline comment
96         bbtk::Interpreter::mGlobalInterpreter->InterpretLine( ccommand, insideComment );
97         pos1=pos2+1;
98         pos2 = commandstr.find(";",pos2+1);
99         
100       }
101    
102     mBox->UpdateLabel();
103     mBox->UpdateColour();
104     mBox->bbSignalOutputModification();
105   }
106
107   
108   //--------------------------------------------------------------------------
109   
110   void CommandButtonWidget::SetLabel(wxString title)
111   {
112     mwxCommandButton->SetLabel(title);
113   }
114   //--------------------------------------------------------------------------
115   
116   void CommandButtonWidget::SetColour(wxColour color)
117   {
118     mwxCommandButton->SetBackgroundColour(color);
119   }
120
121
122
123   //--------------------------------------------------------------------------
124   //-------------------------------------------------------------------------
125   //--------------------------------------------------------------------------
126   //-------------------------------------------------------------------------- 
127
128
129   BBTK_USER_BLACK_BOX_IMPLEMENTATION(CommandButton,bbtk::WxBlackBox);
130
131   void CommandButton::bbUserConstructor() 
132   { 
133     bbSetInputIn("");
134     bbSetInputLabel("");
135     std::vector<double> lstColour;
136     lstColour.push_back(-1);
137     lstColour.push_back(-1);
138     lstColour.push_back(-1);
139     bbSetInputColour(lstColour);
140   }
141   
142   
143   void CommandButton::Process() 
144   { 
145     UpdateColour();
146     UpdateLabel();
147   }
148   
149   void CommandButton::UpdateColour()
150   {
151     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
152     if  ( (bbGetInputColour()[0]==-1) && 
153           (bbGetInputColour()[1]==-1) &&
154           (bbGetInputColour()[2]==-1) )
155       {
156         wxwidget->SetColour( wxwidget->GetParent()->GetBackgroundColour() );
157       } 
158     else 
159       {
160       int r=(int) (255*bbGetInputColour()[0]);
161       int g=(int) (255*bbGetInputColour()[1]);
162       int b=(int) (255*bbGetInputColour()[2]);
163       wxwidget->SetColour( wxColour(r,g,b) );
164     }
165
166   } 
167   void CommandButton::UpdateLabel()
168   {
169     CommandButtonWidget* wxwidget = (CommandButtonWidget*)bbGetOutputWidget();
170     wxwidget->SetLabel( bbtk::std2wx( bbGetInputLabel() ) );
171   }   
172  
173   /** 
174    * \brief  Create wxWidget .  
175    * 
176    * 
177    */  
178   void CommandButton::CreateWidget() 
179   { 
180     bbSetOutputWidget
181       ( new CommandButtonWidget ( this, bbGetWxParent(), 
182                                   bbtk::std2wx(bbGetInputLabel()) ) );
183     UpdateColour();
184   } 
185  
186  
187 }  //namespace bbwx 
188  
189 #endif  // _USE_WXWIDGETS_ 
190