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