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