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