]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
On Win32, look explicitly for LANG, LC_ALL and LC_CTYPE, and if not found,
[~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 #include "gdkconfig.h"
28
29 #include <locale.h>
30
31 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
32 #include <libintl.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <gmodule.h>
39 #ifdef G_OS_UNIX
40 #include <unistd.h>
41 #include <sys/types.h>          /* For uid_t, gid_t */
42 #endif
43 #ifdef G_OS_WIN32
44 #define STRICT
45 #include <windows.h>
46 #undef STRICT
47 #endif
48
49 #include <pango/pango-utils.h>  /* For pango_split_file_list */
50
51 #include "gtkaccelmap.h"
52 #include "gtkbox.h"
53 #include "gtkdnd.h"
54 #include "gtkversion.h"
55 #include "gtkmain.h"
56 #include "gtkrc.h"
57 #include "gtkselection.h"
58 #include "gtksettings.h"
59 #include "gtksignal.h"
60 #include "gtkwidget.h"
61 #include "gtkwindow.h"
62 #include "gtkprivate.h"
63 #include "config.h"
64 #include "gtkdebug.h"
65 #include "gtkintl.h"
66
67 /* Private type definitions
68  */
69 typedef struct _GtkInitFunction          GtkInitFunction;
70 typedef struct _GtkQuitFunction          GtkQuitFunction;
71 typedef struct _GtkClosure               GtkClosure;
72 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
73 typedef struct _GtkModuleInfo            GtkModuleInfo;
74
75 struct _GtkInitFunction
76 {
77   GtkFunction function;
78   gpointer data;
79 };
80
81 struct _GtkQuitFunction
82 {
83   guint id;
84   guint main_level;
85   GtkCallbackMarshal marshal;
86   GtkFunction function;
87   gpointer data;
88   GtkDestroyNotify destroy;
89 };
90
91 struct _GtkClosure
92 {
93   GtkCallbackMarshal marshal;
94   gpointer data;
95   GtkDestroyNotify destroy;
96 };
97
98 struct _GtkKeySnooperData
99 {
100   GtkKeySnoopFunc func;
101   gpointer func_data;
102   guint id;
103 };
104
105 struct _GtkModuleInfo
106 {
107   GtkModuleInitFunc init_func;
108   GtkModuleDisplayInitFunc display_init_func;
109 };
110
111 static gint  gtk_quit_invoke_function    (GtkQuitFunction    *quitf);
112 static void  gtk_quit_destroy            (GtkQuitFunction    *quitf);
113 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
114                                           GdkEvent           *event);
115
116 static void     gtk_destroy_closure      (gpointer            data);
117 static gboolean gtk_invoke_idle_timeout  (gpointer            data);
118 static void     gtk_invoke_input         (gpointer            data,
119                                           gint                source,
120                                           GdkInputCondition   condition);
121
122 #if 0
123 static void  gtk_error                   (gchar              *str);
124 static void  gtk_warning                 (gchar              *str);
125 static void  gtk_message                 (gchar              *str);
126 static void  gtk_print                   (gchar              *str);
127 #endif
128
129 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);
130
131 const guint gtk_major_version = GTK_MAJOR_VERSION;
132 const guint gtk_minor_version = GTK_MINOR_VERSION;
133 const guint gtk_micro_version = GTK_MICRO_VERSION;
134 const guint gtk_binary_age = GTK_BINARY_AGE;
135 const guint gtk_interface_age = GTK_INTERFACE_AGE;
136
137 static GSList *gtk_modules;
138
139 /* Saved argc,argv for delayed module initialization
140  */
141 static gint    gtk_argc = 0;
142 static gchar **gtk_argv = NULL;
143
144 static guint gtk_main_loop_level = 0;
145 static gint gtk_initialized = FALSE;
146 static GList *current_events = NULL;
147
148 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
149
150 static GList *init_functions = NULL;       /* A list of init functions.
151                                             */
152 static GList *quit_functions = NULL;       /* A list of quit functions.
153                                             */
154 static GMemChunk *quit_mem_chunk = NULL;
155
156 static GSList *key_snoopers = NULL;
157
158 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
159
160 #ifdef G_ENABLE_DEBUG
161 static const GDebugKey gtk_debug_keys[] = {
162   {"misc", GTK_DEBUG_MISC},
163   {"plugsocket", GTK_DEBUG_PLUGSOCKET},
164   {"text", GTK_DEBUG_TEXT},
165   {"tree", GTK_DEBUG_TREE},
166   {"updates", GTK_DEBUG_UPDATES},
167   {"keybindings", GTK_DEBUG_KEYBINDINGS},
168   {"multihead", GTK_DEBUG_MULTIHEAD}
169 };
170
171 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
172
173 #endif /* G_ENABLE_DEBUG */
174
175 gchar*
176 gtk_check_version (guint required_major,
177                    guint required_minor,
178                    guint required_micro)
179 {
180   gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
181   gint required_effective_micro = 100 * required_minor + required_micro;
182
183   if (required_major > GTK_MAJOR_VERSION)
184     return "Gtk+ version too old (major mismatch)";
185   if (required_major < GTK_MAJOR_VERSION)
186     return "Gtk+ version too new (major mismatch)";
187   if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
188     return "Gtk+ version too new (micro mismatch)";
189   if (required_effective_micro > gtk_effective_micro)
190     return "Gtk+ version too old (micro mismatch)";
191   return NULL;
192 }
193
194 /* This checks to see if the process is running suid or sgid
195  * at the current time. If so, we don't allow GTK+ to be initialized.
196  * This is meant to be a mild check - we only error out if we
197  * can prove the programmer is doing something wrong, not if
198  * they could be doing something wrong. For this reason, we
199  * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
200  */
201 static gboolean
202 check_setugid (void)
203 {
204 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
205 #ifndef G_OS_WIN32
206   uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
207   gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
208   
209 #ifdef HAVE_GETRESUID
210   /* These aren't in the header files, so we prototype them here.
211    */
212   int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
213   int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
214
215   if (getresuid (&ruid, &euid, &suid) != 0 ||
216       getresgid (&rgid, &egid, &sgid) != 0)
217 #endif /* HAVE_GETRESUID */
218     {
219       suid = ruid = getuid ();
220       sgid = rgid = getgid ();
221       euid = geteuid ();
222       egid = getegid ();
223     }
224
225   if (ruid != euid || ruid != suid ||
226       rgid != egid || rgid != sgid)
227     {
228       g_warning ("This process is currently running setuid or setgid.\n"
229                  "This is not a supported use of GTK+. You must create a helper\n"
230                  "program instead. For further details, see:\n\n"
231                  "    http://www.gtk.org/setuid.html\n\n"
232                  "Refusing to initialize GTK+.");
233       exit (1);
234     }
235 #endif
236   return TRUE;
237 }
238
239 #ifdef G_OS_WIN32
240
241 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
242
243 const gchar *
244 _gtk_get_libdir (void)
245 {
246   static char *gtk_libdir = NULL;
247   if (gtk_libdir == NULL)
248     gtk_libdir = g_win32_get_package_installation_subdirectory
249       (GETTEXT_PACKAGE, dll_name, "lib");
250
251   return gtk_libdir;
252 }
253
254 const gchar *
255 _gtk_get_localedir (void)
256 {
257   static char *gtk_localedir = NULL;
258   if (gtk_localedir == NULL)
259     gtk_localedir = g_win32_get_package_installation_subdirectory
260       (GETTEXT_PACKAGE, dll_name, "lib\\locale");
261
262   return gtk_localedir;
263 }
264
265 const gchar *
266 _gtk_get_sysconfdir (void)
267 {
268   static char *gtk_sysconfdir = NULL;
269   if (gtk_sysconfdir == NULL)
270     gtk_sysconfdir = g_win32_get_package_installation_subdirectory
271       (GETTEXT_PACKAGE, dll_name, "etc");
272
273   return gtk_sysconfdir;
274 }
275
276 const gchar *
277 _gtk_get_data_prefix (void)
278 {
279   static char *gtk_data_prefix = NULL;
280   if (gtk_data_prefix == NULL)
281     gtk_data_prefix = g_win32_get_package_installation_directory
282       (GETTEXT_PACKAGE, dll_name);
283
284   return gtk_data_prefix;
285 }
286
287 #endif /* G_OS_WIN32 */
288
289 static gchar **
290 get_module_path (void)
291 {
292   const gchar *module_path_env;
293   const gchar *exe_prefix;
294   const gchar *home_dir;
295   gchar *home_gtk_dir = NULL;
296   gchar *module_path;
297   gchar *default_dir;
298   static gchar **result = NULL;
299
300   if (result)
301     return result;
302
303   home_dir = g_get_home_dir();
304   if (home_dir)
305     home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);
306
307   module_path_env = g_getenv ("GTK_PATH");
308   exe_prefix = g_getenv ("GTK_EXE_PREFIX");
309
310   if (exe_prefix)
311     default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
312   else
313     default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);
314
315   if (module_path_env && home_gtk_dir)
316     module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
317                                 module_path_env, home_gtk_dir, default_dir, NULL);
318   else if (module_path_env)
319     module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
320                                 module_path_env, default_dir, NULL);
321   else if (home_gtk_dir)
322     module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
323                                 home_gtk_dir, default_dir, NULL);
324   else
325     module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
326                                 default_dir, NULL);
327
328   g_free (home_gtk_dir);
329   g_free (default_dir);
330
331   result = pango_split_file_list (module_path);
332   g_free (module_path);
333
334   return result;
335 }
336
337 /**
338  * _gtk_get_module_path:
339  * @type: the type of the module, for instance 'modules', 'engines', immodules'
340  * 
341  * Determines the search path for a particular type of module.
342  * 
343  * Return value: the search path for the module type. Free with g_strfreev().
344  **/
345 gchar **
346 _gtk_get_module_path (const gchar *type)
347 {
348   gchar **paths = get_module_path();
349   gchar **path;
350   gchar **result;
351   gint count = 0;
352
353   for (path = paths; *path; path++)
354     count++;
355
356   result = g_new (gchar *, count * 4 + 1);
357
358   count = 0;
359   for (path = get_module_path (); *path; path++)
360     {
361       gint use_version, use_host;
362       
363       for (use_version = TRUE; use_version >= FALSE; use_version--)
364         for (use_host = TRUE; use_host >= FALSE; use_host--)
365           {
366             gchar *tmp_dir;
367             
368             if (use_version && use_host)
369               tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
370             else if (use_version)
371               tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
372             else if (use_host)
373               tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
374             else
375               tmp_dir = g_build_filename (*path, type, NULL);
376
377             result[count++] = tmp_dir;
378           }
379     }
380
381   result[count++] = NULL;
382
383   return result;
384 }
385
386 /* Like g_module_path, but use .la as the suffix
387  */
388 static gchar*
389 module_build_la_path (const gchar *directory,
390                       const gchar *module_name)
391 {
392         gchar *filename;
393         gchar *result;
394         
395         if (strncmp (module_name, "lib", 3) == 0)
396                 filename = (gchar *)module_name;
397         else
398                 filename =  g_strconcat ("lib", module_name, ".la", NULL);
399
400         if (directory && *directory)
401                 result = g_build_filename (directory, filename, NULL);
402         else
403                 result = g_strdup (filename);
404
405         if (filename != module_name)
406                 g_free (filename);
407
408         return result;
409 }
410
411 /**
412  * _gtk_find_module:
413  * @name: the name of the module
414  * @type: the type of the module, for instance 'modules', 'engines', immodules'
415  * 
416  * Looks for a dynamically module named @name of type @type in the standard GTK+
417  *  module search path.
418  * 
419  * Return value: the pathname to the found module, or %NULL if it wasn't found.
420  *  Free with g_free().
421  **/
422 gchar *
423 _gtk_find_module (const gchar *name,
424                   const gchar *type)
425 {
426   gchar **paths;
427   gchar **path;
428   gchar *module_name = NULL;
429
430   if (g_path_is_absolute (name))
431     return g_strdup (name);
432
433   paths = _gtk_get_module_path (type);
434   for (path = paths; *path; path++)
435     {
436       gchar *tmp_name;
437
438       tmp_name = g_module_build_path (*path, name);
439       if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
440         {
441           module_name = tmp_name;
442           goto found;
443         }
444       g_free(tmp_name);
445
446       tmp_name = module_build_la_path (*path, name);
447       if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
448         {
449           module_name = tmp_name;
450           goto found;
451         }
452       g_free(tmp_name);
453     }
454
455  found:
456   g_strfreev (paths);
457   return module_name;
458 }
459
460 static GModule *
461 find_module (const gchar *name)
462 {
463   GModule *module;
464   gchar *module_name;
465
466   module_name = _gtk_find_module (name, "modules");
467   if (!module_name)
468     {
469       /* As last resort, try loading without an absolute path (using system
470        * library path)
471        */
472       module_name = g_module_build_path (NULL, name);
473     }
474   
475   module = g_module_open (module_name, G_MODULE_BIND_LAZY);
476   g_free(module_name);
477
478   return module;
479 }
480
481 static GSList *
482 load_module (GSList      *module_list,
483              const gchar *name)
484 {
485   GtkModuleInitFunc modinit_func = NULL;
486   GtkModuleInfo *info;
487   GModule *module = NULL;
488   
489   if (g_module_supported ())
490     {
491       module = find_module (name);
492       if (module &&
493           g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
494           modinit_func)
495         {
496           if (!g_slist_find (module_list, (gconstpointer) modinit_func))
497             {
498               g_module_make_resident (module);
499               info = g_new (GtkModuleInfo, 1);
500
501               info->init_func = modinit_func;
502               g_module_symbol (module, "gtk_module_display_init",
503                                (gpointer *) &info->display_init_func);
504               
505               module_list = g_slist_prepend (module_list, info);
506             }
507           else
508             {
509               g_module_close (module);
510               module = NULL;
511             }
512         }
513     }
514   if (!modinit_func)
515     {
516       g_message ("Failed to load module \"%s\": %s",
517                  module ? g_module_name (module) : name,
518                  g_module_error ());
519       if (module)
520         g_module_close (module);
521     }
522   
523   return module_list;
524 }
525
526 static GSList *
527 load_modules (const char *module_str)
528 {
529   gchar **module_names = pango_split_file_list (module_str);
530   GSList *module_list = NULL;
531   gint i;
532   
533   for (i = 0; module_names[i]; i++)
534     module_list = load_module (module_list, module_names[i]);
535   
536   module_list = g_slist_reverse (module_list);
537   
538   g_strfreev (module_names);
539
540   return module_list;
541 }
542
543 static gboolean do_setlocale = TRUE;
544
545 /**
546  * gtk_disable_setlocale:
547  * 
548  * Prevents gtk_init() and gtk_init_check() from automatically
549  * calling <literal>setlocale (LC_ALL, "")</literal>. You would 
550  * want to use this function if you wanted to set the locale for 
551  * your program to something other than the user's locale, or if 
552  * you wanted to set different values for different locale categories.
553  *
554  * Most programs should not need to call this function.
555  **/
556 void
557 gtk_disable_setlocale (void)
558 {
559   if (gtk_initialized)
560     g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
561     
562   do_setlocale = FALSE;
563 }
564
565 #undef gtk_init_check
566
567 static void
568 default_display_notify_cb (GdkDisplayManager *display_manager)
569 {
570   GSList *slist;
571
572   /* Initialize non-multihead-aware modules when the
573    * default display is first set to a non-NULL value.
574    */
575   static gboolean initialized = FALSE;
576
577   if (!gdk_display_get_default () || initialized)
578     return;
579
580   initialized = TRUE;
581
582   for (slist = gtk_modules; slist; slist = slist->next)
583     {
584       if (slist->data)
585         {
586           GtkModuleInfo *info = slist->data;
587
588           if (!info->display_init_func)
589             info->init_func (&gtk_argc, &gtk_argv);
590         }
591     }
592 }
593
594 static void
595 display_opened_cb (GdkDisplayManager *display_manager,
596                    GdkDisplay        *display)
597 {
598   GSList *slist;
599   
600   for (slist = gtk_modules; slist; slist = slist->next)
601     {
602       if (slist->data)
603         {
604           GtkModuleInfo *info = slist->data;
605
606           if (info->display_init_func)
607             info->display_init_func (display);
608         }
609     }
610 }
611
612 /**
613  * gdk_parse_args:
614  * @argc: the number of command line arguments.
615  * @argv: the array of command line arguments.
616  * 
617  * Parses command line arguments, and initializes global
618  * attributes of GTK+, but does not actually open a connection
619  * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
620  *
621  * Any arguments used by GTK or GDK are removed from the array and
622  * @argc and @argv are updated accordingly.
623  *
624  * You shouldn't call this function explicitely if you are using
625  * gtk_init(), or gtk_init_check().
626  *
627  * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
628  **/
629 gboolean
630 gtk_parse_args (int    *argc,
631                 char ***argv)
632 {
633   GString *gtk_modules_string = NULL;
634   GSList *slist;
635   GdkDisplayManager *display_manager;
636   const gchar *env_string;
637
638   if (gtk_initialized)
639     return TRUE;
640
641   if (!check_setugid ())
642     return FALSE;
643   
644 #if     0
645   g_set_error_handler (gtk_error);
646   g_set_warning_handler (gtk_warning);
647   g_set_message_handler (gtk_message);
648   g_set_print_handler (gtk_print);
649 #endif
650
651   if (do_setlocale)
652     {
653       if (!setlocale (LC_ALL, ""))
654         g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
655     }
656
657   gdk_parse_args (argc, argv);
658   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
659   
660 #ifdef G_ENABLE_DEBUG
661   env_string = g_getenv ("GTK_DEBUG");
662   if (env_string != NULL)
663     {
664       gtk_debug_flags = g_parse_debug_string (env_string,
665                                               gtk_debug_keys,
666                                               gtk_ndebug_keys);
667       env_string = NULL;
668     }
669 #endif  /* G_ENABLE_DEBUG */
670
671   env_string = g_getenv ("GTK_MODULES");
672   if (env_string)
673     gtk_modules_string = g_string_new (env_string);
674
675   if (argc && argv)
676     {
677       gint i, j, k;
678       
679       for (i = 1; i < *argc;)
680         {
681           if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
682               strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
683             {
684               gchar *module_name = (*argv)[i] + 12;
685               
686               if (*module_name == '=')
687                 module_name++;
688               else if (i + 1 < *argc)
689                 {
690                   (*argv)[i] = NULL;
691                   i += 1;
692                   module_name = (*argv)[i];
693                 }
694               (*argv)[i] = NULL;
695
696               if (module_name && *module_name)
697                 {
698                   if (gtk_modules_string)
699                     g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
700                   else
701                     gtk_modules_string = g_string_new (NULL);
702
703                   g_string_append (gtk_modules_string, module_name);
704                 }
705             }
706           else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
707             {
708               GLogLevelFlags fatal_mask;
709               
710               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
711               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
712               g_log_set_always_fatal (fatal_mask);
713               (*argv)[i] = NULL;
714             }
715 #ifdef G_ENABLE_DEBUG
716           else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
717                    (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
718             {
719               gchar *equal_pos = strchr ((*argv)[i], '=');
720               
721               if (equal_pos != NULL)
722                 {
723                   gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
724                                                            gtk_debug_keys,
725                                                            gtk_ndebug_keys);
726                 }
727               else if ((i + 1) < *argc && (*argv)[i + 1])
728                 {
729                   gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
730                                                            gtk_debug_keys,
731                                                            gtk_ndebug_keys);
732                   (*argv)[i] = NULL;
733                   i += 1;
734                 }
735               (*argv)[i] = NULL;
736             }
737           else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
738                    (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
739             {
740               gchar *equal_pos = strchr ((*argv)[i], '=');
741               
742               if (equal_pos != NULL)
743                 {
744                   gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
745                                                             gtk_debug_keys,
746                                                             gtk_ndebug_keys);
747                 }
748               else if ((i + 1) < *argc && (*argv)[i + 1])
749                 {
750                   gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
751                                                             gtk_debug_keys,
752                                                             gtk_ndebug_keys);
753                   (*argv)[i] = NULL;
754                   i += 1;
755                 }
756               (*argv)[i] = NULL;
757             }
758 #endif /* G_ENABLE_DEBUG */
759           i += 1;
760         }
761       
762       for (i = 1; i < *argc; i++)
763         {
764           for (k = i; k < *argc; k++)
765             if ((*argv)[k] != NULL)
766               break;
767           
768           if (k > i)
769             {
770               k -= i;
771               for (j = i + k; j < *argc; j++)
772                 (*argv)[j-k] = (*argv)[j];
773               *argc -= k;
774             }
775         }
776
777       gtk_argv = g_malloc ((gtk_argc + 1) * sizeof (char*));
778       for (i = 0; i < gtk_argc; i++)
779         gtk_argv[i] = g_strdup ((*argv)[i]);
780       gtk_argv[gtk_argc] = NULL;
781     }
782
783   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
784     gdk_window_set_debug_updates (TRUE);
785
786   /* load gtk modules */
787   if (gtk_modules_string)
788     {
789       gtk_modules = load_modules (gtk_modules_string->str);
790       g_string_free (gtk_modules_string, TRUE);
791     }
792
793 #ifdef ENABLE_NLS
794   bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
795 #    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
796   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
797 #    endif
798 #endif  
799
800   {
801   /* Translate to default:RTL if you want your widgets
802    * to be RTL, otherwise translate to default:LTR.
803    * Do *not* translate it to "predefinito:LTR", if it
804    * it isn't default:LTR or default:RTL it will not work 
805    */
806     char *e = _("default:LTR");
807     if (strcmp (e, "default:RTL")==0) {
808       gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
809     } else if (strcmp (e, "default:LTR")) {
810       g_warning ("Whoever translated default:LTR did so wrongly.\n");
811     }
812   }
813
814   gtk_type_init (0);
815   _gtk_accel_map_init ();  
816   _gtk_rc_init ();
817   
818   /* Set the 'initialized' flag.
819    */
820   gtk_initialized = TRUE;
821
822   display_manager = gdk_display_manager_get ();
823   g_signal_connect (display_manager, "notify::default-display",
824                     G_CALLBACK (default_display_notify_cb), NULL);
825   g_signal_connect (display_manager, "display-opened",
826                     G_CALLBACK (display_opened_cb), NULL);
827
828   /* initialize multhead aware gtk modules; for other modules,
829    * we wait until we have a display open;
830    */
831   for (slist = gtk_modules; slist; slist = slist->next)
832     {
833       if (slist->data)
834         {
835           GtkModuleInfo *info = slist->data;
836
837           if (info->display_init_func)
838             info->init_func (argc, argv);
839         }
840     }
841   
842   return TRUE;
843 }
844
845 #undef gtk_init_check
846
847 /**
848  * gtk_init_check:
849  * @argc: Address of the <parameter>argc</parameter> parameter of your 
850  *   <function>main()</function> function. Changed if any arguments were 
851  *   handled.
852  * @argv: Address of the <parameter>argv</parameter> parameter of 
853  *   <function>main()</function>. Any parameters understood by gtk_init() 
854  *   are stripped before return.
855  * 
856  * This function does the same work as gtk_init() with only 
857  * a single change: It does not terminate the program if the GUI can't be 
858  * initialized. Instead it returns %FALSE on failure.
859  *
860  * This way the application can fall back to some other means of communication 
861  * with the user - for example a curses or command line interface.
862  * 
863  * Return value: %TRUE if the GUI has been successfully initialized, 
864  *               %FALSE otherwise.
865  **/
866 gboolean
867 gtk_init_check (int      *argc,
868                 char   ***argv)
869 {
870   GdkDisplay *display;
871   
872   if (!gtk_parse_args (argc, argv))
873     return FALSE;
874
875   if (gdk_display_get_default ())
876     return TRUE;
877
878   display = gdk_display_open (gdk_get_display_arg_name ());
879
880   if (display)
881     {
882       gdk_display_manager_set_default_display (gdk_display_manager_get (),
883                                                display);
884       return TRUE;
885     }
886   else
887     return FALSE;
888 }
889
890 #undef gtk_init
891
892 /**
893  * gtk_init:
894  * @argc: Address of the <parameter>argc</parameter> parameter of your 
895  *   <function>main()</function> function. Changed if any arguments were 
896  *   handled.
897  * @argv: Address of the <parameter>argv</parameter> parameter of 
898  *   <function>main()</function>. Any parameters understood by gtk_init() 
899  *   are stripped before return.
900  * 
901  * Call this function before using any other GTK+ functions in your GUI
902  * applications.  It will initialize everything needed to operate the toolkit and
903  * parses some standard command line options. @argc and 
904  * @argv are adjusted accordingly so your own code will 
905  * never see those standard arguments.
906  *
907  * <note><para>
908  * This function will terminate your program if it was unable to initialize 
909  * the GUI for some reason. If you want your program to fall back to a 
910  * textual interface you want to call gtk_init_check() instead.
911  * </para></note>
912  **/
913 void
914 gtk_init (int *argc, char ***argv)
915 {
916   if (!gtk_init_check (argc, argv))
917     {
918       char *display_name_arg = gdk_get_display_arg_name ();
919       g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
920       exit (1);
921     }
922 }
923
924 #ifdef G_PLATFORM_WIN32
925
926 static void
927 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
928 {
929   if (sizeof_GtkWindow != sizeof (GtkWindow))
930     g_error ("Incompatible build!\n"
931              "The code using GTK+ thinks GtkWindow is of different\n"
932              "size than it actually is in this build of GTK+.\n"
933              "On Windows, this probably means that you have compiled\n"
934              "your code with gcc without the -fnative-struct\n"
935              "(or -mms-bitfields) switch, or that you are using\n"
936              "an unsupported compiler.");
937 }
938
939 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
940  * gcc-compiled code on Win32 whether compiled with -fnative-struct or
941  * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
942  * from GTK+ 2.0.2 on, check some other struct, too, where the use of
943  * -fnative-struct still matters. GtkBox is one such.
944  */
945 static void
946 check_sizeof_GtkBox (size_t sizeof_GtkBox)
947 {
948   if (sizeof_GtkBox != sizeof (GtkBox))
949     g_error ("Incompatible build!\n"
950              "The code using GTK+ thinks GtkBox is of different\n"
951              "size than it actually is in this build of GTK+.\n"
952              "On Windows, this probably means that you have compiled\n"
953              "your code with gcc without the -fnative-struct\n"
954              "(or -mms-bitfields) switch, or that you are using\n"
955              "an unsupported compiler.");
956 }
957
958 /* These two functions might get more checks added later, thus pass
959  * in the number of extra args.
960  */
961 void
962 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
963 {
964   check_sizeof_GtkWindow (sizeof_GtkWindow);
965   if (num_checks >= 2)
966     check_sizeof_GtkBox (sizeof_GtkBox);
967   gtk_init (argc, argv);
968 }
969
970 gboolean
971 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
972 {
973   check_sizeof_GtkWindow (sizeof_GtkWindow);
974   if (num_checks >= 2)
975     check_sizeof_GtkBox (sizeof_GtkBox);
976   return gtk_init_check (argc, argv);
977 }
978
979 #endif
980
981 void
982 gtk_exit (gint errorcode)
983 {
984   /* Only if "gtk" has been initialized should we de-initialize.
985    */
986   gdk_exit (errorcode);
987 }
988
989
990 /**
991  * gtk_set_locale:
992  *
993  * Initializes internationalization support for GTK+. gtk_init()
994  * automatically does this, so there is typically no point
995  * in calling this function.
996  *
997  * If you are calling this function because you changed the locale
998  * after GTK+ is was initialized, then calling this function
999  * may help a bit. (Note, however, that changing the locale
1000  * after GTK+ is initialized may produce inconsistent results and
1001  * is not really supported.)
1002  * 
1003  * In detail - sets the current locale according to the
1004  * program environment. This is the same as calling the C library function
1005  * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the 
1006  * locale specific setup of the windowing system used by GDK.
1007  * 
1008  * Return value: a string corresponding to the locale set, as with the
1009  * C library function <function>setlocale()</function>.
1010  **/
1011 gchar*
1012 gtk_set_locale (void)
1013 {
1014   return gdk_set_locale ();
1015 }
1016
1017 /**
1018  * gtk_get_default_language:
1019  *
1020  * Returns the ISO language code for the default language currently in
1021  * effect. (Note that this can change over the life of an
1022  * application.)  The default language is derived from the current
1023  * locale. It determines, for example, whether GTK+ uses the
1024  * right-to-left or left-to-right text direction.
1025  * 
1026  * Return value: the default language as an allocated string, must be freed
1027  **/
1028 PangoLanguage *
1029 gtk_get_default_language (void)
1030 {
1031   gchar *lang;
1032   PangoLanguage *result;
1033   gchar *p;
1034   
1035 #ifdef G_OS_WIN32
1036   /* Somebody might try to set the locale for this process using the
1037    * LANG or LC_ environment variables. The Microsoft C library
1038    * doesn't know anything about them. You set the locale in the
1039    * Control Panel. Setting these env vars won't have any affect on
1040    * locale-dependent C library functions like ctime. But just for
1041    * kicks, do obey LC_ALL, LANG and LC_CTYPE in GTK. (This also makes
1042    * it easier to test GTK and Pango in various default languages, you
1043    * don't have to clickety-click in the Control Panel, you can simply
1044    * start the program with LC_ALL=something on the command line.)
1045    */
1046   p = getenv ("LC_ALL");
1047   if (p != NULL)
1048     lang = g_strdup (p);
1049   else
1050     {
1051       p = getenv ("LANG");
1052       if (p != NULL)
1053         lang = g_strdup (p);
1054       else
1055         {
1056           p = getenv ("LC_CTYPE");
1057           if (p != NULL)
1058             lang = g_strdup (p);
1059           else
1060             lang = g_win32_getlocale ();
1061         }
1062     }
1063 #else
1064   lang = g_strdup (setlocale (LC_CTYPE, NULL));
1065 #endif
1066   p = strchr (lang, '.');
1067   if (p)
1068     *p = '\0';
1069   p = strchr (lang, '@');
1070   if (p)
1071     *p = '\0';
1072
1073   result = pango_language_from_string (lang);
1074   g_free (lang);
1075   
1076   return result;
1077 }
1078
1079 void
1080 gtk_main (void)
1081 {
1082   GList *tmp_list;
1083   GList *functions;
1084   GtkInitFunction *init;
1085   GMainLoop *loop;
1086
1087   gtk_main_loop_level++;
1088   
1089   loop = g_main_new (TRUE);
1090   main_loops = g_slist_prepend (main_loops, loop);
1091
1092   tmp_list = functions = init_functions;
1093   init_functions = NULL;
1094   
1095   while (tmp_list)
1096     {
1097       init = tmp_list->data;
1098       tmp_list = tmp_list->next;
1099       
1100       (* init->function) (init->data);
1101       g_free (init);
1102     }
1103   g_list_free (functions);
1104
1105   if (g_main_is_running (main_loops->data))
1106     {
1107       GDK_THREADS_LEAVE ();
1108       g_main_run (loop);
1109       GDK_THREADS_ENTER ();
1110       gdk_flush ();
1111     }
1112
1113   if (quit_functions)
1114     {
1115       GList *reinvoke_list = NULL;
1116       GtkQuitFunction *quitf;
1117
1118       while (quit_functions)
1119         {
1120           quitf = quit_functions->data;
1121
1122           tmp_list = quit_functions;
1123           quit_functions = g_list_remove_link (quit_functions, quit_functions);
1124           g_list_free_1 (tmp_list);
1125
1126           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1127               gtk_quit_invoke_function (quitf))
1128             {
1129               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1130             }
1131           else
1132             {
1133               gtk_quit_destroy (quitf);
1134             }
1135         }
1136       if (reinvoke_list)
1137         {
1138           GList *work;
1139           
1140           work = g_list_last (reinvoke_list);
1141           if (quit_functions)
1142             quit_functions->prev = work;
1143           work->next = quit_functions;
1144           quit_functions = work;
1145         }
1146
1147       gdk_flush ();
1148     }
1149               
1150   main_loops = g_slist_remove (main_loops, loop);
1151
1152   g_main_destroy (loop);
1153
1154   gtk_main_loop_level--;
1155 }
1156
1157 guint
1158 gtk_main_level (void)
1159 {
1160   return gtk_main_loop_level;
1161 }
1162
1163 void
1164 gtk_main_quit (void)
1165 {
1166   g_return_if_fail (main_loops != NULL);
1167
1168   g_main_quit (main_loops->data);
1169 }
1170
1171 gint
1172 gtk_events_pending (void)
1173 {
1174   gboolean result;
1175   
1176   GDK_THREADS_LEAVE ();  
1177   result = g_main_pending ();
1178   GDK_THREADS_ENTER ();
1179
1180   return result;
1181 }
1182
1183 gboolean
1184 gtk_main_iteration (void)
1185 {
1186   GDK_THREADS_LEAVE ();
1187   g_main_iteration (TRUE);
1188   GDK_THREADS_ENTER ();
1189
1190   if (main_loops)
1191     return !g_main_is_running (main_loops->data);
1192   else
1193     return TRUE;
1194 }
1195
1196 gboolean
1197 gtk_main_iteration_do (gboolean blocking)
1198 {
1199   GDK_THREADS_LEAVE ();
1200   g_main_iteration (blocking);
1201   GDK_THREADS_ENTER ();
1202
1203   if (main_loops)
1204     return !g_main_is_running (main_loops->data);
1205   else
1206     return TRUE;
1207 }
1208
1209 /* private libgtk to libgdk interfaces
1210  */
1211 gboolean gdk_pointer_grab_info_libgtk_only  (GdkDisplay *display,
1212                                              GdkWindow **grab_window,
1213                                              gboolean   *owner_events);
1214 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1215                                              GdkWindow **grab_window,
1216                                              gboolean   *owner_events);
1217
1218 static void
1219 rewrite_events_translate (GdkWindow *old_window,
1220                           GdkWindow *new_window,
1221                           gdouble   *x,
1222                           gdouble   *y)
1223 {
1224   gint old_origin_x, old_origin_y;
1225   gint new_origin_x, new_origin_y;
1226
1227   gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1228   gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1229
1230   *x += old_origin_x - new_origin_x;
1231   *y += old_origin_y - new_origin_y;
1232 }
1233
1234 static GdkEvent *
1235 rewrite_event_for_window (GdkEvent  *event,
1236                           GdkWindow *new_window)
1237 {
1238   event = gdk_event_copy (event);
1239
1240   switch (event->type)
1241     {
1242     case GDK_SCROLL:
1243       rewrite_events_translate (event->any.window,
1244                                 new_window,
1245                                 &event->scroll.x, &event->scroll.y);
1246       break;
1247     case GDK_BUTTON_PRESS:
1248     case GDK_2BUTTON_PRESS:
1249     case GDK_3BUTTON_PRESS:
1250     case GDK_BUTTON_RELEASE:
1251       rewrite_events_translate (event->any.window,
1252                                 new_window,
1253                                 &event->button.x, &event->button.y);
1254       break;
1255     case GDK_MOTION_NOTIFY:
1256       rewrite_events_translate (event->any.window,
1257                                 new_window,
1258                                 &event->motion.x, &event->motion.y);
1259       break;
1260     case GDK_KEY_PRESS:
1261     case GDK_KEY_RELEASE:
1262     case GDK_PROXIMITY_IN:
1263     case GDK_PROXIMITY_OUT:
1264       break;
1265
1266     default:
1267       return event;
1268     }
1269
1270   g_object_unref (event->any.window);
1271   event->any.window = g_object_ref (new_window);
1272
1273   return event;
1274 }
1275
1276 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1277  * then what X11 does is deliver the event normally if it was going to this
1278  * client, otherwise, delivers it in terms of the grab window. This function
1279  * rewrites events to the effect that events going to the same window group
1280  * are delivered normally, otherwise, the event is delivered in terms of the
1281  * grab window.
1282  */
1283 static GdkEvent *
1284 rewrite_event_for_grabs (GdkEvent *event)
1285 {
1286   GdkWindow *grab_window;
1287   GtkWidget *event_widget, *grab_widget;
1288   gboolean owner_events;
1289   GdkDisplay *display;
1290
1291   switch (event->type)
1292     {
1293     case GDK_SCROLL:
1294     case GDK_BUTTON_PRESS:
1295     case GDK_2BUTTON_PRESS:
1296     case GDK_3BUTTON_PRESS:
1297     case GDK_BUTTON_RELEASE:
1298     case GDK_MOTION_NOTIFY:
1299     case GDK_PROXIMITY_IN:
1300     case GDK_PROXIMITY_OUT:
1301       display = gdk_drawable_get_display (event->proximity.window);
1302       if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1303           !owner_events)
1304         return NULL;
1305       break;
1306
1307     case GDK_KEY_PRESS:
1308     case GDK_KEY_RELEASE:
1309       display = gdk_drawable_get_display (event->key.window);
1310       if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1311           !owner_events)
1312         return NULL;
1313       break;
1314
1315     default:
1316       return NULL;
1317     }
1318
1319   event_widget = gtk_get_event_widget (event);
1320   gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1321
1322   if (grab_widget &&
1323       gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1324     return rewrite_event_for_window (event, grab_window);
1325   else
1326     return NULL;
1327 }
1328
1329 void 
1330 gtk_main_do_event (GdkEvent *event)
1331 {
1332   GtkWidget *event_widget;
1333   GtkWidget *grab_widget;
1334   GtkWindowGroup *window_group;
1335   GdkEvent *next_event;
1336   GdkEvent *rewritten_event = NULL;
1337   GList *tmp_list;
1338
1339   /* If there are any events pending then get the next one.
1340    */
1341   next_event = gdk_event_peek ();
1342   
1343   /* Try to compress enter/leave notify events. These event
1344    *  pairs occur when the mouse is dragged quickly across
1345    *  a window with many buttons (or through a menu). Instead
1346    *  of highlighting and de-highlighting each widget that
1347    *  is crossed it is better to simply de-highlight the widget
1348    *  which contained the mouse initially and highlight the
1349    *  widget which ends up containing the mouse.
1350    */
1351   if (next_event)
1352     if (((event->type == GDK_ENTER_NOTIFY) ||
1353          (event->type == GDK_LEAVE_NOTIFY)) &&
1354         ((next_event->type == GDK_ENTER_NOTIFY) ||
1355          (next_event->type == GDK_LEAVE_NOTIFY)) &&
1356         (next_event->type != event->type) &&
1357         (next_event->any.window == event->any.window))
1358       {
1359         /* Throw both the peeked copy and the queued copy away 
1360          */
1361         gdk_event_free (next_event);
1362         next_event = gdk_event_get ();
1363         gdk_event_free (next_event);
1364         
1365         return;
1366       }
1367
1368   if (next_event)
1369     gdk_event_free (next_event);
1370
1371   /* Find the widget which got the event. We store the widget
1372    *  in the user_data field of GdkWindow's.
1373    *  Ignore the event if we don't have a widget for it, except
1374    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
1375    *  Though this happens rarely, bogus events can occour
1376    *  for e.g. destroyed GdkWindows. 
1377    */
1378   event_widget = gtk_get_event_widget (event);
1379   if (!event_widget)
1380     {
1381       /* To handle selection INCR transactions, we select
1382        * PropertyNotify events on the requestor window and create
1383        * a corresponding (fake) GdkWindow so that events get
1384        * here. There won't be a widget though, so we have to handle
1385            * them specially
1386            */
1387       if (event->type == GDK_PROPERTY_NOTIFY)
1388         gtk_selection_incr_event (event->any.window,
1389                                   &event->property);
1390       else if (event->type == GDK_SETTING)
1391         _gtk_settings_handle_event (&event->setting);
1392
1393       return;
1394     }
1395
1396   /* If pointer or keyboard grabs are in effect, munge the events
1397    * so that each window group looks like a separate app.
1398    */
1399   rewritten_event = rewrite_event_for_grabs (event);
1400   if (rewritten_event)
1401     {
1402       event = rewritten_event;
1403       event_widget = gtk_get_event_widget (event);
1404     }
1405   
1406   window_group = gtk_main_get_window_group (event_widget);
1407
1408   /* Push the event onto a stack of current events for
1409    * gtk_current_event_get().
1410    */
1411   current_events = g_list_prepend (current_events, event);
1412
1413   /* If there is a grab in effect...
1414    */
1415   if (window_group->grabs)
1416     {
1417       grab_widget = window_group->grabs->data;
1418       
1419       /* If the grab widget is an ancestor of the event widget
1420        *  then we send the event to the original event widget.
1421        *  This is the key to implementing modality.
1422        */
1423       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1424           gtk_widget_is_ancestor (event_widget, grab_widget))
1425         grab_widget = event_widget;
1426     }
1427   else
1428     {
1429       grab_widget = event_widget;
1430     }
1431
1432   /* Not all events get sent to the grabbing widget.
1433    * The delete, destroy, expose, focus change and resize
1434    *  events still get sent to the event widget because
1435    *  1) these events have no meaning for the grabbing widget
1436    *  and 2) redirecting these events to the grabbing widget
1437    *  could cause the display to be messed up.
1438    * 
1439    * Drag events are also not redirected, since it isn't
1440    *  clear what the semantics of that would be.
1441    */
1442   switch (event->type)
1443     {
1444     case GDK_NOTHING:
1445       break;
1446       
1447     case GDK_DELETE:
1448       gtk_widget_ref (event_widget);
1449       if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1450           !gtk_widget_event (event_widget, event))
1451         gtk_widget_destroy (event_widget);
1452       gtk_widget_unref (event_widget);
1453       break;
1454       
1455     case GDK_DESTROY:
1456       /* Unexpected GDK_DESTROY from the outside, ignore for
1457        * child windows, handle like a GDK_DELETE for toplevels
1458        */
1459       if (!event_widget->parent)
1460         {
1461           gtk_widget_ref (event_widget);
1462           if (!gtk_widget_event (event_widget, event) &&
1463               GTK_WIDGET_REALIZED (event_widget))
1464             gtk_widget_destroy (event_widget);
1465           gtk_widget_unref (event_widget);
1466         }
1467       break;
1468       
1469     case GDK_EXPOSE:
1470       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1471         {
1472           gdk_window_begin_paint_region (event->any.window, event->expose.region);
1473           gtk_widget_send_expose (event_widget, event);
1474           gdk_window_end_paint (event->any.window);
1475         }
1476       else
1477         gtk_widget_send_expose (event_widget, event);
1478       break;
1479
1480     case GDK_PROPERTY_NOTIFY:
1481     case GDK_NO_EXPOSE:
1482     case GDK_FOCUS_CHANGE:
1483     case GDK_CONFIGURE:
1484     case GDK_MAP:
1485     case GDK_UNMAP:
1486     case GDK_SELECTION_CLEAR:
1487     case GDK_SELECTION_REQUEST:
1488     case GDK_SELECTION_NOTIFY:
1489     case GDK_CLIENT_EVENT:
1490     case GDK_VISIBILITY_NOTIFY:
1491     case GDK_WINDOW_STATE:
1492       gtk_widget_event (event_widget, event);
1493       break;
1494
1495     case GDK_SCROLL:
1496     case GDK_BUTTON_PRESS:
1497     case GDK_2BUTTON_PRESS:
1498     case GDK_3BUTTON_PRESS:
1499       gtk_propagate_event (grab_widget, event);
1500       break;
1501
1502     case GDK_KEY_PRESS:
1503     case GDK_KEY_RELEASE:
1504       if (key_snoopers)
1505         {
1506           if (gtk_invoke_key_snoopers (grab_widget, event))
1507             break;
1508         }
1509       /* else fall through */
1510     case GDK_MOTION_NOTIFY:
1511     case GDK_BUTTON_RELEASE:
1512     case GDK_PROXIMITY_IN:
1513     case GDK_PROXIMITY_OUT:
1514       gtk_propagate_event (grab_widget, event);
1515       break;
1516       
1517     case GDK_ENTER_NOTIFY:
1518       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1519         {
1520           g_object_ref (event_widget);
1521           
1522           gtk_widget_event (grab_widget, event);
1523           if (event_widget == grab_widget)
1524             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1525           
1526           g_object_unref (event_widget);
1527         }
1528       break;
1529       
1530     case GDK_LEAVE_NOTIFY:
1531       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1532         {
1533           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1534           gtk_widget_event (event_widget, event);
1535         }
1536       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1537         gtk_widget_event (grab_widget, event);
1538       break;
1539       
1540     case GDK_DRAG_STATUS:
1541     case GDK_DROP_FINISHED:
1542       _gtk_drag_source_handle_event (event_widget, event);
1543       break;
1544     case GDK_DRAG_ENTER:
1545     case GDK_DRAG_LEAVE:
1546     case GDK_DRAG_MOTION:
1547     case GDK_DROP_START:
1548       _gtk_drag_dest_handle_event (event_widget, event);
1549       break;
1550     default:
1551       g_assert_not_reached ();
1552       break;
1553     }
1554   
1555   tmp_list = current_events;
1556   current_events = g_list_remove_link (current_events, tmp_list);
1557   g_list_free_1 (tmp_list);
1558
1559   if (rewritten_event)
1560     gdk_event_free (rewritten_event);
1561 }
1562
1563 gboolean
1564 gtk_true (void)
1565 {
1566   return TRUE;
1567 }
1568
1569 gboolean
1570 gtk_false (void)
1571 {
1572   return FALSE;
1573 }
1574
1575 static GtkWindowGroup *
1576 gtk_main_get_window_group (GtkWidget   *widget)
1577 {
1578   GtkWidget *toplevel = NULL;
1579
1580   if (widget)
1581     toplevel = gtk_widget_get_toplevel (widget);
1582
1583   if (toplevel && GTK_IS_WINDOW (toplevel))
1584     return _gtk_window_get_group (GTK_WINDOW (toplevel));
1585   else
1586     return _gtk_window_get_group (NULL);
1587 }
1588
1589 typedef struct
1590 {
1591   GtkWidget *old_grab_widget;
1592   GtkWidget *new_grab_widget;
1593 } GrabNotifyInfo;
1594
1595 static gboolean
1596 check_is_grabbed (GtkWidget *widget,
1597                   GtkWidget *grab_widget)
1598 {
1599   if (grab_widget)
1600     return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1601   else
1602     return FALSE;
1603 }
1604
1605 static void
1606 gtk_grab_notify_foreach (GtkWidget *child,
1607                          gpointer   data)
1608                         
1609 {
1610   GrabNotifyInfo *info = data;
1611   gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1612   gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1613
1614   if (was_grabbed != is_grabbed)
1615     {
1616       g_object_ref (G_OBJECT (child));
1617       
1618       gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", was_grabbed);
1619       
1620       if (GTK_IS_CONTAINER (child))
1621         gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1622       
1623       g_object_unref (G_OBJECT (child));
1624     }
1625 }
1626
1627 static void
1628 gtk_grab_notify (GtkWindowGroup *group,
1629                  GtkWidget      *grab_widget,
1630                  gboolean        was_grabbed)
1631 {
1632   GList *toplevels;
1633   GrabNotifyInfo info;
1634
1635   if (was_grabbed)
1636     {
1637       info.old_grab_widget = grab_widget;
1638       info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1639     }
1640   else
1641     {
1642       info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1643       info.new_grab_widget = grab_widget;
1644     }
1645
1646   g_object_ref (group);
1647   g_object_ref (grab_widget);
1648
1649   toplevels = gtk_window_list_toplevels ();
1650   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1651                             
1652   while (toplevels)
1653     {
1654       GtkWindow *toplevel = toplevels->data;
1655       toplevels = g_list_delete_link (toplevels, toplevels);
1656
1657       if (group == _gtk_window_get_group (toplevel))
1658         gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1659       g_object_unref (toplevel);
1660     }
1661
1662   g_object_unref (group);
1663   g_object_unref (grab_widget);
1664 }
1665
1666 void
1667 gtk_grab_add (GtkWidget *widget)
1668 {
1669   GtkWindowGroup *group;
1670   gboolean was_grabbed;
1671   
1672   g_return_if_fail (widget != NULL);
1673   
1674   if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1675     {
1676       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1677       
1678       group = gtk_main_get_window_group (widget);
1679
1680       was_grabbed = (group->grabs != NULL);
1681       
1682       gtk_widget_ref (widget);
1683       group->grabs = g_slist_prepend (group->grabs, widget);
1684
1685       gtk_grab_notify (group, widget, FALSE);
1686     }
1687 }
1688
1689 GtkWidget*
1690 gtk_grab_get_current (void)
1691 {
1692   GtkWindowGroup *group;
1693
1694   group = gtk_main_get_window_group (NULL);
1695
1696   if (group->grabs)
1697     return GTK_WIDGET (group->grabs->data);
1698   return NULL;
1699 }
1700
1701 void
1702 gtk_grab_remove (GtkWidget *widget)
1703 {
1704   GtkWindowGroup *group;
1705   
1706   g_return_if_fail (widget != NULL);
1707   
1708   if (GTK_WIDGET_HAS_GRAB (widget))
1709     {
1710       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1711
1712       group = gtk_main_get_window_group (widget);
1713       group->grabs = g_slist_remove (group->grabs, widget);
1714       
1715       gtk_widget_unref (widget);
1716
1717       gtk_grab_notify (group, widget, TRUE);
1718     }
1719 }
1720
1721 void
1722 gtk_init_add (GtkFunction function,
1723               gpointer    data)
1724 {
1725   GtkInitFunction *init;
1726   
1727   init = g_new (GtkInitFunction, 1);
1728   init->function = function;
1729   init->data = data;
1730   
1731   init_functions = g_list_prepend (init_functions, init);
1732 }
1733
1734 guint
1735 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1736                          gpointer        func_data)
1737 {
1738   GtkKeySnooperData *data;
1739   static guint snooper_id = 1;
1740
1741   g_return_val_if_fail (snooper != NULL, 0);
1742
1743   data = g_new (GtkKeySnooperData, 1);
1744   data->func = snooper;
1745   data->func_data = func_data;
1746   data->id = snooper_id++;
1747   key_snoopers = g_slist_prepend (key_snoopers, data);
1748
1749   return data->id;
1750 }
1751
1752 void
1753 gtk_key_snooper_remove (guint            snooper_id)
1754 {
1755   GtkKeySnooperData *data = NULL;
1756   GSList *slist;
1757
1758   slist = key_snoopers;
1759   while (slist)
1760     {
1761       data = slist->data;
1762       if (data->id == snooper_id)
1763         break;
1764
1765       slist = slist->next;
1766       data = NULL;
1767     }
1768   if (data)
1769     key_snoopers = g_slist_remove (key_snoopers, data);
1770 }
1771
1772 static gint
1773 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1774                          GdkEvent  *event)
1775 {
1776   GSList *slist;
1777   gint return_val = FALSE;
1778
1779   slist = key_snoopers;
1780   while (slist && !return_val)
1781     {
1782       GtkKeySnooperData *data;
1783
1784       data = slist->data;
1785       slist = slist->next;
1786       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1787     }
1788
1789   return return_val;
1790 }
1791
1792 guint
1793 gtk_quit_add_full (guint                main_level,
1794                    GtkFunction          function,
1795                    GtkCallbackMarshal   marshal,
1796                    gpointer             data,
1797                    GtkDestroyNotify     destroy)
1798 {
1799   static guint quit_id = 1;
1800   GtkQuitFunction *quitf;
1801   
1802   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1803
1804   if (!quit_mem_chunk)
1805     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1806                                       512, G_ALLOC_AND_FREE);
1807   
1808   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1809   
1810   quitf->id = quit_id++;
1811   quitf->main_level = main_level;
1812   quitf->function = function;
1813   quitf->marshal = marshal;
1814   quitf->data = data;
1815   quitf->destroy = destroy;
1816
1817   quit_functions = g_list_prepend (quit_functions, quitf);
1818   
1819   return quitf->id;
1820 }
1821
1822 static void
1823 gtk_quit_destroy (GtkQuitFunction *quitf)
1824 {
1825   if (quitf->destroy)
1826     quitf->destroy (quitf->data);
1827   g_mem_chunk_free (quit_mem_chunk, quitf);
1828 }
1829
1830 static gint
1831 gtk_quit_destructor (GtkObject **object_p)
1832 {
1833   if (*object_p)
1834     gtk_object_destroy (*object_p);
1835   g_free (object_p);
1836
1837   return FALSE;
1838 }
1839
1840 void
1841 gtk_quit_add_destroy (guint              main_level,
1842                       GtkObject         *object)
1843 {
1844   GtkObject **object_p;
1845
1846   g_return_if_fail (main_level > 0);
1847   g_return_if_fail (GTK_IS_OBJECT (object));
1848
1849   object_p = g_new (GtkObject*, 1);
1850   *object_p = object;
1851   gtk_signal_connect (object,
1852                       "destroy",
1853                       GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1854                       object_p);
1855   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1856 }
1857
1858 guint
1859 gtk_quit_add (guint       main_level,
1860               GtkFunction function,
1861               gpointer    data)
1862 {
1863   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1864 }
1865
1866 void
1867 gtk_quit_remove (guint id)
1868 {
1869   GtkQuitFunction *quitf;
1870   GList *tmp_list;
1871   
1872   tmp_list = quit_functions;
1873   while (tmp_list)
1874     {
1875       quitf = tmp_list->data;
1876       
1877       if (quitf->id == id)
1878         {
1879           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1880           g_list_free (tmp_list);
1881           gtk_quit_destroy (quitf);
1882           
1883           return;
1884         }
1885       
1886       tmp_list = tmp_list->next;
1887     }
1888 }
1889
1890 void
1891 gtk_quit_remove_by_data (gpointer data)
1892 {
1893   GtkQuitFunction *quitf;
1894   GList *tmp_list;
1895   
1896   tmp_list = quit_functions;
1897   while (tmp_list)
1898     {
1899       quitf = tmp_list->data;
1900       
1901       if (quitf->data == data)
1902         {
1903           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1904           g_list_free (tmp_list);
1905           gtk_quit_destroy (quitf);
1906
1907           return;
1908         }
1909       
1910       tmp_list = tmp_list->next;
1911     }
1912 }
1913
1914 guint
1915 gtk_timeout_add_full (guint32            interval,
1916                       GtkFunction        function,
1917                       GtkCallbackMarshal marshal,
1918                       gpointer           data,
1919                       GtkDestroyNotify   destroy)
1920 {
1921   if (marshal)
1922     {
1923       GtkClosure *closure;
1924
1925       closure = g_new (GtkClosure, 1);
1926       closure->marshal = marshal;
1927       closure->data = data;
1928       closure->destroy = destroy;
1929
1930       return g_timeout_add_full (0, interval, 
1931                                  gtk_invoke_idle_timeout,
1932                                  closure,
1933                                  gtk_destroy_closure);
1934     }
1935   else
1936     return g_timeout_add_full (0, interval, function, data, destroy);
1937 }
1938
1939 guint
1940 gtk_timeout_add (guint32     interval,
1941                  GtkFunction function,
1942                  gpointer    data)
1943 {
1944   return g_timeout_add_full (0, interval, function, data, NULL);
1945 }
1946
1947 void
1948 gtk_timeout_remove (guint tag)
1949 {
1950   g_source_remove (tag);
1951 }
1952
1953 guint
1954 gtk_idle_add_full (gint                 priority,
1955                    GtkFunction          function,
1956                    GtkCallbackMarshal   marshal,
1957                    gpointer             data,
1958                    GtkDestroyNotify     destroy)
1959 {
1960   if (marshal)
1961     {
1962       GtkClosure *closure;
1963
1964       closure = g_new (GtkClosure, 1);
1965       closure->marshal = marshal;
1966       closure->data = data;
1967       closure->destroy = destroy;
1968
1969       return g_idle_add_full (priority,
1970                               gtk_invoke_idle_timeout,
1971                               closure,
1972                               gtk_destroy_closure);
1973     }
1974   else
1975     return g_idle_add_full (priority, function, data, destroy);
1976 }
1977
1978 guint
1979 gtk_idle_add (GtkFunction function,
1980               gpointer    data)
1981 {
1982   return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1983 }
1984
1985 guint       
1986 gtk_idle_add_priority (gint        priority,
1987                        GtkFunction function,
1988                        gpointer    data)
1989 {
1990   return g_idle_add_full (priority, function, data, NULL);
1991 }
1992
1993 void
1994 gtk_idle_remove (guint tag)
1995 {
1996   g_source_remove (tag);
1997 }
1998
1999 void
2000 gtk_idle_remove_by_data (gpointer data)
2001 {
2002   if (!g_idle_remove_by_data (data))
2003     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2004 }
2005
2006 guint
2007 gtk_input_add_full (gint                source,
2008                     GdkInputCondition   condition,
2009                     GdkInputFunction    function,
2010                     GtkCallbackMarshal  marshal,
2011                     gpointer            data,
2012                     GtkDestroyNotify    destroy)
2013 {
2014   if (marshal)
2015     {
2016       GtkClosure *closure;
2017
2018       closure = g_new (GtkClosure, 1);
2019       closure->marshal = marshal;
2020       closure->data = data;
2021       closure->destroy = destroy;
2022
2023       return gdk_input_add_full (source,
2024                                  condition,
2025                                  (GdkInputFunction) gtk_invoke_input,
2026                                  closure,
2027                                  (GdkDestroyNotify) gtk_destroy_closure);
2028     }
2029   else
2030     return gdk_input_add_full (source, condition, function, data, destroy);
2031 }
2032
2033 void
2034 gtk_input_remove (guint tag)
2035 {
2036   g_source_remove (tag);
2037 }
2038
2039 static void
2040 gtk_destroy_closure (gpointer data)
2041 {
2042   GtkClosure *closure = data;
2043
2044   if (closure->destroy)
2045     (closure->destroy) (closure->data);
2046   g_free (closure);
2047 }
2048
2049 static gboolean
2050 gtk_invoke_idle_timeout (gpointer data)
2051 {
2052   GtkClosure *closure = data;
2053
2054   GtkArg args[1];
2055   gint ret_val = FALSE;
2056   args[0].name = NULL;
2057   args[0].type = GTK_TYPE_BOOL;
2058   args[0].d.pointer_data = &ret_val;
2059   closure->marshal (NULL, closure->data,  0, args);
2060   return ret_val;
2061 }
2062
2063 static void
2064 gtk_invoke_input (gpointer          data,
2065                   gint              source,
2066                   GdkInputCondition condition)
2067 {
2068   GtkClosure *closure = data;
2069
2070   GtkArg args[3];
2071   args[0].type = GTK_TYPE_INT;
2072   args[0].name = NULL;
2073   GTK_VALUE_INT (args[0]) = source;
2074   args[1].type = GDK_TYPE_INPUT_CONDITION;
2075   args[1].name = NULL;
2076   GTK_VALUE_FLAGS (args[1]) = condition;
2077   args[2].type = GTK_TYPE_NONE;
2078   args[2].name = NULL;
2079
2080   closure->marshal (NULL, closure->data, 2, args);
2081 }
2082
2083 /**
2084  * gtk_get_current_event:
2085  * 
2086  * Obtains a copy of the event currently being processed by GTK+.  For
2087  * example, if you get a "clicked" signal from #GtkButton, the current
2088  * event will be the #GdkEventButton that triggered the "clicked"
2089  * signal. The returned event must be freed with gdk_event_free().
2090  * If there is no current event, the function returns %NULL.
2091  * 
2092  * Return value: a copy of the current event, or %NULL if no current event.
2093  **/
2094 GdkEvent*
2095 gtk_get_current_event (void)
2096 {
2097   if (current_events)
2098     return gdk_event_copy (current_events->data);
2099   else
2100     return NULL;
2101 }
2102
2103 /**
2104  * gtk_get_current_event_time:
2105  * 
2106  * If there is a current event and it has a timestamp, return that
2107  * timestamp, otherwise return %GDK_CURRENT_TIME.
2108  * 
2109  * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2110  **/
2111 guint32
2112 gtk_get_current_event_time (void)
2113 {
2114   if (current_events)
2115     return gdk_event_get_time (current_events->data);
2116   else
2117     return GDK_CURRENT_TIME;
2118 }
2119
2120 /**
2121  * gtk_get_current_event_state:
2122  * @state: a location to store the state of the current event
2123  * 
2124  * If there is a current event and it has a state field, place
2125  * that state field in @state and return %TRUE, otherwise return
2126  * %FALSE.
2127  * 
2128  * Return value: %TRUE if there was a current event and it had a state field
2129  **/
2130 gboolean
2131 gtk_get_current_event_state (GdkModifierType *state)
2132 {
2133   g_return_val_if_fail (state != NULL, FALSE);
2134   
2135   if (current_events)
2136     return gdk_event_get_state (current_events->data, state);
2137   else
2138     {
2139       *state = 0;
2140       return FALSE;
2141     }
2142 }
2143
2144 /**
2145  * gtk_get_event_widget:
2146  * @event: a #GdkEvent
2147  *
2148  * If @event is %NULL or the event was not associated with any widget,
2149  * returns %NULL, otherwise returns the widget that received the event
2150  * originally.
2151  * 
2152  * Return value: the widget that originally received @event, or %NULL
2153  **/
2154 GtkWidget*
2155 gtk_get_event_widget (GdkEvent *event)
2156 {
2157   GtkWidget *widget;
2158
2159   widget = NULL;
2160   if (event && event->any.window)
2161     gdk_window_get_user_data (event->any.window, (void**) &widget);
2162   
2163   return widget;
2164 }
2165
2166 static gint
2167 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2168 {
2169   if (!quitf->marshal)
2170     return quitf->function (quitf->data);
2171   else
2172     {
2173       GtkArg args[1];
2174       gint ret_val = FALSE;
2175
2176       args[0].name = NULL;
2177       args[0].type = GTK_TYPE_BOOL;
2178       args[0].d.pointer_data = &ret_val;
2179       ((GtkCallbackMarshal) quitf->marshal) (NULL,
2180                                              quitf->data,
2181                                              0, args);
2182       return ret_val;
2183     }
2184 }
2185
2186 /**
2187  * gtk_propagate_event:
2188  * @widget: a #GtkWidget
2189  * @event: an event
2190  *
2191  * Sends an event to a widget, propagating the event to parent widgets
2192  * if the event remains unhandled. Events received by GTK+ from GDK
2193  * normally begin in gtk_main_do_event(). Depending on the type of
2194  * event, existence of modal dialogs, grabs, etc., the event may be
2195  * propagated; if so, this function is used. gtk_propagate_event()
2196  * calls gtk_widget_event() on each widget it decides to send the
2197  * event to.  So gtk_widget_event() is the lowest-level function; it
2198  * simply emits the "event" and possibly an event-specific signal on a
2199  * widget.  gtk_propagate_event() is a bit higher-level, and
2200  * gtk_main_do_event() is the highest level.
2201  *
2202  * All that said, you most likely don't want to use any of these
2203  * functions; synthesizing events is rarely needed. Consider asking on
2204  * the mailing list for better ways to achieve your goals. For
2205  * example, use gdk_window_invalidate_rect() or
2206  * gtk_widget_queue_draw() instead of making up expose events.
2207  * 
2208  **/
2209 void
2210 gtk_propagate_event (GtkWidget *widget,
2211                      GdkEvent  *event)
2212 {
2213   gint handled_event;
2214   
2215   g_return_if_fail (GTK_IS_WIDGET (widget));
2216   g_return_if_fail (event != NULL);
2217   
2218   handled_event = FALSE;
2219
2220   gtk_widget_ref (widget);
2221       
2222   if ((event->type == GDK_KEY_PRESS) ||
2223       (event->type == GDK_KEY_RELEASE))
2224     {
2225       /* Only send key events within Window widgets to the Window
2226        *  The Window widget will in turn pass the
2227        *  key event on to the currently focused widget
2228        *  for that window.
2229        */
2230       GtkWidget *window;
2231
2232       window = gtk_widget_get_toplevel (widget);
2233       if (window && GTK_IS_WINDOW (window))
2234         {
2235           /* If there is a grab within the window, give the grab widget
2236            * a first crack at the key event
2237            */
2238           if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2239             handled_event = gtk_widget_event (widget, event);
2240           
2241           if (!handled_event)
2242             {
2243               window = gtk_widget_get_toplevel (widget);
2244               if (window && GTK_IS_WINDOW (window))
2245                 {
2246                   if (GTK_WIDGET_IS_SENSITIVE (window))
2247                     gtk_widget_event (window, event);
2248                 }
2249             }
2250                   
2251           handled_event = TRUE; /* don't send to widget */
2252         }
2253     }
2254   
2255   /* Other events get propagated up the widget tree
2256    *  so that parents can see the button and motion
2257    *  events of the children.
2258    */
2259   if (!handled_event)
2260     {
2261       while (TRUE)
2262         {
2263           GtkWidget *tmp;
2264           
2265           handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2266           tmp = widget->parent;
2267           gtk_widget_unref (widget);
2268
2269           widget = tmp;
2270           
2271           if (!handled_event && widget)
2272             gtk_widget_ref (widget);
2273           else
2274             break;
2275         }
2276     }
2277   else
2278     gtk_widget_unref (widget);
2279 }
2280
2281 #if 0
2282 static void
2283 gtk_error (gchar *str)
2284 {
2285   gtk_print (str);
2286 }
2287
2288 static void
2289 gtk_warning (gchar *str)
2290 {
2291   gtk_print (str);
2292 }
2293
2294 static void
2295 gtk_message (gchar *str)
2296 {
2297   gtk_print (str);
2298 }
2299
2300 static void
2301 gtk_print (gchar *str)
2302 {
2303   static GtkWidget *window = NULL;
2304   static GtkWidget *text;
2305   static int level = 0;
2306   GtkWidget *box1;
2307   GtkWidget *box2;
2308   GtkWidget *table;
2309   GtkWidget *hscrollbar;
2310   GtkWidget *vscrollbar;
2311   GtkWidget *separator;
2312   GtkWidget *button;
2313   
2314   if (level > 0)
2315     {
2316       fputs (str, stdout);
2317       fflush (stdout);
2318       return;
2319     }
2320   
2321   if (!window)
2322     {
2323       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2324       
2325       gtk_signal_connect (GTK_OBJECT (window), "destroy",
2326                           (GtkSignalFunc) gtk_widget_destroyed,
2327                           &window);
2328       
2329       gtk_window_set_title (GTK_WINDOW (window), "Messages");
2330       
2331       box1 = gtk_vbox_new (FALSE, 0);
2332       gtk_container_add (GTK_CONTAINER (window), box1);
2333       gtk_widget_show (box1);
2334       
2335       
2336       box2 = gtk_vbox_new (FALSE, 10);
2337       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2338       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2339       gtk_widget_show (box2);
2340       
2341       
2342       table = gtk_table_new (2, 2, FALSE);
2343       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2344       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2345       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2346       gtk_widget_show (table);
2347       
2348       text = gtk_text_new (NULL, NULL);
2349       gtk_text_set_editable (GTK_TEXT (text), FALSE);
2350       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2351       gtk_widget_show (text);
2352       gtk_widget_realize (text);
2353       
2354       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2355       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2356                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2357       gtk_widget_show (hscrollbar);
2358       
2359       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2360       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2361                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2362       gtk_widget_show (vscrollbar);
2363       
2364       separator = gtk_hseparator_new ();
2365       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2366       gtk_widget_show (separator);
2367       
2368       
2369       box2 = gtk_vbox_new (FALSE, 10);
2370       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2371       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2372       gtk_widget_show (box2);
2373       
2374       
2375       button = gtk_button_new_with_label ("close");
2376       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2377                                  (GtkSignalFunc) gtk_widget_hide,
2378                                  GTK_OBJECT (window));
2379       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2380       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2381       gtk_widget_grab_default (button);
2382       gtk_widget_show (button);
2383     }
2384   
2385   level += 1;
2386   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2387   level -= 1;
2388   
2389   if (!GTK_WIDGET_VISIBLE (window))
2390     gtk_widget_show (window);
2391 }
2392 #endif
2393
2394 gboolean
2395 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2396                                   GValue                *return_accu,
2397                                   const GValue          *handler_return,
2398                                   gpointer               dummy)
2399 {
2400   gboolean continue_emission;
2401   gboolean signal_handled;
2402   
2403   signal_handled = g_value_get_boolean (handler_return);
2404   g_value_set_boolean (return_accu, signal_handled);
2405   continue_emission = !signal_handled;
2406   
2407   return continue_emission;
2408 }