PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ui-menu.h

https://bitbucket.org/ekolis/jackband
C Header | 260 lines | 100 code | 59 blank | 101 comment | 0 complexity | 10851f5edc89f2bad1bad747768f11e7 MD5 | raw file
  1. /*
  2. * Copyright (c) 2007 Pete Mack and others
  3. * This code released under the Gnu Public License. See www.fsf.org
  4. * for current GPL license details. Addition permission granted to
  5. * incorporate modifications in all Angband variants as defined in the
  6. * Angband variants FAQ. See rec.games.roguelike.angband for FAQ.
  7. */
  8. #ifndef INCLUDED_UI_MENU_H
  9. #define INCLUDED_UI_MENU_H
  10. /* ============= Constants ============ */
  11. /* Colors for interactive menus */
  12. enum
  13. {
  14. CURS_UNKNOWN = 0, /* Use gray; dark blue for cursor */
  15. CURS_KNOWN = 1 /* Use white; light blue for cursor */
  16. };
  17. /* Cursor colours for different states */
  18. extern const byte curs_attrs[2][2];
  19. /* Standard menu orderings */
  20. extern const char lower_case[]; /* abc..z */
  21. extern const char upper_case[]; /* ABC..Z */
  22. /* Forward declare */
  23. /* RISC OS already has a menu_item in system library */
  24. #ifdef RISCOS
  25. #define menu_item ang_menu_item
  26. #endif
  27. typedef struct menu_item menu_item;
  28. typedef struct menu_type menu_type;
  29. typedef struct menu_skin menu_skin;
  30. typedef struct menu_iter menu_iter;
  31. /* ================== MENUS ================= */
  32. /*
  33. * Performs an action on object with an optional environment label
  34. * Member function of "menu_iter" VTAB
  35. */
  36. typedef void (*action_f)(void *object, const char *name);
  37. /*
  38. * Displays a single row in a menu
  39. * Driver function for populating a single menu row.
  40. * Member function for "menu_iter" VTAB
  41. */
  42. typedef void (*display_row_f) (menu_type *menu, int pos,
  43. bool cursor, int row, int col, int width);
  44. /*
  45. * Driver function for displaying a page of rows.
  46. * Member function of "menu_skin" VTAB
  47. */
  48. typedef void (*display_list_f)(menu_type *menu, int cursor, int *top, region *);
  49. /* Primitive menu item with bound action */
  50. typedef struct menu_action
  51. {
  52. int id; /* Object id used to define macros &c */
  53. const char *name; /* Name of the action */
  54. action_f action; /* Action to perform, if any */
  55. void *data; /* Local environment for the action, if required */
  56. } menu_action;
  57. /* Decorated menu item with bound action */
  58. struct menu_item
  59. {
  60. menu_action act; /* Base type */
  61. char sel; /* Character used for selection, if special-purpose */
  62. /* bindings are desired. */
  63. int flags; /* State of the menu item. See menu flags below */
  64. };
  65. /* TODO: menu registry */
  66. /*
  67. Together, these classes define the constant properties of
  68. the various menu classes.
  69. A menu consists of:
  70. - menu_iter, which describes how to handle the type of "list" that's
  71. being displayed as a menu
  72. - a menu_skin, which describes the layout of the menu on the screen.
  73. - various bits and bobs of other data (e.g. the actual list of entries)
  74. */
  75. /* Flags for menu appearance & behavior */
  76. /* ==================================== */
  77. /* Tags are associated with the view, not the element */
  78. #define MN_REL_TAGS 0x0100
  79. /* No tags -- movement key and mouse browsing only */
  80. #define MN_NO_TAGS 0x0200
  81. /* Tags to be generated by the display function */
  82. #define MN_PVT_TAGS 0x0400
  83. /* Tag selections can be made regardless of the case of the key pressed.
  84. * i.e. 'a' activates the line tagged 'A'. */
  85. #define MN_CASELESS_TAGS 0x0800
  86. /* double tap (or keypress) for selection; single tap is cursor movement */
  87. #define MN_DBL_TAP 0x1000
  88. /* Do not invoke the specified action; menu selection only */
  89. #define MN_NO_ACT 0x2000
  90. /* No cursor movement */
  91. #define MN_NO_CURSOR 0x8000
  92. /* Row flags in action_menu structure. */
  93. #define MN_DISABLED 0x0100000 /* Neither action nor selection is permitted */
  94. #define MN_GRAYED 0x0200000 /* Row is displayed with CURS_UNKNOWN colors */
  95. #define MN_GREYED 0x0200000 /* Row is displayed with CURS_UNKNOWN colors */
  96. #define MN_SELECTED 0x0400000 /* Row is currently selected */
  97. #define MN_SELECTABLE 0x0800000 /* Row is permitted to be selected */
  98. #define MN_HIDDEN 0x1000000 /* Row is hidden, but may be selected via */
  99. /* key-binding. */
  100. /** Identifier for the type of menu layout to use */
  101. typedef enum
  102. {
  103. MN_SKIN_SCROLL = 1, /**< Ordinary scrollable single-column list */
  104. MN_SKIN_COLUMNS = 2 /**< Multicolumn view */
  105. } skin_id;
  106. /* Class functions for menu layout */
  107. struct menu_skin
  108. {
  109. /* Determines the cursor index given a (mouse) location */
  110. int (*get_cursor)(int row, int col, int n, int top, region *loc);
  111. /* Displays the current list of visible menu items */
  112. display_list_f display_list;
  113. /* Specifies the relative menu item given the state of the menu */
  114. char (*get_tag)(menu_type *menu, int pos);
  115. };
  116. /* Identifiers for canned row iterator implementations */
  117. typedef enum
  118. {
  119. /* A simple list of actions with an associated name and id.
  120. An array of menu_action */
  121. MN_ITER_ACTIONS = 1,
  122. /* Slightly more sophisticated, a list of menu items that also
  123. allows per-item flags and a "selection" character to be specified.
  124. An array of menu_item */
  125. MN_ITER_ITEMS = 2,
  126. /* A list of strings to be selected from - no associated actions.
  127. An array of const char * */
  128. MN_ITER_STRINGS = 3
  129. } menu_iter_id;
  130. /* Class functions for menu row-level accessor functions */
  131. struct menu_iter
  132. {
  133. /* Optional selection tag function */
  134. char (*get_tag)(menu_type *menu, int oid);
  135. /* Optional validity checker. All rows are assumed valid if not present. */
  136. /* To support "hidden" items, it uses 3-level logic: 0 = no 1 = yes 2 = hide */
  137. int (*valid_row)(menu_type *menu, int oid);
  138. /* Displays a menu row at specified location */
  139. display_row_f display_row;
  140. /* Handler function called for selection or command key events */
  141. bool (*row_handler)(char cmd, void *db, int oid);
  142. };
  143. /* A menu defines either an action
  144. * or db row event
  145. */
  146. struct menu_type
  147. {
  148. /* menu inherits from panel */
  149. event_listener target;
  150. void (*refresh)();
  151. region boundary;
  152. /* set of commands that may be performed on a menu item */
  153. const char *cmd_keys;
  154. /* Public variables */
  155. const char *title;
  156. const char *prompt;
  157. /* Keyboard shortcuts for menu selection */
  158. /* IMPORTANT: this cannot intersect with cmd_keys */
  159. const char *selections;
  160. int flags; /* Flags specifying the behavior of this menu. */
  161. int filter_count; /* number of rows in current view */
  162. const int *filter_list; /* optional filter (view) of menu objects */
  163. int count; /* number of rows in underlying data set */
  164. void *menu_data; /* the data used to access rows. */
  165. /* auxiliary browser help function */
  166. void (*browse_hook)(int oid, void *db, const region *loc);
  167. /* These are "protected" - not visible for canned menu classes, */
  168. /* The per-row functions */
  169. const menu_iter *row_funcs;
  170. /* State variables for the menu */
  171. int cursor; /* Currently selected row */
  172. int top; /* Position in list for partial display */
  173. region active; /* Subregion actually active for selection */
  174. /* helper functions for layout information. */
  175. const menu_skin *skin; /* Defines menu appearance */
  176. };
  177. /*
  178. * Select a row from a menu.
  179. *
  180. * Arguments:
  181. * object_list - optional ordered subset of menu OID. If NULL, cursor is used for OID
  182. * cursor - current (absolute) position in menu. Modified on selection.
  183. * loc - location to display the menu.
  184. * Return: A command key; cursor is set to the corresponding row.
  185. * (This is a stand-in for a menu event)
  186. * reserved commands are 0xff for selection and ESCAPE for escape.
  187. */
  188. ui_event_data menu_select(menu_type *menu, int *cursor, int no_handle);
  189. /* TODO: This belongs in the VTAB */
  190. bool menu_layout(menu_type *menu, const region *loc);
  191. /* accessor & utility functions */
  192. void menu_set_filter(menu_type *menu, const int object_list[], int n);
  193. void menu_release_filter(menu_type *menu);
  194. /* Find a menu iterator struct */
  195. const menu_iter *find_menu_iter(menu_iter_id iter_id);
  196. /* Initialize a menu given skin ID and an iterator */
  197. bool menu_init(menu_type *menu, skin_id skin, const menu_iter *iter, const region *loc);
  198. /* Refresh the menu */
  199. void menu_refresh(menu_type *menu);
  200. #endif /* INCLUDED_UI_MENU_H */