libyui  3.4.2
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 
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 }
Abstract base class for macro recorders.
virtual void setInputMaxLength(int numberOfChars)
Set the maximum input length, i.e., the maximum number of characters the user can enter...
Definition: YInputField.cc:119
virtual const char * widgetClass() const
Return a descriptive name of this widget class for logging, debugging etc.
Definition: YInputField.cc:194
bool passwordMode() const
Returns &#39;true&#39; if this input field is in password mode, i.e.
Definition: YInputField.cc:82
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
Transport class for the value of simple properties.
Definition: YProperty.h:104
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:430
std::string label() const
Get the label (the caption above the input field).
Definition: YInputField.cc:70
A set of properties to check names and types against.
Definition: YProperty.h:197
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YInputField.cc:150
virtual void setShrinkable(bool shrinkable=true)
Make this InputField very small.
Definition: YInputField.cc:94
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YInputField.cc:126
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual void setLabel(const std::string &label)
Set the label (the caption above the input field).
Definition: YInputField.cc:76
virtual std::string value()=0
Get the current value (the text entered by the user or set from the outside) of this input field...
int inputMaxLength() const
The maximum input length, i.e., the maximum number of characters the user can enter.
Definition: YInputField.cc:113
virtual void setValue(const std::string &text)=0
Set the current value (the text entered by the user or set from the outside) of this input field...
virtual void setValidChars(const std::string &validChars)
Set the valid input characters.
Definition: YInputField.cc:107
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:455
virtual void saveUserInput(YMacroRecorder *macroRecorder)
Save the widget&#39;s user input to a macro recorder.
Definition: YInputField.cc:184
YInputField(YWidget *parent, const std::string &label, bool passwordMode=false)
Constructor.
Definition: YInputField.cc:53
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
virtual ~YInputField()
Destructor.
Definition: YInputField.cc:64
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:564
virtual void recordWidgetProperty(YWidget *widget, const char *propertyName)=0
Record one widget property.
Class for widget properties.
Definition: YProperty.h:51
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YInputField.cc:168
bool shrinkable() const
Return &#39;true&#39; if this InputField should be very small.
Definition: YInputField.cc:88
std::string validChars()
Get the valid input characters.
Definition: YInputField.cc:101
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169