tbx  0.7.5
resmenu.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_RES_MENU_H
26 #define TBX_RES_MENU_H
27 
28 #include "resobject.h"
29 #include "resiteratorbase.h"
30 #include "../handles.h"
31 
32 namespace tbx
33 {
34 class Menu;
35 namespace res
36 {
37 
38 class ResMenu;
39 
40 const int MENU_DATA_SIZE = 32;
41 const int MENU_ITEM_SIZE = 40;
42 
46 class ResMenuItem : public ResBase
47 {
48  friend class ResMenu;
49 
50  ResMenuItem(void *item_header, int size, ResData *data);
51 
52  // Allow Menu to add items
53  friend class tbx::Menu;
54  char *header() const {return _impl->header();}
55 
56 public:
62  ResMenuItem(const ResMenuItem &other) : ResBase(other)
63  {
64  }
65 
66  virtual ~ResMenuItem() {}
67 
74  {
75  ResBase::operator=(other);
76  return *this;
77  }
78 
79  ResMenuItem();
80 
81 protected:
86  {
87  if (!_impl->unique())
88  {
89  ResImpl *new_impl = new ResImpl(*_impl);
90  _impl->release();
91  _impl = new_impl;
92  }
93  }
94 
95 public:
103  unsigned int flags() const {return uint_value(0);}
104 
111  void flags(unsigned int value) {uint_value(0, value);}
112 
118  bool ticked() const {return flag(0,1);}
124  void ticked(bool t) {flag(0,1,t);}
125 
131  bool dotted_line() const {return flag(0,2);}
137  void dotted_line(bool value) {flag(0,2,value);}
138 
144  bool faded() const {return flag(0,256);}
150  void faded(bool value) {flag(0,256, value);}
151 
159  bool sprite() const {return flag(0,512);}
160 
161  // TODO: Changing this flag changes in which table the text is stored (string or messages)
162  // void sprite(bool value) {flag(0,512,value);}
163 
171  bool has_submenu() const {return flag(0,1024);}
179  void has_submenu(bool value) {flag(0,1024, value);}
188  bool generate_submenu_event() const {return flag(0,2048);}
197  void generate_submenu_event(bool value) {flag(0,2048, value);}
203  bool show_transient() const {return flag(0,4096);}
209  void show_transient(bool value) {flag(0,4096, value);}
210 
214  ComponentId component_id() const {return int_value(4);}
221 
229  const char *text() const {return string(8);}
239  void text(const char *value, int max_length = -1) {make_writeable(); _impl->text_with_length(8, value, max_length, sprite());}
249  void text(const std::string &value, int max_length = -1) {make_writeable(); _impl->text_with_length(8, value, max_length, sprite());}
253  int max_text() const {return int_value(12);}
254 
260  const char *click_show() const {return string(16);}
266  void click_show(const char *show_name) {string(16, show_name);}
272  void click_show(const std::string &show_name) {string(16, show_name);}
273 
279  const char *submenu_show() const {return string(20);}
285  void submenu_show(char *show_name) {string(20, show_name);}
291  void submenu_show(const std::string &show_name) {string(20, show_name);}
297  int submenu_event() const {return int_value(24);}
303  void submenu_event(int event_id) {int_value(24, event_id);}
309  int click_event() const {return int_value(28);}
315  void click_event(int event_id) {int_value(28, event_id);}
316 
322  const char *help_message() const {return message(32);}
323 
331  void help_message(const char *value, int max_length = -1) {message_with_length(32, value, max_length);}
339  void help_message(const std::string &value, int max_length = -1) {message_with_length(32, value, max_length);}
340 
344  int max_help_message() const {return int_value(36);}
345 };
346 
350 class ResMenu : public ResObject
351 {
352  public:
353  enum { CLASS_ID = 0x828c0};
354 
359  ResMenu(std::string name) :
360  ResObject(name, CLASS_ID, 102, 36 + 32)
361  {
362  init_message(4, 0); // Title bar
363  init_message(12, 0); // Help message
364  about_to_be_shown_event(-1);
365  has_been_hidden_event(-1);
366  }
367 
373  ResMenu(const ResMenu &other) : ResObject(other) {}
374 
380  ResMenu(const ResObject &other) : ResObject(other) {check_class_id(CLASS_ID);}
381 
387  ResMenu&operator=(const ResMenu&other)
388  {
389  ResObject::operator=(other);
390  return *this;
391  }
392 
399  {
400  other.check_class_id(CLASS_ID);
401  ResObject::operator =(other);
402  return *this;
403  }
404 
412  unsigned int flags() const {return uint_value(0);}
413 
422  void flags(unsigned int value) {uint_value(0, value);}
423 
429  bool generate_about_to_be_shown() const {return flag(0, 1);}
435  void generate_about_to_be_shown(bool generate) {flag(0, 1, generate);}
436 
442  bool generate_has_been_hidden() const {return flag(0, 2);}
448  void generate_has_been_hidden(bool generate) {flag(0, 2, generate);}
449 
455  const char *title() const {return message(4);}
456 
464  void title(const char *value, int max_length = -1) {message_with_length(4, value, max_length);}
472  void title(const std::string &value, int max_length = -1) {message_with_length(4, value, max_length);}
473 
477  int max_title() const {return int_value(8);}
478 
484  const char *help_message() const {return message(12);}
485 
493  void help_message(const char *value, int max_length = -1) {message_with_length(12, value, max_length);}
501  void help_message(const std::string &value, int max_length = -1) {message_with_length(12, value, max_length);}
502 
506  int max_help_message() const {return int_value(16);}
507 
513  int about_to_be_shown_event() const {return int_value(20);}
514 
520  void about_to_be_shown_event(int event_id) {int_value(20, event_id);}
521 
527  int has_been_hidden_event() const {return int_value(24);}
528 
534  void has_been_hidden_event(int event_id) {int_value(24, event_id);}
535 
539  int item_count() const {return int_value(28);}
540 
542 
543  ResMenuItem item_at(int index) const;
544  ResMenuItem item_at_offset(int item_offset) const;
545 
546 
550  class const_iterator : public ResIteratorBase<ResMenu>
551  {
552  const_iterator(const ResMenu *menu, int offset) : ResIteratorBase<ResMenu>(menu, offset) {};
553  friend class ResMenu;
554  public:
560  const_iterator &operator++() {_offset += MENU_ITEM_SIZE; return *this;}
566  const_iterator operator++(int) {const_iterator temp(*this); _offset += MENU_ITEM_SIZE; return temp;}
572  ResMenuItem operator*() const {return _object->item_at_offset(_offset);}
573  };
574  friend class const_iterator;
575 
582  class iterator : public ResIteratorBase<ResMenu>
583  {
584  iterator(const ResMenu *menu, int offset) : ResIteratorBase<ResMenu>(menu, offset) {};
585  friend class ResMenu;
586  public:
592  iterator &operator++() {_offset += MENU_ITEM_SIZE; return *this;}
598  iterator operator++(int) {iterator temp(*this); _offset += MENU_ITEM_SIZE; return temp;}
604  ResMenuItem operator*() const {return _object->item_at_offset(_offset);}
605  };
606  friend class iterator;
607 
608  const_iterator begin() const;
609  const_iterator end() const;
610  const_iterator cbegin() const;
611  const_iterator cend() const;
612  const_iterator find(ComponentId component_id) const;
613 
614  iterator begin();
615  iterator end();
616  iterator find(ComponentId component_id);
617 
618  bool contains(ComponentId component_id) const;
619  void add(const ResMenuItem &item);
620  void replace(const ResMenuItem &item);
621  void erase(ComponentId component_id);
622 
623  iterator insert(iterator pos, const ResMenuItem &item);
624  iterator replace(iterator pos, const ResMenuItem &item);
625  iterator erase(iterator pos);
626 };
627 
628 }
629 }
630 
631 #endif
int max_help_message() const
Get the maximum help message length.
Definition: resmenu.h:344
ResMenu(std::string name)
Constructor and empty ResMenu.
Definition: resmenu.h:359
ResMenuItem & operator=(const ResMenuItem &other)
Assign this menu item to be equal to another.
Definition: resmenu.h:73
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
Base class for Resource component iterators.
Definition: resiteratorbase.h:8
Class representing a toolbox menu.
Definition: menu.h:49
bool faded() const
Check if entry is faded.
Definition: resmenu.h:144
iterator operator++(int)
Increment iterator.
Definition: resmenu.h:598
void submenu_show(const std::string &show_name)
Set the name of the sub menu to show for this menu item.
Definition: resmenu.h:291
Menu object template class.
Definition: resmenu.h:350
Base class for objects, gadgets and menu items from ressources.
Definition: resbase.h:252
void flags(unsigned int value)
Set all flags as a word.
Definition: resmenu.h:111
void message_with_length(int offset, const char *value, int length=-1)
Assign a message where the length is at offset+4.
Definition: resbase.cc:1639
bool flag(int offset, int mask) const
Check if any of the bits in a mask are set.
Definition: resbase.h:352
int max_title() const
Get the maximum title length.
Definition: resmenu.h:477
void has_submenu(bool value)
Set if this entry has a sub menu.
Definition: resmenu.h:179
int item_count() const
Get the number of menu entries.
Definition: resmenu.h:539
int ComponentId
Type for underlying toolbox component id.
Definition: handles.h:33
const_iterator operator++(int)
Increment constant iterator.
Definition: resmenu.h:566
unsigned int uint_value(int offset) const
Get an unsigned integer value.
Definition: resbase.h:324
void check_class_id(int class_id) const
Check if this objects class id is as specified.
Definition: resobject.cc:383
void submenu_event(int event_id)
Set the event ID generated when sub menu is shown.
Definition: resmenu.h:303
bool show_transient() const
Check if shown object will be shown transiently.
Definition: resmenu.h:203
bool generate_submenu_event() const
Check if the sub menu event will be generated.
Definition: resmenu.h:188
void title(const char *value, int max_length=-1)
Set the title.
Definition: resmenu.h:464
Classes to use/edit toolbox resources in memory.
void generate_submenu_event(bool value)
Set if the sub menu event will be generated.
Definition: resmenu.h:197
Iterator for menu items.
Definition: resmenu.h:582
ComponentId component_id() const
Get component ID of menu item.
Definition: resmenu.h:214
void generate_has_been_hidden(bool generate)
Set if event will be generated when the menu has been removed from the screen.
Definition: resmenu.h:448
void component_id(ComponentId id)
Set component ID.
Definition: resmenu.h:220
bool dotted_line() const
Check if this entry is followed by a dotted line.
Definition: resmenu.h:131
ResMenu & operator=(const ResObject &other)
Assignment from ResObject.
Definition: resmenu.h:398
bool generate_has_been_hidden() const
Check if event will be generated when the menu has been removed from the screen.
Definition: resmenu.h:442
int click_event() const
Get the event ID generated when the item is clicked.
Definition: resmenu.h:309
const char * help_message() const
Get the menu help message.
Definition: resmenu.h:484
const char * text() const
Get text or sprite name for entry.
Definition: resmenu.h:229
const char * title() const
Get the iconbar icon title.
Definition: resmenu.h:455
void submenu_show(char *show_name)
Set the name of the sub menu to show for this menu item.
Definition: resmenu.h:285
int max_help_message() const
Get the maximum help message length.
Definition: resmenu.h:506
void make_writeable()
Make item writeable.
Definition: resmenu.h:85
const char * help_message() const
Get the menu item help message.
Definition: resmenu.h:322
void text(const char *value, int max_length=-1)
Set text or sprite name for entry.
Definition: resmenu.h:239
ResMenuItem(const ResMenuItem &other)
Create a ResMenuItem as a copy of another.
Definition: resmenu.h:62
void ticked(bool t)
Set if the item is ticked.
Definition: resmenu.h:124
int int_value(int offset) const
Get an integer value.
Definition: resbase.h:316
void click_show(const std::string &show_name)
Set the name of the object to show on click.
Definition: resmenu.h:272
const char * string(int offset) const
Return a string at the given offset.
Definition: resbase.cc:1555
ResMenu(const ResObject &other)
Construct from a ResObject.
Definition: resmenu.h:380
unsigned int flags() const
Get all flags as a word raw access.
Definition: resmenu.h:103
int about_to_be_shown_event() const
Get the event to generate when the menu is shown.
Definition: resmenu.h:513
void help_message(const std::string &value, int max_length=-1)
Set the item help message.
Definition: resmenu.h:339
void dotted_line(bool value)
Set if this entry is followed by a dotted line.
Definition: resmenu.h:137
bool sprite() const
Check if this entry shows a sprite.
Definition: resmenu.h:159
iterator & operator++()
Increment iterator.
Definition: resmenu.h:592
const char * submenu_show() const
Get the name of the sub menu to show for this menu item.
Definition: resmenu.h:279
int max_text() const
Maximum space for text or sprite name.
Definition: resmenu.h:253
bool has_submenu() const
Check if this entry has a sub menu.
Definition: resmenu.h:171
int submenu_event() const
Get the event ID generated when sub menu is shown.
Definition: resmenu.h:297
ResMenu(const ResMenu &other)
Copy constructor.
Definition: resmenu.h:373
const char * click_show() const
Get the name of the object to show on click.
Definition: resmenu.h:260
Menu item object.
Definition: resmenu.h:46
const char * message(int offset) const
Get the message string at the given offset.
Definition: resbase.cc:1602
bool generate_about_to_be_shown() const
Check if about to be shown event will be generated.
Definition: resmenu.h:429
int has_been_hidden_event() const
Get the event to generate when menu is hidden.
Definition: resmenu.h:527
void faded(bool value)
Set if entry is faded.
Definition: resmenu.h:150
void click_show(const char *show_name)
Set the name of the object to show on click.
Definition: resmenu.h:266
Constant iterator for menu items.
Definition: resmenu.h:550
ResObject & operator=(const ResObject &other)
Assignment.
Definition: resobject.cc:109
void help_message(const char *value, int max_length=-1)
Set the item help message.
Definition: resmenu.h:331
void flags(unsigned int value)
Set all flags as a word.
Definition: resmenu.h:422
ResBase & operator=(const ResBase &other)
Assignment.
Definition: resbase.cc:1534
unsigned int flags() const
Get all flags as a word raw access.
Definition: resmenu.h:412
Base class for a resource object that can be edited.
Definition: resobject.h:52
void click_event(int event_id)
Set the event ID generated when the item is clicked.
Definition: resmenu.h:315
void help_message(const std::string &value, int max_length=-1)
Set the help message.
Definition: resmenu.h:501
ResMenuItem operator*() const
Get the menu item this constant iterator refers to.
Definition: resmenu.h:572
ResMenuItem()
Construct a ResMenuItem with flags unset, item text "" and show objects and help message null...
Definition: resmenu.cc:38
void about_to_be_shown_event(int event_id)
Set the event to generate when the menu is shown.
Definition: resmenu.h:520
ResMenuItem operator*() const
Get the menu item this iterator refers to.
Definition: resmenu.h:604
void help_message(const char *value, int max_length=-1)
Set the help message.
Definition: resmenu.h:493
void show_transient(bool value)
Set if shown object will be shown transiently.
Definition: resmenu.h:209
void has_been_hidden_event(int event_id)
Set the event to generate when the menu is hidden.
Definition: resmenu.h:534
const_iterator & operator++()
Increment constant iterator.
Definition: resmenu.h:560
ResMenu & operator=(const ResMenu &other)
Assignment.
Definition: resmenu.h:387
void text(const std::string &value, int max_length=-1)
Set text or sprite name for entry.
Definition: resmenu.h:249
void generate_about_to_be_shown(bool generate)
Set if about to be shown event will be generated.
Definition: resmenu.h:435
void title(const std::string &value, int max_length=-1)
Set the title.
Definition: resmenu.h:472
ResImpl * _impl
Internal Resource implementation.
Definition: resbase.h:255
bool ticked() const
Check if the item is ticked.
Definition: resmenu.h:118
void init_message(int offset, const char *value)
Initialise a message in a constructor helper.
Definition: resbase.cc:1736