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