1 /*
2     GUILIB:  An example GUI framework library for use with SDL
3 */
4 
5 #include <stdio.h>
6 #include "GUI_generic.h"
7 
8 
GUI_GenericWidget(void * data,GUI_DrawProc drawproc,GUI_EventProc eventproc,GUI_FreeProc freeproc)9 GUI_GenericWidget:: GUI_GenericWidget(void *data, GUI_DrawProc drawproc,
10 			GUI_EventProc eventproc, GUI_FreeProc freeproc)
11  : GUI_Widget(data)
12 {
13 	DrawProc = drawproc;
14 	EventProc = eventproc;
15 	FreeProc = freeproc;
16 }
17 
~GUI_GenericWidget()18 GUI_GenericWidget:: ~GUI_GenericWidget()
19 {
20 	if ( FreeProc ) {
21 		widget_info info;
22 
23 		FillInfo(&info);
24 		FreeProc(&info);
25 	}
26 }
27 
28 void
FillInfo(widget_info * info)29 GUI_GenericWidget:: FillInfo(widget_info *info)
30 {
31 	info->widget_data = widget_data;
32 	info->screen = screen;
33 	info->area = area;
34 }
35 
36 void
Display(void)37 GUI_GenericWidget:: Display(void)
38 {
39 	if ( DrawProc ) {
40 		widget_info info;
41 
42 		FillInfo(&info);
43 		DrawProc(&info);
44 	}
45 }
46 
47 GUI_status
HandleEvent(const SDL_Event * event)48 GUI_GenericWidget:: HandleEvent(const SDL_Event *event)
49 {
50 	GUI_status status;
51 
52 	status = GUI_PASS;
53 	if ( EventProc ) {
54 		int handle_it;
55 
56 		/* Mouse events outside the widget area are ignored */
57 		handle_it = 1;
58 		switch (event->type) {
59 			case SDL_MOUSEBUTTONDOWN:
60 			case SDL_MOUSEBUTTONUP: {
61 				int x, y;
62 				x = event->button.x;
63 				y = event->button.y;
64 				if ( ! HitRect(x, y) ) {
65 					handle_it = 0;
66 				}
67 			}
68 			break;
69 			case SDL_MOUSEMOTION: {
70 				int x, y;
71 				x = event->motion.x;
72 				y = event->motion.y;
73 				if ( ! HitRect(x, y) ) {
74 					handle_it = 0;
75 				}
76 			}
77 			break;
78 		}
79 		if ( handle_it ) {
80 			widget_info info;
81 
82 			FillInfo(&info);
83 			status = EventProc(&info, event);
84 		}
85 	}
86 	return(status);
87 }
88