]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
The locale directory is passed to bindtextdomain() which isn't
[~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 GMemChunk *quit_mem_chunk = NULL;
148
149 static GSList *key_snoopers = NULL;
150
151 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
152
153 #ifdef G_ENABLE_DEBUG
154 static const GDebugKey gtk_debug_keys[] = {
155   {"misc", GTK_DEBUG_MISC},
156   {"plugsocket", GTK_DEBUG_PLUGSOCKET},
157   {"text", GTK_DEBUG_TEXT},
158   {"tree", GTK_DEBUG_TREE},
159   {"updates", GTK_DEBUG_UPDATES},
160   {"keybindings", GTK_DEBUG_KEYBINDINGS},
161   {"multihead", GTK_DEBUG_MULTIHEAD},
162   {"modules", GTK_DEBUG_MODULES},
163   {"geometry", GTK_DEBUG_GEOMETRY},
164   {"icontheme", GTK_DEBUG_ICONTHEME}
165 };
166
167 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
168
169 #endif /* G_ENABLE_DEBUG */
170
171 /**
172  * gtk_check_version:
173  * @required_major: the required major version.
174  * @required_minor: the required major version.
175  * @required_micro: the required major version.
176  * 
177  * Checks that the GTK+ library in use is compatible with the
178  * given version. Generally you would pass in the constants
179  * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
180  * as the three arguments to this function; that produces
181  * a check that the library in use is compatible with
182  * the version of GTK+ the application or module was compiled
183  * against.
184  *
185  * Compatibility is defined by two things: first the version
186  * of the running library is newer than the version
187  * @required_major.required_minor.@required_micro. Second
188  * the running library must be binary compatible with the
189  * version @required_major.required_minor.@required_micro
190  * (same major version.)
191  *
192  * This function is primarily for GTK+ modules; the module
193  * can call this function to check that it wasn't loaded
194  * into an incompatible version of GTK+. However, such a
195  * a check isn't completely reliable, since the module may be
196  * linked against an old version of GTK+ and calling the
197  * old version of gtk_check_version(), but still get loaded
198  * into an application using a newer version of GTK+.
199  *
200  * Return value: %NULL if the GTK+ library is compatible with the
201  *   given version, or a string describing the version mismatch.
202  *   The returned string is owned by GTK+ and should not be modified
203  *   or freed.
204  **/
205 gchar*
206 gtk_check_version (guint required_major,
207                    guint required_minor,
208                    guint required_micro)
209 {
210   gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
211   gint required_effective_micro = 100 * required_minor + required_micro;
212
213   if (required_major > GTK_MAJOR_VERSION)
214     return "Gtk+ version too old (major mismatch)";
215   if (required_major < GTK_MAJOR_VERSION)
216     return "Gtk+ version too new (major mismatch)";
217   if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
218     return "Gtk+ version too new (micro mismatch)";
219   if (required_effective_micro > gtk_effective_micro)
220     return "Gtk+ version too old (micro mismatch)";
221   return NULL;
222 }
223
224 /* This checks to see if the process is running suid or sgid
225  * at the current time. If so, we don't allow GTK+ to be initialized.
226  * This is meant to be a mild check - we only error out if we
227  * can prove the programmer is doing something wrong, not if
228  * they could be doing something wrong. For this reason, we
229  * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
230  */
231 static gboolean
232 check_setugid (void)
233 {
234 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
235 #ifndef G_OS_WIN32
236   uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
237   gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
238   
239 #ifdef HAVE_GETRESUID
240   /* These aren't in the header files, so we prototype them here.
241    */
242   int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
243   int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
244
245   if (getresuid (&ruid, &euid, &suid) != 0 ||
246       getresgid (&rgid, &egid, &sgid) != 0)
247 #endif /* HAVE_GETRESUID */
248     {
249       suid = ruid = getuid ();
250       sgid = rgid = getgid ();
251       euid = geteuid ();
252       egid = getegid ();
253     }
254
255   if (ruid != euid || ruid != suid ||
256       rgid != egid || rgid != sgid)
257     {
258       g_warning ("This process is currently running setuid or setgid.\n"
259                  "This is not a supported use of GTK+. You must create a helper\n"
260                  "program instead. For further details, see:\n\n"
261                  "    http://www.gtk.org/setuid.html\n\n"
262                  "Refusing to initialize GTK+.");
263       exit (1);
264     }
265 #endif
266   return TRUE;
267 }
268
269 #ifdef G_OS_WIN32
270
271 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
272
273 const gchar *
274 _gtk_get_datadir (void)
275 {
276   static char *gtk_datadir = NULL;
277   if (gtk_datadir == NULL)
278     gtk_datadir = g_win32_get_package_installation_subdirectory
279       (GETTEXT_PACKAGE, dll_name, "share");
280
281   return gtk_datadir;
282 }
283
284 const gchar *
285 _gtk_get_libdir (void)
286 {
287   static char *gtk_libdir = NULL;
288   if (gtk_libdir == NULL)
289     gtk_libdir = g_win32_get_package_installation_subdirectory
290       (GETTEXT_PACKAGE, dll_name, "lib");
291
292   return gtk_libdir;
293 }
294
295 const gchar *
296 _gtk_get_localedir (void)
297 {
298   static char *gtk_localedir = NULL;
299   if (gtk_localedir == NULL)
300     {
301       gchar *temp;
302       
303       temp = g_win32_get_package_installation_subdirectory
304         (GETTEXT_PACKAGE, dll_name, "lib\\locale");
305
306       /* gtk_localedir is passed to bindtextdomain() which isn't
307        * UTF-8-aware.
308        */
309       gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
310       g_free (temp);
311     }
312   return gtk_localedir;
313 }
314
315 const gchar *
316 _gtk_get_sysconfdir (void)
317 {
318   static char *gtk_sysconfdir = NULL;
319   if (gtk_sysconfdir == NULL)
320     gtk_sysconfdir = g_win32_get_package_installation_subdirectory
321       (GETTEXT_PACKAGE, dll_name, "etc");
322
323   return gtk_sysconfdir;
324 }
325
326 const gchar *
327 _gtk_get_data_prefix (void)
328 {
329   static char *gtk_data_prefix = NULL;
330   if (gtk_data_prefix == NULL)
331     gtk_data_prefix = g_win32_get_package_installation_directory
332       (GETTEXT_PACKAGE, dll_name);
333
334   return gtk_data_prefix;
335 }
336
337 #endif /* G_OS_WIN32 */
338
339 static gboolean do_setlocale = TRUE;
340
341 /**
342  * gtk_disable_setlocale:
343  * 
344  * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
345  * gtk_parse_args() from automatically
346  * calling <literal>setlocale (LC_ALL, "")</literal>. You would 
347  * want to use this function if you wanted to set the locale for 
348  * your program to something other than the user's locale, or if 
349  * you wanted to set different values for different locale categories.
350  *
351  * Most programs should not need to call this function.
352  **/
353 void
354 gtk_disable_setlocale (void)
355 {
356   if (gtk_initialized)
357     g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
358     
359   do_setlocale = FALSE;
360 }
361
362 #ifdef G_PLATFORM_WIN32
363 #undef gtk_init_check
364 #endif
365
366 static GString *gtk_modules_string = NULL;
367 static gboolean g_fatal_warnings = FALSE;
368
369 #ifdef G_ENABLE_DEBUG
370 static gboolean
371 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
372 {
373   gtk_debug_flags |= g_parse_debug_string (value,
374                                            gtk_debug_keys,
375                                            gtk_ndebug_keys);
376
377   return TRUE;
378 }
379
380 static gboolean
381 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
382 {
383   gtk_debug_flags &= ~g_parse_debug_string (value,
384                                             gtk_debug_keys,
385                                             gtk_ndebug_keys);
386
387   return TRUE;
388 }
389 #endif /* G_ENABLE_DEBUG */
390
391 static gboolean
392 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
393 {
394   if (value && *value)
395     {
396       if (gtk_modules_string)
397         g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
398       else
399         gtk_modules_string = g_string_new (NULL);
400       
401       g_string_append (gtk_modules_string, value);
402     }
403
404   return TRUE;
405 }
406
407 static GOptionEntry gtk_args[] = {
408   { "gtk-module",       0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,   
409     /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"), 
410     /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
411   { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, 
412     /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
413 #ifdef G_ENABLE_DEBUG
414   { "gtk-debug",        0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,    
415     /* Description of --gtk-debug=FLAGS in --help output */    N_("GTK+ debugging flags to set"), 
416     /* Placeholder in --gtk-debug=FLAGS in --help output */    N_("FLAGS") },
417   { "gtk-no-debug",     0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb, 
418     /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"), 
419     /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
420 #endif 
421   { NULL }
422 };
423
424 static void
425 do_pre_parse_initialization (int    *argc,
426                              char ***argv)
427 {
428   const gchar *env_string;
429   
430 #if     0
431   g_set_error_handler (gtk_error);
432   g_set_warning_handler (gtk_warning);
433   g_set_message_handler (gtk_message);
434   g_set_print_handler (gtk_print);
435 #endif
436
437   if (do_setlocale)
438     {
439       if (!setlocale (LC_ALL, ""))
440         g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
441     }
442
443   gdk_pre_parse_libgtk_only ();
444   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
445   
446 #ifdef G_ENABLE_DEBUG
447   env_string = g_getenv ("GTK_DEBUG");
448   if (env_string != NULL)
449     {
450       gtk_debug_flags = g_parse_debug_string (env_string,
451                                               gtk_debug_keys,
452                                               gtk_ndebug_keys);
453       env_string = NULL;
454     }
455 #endif  /* G_ENABLE_DEBUG */
456
457   env_string = g_getenv ("GTK_MODULES");
458   if (env_string)
459     gtk_modules_string = g_string_new (env_string);
460 }
461
462 static void
463 gettext_initialization (void)
464 {
465 #ifdef ENABLE_NLS
466   bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
467   bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
468 #    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
469   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
470   bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
471 #    endif
472 #endif  
473 }
474
475 static void
476 do_post_parse_initialization (int    *argc,
477                               char ***argv)
478 {
479   if (gtk_initialized)
480     return;
481
482   gettext_initialization ();
483
484   if (g_fatal_warnings)
485     {
486       GLogLevelFlags fatal_mask;
487
488       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
489       fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
490       g_log_set_always_fatal (fatal_mask);
491     }
492
493   if (gtk_debug_flags & GTK_DEBUG_UPDATES)
494     gdk_window_set_debug_updates (TRUE);
495
496   {
497   /* Translate to default:RTL if you want your widgets
498    * to be RTL, otherwise translate to default:LTR.
499    * Do *not* translate it to "predefinito:LTR", if it
500    * it isn't default:LTR or default:RTL it will not work 
501    */
502     char *e = _("default:LTR");
503     if (strcmp (e, "default:RTL")==0) {
504       gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
505     } else if (strcmp (e, "default:LTR")) {
506       g_warning ("Whoever translated default:LTR did so wrongly.\n");
507     }
508   }
509
510   gtk_type_init (0);
511   _gtk_accel_map_init ();  
512   _gtk_rc_init ();
513
514   /* Set the 'initialized' flag.
515    */
516   gtk_initialized = TRUE;
517
518   /* load gtk modules */
519   if (gtk_modules_string)
520     {
521       _gtk_modules_init (argc, argv, gtk_modules_string->str);
522       g_string_free (gtk_modules_string, TRUE);
523     }
524 }
525
526
527 typedef struct
528 {
529   gboolean open_default_display;
530 } OptionGroupInfo;
531
532 static gboolean
533 pre_parse_hook (GOptionContext *context,
534                 GOptionGroup   *group,
535                 gpointer        data,
536                 GError        **error)
537 {
538   do_pre_parse_initialization (NULL, NULL);
539   
540   return TRUE;
541 }
542
543 static gboolean
544 post_parse_hook (GOptionContext *context,
545                  GOptionGroup   *group,
546                  gpointer       data,
547                  GError        **error)
548 {
549   OptionGroupInfo *info = data;
550
551   
552   do_post_parse_initialization (NULL, NULL);
553   
554   if (info->open_default_display)
555     return gdk_display_open_default_libgtk_only () != NULL;
556   else
557     return TRUE;
558 }
559
560
561 /**
562  * gtk_get_option_group:
563  * @open_default_display: whether to open the default display 
564  *    when parsing the commandline arguments
565  * 
566  * Returns a #GOptionGroup for the commandline arguments recognized
567  * by GTK+ and GDK. You should add this group to your #GOptionContext 
568  * with g_option_context_add_group(), if you are using 
569  * g_option_context_parse() to parse your commandline arguments.
570  *
571  * Returns a #GOptionGroup for the commandline arguments recognized
572  *   by GTK+
573  *
574  * Since: 2.6
575  */
576 GOptionGroup *
577 gtk_get_option_group (gboolean open_default_display)
578 {
579   GOptionGroup *group;
580   OptionGroupInfo *info;
581
582   info = g_new0 (OptionGroupInfo, 1);
583   info->open_default_display = open_default_display;
584   
585   group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
586   g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
587
588   gdk_add_option_entries_libgtk_only (group);
589   g_option_group_add_entries (group, gtk_args);
590   g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
591   
592   return group;
593 }
594
595 /**
596  * gtk_init_with_args:
597  * @argc: a pointer to the number of command line arguments.
598  * @argv: a pointer to the array of command line arguments.
599  * @parameter_string: a string which is displayed in
600  *    the first line of <option>--help</option> output, after 
601  *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
602  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
603  *    describing the options of your program
604  * @translation_domain: a translation domain to use for translating
605  *    the <option>--help</option> output for the options in @entries
606  *    with gettext(), or %NULL
607  * @error: a return location for errors 
608  *
609  * This function does the same work as gtk_init_check(). 
610  * Additionally, it allows you to add your own commandline options, 
611  * and it automatically generates nicely formatted 
612  * <option>--help</option> output. Note that your program will
613  * be terminated after writing out the help output.
614  *
615  * Returns: %TRUE if the GUI has been successfully initialized, 
616  *               %FALSE otherwise.
617  * 
618  * Since: 2.6
619  */
620 gboolean
621 gtk_init_with_args (int            *argc,
622                     char         ***argv,
623                     char           *parameter_string,  
624                     GOptionEntry   *entries,
625                     char           *translation_domain,
626                     GError        **error)
627 {
628   GOptionContext *context;
629   GOptionGroup *gtk_group;
630   gboolean retval;
631
632   if (gtk_initialized)
633     return TRUE;
634
635   gettext_initialization ();
636
637   if (!check_setugid ())
638     return FALSE;
639
640   gtk_group = gtk_get_option_group (TRUE);
641   
642   context = g_option_context_new (parameter_string);
643   g_option_context_add_group (context, gtk_group);
644   
645   if (entries)
646     g_option_context_add_main_entries (context, entries, translation_domain);
647   retval = g_option_context_parse (context, argc, argv, error);
648   
649   g_option_context_free (context);
650
651   return retval;
652 }
653
654
655 /**
656  * gtk_parse_args:
657  * @argc: a pointer to the number of command line arguments.
658  * @argv: a pointer to the array of command line arguments.
659  * 
660  * Parses command line arguments, and initializes global
661  * attributes of GTK+, but does not actually open a connection
662  * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
663  *
664  * Any arguments used by GTK+ or GDK are removed from the array and
665  * @argc and @argv are updated accordingly.
666  *
667  * You shouldn't call this function explicitely if you are using
668  * gtk_init(), or gtk_init_check().
669  *
670  * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
671  **/
672 gboolean
673 gtk_parse_args (int    *argc,
674                 char ***argv)
675 {
676   GOptionContext *option_context;
677   GOptionGroup *gtk_group;
678   
679   if (gtk_initialized)
680     return TRUE;
681
682   gettext_initialization ();
683
684   if (!check_setugid ())
685     return FALSE;
686
687   option_context = g_option_context_new (NULL);
688   g_option_context_set_ignore_unknown_options (option_context, TRUE);
689   g_option_context_set_help_enabled (option_context, FALSE);
690   gtk_group = gtk_get_option_group (FALSE);
691   g_option_context_set_main_group (option_context, gtk_group);
692   g_option_context_parse (option_context, argc, argv, NULL);
693   g_option_context_free (option_context);
694
695   return TRUE;
696 }
697
698 #ifdef G_PLATFORM_WIN32
699 #undef gtk_init_check
700 #endif
701
702 /**
703  * gtk_init_check:
704  * @argc: Address of the <parameter>argc</parameter> parameter of your 
705  *   main() function. Changed if any arguments were handled.
706  * @argv: Address of the <parameter>argv</parameter> parameter of main(). 
707  *   Any parameters understood by gtk_init() are stripped before return.
708  * 
709  * This function does the same work as gtk_init() with only 
710  * a single change: It does not terminate the program if the GUI can't be 
711  * initialized. Instead it returns %FALSE on failure.
712  *
713  * This way the application can fall back to some other means of communication 
714  * with the user - for example a curses or command line interface.
715  * 
716  * Return value: %TRUE if the GUI has been successfully initialized, 
717  *               %FALSE otherwise.
718  **/
719 gboolean
720 gtk_init_check (int      *argc,
721                 char   ***argv)
722 {
723   if (!gtk_parse_args (argc, argv))
724     return FALSE;
725
726   return gdk_display_open_default_libgtk_only () != NULL;
727 }
728
729 #ifdef G_PLATFORM_WIN32
730 #undef gtk_init
731 #endif
732
733 /**
734  * gtk_init:
735  * @argc: Address of the <parameter>argc</parameter> parameter of your 
736  *   main() function. Changed if any arguments were handled.
737  * @argv: Address of the <parameter>argv</parameter> parameter of main(). 
738  *   Any parameters understood by gtk_init() are stripped before return.
739  * 
740  * Call this function before using any other GTK+ functions in your GUI
741  * applications.  It will initialize everything needed to operate the 
742  * toolkit and parses some standard command line options. @argc and 
743  * @argv are adjusted accordingly so your own code will 
744  * never see those standard arguments. 
745  *
746  * Note that there are some alternative ways to initialize GTK+: 
747  * if you are calling gtk_parse_args(), gtk_init_check(), 
748  * gtk_init_with_args() or g_option_context_parse() with 
749  * the option group returned by gtk_get_option_group(), you 
750  * <emphasis>don't</emphasis> have to call gtk_init().
751  *
752  * <note><para>
753  * This function will terminate your program if it was unable to initialize 
754  * the GUI for some reason. If you want your program to fall back to a 
755  * textual interface you want to call gtk_init_check() instead.
756  * </para></note>
757  **/
758 void
759 gtk_init (int *argc, char ***argv)
760 {
761   if (!gtk_init_check (argc, argv))
762     {
763       const char *display_name_arg = gdk_get_display_arg_name ();
764       g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
765       exit (1);
766     }
767 }
768
769 #ifdef G_PLATFORM_WIN32
770
771 static void
772 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
773 {
774   if (sizeof_GtkWindow != sizeof (GtkWindow))
775     g_error ("Incompatible build!\n"
776              "The code using GTK+ thinks GtkWindow is of different\n"
777              "size than it actually is in this build of GTK+.\n"
778              "On Windows, this probably means that you have compiled\n"
779              "your code with gcc without the -mms-bitfields switch,\n"
780              "or that you are using an unsupported compiler.");
781 }
782
783 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
784  * gcc-compiled code on Win32 whether compiled with -fnative-struct or
785  * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
786  * from GTK+ 2.0.2 on, check some other struct, too, where the use of
787  * -fnative-struct still matters. GtkBox is one such.
788  */
789 static void
790 check_sizeof_GtkBox (size_t sizeof_GtkBox)
791 {
792   if (sizeof_GtkBox != sizeof (GtkBox))
793     g_error ("Incompatible build!\n"
794              "The code using GTK+ thinks GtkBox is of different\n"
795              "size than it actually is in this build of GTK+.\n"
796              "On Windows, this probably means that you have compiled\n"
797              "your code with gcc without the -mms-bitfields switch,\n"
798              "or that you are using an unsupported compiler.");
799 }
800
801 /* These two functions might get more checks added later, thus pass
802  * in the number of extra args.
803  */
804 void
805 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
806 {
807   check_sizeof_GtkWindow (sizeof_GtkWindow);
808   if (num_checks >= 2)
809     check_sizeof_GtkBox (sizeof_GtkBox);
810   gtk_init (argc, argv);
811 }
812
813 gboolean
814 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
815 {
816   check_sizeof_GtkWindow (sizeof_GtkWindow);
817   if (num_checks >= 2)
818     check_sizeof_GtkBox (sizeof_GtkBox);
819   return gtk_init_check (argc, argv);
820 }
821
822 #endif
823
824 void
825 gtk_exit (gint errorcode)
826 {
827   exit (errorcode);
828 }
829
830
831 /**
832  * gtk_set_locale:
833  *
834  * Initializes internationalization support for GTK+. gtk_init()
835  * automatically does this, so there is typically no point
836  * in calling this function.
837  *
838  * If you are calling this function because you changed the locale
839  * after GTK+ is was initialized, then calling this function
840  * may help a bit. (Note, however, that changing the locale
841  * after GTK+ is initialized may produce inconsistent results and
842  * is not really supported.)
843  * 
844  * In detail - sets the current locale according to the
845  * program environment. This is the same as calling the C library function
846  * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the 
847  * locale specific setup of the windowing system used by GDK.
848  * 
849  * Returns: a string corresponding to the locale set, typically in the
850  * form lang_COUNTRY, where lang is an ISO-639 language code, and
851  * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
852  * result of the setlocale(); it is also used on other machines, such as 
853  * Windows, where the C library returns a different result. The string is 
854  * owned by GTK+ and should not be modified or freed.
855  **/
856 gchar *
857 gtk_set_locale (void)
858 {
859   return gdk_set_locale ();
860 }
861
862 /**
863  * _gtk_get_lc_ctype:
864  *
865  * Return the Unix-style locale string for the language currently in
866  * effect. On Unix systems, this is the return value from
867  * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
868  * affect this through the environment variables LC_ALL, LC_CTYPE or
869  * LANG (checked in that order). The locale strings typically is in
870  * the form lang_COUNTRY, where lang is an ISO-639 language code, and
871  * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
872  * Swedish as written in Finland or pt_BR for Portuguese as written in
873  * Brazil.
874  * 
875  * On Windows, the C library doesn't use any such environment
876  * variables, and setting them won't affect the behaviour of functions
877  * like ctime(). The user sets the locale through the Regional Options 
878  * in the Control Panel. The C library (in the setlocale() function) 
879  * does not use country and language codes, but country and language 
880  * names spelled out in English. 
881  * However, this function does check the above environment
882  * variables, and does return a Unix-style locale string based on
883  * either said environment variables or the thread's current locale.
884  *
885  * Return value: a dynamically allocated string, free with g_free().
886  */
887
888 gchar *
889 _gtk_get_lc_ctype (void)
890 {
891 #ifdef G_OS_WIN32
892   /* Somebody might try to set the locale for this process using the
893    * LANG or LC_ environment variables. The Microsoft C library
894    * doesn't know anything about them. You set the locale in the
895    * Control Panel. Setting these env vars won't have any affect on
896    * locale-dependent C library functions like ctime(). But just for
897    * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
898    * it easier to test GTK and Pango in various default languages, you
899    * don't have to clickety-click in the Control Panel, you can simply
900    * start the program with LC_ALL=something on the command line.)
901    */
902   gchar *p;
903
904   p = getenv ("LC_ALL");
905   if (p != NULL)
906     return g_strdup (p);
907
908   p = getenv ("LC_CTYPE");
909   if (p != NULL)
910     return g_strdup (p);
911
912   p = getenv ("LANG");
913   if (p != NULL)
914     return g_strdup (p);
915
916   return g_win32_getlocale ();
917 #else
918   return g_strdup (setlocale (LC_CTYPE, NULL));
919 #endif
920 }
921
922 /**
923  * gtk_get_default_language:
924  *
925  * Returns the #PangoLanguage for the default language currently in
926  * effect. (Note that this can change over the life of an
927  * application.)  The default language is derived from the current
928  * locale. It determines, for example, whether GTK+ uses the
929  * right-to-left or left-to-right text direction. See
930  * _gtk_get_lc_ctype() for notes on behaviour on Windows.
931  * 
932  * Return value: the default language as a #PangoLanguage, must not be
933  * freed
934  **/
935 PangoLanguage *
936 gtk_get_default_language (void)
937 {
938   gchar *lang;
939   PangoLanguage *result;
940   gchar *p;
941   
942   lang = _gtk_get_lc_ctype ();
943   p = strchr (lang, '.');
944   if (p)
945     *p = '\0';
946   p = strchr (lang, '@');
947   if (p)
948     *p = '\0';
949
950   result = pango_language_from_string (lang);
951   g_free (lang);
952   
953   return result;
954 }
955
956 void
957 gtk_main (void)
958 {
959   GList *tmp_list;
960   GList *functions;
961   GtkInitFunction *init;
962   GMainLoop *loop;
963
964   gtk_main_loop_level++;
965   
966   loop = g_main_loop_new (NULL, TRUE);
967   main_loops = g_slist_prepend (main_loops, loop);
968
969   tmp_list = functions = init_functions;
970   init_functions = NULL;
971   
972   while (tmp_list)
973     {
974       init = tmp_list->data;
975       tmp_list = tmp_list->next;
976       
977       (* init->function) (init->data);
978       g_free (init);
979     }
980   g_list_free (functions);
981
982   if (g_main_loop_is_running (main_loops->data))
983     {
984       GDK_THREADS_LEAVE ();
985       g_main_loop_run (loop);
986       GDK_THREADS_ENTER ();
987       gdk_flush ();
988     }
989
990   if (quit_functions)
991     {
992       GList *reinvoke_list = NULL;
993       GtkQuitFunction *quitf;
994
995       while (quit_functions)
996         {
997           quitf = quit_functions->data;
998
999           tmp_list = quit_functions;
1000           quit_functions = g_list_remove_link (quit_functions, quit_functions);
1001           g_list_free_1 (tmp_list);
1002
1003           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1004               gtk_quit_invoke_function (quitf))
1005             {
1006               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1007             }
1008           else
1009             {
1010               gtk_quit_destroy (quitf);
1011             }
1012         }
1013       if (reinvoke_list)
1014         {
1015           GList *work;
1016           
1017           work = g_list_last (reinvoke_list);
1018           if (quit_functions)
1019             quit_functions->prev = work;
1020           work->next = quit_functions;
1021           quit_functions = work;
1022         }
1023
1024       gdk_flush ();
1025     }
1026     
1027   main_loops = g_slist_remove (main_loops, loop);
1028
1029   g_main_loop_unref (loop);
1030
1031   gtk_main_loop_level--;
1032
1033   /* Try storing all clipboard data we have */
1034   if (gtk_main_loop_level == 0)
1035     _gtk_clipboard_store_all ();
1036 }
1037
1038 guint
1039 gtk_main_level (void)
1040 {
1041   return gtk_main_loop_level;
1042 }
1043
1044 void
1045 gtk_main_quit (void)
1046 {
1047   g_return_if_fail (main_loops != NULL);
1048
1049   g_main_loop_quit (main_loops->data);
1050 }
1051
1052 gboolean
1053 gtk_events_pending (void)
1054 {
1055   gboolean result;
1056   
1057   GDK_THREADS_LEAVE ();  
1058   result = g_main_context_pending (NULL);
1059   GDK_THREADS_ENTER ();
1060
1061   return result;
1062 }
1063
1064 gboolean
1065 gtk_main_iteration (void)
1066 {
1067   GDK_THREADS_LEAVE ();
1068   g_main_context_iteration (NULL, TRUE);
1069   GDK_THREADS_ENTER ();
1070
1071   if (main_loops)
1072     return !g_main_loop_is_running (main_loops->data);
1073   else
1074     return TRUE;
1075 }
1076
1077 gboolean
1078 gtk_main_iteration_do (gboolean blocking)
1079 {
1080   GDK_THREADS_LEAVE ();
1081   g_main_context_iteration (NULL, blocking);
1082   GDK_THREADS_ENTER ();
1083
1084   if (main_loops)
1085     return !g_main_loop_is_running (main_loops->data);
1086   else
1087     return TRUE;
1088 }
1089
1090 /* private libgtk to libgdk interfaces
1091  */
1092 gboolean gdk_pointer_grab_info_libgtk_only  (GdkDisplay *display,
1093                                              GdkWindow **grab_window,
1094                                              gboolean   *owner_events);
1095 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1096                                              GdkWindow **grab_window,
1097                                              gboolean   *owner_events);
1098
1099 static void
1100 rewrite_events_translate (GdkWindow *old_window,
1101                           GdkWindow *new_window,
1102                           gdouble   *x,
1103                           gdouble   *y)
1104 {
1105   gint old_origin_x, old_origin_y;
1106   gint new_origin_x, new_origin_y;
1107
1108   gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1109   gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1110
1111   *x += old_origin_x - new_origin_x;
1112   *y += old_origin_y - new_origin_y;
1113 }
1114
1115 static GdkEvent *
1116 rewrite_event_for_window (GdkEvent  *event,
1117                           GdkWindow *new_window)
1118 {
1119   event = gdk_event_copy (event);
1120
1121   switch (event->type)
1122     {
1123     case GDK_SCROLL:
1124       rewrite_events_translate (event->any.window,
1125                                 new_window,
1126                                 &event->scroll.x, &event->scroll.y);
1127       break;
1128     case GDK_BUTTON_PRESS:
1129     case GDK_2BUTTON_PRESS:
1130     case GDK_3BUTTON_PRESS:
1131     case GDK_BUTTON_RELEASE:
1132       rewrite_events_translate (event->any.window,
1133                                 new_window,
1134                                 &event->button.x, &event->button.y);
1135       break;
1136     case GDK_MOTION_NOTIFY:
1137       rewrite_events_translate (event->any.window,
1138                                 new_window,
1139                                 &event->motion.x, &event->motion.y);
1140       break;
1141     case GDK_KEY_PRESS:
1142     case GDK_KEY_RELEASE:
1143     case GDK_PROXIMITY_IN:
1144     case GDK_PROXIMITY_OUT:
1145       break;
1146
1147     default:
1148       return event;
1149     }
1150
1151   g_object_unref (event->any.window);
1152   event->any.window = g_object_ref (new_window);
1153
1154   return event;
1155 }
1156
1157 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1158  * then what X11 does is deliver the event normally if it was going to this
1159  * client, otherwise, delivers it in terms of the grab window. This function
1160  * rewrites events to the effect that events going to the same window group
1161  * are delivered normally, otherwise, the event is delivered in terms of the
1162  * grab window.
1163  */
1164 static GdkEvent *
1165 rewrite_event_for_grabs (GdkEvent *event)
1166 {
1167   GdkWindow *grab_window;
1168   GtkWidget *event_widget, *grab_widget;
1169   gboolean owner_events;
1170   GdkDisplay *display;
1171
1172   switch (event->type)
1173     {
1174     case GDK_SCROLL:
1175     case GDK_BUTTON_PRESS:
1176     case GDK_2BUTTON_PRESS:
1177     case GDK_3BUTTON_PRESS:
1178     case GDK_BUTTON_RELEASE:
1179     case GDK_MOTION_NOTIFY:
1180     case GDK_PROXIMITY_IN:
1181     case GDK_PROXIMITY_OUT:
1182       display = gdk_drawable_get_display (event->proximity.window);
1183       if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1184           !owner_events)
1185         return NULL;
1186       break;
1187
1188     case GDK_KEY_PRESS:
1189     case GDK_KEY_RELEASE:
1190       display = gdk_drawable_get_display (event->key.window);
1191       if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1192           !owner_events)
1193         return NULL;
1194       break;
1195
1196     default:
1197       return NULL;
1198     }
1199
1200   event_widget = gtk_get_event_widget (event);
1201   gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1202
1203   if (grab_widget &&
1204       gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1205     return rewrite_event_for_window (event, grab_window);
1206   else
1207     return NULL;
1208 }
1209
1210 void 
1211 gtk_main_do_event (GdkEvent *event)
1212 {
1213   GtkWidget *event_widget;
1214   GtkWidget *grab_widget;
1215   GtkWindowGroup *window_group;
1216   GdkEvent *next_event;
1217   GdkEvent *rewritten_event = NULL;
1218   GList *tmp_list;
1219
1220   /* If there are any events pending then get the next one.
1221    */
1222   next_event = gdk_event_peek ();
1223   
1224   /* Try to compress enter/leave notify events. These event
1225    *  pairs occur when the mouse is dragged quickly across
1226    *  a window with many buttons (or through a menu). Instead
1227    *  of highlighting and de-highlighting each widget that
1228    *  is crossed it is better to simply de-highlight the widget
1229    *  which contained the mouse initially and highlight the
1230    *  widget which ends up containing the mouse.
1231    */
1232   if (next_event)
1233     if (((event->type == GDK_ENTER_NOTIFY) ||
1234          (event->type == GDK_LEAVE_NOTIFY)) &&
1235         ((next_event->type == GDK_ENTER_NOTIFY) ||
1236          (next_event->type == GDK_LEAVE_NOTIFY)) &&
1237         (next_event->type != event->type) &&
1238         (next_event->any.window == event->any.window))
1239       {
1240         /* Throw both the peeked copy and the queued copy away 
1241          */
1242         gdk_event_free (next_event);
1243         next_event = gdk_event_get ();
1244         gdk_event_free (next_event);
1245         
1246         return;
1247       }
1248
1249   if (next_event)
1250     gdk_event_free (next_event);
1251
1252   if (event->type == GDK_SETTING)
1253     {
1254       _gtk_settings_handle_event (&event->setting);
1255       return;
1256     }
1257
1258   if (event->type == GDK_OWNER_CHANGE)
1259     {
1260       _gtk_clipboard_handle_event (&event->owner_change);
1261       return;
1262     }
1263
1264   /* Find the widget which got the event. We store the widget
1265    *  in the user_data field of GdkWindow's.
1266    *  Ignore the event if we don't have a widget for it, except
1267    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
1268    *  Though this happens rarely, bogus events can occour
1269    *  for e.g. destroyed GdkWindows. 
1270    */
1271   event_widget = gtk_get_event_widget (event);
1272   if (!event_widget)
1273     {
1274       /* To handle selection INCR transactions, we select
1275        * PropertyNotify events on the requestor window and create
1276        * a corresponding (fake) GdkWindow so that events get
1277        * here. There won't be a widget though, so we have to handle
1278            * them specially
1279            */
1280       if (event->type == GDK_PROPERTY_NOTIFY)
1281         _gtk_selection_incr_event (event->any.window,
1282                                    &event->property);
1283
1284       return;
1285     }
1286
1287   /* If pointer or keyboard grabs are in effect, munge the events
1288    * so that each window group looks like a separate app.
1289    */
1290   rewritten_event = rewrite_event_for_grabs (event);
1291   if (rewritten_event)
1292     {
1293       event = rewritten_event;
1294       event_widget = gtk_get_event_widget (event);
1295     }
1296   
1297   window_group = gtk_main_get_window_group (event_widget);
1298
1299   /* Push the event onto a stack of current events for
1300    * gtk_current_event_get().
1301    */
1302   current_events = g_list_prepend (current_events, event);
1303
1304   /* If there is a grab in effect...
1305    */
1306   if (window_group->grabs)
1307     {
1308       grab_widget = window_group->grabs->data;
1309       
1310       /* If the grab widget is an ancestor of the event widget
1311        *  then we send the event to the original event widget.
1312        *  This is the key to implementing modality.
1313        */
1314       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1315           gtk_widget_is_ancestor (event_widget, grab_widget))
1316         grab_widget = event_widget;
1317     }
1318   else
1319     {
1320       grab_widget = event_widget;
1321     }
1322
1323   /* Not all events get sent to the grabbing widget.
1324    * The delete, destroy, expose, focus change and resize
1325    *  events still get sent to the event widget because
1326    *  1) these events have no meaning for the grabbing widget
1327    *  and 2) redirecting these events to the grabbing widget
1328    *  could cause the display to be messed up.
1329    * 
1330    * Drag events are also not redirected, since it isn't
1331    *  clear what the semantics of that would be.
1332    */
1333   switch (event->type)
1334     {
1335     case GDK_NOTHING:
1336       break;
1337       
1338     case GDK_DELETE:
1339       g_object_ref (event_widget);
1340       if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1341           !gtk_widget_event (event_widget, event))
1342         gtk_widget_destroy (event_widget);
1343       g_object_unref (event_widget);
1344       break;
1345       
1346     case GDK_DESTROY:
1347       /* Unexpected GDK_DESTROY from the outside, ignore for
1348        * child windows, handle like a GDK_DELETE for toplevels
1349        */
1350       if (!event_widget->parent)
1351         {
1352           g_object_ref (event_widget);
1353           if (!gtk_widget_event (event_widget, event) &&
1354               GTK_WIDGET_REALIZED (event_widget))
1355             gtk_widget_destroy (event_widget);
1356           g_object_unref (event_widget);
1357         }
1358       break;
1359       
1360     case GDK_EXPOSE:
1361       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1362         {
1363           gdk_window_begin_paint_region (event->any.window, event->expose.region);
1364           gtk_widget_send_expose (event_widget, event);
1365           gdk_window_end_paint (event->any.window);
1366         }
1367       else
1368         gtk_widget_send_expose (event_widget, event);
1369       break;
1370
1371     case GDK_PROPERTY_NOTIFY:
1372     case GDK_NO_EXPOSE:
1373     case GDK_FOCUS_CHANGE:
1374     case GDK_CONFIGURE:
1375     case GDK_MAP:
1376     case GDK_UNMAP:
1377     case GDK_SELECTION_CLEAR:
1378     case GDK_SELECTION_REQUEST:
1379     case GDK_SELECTION_NOTIFY:
1380     case GDK_CLIENT_EVENT:
1381     case GDK_VISIBILITY_NOTIFY:
1382     case GDK_WINDOW_STATE:
1383     case GDK_GRAB_BROKEN:
1384       gtk_widget_event (event_widget, event);
1385       break;
1386
1387     case GDK_SCROLL:
1388     case GDK_BUTTON_PRESS:
1389     case GDK_2BUTTON_PRESS:
1390     case GDK_3BUTTON_PRESS:
1391       gtk_propagate_event (grab_widget, event);
1392       break;
1393
1394     case GDK_KEY_PRESS:
1395     case GDK_KEY_RELEASE:
1396       if (key_snoopers)
1397         {
1398           if (gtk_invoke_key_snoopers (grab_widget, event))
1399             break;
1400         }
1401       /* else fall through */
1402     case GDK_MOTION_NOTIFY:
1403     case GDK_BUTTON_RELEASE:
1404     case GDK_PROXIMITY_IN:
1405     case GDK_PROXIMITY_OUT:
1406       gtk_propagate_event (grab_widget, event);
1407       break;
1408       
1409     case GDK_ENTER_NOTIFY:
1410       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1411         {
1412           g_object_ref (event_widget);
1413           
1414           gtk_widget_event (grab_widget, event);
1415           if (event_widget == grab_widget)
1416             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1417           
1418           g_object_unref (event_widget);
1419         }
1420       break;
1421       
1422     case GDK_LEAVE_NOTIFY:
1423       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1424         {
1425           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1426           gtk_widget_event (event_widget, event);
1427         }
1428       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1429         gtk_widget_event (grab_widget, event);
1430       break;
1431       
1432     case GDK_DRAG_STATUS:
1433     case GDK_DROP_FINISHED:
1434       _gtk_drag_source_handle_event (event_widget, event);
1435       break;
1436     case GDK_DRAG_ENTER:
1437     case GDK_DRAG_LEAVE:
1438     case GDK_DRAG_MOTION:
1439     case GDK_DROP_START:
1440       _gtk_drag_dest_handle_event (event_widget, event);
1441       break;
1442     default:
1443       g_assert_not_reached ();
1444       break;
1445     }
1446   
1447   tmp_list = current_events;
1448   current_events = g_list_remove_link (current_events, tmp_list);
1449   g_list_free_1 (tmp_list);
1450
1451   if (rewritten_event)
1452     gdk_event_free (rewritten_event);
1453 }
1454
1455 gboolean
1456 gtk_true (void)
1457 {
1458   return TRUE;
1459 }
1460
1461 gboolean
1462 gtk_false (void)
1463 {
1464   return FALSE;
1465 }
1466
1467 static GtkWindowGroup *
1468 gtk_main_get_window_group (GtkWidget   *widget)
1469 {
1470   GtkWidget *toplevel = NULL;
1471
1472   if (widget)
1473     toplevel = gtk_widget_get_toplevel (widget);
1474
1475   if (toplevel && GTK_IS_WINDOW (toplevel))
1476     return _gtk_window_get_group (GTK_WINDOW (toplevel));
1477   else
1478     return _gtk_window_get_group (NULL);
1479 }
1480
1481 typedef struct
1482 {
1483   GtkWidget *old_grab_widget;
1484   GtkWidget *new_grab_widget;
1485 } GrabNotifyInfo;
1486
1487 static gboolean
1488 check_is_grabbed (GtkWidget *widget,
1489                   GtkWidget *grab_widget)
1490 {
1491   if (grab_widget)
1492     return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1493   else
1494     return FALSE;
1495 }
1496
1497 static void
1498 gtk_grab_notify_foreach (GtkWidget *child,
1499                          gpointer   data)
1500                         
1501 {
1502   GrabNotifyInfo *info = data;
1503   gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1504   gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1505
1506   if (was_grabbed != is_grabbed)
1507     {
1508       g_object_ref (child);
1509
1510       _gtk_widget_grab_notify (child, was_grabbed);
1511       
1512       if (GTK_IS_CONTAINER (child))
1513         gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1514       
1515       g_object_unref (child);
1516     }
1517 }
1518
1519 static void
1520 gtk_grab_notify (GtkWindowGroup *group,
1521                  GtkWidget      *grab_widget,
1522                  gboolean        was_grabbed)
1523 {
1524   GList *toplevels;
1525   GrabNotifyInfo info;
1526
1527   if (was_grabbed)
1528     {
1529       info.old_grab_widget = grab_widget;
1530       info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1531     }
1532   else
1533     {
1534       info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1535       info.new_grab_widget = grab_widget;
1536     }
1537
1538   g_object_ref (group);
1539   g_object_ref (grab_widget);
1540
1541   toplevels = gtk_window_list_toplevels ();
1542   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1543                             
1544   while (toplevels)
1545     {
1546       GtkWindow *toplevel = toplevels->data;
1547       toplevels = g_list_delete_link (toplevels, toplevels);
1548
1549       if (group == _gtk_window_get_group (toplevel))
1550         gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1551       g_object_unref (toplevel);
1552     }
1553
1554   g_object_unref (group);
1555   g_object_unref (grab_widget);
1556 }
1557
1558 void
1559 gtk_grab_add (GtkWidget *widget)
1560 {
1561   GtkWindowGroup *group;
1562   
1563   g_return_if_fail (widget != NULL);
1564   
1565   if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1566     {
1567       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1568       
1569       group = gtk_main_get_window_group (widget);
1570
1571       g_object_ref (widget);
1572       group->grabs = g_slist_prepend (group->grabs, widget);
1573
1574       gtk_grab_notify (group, widget, FALSE);
1575     }
1576 }
1577
1578 GtkWidget*
1579 gtk_grab_get_current (void)
1580 {
1581   GtkWindowGroup *group;
1582
1583   group = gtk_main_get_window_group (NULL);
1584
1585   if (group->grabs)
1586     return GTK_WIDGET (group->grabs->data);
1587   return NULL;
1588 }
1589
1590 void
1591 gtk_grab_remove (GtkWidget *widget)
1592 {
1593   GtkWindowGroup *group;
1594   
1595   g_return_if_fail (widget != NULL);
1596   
1597   if (GTK_WIDGET_HAS_GRAB (widget))
1598     {
1599       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1600
1601       group = gtk_main_get_window_group (widget);
1602       group->grabs = g_slist_remove (group->grabs, widget);
1603       
1604       g_object_unref (widget);
1605
1606       gtk_grab_notify (group, widget, TRUE);
1607     }
1608 }
1609
1610 void
1611 gtk_init_add (GtkFunction function,
1612               gpointer    data)
1613 {
1614   GtkInitFunction *init;
1615   
1616   init = g_new (GtkInitFunction, 1);
1617   init->function = function;
1618   init->data = data;
1619   
1620   init_functions = g_list_prepend (init_functions, init);
1621 }
1622
1623 guint
1624 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1625                          gpointer        func_data)
1626 {
1627   GtkKeySnooperData *data;
1628   static guint snooper_id = 1;
1629
1630   g_return_val_if_fail (snooper != NULL, 0);
1631
1632   data = g_new (GtkKeySnooperData, 1);
1633   data->func = snooper;
1634   data->func_data = func_data;
1635   data->id = snooper_id++;
1636   key_snoopers = g_slist_prepend (key_snoopers, data);
1637
1638   return data->id;
1639 }
1640
1641 void
1642 gtk_key_snooper_remove (guint snooper_id)
1643 {
1644   GtkKeySnooperData *data = NULL;
1645   GSList *slist;
1646
1647   slist = key_snoopers;
1648   while (slist)
1649     {
1650       data = slist->data;
1651       if (data->id == snooper_id)
1652         break;
1653
1654       slist = slist->next;
1655       data = NULL;
1656     }
1657   if (data)
1658     {
1659       key_snoopers = g_slist_remove (key_snoopers, data);
1660       g_free (data);
1661     }
1662 }
1663
1664 static gint
1665 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1666                          GdkEvent  *event)
1667 {
1668   GSList *slist;
1669   gint return_val = FALSE;
1670
1671   slist = key_snoopers;
1672   while (slist && !return_val)
1673     {
1674       GtkKeySnooperData *data;
1675
1676       data = slist->data;
1677       slist = slist->next;
1678       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1679     }
1680
1681   return return_val;
1682 }
1683
1684 guint
1685 gtk_quit_add_full (guint                main_level,
1686                    GtkFunction          function,
1687                    GtkCallbackMarshal   marshal,
1688                    gpointer             data,
1689                    GtkDestroyNotify     destroy)
1690 {
1691   static guint quit_id = 1;
1692   GtkQuitFunction *quitf;
1693   
1694   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1695
1696   if (!quit_mem_chunk)
1697     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1698                                       512, G_ALLOC_AND_FREE);
1699   
1700   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
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_mem_chunk_free (quit_mem_chunk, 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"