1 /*
2  * XPilotNG/SDL, an SDL/OpenGL XPilot client.
3  *
4  * Copyright (C) 2003-2004 Erik Andersson <deity_at_home.se>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef GLWIDGETS_H
22 #define GLWIDGETS_H
23 
24 #include "xpclient_sdl.h"
25 
26 #include "sdlkeys.h"
27 #include "text.h"
28 
29 /****************************************************/
30 /* BEGIN: Main GLWidget stuff	    	    	    */
31 /****************************************************/
32 /* Basically the init function will set default width, height in bounds
33  * Then the caller should reshape and position the widget defaults to 0,0
34  * (top left atm, EVEN sub-widgets so you MUST use the SetBounds_GLWidget
35  * function)
36  */
37 /* if this structure is changed, make sure that the generic functions below still work! */
38 typedef struct glwidget_struct GLWidget;
39 struct glwidget_struct {
40     int     	    WIDGET;
41     void    	    *wid_info;
42 
43     SDL_Rect	    bounds; /* atm this really is 'inner bounds' which
44     	    	    	     * the children aren't allowed to exceed
45 			     */
46     void    	    (*Draw)( GLWidget *widget );
47 
48     void    	    (*Close)( GLWidget *widget );
49     void    	    (*SetBounds)( GLWidget *widget, SDL_Rect *b );
50 
51     void    	    (*button)( Uint8 button, Uint8 state , Uint16 x , Uint16 y, void *data );
52     void    	    *buttondata;
53     void    	    (*motion)( Sint16 xrel, Sint16 yrel, Uint16 x, Uint16 y, void *data );
54     void    	    *motiondata;
55     void    	    (*hover)( int over, Uint16 x , Uint16 y , void *data );
56     void    	    *hoverdata;
57 
58     GLWidget	    **list;
59     GLWidget	    *children;
60     GLWidget	    *next;   /* use to build widget lists */
61 };
62 
63 GLWidget *Init_EmptyBaseGLWidget( void );
64 /*GLWidget *Init_BaseGLWidget( int WIDGET, void *wid_info, SDL_Rect bounds,
65     	    	    	    void (*Draw)( GLWidget *widget ), void (*Close)( GLWidget *widget ),
66 			    void (*SetBounds)( GLWidget *widget, SDL_Rect *b ),
67 			    void (*button)( Uint8 button, Uint8 state , Uint16 x , Uint16 y, void *data ), void *buttondata,
68 			    void (*motion)( Sint16 xrel, Sint16 yrel, Uint16 x, Uint16 y, void *data ), void *motiondata,
69 			    void (*hover)( int over, Uint16 x , Uint16 y , void *data ), void *hoverdata,
70 			    GLWidget *children, GLWidget *next
71 			     );*/
72 
73 extern GLWidget *MainWidget;
74 
75 /* Two Methods Needed for widget management */
76 /* new types need to implement theese methods */
77 
78 /* should free any resources committed by the init_foo function */
79 void Close_Widget ( GLWidget **widget );
80 /*void Close_WidgetTree ( GLWidget **widget );*/
81 /* to reshape the widget, and automagically reshape and place sub-widgets */
82 void SetBounds_GLWidget(GLWidget *wid, SDL_Rect *b );
83 /* Initializes the appropriate config widget (if implemented), returns NULL otherwise */
84 GLWidget *Init_OptionWidget( xp_option_t *opt, Uint32 *fgcolor, Uint32 *bgcolor );
85 
86 bool AppendGLWidgetList( GLWidget **list, GLWidget *widget );
87 void PrependGLWidgetList( GLWidget **list, GLWidget *widget );
88 bool DelGLWidgetListItem( GLWidget **list, GLWidget *widget );
89 
90 void DrawGLWidgets( GLWidget *list );
91 GLWidget *FindGLWidget( GLWidget *list, Uint16 x,Uint16 y );
92 void DrawGLWidgetsi( GLWidget *list, int x, int y, int w, int h);
93 GLWidget *FindGLWidgeti( GLWidget *widget, Uint16 x, Uint16 y );
94 
95 extern GLWidget *clicktarget[NUM_MOUSE_BUTTONS];
96 extern GLWidget *hovertarget;
97 
98 /* puts text into the copy buffer */
99 void load_textscrap(char *text);
100 /****************************************************/
101 /* END: Main GLWidget stuff 	    	    	    */
102 /****************************************************/
103 
104 /****************************************************/
105 /* widget-specific stuff is below   	    	    */
106 /****************************************************/
107 
108 /***********************/
109 /* Begin:  ArrowWidget */
110 /***********************/
111 /* Basically a triangle that stays lit while mousebutton 1 is down
112  * And each time it is drawn lit, it calls (*action).
113  * Button two causes it set 'tap' and calls (*action),
114  * (*action) needs to reset it for it to work again. (still haven't
115  * decided whether automatic reset is better)
116  */
117 #define ARROWWIDGET 0
118 typedef enum {RIGHTARROW,UPARROW,LEFTARROW,DOWNARROW} ArrowWidget_dir_t;
119 typedef struct {
120     ArrowWidget_dir_t	direction;
121     bool    	    	press;/*this is set/unset automagically (set:call action each draw)*/
122     bool    	    	tap;/*action needs to clear this (action called once)*/
123     bool    	    	locked;/*Won't call action for any reason*/
124     void    	    	(*action)(void *data);
125     void    	    	*actiondata;
126 } ArrowWidget;
127 GLWidget *Init_ArrowWidget( ArrowWidget_dir_t direction, int width, int height,
128     	    	    	    void (*action)( void *data ), void *actiondata );
129 /*********************/
130 /* End:  ArrowWidget */
131 /*********************/
132 
133 /***********************/
134 /* Begin: ButtonWidget  */
135 /***********************/
136 #define BUTTONWIDGET 1
137 typedef struct {
138     Uint32  	*normal_color;
139     Uint32  	*pressed_color;
140     bool    	pressed;
141     Uint8  	depress_time;
142     int  	press_time;
143     void    	(*action)(void *data);
144     void    	*actiondata;
145 } ButtonWidget;
146 GLWidget *Init_ButtonWidget( Uint32 *normal_color, Uint32 *pressed_color, Uint8 depress_time, void (*action)(void *data), void *actiondata);
147 /*********************/
148 /* End: ButtonWidget  */
149 /*********************/
150 
151 /***********************/
152 /* Begin: SlideWidget  */
153 /***********************/
154 #define SLIDEWIDGET 2
155 typedef struct {
156     bool    sliding;/*Don't slide*/
157     bool    locked;/*Don't slide*/
158     void    (*release)( void *releasedata );
159     void    *releasedata;
160 } SlideWidget;
161 GLWidget *Init_SlideWidget( bool locked,
162     	     void (*motion)( Sint16 xrel, Sint16 yrel, Uint16 x, Uint16 y, void *data ), void *motiondata,
163 	     void (*release)( void *releasedata),void *releasedata );
164 /*********************/
165 /* End: SlideWidget  */
166 /*********************/
167 
168 /***************************/
169 /* Begin: ScrollbarWidget  */
170 /***************************/
171 typedef enum {SB_VERTICAL, SB_HORISONTAL} ScrollWidget_dir_t;
172 /* note 0.0 <= pos && pos + size <= 1.0 */
173 #define SCROLLBARWIDGET 3
174 typedef struct {
175     GLWidget	    	*slide;
176     GLfloat 	    	pos;
177     GLfloat 	    	size;
178     Sint16  	    	oldmoves;
179     ScrollWidget_dir_t	dir;
180     void    	    (	*poschange)( GLfloat pos , void *poschangedata );
181     void    	    	*poschangedata;
182 } ScrollbarWidget;
183 GLWidget *Init_ScrollbarWidget( bool locked, GLfloat pos, GLfloat size,ScrollWidget_dir_t dir,
184     	    	    	    	void (*poschange)( GLfloat pos , void *data), void *data );
185 
186 void ScrollbarWidget_SetSlideSize( GLWidget *widget, GLfloat size );
187 /*************************/
188 /* End:  ScrollbarWidget */
189 /*************************/
190 
191 /**********************/
192 /* Begin: LabelWidget */
193 /**********************/
194 #define LABELWIDGET 4
195 typedef struct {
196     string_tex_t    tex;
197     Uint32     	    *fgcolor;
198     Uint32     	    *bgcolor;
199     int             align;  /* horizontal alignemnt */
200     int             valign; /* vertical alignment   */
201 } LabelWidget;
202 GLWidget *Init_LabelWidget( const char *text , Uint32 *fgcolor, Uint32 *bgcolor, int align, int valign );
203 
204 bool LabelWidget_SetColor( GLWidget *widget , Uint32 *fgcolor, Uint32 *bgcolor );
205 
206 /********************/
207 /* End: LabelWidget */
208 /********************/
209 
210 /***********************************/
211 /* Begin: LabeledRadiobuttonWidget */
212 /***********************************/
213 #define LABELEDRADIOBUTTONWIDGET 5
214 typedef struct {
215     bool    	    state;
216     string_tex_t    *ontex;
217     string_tex_t    *offtex;
218     void    	    (*action)( bool state, void *actiondata );
219     void    	    *actiondata;
220 } LabeledRadiobuttonWidget;
221 /* TODO : add some abstraction layer to init function */
222 GLWidget *Init_LabeledRadiobuttonWidget( string_tex_t *ontex, string_tex_t *offtex,
223     	    	    	    	    	void (*action)(bool state, void *actiondata),
224 					void *actiondata, bool start_state);
225 /*********************************/
226 /* End: LabeledRadiobuttonWidget */
227 /*********************************/
228 
229 /****************************/
230 /* Begin: BoolChooserWidget */
231 /****************************/
232 #define BOOLCHOOSERWIDGET 6
233 typedef struct {
234     bool    	    *value;
235     GLWidget	    *buttonwidget;
236     GLWidget	    *name;
237     Uint32     	    *fgcolor;
238     Uint32     	    *bgcolor;
239     void    	    (*callback)(void *tmp, const char *value);
240     void    	    *data;
241 } BoolChooserWidget;
242 
243 GLWidget *Init_BoolChooserWidget( const char *name, bool *value, Uint32 *fgcolor, Uint32 *bgcolor,
244     	    	    	    	 void (*callback)(void *tmp, const char *value), void *data );
245 /**************************/
246 /* End: BoolChooserWidget */
247 /**************************/
248 
249 /***************************/
250 /* Begin: IntChooserWidget */
251 /***************************/
252 #define INTCHOOSERWIDGET 7
253 typedef struct {
254     GLWidget	    *name;
255     int     	    *value;
256     int     	    minval;
257     int     	    maxval;
258     int     	    valuespace;
259     string_tex_t    valuetex;
260     GLWidget	    *leftarrow;
261     GLWidget	    *rightarrow;
262     int     	    direction;
263     int     	    duration;
264     Uint32     	    *fgcolor;
265     Uint32     	    *bgcolor;
266     void    	    (*callback)(void *tmp, const char *value);
267     void    	    *data;
268 } IntChooserWidget;
269 
270 GLWidget *Init_IntChooserWidget( const char *name, int *value, int minval, int maxval, Uint32 *fgcolor,
271     	    	    	    	Uint32 *bgcolor, void (*callback)(void *tmp, const char *value), void *data );
272 /*************************/
273 /* End: IntChooserWidget */
274 /*************************/
275 
276 /******************************/
277 /* Begin: DoubleChooserWidget */
278 /******************************/
279 #define DOUBLECHOOSERWIDGET 8
280 typedef struct {
281     GLWidget	    *name;
282     double  	    *value;
283     double     	    minval;
284     double     	    maxval;
285     int     	    valuespace;
286     string_tex_t    valuetex;
287     GLWidget	    *leftarrow;
288     GLWidget	    *rightarrow;
289     int     	    direction;
290     Uint32     	    *fgcolor;
291     Uint32     	    *bgcolor;
292     void    	    (*callback)(void *tmp, const char *value);
293     void    	    *data;
294 } DoubleChooserWidget;
295 
296 GLWidget *Init_DoubleChooserWidget( const char *name, double *value, double minval, double maxval,
297     	    	    	    	    Uint32 *fgcolor, Uint32 *bgcolor,
298     	    	    	    	    void (*callback)(void *tmp, const char *value), void *data );
299 /****************************/
300 /* End: DoubleChooserWidget */
301 /****************************/
302 
303 /*****************************/
304 /* Begin: ColorChooserWidget */
305 /*****************************/
306 #define COLORCHOOSERWIDGET 9
307 typedef struct {
308     GLWidget	    *mod;
309     GLWidget	    *name;
310     Uint32  	    *value;
311     GLWidget	    *button;
312     Uint32     	    *fgcolor;
313     Uint32     	    *bgcolor;
314     bool    	    expanded;
315     void    	    (*callback)(void *tmp, const char *value);
316     void    	    *data;
317 } ColorChooserWidget;
318 
319 GLWidget *Init_ColorChooserWidget( const char *name, Uint32 *value, Uint32 *fgcolor, Uint32 *bgcolor,
320     	    	    	    	    void (*callback)(void *tmp, const char *value), void *data );
321 /***************************/
322 /* End: ColorChooserWidget */
323 /***************************/
324 
325 /*************************/
326 /* Begin: ColorModWidget */
327 /*************************/
328 #define COLORMODWIDGET 10
329 typedef struct {
330     Uint32  	    *value;
331     int  	    red;
332     int  	    green;
333     int  	    blue;
334     int  	    alpha;
335     GLWidget	    *redpick;
336     GLWidget	    *greenpick;
337     GLWidget	    *bluepick;
338     GLWidget	    *alphapick;
339     Uint32     	    *fgcolor;
340     Uint32     	    *bgcolor;
341     void    	    (*callback)(void *tmp, const char *value);
342     void    	    *data;
343 } ColorModWidget;
344 
345 GLWidget *Init_ColorModWidget( Uint32 *value, Uint32 *fgcolor, Uint32 *bgcolor,
346     	    	    	    	    void (*callback)(void *tmp, const char *value), void *data );
347 /***********************/
348 /* End: ColorModWidget */
349 /***********************/
350 
351 /**********************/
352 /* Begin: ListWidget  */
353 /**********************/
354 typedef enum {HORISONTAL, VERTICAL} ListWidget_direction;
355 typedef enum {LW_DOWN, LW_VCENTER, LW_UP} ListWidget_ver_dir_t;
356 typedef enum {LW_RIGHT, LW_HCENTER, LW_LEFT} ListWidget_hor_dir_t;
357 #define LISTWIDGET 11
358 typedef struct {
359      int num_elements;
360      Uint32 *bg1;
361      Uint32 *bg2;
362      Uint32 *highlight_color;/*not used (yet) */
363      bool   reverse_scroll;
364      ListWidget_direction   direction;
365      ListWidget_ver_dir_t   v_dir;
366      ListWidget_hor_dir_t   h_dir;
367 } ListWidget;
368 
369 GLWidget *Init_ListWidget( Uint16 x, Uint16 y, Uint32 *bg1, Uint32 *bg2, Uint32 *highlight_color
370     	    	    	    ,ListWidget_ver_dir_t v_dir, ListWidget_hor_dir_t h_dir
371 			    ,ListWidget_direction direction, bool reverse_scroll );
372 
373 /*TODO: allow lists in prepen,append (needs to check against loops) */
374 
375 /* Adds a new item last in the list */
376 bool ListWidget_Append( GLWidget *list, GLWidget *item );
377 /* Adds a new item first in the list */
378 bool ListWidget_Prepend( GLWidget *list, GLWidget *item );
379 /* Adds a new item just before target in the list */
380 bool ListWidget_Insert( GLWidget *list, GLWidget *target, GLWidget *item );
381 /* Removes an item from the list */
382 bool ListWidget_Remove( GLWidget *list, GLWidget *item );
383 
384 bool ListWidget_SetScrollorder( GLWidget *list, bool order );
385 
386 int ListWidget_NELEM( GLWidget *list );
387 /* first item is indexed [0], last is [ListWidget_NELEM - 1]*/
388 GLWidget *ListWidget_GetItemByIndex( GLWidget *list, int i );
389 
390 /*******************/
391 /* End: ListWidget */
392 /*******************/
393 
394 /****************************/
395 /* Begin: ScrollPaneWidget  */
396 /****************************/
397 #define SCROLLPANEWIDGET 12
398 typedef struct {
399     GLWidget	*vert_scroller;
400     GLWidget	*hori_scroller;
401     GLWidget	*masque;
402     GLWidget	*content;
403 } ScrollPaneWidget;
404 
405 GLWidget *Init_ScrollPaneWidget( GLWidget *content );
406 
407 /**************************/
408 /* End: ScrollPaneWidget  */
409 /**************************/
410 
411 /**********************/
412 /* Begin: RadarWidget */
413 /**********************/
414 #define RADARWIDGET 13
415 
416 extern GLWidget *Init_RadarWidget( void );
417 /********************/
418 /* End: RadarWidget */
419 /********************/
420 
421 /**************************/
422 /* Begin: ScorelistWidget */
423 /**************************/
424 #define SCORELISTWIDGET 14
425 
426 extern GLWidget *Init_ScorelistWidget( void );
427 /************************/
428 /* End: ScorelistWidget */
429 /************************/
430 
431 /**********************/
432 /* Begin: MainWidget  */
433 /**********************/
434 #define MAINWIDGET 15
435 typedef struct {
436     bool	showconf;
437     GLWidget	*confmenu;
438     GLWidget	*radar;
439     GLWidget	*scorelist;
440     GLWidget	*chat_msgs;
441     GLWidget	*game_msgs;
442     GLWidget	*alert_msgs;
443     int	    	BORDER;
444     font_data	*font;
445 } WrapperWidget;
446 
447 GLWidget *Init_MainWidget( font_data *font );
448 void MainWidget_ShowMenu( GLWidget *widget, bool show );
449 /*******************/
450 /* End: MainWidget */
451 /*******************/
452 
453 /**************************/
454 /* Begin: ConfMenuWidget  */
455 /**************************/
456 #define CONFMENUWIDGET 16
457 typedef struct {
458     bool	showconf;
459     bool	paused;
460     int     	team;
461     GLWidget	*scrollpane;
462     GLWidget	*main_list;
463     GLWidget	*button_list;
464     GLWidget	*join_list;
465     GLWidget	*qlb;
466     GLWidget	*clb;
467     GLWidget	*slb;
468     GLWidget	*jlb;
469 } ConfMenuWidget;
470 
471 GLWidget *Init_ConfMenuWidget( Uint16 x, Uint16 y );
472 /***********************/
473 /* End: ConfMenuWidget */
474 /***********************/
475 
476 /*****************************/
477 /* Begin: ImageButtonWidget  */
478 /*****************************/
479 #define IMAGEBUTTONWIDGET 17
480 typedef struct {
481     Uint32 fg;
482     Uint32 bg;
483     Uint8 state;
484     string_tex_t tex;
485     GLuint imageUp;
486     GLuint imageDown;
487     texcoord_t txcUp;
488     texcoord_t txcDown;
489     void (*onClick)(GLWidget *widget);
490 } ImageButtonWidget;
491 
492 GLWidget *Init_ImageButtonWidget(const char *text,
493 				 const char *upImage,
494 				 const char *downImage,
495 				 Uint32 bg,
496 				 Uint32 fg,
497 				 void (*onClick)(GLWidget *widget));
498 /**************************/
499 /* End: ImageButtonWidget */
500 /**************************/
501 
502 /*****************************/
503 /* Begin: LabelButtonWidget  */
504 /*****************************/
505 #define LABELBUTTONWIDGET 18
506 typedef struct {
507     GLWidget *button;
508     GLWidget *label;
509 } LabelButtonWidget;
510 
511 GLWidget *Init_LabelButtonWidget(   const char *text,
512 				    Uint32 *text_color,
513     	    	    	    	    Uint32 *bg_color,
514     	    	    	    	    Uint32 *active_color,
515     	    	    	    	    Uint8 depress_time,
516     	    	    	    	    void (*action)(void *data),
517 				    void *actiondata);
518 /**************************/
519 /* End: LabelButtonWidget */
520 /**************************/
521 
522 #endif
523