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