1 /* Tower Toppler - Nebulus
2  * Copyright (C) 2000-2012  Andreas R�ver
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  */
18 
19 #ifndef MENUSYS_H
20 #define MENUSYS_H
21 
22 #include "decl.h"
23 
24 #include "SDL_keyboard.h"
25 
26 /* This module defines a menu system that has the following features
27 */
28 
29 #define MENUTITLELEN  1000
30 #define MENUOPTIONLEN MENUTITLELEN
31 
32 /* Menu option flags */
33 typedef enum {
34   MOF_NONE     = 0x00,
35   MOF_PASSKEYS = 0x01,  /* Do keys get passed onto this option? */
36   MOF_LEFT     = 0x02,  /* Option string is left justified */
37   MOF_RIGHT    = 0x04   /* Option string is right justified */
38 } menuoptflags;
39 
40 struct _menusystem;
41 
42 /* menu option callback function. gets called with the
43    menusystem as it's parameter, and should return a
44    string describing this option. If the parameter
45    is null, then this is called just to get the
46    string back. */
47 typedef const char *FDECL((*menuopt_callback_proc), (struct _menusystem *));
48 
49 /* menu background callback procedure. this should draw the
50    background screen for the menu. */
51 typedef void FDECL((*menubg_callback_proc), (void));
52 
53 
54 /* menu option */
55 typedef struct {
56    char oname[MENUOPTIONLEN];    /* text shown to user */
57    menuopt_callback_proc oproc;  /* callback proc, supplies actions and the name */
58    int  ostate;                  /* callback proc can use this */
59    menuoptflags  oflags;         /* MOF_foo */
60    SDLKey quickkey;              /* quick jump key; if user presses this key,
61                                   * this menu option is hilited.
62                                   */
63 } _menuoption;
64 
65 
66 typedef struct _menusystem {
67    char title[MENUTITLELEN];     /* title of the menu */
68    int numoptions;               /* # of options in this menu */
69    _menuoption *moption;         /* the options */
70    menuopt_callback_proc mproc;
71    menuopt_callback_proc timeproc;
72    long curr_mtime;              /* current time this menu has been running */
73    long mtime;                   /* time when to call timeproc */
74    int hilited;                  /* # of the option that is hilited */
75    int mstate;                   /* menu state, free to use by callback procs */
76    int maxoptlen;                /* longest option name length, in pixels.
77                                   * the hilite bar is slightly longer than this
78                                   */
79    bool exitmenu;                /* if true, the menu exits */
80    bool wraparound;              /* if true, the hilite bar wraps around */
81    int ystart;                   /* y pos where this menu begins, in pixels */
82    int yhilitpos;                /* y pos of the hilite bar, in pixels */
83    int opt_steal_control;        /* if >= 0, that option automagically gets
84                                   * keys passed to it, and normal key/mouse
85                                   * processing doesn't happen.
86                                   */
87    SDLKey key;                   /* the key that was last pressed */
88 
89    struct _menusystem * parent;  /* the current parrent, or NULL */
90 
91 } _menusystem;
92 
93 /* input line; asks the user for a string.
94  *
95  * This function returns immediately, and the return
96  * value tells whether the user finished editing the string.
97  */
98 bool men_input(char *origs, int max_len, int xpos = -1,
99                int ypos = (SCREENHEI  * 2) / 3,
100                const char *allowed = NULL);
101 
102 /* asks a yes/no question; return 0 if "no",
103    1 if "yes" */
104 unsigned char men_yn(char *s, bool defchoice, menuopt_callback_proc pr = 0);
105 
106 /* shows string s, waits a certain time, (-1 = indefinitely),
107    and if fire = 1 -> "press fire", if fire = 2 -> "press space" */
108 void men_info(char *s, long timeout = -1, int fire = 0);
109 
110 
111 /* sets the function that gets called whenever the background
112    needs to be drawn in men_yn(), and men_info() */
113 void set_men_bgproc(menubg_callback_proc proc);
114 
115 
116 /* create a new menu */
117 _menusystem *new_menu_system(const char *title, menuopt_callback_proc pr,
118                                     int molen = 0, int ystart = 25);
119 
120 /* add an option to the menu */
121 _menusystem *add_menu_option(_menusystem *ms, const char *name, menuopt_callback_proc pr,
122                                     SDLKey quickkey = SDLK_UNKNOWN, menuoptflags flags = MOF_NONE, int state = 0);
123 
124 /* displays the given menu on screen and lets the user interact with it*/
125 _menusystem *run_menu_system(_menusystem *ms, _menusystem *parent);
126 
127 
128 void free_menu_system(_menusystem *ms);
129 
130 
131 _menusystem *set_menu_system_timeproc(_menusystem *ms, long t, menuopt_callback_proc pr);
132 
133 #endif
134