1 /**
2  * @file menu.h
3  * @author Joe Wingbermuehle
4  * @date 2004-2006
5  *
6  * @brief Header for the menu functions.
7  *
8  */
9 
10 #ifndef MENU_H
11 #define MENU_H
12 
13 #include "timing.h"
14 
15 struct ScreenType;
16 
17 /** Enumeration of menu action types. */
18 typedef unsigned char MenuActionType;
19 #define MA_NONE               0
20 #define MA_EXECUTE            1
21 #define MA_DESKTOP            2
22 #define MA_SENDTO             3
23 #define MA_LAYER              4
24 #define MA_STICK              5
25 #define MA_MAXIMIZE           6
26 #define MA_MAXIMIZE_H         7
27 #define MA_MAXIMIZE_V         8
28 #define MA_MINIMIZE           9
29 #define MA_RESTORE            10
30 #define MA_SHADE              11
31 #define MA_MOVE               12
32 #define MA_RESIZE             13
33 #define MA_KILL               14
34 #define MA_CLOSE              15
35 #define MA_EXIT               16
36 #define MA_RESTART            17
37 #define MA_DYNAMIC            18
38 #define MA_SENDTO_MENU        19
39 #define MA_DESKTOP_MENU       20
40 #define MA_WINDOW_MENU        21
41 #define MA_ACTION_MASK        0x7F
42 #define MA_GROUP_MASK         0x80
43 
44 /** Structure to represent a menu action for callbacks. */
45 typedef struct MenuAction {
46 
47    void *context;                /**< Action context (client, etc.). */
48 
49    /* Extra data for the action. */
50    char *str;
51    unsigned value;
52 
53    MenuActionType type;          /**< Type of action. */
54 
55 } MenuAction;
56 
57 /** Enumeration of possible menu elements. */
58 typedef unsigned char MenuItemType;
59 #define MENU_ITEM_NORMAL      0  /**< Normal menu item (button). */
60 #define MENU_ITEM_SUBMENU     1  /**< Submenu. */
61 #define MENU_ITEM_SEPARATOR   2  /**< Item separator. */
62 
63 /** Structure to represent a menu item. */
64 typedef struct MenuItem {
65 
66    MenuItemType type;      /**< The menu item type. */
67    char *name;             /**< Name to display (or NULL). */
68    char *tooltip;          /**< Tooltip to display (or NULL). */
69    MenuAction action;      /**< Action to take if selected (or NULL). */
70    char *iconName;         /**< Name of an icon to show (or NULL). */
71    struct Menu *submenu;   /**< Submenu (or NULL). */
72    struct MenuItem *next;  /**< Next item in the menu. */
73 
74    /** An icon for this menu item.
75     * This field is handled by menu.c if iconName is set. */
76    struct IconNode *icon;  /**< Icon to display. */
77 
78 } MenuItem;
79 
80 /** Structure to represent a menu or submenu. */
81 typedef struct Menu {
82 
83    /* These fields must be set before calling ShowMenu */
84    struct MenuItem *items; /**< Menu items. */
85    char *label;            /**< Menu label (NULL for no label). */
86    char *dynamic;          /**< Generating command of dynamic menu. */
87    int itemHeight;         /**< User-specified menu item height. */
88 
89    /* These fields are handled by menu.c */
90    Window window;          /**< The menu window. */
91    Pixmap pixmap;          /**< Pixmap where the menu is rendered. */
92    int x;                  /**< The x-coordinate of the menu. */
93    int y;                  /**< The y-coordinate of the menu. */
94    int width;              /**< The width of the menu. */
95    int height;             /**< The height of the menu. */
96    int currentIndex;       /**< The current menu selection. */
97    int lastIndex;          /**< The last menu selection. */
98    unsigned int itemCount; /**< Number of menu items (excluding separators). */
99    int parentOffset;       /**< y-offset of this menu wrt the parent. */
100    int textOffset;         /**< x-offset of text in the menu. */
101    int *offsets;           /**< y-offsets of menu items. */
102    struct Menu *parent;    /**< The parent menu (or NULL). */
103    const struct ScreenType *screen;
104    int mousex, mousey;
105    TimeType lastTime;
106 
107 } Menu;
108 
109 typedef void (*RunMenuCommandType)(MenuAction *action, unsigned button);
110 
111 /** Allocate an empty menu. */
112 Menu *CreateMenu();
113 
114 /** Create an empty menu item. */
115 MenuItem *CreateMenuItem(MenuItemType type);
116 
117 /** Initialize a menu structure to be shown.
118  * @param menu The menu to initialize.
119  */
120 void InitializeMenu(Menu *menu);
121 
122 /** Show a menu.
123  * @param menu The menu to show.
124  * @param runner Callback executed when an item is selected.
125  * @param x The x-coordinate of the menu.
126  * @param y The y-coordinate of the menu.
127  * @param keyboard Set if the request came from a key binding.
128  * @return 1 if the menu was shown, 0 otherwise.
129  */
130 char ShowMenu(Menu *menu, RunMenuCommandType runner,
131               int x, int y, char keyboard);
132 
133 /** Destroy a menu structure.
134  * @param menu The menu to destroy.
135  */
136 void DestroyMenu(Menu *menu);
137 
138 /** The number of open menus. */
139 extern int menuShown;
140 
141 #endif /* MENU_H */
142 
143