libyui  3.4.2
YContextMenu.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: YContextMenu.cc
20 
21  Author: Thomas Goettlicher <tgoettlicher@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YUISymbols.h"
30 #include "YContextMenu.h"
31 #include "YMenuItem.h"
32 
33 
35 {
37  : nextSerialNo( 0 )
38  {}
39 
40  int nextSerialNo;
41 };
42 
43 
44 
45 
47  : YSelectionWidget( NULL, "test",
48  false ) // enforceSingleSelection
49  , priv( new YContextMenuPrivate() )
50 {
51  YUI_CHECK_NEW( priv );
52 }
53 
54 
56 {
57  // NOP
58 }
59 
60 
61 void
62 YContextMenu::addItems( const YItemCollection & itemCollection )
63 {
64  YSelectionWidget::addItems( itemCollection );
67 }
68 
69 
70 void
72 {
74  item->setIndex( ++(priv->nextSerialNo) );
75 
76  if ( item->hasChildren() )
77  assignUniqueIndex( item->childrenBegin(), item->childrenEnd() );
78 }
79 
80 
81 void
82 YContextMenu::assignUniqueIndex( YItemIterator begin, YItemIterator end )
83 {
84  for ( YItemIterator it = begin; it != end; ++it )
85  {
86  YItem * item = *it;
87 
88  item->setIndex( ++(priv->nextSerialNo) );
89 
90  if ( item->hasChildren() )
91  assignUniqueIndex( item->childrenBegin(), item->childrenEnd() );
92  }
93 }
94 
95 
96 void
98 {
100  priv->nextSerialNo = 0;
101 }
102 
103 
104 YMenuItem *
106 {
107  return findMenuItem( index, itemsBegin(), itemsEnd() );
108 }
109 
110 
111 YMenuItem *
113 {
114  for ( YItemConstIterator it = begin; it != end; ++it )
115  {
116  YMenuItem * item = dynamic_cast<YMenuItem *> (*it);
117 
118  if ( item )
119  {
120  if ( item->index() == wantedIndex )
121  return item;
122 
123  if ( item->hasChildren() )
124  {
125  YMenuItem * result = findMenuItem( wantedIndex, item->childrenBegin(), item->childrenEnd() );
126 
127  if ( result )
128  return result;
129  }
130  }
131  }
132 
133  return 0;
134 }
135 
136 
137 void
139 {
140  // TO DO
141  // TO DO
142  // TO DO
143 
144  // For every menu level, make sure keyboard shortcuts are unique within that menu level.
145  // If necessary, change some of them (move the '&' character to some other character).
146 
147 
148  // See YShortcutManager for more hints.
149 }
150 
151 
152 const YPropertySet &
154 {
155  static YPropertySet propSet;
156 
157  if ( propSet.isEmpty() )
158  {
159  /*
160  * @property std::string Label Label on the menu button
161  * @property itemList Items All menu items and submenus
162  * @property std::string IconPath Base path for icons (on menu items)
163  */
164  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
165  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
166  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
167  propSet.add( YWidget::propertySet() );
168  }
169 
170  return propSet;
171 }
172 
173 
174 bool
175 YContextMenu::setProperty( const std::string & propertyName, const YPropertyValue & val )
176 {
177  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
178 
179  if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
180  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
181  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
182  else
183  {
184  return YWidget::setProperty( propertyName, val );
185  }
186 
187  return true; // success -- no special processing necessary
188 }
189 
190 
192 YContextMenu::getProperty( const std::string & propertyName )
193 {
194  propertySet().check( propertyName ); // throws exceptions if not found
195 
196  if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
197  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
198  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
199  else
200  {
201  return YWidget::getProperty( propertyName );
202  }
203 }
YItemCollection::iterator YItemIterator
Mutable iterator over YItemCollection.
Definition: YItem.h:40
std::string label() const
Return this widget&#39;s label (the caption above the item list).
virtual bool hasChildren() const
Return &#39;true&#39; if this item has any child items.
Definition: YTreeItem.h:78
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YTreeItem.h:93
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
void setIndex(int index)
Set this item&#39;s index.
Definition: YItem.h:119
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
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YItem.h:172
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:430
virtual bool hasChildren() const
Return &#39;true&#39; if this item has any child items.
Definition: YItem.h:153
A set of properties to check names and types against.
Definition: YProperty.h:197
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:124
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YContextMenu.cc:62
YItemIterator itemsBegin()
Return an iterator that points to the first item.
void resolveShortcutConflicts()
Resolve keyboard shortcut conflicts: Change shortcuts of menu items if there are duplicates in the re...
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual void deleteAllItems()
Delete all items.
virtual ~YContextMenu()
Destructor.
Definition: YContextMenu.cc:55
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:455
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Class for widget properties.
Definition: YProperty.h:51
virtual void addItem(YItem *item_disown)
Add one item.
YMenuItem * findMenuItem(int index)
Recursively find the first menu item with the specified index.
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:42
Item class for menu items.
Definition: YMenuItem.h:35
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YItem.h:181
virtual void deleteAllItems()
Delete all items.
Definition: YContextMenu.cc:97
void setIconBasePath(const std::string &basePath)
Set this widget&#39;s base path where to look up icons.
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YTreeItem.h:85
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
virtual void rebuildMenuTree()=0
Rebuild the displayed menu tree from the internally stored YMenuItems.
virtual void addItem(YItem *item_disown)
Add one item.
Definition: YContextMenu.cc:71
YContextMenu()
Constructor.
Definition: YContextMenu.cc:46
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().