libyui  3.3.1
YComboBox.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: YComboBox.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 "YComboBox.h"
31 #include "YUIException.h"
32 
33 
35 {
36  YComboBoxPrivate( bool editable )
37  : editable( editable )
38  , inputMaxLength( -1 )
39  {}
40 
41  bool editable;
42  std::string validChars;
43  int inputMaxLength;
44 };
45 
46 
47 
48 
49 YComboBox::YComboBox( YWidget * parent, const std::string & label, bool editable )
50  : YSelectionWidget( parent, label,
51  true ) // enforceSingleSelection
52  , priv( new YComboBoxPrivate( editable ) )
53 {
54  YUI_CHECK_NEW( priv );
55 }
56 
57 
59 {
60  // NOP
61 }
62 
63 
64 bool YComboBox::editable() const
65 {
66  return priv->editable;
67 }
68 
69 
70 std::string YComboBox::validChars()
71 {
72  return priv->validChars;
73 }
74 
75 
76 void YComboBox::setValidChars( const std::string & newValidChars )
77 {
78  priv->validChars= newValidChars;
79 }
80 
81 
83 {
84  return priv->inputMaxLength;
85 }
86 
87 
89 {
90  priv->inputMaxLength = len;
91 }
92 
93 
94 std::string YComboBox::value()
95 {
96  return text();
97 }
98 
99 
100 void YComboBox::setValue( const std::string & newText )
101 {
102  YItem * item = findItem( newText );
103 
104  if ( item )
105  {
107  item->setSelected();
108  setText( item->label() );
109  }
110  else
111  {
112  if ( editable() )
113  setText( newText );
114  else
115  {
116  YUI_THROW( YUIException( "Invalid value" ) );
117  }
118  }
119 }
120 
121 
122 void YComboBox::selectItem( YItem * item, bool selected )
123 {
124  // Check against null pointer and if the item actually belongs to this
125  // widget, deselect any previously selected item and select the new one
126  YSelectionWidget::selectItem( item, selected );
127 
128  if ( selected )
129  {
130  setText( item->label() );
131  }
132 }
133 
134 
135 YItem *
137 {
138  std::string currentText = text();
139 
140  // Make sure exactly this item is selected (and no other)
142 
143  // Try to find an item with this text
144  YItem * item = findItem( currentText );
145 
146  if ( item )
147  {
148  item->setSelected( true );
149  return item;
150  }
151 
152  return 0;
153 }
154 
155 
158 {
160 
161  // There can be no more than one selected item
162  YItem * item = selectedItem();
163 
164  if ( item )
165  selectedItems.push_back( item );
166 
167  return selectedItems;
168 }
169 
170 
171 const YPropertySet &
173 {
174  static YPropertySet propSet;
175 
176  if ( propSet.isEmpty() )
177  {
178  /*
179  * @property itemID | std::string Value ID of the selected item or the text the user entered
180  * @property std::string Label caption above the combo box
181  * @property itemList Items All items
182  * @property std::string ValidChars set of valid input characters
183  * @property integer InputMaxLength maximum number of input characters
184  * @property std::string IconPath Base path for icons
185  */
186  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
187  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
188  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
189  propSet.add( YProperty( YUIProperty_ValidChars, YStringProperty ) );
190  propSet.add( YProperty( YUIProperty_InputMaxLength, YIntegerProperty ) );
191  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
192  propSet.add( YWidget::propertySet() );
193  }
194 
195  return propSet;
196 }
197 
198 
199 bool
200 YComboBox::setProperty( const std::string & propertyName, const YPropertyValue & val )
201 {
202  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
203 
204  if ( propertyName == YUIProperty_Value ) return false; // Need special handling
205  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
206  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
207  else if ( propertyName == YUIProperty_ValidChars ) setValidChars( val.stringVal() );
208  else if ( propertyName == YUIProperty_InputMaxLength ) setInputMaxLength( val.integerVal() );
209  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
210  else
211  {
212  return YWidget::setProperty( propertyName, val );
213  }
214 
215  return true; // success -- no special processing necessary
216 }
217 
218 
220 YComboBox::getProperty( const std::string & propertyName )
221 {
222  propertySet().check( propertyName ); // throws exceptions if not found
223 
224  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
225  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
226  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
227  else if ( propertyName == YUIProperty_ValidChars ) return YPropertyValue( validChars() );
228  else if ( propertyName == YUIProperty_InputMaxLength ) return YPropertyValue( inputMaxLength() );
229  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
230  else
231  {
232  return YWidget::getProperty( propertyName );
233  }
234 }
std::string label() const
Return this widget&#39;s label (the caption above the item list).
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
std::string label() const
Return this item&#39;s label.
Definition: YItem.h:82
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YComboBox.cc:220
bool editable() const
Return &#39;true&#39; if this ComboBox is editable, i.e.
Definition: YComboBox.cc:64
virtual void setLabel(const std::string &newLabel)
Change this widget&#39;s label (the caption above the item list).
Transport class for the value of simple properties.
Definition: YProperty.h:104
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:38
Base class for various kinds of multi-value widgets.
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:428
virtual void setInputMaxLength(int numberOfChars)
Set the maximum input length, i.e., the maximum number of characters the user can enter...
Definition: YComboBox.cc:88
A set of properties to check names and types against.
Definition: YProperty.h:197
void setValue(const std::string &newText)
Set the value of this ComboBox by string: Try to find a list item with that label and select it...
Definition: YComboBox.cc:100
std::string validChars()
Get the valid input characters.
Definition: YComboBox.cc:70
YComboBox(YWidget *parent, const std::string &label, bool editable)
Constructor.
Definition: YComboBox.cc:49
void setSelected(bool sel=true)
Select or unselect this item.
Definition: YItem.h:114
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
Definition: YComboBox.cc:122
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YComboBox.cc:200
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
virtual std::string text()=0
Return this ComboBox&#39;s current value as text.
Class for widget properties.
Definition: YProperty.h:51
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
virtual ~YComboBox()
Destructor.
Definition: YComboBox.cc:58
virtual YItem * selectedItem()
Return the (first) selected item or 0 if none is selected or if this ComboBox is editable and the use...
Definition: YComboBox.cc:136
virtual void deselectAllItems()
Deselect all items.
void setIconBasePath(const std::string &basePath)
Set this widget&#39;s base path where to look up icons.
int inputMaxLength() const
The maximum input length, i.e., the maximum number of characters the user can enter.
Definition: YComboBox.cc:82
std::string value()
Return the value of this ComboBox:
Definition: YComboBox.cc:94
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
virtual void setText(const std::string &newText)=0
Set this ComboBox&#39;s current value as text.
Abstract base class of all UI widgets.
Definition: YWidget.h:54
virtual YItemCollection selectedItems()
Return all selected items.
Definition: YComboBox.cc:157
Base class for UI Exceptions.
Definition: YUIException.h:297
YItem * findItem(const std::string &itemLabel) const
Find the (first) item with the specified label.
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
std::string iconBasePath() const
Return this widget&#39;s base path where to look up icons as set with setIconBasePath().
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YComboBox.cc:172
virtual void setValidChars(const std::string &validChars)
Set the valid input characters.
Definition: YComboBox.cc:76