libyui  3.0.5
 All Classes Functions Variables Enumerations Friends
YInputField.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YInputField.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YUISymbols.h"
30 #include "YMacroRecorder.h"
31 #include "YInputField.h"
32 
33 
34 
36 {
37  YInputFieldPrivate( std::string label, bool passwordMode )
38  : label( label )
39  , passwordMode( passwordMode )
40  , shrinkable( false )
41  , inputMaxLength( -1 )
42  {}
43 
44  std::string label;
45  bool passwordMode;
46  bool shrinkable;
47  std::string validChars;
48  int inputMaxLength;
49 };
50 
51 
52 
53 YInputField::YInputField( YWidget * parent, const std::string & label, bool passwordMode )
54  : YWidget( parent )
55  , priv( new YInputFieldPrivate( label, passwordMode ) )
56 {
57  YUI_CHECK_NEW( priv );
58 
59  // setDefaultStretchable( YD_HORIZ, true );
60  setDefaultStretchable( YD_VERT, false );
61 }
62 
63 
65 {
66  // NOP
67 }
68 
69 
70 std::string YInputField::label() const
71 {
72  return priv->label;
73 }
74 
75 
76 void YInputField::setLabel( const std::string & label )
77 {
78  priv->label = label;
79 }
80 
81 
83 {
84  return priv->passwordMode;
85 }
86 
87 
89 {
90  return priv->shrinkable;
91 }
92 
93 
94 void YInputField::setShrinkable( bool shrinkable )
95 {
96  priv->shrinkable = shrinkable;
97  // setDefaultStretchable( YD_HORIZ, ! shrinkable );
98 }
99 
100 
102 {
103  return priv->validChars;
104 }
105 
106 
107 void YInputField::setValidChars( const std::string & newValidChars )
108 {
109  priv->validChars= newValidChars;
110 }
111 
112 
114 {
115  return priv->inputMaxLength;
116 }
117 
118 
120 {
121  priv->inputMaxLength = len;
122 }
123 
124 
125 const YPropertySet &
127 {
128  static YPropertySet propSet;
129 
130  if ( propSet.isEmpty() )
131  {
132  /*
133  * @property std::string Value the input field's contents (the user input)
134  * @property std::string Label caption above the input field
135  * @property std::string ValidChars set of valid input characters
136  * @property integer InputMaxLength maximum number of input characters
137  */
138  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
139  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
140  propSet.add( YProperty( YUIProperty_ValidChars, YStringProperty ) );
141  propSet.add( YProperty( YUIProperty_InputMaxLength, YIntegerProperty ) );
142  propSet.add( YWidget::propertySet() );
143  }
144 
145  return propSet;
146 }
147 
148 
149 bool
150 YInputField::setProperty( const std::string & propertyName, const YPropertyValue & val )
151 {
152  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
153 
154  if ( propertyName == YUIProperty_Value ) setValue( val.stringVal() );
155  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
156  else if ( propertyName == YUIProperty_ValidChars ) setValidChars( val.stringVal() );
157  else if ( propertyName == YUIProperty_InputMaxLength ) setInputMaxLength( val.integerVal() );
158  else
159  {
160  return YWidget::setProperty( propertyName, val );
161  }
162 
163  return true; // success -- no special processing necessary
164 }
165 
166 
168 YInputField::getProperty( const std::string & propertyName )
169 {
170  propertySet().check( propertyName ); // throws exceptions if not found
171 
172  if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() );
173  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
174  else if ( propertyName == YUIProperty_ValidChars ) return YPropertyValue( validChars() );
175  else if ( propertyName == YUIProperty_InputMaxLength ) return YPropertyValue( inputMaxLength() );
176  else
177  {
178  return YWidget::getProperty( propertyName );
179  }
180 }
181 
182 
183 void
185 {
186  if ( ! passwordMode() ) // Don't record passwords in the macro file
187  {
188  macroRecorder->recordWidgetProperty( this, YUIProperty_Value );
189  }
190 }
191 
192 
193 const char *
195 {
196  if ( priv->passwordMode ) return "YPasswordField";
197  else return "YInputField";
198 }