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