]> Creatis software - bbtk.git/blob - packages/wx/src/bbwxInputText.cxx
#3234 BBTK Feature New Normal InputText with password option
[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         void SetWindowStyle(int style);
62
63     void OnTextEnter(wxCommandEvent& event);
64     void OnTextUpdate(wxCommandEvent& event);
65
66     void SetTitle(wxString);
67
68   private:
69     InputText    *mBox;
70     wxTextCtrl   *mwxTextCtrl;
71     wxStaticText *mwxTitle;
72   };
73   
74   //------------------------------------------------------------------------
75   //------------------------------------------------------------------------
76   //------------------------------------------------------------------------
77
78   InputTextWidget::InputTextWidget(InputText* box,
79                                    wxWindow *parent, 
80                                    wxString In, 
81                                    wxString title )
82     : wxPanel( parent, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL),
83       mBox(box)
84   {
85     wxPanel                     *panel  = this;
86
87 //wxTE_PROCESS_ENTER
88 //wxTE_MULTILINE    For multiple line you have to create an other box (EED).
89
90     mwxTextCtrl = new wxTextCtrl( panel, -1, In,wxDefaultPosition, wxSize(800,40) , wxTE_PROCESS_ENTER);
91     Connect( mwxTextCtrl->GetId(),  wxEVT_COMMAND_TEXT_ENTER, 
92              (wxObjectEventFunction) 
93              (wxEventFunction)
94              (wxCommandEventFunction) 
95              (void (wxPanel::*)(wxCommandEvent&))
96              &InputTextWidget::OnTextEnter ); 
97
98     Connect( mwxTextCtrl->GetId(),  wxEVT_COMMAND_TEXT_UPDATED, 
99              (wxObjectEventFunction) 
100              (wxEventFunction)
101              (wxCommandEventFunction) 
102              (void (wxPanel::*)(wxCommandEvent&))
103              &InputTextWidget::OnTextUpdate ); 
104
105
106     wxFlexGridSizer *sizer      = new wxFlexGridSizer(1);
107     mwxTitle = new wxStaticText(panel, -1, title ); 
108     sizer-> Add( mwxTitle ); 
109     sizer-> Add( mwxTextCtrl, 1, wxGROW ); 
110     sizer-> AddGrowableCol(0);
111     
112     panel-> SetSizer(sizer);
113     panel-> SetAutoLayout(true);
114     panel-> Layout();
115
116   }
117   //-------------------------------------------------------------------------
118   
119   InputTextWidget::~InputTextWidget()
120   {
121   }
122
123   //-------------------------------------------------------------------------
124   
125  
126    void InputTextWidget::SetTitle(wxString s)
127   { 
128     mwxTitle->SetLabel(s);
129   }
130
131    void InputTextWidget::SetWindowStyle(int style)
132   { 
133         long wxstyle = wxTE_PROCESS_ENTER;
134         if (style==1) {wxstyle = wxTE_PASSWORD;}
135     mwxTextCtrl->SetWindowStyle(wxstyle);
136   }
137
138
139   //-------------------------------------------------------------------------
140   std::string InputTextWidget::GetValue()
141   { 
142     return bbtk::wx2std ( mwxTextCtrl->GetValue() );
143   }
144         
145   void InputTextWidget::SetValue(std::string value)
146   { 
147         mwxTextCtrl->SetValue( bbtk::std2wx(value) ) ;
148   }
149         
150   //--------------------------------------------------------------------------
151   void InputTextWidget::OnTextUpdate(wxCommandEvent& event)
152   {
153         if (mBox->bbGetInputReactiveOnKeystroke()==2){
154             mBox->bbSetOutputOut( GetValue() );
155             mBox->bbSetInputIn( GetValue() );
156             mBox->bbSignalOutputModification("Out");
157         }
158   }
159
160   //--------------------------------------------------------------------------
161   void InputTextWidget::OnTextEnter(wxCommandEvent& event)
162   {
163         if (mBox->bbGetInputReactiveOnKeystroke()==1){
164             mBox->bbSetOutputOut( GetValue() );
165                 mBox->bbSetInputIn( GetValue() );
166                 mBox->bbSignalOutputModification("Out");
167         }
168   }
169
170
171   //--------------------------------------------------------------------------
172   //-------------------------------------------------------------------------
173   //--------------------------------------------------------------------------
174   //--------------------------------------------------------------------------
175
176   BBTK_ADD_BLACK_BOX_TO_PACKAGE(wx, InputText);
177   BBTK_BLACK_BOX_IMPLEMENTATION(InputText, bbtk::WxBlackBox);
178
179   
180         //-----------------------------------------------------------------     
181         void InputText::bbUserSetDefaultValues()
182         {
183                 firsttime=true;
184                 bbSetInputTitle("");
185                 bbSetInputIn("");
186                 bbSetOutputWidget(0);
187                 bbSetInputReactiveOnKeystroke(2);
188                 bbSetInputStyle(0);
189         }
190         
191         //-----------------------------------------------------------------     
192         void InputText::bbUserInitializeProcessing()
193         {
194         }
195         
196         //-----------------------------------------------------------------     
197         void InputText::bbUserFinalizeProcessing()
198         {
199         }
200
201         
202         //-----------------------------------------------------------------     
203   void InputText::Process() 
204   { 
205     InputTextWidget *w=(InputTextWidget *)bbGetOutputWidget();
206         if (w) 
207         {
208                 if (firsttime==true)
209                 {
210                         firsttime=false;
211                         w->SetValue(  bbGetInputIn().c_str() );
212                 }
213                 w->SetWindowStyle( bbGetInputStyle() );
214                 w->SetTitle( bbtk::std2wx ( bbGetInputTitle() ) );
215                 bbSetOutputOut( w->GetValue() );
216         } else {
217           bbSetOutputOut( "<VOID>" );
218         }
219   }
220   
221
222
223   void InputText::CreateWidget(wxWindow* parent)
224   {
225     bbSetOutputWidget
226      ( (wxWindow*) new InputTextWidget( this, //bbGetWxParent(),
227                                                                                 parent,
228                                                                                 bbtk::std2wx ( bbGetInputIn() ) , 
229                                                                                 bbtk::std2wx ( bbGetInputTitle() ) 
230                                                                           ) 
231          );     
232   }
233
234 }//namespace bbtk
235
236 #endif // _USE_WXWIDGETS_ 
237