core/vgui/vgui_menu.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_menu.h
00002 #ifndef vgui_menu_h_
00003 #define vgui_menu_h_
00004 //:
00005 // \file
00006 // \brief  Representation of menus and menu items for pop-up and menubar menus.
00007 // \author fsm
00008 //
00009 //  Contains classes  vgui_menu_item  vgui_menu
00010 //
00011 //  These menu description structures are designed to work for both
00012 //  pop-up (modal) menus and stay-up (non-modal) menus. They can be
00013 //  passed by value.
00014 //
00015 // \verbatim
00016 //  Modifications
00017 //   07-Aug-1999 K.Y.McGaul - Added Doxygen style comments.
00018 // \endverbatim
00019 
00020 #include <vcl_string.h>
00021 #include <vcl_vector.h>
00022 #include <vcl_iosfwd.h>
00023 #include <vgui/vgui_key.h>
00024 #include <vgui/vgui_modifier.h>
00025 #include <vgui/vgui_command_sptr.h>
00026 
00027 class vgui_menu;
00028 
00029 typedef void (*vgui_menu_callback)(void const* client_data);
00030 typedef void (*vgui_menu_callback_no_client_data)();
00031 
00032 //: Representation of an item on a menu.
00033 //
00034 // Definition of vgui_menu::item = vgui_menu_item.
00035 // \verbatim
00036 // Each item is either a command :
00037 // . name
00038 // . action (command)
00039 // . invocation (eg. shortcuts)
00040 // or a submenu :
00041 // . name
00042 // . another menu
00043 // . invocation
00044 // or a toggle button :
00045 // . name
00046 // . toggle_command
00047 // . invocation
00048 // or a separator :
00049 // . -----------
00050 // \endverbatim
00051 struct vgui_menu_item
00052 {
00053   //: Constructor - create a default menu item.
00054   vgui_menu_item();
00055 
00056   //: Constructor - create menu item same as given item.
00057   vgui_menu_item(vgui_menu_item const &);
00058 
00059   //: Destructor
00060   ~vgui_menu_item();
00061 
00062   //: Name of item, "" for separators.
00063   vcl_string name;
00064 
00065   //: Pointer to the command to be executed.
00066   //  Non-zero for command items only.
00067   vgui_command_sptr cmnd;
00068 
00069   //: Pointer to a submenu.
00070   //  The object pointed to is owned (ie managed) by
00071   //  the menu. non-zero for submenu items only.
00072   vgui_menu *menu;
00073 
00074   //: Description of keyboard shortcut (makes no sense for separators).
00075   struct invocation {
00076     vgui_modifier mod;
00077     vgui_key key;
00078   } short_cut;
00079 
00080   //: Return true if the item is a command (item which performs an action).
00081   bool is_command()       const { return name!="" &&  (bool)cmnd && (menu == 0) && !is_toggle_button(); }
00082 
00083   //: Returns true if the item is a sub-menu.
00084   bool is_submenu()       const { return name!="" && !(bool)cmnd && (menu != 0) && !is_toggle_button(); }
00085 
00086   //: Returns true if the item is a toggle button.
00087   bool is_toggle_button() const;
00088 
00089   //: Returns true if the item is a separator.
00090   bool is_separator()     const { return name=="" && !(bool)cmnd && (menu == 0) && !is_toggle_button(); }
00091 };
00092 
00093 //: Representation of a menu for both pop-up and menubar menus.
00094 //
00095 // Building the menu:
00096 //
00097 // NB. empty strings are not acceptable names.
00098 class vgui_menu
00099 {
00100   vcl_vector<vgui_menu_item> items;
00101 
00102  public:
00103   //: Constructor - creates an empty menu.
00104   vgui_menu() {}
00105 
00106   //: Constructor - creates a menu same as the given menu.
00107   vgui_menu(vgui_menu const &);
00108 
00109   //: Make this menu equal to the given menu.
00110   vgui_menu& operator=(vgui_menu const &);
00111 
00112   //: Destructor - clears the menu.
00113   ~vgui_menu() { clear(); }
00114 
00115   //: Add given command to this menu.
00116   void add(vcl_string const &,
00117            vgui_command_sptr c,
00118            vgui_key key =vgui_KEY_NULL,
00119            vgui_modifier modifiers =vgui_MODIFIER_NULL);
00120 
00121   //: Add given callback function to this menu.
00122   void add(vcl_string const &,
00123            vgui_menu_callback_no_client_data f,
00124            vgui_key key =vgui_KEY_NULL,
00125            vgui_modifier modifiers =vgui_MODIFIER_NULL);
00126 
00127   //: Add callback function with client data to this menu.
00128   void add(vcl_string const &,
00129            vgui_menu_callback f,
00130            void const *client_data,
00131            vgui_key key =vgui_KEY_NULL,
00132            vgui_modifier modifiers =vgui_MODIFIER_NULL);
00133 
00134   //: Add given submenu to this menu.
00135   void add(vcl_string const &,
00136            vgui_menu const &,
00137            vgui_key key =vgui_KEY_NULL,
00138            vgui_modifier modifiers =vgui_MODIFIER_NULL);
00139 
00140   //: Add separator to this menu.
00141   void separator();        // a separator
00142 
00143   //: Add the given menu to the end of this menu.
00144   void include(vgui_menu const & that); // add 'that' to end of *this.
00145 
00146   //: Empty this menu.
00147   void clear() { items.clear(); }
00148 
00149   //: Return the number of items in this menu
00150   unsigned size() const { return items.size(); }
00151 
00152   //: Get the menu item using the [] index operator.
00153   vgui_menu_item const & operator[](unsigned i) const { return items[i]; }
00154 };
00155 
00156 vcl_ostream & operator<<(vcl_ostream &,vgui_menu const &);
00157 
00158 //--------------------------------------------------------------------------------
00159 
00160 #endif // vgui_menu_h_