X11workbench Toolkit  1.0
menu.h
Go to the documentation of this file.
1 // _ //
3 // _ __ ___ ___ _ __ _ _ | |__ //
4 // | '_ ` _ \ / _ \| '_ \ | | | | | '_ \ //
5 // | | | | | || __/| | | || |_| | _| | | | //
6 // |_| |_| |_| \___||_| |_| \__,_|(_)_| |_| //
7 // //
8 // generic menu resource implementation //
9 // //
11 
12 /*****************************************************************************
13 
14  X11workbench - X11 programmer's 'work bench' application and toolkit
15  Copyright (c) 2010-2019 by Bob Frazier (aka 'Big Bad Bombastic Bob')
16 
17 
18  DISCLAIMER: The X11workbench application and toolkit software are supplied
19  'as-is', with no warranties, either implied or explicit.
20  Any claims to alleged functionality or features should be
21  considered 'preliminary', and might not function as advertised.
22 
23  MIT-like license:
24 
25  There is no restriction as to what you can do with this software, so long
26  as you include the above copyright notice and DISCLAIMER for any distributed
27  work that is equal to or derived from this one, along with this paragraph
28  that explains the terms of the license if the source is also being made
29  available. A "derived work" describes a work that uses a significant portion
30  of the source files or algorithms that are included with this one.
31  Specifically excluded from this are files that were generated by the software,
32  or anything that is included with the software that is part of another package
33  (such as files that were created or added during the 'configure' process).
34  Specifically included is the use of part or all of any of the X11 workbench
35  toolkit source or header files in your distributed application. If you do not
36  ship the source, the above copyright statement is still required to be placed
37  in a reasonably prominent place, such as documentation, splash screens, and/or
38  'about the application' dialog boxes.
39 
40  Use and distribution are in accordance with GPL, LGPL, and/or the above
41  MIT-like license. See COPYING and README files for more information.
42 
43 
44  Additional information at http://sourceforge.net/projects/X11workbench
45 
46 ******************************************************************************/
47 
62 // a menu bar is a window that contains one or more text menu items, displaying a popup menu
63 // whenever a text menu item is activated. The popup menu is displayed as needed.
64 
65 #ifndef MENU_H_INCLUDED
66 #define MENU_H_INCLUDED
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif // __cplusplus
71 
72 
76 #define WBMENU_RESERVE_DEFAULT (256 * sizeof(void *))
78 #define WBMENU_POPUP_HIGH_BIT 0x80000000
79 #define WBMENU_DYNAMIC_HIGH_BIT 0x40000000
80 #define WBMENU_POPUP_MASK 0x3fffffff
81 #define WBMENU_SEPARATOR -1
127 typedef struct tagWBMenuItem__
128 {
129  unsigned int uiTag;
130 
131  // the following data members are offsets from 'data' for each component of the menu item
132  // '-1' generically indicates "none"
136  int iHotKey;
137 
138  // the following data members are numeric properties (not offsets)
139  int iAction;
140  int nHotKey;
143  int iPosition;
144 
145  int nDataSize;
146  char data[4];
147 
148 } WBMenuItem;
149 
150 
185 typedef struct tagWBMenu
186 {
187  unsigned int uiTag;
188 
189  int iMenuID;
190 
192  int nItems;
193  int nMaxItems;
194 
195  struct tagWBMenu **ppPopups;
196  int nPopups;
198 
199 } WBMenu;
200 
204 #define WBMENU_TAG (*((const unsigned int *)"WBMM"))
205 
208 #define WBMENUITEM_TAG (*((const unsigned int *)"WBMI"))
209 
210 
211 //------------------------------
212 // construction and destruction
213 //------------------------------
214 
228 WBMenu *MBCreateMenu(int iID, int iPopup, const char *pszResource, int iReserveSpace);
229  // pass in full menu resource 'pszResource' including any referenced popup menus. If a popup menu
230  // isn't specified here it can be added later via 'MBAddPopupMenu'. Specify '-1' for iID to
231  // read ID (and popup specifier) from the menu resource. 'iPopup' is non-zero for popup, zero for bar
232 
243 void MBDestroyMenu(WBMenu *pMenu); // always destroy with this function
244 
256 WBMenu *MBCopyMenu(const WBMenu *pMenu, int iReserveSpace);
257 
268 int MBIsMenuValid(const WBMenu *pMenu);
269 
270 //------------
271 // menu items
272 //------------
273 
288 WBMenuItem *MBCreateMenuItem(const char **ppszResource); // create single menu item, and point *ppszResource to next item in resource
289 
300 void MBDestroyMenuItem(WBMenuItem *pMenuItem);
301 
312 int MBIsMenuItemValid(const WBMenuItem *pMenuItem);
313 
327 int MBAddMenuItem(WBMenu *pMenu, const WBMenuItem *pMenuItem, int iPos);
328 
340 void MBRemoveMenuItem(WBMenu *pMenu, int iPos); // deletes menu item - 'iPos' has same semantics as above
341 
342 //-------------
343 // popup menus
344 //-------------
345 
359 int MBAddPopupMenu(WBMenu *pMenu, const WBMenu *pPopupMenu); // use this when you add a popup menu item to add the actual popup menu
360 
376 WBMenu *MBFindPopupMenu(WBMenu *pMenu, int idPopup);
377 
389 void MBRemovePopupMenu(WBMenu *pMenu, int idPopup);
390 
391 
402 static __inline__ int MBMenuIsPopup(WBMenu *pMenu)
403 {
404  return pMenu && (pMenu->iMenuID & WBMENU_POPUP_HIGH_BIT);
405 }
406 
419 int MBMenuProcessHotKey(WBMenu *pMenu, XKeyEvent *pEvent); // menu owner calls this to check for menu-based hotkeys
420 
421 #ifdef __cplusplus
422 };
423 #endif // __cplusplus
424 
425 #endif // MENU_H_INCLUDED
426 
unsigned int uiTag
a 'tag' identifying this as a WBMenu
Definition: menu.h:187
int iTextWidth
width of menu text (in pixels; assign '-1' to calculate it)
Definition: menu.h:142
structure for managing menu items
Definition: menu.h:127
int nMaxItems
The maximum number of menu item entries that can be stored in 'ppItems'.
Definition: menu.h:193
WBMenu * MBCopyMenu(const WBMenu *pMenu, int iReserveSpace)
Create a copy of a WBMenu from an existing WBMenu.
Definition: menu.c:350
int iMenuItemText
offset in 'data' to null-byte terminated strings (-1 if none)
Definition: menu.h:133
WBMenuItem ** ppItems
An allocated array of menu items.
Definition: menu.h:191
WBMenu * MBFindPopupMenu(WBMenu *pMenu, int idPopup)
Locate a WBMenu 'popup' within a menu created by MBCreateMenu()
Definition: menu.c:326
structure for managing menu items
Definition: menu.h:185
struct tagWBMenu ** ppPopups
An allocated array of 'popup' menus contained by this menu.
Definition: menu.h:195
void MBRemovePopupMenu(WBMenu *pMenu, int idPopup)
Remove a WBMenu 'popup' from a menu created by MBCreateMenu(), freeing up its resources.
Definition: menu.c:339
int nItems
The number of menu item entries in the 'ppItems' array.
Definition: menu.h:192
int MBAddPopupMenu(WBMenu *pMenu, const WBMenu *pPopupMenu)
Add a WBMenuItem menu item to an existing WBMenu.
Definition: menu.c:307
unsigned int uiTag
a 'tag' identifying this as a WBMenuItem
Definition: menu.h:129
struct tagWBMenu WBMenu
structure for managing menu items
WBMenu * MBCreateMenu(int iID, int iPopup, const char *pszResource, int iReserveSpace)
Create a WBMenu from a text menu resource.
Definition: menu.c:107
int MBAddMenuItem(WBMenu *pMenu, const WBMenuItem *pMenuItem, int iPos)
Add a WBMenuItem menu item to an existing WBMenu.
Definition: menu.c:280
int nDataSize
total size of data
Definition: menu.h:145
#define WBMENU_POPUP_HIGH_BIT
Definition: menu.h:78
int iUnderscore
offset of (first) 'underscore' within menu text (-1 if none)
Definition: menu.h:134
void MBDestroyMenu(WBMenu *pMenu)
Destroy a WBMenu created by MBCreateMenu(), freeing up its resources.
Definition: menu.c:190
WBMenuItem * MBCreateMenuItem(const char **ppszResource)
Create a single allocated WBMenuItem structure from a text menu resource, advancing the source text p...
Definition: menu.c:670
int nPopups
The number of popup menu entries in the 'ppPopups' array.
Definition: menu.h:196
struct tagWBMenuItem__ WBMenuItem
structure for managing menu items
int iMenuID
menu identifier specified when menu was created (high bit set for popup)
Definition: menu.h:189
static __inline__ int MBMenuIsPopup(WBMenu *pMenu)
Indicate whether a 'WBMenu' refers to a popup menu.
Definition: menu.h:402
int nMaxPopups
The maximum number of popup menu entries that can be stored in 'ppPopups'.
Definition: menu.h:197
void MBRemoveMenuItem(WBMenu *pMenu, int iPos)
Remove a WBMenuItem from a menu created by MBCreateMenu(), freeing up its resources.
Definition: menu.c:297
int MBMenuProcessHotKey(WBMenu *pMenu, XKeyEvent *pEvent)
Event handler for menu hotkeys.
Definition: menu.c:1228
int iPosition
horizontal/vertical position of menu (in pixels; assign '-1' to calculate it)
Definition: menu.h:143
void MBDestroyMenuItem(WBMenuItem *pMenuItem)
Destroy a WBMenuItem created by MBCreateMenuItem(), freeing up its resources.
Definition: menu.c:267
int MBIsMenuItemValid(const WBMenuItem *pMenuItem)
Check whether a 'WBMenuItem' pointer is valid.
Definition: menu.c:244
int iHotKey
hotkey description (-1 if none)
Definition: menu.h:136
int iTooltipText
offset in 'data' to null-byte terminated strings (-1 if none)
Definition: menu.h:135
int iAction
Definition: menu.h:139
int MBIsMenuValid(const WBMenu *pMenu)
Check whether a 'WBMenu' pointer is valid.
Definition: menu.c:221