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