]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
Don't use zip -r on the etc directory, to avoid including editor backup
[~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 "config.h"
52 #include "gtkintl.h"
53
54 #include "gtkaccelmap.h"
55 #include "gtkbox.h"
56 #include "gtkdnd.h"
57 #include "gtkversion.h"
58 #include "gtkmain.h"
59 #include "gtkrc.h"
60 #include "gtkselection.h"
61 #include "gtksettings.h"
62 #include "gtkwidget.h"
63 #include "gtkwindow.h"
64 #include "gtkprivate.h"
65 #include "gtkdebug.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   if (!gtk_parse_args (argc, argv))
871     return FALSE;
872
873   return gdk_display_open_default_libgtk_only () != NULL;
874 }
875
876 #undef gtk_init
877
878 /**
879  * gtk_init:
880  * @argc: Address of the <parameter>argc</parameter> parameter of your 
881  *   <function>main()</function> function. Changed if any arguments were 
882  *   handled.
883  * @argv: Address of the <parameter>argv</parameter> parameter of 
884  *   <function>main()</function>. Any parameters understood by gtk_init() 
885  *   are stripped before return.
886  * 
887  * Call this function before using any other GTK+ functions in your GUI
888  * applications.  It will initialize everything needed to operate the toolkit and
889  * parses some standard command line options. @argc and 
890  * @argv are adjusted accordingly so your own code will 
891  * never see those standard arguments.
892  *
893  * <note><para>
894  * This function will terminate your program if it was unable to initialize 
895  * the GUI for some reason. If you want your program to fall back to a 
896  * textual interface you want to call gtk_init_check() instead.
897  * </para></note>
898  **/
899 void
900 gtk_init (int *argc, char ***argv)
901 {
902   if (!gtk_init_check (argc, argv))
903     {
904       const char *display_name_arg = gdk_get_display_arg_name ();
905       g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
906       exit (1);
907     }
908 }
909
910 #ifdef G_PLATFORM_WIN32
911
912 static void
913 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
914 {
915   if (sizeof_GtkWindow != sizeof (GtkWindow))
916     g_error ("Incompatible build!\n"
917              "The code using GTK+ thinks GtkWindow is of different\n"
918              "size than it actually is in this build of GTK+.\n"
919              "On Windows, this probably means that you have compiled\n"
920              "your code with gcc without the -fnative-struct\n"
921              "(or -mms-bitfields) switch, or that you are using\n"
922              "an unsupported compiler.");
923 }
924
925 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
926  * gcc-compiled code on Win32 whether compiled with -fnative-struct or
927  * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
928  * from GTK+ 2.0.2 on, check some other struct, too, where the use of
929  * -fnative-struct still matters. GtkBox is one such.
930  */
931 static void
932 check_sizeof_GtkBox (size_t sizeof_GtkBox)
933 {
934   if (sizeof_GtkBox != sizeof (GtkBox))
935     g_error ("Incompatible build!\n"
936              "The code using GTK+ thinks GtkBox is of different\n"
937              "size than it actually is in this build of GTK+.\n"
938              "On Windows, this probably means that you have compiled\n"
939              "your code with gcc without the -fnative-struct\n"
940              "(or -mms-bitfields) switch, or that you are using\n"
941              "an unsupported compiler.");
942 }
943
944 /* These two functions might get more checks added later, thus pass
945  * in the number of extra args.
946  */
947 void
948 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
949 {
950   check_sizeof_GtkWindow (sizeof_GtkWindow);
951   if (num_checks >= 2)
952     check_sizeof_GtkBox (sizeof_GtkBox);
953   gtk_init (argc, argv);
954 }
955
956 gboolean
957 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
958 {
959   check_sizeof_GtkWindow (sizeof_GtkWindow);
960   if (num_checks >= 2)
961     check_sizeof_GtkBox (sizeof_GtkBox);
962   return gtk_init_check (argc, argv);
963 }
964
965 #endif
966
967 void
968 gtk_exit (gint errorcode)
969 {
970   exit (errorcode);
971 }
972
973
974 /**
975  * gtk_set_locale:
976  *
977  * Initializes internationalization support for GTK+. gtk_init()
978  * automatically does this, so there is typically no point
979  * in calling this function.
980  *
981  * If you are calling this function because you changed the locale
982  * after GTK+ is was initialized, then calling this function
983  * may help a bit. (Note, however, that changing the locale
984  * after GTK+ is initialized may produce inconsistent results and
985  * is not really supported.)
986  * 
987  * In detail - sets the current locale according to the
988  * program environment. This is the same as calling the C library function
989  * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the 
990  * locale specific setup of the windowing system used by GDK.
991  * 
992  * Return: a string corresponding to the locale set, typically in the
993  * form lang_COUNTRY, where lang is an ISO-639 language code, and
994  * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
995  * result of the <function>setlocale()</function>; it is also used on
996  * other machines, such as Windows, where the C library returns a
997  * different result. The string is owned by GTK+ and should not be
998  * modified or freed.
999  **/
1000 gchar *
1001 gtk_set_locale (void)
1002 {
1003   return gdk_set_locale ();
1004 }
1005
1006 /**
1007  * _gtk_get_lc_ctype:
1008  *
1009  * Return the Unix-style locale string for the language currently in
1010  * effect. On Unix systems, this is the return value from
1011  * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1012  * affect this through the environment variables LC_ALL, LC_CTYPE or
1013  * LANG (checked in that order). The locale strings typically is in
1014  * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1015  * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1016  * Swedish as written in Finland or pt_BR for Portuguese as written in
1017  * Brazil.
1018  * 
1019  * On Windows, the C library doesn't use any such environment
1020  * variables, and setting them won't affect the behaviour of functions
1021  * like <function>ctime()</function>. The user sets the locale through
1022  * the Regional Options in the Control Panel. The C library (in the
1023  * <function>setlocale()</function> function) does not use country and
1024  * language codes, but country and language names spelled out in
1025  * English. However, this function does check the above environment
1026  * variables, and does return a Unix-style locale string based on
1027  * either said environment variables or the thread's current locale.
1028  *
1029  * Return value: a dynamically allocated string, free with g_free().
1030  */
1031
1032 gchar *
1033 _gtk_get_lc_ctype (void)
1034 {
1035   gchar *p;
1036
1037 #ifdef G_OS_WIN32
1038   /* Somebody might try to set the locale for this process using the
1039    * LANG or LC_ environment variables. The Microsoft C library
1040    * doesn't know anything about them. You set the locale in the
1041    * Control Panel. Setting these env vars won't have any affect on
1042    * locale-dependent C library functions like ctime(). But just for
1043    * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1044    * it easier to test GTK and Pango in various default languages, you
1045    * don't have to clickety-click in the Control Panel, you can simply
1046    * start the program with LC_ALL=something on the command line.)
1047    */
1048   p = getenv ("LC_ALL");
1049   if (p != NULL)
1050     return g_strdup (p);
1051
1052   p = getenv ("LC_CTYPE");
1053   if (p != NULL)
1054     return g_strdup (p);
1055
1056   p = getenv ("LANG");
1057   if (p != NULL)
1058     return g_strdup (p);
1059
1060   return g_win32_getlocale ();
1061 #else
1062   return g_strdup (setlocale (LC_CTYPE, NULL));
1063 #endif
1064 }
1065
1066 /**
1067  * gtk_get_default_language:
1068  *
1069  * Returns the #PangoLanguage for the default language currently in
1070  * effect. (Note that this can change over the life of an
1071  * application.)  The default language is derived from the current
1072  * locale. It determines, for example, whether GTK+ uses the
1073  * right-to-left or left-to-right text direction. See
1074  * _gtk_get_lc_ctype for notes on behaviour on Windows.
1075  * 
1076  * Return value: the default language as a #PangoLanguage, must not be
1077  * freed
1078  **/
1079 PangoLanguage *
1080 gtk_get_default_language (void)
1081 {
1082   gchar *lang;
1083   PangoLanguage *result;
1084   gchar *p;
1085   
1086   lang = _gtk_get_lc_ctype ();
1087   p = strchr (lang, '.');
1088   if (p)
1089     *p = '\0';
1090   p = strchr (lang, '@');
1091   if (p)
1092     *p = '\0';
1093
1094   result = pango_language_from_string (lang);
1095   g_free (lang);
1096   
1097   return result;
1098 }
1099
1100 void
1101 gtk_main (void)
1102 {
1103   GList *tmp_list;
1104   GList *functions;
1105   GtkInitFunction *init;
1106   GMainLoop *loop;
1107
1108   gtk_main_loop_level++;
1109   
1110   loop = g_main_loop_new (NULL, TRUE);
1111   main_loops = g_slist_prepend (main_loops, loop);
1112
1113   tmp_list = functions = init_functions;
1114   init_functions = NULL;
1115   
1116   while (tmp_list)
1117     {
1118       init = tmp_list->data;
1119       tmp_list = tmp_list->next;
1120       
1121       (* init->function) (init->data);
1122       g_free (init);
1123     }
1124   g_list_free (functions);
1125
1126   if (g_main_loop_is_running (main_loops->data))
1127     {
1128       GDK_THREADS_LEAVE ();
1129       g_main_loop_run (loop);
1130       GDK_THREADS_ENTER ();
1131       gdk_flush ();
1132     }
1133
1134   if (quit_functions)
1135     {
1136       GList *reinvoke_list = NULL;
1137       GtkQuitFunction *quitf;
1138
1139       while (quit_functions)
1140         {
1141           quitf = quit_functions->data;
1142
1143           tmp_list = quit_functions;
1144           quit_functions = g_list_remove_link (quit_functions, quit_functions);
1145           g_list_free_1 (tmp_list);
1146
1147           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1148               gtk_quit_invoke_function (quitf))
1149             {
1150               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1151             }
1152           else
1153             {
1154               gtk_quit_destroy (quitf);
1155             }
1156         }
1157       if (reinvoke_list)
1158         {
1159           GList *work;
1160           
1161           work = g_list_last (reinvoke_list);
1162           if (quit_functions)
1163             quit_functions->prev = work;
1164           work->next = quit_functions;
1165           quit_functions = work;
1166         }
1167
1168       gdk_flush ();
1169     }
1170               
1171   main_loops = g_slist_remove (main_loops, loop);
1172
1173   g_main_loop_unref (loop);
1174
1175   gtk_main_loop_level--;
1176 }
1177
1178 guint
1179 gtk_main_level (void)
1180 {
1181   return gtk_main_loop_level;
1182 }
1183
1184 void
1185 gtk_main_quit (void)
1186 {
1187   g_return_if_fail (main_loops != NULL);
1188
1189   g_main_loop_quit (main_loops->data);
1190 }
1191
1192 gint
1193 gtk_events_pending (void)
1194 {
1195   gboolean result;
1196   
1197   GDK_THREADS_LEAVE ();  
1198   result = g_main_context_pending (NULL);
1199   GDK_THREADS_ENTER ();
1200
1201   return result;
1202 }
1203
1204 gboolean
1205 gtk_main_iteration (void)
1206 {
1207   GDK_THREADS_LEAVE ();
1208   g_main_context_iteration (NULL, TRUE);
1209   GDK_THREADS_ENTER ();
1210
1211   if (main_loops)
1212     return !g_main_loop_is_running (main_loops->data);
1213   else
1214     return TRUE;
1215 }
1216
1217 gboolean
1218 gtk_main_iteration_do (gboolean blocking)
1219 {
1220   GDK_THREADS_LEAVE ();
1221   g_main_context_iteration (NULL, blocking);
1222   GDK_THREADS_ENTER ();
1223
1224   if (main_loops)
1225     return !g_main_loop_is_running (main_loops->data);
1226   else
1227     return TRUE;
1228 }
1229
1230 /* private libgtk to libgdk interfaces
1231  */
1232 gboolean gdk_pointer_grab_info_libgtk_only  (GdkDisplay *display,
1233                                              GdkWindow **grab_window,
1234                                              gboolean   *owner_events);
1235 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1236                                              GdkWindow **grab_window,
1237                                              gboolean   *owner_events);
1238
1239 static void
1240 rewrite_events_translate (GdkWindow *old_window,
1241                           GdkWindow *new_window,
1242                           gdouble   *x,
1243                           gdouble   *y)
1244 {
1245   gint old_origin_x, old_origin_y;
1246   gint new_origin_x, new_origin_y;
1247
1248   gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1249   gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1250
1251   *x += old_origin_x - new_origin_x;
1252   *y += old_origin_y - new_origin_y;
1253 }
1254
1255 static GdkEvent *
1256 rewrite_event_for_window (GdkEvent  *event,
1257                           GdkWindow *new_window)
1258 {
1259   event = gdk_event_copy (event);
1260
1261   switch (event->type)
1262     {
1263     case GDK_SCROLL:
1264       rewrite_events_translate (event->any.window,
1265                                 new_window,
1266                                 &event->scroll.x, &event->scroll.y);
1267       break;
1268     case GDK_BUTTON_PRESS:
1269     case GDK_2BUTTON_PRESS:
1270     case GDK_3BUTTON_PRESS:
1271     case GDK_BUTTON_RELEASE:
1272       rewrite_events_translate (event->any.window,
1273                                 new_window,
1274                                 &event->button.x, &event->button.y);
1275       break;
1276     case GDK_MOTION_NOTIFY:
1277       rewrite_events_translate (event->any.window,
1278                                 new_window,
1279                                 &event->motion.x, &event->motion.y);
1280       break;
1281     case GDK_KEY_PRESS:
1282     case GDK_KEY_RELEASE:
1283     case GDK_PROXIMITY_IN:
1284     case GDK_PROXIMITY_OUT:
1285       break;
1286
1287     default:
1288       return event;
1289     }
1290
1291   g_object_unref (event->any.window);
1292   event->any.window = g_object_ref (new_window);
1293
1294   return event;
1295 }
1296
1297 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1298  * then what X11 does is deliver the event normally if it was going to this
1299  * client, otherwise, delivers it in terms of the grab window. This function
1300  * rewrites events to the effect that events going to the same window group
1301  * are delivered normally, otherwise, the event is delivered in terms of the
1302  * grab window.
1303  */
1304 static GdkEvent *
1305 rewrite_event_for_grabs (GdkEvent *event)
1306 {
1307   GdkWindow *grab_window;
1308   GtkWidget *event_widget, *grab_widget;
1309   gboolean owner_events;
1310   GdkDisplay *display;
1311
1312   switch (event->type)
1313     {
1314     case GDK_SCROLL:
1315     case GDK_BUTTON_PRESS:
1316     case GDK_2BUTTON_PRESS:
1317     case GDK_3BUTTON_PRESS:
1318     case GDK_BUTTON_RELEASE:
1319     case GDK_MOTION_NOTIFY:
1320     case GDK_PROXIMITY_IN:
1321     case GDK_PROXIMITY_OUT:
1322       display = gdk_drawable_get_display (event->proximity.window);
1323       if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1324           !owner_events)
1325         return NULL;
1326       break;
1327
1328     case GDK_KEY_PRESS:
1329     case GDK_KEY_RELEASE:
1330       display = gdk_drawable_get_display (event->key.window);
1331       if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1332           !owner_events)
1333         return NULL;
1334       break;
1335
1336     default:
1337       return NULL;
1338     }
1339
1340   event_widget = gtk_get_event_widget (event);
1341   gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1342
1343   if (grab_widget &&
1344       gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1345     return rewrite_event_for_window (event, grab_window);
1346   else
1347     return NULL;
1348 }
1349
1350 void 
1351 gtk_main_do_event (GdkEvent *event)
1352 {
1353   GtkWidget *event_widget;
1354   GtkWidget *grab_widget;
1355   GtkWindowGroup *window_group;
1356   GdkEvent *next_event;
1357   GdkEvent *rewritten_event = NULL;
1358   GList *tmp_list;
1359
1360   /* If there are any events pending then get the next one.
1361    */
1362   next_event = gdk_event_peek ();
1363   
1364   /* Try to compress enter/leave notify events. These event
1365    *  pairs occur when the mouse is dragged quickly across
1366    *  a window with many buttons (or through a menu). Instead
1367    *  of highlighting and de-highlighting each widget that
1368    *  is crossed it is better to simply de-highlight the widget
1369    *  which contained the mouse initially and highlight the
1370    *  widget which ends up containing the mouse.
1371    */
1372   if (next_event)
1373     if (((event->type == GDK_ENTER_NOTIFY) ||
1374          (event->type == GDK_LEAVE_NOTIFY)) &&
1375         ((next_event->type == GDK_ENTER_NOTIFY) ||
1376          (next_event->type == GDK_LEAVE_NOTIFY)) &&
1377         (next_event->type != event->type) &&
1378         (next_event->any.window == event->any.window))
1379       {
1380         /* Throw both the peeked copy and the queued copy away 
1381          */
1382         gdk_event_free (next_event);
1383         next_event = gdk_event_get ();
1384         gdk_event_free (next_event);
1385         
1386         return;
1387       }
1388
1389   if (next_event)
1390     gdk_event_free (next_event);
1391
1392   if (event->type == GDK_SETTING)
1393     {
1394       _gtk_settings_handle_event (&event->setting);
1395       return;
1396     }
1397
1398   /* Find the widget which got the event. We store the widget
1399    *  in the user_data field of GdkWindow's.
1400    *  Ignore the event if we don't have a widget for it, except
1401    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
1402    *  Though this happens rarely, bogus events can occour
1403    *  for e.g. destroyed GdkWindows. 
1404    */
1405   event_widget = gtk_get_event_widget (event);
1406   if (!event_widget)
1407     {
1408       /* To handle selection INCR transactions, we select
1409        * PropertyNotify events on the requestor window and create
1410        * a corresponding (fake) GdkWindow so that events get
1411        * here. There won't be a widget though, so we have to handle
1412            * them specially
1413            */
1414       if (event->type == GDK_PROPERTY_NOTIFY)
1415         _gtk_selection_incr_event (event->any.window,
1416                                    &event->property);
1417
1418       return;
1419     }
1420
1421   /* If pointer or keyboard grabs are in effect, munge the events
1422    * so that each window group looks like a separate app.
1423    */
1424   rewritten_event = rewrite_event_for_grabs (event);
1425   if (rewritten_event)
1426     {
1427       event = rewritten_event;
1428       event_widget = gtk_get_event_widget (event);
1429     }
1430   
1431   window_group = gtk_main_get_window_group (event_widget);
1432
1433   /* Push the event onto a stack of current events for
1434    * gtk_current_event_get().
1435    */
1436   current_events = g_list_prepend (current_events, event);
1437
1438   /* If there is a grab in effect...
1439    */
1440   if (window_group->grabs)
1441     {
1442       grab_widget = window_group->grabs->data;
1443       
1444       /* If the grab widget is an ancestor of the event widget
1445        *  then we send the event to the original event widget.
1446        *  This is the key to implementing modality.
1447        */
1448       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1449           gtk_widget_is_ancestor (event_widget, grab_widget))
1450         grab_widget = event_widget;
1451     }
1452   else
1453     {
1454       grab_widget = event_widget;
1455     }
1456
1457   /* Not all events get sent to the grabbing widget.
1458    * The delete, destroy, expose, focus change and resize
1459    *  events still get sent to the event widget because
1460    *  1) these events have no meaning for the grabbing widget
1461    *  and 2) redirecting these events to the grabbing widget
1462    *  could cause the display to be messed up.
1463    * 
1464    * Drag events are also not redirected, since it isn't
1465    *  clear what the semantics of that would be.
1466    */
1467   switch (event->type)
1468     {
1469     case GDK_NOTHING:
1470       break;
1471       
1472     case GDK_DELETE:
1473       g_object_ref (event_widget);
1474       if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1475           !gtk_widget_event (event_widget, event))
1476         gtk_widget_destroy (event_widget);
1477       g_object_unref (event_widget);
1478       break;
1479       
1480     case GDK_DESTROY:
1481       /* Unexpected GDK_DESTROY from the outside, ignore for
1482        * child windows, handle like a GDK_DELETE for toplevels
1483        */
1484       if (!event_widget->parent)
1485         {
1486           g_object_ref (event_widget);
1487           if (!gtk_widget_event (event_widget, event) &&
1488               GTK_WIDGET_REALIZED (event_widget))
1489             gtk_widget_destroy (event_widget);
1490           g_object_unref (event_widget);
1491         }
1492       break;
1493       
1494     case GDK_EXPOSE:
1495       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1496         {
1497           gdk_window_begin_paint_region (event->any.window, event->expose.region);
1498           gtk_widget_send_expose (event_widget, event);
1499           gdk_window_end_paint (event->any.window);
1500         }
1501       else
1502         gtk_widget_send_expose (event_widget, event);
1503       break;
1504
1505     case GDK_PROPERTY_NOTIFY:
1506     case GDK_NO_EXPOSE:
1507     case GDK_FOCUS_CHANGE:
1508     case GDK_CONFIGURE:
1509     case GDK_MAP:
1510     case GDK_UNMAP:
1511     case GDK_SELECTION_CLEAR:
1512     case GDK_SELECTION_REQUEST:
1513     case GDK_SELECTION_NOTIFY:
1514     case GDK_CLIENT_EVENT:
1515     case GDK_VISIBILITY_NOTIFY:
1516     case GDK_WINDOW_STATE:
1517       gtk_widget_event (event_widget, event);
1518       break;
1519
1520     case GDK_SCROLL:
1521     case GDK_BUTTON_PRESS:
1522     case GDK_2BUTTON_PRESS:
1523     case GDK_3BUTTON_PRESS:
1524       gtk_propagate_event (grab_widget, event);
1525       break;
1526
1527     case GDK_KEY_PRESS:
1528     case GDK_KEY_RELEASE:
1529       if (key_snoopers)
1530         {
1531           if (gtk_invoke_key_snoopers (grab_widget, event))
1532             break;
1533         }
1534       /* else fall through */
1535     case GDK_MOTION_NOTIFY:
1536     case GDK_BUTTON_RELEASE:
1537     case GDK_PROXIMITY_IN:
1538     case GDK_PROXIMITY_OUT:
1539       gtk_propagate_event (grab_widget, event);
1540       break;
1541       
1542     case GDK_ENTER_NOTIFY:
1543       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1544         {
1545           g_object_ref (event_widget);
1546           
1547           gtk_widget_event (grab_widget, event);
1548           if (event_widget == grab_widget)
1549             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1550           
1551           g_object_unref (event_widget);
1552         }
1553       break;
1554       
1555     case GDK_LEAVE_NOTIFY:
1556       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1557         {
1558           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1559           gtk_widget_event (event_widget, event);
1560         }
1561       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1562         gtk_widget_event (grab_widget, event);
1563       break;
1564       
1565     case GDK_DRAG_STATUS:
1566     case GDK_DROP_FINISHED:
1567       _gtk_drag_source_handle_event (event_widget, event);
1568       break;
1569     case GDK_DRAG_ENTER:
1570     case GDK_DRAG_LEAVE:
1571     case GDK_DRAG_MOTION:
1572     case GDK_DROP_START:
1573       _gtk_drag_dest_handle_event (event_widget, event);
1574       break;
1575     default:
1576       g_assert_not_reached ();
1577       break;
1578     }
1579   
1580   tmp_list = current_events;
1581   current_events = g_list_remove_link (current_events, tmp_list);
1582   g_list_free_1 (tmp_list);
1583
1584   if (rewritten_event)
1585     gdk_event_free (rewritten_event);
1586 }
1587
1588 gboolean
1589 gtk_true (void)
1590 {
1591   return TRUE;
1592 }
1593
1594 gboolean
1595 gtk_false (void)
1596 {
1597   return FALSE;
1598 }
1599
1600 static GtkWindowGroup *
1601 gtk_main_get_window_group (GtkWidget   *widget)
1602 {
1603   GtkWidget *toplevel = NULL;
1604
1605   if (widget)
1606     toplevel = gtk_widget_get_toplevel (widget);
1607
1608   if (toplevel && GTK_IS_WINDOW (toplevel))
1609     return _gtk_window_get_group (GTK_WINDOW (toplevel));
1610   else
1611     return _gtk_window_get_group (NULL);
1612 }
1613
1614 typedef struct
1615 {
1616   GtkWidget *old_grab_widget;
1617   GtkWidget *new_grab_widget;
1618 } GrabNotifyInfo;
1619
1620 static gboolean
1621 check_is_grabbed (GtkWidget *widget,
1622                   GtkWidget *grab_widget)
1623 {
1624   if (grab_widget)
1625     return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1626   else
1627     return FALSE;
1628 }
1629
1630 static void
1631 gtk_grab_notify_foreach (GtkWidget *child,
1632                          gpointer   data)
1633                         
1634 {
1635   GrabNotifyInfo *info = data;
1636   gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1637   gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1638
1639   if (was_grabbed != is_grabbed)
1640     {
1641       g_object_ref (child);
1642       
1643       g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1644       
1645       if (GTK_IS_CONTAINER (child))
1646         gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1647       
1648       g_object_unref (child);
1649     }
1650 }
1651
1652 static void
1653 gtk_grab_notify (GtkWindowGroup *group,
1654                  GtkWidget      *grab_widget,
1655                  gboolean        was_grabbed)
1656 {
1657   GList *toplevels;
1658   GrabNotifyInfo info;
1659
1660   if (was_grabbed)
1661     {
1662       info.old_grab_widget = grab_widget;
1663       info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1664     }
1665   else
1666     {
1667       info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1668       info.new_grab_widget = grab_widget;
1669     }
1670
1671   g_object_ref (group);
1672   g_object_ref (grab_widget);
1673
1674   toplevels = gtk_window_list_toplevels ();
1675   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1676                             
1677   while (toplevels)
1678     {
1679       GtkWindow *toplevel = toplevels->data;
1680       toplevels = g_list_delete_link (toplevels, toplevels);
1681
1682       if (group == _gtk_window_get_group (toplevel))
1683         gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1684       g_object_unref (toplevel);
1685     }
1686
1687   g_object_unref (group);
1688   g_object_unref (grab_widget);
1689 }
1690
1691 void
1692 gtk_grab_add (GtkWidget *widget)
1693 {
1694   GtkWindowGroup *group;
1695   gboolean was_grabbed;
1696   
1697   g_return_if_fail (widget != NULL);
1698   
1699   if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1700     {
1701       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1702       
1703       group = gtk_main_get_window_group (widget);
1704
1705       was_grabbed = (group->grabs != NULL);
1706       
1707       g_object_ref (widget);
1708       group->grabs = g_slist_prepend (group->grabs, widget);
1709
1710       gtk_grab_notify (group, widget, FALSE);
1711     }
1712 }
1713
1714 GtkWidget*
1715 gtk_grab_get_current (void)
1716 {
1717   GtkWindowGroup *group;
1718
1719   group = gtk_main_get_window_group (NULL);
1720
1721   if (group->grabs)
1722     return GTK_WIDGET (group->grabs->data);
1723   return NULL;
1724 }
1725
1726 void
1727 gtk_grab_remove (GtkWidget *widget)
1728 {
1729   GtkWindowGroup *group;
1730   
1731   g_return_if_fail (widget != NULL);
1732   
1733   if (GTK_WIDGET_HAS_GRAB (widget))
1734     {
1735       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1736
1737       group = gtk_main_get_window_group (widget);
1738       group->grabs = g_slist_remove (group->grabs, widget);
1739       
1740       g_object_unref (widget);
1741
1742       gtk_grab_notify (group, widget, TRUE);
1743     }
1744 }
1745
1746 void
1747 gtk_init_add (GtkFunction function,
1748               gpointer    data)
1749 {
1750   GtkInitFunction *init;
1751   
1752   init = g_new (GtkInitFunction, 1);
1753   init->function = function;
1754   init->data = data;
1755   
1756   init_functions = g_list_prepend (init_functions, init);
1757 }
1758
1759 guint
1760 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1761                          gpointer        func_data)
1762 {
1763   GtkKeySnooperData *data;
1764   static guint snooper_id = 1;
1765
1766   g_return_val_if_fail (snooper != NULL, 0);
1767
1768   data = g_new (GtkKeySnooperData, 1);
1769   data->func = snooper;
1770   data->func_data = func_data;
1771   data->id = snooper_id++;
1772   key_snoopers = g_slist_prepend (key_snoopers, data);
1773
1774   return data->id;
1775 }
1776
1777 void
1778 gtk_key_snooper_remove (guint            snooper_id)
1779 {
1780   GtkKeySnooperData *data = NULL;
1781   GSList *slist;
1782
1783   slist = key_snoopers;
1784   while (slist)
1785     {
1786       data = slist->data;
1787       if (data->id == snooper_id)
1788         break;
1789
1790       slist = slist->next;
1791       data = NULL;
1792     }
1793   if (data)
1794     key_snoopers = g_slist_remove (key_snoopers, data);
1795 }
1796
1797 static gint
1798 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1799                          GdkEvent  *event)
1800 {
1801   GSList *slist;
1802   gint return_val = FALSE;
1803
1804   slist = key_snoopers;
1805   while (slist && !return_val)
1806     {
1807       GtkKeySnooperData *data;
1808
1809       data = slist->data;
1810       slist = slist->next;
1811       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1812     }
1813
1814   return return_val;
1815 }
1816
1817 guint
1818 gtk_quit_add_full (guint                main_level,
1819                    GtkFunction          function,
1820                    GtkCallbackMarshal   marshal,
1821                    gpointer             data,
1822                    GtkDestroyNotify     destroy)
1823 {
1824   static guint quit_id = 1;
1825   GtkQuitFunction *quitf;
1826   
1827   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1828
1829   if (!quit_mem_chunk)
1830     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1831                                       512, G_ALLOC_AND_FREE);
1832   
1833   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1834   
1835   quitf->id = quit_id++;
1836   quitf->main_level = main_level;
1837   quitf->function = function;
1838   quitf->marshal = marshal;
1839   quitf->data = data;
1840   quitf->destroy = destroy;
1841
1842   quit_functions = g_list_prepend (quit_functions, quitf);
1843   
1844   return quitf->id;
1845 }
1846
1847 static void
1848 gtk_quit_destroy (GtkQuitFunction *quitf)
1849 {
1850   if (quitf->destroy)
1851     quitf->destroy (quitf->data);
1852   g_mem_chunk_free (quit_mem_chunk, quitf);
1853 }
1854
1855 static gint
1856 gtk_quit_destructor (GtkObject **object_p)
1857 {
1858   if (*object_p)
1859     gtk_object_destroy (*object_p);
1860   g_free (object_p);
1861
1862   return FALSE;
1863 }
1864
1865 void
1866 gtk_quit_add_destroy (guint              main_level,
1867                       GtkObject         *object)
1868 {
1869   GtkObject **object_p;
1870
1871   g_return_if_fail (main_level > 0);
1872   g_return_if_fail (GTK_IS_OBJECT (object));
1873
1874   object_p = g_new (GtkObject*, 1);
1875   *object_p = object;
1876   g_signal_connect (object,
1877                     "destroy",
1878                     G_CALLBACK (gtk_widget_destroyed),
1879                     object_p);
1880   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1881 }
1882
1883 guint
1884 gtk_quit_add (guint       main_level,
1885               GtkFunction function,
1886               gpointer    data)
1887 {
1888   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1889 }
1890
1891 void
1892 gtk_quit_remove (guint id)
1893 {
1894   GtkQuitFunction *quitf;
1895   GList *tmp_list;
1896   
1897   tmp_list = quit_functions;
1898   while (tmp_list)
1899     {
1900       quitf = tmp_list->data;
1901       
1902       if (quitf->id == id)
1903         {
1904           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1905           g_list_free (tmp_list);
1906           gtk_quit_destroy (quitf);
1907           
1908           return;
1909         }
1910       
1911       tmp_list = tmp_list->next;
1912     }
1913 }
1914
1915 void
1916 gtk_quit_remove_by_data (gpointer data)
1917 {
1918   GtkQuitFunction *quitf;
1919   GList *tmp_list;
1920   
1921   tmp_list = quit_functions;
1922   while (tmp_list)
1923     {
1924       quitf = tmp_list->data;
1925       
1926       if (quitf->data == data)
1927         {
1928           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1929           g_list_free (tmp_list);
1930           gtk_quit_destroy (quitf);
1931
1932           return;
1933         }
1934       
1935       tmp_list = tmp_list->next;
1936     }
1937 }
1938
1939 guint
1940 gtk_timeout_add_full (guint32            interval,
1941                       GtkFunction        function,
1942                       GtkCallbackMarshal marshal,
1943                       gpointer           data,
1944                       GtkDestroyNotify   destroy)
1945 {
1946   if (marshal)
1947     {
1948       GtkClosure *closure;
1949
1950       closure = g_new (GtkClosure, 1);
1951       closure->marshal = marshal;
1952       closure->data = data;
1953       closure->destroy = destroy;
1954
1955       return g_timeout_add_full (0, interval, 
1956                                  gtk_invoke_idle_timeout,
1957                                  closure,
1958                                  gtk_destroy_closure);
1959     }
1960   else
1961     return g_timeout_add_full (0, interval, function, data, destroy);
1962 }
1963
1964 guint
1965 gtk_timeout_add (guint32     interval,
1966                  GtkFunction function,
1967                  gpointer    data)
1968 {
1969   return g_timeout_add_full (0, interval, function, data, NULL);
1970 }
1971
1972 void
1973 gtk_timeout_remove (guint tag)
1974 {
1975   g_source_remove (tag);
1976 }
1977
1978 guint
1979 gtk_idle_add_full (gint                 priority,
1980                    GtkFunction          function,
1981                    GtkCallbackMarshal   marshal,
1982                    gpointer             data,
1983                    GtkDestroyNotify     destroy)
1984 {
1985   if (marshal)
1986     {
1987       GtkClosure *closure;
1988
1989       closure = g_new (GtkClosure, 1);
1990       closure->marshal = marshal;
1991       closure->data = data;
1992       closure->destroy = destroy;
1993
1994       return g_idle_add_full (priority,
1995                               gtk_invoke_idle_timeout,
1996                               closure,
1997                               gtk_destroy_closure);
1998     }
1999   else
2000     return g_idle_add_full (priority, function, data, destroy);
2001 }
2002
2003 guint
2004 gtk_idle_add (GtkFunction function,
2005               gpointer    data)
2006 {
2007   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2008 }
2009
2010 guint       
2011 gtk_idle_add_priority (gint        priority,
2012                        GtkFunction function,
2013                        gpointer    data)
2014 {
2015   return g_idle_add_full (priority, function, data, NULL);
2016 }
2017
2018 void
2019 gtk_idle_remove (guint tag)
2020 {
2021   g_source_remove (tag);
2022 }
2023
2024 void
2025 gtk_idle_remove_by_data (gpointer data)
2026 {
2027   if (!g_idle_remove_by_data (data))
2028     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2029 }
2030
2031 guint
2032 gtk_input_add_full (gint                source,
2033                     GdkInputCondition   condition,
2034                     GdkInputFunction    function,
2035                     GtkCallbackMarshal  marshal,
2036                     gpointer            data,
2037                     GtkDestroyNotify    destroy)
2038 {
2039   if (marshal)
2040     {
2041       GtkClosure *closure;
2042
2043       closure = g_new (GtkClosure, 1);
2044       closure->marshal = marshal;
2045       closure->data = data;
2046       closure->destroy = destroy;
2047
2048       return gdk_input_add_full (source,
2049                                  condition,
2050                                  (GdkInputFunction) gtk_invoke_input,
2051                                  closure,
2052                                  (GdkDestroyNotify) gtk_destroy_closure);
2053     }
2054   else
2055     return gdk_input_add_full (source, condition, function, data, destroy);
2056 }
2057
2058 void
2059 gtk_input_remove (guint tag)
2060 {
2061   g_source_remove (tag);
2062 }
2063
2064 static void
2065 gtk_destroy_closure (gpointer data)
2066 {
2067   GtkClosure *closure = data;
2068
2069   if (closure->destroy)
2070     (closure->destroy) (closure->data);
2071   g_free (closure);
2072 }
2073
2074 static gboolean
2075 gtk_invoke_idle_timeout (gpointer data)
2076 {
2077   GtkClosure *closure = data;
2078
2079   GtkArg args[1];
2080   gint ret_val = FALSE;
2081   args[0].name = NULL;
2082   args[0].type = G_TYPE_BOOLEAN;
2083   args[0].d.pointer_data = &ret_val;
2084   closure->marshal (NULL, closure->data,  0, args);
2085   return ret_val;
2086 }
2087
2088 static void
2089 gtk_invoke_input (gpointer          data,
2090                   gint              source,
2091                   GdkInputCondition condition)
2092 {
2093   GtkClosure *closure = data;
2094
2095   GtkArg args[3];
2096   args[0].type = G_TYPE_INT;
2097   args[0].name = NULL;
2098   GTK_VALUE_INT (args[0]) = source;
2099   args[1].type = GDK_TYPE_INPUT_CONDITION;
2100   args[1].name = NULL;
2101   GTK_VALUE_FLAGS (args[1]) = condition;
2102   args[2].type = G_TYPE_NONE;
2103   args[2].name = NULL;
2104
2105   closure->marshal (NULL, closure->data, 2, args);
2106 }
2107
2108 /**
2109  * gtk_get_current_event:
2110  * 
2111  * Obtains a copy of the event currently being processed by GTK+.  For
2112  * example, if you get a "clicked" signal from #GtkButton, the current
2113  * event will be the #GdkEventButton that triggered the "clicked"
2114  * signal. The returned event must be freed with gdk_event_free().
2115  * If there is no current event, the function returns %NULL.
2116  * 
2117  * Return value: a copy of the current event, or %NULL if no current event.
2118  **/
2119 GdkEvent*
2120 gtk_get_current_event (void)
2121 {
2122   if (current_events)
2123     return gdk_event_copy (current_events->data);
2124   else
2125     return NULL;
2126 }
2127
2128 /**
2129  * gtk_get_current_event_time:
2130  * 
2131  * If there is a current event and it has a timestamp, return that
2132  * timestamp, otherwise return %GDK_CURRENT_TIME.
2133  * 
2134  * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2135  **/
2136 guint32
2137 gtk_get_current_event_time (void)
2138 {
2139   if (current_events)
2140     return gdk_event_get_time (current_events->data);
2141   else
2142     return GDK_CURRENT_TIME;
2143 }
2144
2145 /**
2146  * gtk_get_current_event_state:
2147  * @state: a location to store the state of the current event
2148  * 
2149  * If there is a current event and it has a state field, place
2150  * that state field in @state and return %TRUE, otherwise return
2151  * %FALSE.
2152  * 
2153  * Return value: %TRUE if there was a current event and it had a state field
2154  **/
2155 gboolean
2156 gtk_get_current_event_state (GdkModifierType *state)
2157 {
2158   g_return_val_if_fail (state != NULL, FALSE);
2159   
2160   if (current_events)
2161     return gdk_event_get_state (current_events->data, state);
2162   else
2163     {
2164       *state = 0;
2165       return FALSE;
2166     }
2167 }
2168
2169 /**
2170  * gtk_get_event_widget:
2171  * @event: a #GdkEvent
2172  *
2173  * If @event is %NULL or the event was not associated with any widget,
2174  * returns %NULL, otherwise returns the widget that received the event
2175  * originally.
2176  * 
2177  * Return value: the widget that originally received @event, or %NULL
2178  **/
2179 GtkWidget*
2180 gtk_get_event_widget (GdkEvent *event)
2181 {
2182   GtkWidget *widget;
2183
2184   widget = NULL;
2185   if (event && event->any.window)
2186     gdk_window_get_user_data (event->any.window, (void**) &widget);
2187   
2188   return widget;
2189 }
2190
2191 static gint
2192 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2193 {
2194   if (!quitf->marshal)
2195     return quitf->function (quitf->data);
2196   else
2197     {
2198       GtkArg args[1];
2199       gint ret_val = FALSE;
2200
2201       args[0].name = NULL;
2202       args[0].type = G_TYPE_BOOLEAN;
2203       args[0].d.pointer_data = &ret_val;
2204       ((GtkCallbackMarshal) quitf->marshal) (NULL,
2205                                              quitf->data,
2206                                              0, args);
2207       return ret_val;
2208     }
2209 }
2210
2211 /**
2212  * gtk_propagate_event:
2213  * @widget: a #GtkWidget
2214  * @event: an event
2215  *
2216  * Sends an event to a widget, propagating the event to parent widgets
2217  * if the event remains unhandled. Events received by GTK+ from GDK
2218  * normally begin in gtk_main_do_event(). Depending on the type of
2219  * event, existence of modal dialogs, grabs, etc., the event may be
2220  * propagated; if so, this function is used. gtk_propagate_event()
2221  * calls gtk_widget_event() on each widget it decides to send the
2222  * event to.  So gtk_widget_event() is the lowest-level function; it
2223  * simply emits the "event" and possibly an event-specific signal on a
2224  * widget.  gtk_propagate_event() is a bit higher-level, and
2225  * gtk_main_do_event() is the highest level.
2226  *
2227  * All that said, you most likely don't want to use any of these
2228  * functions; synthesizing events is rarely needed. Consider asking on
2229  * the mailing list for better ways to achieve your goals. For
2230  * example, use gdk_window_invalidate_rect() or
2231  * gtk_widget_queue_draw() instead of making up expose events.
2232  * 
2233  **/
2234 void
2235 gtk_propagate_event (GtkWidget *widget,
2236                      GdkEvent  *event)
2237 {
2238   gint handled_event;
2239   
2240   g_return_if_fail (GTK_IS_WIDGET (widget));
2241   g_return_if_fail (event != NULL);
2242   
2243   handled_event = FALSE;
2244
2245   g_object_ref (widget);
2246       
2247   if ((event->type == GDK_KEY_PRESS) ||
2248       (event->type == GDK_KEY_RELEASE))
2249     {
2250       /* Only send key events within Window widgets to the Window
2251        *  The Window widget will in turn pass the
2252        *  key event on to the currently focused widget
2253        *  for that window.
2254        */
2255       GtkWidget *window;
2256
2257       window = gtk_widget_get_toplevel (widget);
2258       if (window && GTK_IS_WINDOW (window))
2259         {
2260           /* If there is a grab within the window, give the grab widget
2261            * a first crack at the key event
2262            */
2263           if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2264             handled_event = gtk_widget_event (widget, event);
2265           
2266           if (!handled_event)
2267             {
2268               window = gtk_widget_get_toplevel (widget);
2269               if (window && GTK_IS_WINDOW (window))
2270                 {
2271                   if (GTK_WIDGET_IS_SENSITIVE (window))
2272                     gtk_widget_event (window, event);
2273                 }
2274             }
2275                   
2276           handled_event = TRUE; /* don't send to widget */
2277         }
2278     }
2279   
2280   /* Other events get propagated up the widget tree
2281    *  so that parents can see the button and motion
2282    *  events of the children.
2283    */
2284   if (!handled_event)
2285     {
2286       while (TRUE)
2287         {
2288           GtkWidget *tmp;
2289           
2290           handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2291           tmp = widget->parent;
2292           g_object_unref (widget);
2293
2294           widget = tmp;
2295           
2296           if (!handled_event && widget)
2297             g_object_ref (widget);
2298           else
2299             break;
2300         }
2301     }
2302   else
2303     g_object_unref (widget);
2304 }
2305
2306 #if 0
2307 static void
2308 gtk_error (gchar *str)
2309 {
2310   gtk_print (str);
2311 }
2312
2313 static void
2314 gtk_warning (gchar *str)
2315 {
2316   gtk_print (str);
2317 }
2318
2319 static void
2320 gtk_message (gchar *str)
2321 {
2322   gtk_print (str);
2323 }
2324
2325 static void
2326 gtk_print (gchar *str)
2327 {
2328   static GtkWidget *window = NULL;
2329   static GtkWidget *text;
2330   static int level = 0;
2331   GtkWidget *box1;
2332   GtkWidget *box2;
2333   GtkWidget *table;
2334   GtkWidget *hscrollbar;
2335   GtkWidget *vscrollbar;
2336   GtkWidget *separator;
2337   GtkWidget *button;
2338   
2339   if (level > 0)
2340     {
2341       fputs (str, stdout);
2342       fflush (stdout);
2343       return;
2344     }
2345   
2346   if (!window)
2347     {
2348       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2349       
2350       gtk_signal_connect (GTK_OBJECT (window), "destroy",
2351                           (GtkSignalFunc) gtk_widget_destroyed,
2352                           &window);
2353       
2354       gtk_window_set_title (GTK_WINDOW (window), "Messages");
2355       
2356       box1 = gtk_vbox_new (FALSE, 0);
2357       gtk_container_add (GTK_CONTAINER (window), box1);
2358       gtk_widget_show (box1);
2359       
2360       
2361       box2 = gtk_vbox_new (FALSE, 10);
2362       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2363       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2364       gtk_widget_show (box2);
2365       
2366       
2367       table = gtk_table_new (2, 2, FALSE);
2368       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2369       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2370       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2371       gtk_widget_show (table);
2372       
2373       text = gtk_text_new (NULL, NULL);
2374       gtk_text_set_editable (GTK_TEXT (text), FALSE);
2375       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2376       gtk_widget_show (text);
2377       gtk_widget_realize (text);
2378       
2379       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2380       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2381                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2382       gtk_widget_show (hscrollbar);
2383       
2384       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2385       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2386                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2387       gtk_widget_show (vscrollbar);
2388       
2389       separator = gtk_hseparator_new ();
2390       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2391       gtk_widget_show (separator);
2392       
2393       
2394       box2 = gtk_vbox_new (FALSE, 10);
2395       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2396       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2397       gtk_widget_show (box2);
2398       
2399       
2400       button = gtk_button_new_with_label ("close");
2401       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2402                                  (GtkSignalFunc) gtk_widget_hide,
2403                                  GTK_OBJECT (window));
2404       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2405       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2406       gtk_widget_grab_default (button);
2407       gtk_widget_show (button);
2408     }
2409   
2410   level += 1;
2411   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2412   level -= 1;
2413   
2414   if (!GTK_WIDGET_VISIBLE (window))
2415     gtk_widget_show (window);
2416 }
2417 #endif
2418
2419 gboolean
2420 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2421                                   GValue                *return_accu,
2422                                   const GValue          *handler_return,
2423                                   gpointer               dummy)
2424 {
2425   gboolean continue_emission;
2426   gboolean signal_handled;
2427   
2428   signal_handled = g_value_get_boolean (handler_return);
2429   g_value_set_boolean (return_accu, signal_handled);
2430   continue_emission = !signal_handled;
2431   
2432   return continue_emission;
2433 }