Mercurial > pidgin
annotate finch/libgnt/gntbox.c @ 18222:ef65d43190e5
Fix a few runtime warnings.
author | Sadrul Habib Chowdhury <imadil@gmail.com> |
---|---|
date | Thu, 21 Jun 2007 21:32:13 +0000 |
parents | 1cedd520cd18 |
children | 2e11d73c4ff1 ac904659104f |
rev | line source |
---|---|
18049
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
1 /** |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
2 * GNT - The GLib Ncurses Toolkit |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
3 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
4 * GNT is the legal property of its developers, whose names are too numerous |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
6 * source distribution. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
7 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
8 * This library is free software; you can redistribute it and/or modify |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
9 * it under the terms of the GNU General Public License as published by |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
10 * the Free Software Foundation; either version 2 of the License, or |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
11 * (at your option) any later version. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
12 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
13 * This program is distributed in the hope that it will be useful, |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
16 * GNU General Public License for more details. |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
17 * |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
18 * You should have received a copy of the GNU General Public License |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
19 * along with this program; if not, write to the Free Software |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
21 */ |
1cedd520cd18
Doxygen skeleton and license info for gnt files.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
18000
diff
changeset
|
22 |
15817 | 23 #include "gntbox.h" |
24 #include "gntutils.h" | |
25 | |
26 #include <string.h> | |
27 | |
28 enum | |
29 { | |
30 SIGS = 1, | |
31 }; | |
32 | |
33 static GntWidgetClass *parent_class = NULL; | |
34 | |
35 static GntWidget * find_focusable_widget(GntBox *box); | |
36 | |
37 static void | |
38 add_to_focus(gpointer value, gpointer data) | |
39 { | |
40 GntBox *box = GNT_BOX(data); | |
41 GntWidget *w = GNT_WIDGET(value); | |
42 | |
43 if (GNT_IS_BOX(w)) | |
44 g_list_foreach(GNT_BOX(w)->list, add_to_focus, box); | |
45 else if (GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_CAN_TAKE_FOCUS)) | |
46 box->focus = g_list_append(box->focus, w); | |
47 } | |
48 | |
49 static void | |
50 get_title_thingies(GntBox *box, char *title, int *p, int *r) | |
51 { | |
52 GntWidget *widget = GNT_WIDGET(box); | |
53 int len; | |
54 char *end = (char*)gnt_util_onscreen_width_to_pointer(title, widget->priv.width - 4, &len); | |
55 | |
56 if (p) | |
57 *p = (widget->priv.width - len) / 2; | |
58 if (r) | |
59 *r = (widget->priv.width + len) / 2; | |
60 *end = '\0'; | |
61 } | |
62 | |
63 static void | |
64 gnt_box_draw(GntWidget *widget) | |
65 { | |
66 GntBox *box = GNT_BOX(widget); | |
67 | |
68 if (box->focus == NULL && widget->parent == NULL) | |
69 g_list_foreach(box->list, add_to_focus, box); | |
70 | |
71 g_list_foreach(box->list, (GFunc)gnt_widget_draw, NULL); | |
72 | |
73 gnt_box_sync_children(box); | |
74 | |
75 if (box->title && !GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
76 { | |
77 int pos, right; | |
78 char *title = g_strdup(box->title); | |
79 | |
80 get_title_thingies(box, title, &pos, &right); | |
81 | |
82 if (gnt_widget_has_focus(widget)) | |
83 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_TITLE)); | |
84 else | |
85 wbkgdset(widget->window, '\0' | COLOR_PAIR(GNT_COLOR_TITLE_D)); | |
86 mvwaddch(widget->window, 0, pos-1, ACS_RTEE | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
87 mvwaddstr(widget->window, 0, pos, title); | |
88 mvwaddch(widget->window, 0, right, ACS_LTEE | COLOR_PAIR(GNT_COLOR_NORMAL)); | |
89 g_free(title); | |
90 } | |
91 | |
92 GNTDEBUG; | |
93 } | |
94 | |
95 static void | |
96 reposition_children(GntWidget *widget) | |
97 { | |
98 GList *iter; | |
99 GntBox *box = GNT_BOX(widget); | |
100 int w, h, curx, cury, max; | |
101 gboolean has_border = FALSE; | |
102 | |
103 w = h = 0; | |
104 max = 0; | |
105 curx = widget->priv.x; | |
106 cury = widget->priv.y; | |
107 if (!(GNT_WIDGET_FLAGS(widget) & GNT_WIDGET_NO_BORDER)) | |
108 { | |
109 has_border = TRUE; | |
110 curx += 1; | |
111 cury += 1; | |
112 } | |
113 | |
114 for (iter = box->list; iter; iter = iter->next) | |
115 { | |
116 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(iter->data), GNT_WIDGET_INVISIBLE)) | |
117 continue; | |
118 gnt_widget_set_position(GNT_WIDGET(iter->data), curx, cury); | |
119 gnt_widget_get_size(GNT_WIDGET(iter->data), &w, &h); | |
120 if (box->vertical) | |
121 { | |
122 if (h) | |
123 { | |
124 cury += h + box->pad; | |
125 if (max < w) | |
126 max = w; | |
127 } | |
128 } | |
129 else | |
130 { | |
131 if (w) | |
132 { | |
133 curx += w + box->pad; | |
134 if (max < h) | |
135 max = h; | |
136 } | |
137 } | |
138 } | |
139 | |
140 if (has_border) | |
141 { | |
142 curx += 1; | |
143 cury += 1; | |
144 max += 2; | |
145 } | |
146 | |
147 if (box->list) | |
148 { | |
149 if (box->vertical) | |
150 cury -= box->pad; | |
151 else | |
152 curx -= box->pad; | |
153 } | |
154 | |
155 if (box->vertical) | |
156 { | |
157 widget->priv.width = max; | |
158 widget->priv.height = cury - widget->priv.y; | |
159 } | |
160 else | |
161 { | |
162 widget->priv.width = curx - widget->priv.x; | |
163 widget->priv.height = max; | |
164 } | |
165 } | |
166 | |
167 static void | |
168 gnt_box_set_position(GntWidget *widget, int x, int y) | |
169 { | |
170 GList *iter; | |
171 int changex, changey; | |
172 | |
173 changex = widget->priv.x - x; | |
174 changey = widget->priv.y - y; | |
175 | |
176 for (iter = GNT_BOX(widget)->list; iter; iter = iter->next) | |
177 { | |
178 GntWidget *w = GNT_WIDGET(iter->data); | |
179 gnt_widget_set_position(w, w->priv.x - changex, | |
180 w->priv.y - changey); | |
181 } | |
182 } | |
183 | |
184 static void | |
185 gnt_box_size_request(GntWidget *widget) | |
186 { | |
187 GntBox *box = GNT_BOX(widget); | |
188 GList *iter; | |
189 int maxw = 0, maxh = 0; | |
190 | |
191 g_list_foreach(box->list, (GFunc)gnt_widget_size_request, NULL); | |
192 | |
193 for (iter = box->list; iter; iter = iter->next) | |
194 { | |
195 int w, h; | |
196 gnt_widget_get_size(GNT_WIDGET(iter->data), &w, &h); | |
197 if (maxh < h) | |
198 maxh = h; | |
199 if (maxw < w) | |
200 maxw = w; | |
201 } | |
202 | |
203 for (iter = box->list; iter; iter = iter->next) | |
204 { | |
205 int w, h; | |
206 GntWidget *wid = GNT_WIDGET(iter->data); | |
207 | |
208 gnt_widget_get_size(wid, &w, &h); | |
209 | |
210 if (box->homogeneous) | |
211 { | |
212 if (box->vertical) | |
213 h = maxh; | |
214 else | |
215 w = maxw; | |
216 } | |
217 if (box->fill) | |
218 { | |
219 if (box->vertical) | |
220 w = maxw; | |
221 else | |
222 h = maxh; | |
223 } | |
224 | |
225 gnt_widget_set_size(wid, w, h); | |
226 } | |
227 | |
228 reposition_children(widget); | |
229 } | |
230 | |
231 static void | |
232 gnt_box_map(GntWidget *widget) | |
233 { | |
234 if (widget->priv.width == 0 || widget->priv.height == 0) | |
235 { | |
236 gnt_widget_size_request(widget); | |
237 find_focusable_widget(GNT_BOX(widget)); | |
238 } | |
239 GNTDEBUG; | |
240 } | |
241 | |
242 /* Ensures that the current widget can take focus */ | |
243 static GntWidget * | |
244 find_focusable_widget(GntBox *box) | |
245 { | |
246 /* XXX: Make sure the widget is visible? */ | |
247 if (box->focus == NULL && GNT_WIDGET(box)->parent == NULL) | |
248 g_list_foreach(box->list, add_to_focus, box); | |
249 | |
250 if (box->active == NULL && box->focus) | |
251 box->active = box->focus->data; | |
252 | |
253 return box->active; | |
254 } | |
255 | |
256 static void | |
257 find_next_focus(GntBox *box) | |
258 { | |
259 gpointer last = box->active; | |
260 do | |
261 { | |
262 GList *iter = g_list_find(box->focus, box->active); | |
263 if (iter && iter->next) | |
264 box->active = iter->next->data; | |
265 else if (box->focus) | |
266 box->active = box->focus->data; | |
267 if (!GNT_WIDGET_IS_FLAG_SET(box->active, GNT_WIDGET_INVISIBLE)) | |
268 break; | |
269 } while (box->active != last); | |
270 } | |
271 | |
272 static void | |
273 find_prev_focus(GntBox *box) | |
274 { | |
275 gpointer last = box->active; | |
276 | |
277 if (!box->focus) | |
278 return; | |
279 | |
280 do | |
281 { | |
282 GList *iter = g_list_find(box->focus, box->active); | |
283 if (!iter) | |
284 box->active = box->focus->data; | |
285 else if (!iter->prev) | |
286 box->active = g_list_last(box->focus)->data; | |
287 else | |
288 box->active = iter->prev->data; | |
289 if (!GNT_WIDGET_IS_FLAG_SET(box->active, GNT_WIDGET_INVISIBLE)) | |
290 break; | |
291 } while (box->active != last); | |
292 } | |
293 | |
294 static gboolean | |
295 gnt_box_key_pressed(GntWidget *widget, const char *text) | |
296 { | |
297 GntBox *box = GNT_BOX(widget); | |
298 GntWidget *now; | |
299 | |
300 if (box->active == NULL && !find_focusable_widget(box)) | |
301 return FALSE; | |
302 | |
303 if (gnt_widget_key_pressed(box->active, text)) | |
304 return TRUE; | |
305 | |
306 now = box->active; | |
307 | |
308 if (text[0] == 27) | |
309 { | |
310 if (strcmp(text, GNT_KEY_LEFT) == 0) | |
311 { | |
312 find_prev_focus(box); | |
313 } | |
314 else if (strcmp(text, GNT_KEY_RIGHT) == 0) | |
315 { | |
316 find_next_focus(box); | |
317 } | |
18000
05d7fe2320a9
(16:39:44) rekkanoryo: i have a feature request
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15945
diff
changeset
|
318 else if (strcmp(text, GNT_KEY_BACK_TAB) == 0) |
05d7fe2320a9
(16:39:44) rekkanoryo: i have a feature request
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15945
diff
changeset
|
319 { |
05d7fe2320a9
(16:39:44) rekkanoryo: i have a feature request
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15945
diff
changeset
|
320 find_prev_focus(box); |
05d7fe2320a9
(16:39:44) rekkanoryo: i have a feature request
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15945
diff
changeset
|
321 } |
15817 | 322 } |
323 else if (text[0] == '\t') | |
324 { | |
325 find_next_focus(box); | |
326 } | |
327 | |
328 if (now && now != box->active) | |
329 { | |
330 gnt_widget_set_focus(now, FALSE); | |
331 gnt_widget_set_focus(box->active, TRUE); | |
332 return TRUE; | |
333 } | |
334 | |
335 return FALSE; | |
336 } | |
337 | |
338 static void | |
339 gnt_box_lost_focus(GntWidget *widget) | |
340 { | |
341 GntWidget *w = GNT_BOX(widget)->active; | |
342 if (w) | |
343 gnt_widget_set_focus(w, FALSE); | |
344 gnt_widget_draw(widget); | |
345 } | |
346 | |
347 static void | |
348 gnt_box_gained_focus(GntWidget *widget) | |
349 { | |
350 GntWidget *w = GNT_BOX(widget)->active; | |
351 if (w) | |
352 gnt_widget_set_focus(w, TRUE); | |
353 gnt_widget_draw(widget); | |
354 } | |
355 | |
356 static void | |
357 gnt_box_destroy(GntWidget *w) | |
358 { | |
359 GntBox *box = GNT_BOX(w); | |
360 | |
361 gnt_box_remove_all(box); | |
362 gnt_screen_release(w); | |
363 } | |
364 | |
365 static void | |
366 gnt_box_expose(GntWidget *widget, int x, int y, int width, int height) | |
367 { | |
368 WINDOW *win = newwin(height, width, widget->priv.y + y, widget->priv.x + x); | |
369 copywin(widget->window, win, y, x, 0, 0, height - 1, width - 1, FALSE); | |
370 wrefresh(win); | |
371 delwin(win); | |
372 } | |
373 | |
374 static gboolean | |
375 gnt_box_confirm_size(GntWidget *widget, int width, int height) | |
376 { | |
377 GList *iter; | |
378 GntBox *box = GNT_BOX(widget); | |
379 int wchange, hchange; | |
380 | |
381 if (!box->list) | |
382 return TRUE; | |
383 | |
384 wchange = widget->priv.width - width; | |
385 hchange = widget->priv.height - height; | |
386 | |
387 if (wchange == 0 && hchange == 0) | |
388 return TRUE; /* Quit playing games */ | |
389 | |
390 /* XXX: Right now, I am trying to just apply all the changes to | |
391 * just one widget. It should be possible to distribute the | |
392 * changes to all the widgets in the box. */ | |
393 for (iter = box->list; iter; iter = iter->next) | |
394 { | |
395 GntWidget *wid = iter->data; | |
396 int w, h; | |
397 | |
398 gnt_widget_get_size(wid, &w, &h); | |
399 | |
400 if (gnt_widget_confirm_size(wid, w - wchange, h - hchange)) | |
401 { | |
402 GList *i; | |
403 | |
404 for (i = box->list; i; i = i->next) | |
405 { | |
406 int tw, th; | |
407 if (i == iter) continue; | |
408 gnt_widget_get_size(GNT_WIDGET(i->data), &tw, &th); | |
409 if (box->vertical) | |
410 { | |
15945
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
411 if (!gnt_widget_confirm_size(i->data, tw - wchange, th)) { |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
412 /* If we are decreasing the size and the widget is going |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
413 * to be too large to fit into the box, then do not allow |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
414 * resizing. */ |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
415 if (wchange > 0 && tw >= widget->priv.width) |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
416 return FALSE; |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
417 } |
15817 | 418 } |
419 else | |
420 { | |
15945
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
421 if (!gnt_widget_confirm_size(i->data, tw, th - hchange)) { |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
422 if (hchange > 0 && th >= widget->priv.height) |
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
423 return FALSE; |
15817 | 424 return FALSE; |
15945
92fa7d6e75b0
A few more fixes for resizing.
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
15817
diff
changeset
|
425 } |
15817 | 426 } |
427 } | |
428 #if 0 | |
429 gnt_widget_set_size(wid, w - wchange, h - hchange); | |
430 if (box->vertical) | |
431 hchange = 0; | |
432 else | |
433 wchange = 0; | |
434 | |
435 for (i = box->list; i; i = i->next) | |
436 { | |
437 int tw, th; | |
438 if (i == iter) continue; | |
439 gnt_widget_get_size(GNT_WIDGET(i->data), &tw, &th); | |
440 gnt_widget_set_size(i->data, tw - wchange, th - hchange); | |
441 } | |
442 #endif | |
443 g_object_set_data(G_OBJECT(box), "size-queued", wid); | |
444 return TRUE; | |
445 } | |
446 } | |
447 | |
448 return FALSE; | |
449 } | |
450 | |
451 static void | |
452 gnt_box_size_changed(GntWidget *widget, int oldw, int oldh) | |
453 { | |
454 int wchange, hchange; | |
455 GList *i; | |
456 GntBox *box = GNT_BOX(widget); | |
457 GntWidget *wid; | |
458 int tw, th; | |
459 | |
460 wchange = widget->priv.width - oldw; | |
461 hchange = widget->priv.height - oldh; | |
462 | |
463 wid = g_object_get_data(G_OBJECT(box), "size-queued"); | |
464 if (wid) | |
465 { | |
466 gnt_widget_get_size(wid, &tw, &th); | |
467 gnt_widget_set_size(wid, tw + wchange, th + hchange); | |
468 g_object_set_data(G_OBJECT(box), "size-queued", NULL); | |
469 } | |
470 | |
471 if (box->vertical) | |
472 hchange = 0; | |
473 else | |
474 wchange = 0; | |
475 | |
476 for (i = box->list; i; i = i->next) | |
477 { | |
478 if (wid != i->data) | |
479 { | |
480 gnt_widget_get_size(GNT_WIDGET(i->data), &tw, &th); | |
481 gnt_widget_set_size(i->data, tw + wchange, th + hchange); | |
482 } | |
483 } | |
484 | |
485 reposition_children(widget); | |
486 } | |
487 | |
488 static gboolean | |
489 gnt_box_clicked(GntWidget *widget, GntMouseEvent event, int cx, int cy) | |
490 { | |
491 GList *iter; | |
492 for (iter = GNT_BOX(widget)->list; iter; iter = iter->next) { | |
493 int x, y, w, h; | |
494 GntWidget *wid = iter->data; | |
495 | |
496 gnt_widget_get_position(wid, &x, &y); | |
497 gnt_widget_get_size(wid, &w, &h); | |
498 | |
499 if (cx >= x && cx < x + w && cy >= y && cy < y + h) { | |
500 if (event <= GNT_MIDDLE_MOUSE_DOWN && | |
501 GNT_WIDGET_IS_FLAG_SET(wid, GNT_WIDGET_CAN_TAKE_FOCUS)) { | |
502 while (widget->parent) | |
503 widget = widget->parent; | |
504 gnt_box_give_focus_to_child(GNT_BOX(widget), wid); | |
505 } | |
506 return gnt_widget_clicked(wid, event, cx, cy); | |
507 } | |
508 } | |
509 return FALSE; | |
510 } | |
511 | |
512 static void | |
513 gnt_box_class_init(GntBoxClass *klass) | |
514 { | |
515 parent_class = GNT_WIDGET_CLASS(klass); | |
516 parent_class->destroy = gnt_box_destroy; | |
517 parent_class->draw = gnt_box_draw; | |
518 parent_class->expose = gnt_box_expose; | |
519 parent_class->map = gnt_box_map; | |
520 parent_class->size_request = gnt_box_size_request; | |
521 parent_class->set_position = gnt_box_set_position; | |
522 parent_class->key_pressed = gnt_box_key_pressed; | |
523 parent_class->clicked = gnt_box_clicked; | |
524 parent_class->lost_focus = gnt_box_lost_focus; | |
525 parent_class->gained_focus = gnt_box_gained_focus; | |
526 parent_class->confirm_size = gnt_box_confirm_size; | |
527 parent_class->size_changed = gnt_box_size_changed; | |
528 | |
529 GNTDEBUG; | |
530 } | |
531 | |
532 static void | |
533 gnt_box_init(GTypeInstance *instance, gpointer class) | |
534 { | |
535 GntWidget *widget = GNT_WIDGET(instance); | |
536 GntBox *box = GNT_BOX(widget); | |
537 /* Initially make both the height and width resizable. | |
538 * Update the flags as necessary when widgets are added to it. */ | |
539 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_GROW_X | GNT_WIDGET_GROW_Y); | |
540 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_CAN_TAKE_FOCUS); | |
541 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW); | |
542 box->pad = 1; | |
543 box->fill = TRUE; | |
544 GNTDEBUG; | |
545 } | |
546 | |
547 /****************************************************************************** | |
548 * GntBox API | |
549 *****************************************************************************/ | |
550 GType | |
551 gnt_box_get_gtype(void) | |
552 { | |
553 static GType type = 0; | |
554 | |
555 if(type == 0) | |
556 { | |
557 static const GTypeInfo info = { | |
558 sizeof(GntBoxClass), | |
559 NULL, /* base_init */ | |
560 NULL, /* base_finalize */ | |
561 (GClassInitFunc)gnt_box_class_init, | |
562 NULL, /* class_finalize */ | |
563 NULL, /* class_data */ | |
564 sizeof(GntBox), | |
565 0, /* n_preallocs */ | |
566 gnt_box_init, /* instance_init */ | |
567 NULL /* value_table */ | |
568 }; | |
569 | |
570 type = g_type_register_static(GNT_TYPE_WIDGET, | |
571 "GntBox", | |
572 &info, 0); | |
573 } | |
574 | |
575 return type; | |
576 } | |
577 | |
578 GntWidget *gnt_box_new(gboolean homo, gboolean vert) | |
579 { | |
580 GntWidget *widget = g_object_new(GNT_TYPE_BOX, NULL); | |
581 GntBox *box = GNT_BOX(widget); | |
582 | |
583 box->homogeneous = homo; | |
584 box->vertical = vert; | |
585 box->alignment = vert ? GNT_ALIGN_LEFT : GNT_ALIGN_MID; | |
586 | |
587 return widget; | |
588 } | |
589 | |
590 void gnt_box_add_widget(GntBox *b, GntWidget *widget) | |
591 { | |
592 b->list = g_list_append(b->list, widget); | |
593 widget->parent = GNT_WIDGET(b); | |
594 } | |
595 | |
596 void gnt_box_set_title(GntBox *b, const char *title) | |
597 { | |
598 char *prev = b->title; | |
599 GntWidget *w = GNT_WIDGET(b); | |
600 b->title = g_strdup(title); | |
601 if (w->window && !GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_NO_BORDER)) { | |
602 /* Erase the old title */ | |
603 int pos, right; | |
604 get_title_thingies(b, prev, &pos, &right); | |
605 mvwhline(w->window, 0, pos - 1, ACS_HLINE | COLOR_PAIR(GNT_COLOR_NORMAL), | |
606 right - pos + 2); | |
607 g_free(prev); | |
608 } | |
609 } | |
610 | |
611 void gnt_box_set_pad(GntBox *box, int pad) | |
612 { | |
613 box->pad = pad; | |
614 /* XXX: Perhaps redraw if already showing? */ | |
615 } | |
616 | |
617 void gnt_box_set_toplevel(GntBox *box, gboolean set) | |
618 { | |
619 GntWidget *widget = GNT_WIDGET(box); | |
620 if (set) | |
621 { | |
622 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW); | |
623 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_CAN_TAKE_FOCUS); | |
624 } | |
625 else | |
626 { | |
627 GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW); | |
628 GNT_WIDGET_UNSET_FLAGS(widget, GNT_WIDGET_CAN_TAKE_FOCUS); | |
629 } | |
630 } | |
631 | |
632 void gnt_box_sync_children(GntBox *box) | |
633 { | |
634 GList *iter; | |
635 GntWidget *widget = GNT_WIDGET(box); | |
636 int pos = 1; | |
637 | |
638 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_NO_BORDER)) | |
639 pos = 0; | |
640 | |
641 for (iter = box->list; iter; iter = iter->next) | |
642 { | |
643 GntWidget *w = GNT_WIDGET(iter->data); | |
644 int height, width; | |
645 int x, y; | |
646 | |
647 if (GNT_WIDGET_IS_FLAG_SET(w, GNT_WIDGET_INVISIBLE)) | |
648 continue; | |
649 | |
650 if (GNT_IS_BOX(w)) | |
651 gnt_box_sync_children(GNT_BOX(w)); | |
652 | |
653 gnt_widget_get_size(w, &width, &height); | |
654 | |
655 x = w->priv.x - widget->priv.x; | |
656 y = w->priv.y - widget->priv.y; | |
657 | |
658 if (box->vertical) | |
659 { | |
660 x = pos; | |
661 if (box->alignment == GNT_ALIGN_RIGHT) | |
662 x += widget->priv.width - width; | |
663 else if (box->alignment == GNT_ALIGN_MID) | |
664 x += (widget->priv.width - width)/2; | |
665 if (x + width > widget->priv.width - pos) | |
666 x -= x + width - (widget->priv.width - pos); | |
667 } | |
668 else | |
669 { | |
670 y = pos; | |
671 if (box->alignment == GNT_ALIGN_BOTTOM) | |
672 y += widget->priv.height - height; | |
673 else if (box->alignment == GNT_ALIGN_MID) | |
674 y += (widget->priv.height - height)/2; | |
675 if (y + height >= widget->priv.height - pos) | |
676 y = widget->priv.height - height - pos; | |
677 } | |
678 | |
679 copywin(w->window, widget->window, 0, 0, | |
680 y, x, y + height - 1, x + width - 1, FALSE); | |
681 gnt_widget_set_position(w, x + widget->priv.x, y + widget->priv.y); | |
682 } | |
683 } | |
684 | |
685 void gnt_box_set_alignment(GntBox *box, GntAlignment alignment) | |
686 { | |
687 box->alignment = alignment; | |
688 } | |
689 | |
690 void gnt_box_remove(GntBox *box, GntWidget *widget) | |
691 { | |
692 box->list = g_list_remove(box->list, widget); | |
693 if (GNT_WIDGET_IS_FLAG_SET(widget, GNT_WIDGET_CAN_TAKE_FOCUS) | |
694 && GNT_WIDGET(box)->parent == NULL && box->focus) | |
695 { | |
696 if (widget == box->active) | |
697 { | |
698 find_next_focus(box); | |
699 if (box->active == widget) /* There's only one widget */ | |
700 box->active = NULL; | |
701 } | |
702 box->focus = g_list_remove(box->focus, widget); | |
703 } | |
704 | |
705 if (GNT_WIDGET_IS_FLAG_SET(GNT_WIDGET(box), GNT_WIDGET_MAPPED)) | |
706 gnt_widget_draw(GNT_WIDGET(box)); | |
707 } | |
708 | |
709 void gnt_box_remove_all(GntBox *box) | |
710 { | |
711 g_list_foreach(box->list, (GFunc)gnt_widget_destroy, NULL); | |
712 g_list_free(box->list); | |
713 g_list_free(box->focus); | |
714 box->list = NULL; | |
715 box->focus = NULL; | |
716 GNT_WIDGET(box)->priv.width = 0; | |
717 GNT_WIDGET(box)->priv.height = 0; | |
718 } | |
719 | |
720 void gnt_box_readjust(GntBox *box) | |
721 { | |
722 GList *iter; | |
723 GntWidget *wid; | |
724 int width, height; | |
725 | |
726 if (GNT_WIDGET(box)->parent != NULL) | |
727 return; | |
728 | |
729 for (iter = box->list; iter; iter = iter->next) | |
730 { | |
731 GntWidget *w = iter->data; | |
732 if (GNT_IS_BOX(w)) | |
733 gnt_box_readjust(GNT_BOX(w)); | |
734 else | |
735 { | |
736 GNT_WIDGET_UNSET_FLAGS(w, GNT_WIDGET_MAPPED); | |
737 w->priv.width = 0; | |
738 w->priv.height = 0; | |
739 } | |
740 } | |
741 | |
742 wid = GNT_WIDGET(box); | |
743 GNT_WIDGET_UNSET_FLAGS(wid, GNT_WIDGET_MAPPED); | |
744 wid->priv.width = 0; | |
745 wid->priv.height = 0; | |
746 | |
747 if (wid->parent == NULL) | |
748 { | |
749 g_list_free(box->focus); | |
750 box->focus = NULL; | |
751 box->active = NULL; | |
752 gnt_widget_size_request(wid); | |
753 gnt_widget_get_size(wid, &width, &height); | |
754 gnt_screen_resize_widget(wid, width, height); | |
755 find_focusable_widget(box); | |
756 } | |
757 } | |
758 | |
759 void gnt_box_set_fill(GntBox *box, gboolean fill) | |
760 { | |
761 box->fill = fill; | |
762 } | |
763 | |
764 void gnt_box_move_focus(GntBox *box, int dir) | |
765 { | |
766 GntWidget *now; | |
767 | |
768 if (box->active == NULL) | |
769 { | |
770 find_focusable_widget(box); | |
771 return; | |
772 } | |
773 | |
774 now = box->active; | |
775 | |
776 if (dir == 1) | |
777 find_next_focus(box); | |
778 else if (dir == -1) | |
779 find_prev_focus(box); | |
780 | |
781 if (now && now != box->active) | |
782 { | |
783 gnt_widget_set_focus(now, FALSE); | |
784 gnt_widget_set_focus(box->active, TRUE); | |
785 } | |
786 | |
787 if (GNT_WIDGET(box)->window) | |
788 gnt_widget_draw(GNT_WIDGET(box)); | |
789 } | |
790 | |
791 void gnt_box_give_focus_to_child(GntBox *box, GntWidget *widget) | |
792 { | |
793 GList *find = g_list_find(box->focus, widget); | |
794 gpointer now = box->active; | |
795 if (find) | |
796 box->active = widget; | |
797 if (now && now != box->active) | |
798 { | |
799 gnt_widget_set_focus(now, FALSE); | |
800 gnt_widget_set_focus(box->active, TRUE); | |
801 } | |
802 | |
803 if (GNT_WIDGET(box)->window) | |
804 gnt_widget_draw(GNT_WIDGET(box)); | |
805 } | |
806 |