libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
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 *
112 YContextMenu::findMenuItem( int wantedIndex, YItemConstIterator begin, YItemConstIterator end )
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 }
virtual YItemIterator childrenEnd()
Definition: YTreeItem.h:93
virtual void addItems(const YItemCollection &itemCollection)
YItemIterator itemsEnd()
void setIndex(int index)
Definition: YItem.h:113
virtual void setLabel(const std::string &newLabel)
virtual YItemIterator childrenBegin()
Definition: YItem.h:166
void check(const std::string &propertyName) const
Definition: YProperty.cc:62
void add(const YProperty &prop)
Definition: YProperty.cc:120
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Definition: YWidget.cc:428
std::string iconBasePath() const
virtual void addItems(const YItemCollection &itemCollection)
Definition: YContextMenu.cc:62
std::string stringVal() const
Definition: YProperty.h:167
YItemIterator itemsBegin()
void resolveShortcutConflicts()
virtual const YPropertySet & propertySet()
Definition: YWidget.cc:393
virtual bool hasChildren() const
Definition: YTreeItem.h:78
int index() const
Definition: YItem.h:118
virtual void deleteAllItems()
virtual ~YContextMenu()
Definition: YContextMenu.cc:55
virtual YPropertyValue getProperty(const std::string &propertyName)
virtual YPropertyValue getProperty(const std::string &propertyName)
Definition: YWidget.cc:453
bool isEmpty() const
Definition: YProperty.h:250
virtual const YPropertySet & propertySet()
virtual void addItem(YItem *item_disown)
YMenuItem * findMenuItem(int index)
Definition: YItem.h:43
virtual YItemIterator childrenEnd()
Definition: YItem.h:175
std::string label() const
virtual bool hasChildren() const
Definition: YItem.h:147
virtual void deleteAllItems()
Definition: YContextMenu.cc:97
void setIconBasePath(const std::string &basePath)
YPropertyType type() const
Definition: YProperty.h:156
virtual YItemIterator childrenBegin()
Definition: YTreeItem.h:85
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
virtual void rebuildMenuTree()=0
virtual void addItem(YItem *item_disown)
Definition: YContextMenu.cc:71