1 /* Very basic widgets for SDL
2  * Ben Lynn
3  */
4 /*
5 Copyright (C) 2004 Benjamin Lynn (blynn@cs.stanford.edu)
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 #include "widget.h"
22 #include "colour.h"
23 
widget_computexy(widget_ptr wid)24 void widget_computexy(widget_ptr wid)
25 {
26     wid->x = wid->localx;
27     wid->y = wid->localy;
28     if (wid->parent) {
29 	wid->x += wid->parent->x;
30 	wid->y += wid->parent->y;
31     }
32 }
33 
widget_init(widget_ptr wid)34 void widget_init(widget_ptr wid)
35 {
36     int i;
37 
38     wid->parent = NULL;
39     wid->update = NULL;
40     wid->handle_click = NULL;
41     for (i=0; i<signal_count; i++) {
42 	wid->signal_handler[i] = NULL;
43 	wid->data[i] = NULL;
44     }
45     wid->computexy = widget_computexy;
46 }
47 
widget_put_geometry(void * p,int x,int y,int w,int h)48 void widget_put_geometry(void *p, int x, int y, int w, int h)
49 {
50     struct widget_s *wid = (struct widget_s *) p;
51     wid->localx = x;
52     wid->localy = y;
53     wid->w = w;
54     wid->h = h;
55 }
56 
widget_put_handler(widget_ptr p,void (* f)(widget_ptr,void *),int sig)57 void widget_put_handler(widget_ptr p, void (* f)(widget_ptr, void *), int sig)
58 {
59     p->signal_handler[sig] = f;
60 }
61 
widget_put_handler_data(widget_ptr p,void (* f)(widget_ptr,void *),void * data,int sig)62 void widget_put_handler_data(widget_ptr p,
63 	void (* f)(widget_ptr, void *), void *data, int sig)
64 {
65     p->signal_handler[sig] = f;
66     p->data[sig] = data;
67 }
68 
widget_fill(widget_ptr w,int c)69 void widget_fill(widget_ptr w, int c)
70 {
71     SDL_Rect rect;
72     rect.x = w->x;
73     rect.y = w->y;
74     rect.w = w->w;
75     rect.h = w->h;
76 
77     SDL_FillRect(screen, &rect, ctable[c]);
78 }
79 
widget_fillrect(widget_ptr w,SDL_Rect * rect,int c)80 void widget_fillrect(widget_ptr w, SDL_Rect *rect, int c)
81 {
82     SDL_Rect r;
83 
84     r.x = rect->x + w->x;
85     r.y = rect->y + w->y;
86     r.w = rect->w;
87     r.h = rect->h;
88     SDL_FillRect(screen, &r, ctable[c]);
89 }
90 
widget_blit(void * p,SDL_Surface * s,SDL_Rect * src,SDL_Rect * dst)91 void widget_blit(void *p, SDL_Surface *s, SDL_Rect *src, SDL_Rect *dst)
92 {
93     widget_ptr wid = (widget_ptr) p;
94     dst->x += wid->x;
95     dst->y += wid->y;
96     SDL_BlitSurface(s, src, screen, dst);
97     dst->x -= wid->x;
98     dst->y -= wid->y;
99 }
100 
widget_raise_signal(widget_ptr w,int sig)101 void widget_raise_signal(widget_ptr w, int sig)
102 {
103     void (*f)();
104     f = w->signal_handler[sig];
105     if (f) {
106 	f(w, w->data[sig]);
107     }
108 }
109 
widget_update(void * p)110 void widget_update(void *p)
111 {
112     ((widget_ptr) p)->update(p);
113 }
114 
in_widget(widget_ptr wid,int x,int y)115 int in_widget(widget_ptr wid, int x, int y)
116 {
117     return (wid->x <= x && x < wid->x + wid->w
118 		&& wid->y <= y && y < wid->y + wid->h);
119 }
120 
widget_box(widget_ptr w,int x0,int y0,int x1,int y1,int c)121 void widget_box(widget_ptr w, int x0, int y0, int x1, int y1, int c)
122 {
123     SDL_Rect r;
124 
125     r.x = w->x + x0;
126     r.y = w->y + y0;
127     r.w = x1 - x0 + 1;
128     r.h = y1 - y0 + 1;
129     SDL_FillRect(screen, &r, ctable[c]);
130 }
131 
widget_raised_border(widget_ptr rect)132 void widget_raised_border(widget_ptr rect)
133 {
134     int x0, y0;
135     x0 = rect->w - 1;
136     y0 = rect->h - 1;
137     widget_box(rect, 1, y0 - 1, x0 - 1, y0 - 1, c_shadow);
138     widget_box(rect, x0 - 1, 1, x0 - 1, y0 - 1, c_shadow);
139 
140     widget_box(rect, 0, 0, x0, 0, c_highlight);
141     widget_box(rect, 0, 0, 0, y0, c_highlight);
142 
143     widget_box(rect, 0, y0, x0, y0, c_darkshadow);
144     widget_box(rect, x0, 0, x0, y0, c_darkshadow);
145 }
146 
widget_raised_background(widget_ptr rect)147 void widget_raised_background(widget_ptr rect)
148 {
149     widget_fill(rect, c_background);
150     widget_raised_border(rect);
151 }
152 
widget_lowered_border(widget_ptr rect)153 void widget_lowered_border(widget_ptr rect)
154 {
155     int x1, y1;
156     x1 = rect->w - 1;
157     y1 = rect->h - 2;
158     widget_box(rect, 2, y1, x1 - 1, y1, c_background);
159     widget_box(rect, x1 - 1, 2, x1 - 1, y1, c_background);
160     y1++;
161     widget_box(rect, 1, y1, x1 - 1, y1, c_highlight);
162     widget_box(rect, x1, 1, x1, y1, c_highlight);
163 
164     widget_box(rect, 0, 0, 0, y1, c_shadow);
165     widget_box(rect, 0, 0, x1, 0, c_shadow);
166 
167     widget_box(rect, 1, 1, 1, y1 - 1, c_darkshadow);
168     widget_box(rect, 1, 1, x1 - 1, 1, c_darkshadow);
169 }
170 
widget_lowered_background(widget_ptr rect)171 void widget_lowered_background(widget_ptr rect)
172 {
173     widget_fill(rect, c_background);
174     widget_lowered_border(rect);
175 }
176