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