]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxInputText.cxx
#3477 bug InputText initial value
[bbtk.git] / packages / wx / src / bbwxInputText.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbwxInputText.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:52:14 $
33   Version:   $Revision: 1.13 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41 #ifdef _USE_WXWIDGETS_
42
43 #include "bbwxInputText.h"
44 #include "bbwxPackage.h"
45 //#include <wx/dialog.h>
46
47
48 namespace bbwx
49 {
50  
51   //--------------------------------------------------------------------------
52   class InputTextWidget : wxPanel
53   {
54   public:
55     InputTextWidget(InputText* box, wxWindow *parent,
56                     wxString In, wxString title );
57     ~InputTextWidget();
58
59     std::string GetValue();
60         void SetValue(std::string value);
61
62     void OnTextEnter(wxCommandEvent& event);
63     void OnTextUpdate(wxCommandEvent& event);
64
65     void SetTitle(wxString);
66
67   private:
68     InputText    *mBox;
69     wxTextCtrl   *mwxTextCtrl;
70     wxStaticText *mwxTitle;
71   };
72   
73   //------------------------------------------------------------------------
74   //------------------------------------------------------------------------
75   //------------------------------------------------------------------------
76
77   InputTextWidget::InputTextWidget(InputText* box,
78                                    wxWindow *parent, 
79                                    wxString In, 
80                                    wxString title )
81     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
82       mBox(box)
83   {
84     wxPanel                     *panel  = this;
85
86 //wxTE_PROCESS_ENTER
87 //wxTE_MULTILINE    
88 //wxTE_PASSWORD
89     mwxTextCtrl = new wxTextCtrl( panel, -1, In,wxDefaultPosition, wxSize(80,80) , wxTE_PROCESS_ENTER);
90     Connect( mwxTextCtrl->GetId(),  wxEVT_COMMAND_TEXT_ENTER, 
91              (wxObjectEventFunction) 
92              (wxEventFunction)
93              (wxCommandEventFunction) 
94              (void (wxPanel::*)(wxCommandEvent&))
95              &InputTextWidget::OnTextEnter ); 
96
97     Connect( mwxTextCtrl->GetId(),  wxEVT_COMMAND_TEXT_UPDATED, 
98              (wxObjectEventFunction) 
99              (wxEventFunction)
100              (wxCommandEventFunction) 
101              (void (wxPanel::*)(wxCommandEvent&))
102              &InputTextWidget::OnTextUpdate ); 
103
104
105     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
106     mwxTitle = new wxStaticText(panel, -1, title ); 
107     sizer-> Add( mwxTitle ); 
108     sizer-> Add( mwxTextCtrl, 1, wxEXPAND ); 
109     sizer-> AddGrowableCol(0);
110     
111     panel-> SetSizer(sizer);
112     panel-> SetAutoLayout(true);
113     panel-> Layout();
114
115   }
116   //-------------------------------------------------------------------------
117   
118   InputTextWidget::~InputTextWidget()
119   {
120   }
121
122   //-------------------------------------------------------------------------
123   
124  
125    void InputTextWidget::SetTitle(wxString s)
126   { 
127     mwxTitle->SetLabel(s);
128   }
129
130   //-------------------------------------------------------------------------
131   std::string InputTextWidget::GetValue()
132   { 
133     return bbtk::wx2std ( mwxTextCtrl->GetValue() );
134   }
135         
136   void InputTextWidget::SetValue(std::string value)
137   { 
138         mwxTextCtrl->SetValue( bbtk::std2wx(value) ) ;
139   }
140         
141   //--------------------------------------------------------------------------
142   void InputTextWidget::OnTextUpdate(wxCommandEvent& event)
143   {
144         if (mBox->bbGetInputReactiveOnKeystroke()==2){
145             mBox->bbSetOutputOut( GetValue() );
146             mBox->bbSetInputIn( GetValue() );
147             mBox->bbSignalOutputModification("Out");
148         }
149   }
150
151   //--------------------------------------------------------------------------
152   void InputTextWidget::OnTextEnter(wxCommandEvent& event)
153   {
154         if (mBox->bbGetInputReactiveOnKeystroke()==1){
155             mBox->bbSetOutputOut( GetValue() );
156                 mBox->bbSetInputIn( GetValue() );
157                 mBox->bbSignalOutputModification("Out");
158         }
159   }
160
161
162   //--------------------------------------------------------------------------
163   //-------------------------------------------------------------------------
164   //--------------------------------------------------------------------------
165   //--------------------------------------------------------------------------
166
167   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx, InputText);
168   BBTK_BLACK_BOX_IMPLEMENTATION(InputText, bbtk::WxBlackBox);
169
170   
171         //-----------------------------------------------------------------     
172         void InputText::bbUserSetDefaultValues()
173         {
174                 firsttime=true;
175                 bbSetInputTitle("");
176                 bbSetInputIn("");
177                 bbSetOutputWidget(NULL);
178                 bbSetInputReactiveOnKeystroke(2);
179         }
180         
181         //-----------------------------------------------------------------     
182         void InputText::bbUserInitializeProcessing()
183         {
184         }
185         
186         //-----------------------------------------------------------------     
187         void InputText::bbUserFinalizeProcessing()
188         {
189         }
190
191         
192         //-----------------------------------------------------------------     
193   void InputText::Process() 
194   { 
195     InputTextWidget *w=(InputTextWidget *)bbGetOutputWidget();
196         if (w) 
197         {
198                 if (firsttime==true)
199                 {
200                         firsttime=false;
201                         w->SetValue(  bbGetInputIn().c_str() );
202                 }
203                 w->SetTitle( bbtk::std2wx ( bbGetInputTitle() ) );
204                 bbSetOutputOut( w->GetValue() );
205         } else {
206           bbSetOutputOut( bbGetInputIn().c_str() );
207         }
208   }
209   
210
211
212   void InputText::CreateWidget(wxWindow* parent)
213   {
214     bbSetOutputWidget
215      ( (wxWindow*) new InputTextWidget( this, //bbGetWxParent(),
216                                                                                 parent,
217                                                                                 bbtk::std2wx ( bbGetInputIn() ) , 
218                                                                                 bbtk::std2wx ( bbGetInputTitle() ) 
219                                                                           ) 
220          );     
221   }
222
223 }//namespace bbtk
224
225 #endif // _USE_WXWIDGETS_ 
226