libyui  3.10.0
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 using std::string;
34 
35 
37 {
38  YInputFieldPrivate( string label, bool passwordMode )
39  : label( label )
40  , passwordMode( passwordMode )
41  , shrinkable( false )
42  , inputMaxLength( -1 )
43  {}
44 
45  string label;
46  bool passwordMode;
47  bool shrinkable;
48  string validChars;
49  int inputMaxLength;
50 };
51 
52 
53 
54 YInputField::YInputField( YWidget * parent, const string & label, bool passwordMode )
55  : YWidget( parent )
56  , priv( new YInputFieldPrivate( label, passwordMode ) )
57 {
58  YUI_CHECK_NEW( priv );
59 
60  // setDefaultStretchable( YD_HORIZ, true );
61  setDefaultStretchable( YD_VERT, false );
62 }
63 
64 
66 {
67  // NOP
68 }
69 
70 
71 string YInputField::label() const
72 {
73  return priv->label;
74 }
75 
76 
77 void YInputField::setLabel( const string & label )
78 {
79  priv->label = label;
80 }
81 
82 
84 {
85  return priv->passwordMode;
86 }
87 
88 
90 {
91  return priv->shrinkable;
92 }
93 
94 
95 void YInputField::setShrinkable( bool shrinkable )
96 {
97  priv->shrinkable = shrinkable;
98  // setDefaultStretchable( YD_HORIZ, ! shrinkable );
99 }
100 
101 
103 {
104  return priv->validChars;
105 }
106 
107 
108 void YInputField::setValidChars( const string & newValidChars )
109 {
110  priv->validChars= newValidChars;
111 }
112 
113 
115 {
116  return priv->inputMaxLength;
117 }
118 
119 
121 {
122  priv->inputMaxLength = len;
123 }
124 
125 
126 const YPropertySet &
128 {
129  static YPropertySet propSet;
130 
131  if ( propSet.isEmpty() )
132  {
133  /*
134  * @property string Value the input field's contents (the user input)
135  * @property string Label caption above the input field
136  * @property string ValidChars set of valid input characters
137  * @property integer InputMaxLength maximum number of input characters
138  */
139  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
140  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
141  propSet.add( YProperty( YUIProperty_ValidChars, YStringProperty ) );
142  propSet.add( YProperty( YUIProperty_InputMaxLength, YIntegerProperty ) );
143  propSet.add( YWidget::propertySet() );
144  }
145 
146  return propSet;
147 }
148 
149 
150 bool
151 YInputField::setProperty( const string & propertyName, const YPropertyValue & val )
152 {
153  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
154 
155  if ( propertyName == YUIProperty_Value ) setValue( val.stringVal() );
156  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
157  else if ( propertyName == YUIProperty_ValidChars ) setValidChars( val.stringVal() );
158  else if ( propertyName == YUIProperty_InputMaxLength ) setInputMaxLength( val.integerVal() );
159  else
160  {
161  return YWidget::setProperty( propertyName, val );
162  }
163 
164  return true; // success -- no special processing necessary
165 }
166 
167 
169 YInputField::getProperty( const string & propertyName )
170 {
171  propertySet().check( propertyName ); // throws exceptions if not found
172 
173  if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() );
174  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
175  else if ( propertyName == YUIProperty_ValidChars ) return YPropertyValue( validChars() );
176  else if ( propertyName == YUIProperty_InputMaxLength ) return YPropertyValue( inputMaxLength() );
177  else
178  {
179  return YWidget::getProperty( propertyName );
180  }
181 }
182 
183 
184 void
186 {
187  if ( ! passwordMode() ) // Don't record passwords in the macro file
188  {
189  macroRecorder->recordWidgetProperty( this, YUIProperty_Value );
190  }
191 }
192 
193 
194 const char *
196 {
197  if ( priv->passwordMode ) return "YPasswordField";
198  else return "YInputField";
199 }
std::string label() const
Get the label (the caption above the input field).
Definition: YInputField.cc:71
std::string validChars()
Get the valid input characters.
Definition: YInputField.cc:102
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YInputField.cc:151
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YInputField.cc:169
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YInputField.cc:127
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:108
virtual void setShrinkable(bool shrinkable=true)
Make this InputField very small.
Definition: YInputField.cc:95
YInputField(YWidget *parent, const std::string &label, bool passwordMode=false)
Constructor.
Definition: YInputField.cc:54
virtual void saveUserInput(YMacroRecorder *macroRecorder)
Save the widget's user input to a macro recorder.
Definition: YInputField.cc:185
virtual const char * widgetClass() const
Return a descriptive name of this widget class for logging, debugging etc.
Definition: YInputField.cc:195
virtual std::string value()=0
Get the current value (the text entered by the user or set from the outside) of this input field.
virtual void setInputMaxLength(int numberOfChars)
Set the maximum input length, i.e., the maximum number of characters the user can enter.
Definition: YInputField.cc:120
virtual void setLabel(const std::string &label)
Set the label (the caption above the input field).
Definition: YInputField.cc:77
bool shrinkable() const
Return 'true' if this InputField should be very small.
Definition: YInputField.cc:89
bool passwordMode() const
Returns 'true' if this input field is in password mode, i.e.
Definition: YInputField.cc:83
int inputMaxLength() const
The maximum input length, i.e., the maximum number of characters the user can enter.
Definition: YInputField.cc:114
virtual ~YInputField()
Destructor.
Definition: YInputField.cc:65
Abstract base class for macro recorders.
virtual void recordWidgetProperty(YWidget *widget, const char *propertyName)=0
Record one widget property.
A set of properties to check names and types against.
Definition: YProperty.h:198
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
Transport class for the value of simple properties.
Definition: YProperty.h:105
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
Class for widget properties.
Definition: YProperty.h:52
Abstract base class of all UI widgets.
Definition: YWidget.h:55
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457