]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
added an assertement.
[~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_get ();
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         gdk_event_free (next_event);
575         next_event = NULL;
576         
577         return;
578       }
579   
580
581   if (next_event)
582     {
583       gdk_event_put (next_event);
584       gdk_event_free (next_event);
585       next_event = NULL;
586     }
587
588   /* Find the widget which got the event. We store the widget
589    *  in the user_data field of GdkWindow's.
590    *  Ignore the event if we don't have a widget for it, except
591    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
592    *  Though this happens rarely, bogus events can occour
593    *  for e.g. destroyed GdkWindows. 
594    */
595   event_widget = gtk_get_event_widget (event);
596   if (!event_widget)
597     {
598       /* To handle selection INCR transactions, we select
599        * PropertyNotify events on the requestor window and create
600        * a corresponding (fake) GdkWindow so that events get
601        * here. There won't be a widget though, so we have to handle
602            * them specially
603            */
604       if (event->type == GDK_PROPERTY_NOTIFY)
605         gtk_selection_incr_event (event->any.window,
606                                   &event->property);
607       
608       return;
609     }
610   
611   /* Push the event onto a stack of current events for
612    * gtk_current_event_get().
613    */
614   current_events = g_list_prepend (current_events, event);
615   
616   /* If there is a grab in effect...
617    */
618   if (grabs)
619     {
620       grab_widget = grabs->data;
621       
622       /* If the grab widget is an ancestor of the event widget
623        *  then we send the event to the original event widget.
624        *  This is the key to implementing modality.
625        */
626       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
627           gtk_widget_is_ancestor (event_widget, grab_widget))
628         grab_widget = event_widget;
629     }
630   else
631     {
632       grab_widget = event_widget;
633     }
634   
635   /* Not all events get sent to the grabbing widget.
636    * The delete, destroy, expose, focus change and resize
637    *  events still get sent to the event widget because
638    *  1) these events have no meaning for the grabbing widget
639    *  and 2) redirecting these events to the grabbing widget
640    *  could cause the display to be messed up.
641    * 
642    * Drag events are also not redirected, since it isn't
643    *  clear what the semantics of that would be.
644    */
645   switch (event->type)
646     {
647     case GDK_NOTHING:
648       break;
649       
650     case GDK_DELETE:
651       gtk_widget_ref (event_widget);
652       if (!gtk_widget_event (event_widget, event) &&
653           !GTK_OBJECT_DESTROYED (event_widget))
654         gtk_widget_destroy (event_widget);
655       gtk_widget_unref (event_widget);
656       break;
657       
658     case GDK_DESTROY:
659       gtk_widget_ref (event_widget);
660       gtk_widget_event (event_widget, event);
661       if (!GTK_OBJECT_DESTROYED (event_widget))
662         gtk_widget_destroy (event_widget);
663       gtk_widget_unref (event_widget);
664       break;
665       
666     case GDK_PROPERTY_NOTIFY:
667     case GDK_EXPOSE:
668     case GDK_NO_EXPOSE:
669     case GDK_FOCUS_CHANGE:
670     case GDK_CONFIGURE:
671     case GDK_MAP:
672     case GDK_UNMAP:
673     case GDK_SELECTION_CLEAR:
674     case GDK_SELECTION_REQUEST:
675     case GDK_SELECTION_NOTIFY:
676     case GDK_CLIENT_EVENT:
677     case GDK_VISIBILITY_NOTIFY:
678       gtk_widget_event (event_widget, event);
679       break;
680       
681     case GDK_KEY_PRESS:
682     case GDK_KEY_RELEASE:
683       if (key_snoopers)
684         {
685           if (gtk_invoke_key_snoopers (grab_widget, event))
686             break;
687         }
688       /* else fall through */
689     case GDK_MOTION_NOTIFY:
690     case GDK_BUTTON_PRESS:
691     case GDK_2BUTTON_PRESS:
692     case GDK_3BUTTON_PRESS:
693     case GDK_BUTTON_RELEASE:
694     case GDK_PROXIMITY_IN:
695     case GDK_PROXIMITY_OUT:
696       gtk_propagate_event (grab_widget, event);
697       break;
698       
699     case GDK_ENTER_NOTIFY:
700       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
701         {
702           gtk_widget_event (grab_widget, event);
703           if (event_widget == grab_widget)
704             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
705         }
706       break;
707       
708     case GDK_LEAVE_NOTIFY:
709       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
710         {
711           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
712           gtk_widget_event (event_widget, event);
713         }
714       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
715         gtk_widget_event (grab_widget, event);
716       break;
717       
718     case GDK_DRAG_STATUS:
719     case GDK_DROP_FINISHED:
720       gtk_drag_source_handle_event (event_widget, event);
721       break;
722     case GDK_DRAG_ENTER:
723     case GDK_DRAG_LEAVE:
724     case GDK_DRAG_MOTION:
725     case GDK_DROP_START:
726       gtk_drag_dest_handle_event (event_widget, event);
727       break;
728     }
729   
730   tmp_list = current_events;
731   current_events = g_list_remove_link (current_events, tmp_list);
732   g_list_free_1 (tmp_list);
733 }
734
735 gint
736 gtk_true (void)
737 {
738   return TRUE;
739 }
740
741 gint
742 gtk_false (void)
743 {
744   return FALSE;
745 }
746
747 void
748 gtk_grab_add (GtkWidget *widget)
749 {
750   g_return_if_fail (widget != NULL);
751   
752   if (!GTK_WIDGET_HAS_GRAB (widget))
753     {
754       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
755       
756       grabs = g_slist_prepend (grabs, widget);
757       gtk_widget_ref (widget);
758     }
759 }
760
761 GtkWidget*
762 gtk_grab_get_current (void)
763 {
764   if (grabs)
765     return GTK_WIDGET (grabs->data);
766   return NULL;
767 }
768
769 void
770 gtk_grab_remove (GtkWidget *widget)
771 {
772   g_return_if_fail (widget != NULL);
773   
774   if (GTK_WIDGET_HAS_GRAB (widget))
775     {
776       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
777       
778       grabs = g_slist_remove (grabs, widget);
779       gtk_widget_unref (widget);
780     }
781 }
782
783 void
784 gtk_init_add (GtkFunction function,
785               gpointer    data)
786 {
787   GtkInitFunction *init;
788   
789   init = g_new (GtkInitFunction, 1);
790   init->function = function;
791   init->data = data;
792   
793   init_functions = g_list_prepend (init_functions, init);
794 }
795
796 guint
797 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
798                          gpointer        func_data)
799 {
800   GtkKeySnooperData *data;
801   static guint snooper_id = 1;
802
803   g_return_val_if_fail (snooper != NULL, 0);
804
805   data = g_new (GtkKeySnooperData, 1);
806   data->func = snooper;
807   data->func_data = func_data;
808   data->id = snooper_id++;
809   key_snoopers = g_slist_prepend (key_snoopers, data);
810
811   return data->id;
812 }
813
814 void
815 gtk_key_snooper_remove (guint            snooper_id)
816 {
817   GtkKeySnooperData *data = NULL;
818   GSList *slist;
819
820   slist = key_snoopers;
821   while (slist)
822     {
823       data = slist->data;
824       if (data->id == snooper_id)
825         break;
826
827       slist = slist->next;
828       data = NULL;
829     }
830   if (data)
831     key_snoopers = g_slist_remove (key_snoopers, data);
832 }
833
834 static gint
835 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
836                          GdkEvent  *event)
837 {
838   GSList *slist;
839   gint return_val = FALSE;
840
841   slist = key_snoopers;
842   while (slist && !return_val)
843     {
844       GtkKeySnooperData *data;
845
846       data = slist->data;
847       slist = slist->next;
848       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
849     }
850
851   return return_val;
852 }
853
854 guint
855 gtk_quit_add_full (guint                main_level,
856                    GtkFunction          function,
857                    GtkCallbackMarshal   marshal,
858                    gpointer             data,
859                    GtkDestroyNotify     destroy)
860 {
861   static guint quit_id = 1;
862   GtkQuitFunction *quitf;
863   
864   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
865
866   if (!quit_mem_chunk)
867     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
868                                       512, G_ALLOC_AND_FREE);
869   
870   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
871   
872   quitf->id = quit_id++;
873   quitf->main_level = main_level;
874   quitf->function = function;
875   quitf->marshal = marshal;
876   quitf->data = data;
877   quitf->destroy = destroy;
878
879   quit_functions = g_list_prepend (quit_functions, quitf);
880   
881   return quitf->id;
882 }
883
884 static void
885 gtk_quit_destroy (GtkQuitFunction *quitf)
886 {
887   if (quitf->destroy)
888     quitf->destroy (quitf->data);
889   g_mem_chunk_free (quit_mem_chunk, quitf);
890 }
891
892 static gint
893 gtk_quit_destructor (GtkObject **object_p)
894 {
895   if (*object_p)
896     gtk_object_destroy (*object_p);
897   g_free (object_p);
898
899   return FALSE;
900 }
901
902 void
903 gtk_quit_add_destroy (guint              main_level,
904                       GtkObject         *object)
905 {
906   GtkObject **object_p;
907
908   g_return_if_fail (main_level > 0);
909   g_return_if_fail (object != NULL);
910   g_return_if_fail (GTK_IS_OBJECT (object));
911
912   object_p = g_new (GtkObject*, 1);
913   *object_p = object;
914   gtk_signal_connect (object,
915                       "destroy",
916                       GTK_SIGNAL_FUNC (gtk_widget_destroyed),
917                       object_p);
918   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
919 }
920
921 guint
922 gtk_quit_add (guint       main_level,
923               GtkFunction function,
924               gpointer    data)
925 {
926   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
927 }
928
929 void
930 gtk_quit_remove (guint id)
931 {
932   GtkQuitFunction *quitf;
933   GList *tmp_list;
934   
935   tmp_list = quit_functions;
936   while (tmp_list)
937     {
938       quitf = tmp_list->data;
939       
940       if (quitf->id == id)
941         {
942           quit_functions = g_list_remove_link (quit_functions, tmp_list);
943           g_list_free (tmp_list);
944           gtk_quit_destroy (quitf);
945           
946           return;
947         }
948       
949       tmp_list = tmp_list->next;
950     }
951 }
952
953 void
954 gtk_quit_remove_by_data (gpointer data)
955 {
956   GtkQuitFunction *quitf;
957   GList *tmp_list;
958   
959   tmp_list = quit_functions;
960   while (tmp_list)
961     {
962       quitf = tmp_list->data;
963       
964       if (quitf->data == data)
965         {
966           quit_functions = g_list_remove_link (quit_functions, tmp_list);
967           g_list_free (tmp_list);
968           gtk_quit_destroy (quitf);
969
970           return;
971         }
972       
973       tmp_list = tmp_list->next;
974     }
975 }
976
977 guint
978 gtk_timeout_add_full (guint32            interval,
979                       GtkFunction        function,
980                       GtkCallbackMarshal marshal,
981                       gpointer           data,
982                       GtkDestroyNotify   destroy)
983 {
984   if (marshal)
985     {
986       GtkClosure *closure;
987
988       closure = g_new (GtkClosure, 1);
989       closure->marshal = marshal;
990       closure->data = data;
991       closure->destroy = destroy;
992
993       return g_timeout_add_full (0, interval, 
994                                  gtk_invoke_idle_timeout,
995                                  closure,
996                                  gtk_destroy_closure);
997     }
998   else
999     return g_timeout_add_full (0, interval, function, data, destroy);
1000 }
1001
1002 guint
1003 gtk_timeout_add (guint32     interval,
1004                  GtkFunction function,
1005                  gpointer    data)
1006 {
1007   return g_timeout_add_full (0, interval, function, data, NULL);
1008 }
1009
1010 void
1011 gtk_timeout_remove (guint tag)
1012 {
1013   g_source_remove (tag);
1014 }
1015
1016 guint
1017 gtk_idle_add_full (gint                 priority,
1018                    GtkFunction          function,
1019                    GtkCallbackMarshal   marshal,
1020                    gpointer             data,
1021                    GtkDestroyNotify     destroy)
1022 {
1023   if (marshal)
1024     {
1025       GtkClosure *closure;
1026
1027       closure = g_new (GtkClosure, 1);
1028       closure->marshal = marshal;
1029       closure->data = data;
1030       closure->destroy = destroy;
1031
1032       return g_idle_add_full (priority,
1033                               gtk_invoke_idle_timeout,
1034                               closure,
1035                               gtk_destroy_closure);
1036     }
1037   else
1038     return g_idle_add_full (priority, function, data, destroy);
1039 }
1040
1041 guint
1042 gtk_idle_add (GtkFunction function,
1043               gpointer    data)
1044 {
1045   return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1046 }
1047
1048 guint       
1049 gtk_idle_add_priority   (gint               priority,
1050                          GtkFunction        function,
1051                          gpointer           data)
1052 {
1053   return g_idle_add_full (priority, function, data, NULL);
1054 }
1055
1056 void
1057 gtk_idle_remove (guint tag)
1058 {
1059   g_source_remove (tag);
1060 }
1061
1062 void
1063 gtk_idle_remove_by_data (gpointer data)
1064 {
1065   g_source_remove_by_user_data (data);
1066 }
1067
1068 guint
1069 gtk_input_add_full (gint                source,
1070                     GdkInputCondition   condition,
1071                     GdkInputFunction    function,
1072                     GtkCallbackMarshal  marshal,
1073                     gpointer            data,
1074                     GtkDestroyNotify    destroy)
1075 {
1076   if (marshal)
1077     {
1078       GtkClosure *closure;
1079
1080       closure = g_new (GtkClosure, 1);
1081       closure->marshal = marshal;
1082       closure->data = data;
1083       closure->destroy = destroy;
1084
1085       return gdk_input_add_full (source,
1086                                  condition,
1087                                  (GdkInputFunction) gtk_invoke_input,
1088                                  closure,
1089                                  (GdkDestroyNotify) gtk_destroy_closure);
1090     }
1091   else
1092     return gdk_input_add_full (source, condition, function, data, destroy);
1093 }
1094
1095 void
1096 gtk_input_remove (guint tag)
1097 {
1098   g_source_remove (tag);
1099 }
1100
1101 static void
1102 gtk_destroy_closure (gpointer data)
1103 {
1104   GtkClosure *closure = data;
1105
1106   if (closure->destroy)
1107     (closure->destroy) (closure->data);
1108   g_free (closure);
1109 }
1110
1111 static gboolean
1112 gtk_invoke_idle_timeout (gpointer data)
1113 {
1114   GtkClosure *closure = data;
1115
1116   GtkArg args[1];
1117   gint ret_val = FALSE;
1118   args[0].name = NULL;
1119   args[0].type = GTK_TYPE_BOOL;
1120   args[0].d.pointer_data = &ret_val;
1121   closure->marshal (NULL, closure->data,  0, args);
1122   return ret_val;
1123 }
1124
1125 static void
1126 gtk_invoke_input (gpointer          data,
1127                   gint              source,
1128                   GdkInputCondition condition)
1129 {
1130   GtkClosure *closure = data;
1131
1132   GtkArg args[3];
1133   args[0].type = GTK_TYPE_INT;
1134   args[0].name = NULL;
1135   GTK_VALUE_INT(args[0]) = source;
1136   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1137   args[1].name = NULL;
1138   GTK_VALUE_FLAGS(args[1]) = condition;
1139   args[2].type = GTK_TYPE_NONE;
1140   args[2].name = NULL;
1141
1142   closure->marshal (NULL, closure->data, 2, args);
1143 }
1144
1145 GdkEvent*
1146 gtk_get_current_event (void)
1147 {
1148   if (current_events)
1149     return gdk_event_copy ((GdkEvent *) current_events->data);
1150   else
1151     return NULL;
1152 }
1153
1154 GtkWidget*
1155 gtk_get_event_widget (GdkEvent *event)
1156 {
1157   GtkWidget *widget;
1158
1159   widget = NULL;
1160   if (event && event->any.window)
1161     gdk_window_get_user_data (event->any.window, (void**) &widget);
1162   
1163   return widget;
1164 }
1165
1166 static void
1167 gtk_exit_func (void)
1168 {
1169   if (gtk_initialized)
1170     {
1171       gtk_initialized = FALSE;
1172       gtk_preview_uninit ();
1173     }
1174 }
1175
1176
1177 static gint
1178 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1179 {
1180   if (!quitf->marshal)
1181     return quitf->function (quitf->data);
1182   else
1183     {
1184       GtkArg args[1];
1185       gint ret_val = FALSE;
1186
1187       args[0].name = NULL;
1188       args[0].type = GTK_TYPE_BOOL;
1189       args[0].d.pointer_data = &ret_val;
1190       ((GtkCallbackMarshal) quitf->marshal) (NULL,
1191                                              quitf->data,
1192                                              0, args);
1193       return ret_val;
1194     }
1195 }
1196
1197 void
1198 gtk_propagate_event (GtkWidget *widget,
1199                      GdkEvent  *event)
1200 {
1201   gint handled_event;
1202   
1203   g_return_if_fail (widget != NULL);
1204   g_return_if_fail (GTK_IS_WIDGET (widget));
1205   g_return_if_fail (event != NULL);
1206   
1207   handled_event = FALSE;
1208
1209   if ((event->type == GDK_KEY_PRESS) ||
1210       (event->type == GDK_KEY_RELEASE))
1211     {
1212       /* Only send key events within Window widgets to the Window
1213        *  The Window widget will in turn pass the
1214        *  key event on to the currently focused widget
1215        *  for that window.
1216        */
1217       GtkWidget *window;
1218
1219       window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1220       if (window)
1221         {
1222           if (GTK_WIDGET_IS_SENSITIVE (window))
1223             gtk_widget_event (window, event);
1224
1225           handled_event = TRUE; /* don't send to widget */
1226         }
1227     }
1228   
1229   /* Other events get propagated up the widget tree
1230    *  so that parents can see the button and motion
1231    *  events of the children.
1232    */
1233   while (!handled_event && widget)
1234     {
1235       GtkWidget *tmp;
1236
1237       gtk_widget_ref (widget);
1238       handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1239       tmp = widget->parent;
1240       gtk_widget_unref (widget);
1241       widget  = tmp;
1242     }
1243 }
1244
1245 #if 0
1246 static void
1247 gtk_error (gchar *str)
1248 {
1249   gtk_print (str);
1250 }
1251
1252 static void
1253 gtk_warning (gchar *str)
1254 {
1255   gtk_print (str);
1256 }
1257
1258 static void
1259 gtk_message (gchar *str)
1260 {
1261   gtk_print (str);
1262 }
1263
1264 static void
1265 gtk_print (gchar *str)
1266 {
1267   static GtkWidget *window = NULL;
1268   static GtkWidget *text;
1269   static int level = 0;
1270   GtkWidget *box1;
1271   GtkWidget *box2;
1272   GtkWidget *table;
1273   GtkWidget *hscrollbar;
1274   GtkWidget *vscrollbar;
1275   GtkWidget *separator;
1276   GtkWidget *button;
1277   
1278   if (level > 0)
1279     {
1280       fputs (str, stdout);
1281       fflush (stdout);
1282       return;
1283     }
1284   
1285   if (!window)
1286     {
1287       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1288       
1289       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1290                           (GtkSignalFunc) gtk_widget_destroyed,
1291                           &window);
1292       
1293       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1294       
1295       box1 = gtk_vbox_new (FALSE, 0);
1296       gtk_container_add (GTK_CONTAINER (window), box1);
1297       gtk_widget_show (box1);
1298       
1299       
1300       box2 = gtk_vbox_new (FALSE, 10);
1301       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1302       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1303       gtk_widget_show (box2);
1304       
1305       
1306       table = gtk_table_new (2, 2, FALSE);
1307       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1308       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1309       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1310       gtk_widget_show (table);
1311       
1312       text = gtk_text_new (NULL, NULL);
1313       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1314       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1315       gtk_widget_show (text);
1316       gtk_widget_realize (text);
1317       
1318       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1319       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1320                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1321       gtk_widget_show (hscrollbar);
1322       
1323       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1324       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1325                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1326       gtk_widget_show (vscrollbar);
1327       
1328       separator = gtk_hseparator_new ();
1329       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1330       gtk_widget_show (separator);
1331       
1332       
1333       box2 = gtk_vbox_new (FALSE, 10);
1334       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1335       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1336       gtk_widget_show (box2);
1337       
1338       
1339       button = gtk_button_new_with_label ("close");
1340       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1341                                  (GtkSignalFunc) gtk_widget_hide,
1342                                  GTK_OBJECT (window));
1343       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1344       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1345       gtk_widget_grab_default (button);
1346       gtk_widget_show (button);
1347     }
1348   
1349   level += 1;
1350   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1351   level -= 1;
1352   
1353   if (!GTK_WIDGET_VISIBLE (window))
1354     gtk_widget_show (window);
1355 }
1356 #endif