Debian Codegraph @

gdraw/gcontainer.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gdrawP.h"
31 #include "ggadget.h"
32 #include "ggadgetP.h"
33 #include "gkeysym.h"
34 #include "gwidgetP.h"
35 #include "utype.h"
36
37 static GWindow current_focus_window, previous_focus_window, last_input_window;
38 /* in focus follows pointer mode, the current focus doesn't really count */
39 /* until the window gets input (otherwise moving the mouse across a wind */
40 /* would set the previous_focus_window to something inappropriate) */
41 /* So... we set current focus window when a window gains the focus, and */
42 /* at the same time we set the previous focus window to the last window */
43 /* that got input. NOT to the old current_focus_window (which might be junk) */
44
45 GWindow GWindowGetCurrentFocusTopWindow(void) {
46 return( current_focus_window );
47 }
48
49 GWindow GWidgetGetPreviousFocusTopWindow(void) {
50 return( previous_focus_window );
51 }
52
53 GGadget *GWindowGetCurrentFocusGadget(void) {
54 GTopLevelD *td;
55 if ( current_focus_window==NULL )
56 return( NULL );
57 td = (GTopLevelD *) (current_focus_window->widget_data);
58 return( td->gfocus );
59 }
60
61 void GWindowClearFocusGadgetOfWindow(GWindow gw) {
62 GTopLevelD *td;
63 if ( gw==NULL )
64 return;
65 while ( gw->parent!=NULL && !gw->is_toplevel ) gw=gw->parent;
66 td = (GTopLevelD *) (gw->widget_data);
67 if ( gw == current_focus_window && td->gfocus!=NULL &&
68 td->gfocus->funcs->handle_focus!=NULL ) {
69 GEvent e;
70 e.type = et_focus;
71 e.w = gw;
72 e.u.focus.gained_focus = false;
73 e.u.focus.mnemonic_focus = mf_normal;
74 (td->gfocus->funcs->handle_focus)(td->gfocus,&e);
75 }
76 td->gfocus = NULL;
77 }
78
79 GGadget *GWindowGetFocusGadgetOfWindow(GWindow gw) {
80 GTopLevelD *td;
81 if ( gw==NULL )
82 return( NULL );
83 while ( gw->parent!=NULL && !gw->is_toplevel ) gw=gw->parent;
84 td = (GTopLevelD *) (gw->widget_data);
85 return( td->gfocus );
86 }
87
88 int GGadgetActiveGadgetEditCmd(GWindow gw,enum editor_commands cmd) {
89 GGadget *g = GWindowGetFocusGadgetOfWindow(gw);
90
91 if ( g==NULL )
92 return( false );
93 return( GGadgetEditCmd(g,cmd));
94 }
95
96 GWindow GWidgetGetCurrentFocusWindow(void) {
97 GTopLevelD *td;
98 if ( current_focus_window==NULL )
99 return( NULL );
100 td = (GTopLevelD *) (current_focus_window->widget_data);
101 if ( td->gfocus!=NULL )
102 return( td->gfocus->base );
103 return NULL;
104 }
105
106 struct gfuncs *last_indicatedfocus_funcs; /* !!!! Debug code */
107 GGadget *last_indicatedfocus_gadget;
108 struct gwindow *last_indicatedfocus_widget;
109
110 static void _GWidget_IndicateFocusGadget(GGadget *g, enum mnemonic_focus mf) {
111 GWindow top;
112 GTopLevelD *td;
113 GEvent e;
114
115 last_indicatedfocus_funcs = g->funcs;
116 last_indicatedfocus_gadget = g;
117 last_indicatedfocus_widget = g->base;
118
119 if ( g->funcs==NULL ) {
120 fprintf( stderr, "Bad focus attempt\n" );
121 return;
122 }
123
124 // We recurse and find the top-level gadget.
125 for ( top=g->base; top->parent!=NULL && !top->is_toplevel ; top=top->parent );
126 td = (GTopLevelD *) (top->widget_data);
127
128 // We check whether the gadget in question has focus.
129 if ( td->gfocus!=g ) {
130 // If not, we try to deal with the lack of focus.
131 /* Hmm. KDE doesn't give us a focus out event when we make a window invisible */
132 /* So to be on the save side lets send local focus out events even when not */
133 /* strictly needed */
134 if ( /*top == current_focus_window &&*/ td->gfocus!=NULL &&
135 td->gfocus->funcs->handle_focus!=NULL ) {
136 // We use the focus handler provided by the presently focused gadget and process a loss-of-focus event for the currently focused object.
137 memset(&e, 0, sizeof(GEvent));
138 e.type = et_focus;
139 e.w = top;
140 e.u.focus.gained_focus = false;
141 e.u.focus.mnemonic_focus = mf_normal;
142 (td->gfocus->funcs->handle_focus)(td->gfocus,&e);
143 }
144 }
145 // We give focus to the desired gadget.
146 td->gfocus = g;
147 if ( top == current_focus_window && g->funcs->handle_focus!=NULL ) {
148 // If the desired gadget has a focus handler, we construct an event and run it.
149 memset(&e, 0, sizeof(GEvent));
150 e.u.focus.gained_focus = true;
151 e.u.focus.mnemonic_focus = mf;
152 (g->funcs->handle_focus)(g,&e);
153 }
154 }
155
156 void GWidgetIndicateFocusGadget(GGadget *g) {
157 _GWidget_IndicateFocusGadget(g,mf_normal);
158 }
159
160 static GGadget *_GWidget_FindPost(GContainerD *cd,GGadget *oldfocus,GGadget **last) {
161 GGadget *g;
162 GWidgetD *w;
163
164 if ( cd==NULL || !cd->iscontainer )
165 return( false );
166 for ( g=cd->gadgets; g!=NULL; g=g->prev ) {
167 if ( g==oldfocus )
168 return( *last );
169 if ( g->focusable && g->state!=gs_invisible && g->state!=gs_disabled )
170 *last = g;
171 }
172 for ( w=cd->widgets; w!=NULL; w=w->next ) {
173 if (( g = _GWidget_FindPost((GContainerD *) w,oldfocus,last))!=NULL )
174 return( g );
175 }
176 return( NULL );
177 }
178
179 void GWidgetNextFocus(GWindow top) {
180 GTopLevelD *topd;
181 GGadget *focus, *last=NULL;
182
183 while ( top->parent!=NULL && !top->is_toplevel ) top = top->parent;
184 topd = (GTopLevelD *) (top->widget_data);
185 if ( topd==NULL || topd->gfocus == NULL )
186 return;
187 if ( (focus = _GWidget_FindPost((GContainerD *) topd,topd->gfocus,&last))== NULL ) {
188 /* if we didn't find a Next Gadget, it's either because: */
189 /* 1) the focus gadget is first in the chain, in which case we want */
190 /* the last thing in the chain */
191 /* 2) our data structures are screwed up */
192 _GWidget_FindPost((GContainerD *) topd,NULL,&last);
193 focus = last;
194 }
195 _GWidget_IndicateFocusGadget(focus,mf_tab);
196 }
197
198 static GGadget *_GWidget_FindPrev(GContainerD *cd,GGadget *oldfocus,GGadget **first, int *found) {
199 GGadget *g;
200 GWidgetD *w;
201
202 if ( cd==NULL || !cd->iscontainer )
203 return( false );
204 for ( g=cd->gadgets; g!=NULL; g=g->prev ) {
205 if ( g->focusable && g->state!=gs_invisible && g->state!=gs_disabled ) {
206 if ( *first==NULL )
207 *first = g;
208 if ( *found )
209 return( g );
210 }
211 if ( g==oldfocus )
212 *found = true;
213 }
214 for ( w=cd->widgets; w!=NULL; w=w->next ) {
215 if (( g = _GWidget_FindPrev((GContainerD *) w,oldfocus,first,found))!=NULL )
216 return( g );
217 }
218 return( NULL );
219 }
220
221 void GWidgetPrevFocus(GWindow top) {
222 GTopLevelD *topd;
223 GGadget *focus;
224
225 while ( top->parent!=NULL && !top->is_toplevel ) top = top->parent;
226 topd = (GTopLevelD *) (top->widget_data);
227 if ( topd==NULL || topd->gfocus == NULL )
228 return;
229 for ( focus = topd->gfocus->prev;
230 focus!=NULL && (!focus->focusable || focus->state==gs_invisible || focus->state==gs_disabled);
231 focus = focus->prev );
232 if ( focus==NULL ) {
233 GGadget *first = NULL; int found = false;
234 if ( (focus = _GWidget_FindPrev((GContainerD *) topd,topd->gfocus,&first,&found))== NULL )
235 focus = first;
236 }
237 _GWidget_IndicateFocusGadget(focus,mf_tab);
238 }
239
240 static int _GWidget_Container_eh(GWindow gw, GEvent *event) {
241 /* Gadgets can get mouse, char and expose events */
242 /* Widgets might need char events redirected to them */
243 /* If a gadget doesn't want an event it returns false from its eh */
244 /* If a subwidget doesn't want an event it may send it up to us */
245 int handled = false;
246 GGadget *gadget;
247 GContainerD *gd = (GContainerD *) (gw->widget_data);
248 GWindow pixmap;
249 GWindow parent;
250 GTopLevelD *topd;
251
252 if ( gd==NULL ) /* dying */
253 return(true);
254
255 for ( parent = gw; parent->parent!=NULL && parent->parent->widget_data!=NULL &&
256 !parent->is_toplevel; parent = parent->parent );
257 topd = (GTopLevelD *) (parent->widget_data);
258 if ( topd==NULL )
259 fprintf( stderr, "No top level window found\n" );
260
261 GGadgetPopupExternalEvent(event);
262 if ( event->type == et_expose ) {
263 GRect old;
264
265 pixmap = _GWidget_GetPixmap(gw,&event->u.expose.rect);
266 pixmap->ggc->bg = gw->ggc->bg;
267 GDrawPushClip(pixmap,&event->u.expose.rect,&old);
268 pixmap->user_data = gw->user_data;
269
270 if ( gd->e_h!=NULL ) {
271 (gd->e_h)(pixmap,event);
272 }
273 for ( gadget = gd->gadgets; gadget!=NULL ; gadget=gadget->prev )
274 if ( ! ( gadget->r.x>event->u.expose.rect.x+event->u.expose.rect.width ||
275 gadget->r.y>event->u.expose.rect.y+event->u.expose.rect.height ||
276 gadget->r.x+gadget->r.width<event->u.expose.rect.x ||
277 gadget->r.y+gadget->r.height<event->u.expose.rect.y ) &&
278 gadget->state!=gs_invisible )
279 (gadget->funcs->handle_expose)(pixmap,gadget,event);
280
281 GDrawPopClip(pixmap,&old);
282 _GWidget_RestorePixmap(gw,pixmap,&event->u.expose.rect);
283
284 return( true );
285 } else if ( event->type >= et_mousemove && event->type <= et_crossing ) {
286 if ( topd!=NULL && topd->popupowner!=NULL ) {
287 handled = (topd->popupowner->funcs->handle_mouse)(topd->popupowner,event);
288 return( handled );
289 }
290 if ( gd->grabgadget && event->type==et_mousedown &&
291 (event->u.mouse.state&ksm_buttons)==0 )
292 gd->grabgadget = NULL; /* Happens if a gadget invokes a popup menu. Gadget remains grabbed because menu gets the mouse up */
293 if ( gd->grabgadget!=NULL ) {
294 handled = (gd->grabgadget->funcs->handle_mouse)(gd->grabgadget,event);
295 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
296 gw->is_dying || gw->widget_data==NULL )
297 return( true );
298 if ( event->type==et_mouseup )
299 gd->grabgadget = NULL;
300 } else if ( event->type==et_mousedown ) {
301 if ( !parent->is_popup )
302 last_input_window = parent;
303 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
304 if ( gadget->state!=gs_disabled && gadget->state!=gs_invisible &&
305 gadget->takes_input &&
306 GGadgetWithin(gadget,event->u.mouse.x,event->u.mouse.y)) {
307 handled = (gadget->funcs->handle_mouse)(gadget,event);
308 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
309 gw->is_dying || gw->widget_data==NULL )
310 return( true );
311 gd->grabgadget = gadget;
312 if ( gadget->focusable && handled )
313 GWidgetIndicateFocusGadget(gadget);
314 }
315 }
316 } else {
317 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
318 if ( gadget->state!=gs_disabled && gadget->state!=gs_invisible &&
319 /*gadget->takes_input &&*/ /* everybody needs mouse moves for popups, even labels */
320 GGadgetWithin(gadget,event->u.mouse.x,event->u.mouse.y)) {
321 if ( gd->lastwiggle!=NULL && gd->lastwiggle!=gadget )
322 (gd->lastwiggle->funcs->handle_mouse)(gd->lastwiggle,event);
323 handled = (gadget->funcs->handle_mouse)(gadget,event);
324 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
325 gw->is_dying || gw->widget_data==NULL )
326 return( true );
327 gd->lastwiggle = gadget;
328 }
329 }
330 if ( !handled && gd->lastwiggle!=NULL ) {
331 (gd->lastwiggle->funcs->handle_mouse)(gd->lastwiggle,event);
332 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
333 gw->is_dying )
334 return( true );
335 }
336 }
337 } else if ( event->type == et_char || event->type == et_charup ) {
338 if ( topd!=NULL ) {
339 handled = (topd->handle_key)(parent,gw,event);
340 }
341 } else if ( event->type == et_drag || event->type == et_dragout || event->type==et_drop ) {
342 GGadget *lastdd = NULL;
343 GEvent e;
344 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
345 if ( gadget->funcs->handle_sel &&
346 GGadgetWithin(gadget,event->u.drag_drop.x,event->u.drag_drop.y))
347 if (( handled = (gadget->funcs->handle_sel)(gadget,event) )) {
348 lastdd = gadget;
349 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
350 gw->is_dying )
351 return( true );
352 }
353 }
354 if ( !handled && gd->e_h!=NULL ) {
355 handled = (gd->e_h)(gw,event);
356 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
357 gw->is_dying )
358 return( true );
359 lastdd = (GGadget *) -1;
360 }
361 e.type = et_dragout;
362 e.w = gw;
363 if ( lastdd!=gd->lastddgadget ) {
364 if ( gd->lastddgadget==(GGadget *) -1 )
365 (gd->e_h)(gw,&e);
366 else if ( gd->lastddgadget!=NULL )
367 (gd->lastddgadget->funcs->handle_sel)(gd->lastddgadget,&e);
368 if ( !GDrawNativeWindowExists(NULL,event->native_window) ||
369 gw->is_dying )
370 return( true );
371 }
372 if ( event->type==et_drag )
373 gd->lastddgadget = lastdd;
374 else
375 gd->lastddgadget = NULL;
376 return( handled );
377 } else if ( event->type == et_selclear ) {
378 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
379 if ( gadget->funcs->handle_sel ) {
380 if ( (handled = (gadget->funcs->handle_sel)(gadget,event)) )
381 break;
382 }
383 }
384 } else if ( event->type == et_timer ) {
385 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
386 if ( gadget->funcs->handle_timer ) {
387 if ( (handled = (gadget->funcs->handle_timer)(gadget,event)) )
388 break;
389 }
390 }
391 } else if ( event->type == et_resize ) {
392 GWidgetFlowGadgets(gw);
393 }
394 if ( gd->e_h!=NULL && (!handled || event->type==et_mousemove ))
395 handled = (gd->e_h)(gw,event);
396 if ( event->type == et_destroy ) {
397 GGadget *prev;
398 for ( gadget = gd->gadgets; gadget!=NULL ; gadget=prev ) {
399 prev = gadget->prev;
400 if ( !gadget->contained )
401 (gadget->funcs->destroy)(gadget);
402 /* contained ggadgets will be destroyed when their owner is destroyed */
403 }
404 /* Widgets are windows and should get their own destroy events and free themselves */
405 /* remove us from our parent */
406 parent = gw->parent;
407 if ( parent!=NULL && parent->widget_data!=NULL ) {
408 GContainerD *pgd = (GContainerD *) (parent->widget_data);
409 struct gwidgetdata *wd, *pwd;
410 for ( pwd=NULL, wd=pgd->widgets; wd!=NULL && wd!=(struct gwidgetdata *) gd; pwd = wd, wd = wd->next );
411 if ( pwd==NULL )
412 pgd->widgets = gd->next;
413 else
414 pwd->next = gd->next;
415 }
416 free(gd);
417 gw->widget_data = NULL;
418 }
419 return( handled );
420 }
421
422 static int GWidgetCheckMn(GContainerD *gd,GEvent *event) {
423 int handled = false;
424 GGadget *gadget, *last;
425 struct gwidgetdata *widget;
426 int mask = GMenuMask() & (ksm_control|ksm_cmdmacosx);
427
428 for ( gadget = gd->gadgets; gadget!=NULL && !handled ; gadget=gadget->prev ) {
429 if ( (event->u.chr.state&ksm_meta) && !(event->u.chr.state&mask) &&
430 gadget->state != gs_invisible && gadget->state != gs_disabled &&
431 GDrawShortcutKeyMatches(event, gadget->mnemonic) ) {
432 if ( gadget->focusable ) { /* labels may have a mnemonic */
433 /* (ie. because textfields can't display mnemonics) */
434 /* but they don't act on it */
435 _GWidget_IndicateFocusGadget(gadget,mf_mnemonic);
436 handled = true;
437 } else if ( last!=NULL && last->mnemonic=='\0' ) {
438 /* So if we get a label with a mnemonic, and the next gadget */
439 /* is focusable and doesn't have a mnemoic itself, give it */
440 /* the label's focus */
441 _GWidget_IndicateFocusGadget(last,mf_mnemonic);
442 handled = true;
443 }
444 } else if ( (gadget->short_mask&event->u.chr.state)==gadget->short_mask &&
445 GDrawShortcutKeyMatches(event, gadget->shortcut)) {
446 _GWidget_IndicateFocusGadget(gadget,mf_shortcut);
447 handled = true;
448 } else if ( gadget->state != gs_invisible &&
449 gadget->state != gs_disabled &&
450 gadget->focusable ) {
451 last = gadget;
452 }
453 }
454 for ( widget = gd->widgets; widget!=NULL && !handled ; widget=widget->next ) {
455 if ( widget->iscontainer )
456 handled = GWidgetCheckMn((GContainerD *) widget,event);
457 }
458 return( handled );
459 }
460
461 static int _GWidget_TopLevel_Key(GWindow top, GWindow ew, GEvent *event) {
462 GTopLevelD *topd = (GTopLevelD *) (top->widget_data);
463 int handled=0;
464 GEvent sub;
465
466 if ( !top->is_popup )
467 last_input_window = top;
468
469 /* Check for mnemonics and shortcuts */
470 if ( event->type == et_char && !GKeysymIsModifier(event->u.chr.keysym) ) {
471 handled = GMenuPopupCheckKey(event);
472 if ( !handled )
473 if ( !(handled = GMenuBarCheckKey(top,topd->gmenubar,event)) )
474 handled = GWidgetCheckMn((GContainerD *) topd,event);
475 }
476 if ( handled )
477 /* No op */;
478 else if ( topd->popupowner!=NULL ) {
479 /* When we've got an active popup (menu, list, etc.) all key events */
480 /* go to the popups owner (which, presumably sends them to the popup)*/
481 if ( topd->popupowner->funcs->handle_key !=NULL )
482 handled = (topd->popupowner->funcs->handle_key)(topd->popupowner,event);
483 } else if ( topd->gfocus!=NULL && topd->gfocus->funcs->handle_key )
484 handled = (topd->gfocus->funcs->handle_key)(topd->gfocus,event);
485 if ( !handled ) {
486 if ( ew->widget_data==NULL ) {
487 if ( ew->eh!=NULL )
488 handled = (ew->eh)(ew,event);
489 } else if ( ew->widget_data->e_h!=NULL )
490 handled = (ew->widget_data->e_h)(ew,event);
491 }
492
493 if ( event->type==et_charup )
494 return( handled );
495 /* If no one wanted it then try keyboard navigation */
496 /* Tab or back tab cycles the focus through our widgets/gadgets */
497 if ( !handled && (event->u.chr.keysym==GK_Tab || event->u.chr.keysym==GK_BackTab )) {
498 if ( event->u.chr.keysym==GK_BackTab || (event->u.chr.state&ksm_shift) )
499 GWidgetPrevFocus(ew);
500 else
501 GWidgetNextFocus(ew);
502 handled = true;
503 }
504 /* Return activates the default button (if there is one) */
505 else if ( !handled && (event->u.chr.keysym==GK_Return || event->u.chr.keysym==GK_KP_Enter) &&
506 topd->gdef!=NULL ) {
507 sub.type = et_controlevent;
508 sub.w = topd->gdef->base;
509 sub.u.control.subtype = et_buttonactivate;
510 sub.u.control.g = topd->gdef;
511 sub.u.control.u.button.clicks = 0;
512 if ( topd->gdef->handle_controlevent != NULL )
513 (topd->gdef->handle_controlevent)(topd->gdef,&sub);
514 else
515 GDrawPostEvent(&sub);
516 }
517 /* Escape activates the cancel button (if there is one) */
518 /* (On the mac, Command-. has that meaning) */
519 else if ( !handled && topd->gcancel!=NULL &&
520 (event->u.chr.keysym==GK_Escape ||
521 ((GMenuMask()&ksm_cmdmacosx) &&
522 (event->u.chr.state&GMenuMask())==ksm_cmdmacosx &&
523 event->u.chr.keysym=='.'))) {
524 sub.type = et_controlevent;
525 sub.w = topd->gcancel->base;
526 sub.u.control.subtype = et_buttonactivate;
527 sub.u.control.g = topd->gcancel;
528 sub.u.control.u.button.clicks = 0;
529 if ( topd->gcancel->handle_controlevent != NULL )
530 (topd->gcancel->handle_controlevent)(topd->gcancel,&sub);
531 else
532 GDrawPostEvent(&sub);
533 }
534 return( handled );
535 }
536
537 static int GiveToAll(GContainerD *wd, GEvent *event) {
538 GGadget *g;
539 GContainerD *sub;
540
541 if ( wd!=NULL && wd->iscontainer ) {
542 for ( g=wd->gadgets; g!=NULL; g=g->prev )
543 if ( g->funcs->handle_mouse!=NULL )
544 (g->funcs->handle_mouse)(g,event);
545 for ( sub = (GContainerD *) (wd->widgets); sub!=NULL;
546 sub=(GContainerD *) (sub->next) )
547 GiveToAll(sub,event);
548 }
549 if ( wd!=NULL ) {
550 if ( wd->e_h!=NULL )
551 (wd->e_h)(wd->w,event);
552 } else {
553 if ( wd->w->eh!=NULL )
554 (wd->w->eh)(wd->w,event);
555 }
556 return( true );
557 }
558
559 static GTopLevelD *oldtd = NULL;
560 static GGadget *oldgfocus = NULL;
561
562 static int _GWidget_TopLevel_eh(GWindow gw, GEvent *event) {
563 GTopLevelD *td;
564 int ret;
565
566 if ( !GDrawNativeWindowExists(NULL,event->native_window) )
567 return( true );
568
569 td = (GTopLevelD *) (gw->widget_data);
570 if ( td==NULL ) /* Dying */
571 return( true );
572
573 GGadgetPopupExternalEvent(event);
574 if ( event->type==et_focus ) {
575
576 if ( event->u.focus.gained_focus ) {
577 if ( gw->is_toplevel && !gw->is_popup && !gw->is_dying ) {
578 if ( last_input_window!=gw )
579 previous_focus_window = last_input_window;
580 current_focus_window = gw;
581 }
582 } else if ( current_focus_window==gw ) {
583 current_focus_window = NULL;
584 }
585 if ( !gw->is_dying && td->gfocus!=NULL && td->gfocus->funcs->handle_focus!=NULL ) {
586 { oldtd = td; oldgfocus = td->gfocus; } /* Debug!!!! */
587 (td->gfocus->funcs->handle_focus)(td->gfocus,event);
588 }
589 if ( !gw->is_dying && td->e_h!=NULL )
590 (td->e_h)(gw,event);
591 return( true );
592 } else if ( !gw->is_dying && event->type == et_crossing ) {
593 GiveToAll((GContainerD *) td,event);
594 return( true );
595 } else if ( event->type == et_char || event->type == et_charup ) {
596 // If this is not a nested event, redirect it at the window that
597 // contains the currently focused gadget.
598 if (!td->isnestedkey && td->gfocus && td->gfocus->base) {
599 td->isnestedkey = true;
600 ret = _GWidget_TopLevel_Key(gw, td->gfocus->base, event);
601 td->isnestedkey = false;
602 } else {
603 ret = _GWidget_TopLevel_Key(gw, gw, event);
604 }
605 return ret;
606 } else if ( !gw->is_dying && event->type == et_resize ) {
607 GRect r;
608 if ( td->gmenubar!=NULL ) {
609 GGadgetGetSize(td->gmenubar,&r);
610 GGadgetResize(td->gmenubar,event->u.resize.size.width,r.height);
611 GGadgetRedraw(td->gmenubar);
612 } /* status line, toolbar, etc. */
613 }
614 ret = _GWidget_Container_eh(gw,event);
615 if ( event->type == et_destroy ) {
616 if ( gw==current_focus_window )
617 current_focus_window = NULL;
618 if ( gw==previous_focus_window )
619 previous_focus_window = NULL;
620 if ( gw==last_input_window )
621 last_input_window = NULL;
622 ret = true;
623 }
624 return( ret );
625 }
626
627 static struct wfuncs _gwidget_container_funcs;
628 static struct wfuncs _gwidget_toplevel_funcs;
629
630 static void MakeContainerWidget(GWindow gw) {
631 struct gwidgetcontainerdata *gd;
632
633 if ( gw->widget_data!=NULL )
634 GDrawIError( "Attempt to make a window into a widget twice");
635 if ( gw->parent==NULL || gw->is_toplevel )
636 gd = calloc(1,sizeof(struct gtopleveldata));
637 else
638 gd = calloc(1,sizeof(struct gwidgetcontainerdata));
639 gw->widget_data = (struct gwidgetdata *) gd;
640 gd->w = gw;
641 gd->e_h = gw->eh;
642 gw->eh = _GWidget_Container_eh;
643 gd->enabled = true;
644 gd->iscontainer = true;
645 gd->funcs = &_gwidget_container_funcs;
646 if ( gw->parent!=NULL && !gw->is_toplevel ) {
647 if ( gw->parent->widget_data==NULL )
648 MakeContainerWidget(gw->parent);
649 if ( !gw->parent->widget_data->iscontainer )
650 GDrawIError( "Attempt to add a widget to something which is not a container");
651 gd->next = ((struct gwidgetcontainerdata *) (gw->parent->widget_data))->widgets;
652 ((struct gwidgetcontainerdata *) (gw->parent->widget_data))->widgets =
653 (struct gwidgetdata *) gd;
654 } else {
655 struct gtopleveldata *topd;
656 topd = (struct gtopleveldata *) gd;
657 gd->funcs = &_gwidget_toplevel_funcs;
658 gw->eh = _GWidget_TopLevel_eh;
659 topd->handle_key = _GWidget_TopLevel_Key;
660 topd->istoplevel = true;
661 }
662 }
663
664 void _GWidget_AddGGadget(GWindow gw,GGadget *g) {
665 struct gwidgetcontainerdata *gd;
666
667 if ( gw->widget_data==NULL )
668 MakeContainerWidget(gw);
669 gd = (struct gwidgetcontainerdata *) (gw->widget_data);
670 if ( !gd->iscontainer )
671 GDrawIError( "Attempt to add a gadget to something which is not a container");
672 g->prev = gd->gadgets;
673 gd->gadgets = g;
674 if ( g->base!=NULL )
675 GDrawIError( "Attempt to add a gadget to two widgets" );
676 g->base = gw;
677 }
678
679 void _GWidget_RemoveGadget(GGadget *g) {
680 struct gwidgetcontainerdata *gd;
681 GTopLevelD *td;
682 GWindow gw = g->base;
683 GGadget *next;
684
685 if ( gw==NULL )
686 return;
687
688 gd = (struct gwidgetcontainerdata *) (gw->widget_data);
689 if ( gd==NULL || !gd->iscontainer )
690 GDrawIError( "Attempt to remove a gadget to something which is not a container");
691 if ( gd->gadgets==g )
692 gd->gadgets = g->prev;
693 else {
694 for ( next = gd->gadgets; next!=NULL && next->prev!=g; next = next->prev );
695 if ( next==NULL )
696 GDrawIError( "Attempt to remove a gadget which is not in the gadget list" );
697 else
698 next->prev = g->prev;
699 }
700 if ( gd->grabgadget == g ) gd->grabgadget = NULL;
701 g->prev = NULL;
702 g->base = NULL;
703
704 while ( gw->parent!=NULL && !gw->is_toplevel ) gw = gw->parent;
705 td = (GTopLevelD *) (gw->widget_data);
706 if ( td->gdef == g ) td->gdef = NULL;
707 if ( td->gcancel == g ) td->gcancel = NULL;
708 if ( td->gfocus == g ) td->gfocus = NULL;
709 }
710
711 void _GWidget_SetDefaultButton(GGadget *g) {
712 struct gtopleveldata *gd=NULL;
713 GWindow gw = g->base;
714
715 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
716 if ( gw!=NULL )
717 gd = (struct gtopleveldata *) (gw->widget_data);
718 if ( gd==NULL || !gd->istoplevel )
719 GDrawIError( "This gadget isn't in a top level widget, can't be a default button" );
720 else
721 gd->gdef = g;
722 }
723
724 /* The previous function assumes everything is set up properly. This one doesn't */
725 /* it's for when you've got two buttons which might be default and you toggle */
726 /* between them. It lets each button know if it's default, and then redraws */
727 /* both */
728 void _GWidget_MakeDefaultButton(GGadget *g) {
729 struct gtopleveldata *gd=NULL;
730 GWindow gw = g->base;
731
732 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
733 if ( gw!=NULL )
734 gd = (struct gtopleveldata *) (gw->widget_data);
735 if ( gd==NULL || !gd->istoplevel )
736 GDrawIError( "This gadget isn't in a top level widget, can't be a default button" );
737 else if ( gd->gdef!=g ) {
738 _GButton_SetDefault(gd->gdef,false);
739 gd->gdef = g;
740 _GButton_SetDefault(g,true);
741 }
742 }
743
744 void _GWidget_SetCancelButton(GGadget *g) {
745 struct gtopleveldata *gd=NULL;
746 GWindow gw = g->base;
747
748 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
749 if ( gw!=NULL )
750 gd = (struct gtopleveldata *) (gw->widget_data);
751 if ( gd==NULL || !gd->istoplevel )
752 GDrawIError( "This gadget isn't in a top level widget, can't be a cancel button" );
753 else
754 gd->gcancel = g;
755 }
756
757 void _GWidget_SetMenuBar(GGadget *g) {
758 struct gtopleveldata *gd=NULL;
759 GWindow gw = g->base;
760
761 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
762 if ( gw!=NULL )
763 gd = (struct gtopleveldata *) (gw->widget_data);
764 if ( gd==NULL || !gd->istoplevel )
765 GDrawIError( "This gadget isn't in a top level widget, can't be a menubar" );
766 else
767 gd->gmenubar = g;
768 }
769
770 void _GWidget_SetPopupOwner(GGadget *g) {
771 struct gtopleveldata *gd=NULL;
772 GWindow gw = g->base;
773
774 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
775 if ( gw!=NULL )
776 gd = (struct gtopleveldata *) (gw->widget_data);
777 if ( gd==NULL || !gd->istoplevel )
778 GDrawIError( "This gadget isn't in a top level widget, can't have a popup" );
779 else
780 gd->popupowner = g;
781 }
782
783 void _GWidget_ClearPopupOwner(GGadget *g) {
784 struct gtopleveldata *gd=NULL;
785 GWindow gw = g->base;
786
787 if ( gw!=NULL ) while ( gw->parent != NULL && !gw->is_toplevel ) gw = gw->parent;
788 if ( gw!=NULL )
789 gd = (struct gtopleveldata *) (gw->widget_data);
790 if ( gd==NULL || !gd->istoplevel )
791 GDrawIError( "This gadget isn't in a top level widget, can't have a popup" );
792 else
793 gd->popupowner = NULL;
794 }
795
796 void _GWidget_SetGrabGadget(GGadget *g) {
797 struct gwidgetcontainerdata *gd=NULL;
798 GWindow gw = g->base;
799
800 if ( gw!=NULL )
801 gd = (struct gwidgetcontainerdata *) (gw->widget_data);
802 if ( gd==NULL || !gd->iscontainer )
803 GDrawIError( "This gadget isn't in a container, can't be a grab gadget" );
804 else
805 gd->grabgadget = g;
806 }
807
808 void _GWidget_ClearGrabGadget(GGadget *g) {
809 struct gwidgetcontainerdata *gd=NULL;
810 GWindow gw = g->base;
811
812 if ( gw!=NULL )
813 gd = (struct gwidgetcontainerdata *) (gw->widget_data);
814 if ( gd==NULL || !gd->iscontainer )
815 GDrawIError( "This gadget isn't in a container, can't be a grab gadget" );
816 else
817 gd->grabgadget = NULL;
818 }
819
820 GWindow GWidgetCreateTopWindow(GDisplay *gdisp, GRect *pos, int (*eh)(GWindow,GEvent *), void *user_data, GWindowAttrs *wattrs) {
821 GWindow gw = GDrawCreateTopWindow(gdisp,pos,eh,user_data,wattrs);
822 MakeContainerWidget(gw);
823 return( gw );
824 }
825
826 GWindow GWidgetCreateSubWindow(GWindow w, GRect *pos, int (*eh)(GWindow,GEvent *), void *user_data, GWindowAttrs *wattrs) {
827 GWindow gw = GDrawCreateSubWindow(w,pos,eh,user_data,wattrs);
828 MakeContainerWidget(gw);
829 return( gw );
830 }
831
832 GGadget *GWidgetGetControl(GWindow gw, int cid) {
833 GGadget *gadget;
834 GContainerD *gd = (GContainerD *) (gw->widget_data);
835 GWidgetD *widg;
836
837 if ( gd==NULL || !gd->iscontainer )
838 return( NULL );
839 for ( gadget = gd->gadgets; gadget!=NULL ; gadget=gadget->prev ) {
840 if ( gadget->cid == cid )
841 return( gadget );
842 }
843 for ( widg = gd->widgets; widg!=NULL; widg = widg->next ) {
844 if ( widg->iscontainer ) {
845 gadget = GWidgetGetControl(widg->w,cid);
846 if ( gadget!=NULL )
847 return( gadget );
848 }
849 }
850 return( NULL );
851 }
852
853 GGadget *_GWidgetGetGadgets(GWindow gw) {
854 GContainerD *gd;
855
856 if ( gw==NULL )
857 return( NULL );
858
859 gd = (GContainerD *) (gw->widget_data);
860
861 if ( gd==NULL || !gd->iscontainer )
862 return( NULL );
863
864 return( gd->gadgets );
865 }
866
867 GWindow GWidgetGetParent(GWindow gw) {
868
869 return( gw->parent );
870 }
871
872 GWindow GWidgetGetTopWidget(GWindow gw) {
873
874 for ( ; gw->parent!=NULL && !gw->is_toplevel; gw = gw->parent );
875 return( gw );
876 }
877
878 GDrawEH GWidgetGetEH(GWindow gw) {
879 if ( gw->widget_data==NULL )
880 return( gw->eh );
881 else
882 return( gw->widget_data->e_h );
883 }
884
885 void GWidgetSetEH(GWindow gw, GDrawEH e_h ) {
886 if ( gw->widget_data==NULL )
887 gw->eh = e_h;
888 else
889 gw->widget_data->e_h = e_h;
890 }
891
892 GIC *GWidgetCreateInputContext(GWindow w,enum gic_style def_style) {
893 GWidgetD *wd = (GWidgetD *) (w->widget_data);
894
895 if ( wd->gic==NULL )
896 wd->gic = GDrawCreateInputContext(w,def_style);
897 return( wd->gic );
898 }
899
900 GIC *GWidgetGetInputContext(GWindow w) {
901 GWidgetD *wd = (GWidgetD *) (w->widget_data);
902 return( wd->gic );
903 }
904
905 void GWidgetFlowGadgets(GWindow gw) {
906 GGadget *gadget;
907 GContainerD *gd = (GContainerD *) (gw->widget_data);
908
909 if ( gd==NULL )
910 return;
911
912 gadget = gd->gadgets;
913 if ( gadget!=NULL ) {
914 while ( gadget->prev!=NULL )
915 gadget=gadget->prev;
916 }
917 if ( gadget != NULL && GGadgetFillsWindow(gadget)) {
918 GRect wsize;
919 GDrawGetSize(gw, &wsize);
920
921 /* Make any offset symmetrical */
922 if (wsize.width >= 2*gadget->r.x) wsize.width -= 2*gadget->r.x;
923 else wsize.width = 0;
924
925 if (wsize.height >= 2*gadget->r.y) wsize.height -= 2*gadget->r.y;
926 else wsize.height = 0;
927
928 GGadgetResize(gadget,wsize.width,wsize.height);
929 GDrawRequestExpose(gw,NULL,false);
930 }
931 }
932
933 void GWidgetToDesiredSize(GWindow gw) {
934 GGadget *gadget;
935 GContainerD *gd = (GContainerD *) (gw->widget_data);
936
937 if ( gd==NULL )
938 return;
939
940 gadget = gd->gadgets;
941 if ( gadget!=NULL ) {
942 while ( gadget->prev!=NULL )
943 gadget=gadget->prev;
944 }
945 if ( gadget != NULL && GGadgetFillsWindow(gadget))
946 GHVBoxFitWindow(gadget);
947 }