libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
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 
156 YItemCollection
158 {
159  YItemCollection selectedItems;
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 }