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 }
YPropertySet::add
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YContextMenu::resolveShortcutConflicts
void resolveShortcutConflicts()
Resolve keyboard shortcut conflicts: Change shortcuts of menu items if there are duplicates in the re...
Definition: YContextMenu.cc:140
YSelectionWidget
Base class for various kinds of multi-value widgets.
Definition: YSelectionWidget.h:42
YWidget::end
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
YSelectionWidget::itemsEnd
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
Definition: YSelectionWidget.cc:296
YItem::setIndex
void setIndex(int index)
Set this item's index.
Definition: YItem.h:133
YContextMenu::~YContextMenu
virtual ~YContextMenu()
Destructor.
Definition: YContextMenu.cc:57
YItemIterator
YItemCollection::iterator YItemIterator
Mutable iterator over YItemCollection.
Definition: YItem.h:40
YContextMenu::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YContextMenu.cc:194
YItemCollection
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:38
YContextMenu::YContextMenu
YContextMenu()
Constructor.
Definition: YContextMenu.cc:48
YItem::index
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:138
YContextMenu::findMenuItem
YMenuItem * findMenuItem(int index)
Recursively find the first menu item with the specified index.
Definition: YContextMenu.cc:107
YContextMenu::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YContextMenu.cc:64
YItem::childrenBegin
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YItem.h:186
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:197
YContextMenu::deleteAllItems
virtual void deleteAllItems()
Delete all items.
Definition: YContextMenu.cc:99
YContextMenuPrivate
Definition: YContextMenu.cc:36
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YWidget::begin
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
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
YContextMenu::rebuildMenuTree
virtual void rebuildMenuTree()=0
Rebuild the displayed menu tree from the internally stored YMenuItems.
YTreeItem::hasChildren
virtual bool hasChildren() const
Return 'true' if this item has any child items.
Definition: YTreeItem.h:76
YTreeItem::childrenBegin
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YTreeItem.h:83
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
YTreeItem::childrenEnd
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YTreeItem.h:91
YItem::hasChildren
virtual bool hasChildren() const
Return 'true' if this item has any child items.
Definition: YItem.h:167
YContextMenu::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YContextMenu.cc:155
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YContextMenu::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YContextMenu.cc:177
YProperty
Class for widget properties.
Definition: YProperty.h:51
YSelectionWidget::setLabel
virtual void setLabel(const std::string &newLabel)
Change this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:105
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
YSelectionWidget::deleteAllItems
virtual void deleteAllItems()
Delete all items.
Definition: YSelectionWidget.cc:80
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:104
YMenuItem
Item class for menu items.
Definition: YMenuItem.h:35
YItem::childrenEnd
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YItem.h:195
YSelectionWidget::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YSelectionWidget.cc:264
YContextMenu::addItem
virtual void addItem(YItem *item_disown)
Add one item.
Definition: YContextMenu.cc:73
YSelectionWidget::addItem
virtual void addItem(YItem *item_disown)
Add one item.
Definition: YSelectionWidget.cc:186
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YSelectionWidget::itemsBegin
YItemIterator itemsBegin()
Return an iterator that points to the first item.
Definition: YSelectionWidget.cc:283
YItemConstIterator
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:42
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