added handling of unselectable selectbox options
[rocksndiamonds.git] / src / libgame / gadgets.h
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // gadgets.h
10 // ============================================================================
11
12 #ifndef GADGETS_H
13 #define GADGETS_H
14
15 #include "system.h"
16
17
18 #define GADGET_FRAME_DELAY_FIRST        250     /* delay after first click */
19 #define GADGET_FRAME_DELAY              100     /* delay for pressed butten */
20
21 /* gadget types */
22 #define GD_TYPE_NORMAL_BUTTON           (1 << 0)
23 #define GD_TYPE_TEXT_BUTTON             (1 << 1)
24 #define GD_TYPE_CHECK_BUTTON            (1 << 2)
25 #define GD_TYPE_RADIO_BUTTON            (1 << 3)
26 #define GD_TYPE_DRAWING_AREA            (1 << 4)
27 #define GD_TYPE_TEXT_INPUT_ALPHANUMERIC (1 << 5)
28 #define GD_TYPE_TEXT_INPUT_NUMERIC      (1 << 6)
29 #define GD_TYPE_TEXT_AREA               (1 << 7)
30 #define GD_TYPE_SELECTBOX               (1 << 8)
31 #define GD_TYPE_SCROLLBAR_VERTICAL      (1 << 9)
32 #define GD_TYPE_SCROLLBAR_HORIZONTAL    (1 << 10)
33
34 #define GD_TYPE_BUTTON                  (GD_TYPE_NORMAL_BUTTON | \
35                                          GD_TYPE_TEXT_BUTTON | \
36                                          GD_TYPE_CHECK_BUTTON | \
37                                          GD_TYPE_RADIO_BUTTON)
38 #define GD_TYPE_SCROLLBAR               (GD_TYPE_SCROLLBAR_VERTICAL | \
39                                          GD_TYPE_SCROLLBAR_HORIZONTAL)
40 #define GD_TYPE_TEXT_INPUT              (GD_TYPE_TEXT_INPUT_ALPHANUMERIC | \
41                                          GD_TYPE_TEXT_INPUT_NUMERIC)
42
43 /* gadget events */
44 #define GD_EVENT_PRESSED                (1 << 0)
45 #define GD_EVENT_RELEASED               (1 << 1)
46 #define GD_EVENT_MOVING                 (1 << 2)
47 #define GD_EVENT_REPEATED               (1 << 3)
48 #define GD_EVENT_OFF_BORDERS            (1 << 4)
49 #define GD_EVENT_TEXT_RETURN            (1 << 5)
50 #define GD_EVENT_TEXT_LEAVING           (1 << 6)
51 #define GD_EVENT_INFO_ENTERING          (1 << 7)
52 #define GD_EVENT_INFO_LEAVING           (1 << 8)
53
54 /* gadget button states */
55 #define GD_BUTTON_UNPRESSED             0
56 #define GD_BUTTON_PRESSED               1
57
58 /* gadget structure constants */
59 #define MAX_GADGET_TEXTSIZE             1024
60 #define MAX_INFO_TEXTSIZE               1024
61
62 /* gadget creation tags */
63 #define GDI_END                         0
64 #define GDI_CUSTOM_ID                   1
65 #define GDI_CUSTOM_TYPE_ID              2
66 #define GDI_X                           3
67 #define GDI_Y                           4
68 #define GDI_WIDTH                       5
69 #define GDI_HEIGHT                      6
70 #define GDI_TYPE                        7
71 #define GDI_STATE                       8
72 #define GDI_CHECKED                     9
73 #define GDI_RADIO_NR                    10
74 #define GDI_NUMBER_VALUE                11
75 #define GDI_NUMBER_MIN                  12
76 #define GDI_NUMBER_MAX                  13
77 #define GDI_TEXT_VALUE                  14
78 #define GDI_TEXT_SIZE                   15
79 #define GDI_TEXT_FONT                   16
80 #define GDI_TEXT_FONT_ACTIVE            17
81 #define GDI_TEXT_FONT_UNSELECTABLE      18
82 #define GDI_SELECTBOX_OPTIONS           19
83 #define GDI_SELECTBOX_INDEX             20
84 #define GDI_SELECTBOX_CHAR_UNSELECTABLE 21
85 #define GDI_DESIGN_UNPRESSED            22
86 #define GDI_DESIGN_PRESSED              23
87 #define GDI_ALT_DESIGN_UNPRESSED        24
88 #define GDI_ALT_DESIGN_PRESSED          25
89 #define GDI_BORDER_SIZE                 26
90 #define GDI_BORDER_SIZE_SELECTBUTTON    27
91 #define GDI_DESIGN_WIDTH                28
92 #define GDI_DECORATION_DESIGN           29
93 #define GDI_DECORATION_POSITION         30
94 #define GDI_DECORATION_SIZE             31
95 #define GDI_DECORATION_SHIFTING         32
96 #define GDI_EVENT_MASK                  33
97 #define GDI_EVENT                       34
98 #define GDI_CALLBACK_INFO               35
99 #define GDI_CALLBACK_ACTION             36
100 #define GDI_AREA_SIZE                   37
101 #define GDI_ITEM_SIZE                   38
102 #define GDI_SCROLLBAR_ITEMS_MAX         39
103 #define GDI_SCROLLBAR_ITEMS_VISIBLE     40
104 #define GDI_SCROLLBAR_ITEM_POSITION     41
105 #define GDI_WHEEL_AREA_X                42
106 #define GDI_WHEEL_AREA_Y                43
107 #define GDI_WHEEL_AREA_WIDTH            44
108 #define GDI_WHEEL_AREA_HEIGHT           45
109 #define GDI_INFO_TEXT                   46
110 #define GDI_ACTIVE                      47
111 #define GDI_DIRECT_DRAW                 48
112
113 /* gadget deactivation hack */
114 #define GDI_ACTIVE_POS(a)               ((a) < 0 ? POS_OFFSCREEN : (a))
115
116
117 typedef void (*gadget_function)(void *);
118
119 struct GadgetBorder
120 {
121   int xsize, ysize;                     /* size of gadget border */
122   int xsize_selectbutton;               /* for selectbox gadgets */
123   int width;                            /* for selectbox/text input gadgets */
124 };
125
126 struct GadgetDesign
127 {
128   Bitmap *bitmap;                       /* Bitmap with gadget surface */
129   int x, y;                             /* position of rectangle in Bitmap */
130 };
131
132 struct GadgetDecoration
133 {
134   struct GadgetDesign design;           /* decoration design structure */
135   int x, y;                             /* position of deco on the gadget */
136   int width, height;                    /* width and height of decoration */
137   int xshift, yshift;                   /* deco shifting when gadget pressed */
138 };
139
140 struct GadgetEvent
141 {
142   unsigned int type;                    /* event type */
143   int button;                           /* button number for button events */
144   int x, y;                             /* gadget position at event time */
145   boolean off_borders;                  /* mouse pointer outside gadget? */
146   int item_x, item_y, item_position;    /* new item position */
147 };
148
149 struct GadgetDrawingArea
150 {
151   int area_xsize, area_ysize;           /* size of drawing area (in items) */
152   int item_xsize, item_ysize;           /* size of each item in drawing area */
153 };
154
155 struct GadgetTextButton
156 {
157   char value[MAX_GADGET_TEXTSIZE + 1];  /* text written on the button */
158   int size;                             /* maximal size of button text */
159 };
160
161 struct GadgetTextInput
162 {
163   char value[MAX_GADGET_TEXTSIZE + 1];  /* text string in input field */
164   char last_value[MAX_GADGET_TEXTSIZE + 1];/* last text string in input field */
165   int cursor_position;                  /* actual text cursor position */
166   int number_value;                     /* integer value, if numeric */
167   int number_min;                       /* minimal allowed numeric value */
168   int number_max;                       /* maximal allowed numeric value */
169   int size;                             /* maximal size of input text */
170 };
171
172 struct GadgetTextArea
173 {
174   char value[MAX_GADGET_TEXTSIZE + 1];  /* text string in input field */
175   char last_value[MAX_GADGET_TEXTSIZE + 1];/* last text string in input field */
176   int cursor_position;                  /* actual text cursor position */
177   int cursor_x;                         /* actual x cursor position */
178   int cursor_y;                         /* actual y cursor position */
179   int cursor_x_preferred;               /* "preferred" x cursor position */
180   int size;                             /* maximal size of input text */
181   int xsize, ysize;                     /* size of text area (in chars) */
182 };
183
184 struct GadgetSelectbox
185 {
186   struct ValueTextInfo *options;        /* pointer to text/value array */
187   int index;                            /* index of actual text string */
188   int size;                             /* maximal size of text strings */
189   char char_unselectable;               /* first char of unselectable options */
190
191   /* automatically determined values */
192   int x, y;                             /* open selectbox position */
193   int width, height;                    /* open selectbox size */
194   int num_values;                       /* number of text strings */
195   Pixel inverse_color;                  /* color for highlighting */
196
197   /* runtime values */
198   boolean open;                         /* opening state of selectbox */
199   boolean stay_open;                    /* open after button release */
200   int current_index;                    /* index of text while selecting */
201 };
202
203 struct GadgetScrollbar
204 {
205   int items_max;                        /* number of items to access */
206   int items_visible;                    /* number of visible items */
207   int item_position;                    /* actual item position */
208   int size_min;                         /* minimal scrollbar size */
209   int size_max;                         /* this is either width or height */
210   int size_max_cmp;                     /* needed for minimal scrollbar size */
211   int size;                             /* scrollbar size on screen */
212   int position;                         /* scrollbar position on screen */
213   int position_max;                     /* bottom/right scrollbar position */
214   int drag_position;                    /* drag position on scrollbar */
215   int correction;                       /* scrollbar position correction */
216 };
217
218 struct GadgetWheelArea
219 {
220   int x, y;                             /* active area for wheel (start) */
221   int width, height;                    /* active area for wheel (size) */ 
222 };
223
224 struct GadgetInfo
225 {
226   int id;                               /* internal gadget identifier */
227   int custom_id;                        /* custom gadget identifier */
228   int custom_type_id;                   /* custom gadget type identifier */
229   char info_text[MAX_INFO_TEXTSIZE + 1];/* short popup info text */
230   int x, y;                             /* gadget position */
231   int width, height;                    /* gadget size */
232   unsigned int type;                    /* type (button, text input, ...) */
233   unsigned int state;                   /* state (pressed, released, ...) */
234   boolean checked;                      /* check/radio button state */
235   int radio_nr;                         /* number of radio button series */
236   boolean mapped;                       /* gadget is mapped on the screen */
237   boolean active;                       /* gadget is active */
238   boolean direct_draw;                  /* directly draw to frontbuffer */
239   int font;                             /* font to use when inactive */
240   int font_active;                      /* font to use when active */
241   int font_unselectable;                /* font to use when unselectable */
242   struct GadgetBorder border;           /* gadget border design */
243   struct GadgetDesign design[2];        /* 0: normal; 1: pressed */
244   struct GadgetDesign alt_design[2];    /* alternative design */
245   struct GadgetDecoration deco;         /* decoration on top of gadget */
246   unsigned int event_mask;              /* possible events for this gadget */
247   struct GadgetEvent event;             /* actual gadget event */
248   gadget_function callback_info;        /* function for pop-up info text */
249   gadget_function callback_action;      /* function for gadget action */
250   struct GadgetDrawingArea drawing;     /* fields for drawing area gadget */
251   struct GadgetTextButton textbutton;   /* fields for text button gadget */
252   struct GadgetTextInput textinput;     /* fields for text input gadget */
253   struct GadgetTextArea textarea;       /* fields for text area gadget */
254   struct GadgetSelectbox selectbox;     /* fields for selectbox gadget */
255   struct GadgetScrollbar scrollbar;     /* fields for scrollbar gadget */
256   struct GadgetWheelArea wheelarea;     /* fields for scroll wheel area */
257   struct GadgetInfo *next;              /* next list entry */
258 };
259
260
261 void InitGadgetsSoundCallback(void (*activating_function)(void),
262                               void (*selecting_function)(void));
263
264 struct GadgetInfo *CreateGadget(int, ...);
265 void FreeGadget(struct GadgetInfo *);
266
267 void ModifyGadget(struct GadgetInfo *, int, ...);
268 void RedrawGadget(struct GadgetInfo *);
269
270 void MapGadget(struct GadgetInfo *);
271 void UnmapGadget(struct GadgetInfo *);
272 void UnmapAllGadgets();
273 void RemapAllGadgets();
274
275 boolean anyTextInputGadgetActive();
276 boolean anyTextAreaGadgetActive();
277 boolean anySelectboxGadgetActive();
278 boolean anyScrollbarGadgetActive();
279 boolean anyTextGadgetActive();
280
281 void ClickOnGadget(struct GadgetInfo *, int);
282
283 boolean HandleGadgets(int, int, int);
284 boolean HandleGadgetsKeyInput(Key);
285
286 #endif  /* GADGETS_H */