libyui  3.0.5
 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  if ( ! currentText.empty() )
141  {
142  // Try to find an item with this text
143 
144  YItem * item = findItem( currentText );
145 
146  if ( item )
147  {
148  // Make sure exactly this item is selected (and no other)
149 
151  item->setSelected( true );
152 
153  return item;
154  }
155  }
156  else
157  {
159  }
160 
161  return 0;
162 }
163 
164 
165 YItemCollection
167 {
168  YItemCollection selectedItems;
169 
170  // There can be no more than one selected item
171  YItem * item = selectedItem();
172 
173  if ( item )
174  selectedItems.push_back( item );
175 
176  return selectedItems;
177 }
178 
179 
180 const YPropertySet &
182 {
183  static YPropertySet propSet;
184 
185  if ( propSet.isEmpty() )
186  {
187  /*
188  * @property itemID | std::string Value ID of the selected item or the text the user entered
189  * @property std::string Label caption above the combo box
190  * @property itemList Items All items
191  * @property std::string ValidChars set of valid input characters
192  * @property integer InputMaxLength maximum number of input characters
193  * @property std::string IconPath Base path for icons
194  */
195  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
196  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
197  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
198  propSet.add( YProperty( YUIProperty_ValidChars, YStringProperty ) );
199  propSet.add( YProperty( YUIProperty_InputMaxLength, YIntegerProperty ) );
200  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
201  propSet.add( YWidget::propertySet() );
202  }
203 
204  return propSet;
205 }
206 
207 
208 bool
209 YComboBox::setProperty( const std::string & propertyName, const YPropertyValue & val )
210 {
211  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
212 
213  if ( propertyName == YUIProperty_Value ) return false; // Need special handling
214  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
215  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
216  else if ( propertyName == YUIProperty_ValidChars ) setValidChars( val.stringVal() );
217  else if ( propertyName == YUIProperty_InputMaxLength ) setInputMaxLength( val.integerVal() );
218  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
219  else
220  {
221  return YWidget::setProperty( propertyName, val );
222  }
223 
224  return true; // success -- no special processing necessary
225 }
226 
227 
229 YComboBox::getProperty( const std::string & propertyName )
230 {
231  propertySet().check( propertyName ); // throws exceptions if not found
232 
233  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
234  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
235  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
236  else if ( propertyName == YUIProperty_ValidChars ) return YPropertyValue( validChars() );
237  else if ( propertyName == YUIProperty_InputMaxLength ) return YPropertyValue( inputMaxLength() );
238  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
239  else
240  {
241  return YWidget::getProperty( propertyName );
242  }
243 }