tbx  0.7.3
menu.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef TBX_MENU_H_
26 #define TBX_MENU_H_
27 
28 #include "object.h"
29 #include "component.h"
30 
31 namespace tbx {
32 
33 class MenuItem;
34 class ResMenuItem;
35 class AboutToBeShownListener;
36 class HasBeenHiddenListener;
37 class SubMenuListener;
38 class MenuSelectionListener;
39 class Command;
40 
41 namespace res
42 {
43 class ResMenuItem;
44 }
45 
49 class Menu: public tbx::Object {
50 public:
51  enum { TOOLBOX_CLASS = 0x828c0 };
52 
59  Menu() {};
60 
66  Menu(const Menu &other) : Object(other._handle) {}
67 
74  Menu(const Object &other) : Object(other) {check_toolbox_class(Menu::TOOLBOX_CLASS);}
82  Menu(const char *template_name) : Object(template_name) {check_toolbox_class(Menu::TOOLBOX_CLASS);}
83 
89  Menu &operator=(const Menu &other) {_handle = other.handle(); return *this;}
90 
97  Menu &operator=(const Object &other) {_handle = other.handle(); check_toolbox_class(TOOLBOX_CLASS); return *this;}
98 
104  bool operator==(const Object &other) const {return (_handle == other.handle());}
105 
111  bool operator!=(const Object &other) const {return (_handle != other.handle());}
112 
114 
118  void help_message(const std::string &msg) {string_property(16,msg);}
122  std::string help_message() const {return string_property(17);}
126  int help_message_length() const {return string_property_length(17);}
127 
128  MenuItem add(const res::ResMenuItem &res_item, MenuItem *after = 0);
129  MenuItem insert(const res::ResMenuItem &res_item, MenuItem *before = 0);
130  void erase(ComponentId id);
131  void erase(MenuItem &item);
132 
136  int height() const {return int_property(22);}
137 
141  int width() const {return int_property(23);}
142 
146  void title(const std::string &title) {string_property(24, title);}
147 
151  std::string title() const {return string_property(25);}
155  int title_length() const {return string_property_length(25);}
156 
159 
162 
163  void add_submenu_listener(SubMenuListener *listener);
165 
168 };
169 
173 class MenuItem : public Component
174 {
175  MenuItem(ObjectId handle, ComponentId id) : Component(handle, id) {};
176  friend class Menu;
177 
178 public:
183 
189  MenuItem(const MenuItem &other) : Component(other.handle(), other.id()) {}
190 
196  MenuItem(const Component &other) : Component(other) {Menu check(_handle);}
197 
203  MenuItem &operator=(const MenuItem &other) {_handle = other._handle; _id = other._id; return *this;}
204 
210  MenuItem &operator=(const Component &other) {Menu check(other.handle()); _handle = other.handle(); _id = other.id(); return *this;}
211 
217  bool operator==(const MenuItem &other) {return (_handle == other._handle && _id == other._id);}
218 
224  bool operator!=(const MenuItem &other) {return (_handle != other._handle || _id != other._id);}
225 
229  Menu menu() {return Menu(_handle);}
230 
231  // Properties
232  void tick(bool set);
233  bool ticked() const;
234 
235  void fade(bool set);
236  bool faded() const;
237 
238  void text(const std::string &text);
239  std::string text() const;
240  int text_length() const;
241 
242  void sprite_name(const std::string &name);
243  std::string sprite_name() const;
244  int sprite_name_length() const;
245 
246  void submenu(const Object &object);
247  void clear_submenu();
248  bool has_submenu() const;
249  Object submenu();
250  void submenu_event(int id);
251  int submenu_event() const;
252 
253  void click_show(const Object &object, bool transient = true);
254  void clear_click_show();
255  bool has_click_show() const;
256  Object click_show(bool *transient = 0);
257  void click_event(int id);
258  int click_event() const;
259 
260  void help_message(const std::string &msg);
261  std::string help_message() const;
262  int help_message_length() const;
263 
264  void add_submenu_listener(SubMenuListener *listener);
266 
269 
270  void add_selection_command(Command *command);
271  void remove_selection_command(Command *command);
272 };
273 
274 }
275 
276 #endif /* TBX_MENU_H_ */
ObjectId _handle
Handle for toolbox object.
Definition: object.h:173
int help_message_length() const
Get the length of the buffer for the help message.
Definition: menu.cc:502
const ComponentId NULL_ComponentId
NULL component id.
Definition: handles.h:46
Class representing a toolbox menu.
Definition: menu.h:49
int click_event() const
Get the click event for this menu item.
Definition: menu.cc:474
void fade(bool set)
Fade the menu item.
Definition: menu.cc:259
Listener for about to be shown event.
Definition: abouttobeshownlistener.h:76
void check_toolbox_class(int class_id) const
Check if this objects toolbox class is as specified.
Definition: object.cc:122
bool faded() const
Check if the menu item is faded.
Definition: menu.cc:267
Menu & operator=(const Menu &other)
Assign a menu to an existing menu.
Definition: menu.h:89
bool ticked() const
Check if the menu item is ticked.
Definition: menu.cc:248
int title_length() const
Get the size of the buffer for the title.
Definition: menu.h:155
void add_submenu_listener(SubMenuListener *listener)
Add listener for mouse moving over the menu arrow when no other toolbox event is associated with this...
Definition: menu.cc:513
int text_length() const
Get the length of the buffer for the menu item text.
Definition: menu.cc:298
int string_property_length(int property_id) const
Gets the size of a string properties buffer.
Definition: object.cc:530
bool has_click_show() const
Check if menu item has a click show object.
Definition: menu.cc:422
int sprite_name_length() const
Get the length of the buffer for the sprite name.
Definition: menu.cc:326
int ComponentId
Type for underlying toolbox component id.
Definition: handles.h:33
int height() const
Return the height of the work area of the menu in OS units.
Definition: menu.h:136
Base class for commands in tbx.
Definition: command.h:36
void remove_selection_listener(MenuSelectionListener *listener)
Remove menu selection listener.
Definition: menu.cc:152
int help_message_length() const
Get the length of buffer for the help message text.
Definition: menu.h:126
Listener for event when mouse moves over the submenu arrow if no other toolbox event has been associa...
Definition: submenulistener.h:69
void erase(ComponentId id)
Erase the menu item with the given id.
Definition: menu.cc:218
MenuItem(const MenuItem &other)
Construct a menu item from an existing menu item.
Definition: menu.h:189
MenuItem()
Construct an uninitialised menu item.
Definition: menu.h:182
bool operator==(const Object &other) const
Checks if this menu refers to the same underlying toolbox object as the given object.
Definition: menu.h:104
void add_selection_command(Command *command)
Add command to be run when this menu item is selected.
Definition: menu.cc:557
void remove_about_to_be_shown_listener(AboutToBeShownListener *listener)
Remover listener triggered when a menu is about to be shown.
Definition: menu.cc:81
void remove_submenu_listener(SubMenuListener *listener)
Remove listener for mouse moving over the menu arrow when no other toolbox event is associated with t...
Definition: menu.cc:524
Listener for selection of menu items that do not have another event associated with them...
Definition: menuselectionlistener.h:42
void add_submenu_listener(SubMenuListener *listener)
Add listener for mouse moving over the menu arrow when no other toolbox event is assosiated with this...
Definition: menu.cc:116
void help_message(const std::string &msg)
Set the help message text for the menu.
Definition: menu.h:118
ComponentId id() const
Get the component ID of this component.
Definition: component.h:103
MenuItem item(ComponentId id)
Retrieve the menu item for a given component id.
Definition: menu.cc:58
Base class for components in an object.
Definition: component.h:42
int int_property(int property_id) const
Return the value of a property that returns an integer.
Definition: object.cc:435
MenuItem insert(const res::ResMenuItem &res_item, MenuItem *before=0)
Insert a new menu entry in the menu.
Definition: menu.cc:198
void remove_has_been_hidden_listener(HasBeenHiddenListener *listener)
Remove listener triggered when a menu is about to be shown.
Definition: menu.cc:105
std::string string_property(int property_id) const
Return the value of a property that returns text.
Definition: object.cc:477
MenuItem add(const res::ResMenuItem &res_item, MenuItem *after=0)
Add a new menu entry to the menu.
Definition: menu.cc:169
void remove_submenu_listener(SubMenuListener *listener)
Remove listener for mouse moving over the menu arrow when no other toolbox event is assosiated with t...
Definition: menu.cc:127
unsigned int ObjectId
Type for underlying toolbox object id.
Definition: handles.h:31
Class representing a single menu item on a menu.
Definition: menu.h:173
void remove_selection_listener(MenuSelectionListener *listener)
Remove menu selection listener.
Definition: menu.cc:545
Menu()
Construct an uninitiased menu.
Definition: menu.h:59
bool operator!=(const MenuItem &other)
Check if this menu item refers to the same underlying toolbox menu option.
Definition: menu.h:224
std::string help_message() const
Get the help message text.
Definition: menu.h:122
ObjectId handle() const
Return the underlying toolbox object id this object references.
Definition: object.h:93
Menu item object.
Definition: resmenu.h:46
MenuItem & operator=(const MenuItem &other)
Assign to another menu item.
Definition: menu.h:203
bool has_submenu() const
Returns true if this object has a submenu.
Definition: menu.cc:350
bool operator!=(const Object &other) const
Checks if this menu refers to the same underlying toolbox object as the given object.
Definition: menu.h:111
void clear_submenu()
Clear the submenu from this item.
Definition: menu.cc:342
void clear_click_show()
Clear the click show object.
Definition: menu.cc:405
void add_selection_listener(MenuSelectionListener *listener)
Add listener for menu item selection.
Definition: menu.cc:535
void add_selection_listener(MenuSelectionListener *listener)
Add listener for menu item selection.
Definition: menu.cc:142
int submenu_event() const
Get the event raised when the mouse is moved over the submenu arrow.
Definition: menu.cc:378
std::string text() const
Get the text of the menu item.
Definition: menu.cc:289
int width() const
Return the width of the work area of the menu in OS units.
Definition: menu.h:141
void add_has_been_hidden_listener(HasBeenHiddenListener *listener)
Add listener triggered when a menu is hidden.
Definition: menu.cc:95
MenuItem(const Component &other)
Construct a menu item from a Component.
Definition: menu.h:196
Listener for an object has been hidden event.
Definition: hasbeenhiddenlistener.h:36
Menu & operator=(const Object &other)
Assign a menu to an existing object.
Definition: menu.h:97
Object submenu()
Return submenu or Object::null if none.
Definition: menu.cc:358
std::string help_message() const
Get the items help message.
Definition: menu.cc:494
bool operator==(const MenuItem &other)
Check if this menu item refers to the same underlying toolbox menu option.
Definition: menu.h:217
std::string title() const
Get the title of the menu.
Definition: menu.h:151
Menu(const Object &other)
Construct a menu from another toolbox object.
Definition: menu.h:74
void title(const std::string &title)
Set the title of the menu.
Definition: menu.h:146
Component()
Construct an uninitialised component.
Definition: component.h:55
ObjectId _handle
Underlying toolbox handle.
Definition: component.h:45
Menu(const Menu &other)
Construct a menu from another menu.
Definition: menu.h:66
void click_show(const Object &object, bool transient=true)
Set object to show if menu item is clicked.
Definition: menu.cc:390
void tick(bool set)
Put a tick by the menu item.
Definition: menu.cc:240
void add_about_to_be_shown_listener(AboutToBeShownListener *listener)
Add listener triggered when a menu is about to be shown.
Definition: menu.cc:71
MenuItem & operator=(const Component &other)
Assign a menuitem field to refer to the same underlying toolbox component as an existing component...
Definition: menu.h:210
int _id
Underlying toolbox component id.
Definition: component.h:46
Class to manipulate a toolbox object.
Definition: object.h:50
Menu(const char *template_name)
Create the toolbox object with the given name from the Toolbox resources for the application and assi...
Definition: menu.h:82
ObjectId handle() const
Return the object handle for his component.
Definition: component.h:95
std::string sprite_name() const
Get the sprite name for a sprite menu item.
Definition: menu.cc:317
Menu menu()
Return the menu this menu item belongs to.
Definition: menu.h:229