Tue Jun 22 23:13:04 PDT 2004
[netwalk.git] / widget.c
blobcbbbf2019b42fcc441e15d77ce1a529e0e88a295
1 /* Very basic widgets for SDL
2 * Ben Lynn
3 */
4 /*
5 Copyright (C) 2004 Benjamin Lynn (blynn@cs.stanford.edu)
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.
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.
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.
21 #include "widget.h"
22 #include "colour.h"
24 void widget_computexy(widget_ptr wid)
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;
34 void widget_init(widget_ptr wid)
36 int i;
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;
45 wid->computexy = widget_computexy;
48 void widget_put_geometry(void *p, int x, int y, int w, int h)
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;
57 void widget_put_handler(widget_ptr p, void (* f)(widget_ptr, void *), int sig)
59 p->signal_handler[sig] = f;
62 void widget_put_handler_data(widget_ptr p,
63 void (* f)(widget_ptr, void *), void *data, int sig)
65 p->signal_handler[sig] = f;
66 p->data[sig] = data;
69 void widget_fill(widget_ptr w, int c)
71 SDL_Rect rect;
72 rect.x = w->x;
73 rect.y = w->y;
74 rect.w = w->w;
75 rect.h = w->h;
77 SDL_FillRect(screen, &rect, ctable[c]);
80 void widget_fillrect(widget_ptr w, SDL_Rect *rect, int c)
82 SDL_Rect r;
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]);
91 void widget_blit(void *p, SDL_Surface *s, SDL_Rect *src, SDL_Rect *dst)
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;
101 void widget_raise_signal(widget_ptr w, int sig)
103 void (*f)();
104 f = w->signal_handler[sig];
105 if (f) {
106 f(w, w->data[sig]);
110 void widget_update(void *p)
112 ((widget_ptr) p)->update(p);
115 int in_widget(widget_ptr wid, int x, int y)
117 return (wid->x <= x && x < wid->x + wid->w
118 && wid->y <= y && y < wid->y + wid->h);
121 void widget_box(widget_ptr w, int x0, int y0, int x1, int y1, int c)
123 SDL_Rect r;
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]);
132 void widget_raised_border(widget_ptr rect)
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);
140 widget_box(rect, 0, 0, x0, 0, c_highlight);
141 widget_box(rect, 0, 0, 0, y0, c_highlight);
143 widget_box(rect, 0, y0, x0, y0, c_darkshadow);
144 widget_box(rect, x0, 0, x0, y0, c_darkshadow);
147 void widget_raised_background(widget_ptr rect)
149 widget_fill(rect, c_background);
150 widget_raised_border(rect);
153 void widget_lowered_border(widget_ptr rect)
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);
164 widget_box(rect, 0, 0, 0, y1, c_shadow);
165 widget_box(rect, 0, 0, x1, 0, c_shadow);
167 widget_box(rect, 1, 1, 1, y1 - 1, c_darkshadow);
168 widget_box(rect, 1, 1, x1 - 1, 1, c_darkshadow);
171 void widget_lowered_background(widget_ptr rect)
173 widget_fill(rect, c_background);
174 widget_lowered_border(rect);