]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
Applied patch from Ron Steinke to add signal accumulators so that signals
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #ifdef GDK_WINDOWING_X11
28 #include <X11/Xlocale.h>        /* so we get the right setlocale */
29 #else
30 #include <locale.h>
31 #endif
32
33 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
34 #include <libintl.h>
35 #endif
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <gmodule.h>
41 #include "gtkdnd.h"
42 #include "gtkcompat.h"
43 #include "gtkmain.h"
44 #include "gtkrc.h"
45 #include "gtkselection.h"
46 #include "gtksignal.h"
47 #include "gtkwidget.h"
48 #include "gtkwindow.h"
49 #include "gtkprivate.h"
50 #include "gdk/gdki18n.h"
51 #include "config.h"
52 #include "gtkdebug.h"
53 #include "gtkintl.h"
54
55 /* Private type definitions
56  */
57 typedef struct _GtkInitFunction          GtkInitFunction;
58 typedef struct _GtkQuitFunction          GtkQuitFunction;
59 typedef struct _GtkClosure               GtkClosure;
60 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
61
62 struct _GtkInitFunction
63 {
64   GtkFunction function;
65   gpointer data;
66 };
67
68 struct _GtkQuitFunction
69 {
70   guint id;
71   guint main_level;
72   GtkCallbackMarshal marshal;
73   GtkFunction function;
74   gpointer data;
75   GtkDestroyNotify destroy;
76 };
77
78 struct _GtkClosure
79 {
80   GtkCallbackMarshal marshal;
81   gpointer data;
82   GtkDestroyNotify destroy;
83 };
84
85 struct _GtkKeySnooperData
86 {
87   GtkKeySnoopFunc func;
88   gpointer func_data;
89   guint id;
90 };
91
92 static void  gtk_exit_func               (void);
93 static gint  gtk_quit_invoke_function    (GtkQuitFunction    *quitf);
94 static void  gtk_quit_destroy            (GtkQuitFunction    *quitf);
95 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
96                                           GdkEvent           *event);
97
98 static void     gtk_destroy_closure      (gpointer            data);
99 static gboolean gtk_invoke_idle_timeout  (gpointer            data);
100 static void     gtk_invoke_input         (gpointer            data,
101                                           gint                source,
102                                           GdkInputCondition   condition);
103
104 #if 0
105 static void  gtk_error                   (gchar              *str);
106 static void  gtk_warning                 (gchar              *str);
107 static void  gtk_message                 (gchar              *str);
108 static void  gtk_print                   (gchar              *str);
109 #endif
110
111 const guint gtk_major_version = GTK_MAJOR_VERSION;
112 const guint gtk_minor_version = GTK_MINOR_VERSION;
113 const guint gtk_micro_version = GTK_MICRO_VERSION;
114 const guint gtk_binary_age = GTK_BINARY_AGE;
115 const guint gtk_interface_age = GTK_INTERFACE_AGE;
116
117 static guint gtk_main_loop_level = 0;
118 static gint gtk_initialized = FALSE;
119 static GList *current_events = NULL;
120
121 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
122
123 static GSList *grabs = NULL;               /* A stack of unique grabs. The grabbing
124                                             *  widget is the first one on the list.
125                                             */
126 static GList *init_functions = NULL;       /* A list of init functions.
127                                             */
128 static GList *quit_functions = NULL;       /* A list of quit functions.
129                                             */
130 static GMemChunk *quit_mem_chunk = NULL;
131
132 static GSList *key_snoopers = NULL;
133
134 static GdkVisual *gtk_visual;              /* The visual to be used in creating new
135                                             *  widgets.
136                                             */
137 static GdkColormap *gtk_colormap;          /* The colormap to be used in creating new
138                                             *  widgets.
139                                             */
140
141 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
142
143 #ifdef G_ENABLE_DEBUG
144 static const GDebugKey gtk_debug_keys[] = {
145   {"misc", GTK_DEBUG_MISC},
146   {"dnd", GTK_DEBUG_DND},
147   {"plugsocket", GTK_DEBUG_PLUGSOCKET},
148   {"text", GTK_DEBUG_TEXT},
149   {"tree", GTK_DEBUG_TREE},
150   {"updates", GTK_DEBUG_UPDATES}
151 };
152
153 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
154
155 #endif /* G_ENABLE_DEBUG */
156
157 gchar*
158 gtk_check_version (guint required_major,
159                    guint required_minor,
160                    guint required_micro)
161 {
162   if (required_major > GTK_MAJOR_VERSION)
163     return "Gtk+ version too old (major mismatch)";
164   if (required_major < GTK_MAJOR_VERSION)
165     return "Gtk+ version too new (major mismatch)";
166   if (required_minor > GTK_MINOR_VERSION)
167     return "Gtk+ version too old (minor mismatch)";
168   if (required_minor < GTK_MINOR_VERSION)
169     return "Gtk+ version too new (minor mismatch)";
170   if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
171     return "Gtk+ version too new (micro mismatch)";
172   if (required_micro > GTK_MICRO_VERSION)
173     return "Gtk+ version too old (micro mismatch)";
174   return NULL;
175 }
176
177 #ifdef __EMX__
178 static gchar *add_dll_suffix(gchar *module_name)
179 {
180     gchar *suffix = strrchr(module_name, '.');
181     
182     if (!suffix || stricmp(suffix, ".dll"))
183     {
184         gchar *old = module_name;
185           
186         module_name = g_strconcat (module_name, ".dll", NULL);
187         g_free (old);
188     }
189     return (module_name);
190 }
191 #endif
192
193 #undef gtk_init_check
194
195 /* This checks to see if the process is running suid or sgid
196  * at the current time. If so, we don't allow GTK+ to be initialized.
197  * This is meant to be a mild check - we only error out if we
198  * can prove the programmer is doing something wrong, not if
199  * they could be doing something wrong. For this reason, we
200  * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
201  */
202 static gboolean
203 check_setugid (void)
204 {
205 /* this isn't at all relevant on Windoze and doesn't compile ... --hb */
206 #ifndef G_OS_WIN32
207   uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
208   gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
209   
210 #ifdef HAVE_GETRESUID
211   /* These aren't in the header files, so we prototype them here.
212    */
213   int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
214   int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
215
216   if (getresuid (&ruid, &euid, &suid) != 0 ||
217       getresgid (&rgid, &egid, &sgid) != 0)
218 #endif /* HAVE_GETRESUID */
219     {
220       suid = ruid = getuid ();
221       sgid = rgid = getgid ();
222       euid = geteuid ();
223       egid = getegid ();
224     }
225
226   if (ruid != euid || ruid != suid ||
227       rgid != egid || rgid != sgid)
228     {
229       g_warning ("This process is currently running setuid or setgid.\n"
230                  "This is not a supported use of GTK+. You must create a helper\n"
231                  "program instead. For further details, see:\n\n"
232                  "    http://www.gtk.org/setuid.html\n\n"
233                  "Refusing to initialize GTK+.");
234       exit (1);
235     }
236 #endif
237   return TRUE;
238 }
239
240 gboolean
241 gtk_init_check (int      *argc,
242                 char   ***argv)
243 {
244   GSList *gtk_modules = NULL;
245   GSList *slist;
246   gchar *env_string = NULL;
247
248   if (gtk_initialized)
249     return TRUE;
250
251   if (!check_setugid ())
252     return FALSE;
253   
254 #if     0
255   g_set_error_handler (gtk_error);
256   g_set_warning_handler (gtk_warning);
257   g_set_message_handler (gtk_message);
258   g_set_print_handler (gtk_print);
259 #endif
260   
261   /* Initialize "gdk". We pass along the 'argc' and 'argv'
262    *  parameters as they contain information that GDK uses
263    */
264   if (!gdk_init_check (argc, argv))
265     return FALSE;
266
267   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
268   
269 #ifdef G_ENABLE_DEBUG
270   env_string = getenv ("GTK_DEBUG");
271   if (env_string != NULL)
272     {
273       gtk_debug_flags = g_parse_debug_string (env_string,
274                                               gtk_debug_keys,
275                                               gtk_ndebug_keys);
276       env_string = NULL;
277     }
278 #endif  /* G_ENABLE_DEBUG */
279
280   env_string = getenv ("GTK_MODULES");
281   if (env_string)
282     {
283       gchar **modules, **as;
284
285 #ifndef __EMX__
286       modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
287 #else
288       modules = g_strsplit (env_string, ";", -1);
289 #endif
290       for (as = modules; *as; as++)
291         {
292           if (**as)
293             gtk_modules = g_slist_prepend (gtk_modules, *as);
294           else
295             g_free (*as);
296         }
297       g_free (modules);
298       env_string = NULL;
299     }
300
301   if (argc && argv)
302     {
303       gint i, j, k;
304       
305       for (i = 1; i < *argc;)
306         {
307           if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
308               strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
309             {
310               gchar *module_name = (*argv)[i] + 12;
311               
312               if (*module_name == '=')
313                 module_name++;
314               else if (i + 1 < *argc)
315                 {
316                   (*argv)[i] = NULL;
317                   i += 1;
318                   module_name = (*argv)[i];
319                 }
320               (*argv)[i] = NULL;
321
322               if (module_name && *module_name)
323                 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
324             }
325           else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
326             {
327               GLogLevelFlags fatal_mask;
328               
329               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
330               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
331               g_log_set_always_fatal (fatal_mask);
332               (*argv)[i] = NULL;
333             }
334 #ifdef G_ENABLE_DEBUG
335           else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
336                    (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
337             {
338               gchar *equal_pos = strchr ((*argv)[i], '=');
339               
340               if (equal_pos != NULL)
341                 {
342                   gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
343                                                            gtk_debug_keys,
344                                                            gtk_ndebug_keys);
345                 }
346               else if ((i + 1) < *argc && (*argv)[i + 1])
347                 {
348                   gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
349                                                            gtk_debug_keys,
350                                                            gtk_ndebug_keys);
351                   (*argv)[i] = NULL;
352                   i += 1;
353                 }
354               (*argv)[i] = NULL;
355             }
356           else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
357                    (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
358             {
359               gchar *equal_pos = strchr ((*argv)[i], '=');
360               
361               if (equal_pos != NULL)
362                 {
363                   gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
364                                                             gtk_debug_keys,
365                                                             gtk_ndebug_keys);
366                 }
367               else if ((i + 1) < *argc && (*argv)[i + 1])
368                 {
369                   gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
370                                                             gtk_debug_keys,
371                                                             gtk_ndebug_keys);
372                   (*argv)[i] = NULL;
373                   i += 1;
374                 }
375               (*argv)[i] = NULL;
376             }
377 #endif /* G_ENABLE_DEBUG */
378           i += 1;
379         }
380       
381       for (i = 1; i < *argc; i++)
382         {
383           for (k = i; k < *argc; k++)
384             if ((*argv)[k] != NULL)
385               break;
386           
387           if (k > i)
388             {
389               k -= i;
390               for (j = i + k; j < *argc; j++)
391                 (*argv)[j-k] = (*argv)[j];
392               *argc -= k;
393             }
394         }
395     }
396
397   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
398     gdk_window_set_debug_updates (TRUE);
399   
400   /* load gtk modules */
401   gtk_modules = g_slist_reverse (gtk_modules);
402   for (slist = gtk_modules; slist; slist = slist->next)
403     {
404       gchar *module_name;
405       GModule *module = NULL;
406       GtkModuleInitFunc modinit_func = NULL;
407       
408       module_name = slist->data;
409       slist->data = NULL;
410 #ifndef __EMX__
411       if (!g_path_is_absolute (module_name))
412         {
413           gchar *old = module_name;
414           
415           module_name = g_module_build_path (NULL, module_name);
416           g_free (old);
417         }
418 #else
419       module_name = add_dll_suffix(module_name);
420 #endif
421       if (g_module_supported ())
422         {
423           module = g_module_open (module_name, G_MODULE_BIND_LAZY);
424           if (module &&
425               g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
426               modinit_func)
427             {
428               if (!g_slist_find (gtk_modules, modinit_func))
429                 {
430                   g_module_make_resident (module);
431                   slist->data = modinit_func;
432                 }
433               else
434                 {
435                   g_module_close (module);
436                   module = NULL;
437                 }
438             }
439         }
440       if (!modinit_func)
441         {
442           g_message ("Failed to load module \"%s\": %s",
443                      module ? g_module_name (module) : module_name,
444                      g_module_error ());
445           if (module)
446             g_module_close (module);
447         }
448       g_free (module_name);
449     }
450
451 #ifdef ENABLE_NLS
452 #  ifndef G_OS_WIN32
453   bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
454 #    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
455   bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
456 #    endif
457 #  else /* !G_OS_WIN32 */
458   {
459     bindtextdomain (GETTEXT_PACKAGE,
460                     g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
461                                                                    g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
462                                                                    "locale"));
463   }
464 #endif
465 #endif  
466
467   {
468   /* Translate to default:RTL if you want your widgets
469    * to be RTL, otherwise translate to default:LTR.
470    * Do *not* translate it to "predefinito:LTR", if it
471    * it isn't default:LTR or default:RTL it will not work 
472    */
473     char *e = _("default:LTR");
474     if (strcmp (e, "default:RTL")==0) {
475       gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
476     } else if (strcmp (e, "default:LTR")) {
477       g_warning ("Whoever translated default:LTR did so wrongly.\n");
478     }
479   }
480
481   /* Initialize the default visual and colormap to be
482    *  used in creating widgets. (We want to use the system
483    *  defaults so as to be nice to the colormap).
484    */
485   gtk_visual = gdk_visual_get_system ();
486   gtk_colormap = gdk_colormap_get_system ();
487
488   gtk_type_init (0);
489   gtk_rc_init ();
490   
491   
492   /* Register an exit function to make sure we are able to cleanup.
493    */
494   g_atexit (gtk_exit_func);
495   
496   /* Set the 'initialized' flag.
497    */
498   gtk_initialized = TRUE;
499
500   /* initialize gtk modules
501    */
502   for (slist = gtk_modules; slist; slist = slist->next)
503     {
504       if (slist->data)
505         {
506           GtkModuleInitFunc modinit;
507           
508           modinit = slist->data;
509           modinit (argc, argv);
510         }
511     }
512   g_slist_free (gtk_modules);
513   
514 #ifndef G_OS_WIN32
515   /* No use warning on Win32, there aren't any non-devel versions anyhow... */
516   g_message (""              "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
517              "                UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
518              "                if you don't know why you are getting this, you probably want to\n"
519              "                use the stable branch which can be retrived from\n"
520              "                ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
521              "                cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
522 #endif
523
524   return TRUE;
525 }
526
527 #undef gtk_init
528
529 void
530 gtk_init (int *argc, char ***argv)
531 {
532   if (!gtk_init_check (argc, argv))
533     {
534       g_warning ("cannot open display: %s", gdk_get_display ());
535       exit(1);
536     }
537 }
538
539 #ifdef G_OS_WIN32
540
541 static void
542 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
543 {
544   if (sizeof_GtkWindow != sizeof (GtkWindow))
545     g_error ("Incompatible build!\n"
546              "The code using GTK+ thinks GtkWindow is of different\n"
547              "size than it actually is in this build of GTK+.\n"
548              "On Windows, this probably means that you have compiled\n"
549              "your code with gcc without the -fnative-struct switch.");
550 }
551
552 /* These two functions might get more checks added later, thus pass
553  * in the number of extra args.
554  */
555 void
556 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
557 {
558   check_sizeof_GtkWindow (sizeof_GtkWindow);
559   gtk_init (argc, argv);
560 }
561
562 gboolean
563 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
564 {
565   check_sizeof_GtkWindow (sizeof_GtkWindow);
566   return gtk_init_check (argc, argv);
567 }
568
569 #endif
570
571 void
572 gtk_exit (gint errorcode)
573 {
574   /* Only if "gtk" has been initialized should we de-initialize.
575    */
576   /* de-initialisation is done by the gtk_exit_funct(),
577    * no need to do this here (Alex J.)
578    */
579   gdk_exit(errorcode);
580 }
581
582 gchar*
583 gtk_set_locale (void)
584 {
585   return gdk_set_locale ();
586 }
587
588 /**
589  * gtk_get_default_language:
590  *
591  * Returns the ISO language code for the default language currently in
592  * effect. (Note that this can change over the life of an
593  * application.)  The default language is derived from the current
594  * locale. It determines, for example, whether GTK+ uses the
595  * right-to-left or left-to-right text direction.
596  * 
597  * Return value: the default language as an allocated string, must be freed
598  **/
599 gchar*
600 gtk_get_default_language (void)
601 {
602   gchar *lang;
603   gchar *p;
604   
605   lang = g_strdup (setlocale (LC_CTYPE, NULL));
606   p = strchr (lang, '.');
607   if (p)
608     *p = '\0';
609   p = strchr (lang, '@');
610   if (p)
611     *p = '\0';
612
613   return lang;
614 }
615
616 void
617 gtk_main (void)
618 {
619   GList *tmp_list;
620   GList *functions;
621   GtkInitFunction *init;
622   GMainLoop *loop;
623
624   gtk_main_loop_level++;
625   
626   loop = g_main_new (TRUE);
627   main_loops = g_slist_prepend (main_loops, loop);
628
629   tmp_list = functions = init_functions;
630   init_functions = NULL;
631   
632   while (tmp_list)
633     {
634       init = tmp_list->data;
635       tmp_list = tmp_list->next;
636       
637       (* init->function) (init->data);
638       g_free (init);
639     }
640   g_list_free (functions);
641
642   if (g_main_is_running (main_loops->data))
643     {
644       GDK_THREADS_LEAVE ();
645       g_main_run (loop);
646       GDK_THREADS_ENTER ();
647       gdk_flush ();
648     }
649
650   if (quit_functions)
651     {
652       GList *reinvoke_list = NULL;
653       GtkQuitFunction *quitf;
654
655       while (quit_functions)
656         {
657           quitf = quit_functions->data;
658
659           tmp_list = quit_functions;
660           quit_functions = g_list_remove_link (quit_functions, quit_functions);
661           g_list_free_1 (tmp_list);
662
663           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
664               gtk_quit_invoke_function (quitf))
665             {
666               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
667             }
668           else
669             {
670               gtk_quit_destroy (quitf);
671             }
672         }
673       if (reinvoke_list)
674         {
675           GList *work;
676           
677           work = g_list_last (reinvoke_list);
678           if (quit_functions)
679             quit_functions->prev = work;
680           work->next = quit_functions;
681           quit_functions = work;
682         }
683
684       gdk_flush ();
685     }
686               
687   main_loops = g_slist_remove (main_loops, loop);
688
689   g_main_destroy (loop);
690
691   gtk_main_loop_level--;
692 }
693
694 guint
695 gtk_main_level (void)
696 {
697   return gtk_main_loop_level;
698 }
699
700 void
701 gtk_main_quit (void)
702 {
703   g_return_if_fail (main_loops != NULL);
704
705   g_main_quit (main_loops->data);
706 }
707
708 gint
709 gtk_events_pending (void)
710 {
711   gboolean result;
712   
713   GDK_THREADS_LEAVE ();  
714   result = g_main_pending();
715   GDK_THREADS_ENTER ();
716
717   return result;
718 }
719
720 gboolean
721 gtk_main_iteration (void)
722 {
723   GDK_THREADS_LEAVE ();
724   g_main_iteration (TRUE);
725   GDK_THREADS_ENTER ();
726
727   if (main_loops)
728     return !g_main_is_running (main_loops->data);
729   else
730     return TRUE;
731 }
732
733 gboolean
734 gtk_main_iteration_do (gboolean blocking)
735 {
736   GDK_THREADS_LEAVE ();
737   g_main_iteration (blocking);
738   GDK_THREADS_ENTER ();
739
740   if (main_loops)
741     return !g_main_is_running (main_loops->data);
742   else
743     return TRUE;
744 }
745
746 void 
747 gtk_main_do_event (GdkEvent *event)
748 {
749   GtkWidget *event_widget;
750   GtkWidget *grab_widget;
751   GdkEvent *next_event;
752   GList *tmp_list;
753
754   /* If there are any events pending then get the next one.
755    */
756   next_event = gdk_event_peek ();
757   
758   /* Try to compress enter/leave notify events. These event
759    *  pairs occur when the mouse is dragged quickly across
760    *  a window with many buttons (or through a menu). Instead
761    *  of highlighting and de-highlighting each widget that
762    *  is crossed it is better to simply de-highlight the widget
763    *  which contained the mouse initially and highlight the
764    *  widget which ends up containing the mouse.
765    */
766   if (next_event)
767     if (((event->type == GDK_ENTER_NOTIFY) ||
768          (event->type == GDK_LEAVE_NOTIFY)) &&
769         ((next_event->type == GDK_ENTER_NOTIFY) ||
770          (next_event->type == GDK_LEAVE_NOTIFY)) &&
771         (next_event->type != event->type) &&
772         (next_event->any.window == event->any.window))
773       {
774         /* Throw both the peeked copy and the queued copy away 
775          */
776         gdk_event_free (next_event);
777         next_event = gdk_event_get ();
778         gdk_event_free (next_event);
779         
780         return;
781       }
782
783   if (next_event)
784     gdk_event_free (next_event);
785
786   /* Find the widget which got the event. We store the widget
787    *  in the user_data field of GdkWindow's.
788    *  Ignore the event if we don't have a widget for it, except
789    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
790    *  Though this happens rarely, bogus events can occour
791    *  for e.g. destroyed GdkWindows. 
792    */
793   event_widget = gtk_get_event_widget (event);
794   if (!event_widget)
795     {
796       /* To handle selection INCR transactions, we select
797        * PropertyNotify events on the requestor window and create
798        * a corresponding (fake) GdkWindow so that events get
799        * here. There won't be a widget though, so we have to handle
800            * them specially
801            */
802       if (event->type == GDK_PROPERTY_NOTIFY)
803         gtk_selection_incr_event (event->any.window,
804                                   &event->property);
805       
806       return;
807     }
808   
809   /* Push the event onto a stack of current events for
810    * gtk_current_event_get().
811    */
812   current_events = g_list_prepend (current_events, event);
813   
814   /* If there is a grab in effect...
815    */
816   if (grabs)
817     {
818       grab_widget = grabs->data;
819       
820       /* If the grab widget is an ancestor of the event widget
821        *  then we send the event to the original event widget.
822        *  This is the key to implementing modality.
823        */
824       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
825           gtk_widget_is_ancestor (event_widget, grab_widget))
826         grab_widget = event_widget;
827     }
828   else
829     {
830       grab_widget = event_widget;
831     }
832
833   /* Not all events get sent to the grabbing widget.
834    * The delete, destroy, expose, focus change and resize
835    *  events still get sent to the event widget because
836    *  1) these events have no meaning for the grabbing widget
837    *  and 2) redirecting these events to the grabbing widget
838    *  could cause the display to be messed up.
839    * 
840    * Drag events are also not redirected, since it isn't
841    *  clear what the semantics of that would be.
842    */
843   switch (event->type)
844     {
845     case GDK_NOTHING:
846       break;
847       
848     case GDK_DELETE:
849       gtk_widget_ref (event_widget);
850       if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
851           !gtk_widget_event (event_widget, event))
852         gtk_widget_destroy (event_widget);
853       gtk_widget_unref (event_widget);
854       break;
855       
856     case GDK_DESTROY:
857       /* Unexpected GDK_DESTROY from the outside, ignore for
858        * child windows, handle like a GDK_DELETE for toplevels
859        */
860       if (!event_widget->parent)
861         {
862           gtk_widget_ref (event_widget);
863           if (!gtk_widget_event (event_widget, event) &&
864               !GTK_OBJECT_DESTROYED (event_widget))
865             gtk_widget_destroy (event_widget);
866           gtk_widget_unref (event_widget);
867         }
868       break;
869       
870     case GDK_EXPOSE:
871       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
872         gdk_window_begin_paint_region (event->any.window, event->expose.region);
873
874       gtk_widget_send_expose (event_widget, event);
875
876       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
877         gdk_window_end_paint (event->any.window);
878       break;
879
880     case GDK_PROPERTY_NOTIFY:
881     case GDK_NO_EXPOSE:
882     case GDK_FOCUS_CHANGE:
883     case GDK_CONFIGURE:
884     case GDK_MAP:
885     case GDK_UNMAP:
886     case GDK_SELECTION_CLEAR:
887     case GDK_SELECTION_REQUEST:
888     case GDK_SELECTION_NOTIFY:
889     case GDK_CLIENT_EVENT:
890     case GDK_VISIBILITY_NOTIFY:
891     case GDK_WINDOW_STATE:
892       gtk_widget_event (event_widget, event);
893       break;
894
895     case GDK_SCROLL:
896     case GDK_BUTTON_PRESS:
897     case GDK_2BUTTON_PRESS:
898     case GDK_3BUTTON_PRESS:
899       gtk_propagate_event (grab_widget, event);
900       break;
901
902     case GDK_KEY_PRESS:
903     case GDK_KEY_RELEASE:
904       if (key_snoopers)
905         {
906           if (gtk_invoke_key_snoopers (grab_widget, event))
907             break;
908         }
909       /* else fall through */
910     case GDK_MOTION_NOTIFY:
911     case GDK_BUTTON_RELEASE:
912     case GDK_PROXIMITY_IN:
913     case GDK_PROXIMITY_OUT:
914       gtk_propagate_event (grab_widget, event);
915       break;
916       
917     case GDK_ENTER_NOTIFY:
918       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
919         {
920           gtk_widget_event (grab_widget, event);
921           if (event_widget == grab_widget)
922             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
923         }
924       break;
925       
926     case GDK_LEAVE_NOTIFY:
927       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
928         {
929           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
930           gtk_widget_event (event_widget, event);
931         }
932       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
933         gtk_widget_event (grab_widget, event);
934       break;
935       
936     case GDK_DRAG_STATUS:
937     case GDK_DROP_FINISHED:
938       gtk_drag_source_handle_event (event_widget, event);
939       break;
940     case GDK_DRAG_ENTER:
941     case GDK_DRAG_LEAVE:
942     case GDK_DRAG_MOTION:
943     case GDK_DROP_START:
944       gtk_drag_dest_handle_event (event_widget, event);
945       break;
946     }
947   
948   tmp_list = current_events;
949   current_events = g_list_remove_link (current_events, tmp_list);
950   g_list_free_1 (tmp_list);
951 }
952
953 gboolean
954 gtk_true (void)
955 {
956   return TRUE;
957 }
958
959 gboolean
960 gtk_false (void)
961 {
962   return FALSE;
963 }
964
965 void
966 gtk_grab_add (GtkWidget *widget)
967 {
968   g_return_if_fail (widget != NULL);
969   
970   if (!GTK_WIDGET_HAS_GRAB (widget))
971     {
972       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
973       
974       grabs = g_slist_prepend (grabs, widget);
975       gtk_widget_ref (widget);
976     }
977 }
978
979 GtkWidget*
980 gtk_grab_get_current (void)
981 {
982   if (grabs)
983     return GTK_WIDGET (grabs->data);
984   return NULL;
985 }
986
987 void
988 gtk_grab_remove (GtkWidget *widget)
989 {
990   g_return_if_fail (widget != NULL);
991   
992   if (GTK_WIDGET_HAS_GRAB (widget))
993     {
994       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
995       
996       grabs = g_slist_remove (grabs, widget);
997       gtk_widget_unref (widget);
998     }
999 }
1000
1001 void
1002 gtk_init_add (GtkFunction function,
1003               gpointer    data)
1004 {
1005   GtkInitFunction *init;
1006   
1007   init = g_new (GtkInitFunction, 1);
1008   init->function = function;
1009   init->data = data;
1010   
1011   init_functions = g_list_prepend (init_functions, init);
1012 }
1013
1014 guint
1015 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1016                          gpointer        func_data)
1017 {
1018   GtkKeySnooperData *data;
1019   static guint snooper_id = 1;
1020
1021   g_return_val_if_fail (snooper != NULL, 0);
1022
1023   data = g_new (GtkKeySnooperData, 1);
1024   data->func = snooper;
1025   data->func_data = func_data;
1026   data->id = snooper_id++;
1027   key_snoopers = g_slist_prepend (key_snoopers, data);
1028
1029   return data->id;
1030 }
1031
1032 void
1033 gtk_key_snooper_remove (guint            snooper_id)
1034 {
1035   GtkKeySnooperData *data = NULL;
1036   GSList *slist;
1037
1038   slist = key_snoopers;
1039   while (slist)
1040     {
1041       data = slist->data;
1042       if (data->id == snooper_id)
1043         break;
1044
1045       slist = slist->next;
1046       data = NULL;
1047     }
1048   if (data)
1049     key_snoopers = g_slist_remove (key_snoopers, data);
1050 }
1051
1052 static gint
1053 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1054                          GdkEvent  *event)
1055 {
1056   GSList *slist;
1057   gint return_val = FALSE;
1058
1059   slist = key_snoopers;
1060   while (slist && !return_val)
1061     {
1062       GtkKeySnooperData *data;
1063
1064       data = slist->data;
1065       slist = slist->next;
1066       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1067     }
1068
1069   return return_val;
1070 }
1071
1072 guint
1073 gtk_quit_add_full (guint                main_level,
1074                    GtkFunction          function,
1075                    GtkCallbackMarshal   marshal,
1076                    gpointer             data,
1077                    GtkDestroyNotify     destroy)
1078 {
1079   static guint quit_id = 1;
1080   GtkQuitFunction *quitf;
1081   
1082   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1083
1084   if (!quit_mem_chunk)
1085     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1086                                       512, G_ALLOC_AND_FREE);
1087   
1088   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1089   
1090   quitf->id = quit_id++;
1091   quitf->main_level = main_level;
1092   quitf->function = function;
1093   quitf->marshal = marshal;
1094   quitf->data = data;
1095   quitf->destroy = destroy;
1096
1097   quit_functions = g_list_prepend (quit_functions, quitf);
1098   
1099   return quitf->id;
1100 }
1101
1102 static void
1103 gtk_quit_destroy (GtkQuitFunction *quitf)
1104 {
1105   if (quitf->destroy)
1106     quitf->destroy (quitf->data);
1107   g_mem_chunk_free (quit_mem_chunk, quitf);
1108 }
1109
1110 static gint
1111 gtk_quit_destructor (GtkObject **object_p)
1112 {
1113   if (*object_p)
1114     gtk_object_destroy (*object_p);
1115   g_free (object_p);
1116
1117   return FALSE;
1118 }
1119
1120 void
1121 gtk_quit_add_destroy (guint              main_level,
1122                       GtkObject         *object)
1123 {
1124   GtkObject **object_p;
1125
1126   g_return_if_fail (main_level > 0);
1127   g_return_if_fail (object != NULL);
1128   g_return_if_fail (GTK_IS_OBJECT (object));
1129
1130   object_p = g_new (GtkObject*, 1);
1131   *object_p = object;
1132   gtk_signal_connect (object,
1133                       "destroy",
1134                       GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1135                       object_p);
1136   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1137 }
1138
1139 guint
1140 gtk_quit_add (guint       main_level,
1141               GtkFunction function,
1142               gpointer    data)
1143 {
1144   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1145 }
1146
1147 void
1148 gtk_quit_remove (guint id)
1149 {
1150   GtkQuitFunction *quitf;
1151   GList *tmp_list;
1152   
1153   tmp_list = quit_functions;
1154   while (tmp_list)
1155     {
1156       quitf = tmp_list->data;
1157       
1158       if (quitf->id == id)
1159         {
1160           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1161           g_list_free (tmp_list);
1162           gtk_quit_destroy (quitf);
1163           
1164           return;
1165         }
1166       
1167       tmp_list = tmp_list->next;
1168     }
1169 }
1170
1171 void
1172 gtk_quit_remove_by_data (gpointer data)
1173 {
1174   GtkQuitFunction *quitf;
1175   GList *tmp_list;
1176   
1177   tmp_list = quit_functions;
1178   while (tmp_list)
1179     {
1180       quitf = tmp_list->data;
1181       
1182       if (quitf->data == data)
1183         {
1184           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1185           g_list_free (tmp_list);
1186           gtk_quit_destroy (quitf);
1187
1188           return;
1189         }
1190       
1191       tmp_list = tmp_list->next;
1192     }
1193 }
1194
1195 guint
1196 gtk_timeout_add_full (guint32            interval,
1197                       GtkFunction        function,
1198                       GtkCallbackMarshal marshal,
1199                       gpointer           data,
1200                       GtkDestroyNotify   destroy)
1201 {
1202   if (marshal)
1203     {
1204       GtkClosure *closure;
1205
1206       closure = g_new (GtkClosure, 1);
1207       closure->marshal = marshal;
1208       closure->data = data;
1209       closure->destroy = destroy;
1210
1211       return g_timeout_add_full (0, interval, 
1212                                  gtk_invoke_idle_timeout,
1213                                  closure,
1214                                  gtk_destroy_closure);
1215     }
1216   else
1217     return g_timeout_add_full (0, interval, function, data, destroy);
1218 }
1219
1220 guint
1221 gtk_timeout_add (guint32     interval,
1222                  GtkFunction function,
1223                  gpointer    data)
1224 {
1225   return g_timeout_add_full (0, interval, function, data, NULL);
1226 }
1227
1228 void
1229 gtk_timeout_remove (guint tag)
1230 {
1231   g_source_remove (tag);
1232 }
1233
1234 guint
1235 gtk_idle_add_full (gint                 priority,
1236                    GtkFunction          function,
1237                    GtkCallbackMarshal   marshal,
1238                    gpointer             data,
1239                    GtkDestroyNotify     destroy)
1240 {
1241   if (marshal)
1242     {
1243       GtkClosure *closure;
1244
1245       closure = g_new (GtkClosure, 1);
1246       closure->marshal = marshal;
1247       closure->data = data;
1248       closure->destroy = destroy;
1249
1250       return g_idle_add_full (priority,
1251                               gtk_invoke_idle_timeout,
1252                               closure,
1253                               gtk_destroy_closure);
1254     }
1255   else
1256     return g_idle_add_full (priority, function, data, destroy);
1257 }
1258
1259 guint
1260 gtk_idle_add (GtkFunction function,
1261               gpointer    data)
1262 {
1263   return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1264 }
1265
1266 guint       
1267 gtk_idle_add_priority   (gint               priority,
1268                          GtkFunction        function,
1269                          gpointer           data)
1270 {
1271   return g_idle_add_full (priority, function, data, NULL);
1272 }
1273
1274 void
1275 gtk_idle_remove (guint tag)
1276 {
1277   g_source_remove (tag);
1278 }
1279
1280 void
1281 gtk_idle_remove_by_data (gpointer data)
1282 {
1283   if (!g_idle_remove_by_data (data))
1284     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1285 }
1286
1287 guint
1288 gtk_input_add_full (gint                source,
1289                     GdkInputCondition   condition,
1290                     GdkInputFunction    function,
1291                     GtkCallbackMarshal  marshal,
1292                     gpointer            data,
1293                     GtkDestroyNotify    destroy)
1294 {
1295   if (marshal)
1296     {
1297       GtkClosure *closure;
1298
1299       closure = g_new (GtkClosure, 1);
1300       closure->marshal = marshal;
1301       closure->data = data;
1302       closure->destroy = destroy;
1303
1304       return gdk_input_add_full (source,
1305                                  condition,
1306                                  (GdkInputFunction) gtk_invoke_input,
1307                                  closure,
1308                                  (GdkDestroyNotify) gtk_destroy_closure);
1309     }
1310   else
1311     return gdk_input_add_full (source, condition, function, data, destroy);
1312 }
1313
1314 void
1315 gtk_input_remove (guint tag)
1316 {
1317   g_source_remove (tag);
1318 }
1319
1320 static void
1321 gtk_destroy_closure (gpointer data)
1322 {
1323   GtkClosure *closure = data;
1324
1325   if (closure->destroy)
1326     (closure->destroy) (closure->data);
1327   g_free (closure);
1328 }
1329
1330 static gboolean
1331 gtk_invoke_idle_timeout (gpointer data)
1332 {
1333   GtkClosure *closure = data;
1334
1335   GtkArg args[1];
1336   gint ret_val = FALSE;
1337   args[0].name = NULL;
1338   args[0].type = GTK_TYPE_BOOL;
1339   args[0].d.pointer_data = &ret_val;
1340   closure->marshal (NULL, closure->data,  0, args);
1341   return ret_val;
1342 }
1343
1344 static void
1345 gtk_invoke_input (gpointer          data,
1346                   gint              source,
1347                   GdkInputCondition condition)
1348 {
1349   GtkClosure *closure = data;
1350
1351   GtkArg args[3];
1352   args[0].type = GTK_TYPE_INT;
1353   args[0].name = NULL;
1354   GTK_VALUE_INT(args[0]) = source;
1355   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1356   args[1].name = NULL;
1357   GTK_VALUE_FLAGS(args[1]) = condition;
1358   args[2].type = GTK_TYPE_NONE;
1359   args[2].name = NULL;
1360
1361   closure->marshal (NULL, closure->data, 2, args);
1362 }
1363
1364 /**
1365  * gtk_get_current_event:
1366  * 
1367  * Obtains a copy of the event currently being processed by GTK+.  For
1368  * example, if you get a "clicked" signal from #GtkButton, the current
1369  * event will be the #GdkEventButton that triggered the "clicked"
1370  * signal. The returned event must be freed with gdk_event_free().
1371  * If there is no current event, the function returns %NULL.
1372  * 
1373  * Return value: a copy of the current event, or %NULL if no current event.
1374  **/
1375 GdkEvent*
1376 gtk_get_current_event (void)
1377 {
1378   if (current_events)
1379     return gdk_event_copy (current_events->data);
1380   else
1381     return NULL;
1382 }
1383
1384 /**
1385  * gtk_get_current_event_time:
1386  * 
1387  * If there is a current event and it has a timestamp, return that
1388  * timestamp, otherwise return %GDK_CURRENT_TIME.
1389  * 
1390  * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1391  **/
1392 guint32
1393 gtk_get_current_event_time (void)
1394 {
1395   if (current_events)
1396     return gdk_event_get_time (current_events->data);
1397   else
1398     return GDK_CURRENT_TIME;
1399 }
1400
1401 /**
1402  * gtk_get_current_event_state:
1403  * @state: a location to store the state of the current event
1404  * 
1405  * If there is a current event and it has a state field, place
1406  * that state field in @state and return %TRUE, otherwise return
1407  * %FALSE.
1408  * 
1409  * Return value: %TRUE if there was a current event and it had a state field
1410  **/
1411 gboolean
1412 gtk_get_current_event_state (GdkModifierType *state)
1413 {
1414   g_return_val_if_fail (state != NULL, FALSE);
1415   
1416   if (current_events)
1417     return gdk_event_get_state (current_events->data, state);
1418   else
1419     {
1420       *state = 0;
1421       return FALSE;
1422     }
1423 }
1424
1425 /**
1426  * gtk_get_event_widget:
1427  * @event: a #GdkEvent
1428  *
1429  * If @event is %NULL or the event was not associated with any widget,
1430  * returns %NULL, otherwise returns the widget that received the event
1431  * originally.
1432  * 
1433  * Return value: the widget that originally received @event, or %NULL
1434  **/
1435 GtkWidget*
1436 gtk_get_event_widget (GdkEvent *event)
1437 {
1438   GtkWidget *widget;
1439
1440   widget = NULL;
1441   if (event && event->any.window)
1442     gdk_window_get_user_data (event->any.window, (void**) &widget);
1443   
1444   return widget;
1445 }
1446
1447 static void
1448 gtk_exit_func (void)
1449 {
1450   if (gtk_initialized)
1451     {
1452       gtk_initialized = FALSE;
1453     }
1454 }
1455
1456
1457 static gint
1458 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1459 {
1460   if (!quitf->marshal)
1461     return quitf->function (quitf->data);
1462   else
1463     {
1464       GtkArg args[1];
1465       gint ret_val = FALSE;
1466
1467       args[0].name = NULL;
1468       args[0].type = GTK_TYPE_BOOL;
1469       args[0].d.pointer_data = &ret_val;
1470       ((GtkCallbackMarshal) quitf->marshal) (NULL,
1471                                              quitf->data,
1472                                              0, args);
1473       return ret_val;
1474     }
1475 }
1476
1477 /**
1478  * gtk_propagate_event:
1479  * @widget: a #GtkWidget
1480  * @event: an event
1481  *
1482  * Sends an event to a widget, propagating the event to parent widgets
1483  * if the event remains unhandled. Events received by GTK+ from GDK
1484  * normally begin in gtk_main_do_event(). Depending on the type of
1485  * event, existence of modal dialogs, grabs, etc., the event may be
1486  * propagated; if so, this function is used. gtk_propagate_event()
1487  * calls gtk_widget_event() on each widget it decides to send the
1488  * event to.  So gtk_widget_event() is the lowest-level function; it
1489  * simply emits the "event" and possibly an event-specific signal on a
1490  * widget.  gtk_propagate_event() is a bit higher-level, and
1491  * gtk_main_do_event() is the highest level.
1492  *
1493  * All that said, you most likely don't want to use any of these
1494  * functions; synthesizing events is rarely needed. Consider asking on
1495  * the mailing list for better ways to achieve your goals. For
1496  * example, use gdk_window_invalidate_rect() or
1497  * gtk_widget_queue_draw() instead of making up expose events.
1498  * 
1499  **/
1500 void
1501 gtk_propagate_event (GtkWidget *widget,
1502                      GdkEvent  *event)
1503 {
1504   gint handled_event;
1505   
1506   g_return_if_fail (widget != NULL);
1507   g_return_if_fail (GTK_IS_WIDGET (widget));
1508   g_return_if_fail (event != NULL);
1509   
1510   handled_event = FALSE;
1511
1512   gtk_widget_ref (widget);
1513       
1514   if ((event->type == GDK_KEY_PRESS) ||
1515       (event->type == GDK_KEY_RELEASE))
1516     {
1517       /* Only send key events within Window widgets to the Window
1518        *  The Window widget will in turn pass the
1519        *  key event on to the currently focused widget
1520        *  for that window.
1521        */
1522       GtkWidget *window;
1523
1524       window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1525
1526       if (window)
1527         {
1528           /* If there is a grab within the window, give the grab widget
1529            * a first crack at the key event
1530            */
1531           if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1532             handled_event = gtk_widget_event (widget, event);
1533           
1534           if (!handled_event)
1535             {
1536               window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1537               if (window)
1538                 {
1539                   if (GTK_WIDGET_IS_SENSITIVE (window))
1540                     gtk_widget_event (window, event);
1541                 }
1542             }
1543                   
1544           handled_event = TRUE; /* don't send to widget */
1545         }
1546     }
1547   
1548   /* Other events get propagated up the widget tree
1549    *  so that parents can see the button and motion
1550    *  events of the children.
1551    */
1552   if (!handled_event)
1553     {
1554       while (TRUE)
1555         {
1556           GtkWidget *tmp;
1557           
1558           handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1559           tmp = widget->parent;
1560           gtk_widget_unref (widget);
1561
1562           widget = tmp;
1563           
1564           if (!handled_event && widget)
1565             gtk_widget_ref (widget);
1566           else
1567             break;
1568         }
1569     }
1570   else
1571     gtk_widget_unref (widget);
1572 }
1573
1574 #if 0
1575 static void
1576 gtk_error (gchar *str)
1577 {
1578   gtk_print (str);
1579 }
1580
1581 static void
1582 gtk_warning (gchar *str)
1583 {
1584   gtk_print (str);
1585 }
1586
1587 static void
1588 gtk_message (gchar *str)
1589 {
1590   gtk_print (str);
1591 }
1592
1593 static void
1594 gtk_print (gchar *str)
1595 {
1596   static GtkWidget *window = NULL;
1597   static GtkWidget *text;
1598   static int level = 0;
1599   GtkWidget *box1;
1600   GtkWidget *box2;
1601   GtkWidget *table;
1602   GtkWidget *hscrollbar;
1603   GtkWidget *vscrollbar;
1604   GtkWidget *separator;
1605   GtkWidget *button;
1606   
1607   if (level > 0)
1608     {
1609       fputs (str, stdout);
1610       fflush (stdout);
1611       return;
1612     }
1613   
1614   if (!window)
1615     {
1616       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1617       
1618       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1619                           (GtkSignalFunc) gtk_widget_destroyed,
1620                           &window);
1621       
1622       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1623       
1624       box1 = gtk_vbox_new (FALSE, 0);
1625       gtk_container_add (GTK_CONTAINER (window), box1);
1626       gtk_widget_show (box1);
1627       
1628       
1629       box2 = gtk_vbox_new (FALSE, 10);
1630       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1631       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1632       gtk_widget_show (box2);
1633       
1634       
1635       table = gtk_table_new (2, 2, FALSE);
1636       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1637       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1638       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1639       gtk_widget_show (table);
1640       
1641       text = gtk_text_new (NULL, NULL);
1642       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1643       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1644       gtk_widget_show (text);
1645       gtk_widget_realize (text);
1646       
1647       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1648       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1649                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1650       gtk_widget_show (hscrollbar);
1651       
1652       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1653       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1654                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1655       gtk_widget_show (vscrollbar);
1656       
1657       separator = gtk_hseparator_new ();
1658       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1659       gtk_widget_show (separator);
1660       
1661       
1662       box2 = gtk_vbox_new (FALSE, 10);
1663       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1664       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1665       gtk_widget_show (box2);
1666       
1667       
1668       button = gtk_button_new_with_label ("close");
1669       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1670                                  (GtkSignalFunc) gtk_widget_hide,
1671                                  GTK_OBJECT (window));
1672       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1673       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1674       gtk_widget_grab_default (button);
1675       gtk_widget_show (button);
1676     }
1677   
1678   level += 1;
1679   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1680   level -= 1;
1681   
1682   if (!GTK_WIDGET_VISIBLE (window))
1683     gtk_widget_show (window);
1684 }
1685 #endif
1686
1687 gboolean
1688 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1689                                   GValue                *return_accu,
1690                                   const GValue          *handler_return,
1691                                   gpointer               dummy)
1692 {
1693   gboolean val;
1694
1695   val = g_value_get_boolean (handler_return);
1696   g_value_set_boolean (return_accu, val);
1697   
1698   return !val;
1699 }