]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
incremented version number to 1.1.13, bin age 0, interface age 0.
[~andy/gtk] / gtk / gtkmain.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <X11/Xlocale.h>        /* so we get the right setlocale */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <gmodule.h>
24 #include "gtkbutton.h"
25 #include "gtkdnd.h"
26 #include "gtkfeatures.h"
27 #include "gtkhscrollbar.h"
28 #include "gtkhseparator.h"
29 #include "gtkmain.h"
30 #include "gtkpreview.h"
31 #include "gtkrc.h"
32 #include "gtkselection.h"
33 #include "gtksignal.h"
34 #include "gtktable.h"
35 #include "gtktext.h"
36 #include "gtkvbox.h"
37 #include "gtkvscrollbar.h"
38 #include "gtkwidget.h"
39 #include "gtkwindow.h"
40 #include "gtkprivate.h"
41 #include "gdk/gdki18n.h"
42 #include "config.h"
43 #include "gtkdebug.h"
44 #include "gtkintl.h"
45
46 /* Private type definitions
47  */
48 typedef struct _GtkInitFunction          GtkInitFunction;
49 typedef struct _GtkQuitFunction          GtkQuitFunction;
50 typedef struct _GtkClosure               GtkClosure;
51 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
52
53 struct _GtkInitFunction
54 {
55   GtkFunction function;
56   gpointer data;
57 };
58
59 struct _GtkQuitFunction
60 {
61   guint id;
62   guint main_level;
63   GtkCallbackMarshal marshal;
64   GtkFunction function;
65   gpointer data;
66   GtkDestroyNotify destroy;
67 };
68
69 struct _GtkClosure
70 {
71   GtkCallbackMarshal marshal;
72   gpointer data;
73   GtkDestroyNotify destroy;
74 };
75
76 struct _GtkKeySnooperData
77 {
78   GtkKeySnoopFunc func;
79   gpointer func_data;
80   guint id;
81 };
82
83 static void  gtk_exit_func               (void);
84 static gint  gtk_quit_invoke_function    (GtkQuitFunction    *quitf);
85 static void  gtk_quit_destroy            (GtkQuitFunction    *quitf);
86 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
87                                           GdkEvent           *event);
88
89 static void     gtk_destroy_closure      (gpointer            data);
90 static gboolean gtk_invoke_idle_timeout  (gpointer            data);
91 static void     gtk_invoke_input         (gpointer            data,
92                                           gint                source,
93                                           GdkInputCondition   condition);
94
95 #if 0
96 static void  gtk_error                   (gchar              *str);
97 static void  gtk_warning                 (gchar              *str);
98 static void  gtk_message                 (gchar              *str);
99 static void  gtk_print                   (gchar              *str);
100 #endif
101
102 const guint gtk_major_version = GTK_MAJOR_VERSION;
103 const guint gtk_minor_version = GTK_MINOR_VERSION;
104 const guint gtk_micro_version = GTK_MICRO_VERSION;
105 const guint gtk_binary_age = GTK_BINARY_AGE;
106 const guint gtk_interface_age = GTK_INTERFACE_AGE;
107
108 static guint gtk_main_loop_level = 0;
109 static gint gtk_initialized = FALSE;
110 static GList *current_events = NULL;
111
112 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
113
114 static GSList *grabs = NULL;               /* A stack of unique grabs. The grabbing
115                                             *  widget is the first one on the list.
116                                             */
117 static GList *init_functions = NULL;       /* A list of init functions.
118                                             */
119 static GList *quit_functions = NULL;       /* A list of quit functions.
120                                             */
121 static GMemChunk *quit_mem_chunk = NULL;
122
123 static GSList *key_snoopers = NULL;
124
125 static GdkVisual *gtk_visual;              /* The visual to be used in creating new
126                                             *  widgets.
127                                             */
128 static GdkColormap *gtk_colormap;          /* The colormap to be used in creating new
129                                             *  widgets.
130                                             */
131
132 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
133
134 #ifdef G_ENABLE_DEBUG
135 static const GDebugKey gtk_debug_keys[] = {
136   {"objects", GTK_DEBUG_OBJECTS},
137   {"misc", GTK_DEBUG_MISC},
138   {"signals", GTK_DEBUG_SIGNALS},
139   {"dnd", GTK_DEBUG_DND}
140 };
141
142 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
143
144 #endif /* G_ENABLE_DEBUG */
145
146 gchar*
147 gtk_check_version (guint required_major,
148                    guint required_minor,
149                    guint required_micro)
150 {
151   if (required_major > GTK_MAJOR_VERSION)
152     return "Gtk+ version to old (major mismatch)";
153   if (required_major < GTK_MAJOR_VERSION)
154     return "Gtk+ version to new (major mismatch)";
155   if (required_minor > GTK_MINOR_VERSION)
156     return "Gtk+ version to old (minor mismatch)";
157   if (required_minor < GTK_MINOR_VERSION)
158     return "Gtk+ version to new (minor mismatch)";
159   if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
160     return "Gtk+ version to new (micro mismatch)";
161   if (required_micro > GTK_MICRO_VERSION)
162     return "Gtk+ version to old (micro mismatch)";
163   return NULL;
164 }
165
166
167 void
168 gtk_init (int    *argc,
169           char ***argv)
170 {
171   GSList *gtk_modules = NULL;
172   GSList *slist;
173   gchar *env_string = NULL;
174
175   if (gtk_initialized)
176     return;
177
178   /* There is some argument for putting this in a separate
179    * function ... but I don't think that it is much
180    * of a restriction to require that GTK+ be used
181    * single threaded until gtk_init().
182    */
183
184 #if     0
185   g_set_error_handler (gtk_error);
186   g_set_warning_handler (gtk_warning);
187   g_set_message_handler (gtk_message);
188   g_set_print_handler (gtk_print);
189 #endif
190   
191   /* Initialize "gdk". We pass along the 'argc' and 'argv'
192    *  parameters as they contain information that GDK uses
193    */
194   gdk_init (argc, argv);
195
196   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
197   
198 #ifdef G_ENABLE_DEBUG
199   env_string = getenv ("GTK_DEBUG");
200   if (env_string != NULL)
201     {
202       gtk_debug_flags = g_parse_debug_string (env_string,
203                                               gtk_debug_keys,
204                                               gtk_ndebug_keys);
205       env_string = NULL;
206     }
207 #endif  /* G_ENABLE_DEBUG */
208
209   env_string = getenv ("GTK_MODULES");
210   if (env_string)
211     {
212       gchar **modules, **as;
213
214       modules = g_strsplit (env_string, ":", -1);
215       for (as = modules; *as; as++)
216         {
217           if (**as)
218             gtk_modules = g_slist_prepend (gtk_modules, *as);
219           else
220             g_free (*as);
221         }
222       g_free (modules);
223       env_string = NULL;
224     }
225
226   if (argc && argv)
227     {
228       gint i, j, k;
229       
230       for (i = 1; i < *argc;)
231         {
232           if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
233               strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
234             {
235               gchar *module_name = (*argv)[i] + 12;
236               
237               if (*module_name == '=')
238                 module_name++;
239               else
240                 {
241                   (*argv)[i] = NULL;
242                   i += 1;
243                   module_name = (*argv)[i];
244                 }
245               (*argv)[i] = NULL;
246
247               gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
248             }
249           else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
250             {
251               GLogLevelFlags fatal_mask;
252               
253               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
254               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
255               g_log_set_always_fatal (fatal_mask);
256               (*argv)[i] = NULL;
257             }
258 #ifdef G_ENABLE_DEBUG
259           else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
260                    (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
261             {
262               gchar *equal_pos = strchr ((*argv)[i], '=');
263               
264               if (equal_pos != NULL)
265                 {
266                   gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
267                                                            gtk_debug_keys,
268                                                            gtk_ndebug_keys);
269                 }
270               else if ((i + 1) < *argc && (*argv)[i + 1])
271                 {
272                   gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
273                                                            gtk_debug_keys,
274                                                            gtk_ndebug_keys);
275                   (*argv)[i] = NULL;
276                   i += 1;
277                 }
278               (*argv)[i] = NULL;
279             }
280           else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
281                    (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
282             {
283               gchar *equal_pos = strchr ((*argv)[i], '=');
284               
285               if (equal_pos != NULL)
286                 {
287                   gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
288                                                             gtk_debug_keys,
289                                                             gtk_ndebug_keys);
290                 }
291               else if ((i + 1) < *argc && (*argv)[i + 1])
292                 {
293                   gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
294                                                             gtk_debug_keys,
295                                                             gtk_ndebug_keys);
296                   (*argv)[i] = NULL;
297                   i += 1;
298                 }
299               (*argv)[i] = NULL;
300             }
301 #endif /* G_ENABLE_DEBUG */
302           i += 1;
303         }
304       
305       for (i = 1; i < *argc; i++)
306         {
307           for (k = i; k < *argc; k++)
308             if ((*argv)[k] != NULL)
309               break;
310           
311           if (k > i)
312             {
313               k -= i;
314               for (j = i + k; j < *argc; j++)
315                 (*argv)[j-k] = (*argv)[j];
316               *argc -= k;
317             }
318         }
319     }
320   
321   /* load gtk modules */
322   gtk_modules = g_slist_reverse (gtk_modules);
323   for (slist = gtk_modules; slist; slist = slist->next)
324     {
325       gchar *module_name;
326       GModule *module = NULL;
327       GtkModuleInitFunc modinit_func = NULL;
328       
329       module_name = slist->data;
330       slist->data = NULL;
331       if (!(module_name[0] == '/' ||
332             (module_name[0] == 'l' &&
333              module_name[1] == 'i' &&
334              module_name[2] == 'b')))
335         {
336           gchar *old = module_name;
337           
338           module_name = g_strconcat ("lib", module_name, ".so", NULL);
339           g_free (old);
340         }
341       if (g_module_supported ())
342         {
343           module = g_module_open (module_name, G_MODULE_BIND_LAZY);
344           if (module &&
345               g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
346               modinit_func)
347             {
348               if (!g_slist_find (gtk_modules, modinit_func))
349                 {
350                   g_module_make_resident (module);
351                   slist->data = modinit_func;
352                 }
353               else
354                 {
355                   g_module_close (module);
356                   module = NULL;
357                 }
358             }
359         }
360       if (!modinit_func)
361         {
362           g_warning ("Failed to load module \"%s\": %s",
363                      module ? g_module_name (module) : module_name,
364                      g_module_error ());
365           if (module)
366             g_module_close (module);
367         }
368       g_free (module_name);
369     }
370
371 #ifdef ENABLE_NLS
372   bindtextdomain("gtk+",GTK_LOCALEDIR);
373 #endif  
374
375   /* Initialize the default visual and colormap to be
376    *  used in creating widgets. (We want to use the system
377    *  defaults so as to be nice to the colormap).
378    */
379   gtk_visual = gdk_visual_get_system ();
380   gtk_colormap = gdk_colormap_get_system ();
381
382   gtk_type_init ();
383   gtk_signal_init ();
384   gtk_rc_init ();
385   
386   
387   /* Register an exit function to make sure we are able to cleanup.
388    */
389   g_atexit (gtk_exit_func);
390   
391   /* Set the 'initialized' flag.
392    */
393   gtk_initialized = TRUE;
394
395   /* initialize gtk modules
396    */
397   for (slist = gtk_modules; slist; slist = slist->next)
398     {
399       if (slist->data)
400         {
401           GtkModuleInitFunc modinit;
402           
403           modinit = slist->data;
404           modinit (argc, argv);
405         }
406     }
407   g_slist_free (gtk_modules);
408 }
409
410 void
411 gtk_exit (int errorcode)
412 {
413   /* Only if "gtk" has been initialized should we de-initialize.
414    */
415   /* de-initialisation is done by the gtk_exit_funct(),
416    * no need to do this here (Alex J.)
417    */
418   gdk_exit(errorcode);
419 }
420
421 gchar*
422 gtk_set_locale (void)
423 {
424   return gdk_set_locale ();
425 }
426
427 void
428 gtk_main (void)
429 {
430   GList *tmp_list;
431   GList *functions;
432   GtkInitFunction *init;
433   GMainLoop *loop;
434
435   gtk_main_loop_level++;
436   
437   loop = g_main_new (TRUE);
438   main_loops = g_slist_prepend (main_loops, loop);
439
440   tmp_list = functions = init_functions;
441   init_functions = NULL;
442   
443   while (tmp_list)
444     {
445       init = tmp_list->data;
446       tmp_list = tmp_list->next;
447       
448       (* init->function) (init->data);
449       g_free (init);
450     }
451   g_list_free (functions);
452
453   if (g_main_is_running (main_loops->data))
454     {
455       GDK_THREADS_LEAVE ();
456       g_main_run (loop);
457       GDK_THREADS_ENTER ();
458       gdk_flush ();
459     }
460
461   if (quit_functions)
462     {
463       GList *reinvoke_list = NULL;
464       GtkQuitFunction *quitf;
465
466       while (quit_functions)
467         {
468           quitf = quit_functions->data;
469
470           quit_functions = g_list_remove_link (quit_functions, quit_functions);
471
472           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
473               gtk_quit_invoke_function (quitf))
474             {
475               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
476             }
477           else
478             {
479               g_list_free (tmp_list);
480               gtk_quit_destroy (quitf);
481             }
482         }
483       if (reinvoke_list)
484         {
485           GList *work;
486           
487           work = g_list_last (reinvoke_list);
488           if (quit_functions)
489             quit_functions->prev = work;
490           work->next = quit_functions;
491           quit_functions = work;
492         }
493
494       gdk_flush ();
495     }
496               
497   main_loops = g_slist_remove (main_loops, loop);
498
499   g_main_destroy (loop);
500
501   gtk_main_loop_level--;
502 }
503
504 guint
505 gtk_main_level (void)
506 {
507   return gtk_main_loop_level;
508 }
509
510 void
511 gtk_main_quit (void)
512 {
513   g_return_if_fail (main_loops != NULL);
514
515   g_main_quit (main_loops->data);
516 }
517
518 gint
519 gtk_events_pending (void)
520 {
521   return g_main_pending();
522 }
523
524 gint 
525 gtk_main_iteration (void)
526 {
527   g_main_iteration (TRUE);
528
529   if (main_loops)
530     return !g_main_is_running (main_loops->data);
531   else
532     return TRUE;
533 }
534
535 gint 
536 gtk_main_iteration_do (gboolean blocking)
537 {
538   g_main_iteration (blocking);
539
540   if (main_loops)
541     return !g_main_is_running (main_loops->data);
542   else
543     return TRUE;
544 }
545
546 void 
547 gtk_main_do_event (GdkEvent *event)
548 {
549   GtkWidget *event_widget;
550   GtkWidget *grab_widget;
551   GdkEvent *next_event;
552   GList *tmp_list;
553
554   /* If there are any events pending then get the next one.
555    */
556   next_event = gdk_event_peek ();
557   
558   /* Try to compress enter/leave notify events. These event
559    *  pairs occur when the mouse is dragged quickly across
560    *  a window with many buttons (or through a menu). Instead
561    *  of highlighting and de-highlighting each widget that
562    *  is crossed it is better to simply de-highlight the widget
563    *  which contained the mouse initially and highlight the
564    *  widget which ends up containing the mouse.
565    */
566   if (next_event)
567     if (((event->type == GDK_ENTER_NOTIFY) ||
568          (event->type == GDK_LEAVE_NOTIFY)) &&
569         ((next_event->type == GDK_ENTER_NOTIFY) ||
570          (next_event->type == GDK_LEAVE_NOTIFY)) &&
571         (next_event->type != event->type) &&
572         (next_event->any.window == event->any.window))
573       {
574         /* Throw both the peeked copy and the queued copy away 
575          */
576         gdk_event_free (next_event);
577         next_event = gdk_event_get ();
578         gdk_event_free (next_event);
579         
580         return;
581       }
582
583   if (next_event)
584     gdk_event_free (next_event);
585
586   /* Find the widget which got the event. We store the widget
587    *  in the user_data field of GdkWindow's.
588    *  Ignore the event if we don't have a widget for it, except
589    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
590    *  Though this happens rarely, bogus events can occour
591    *  for e.g. destroyed GdkWindows. 
592    */
593   event_widget = gtk_get_event_widget (event);
594   if (!event_widget)
595     {
596       /* To handle selection INCR transactions, we select
597        * PropertyNotify events on the requestor window and create
598        * a corresponding (fake) GdkWindow so that events get
599        * here. There won't be a widget though, so we have to handle
600            * them specially
601            */
602       if (event->type == GDK_PROPERTY_NOTIFY)
603         gtk_selection_incr_event (event->any.window,
604                                   &event->property);
605       
606       return;
607     }
608   
609   /* Push the event onto a stack of current events for
610    * gtk_current_event_get().
611    */
612   current_events = g_list_prepend (current_events, event);
613   
614   /* If there is a grab in effect...
615    */
616   if (grabs)
617     {
618       grab_widget = grabs->data;
619       
620       /* If the grab widget is an ancestor of the event widget
621        *  then we send the event to the original event widget.
622        *  This is the key to implementing modality.
623        */
624       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
625           gtk_widget_is_ancestor (event_widget, grab_widget))
626         grab_widget = event_widget;
627     }
628   else
629     {
630       grab_widget = event_widget;
631     }
632   
633   /* Not all events get sent to the grabbing widget.
634    * The delete, destroy, expose, focus change and resize
635    *  events still get sent to the event widget because
636    *  1) these events have no meaning for the grabbing widget
637    *  and 2) redirecting these events to the grabbing widget
638    *  could cause the display to be messed up.
639    * 
640    * Drag events are also not redirected, since it isn't
641    *  clear what the semantics of that would be.
642    */
643   switch (event->type)
644     {
645     case GDK_NOTHING:
646       break;
647       
648     case GDK_DELETE:
649       gtk_widget_ref (event_widget);
650       if (!gtk_widget_event (event_widget, event) &&
651           !GTK_OBJECT_DESTROYED (event_widget))
652         gtk_widget_destroy (event_widget);
653       gtk_widget_unref (event_widget);
654       break;
655       
656     case GDK_DESTROY:
657       gtk_widget_ref (event_widget);
658       gtk_widget_event (event_widget, event);
659       if (!GTK_OBJECT_DESTROYED (event_widget))
660         gtk_widget_destroy (event_widget);
661       gtk_widget_unref (event_widget);
662       break;
663       
664     case GDK_PROPERTY_NOTIFY:
665     case GDK_EXPOSE:
666     case GDK_NO_EXPOSE:
667     case GDK_FOCUS_CHANGE:
668     case GDK_CONFIGURE:
669     case GDK_MAP:
670     case GDK_UNMAP:
671     case GDK_SELECTION_CLEAR:
672     case GDK_SELECTION_REQUEST:
673     case GDK_SELECTION_NOTIFY:
674     case GDK_CLIENT_EVENT:
675     case GDK_VISIBILITY_NOTIFY:
676       gtk_widget_event (event_widget, event);
677       break;
678       
679     case GDK_KEY_PRESS:
680     case GDK_KEY_RELEASE:
681       if (key_snoopers)
682         {
683           if (gtk_invoke_key_snoopers (grab_widget, event))
684             break;
685         }
686       /* else fall through */
687     case GDK_MOTION_NOTIFY:
688     case GDK_BUTTON_PRESS:
689     case GDK_2BUTTON_PRESS:
690     case GDK_3BUTTON_PRESS:
691     case GDK_BUTTON_RELEASE:
692     case GDK_PROXIMITY_IN:
693     case GDK_PROXIMITY_OUT:
694       gtk_propagate_event (grab_widget, event);
695       break;
696       
697     case GDK_ENTER_NOTIFY:
698       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
699         {
700           gtk_widget_event (grab_widget, event);
701           if (event_widget == grab_widget)
702             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
703         }
704       break;
705       
706     case GDK_LEAVE_NOTIFY:
707       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
708         {
709           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
710           gtk_widget_event (event_widget, event);
711         }
712       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
713         gtk_widget_event (grab_widget, event);
714       break;
715       
716     case GDK_DRAG_STATUS:
717     case GDK_DROP_FINISHED:
718       gtk_drag_source_handle_event (event_widget, event);
719       break;
720     case GDK_DRAG_ENTER:
721     case GDK_DRAG_LEAVE:
722     case GDK_DRAG_MOTION:
723     case GDK_DROP_START:
724       gtk_drag_dest_handle_event (event_widget, event);
725       break;
726     }
727   
728   tmp_list = current_events;
729   current_events = g_list_remove_link (current_events, tmp_list);
730   g_list_free_1 (tmp_list);
731 }
732
733 gint
734 gtk_true (void)
735 {
736   return TRUE;
737 }
738
739 gint
740 gtk_false (void)
741 {
742   return FALSE;
743 }
744
745 void
746 gtk_grab_add (GtkWidget *widget)
747 {
748   g_return_if_fail (widget != NULL);
749   
750   if (!GTK_WIDGET_HAS_GRAB (widget))
751     {
752       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
753       
754       grabs = g_slist_prepend (grabs, widget);
755       gtk_widget_ref (widget);
756     }
757 }
758
759 GtkWidget*
760 gtk_grab_get_current (void)
761 {
762   if (grabs)
763     return GTK_WIDGET (grabs->data);
764   return NULL;
765 }
766
767 void
768 gtk_grab_remove (GtkWidget *widget)
769 {
770   g_return_if_fail (widget != NULL);
771   
772   if (GTK_WIDGET_HAS_GRAB (widget))
773     {
774       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
775       
776       grabs = g_slist_remove (grabs, widget);
777       gtk_widget_unref (widget);
778     }
779 }
780
781 void
782 gtk_init_add (GtkFunction function,
783               gpointer    data)
784 {
785   GtkInitFunction *init;
786   
787   init = g_new (GtkInitFunction, 1);
788   init->function = function;
789   init->data = data;
790   
791   init_functions = g_list_prepend (init_functions, init);
792 }
793
794 guint
795 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
796                          gpointer        func_data)
797 {
798   GtkKeySnooperData *data;
799   static guint snooper_id = 1;
800
801   g_return_val_if_fail (snooper != NULL, 0);
802
803   data = g_new (GtkKeySnooperData, 1);
804   data->func = snooper;
805   data->func_data = func_data;
806   data->id = snooper_id++;
807   key_snoopers = g_slist_prepend (key_snoopers, data);
808
809   return data->id;
810 }
811
812 void
813 gtk_key_snooper_remove (guint            snooper_id)
814 {
815   GtkKeySnooperData *data = NULL;
816   GSList *slist;
817
818   slist = key_snoopers;
819   while (slist)
820     {
821       data = slist->data;
822       if (data->id == snooper_id)
823         break;
824
825       slist = slist->next;
826       data = NULL;
827     }
828   if (data)
829     key_snoopers = g_slist_remove (key_snoopers, data);
830 }
831
832 static gint
833 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
834                          GdkEvent  *event)
835 {
836   GSList *slist;
837   gint return_val = FALSE;
838
839   slist = key_snoopers;
840   while (slist && !return_val)
841     {
842       GtkKeySnooperData *data;
843
844       data = slist->data;
845       slist = slist->next;
846       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
847     }
848
849   return return_val;
850 }
851
852 guint
853 gtk_quit_add_full (guint                main_level,
854                    GtkFunction          function,
855                    GtkCallbackMarshal   marshal,
856                    gpointer             data,
857                    GtkDestroyNotify     destroy)
858 {
859   static guint quit_id = 1;
860   GtkQuitFunction *quitf;
861   
862   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
863
864   if (!quit_mem_chunk)
865     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
866                                       512, G_ALLOC_AND_FREE);
867   
868   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
869   
870   quitf->id = quit_id++;
871   quitf->main_level = main_level;
872   quitf->function = function;
873   quitf->marshal = marshal;
874   quitf->data = data;
875   quitf->destroy = destroy;
876
877   quit_functions = g_list_prepend (quit_functions, quitf);
878   
879   return quitf->id;
880 }
881
882 static void
883 gtk_quit_destroy (GtkQuitFunction *quitf)
884 {
885   if (quitf->destroy)
886     quitf->destroy (quitf->data);
887   g_mem_chunk_free (quit_mem_chunk, quitf);
888 }
889
890 static gint
891 gtk_quit_destructor (GtkObject **object_p)
892 {
893   if (*object_p)
894     gtk_object_destroy (*object_p);
895   g_free (object_p);
896
897   return FALSE;
898 }
899
900 void
901 gtk_quit_add_destroy (guint              main_level,
902                       GtkObject         *object)
903 {
904   GtkObject **object_p;
905
906   g_return_if_fail (main_level > 0);
907   g_return_if_fail (object != NULL);
908   g_return_if_fail (GTK_IS_OBJECT (object));
909
910   object_p = g_new (GtkObject*, 1);
911   *object_p = object;
912   gtk_signal_connect (object,
913                       "destroy",
914                       GTK_SIGNAL_FUNC (gtk_widget_destroyed),
915                       object_p);
916   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
917 }
918
919 guint
920 gtk_quit_add (guint       main_level,
921               GtkFunction function,
922               gpointer    data)
923 {
924   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
925 }
926
927 void
928 gtk_quit_remove (guint id)
929 {
930   GtkQuitFunction *quitf;
931   GList *tmp_list;
932   
933   tmp_list = quit_functions;
934   while (tmp_list)
935     {
936       quitf = tmp_list->data;
937       
938       if (quitf->id == id)
939         {
940           quit_functions = g_list_remove_link (quit_functions, tmp_list);
941           g_list_free (tmp_list);
942           gtk_quit_destroy (quitf);
943           
944           return;
945         }
946       
947       tmp_list = tmp_list->next;
948     }
949 }
950
951 void
952 gtk_quit_remove_by_data (gpointer data)
953 {
954   GtkQuitFunction *quitf;
955   GList *tmp_list;
956   
957   tmp_list = quit_functions;
958   while (tmp_list)
959     {
960       quitf = tmp_list->data;
961       
962       if (quitf->data == data)
963         {
964           quit_functions = g_list_remove_link (quit_functions, tmp_list);
965           g_list_free (tmp_list);
966           gtk_quit_destroy (quitf);
967
968           return;
969         }
970       
971       tmp_list = tmp_list->next;
972     }
973 }
974
975 guint
976 gtk_timeout_add_full (guint32            interval,
977                       GtkFunction        function,
978                       GtkCallbackMarshal marshal,
979                       gpointer           data,
980                       GtkDestroyNotify   destroy)
981 {
982   if (marshal)
983     {
984       GtkClosure *closure;
985
986       closure = g_new (GtkClosure, 1);
987       closure->marshal = marshal;
988       closure->data = data;
989       closure->destroy = destroy;
990
991       return g_timeout_add_full (0, interval, 
992                                  gtk_invoke_idle_timeout,
993                                  closure,
994                                  gtk_destroy_closure);
995     }
996   else
997     return g_timeout_add_full (0, interval, function, data, destroy);
998 }
999
1000 guint
1001 gtk_timeout_add (guint32     interval,
1002                  GtkFunction function,
1003                  gpointer    data)
1004 {
1005   return g_timeout_add_full (0, interval, function, data, NULL);
1006 }
1007
1008 void
1009 gtk_timeout_remove (guint tag)
1010 {
1011   g_source_remove (tag);
1012 }
1013
1014 guint
1015 gtk_idle_add_full (gint                 priority,
1016                    GtkFunction          function,
1017                    GtkCallbackMarshal   marshal,
1018                    gpointer             data,
1019                    GtkDestroyNotify     destroy)
1020 {
1021   if (marshal)
1022     {
1023       GtkClosure *closure;
1024
1025       closure = g_new (GtkClosure, 1);
1026       closure->marshal = marshal;
1027       closure->data = data;
1028       closure->destroy = destroy;
1029
1030       return g_idle_add_full (priority,
1031                               gtk_invoke_idle_timeout,
1032                               closure,
1033                               gtk_destroy_closure);
1034     }
1035   else
1036     return g_idle_add_full (priority, function, data, destroy);
1037 }
1038
1039 guint
1040 gtk_idle_add (GtkFunction function,
1041               gpointer    data)
1042 {
1043   return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1044 }
1045
1046 guint       
1047 gtk_idle_add_priority   (gint               priority,
1048                          GtkFunction        function,
1049                          gpointer           data)
1050 {
1051   return g_idle_add_full (priority, function, data, NULL);
1052 }
1053
1054 void
1055 gtk_idle_remove (guint tag)
1056 {
1057   g_source_remove (tag);
1058 }
1059
1060 void
1061 gtk_idle_remove_by_data (gpointer data)
1062 {
1063   if (!g_idle_remove_by_data (data))
1064     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1065 }
1066
1067 guint
1068 gtk_input_add_full (gint                source,
1069                     GdkInputCondition   condition,
1070                     GdkInputFunction    function,
1071                     GtkCallbackMarshal  marshal,
1072                     gpointer            data,
1073                     GtkDestroyNotify    destroy)
1074 {
1075   if (marshal)
1076     {
1077       GtkClosure *closure;
1078
1079       closure = g_new (GtkClosure, 1);
1080       closure->marshal = marshal;
1081       closure->data = data;
1082       closure->destroy = destroy;
1083
1084       return gdk_input_add_full (source,
1085                                  condition,
1086                                  (GdkInputFunction) gtk_invoke_input,
1087                                  closure,
1088                                  (GdkDestroyNotify) gtk_destroy_closure);
1089     }
1090   else
1091     return gdk_input_add_full (source, condition, function, data, destroy);
1092 }
1093
1094 void
1095 gtk_input_remove (guint tag)
1096 {
1097   g_source_remove (tag);
1098 }
1099
1100 static void
1101 gtk_destroy_closure (gpointer data)
1102 {
1103   GtkClosure *closure = data;
1104
1105   if (closure->destroy)
1106     (closure->destroy) (closure->data);
1107   g_free (closure);
1108 }
1109
1110 static gboolean
1111 gtk_invoke_idle_timeout (gpointer data)
1112 {
1113   GtkClosure *closure = data;
1114
1115   GtkArg args[1];
1116   gint ret_val = FALSE;
1117   args[0].name = NULL;
1118   args[0].type = GTK_TYPE_BOOL;
1119   args[0].d.pointer_data = &ret_val;
1120   closure->marshal (NULL, closure->data,  0, args);
1121   return ret_val;
1122 }
1123
1124 static void
1125 gtk_invoke_input (gpointer          data,
1126                   gint              source,
1127                   GdkInputCondition condition)
1128 {
1129   GtkClosure *closure = data;
1130
1131   GtkArg args[3];
1132   args[0].type = GTK_TYPE_INT;
1133   args[0].name = NULL;
1134   GTK_VALUE_INT(args[0]) = source;
1135   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1136   args[1].name = NULL;
1137   GTK_VALUE_FLAGS(args[1]) = condition;
1138   args[2].type = GTK_TYPE_NONE;
1139   args[2].name = NULL;
1140
1141   closure->marshal (NULL, closure->data, 2, args);
1142 }
1143
1144 GdkEvent*
1145 gtk_get_current_event (void)
1146 {
1147   if (current_events)
1148     return gdk_event_copy ((GdkEvent *) current_events->data);
1149   else
1150     return NULL;
1151 }
1152
1153 GtkWidget*
1154 gtk_get_event_widget (GdkEvent *event)
1155 {
1156   GtkWidget *widget;
1157
1158   widget = NULL;
1159   if (event && event->any.window)
1160     gdk_window_get_user_data (event->any.window, (void**) &widget);
1161   
1162   return widget;
1163 }
1164
1165 static void
1166 gtk_exit_func (void)
1167 {
1168   if (gtk_initialized)
1169     {
1170       gtk_initialized = FALSE;
1171       gtk_preview_uninit ();
1172     }
1173 }
1174
1175
1176 static gint
1177 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1178 {
1179   if (!quitf->marshal)
1180     return quitf->function (quitf->data);
1181   else
1182     {
1183       GtkArg args[1];
1184       gint ret_val = FALSE;
1185
1186       args[0].name = NULL;
1187       args[0].type = GTK_TYPE_BOOL;
1188       args[0].d.pointer_data = &ret_val;
1189       ((GtkCallbackMarshal) quitf->marshal) (NULL,
1190                                              quitf->data,
1191                                              0, args);
1192       return ret_val;
1193     }
1194 }
1195
1196 void
1197 gtk_propagate_event (GtkWidget *widget,
1198                      GdkEvent  *event)
1199 {
1200   gint handled_event;
1201   
1202   g_return_if_fail (widget != NULL);
1203   g_return_if_fail (GTK_IS_WIDGET (widget));
1204   g_return_if_fail (event != NULL);
1205   
1206   handled_event = FALSE;
1207
1208   if ((event->type == GDK_KEY_PRESS) ||
1209       (event->type == GDK_KEY_RELEASE))
1210     {
1211       /* Only send key events within Window widgets to the Window
1212        *  The Window widget will in turn pass the
1213        *  key event on to the currently focused widget
1214        *  for that window.
1215        */
1216       GtkWidget *window;
1217
1218       window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1219       if (window)
1220         {
1221           if (GTK_WIDGET_IS_SENSITIVE (window))
1222             gtk_widget_event (window, event);
1223
1224           handled_event = TRUE; /* don't send to widget */
1225         }
1226     }
1227   
1228   /* Other events get propagated up the widget tree
1229    *  so that parents can see the button and motion
1230    *  events of the children.
1231    */
1232   while (!handled_event && widget)
1233     {
1234       GtkWidget *tmp;
1235
1236       gtk_widget_ref (widget);
1237       handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1238       tmp = widget->parent;
1239       gtk_widget_unref (widget);
1240       widget  = tmp;
1241     }
1242 }
1243
1244 #if 0
1245 static void
1246 gtk_error (gchar *str)
1247 {
1248   gtk_print (str);
1249 }
1250
1251 static void
1252 gtk_warning (gchar *str)
1253 {
1254   gtk_print (str);
1255 }
1256
1257 static void
1258 gtk_message (gchar *str)
1259 {
1260   gtk_print (str);
1261 }
1262
1263 static void
1264 gtk_print (gchar *str)
1265 {
1266   static GtkWidget *window = NULL;
1267   static GtkWidget *text;
1268   static int level = 0;
1269   GtkWidget *box1;
1270   GtkWidget *box2;
1271   GtkWidget *table;
1272   GtkWidget *hscrollbar;
1273   GtkWidget *vscrollbar;
1274   GtkWidget *separator;
1275   GtkWidget *button;
1276   
1277   if (level > 0)
1278     {
1279       fputs (str, stdout);
1280       fflush (stdout);
1281       return;
1282     }
1283   
1284   if (!window)
1285     {
1286       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1287       
1288       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1289                           (GtkSignalFunc) gtk_widget_destroyed,
1290                           &window);
1291       
1292       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1293       
1294       box1 = gtk_vbox_new (FALSE, 0);
1295       gtk_container_add (GTK_CONTAINER (window), box1);
1296       gtk_widget_show (box1);
1297       
1298       
1299       box2 = gtk_vbox_new (FALSE, 10);
1300       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1301       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1302       gtk_widget_show (box2);
1303       
1304       
1305       table = gtk_table_new (2, 2, FALSE);
1306       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1307       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1308       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1309       gtk_widget_show (table);
1310       
1311       text = gtk_text_new (NULL, NULL);
1312       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1313       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1314       gtk_widget_show (text);
1315       gtk_widget_realize (text);
1316       
1317       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1318       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1319                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1320       gtk_widget_show (hscrollbar);
1321       
1322       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1323       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1324                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1325       gtk_widget_show (vscrollbar);
1326       
1327       separator = gtk_hseparator_new ();
1328       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1329       gtk_widget_show (separator);
1330       
1331       
1332       box2 = gtk_vbox_new (FALSE, 10);
1333       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1334       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1335       gtk_widget_show (box2);
1336       
1337       
1338       button = gtk_button_new_with_label ("close");
1339       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1340                                  (GtkSignalFunc) gtk_widget_hide,
1341                                  GTK_OBJECT (window));
1342       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1343       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1344       gtk_widget_grab_default (button);
1345       gtk_widget_show (button);
1346     }
1347   
1348   level += 1;
1349   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1350   level -= 1;
1351   
1352   if (!GTK_WIDGET_VISIBLE (window))
1353     gtk_widget_show (window);
1354 }
1355 #endif