1 /*
2     GUILIB:  An example GUI framework library for use with SDL
3 */
4 
5 /* Base class for all widgets -- the GUI operates on this class */
6 
7 #ifndef _GUI_widget_h
8 #define _GUI_widget_h
9 
10 #include <stdio.h>
11 #include <stdarg.h>
12 
13 #include "SDL.h"
14 #include "GUI_status.h"
15 
16 
17 class GUI_Widget {
18 
19 public:
20 	GUI_Widget(void *data);
21 	GUI_Widget(void *data, int x, int y, int w, int h);
~GUI_Widget()22 	virtual ~GUI_Widget() { }
23 
24 	/* Mark the widget as visible -- this is the default state */
25 	virtual void Show(void);
26 
27 	/* Mark the widget as hidden;  no display, no events */
28 	virtual void Hide(void);
29 
30 	/* Mark the widget as free, so it will be deleted by the GUI */
31 	virtual void Delete(void);
32 
33 	virtual int  Status(void);	/* Reports status to GUI */
34 
35 	/* Set the bounds of the widget.
36 	   If 'w' or 'h' is -1, that parameter will not be changed.
37 	 */
38 	virtual void SetRect(int x, int y, int w, int h);
39 	virtual void SetRect(SDL_Rect **bounds);
40 
41 	/* Return the whole area */
GetRect()42 	virtual SDL_Rect GetRect()
43 	  {return area;}
44 
45 	/* Return the bounds of the widget */
X()46 	virtual int X() { return area.x; }
Y()47 	virtual int Y() { return area.y; }
W()48 	virtual int W() { return area.w; }
H()49 	virtual int H() { return area.h; }
50 
51 	/* Check to see if a point intersects the bounds of the widget.
52 	 */
53 	virtual int HitRect(int x, int y);
54 	virtual int HitRect(int x, int y, SDL_Rect &rect);
55 
56 	/* Set the display surface for this widget */
57 	virtual void SetDisplay(SDL_Surface *display);
58 
59 	/* Show the widget.
60 	   If the surface needs to be locked, it will be locked
61 	   before this call, and unlocked after it returns.
62 	 */
63 	virtual void Display(void);
64 
65 	/* Redraw the widget and only the widget */
66 	virtual void Redraw(void);
67 
68 	/* GUI idle function -- run when no events pending */
69 	virtual GUI_status Idle(void);
70 
71 	/* Widget event handlers.
72 	   These functions should return a status telling the GUI whether
73 	   or not the event should be passed on to other widgets.
74 	   These are called by the default HandleEvent function.
75 	*/
76 	virtual GUI_status KeyDown(SDL_keysym key);
77 	virtual GUI_status KeyUp(SDL_keysym key);
78 	virtual GUI_status MouseDown(int x, int y, int button);
79 	virtual GUI_status MouseUp(int x, int y, int button);
80 	virtual GUI_status MouseMotion(int x, int y, Uint8 state);
81 
82 	/* Main event handler function.
83 	   This function gets raw SDL events from the GUI.
84 	 */
85 	virtual GUI_status HandleEvent(const SDL_Event *event);
86 
87 	/* Returns NULL if everything is okay, or an error message if not */
Error(void)88 	char *Error(void) {
89 		return(error);
90 	}
91 
92 	/* yields click state: none, pressed, intermediate */
ClickState(int button)93 	inline virtual int ClickState(int button)
94 	{ return pressed[button-1]; }
95 
96 	/* set click state from remote */
SetClickState(int button,int value)97 	inline virtual void SetClickState(int button, int value)
98 	{ if ((button>0) && (button<=3)) pressed[button-1]=value; }
99 
100 protected:
101 	/* The constructor, separated out for both access constructors */
102 	void Init(void *data, int x, int y, int w, int h);
103 
104 	/* A generic pointer to user-specified data for the widget.
105 	 */
106 	void *widget_data;
107 
108 	/* The display surface for the widget */
109 	SDL_Surface *screen;
110 
111 	/* The area covered by the widget */
112 	SDL_Rect area;
113 
114 	/* Flag -- whether or not the widget should be freed */
115 	int status;
116 
117 	/* Useful for getting error feedback */
SetError(char * fmt,...)118 	void SetError(char *fmt, ...) {
119 		va_list ap;
120 
121 		va_start(ap, fmt);
122 		vsprintf(errbuf, fmt, ap);
123 		va_end(ap);
124 		error = errbuf;
125 	}
126 	char *error;
127 	char  errbuf[BUFSIZ];
128 
129 	/* the button states for theoretically 3 buttons */
130 	int pressed[3];
131 };
132 
133 #endif /* _GUI_widget_h */
134