]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
20e0e6e181b667f24b1753392eace837c5da76f5
[~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   /* Find the widget which got the event. We store the widget
1355    *  in the user_data field of GdkWindow's.
1356    *  Ignore the event if we don't have a widget for it, except
1357    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
1358    *  Though this happens rarely, bogus events can occour
1359    *  for e.g. destroyed GdkWindows. 
1360    */
1361   event_widget = gtk_get_event_widget (event);
1362   if (!event_widget)
1363     {
1364       /* To handle selection INCR transactions, we select
1365        * PropertyNotify events on the requestor window and create
1366        * a corresponding (fake) GdkWindow so that events get
1367        * here. There won't be a widget though, so we have to handle
1368            * them specially
1369            */
1370       if (event->type == GDK_PROPERTY_NOTIFY)
1371         _gtk_selection_incr_event (event->any.window,
1372                                    &event->property);
1373       else if (event->type == GDK_SETTING)
1374         _gtk_settings_handle_event (&event->setting);
1375
1376       return;
1377     }
1378
1379   /* If pointer or keyboard grabs are in effect, munge the events
1380    * so that each window group looks like a separate app.
1381    */
1382   rewritten_event = rewrite_event_for_grabs (event);
1383   if (rewritten_event)
1384     {
1385       event = rewritten_event;
1386       event_widget = gtk_get_event_widget (event);
1387     }
1388   
1389   window_group = gtk_main_get_window_group (event_widget);
1390
1391   /* Push the event onto a stack of current events for
1392    * gtk_current_event_get().
1393    */
1394   current_events = g_list_prepend (current_events, event);
1395
1396   /* If there is a grab in effect...
1397    */
1398   if (window_group->grabs)
1399     {
1400       grab_widget = window_group->grabs->data;
1401       
1402       /* If the grab widget is an ancestor of the event widget
1403        *  then we send the event to the original event widget.
1404        *  This is the key to implementing modality.
1405        */
1406       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1407           gtk_widget_is_ancestor (event_widget, grab_widget))
1408         grab_widget = event_widget;
1409     }
1410   else
1411     {
1412       grab_widget = event_widget;
1413     }
1414
1415   /* Not all events get sent to the grabbing widget.
1416    * The delete, destroy, expose, focus change and resize
1417    *  events still get sent to the event widget because
1418    *  1) these events have no meaning for the grabbing widget
1419    *  and 2) redirecting these events to the grabbing widget
1420    *  could cause the display to be messed up.
1421    * 
1422    * Drag events are also not redirected, since it isn't
1423    *  clear what the semantics of that would be.
1424    */
1425   switch (event->type)
1426     {
1427     case GDK_NOTHING:
1428       break;
1429       
1430     case GDK_DELETE:
1431       g_object_ref (event_widget);
1432       if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1433           !gtk_widget_event (event_widget, event))
1434         gtk_widget_destroy (event_widget);
1435       g_object_unref (event_widget);
1436       break;
1437       
1438     case GDK_DESTROY:
1439       /* Unexpected GDK_DESTROY from the outside, ignore for
1440        * child windows, handle like a GDK_DELETE for toplevels
1441        */
1442       if (!event_widget->parent)
1443         {
1444           g_object_ref (event_widget);
1445           if (!gtk_widget_event (event_widget, event) &&
1446               GTK_WIDGET_REALIZED (event_widget))
1447             gtk_widget_destroy (event_widget);
1448           g_object_unref (event_widget);
1449         }
1450       break;
1451       
1452     case GDK_EXPOSE:
1453       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1454         {
1455           gdk_window_begin_paint_region (event->any.window, event->expose.region);
1456           gtk_widget_send_expose (event_widget, event);
1457           gdk_window_end_paint (event->any.window);
1458         }
1459       else
1460         gtk_widget_send_expose (event_widget, event);
1461       break;
1462
1463     case GDK_PROPERTY_NOTIFY:
1464     case GDK_NO_EXPOSE:
1465     case GDK_FOCUS_CHANGE:
1466     case GDK_CONFIGURE:
1467     case GDK_MAP:
1468     case GDK_UNMAP:
1469     case GDK_SELECTION_CLEAR:
1470     case GDK_SELECTION_REQUEST:
1471     case GDK_SELECTION_NOTIFY:
1472     case GDK_CLIENT_EVENT:
1473     case GDK_VISIBILITY_NOTIFY:
1474     case GDK_WINDOW_STATE:
1475       gtk_widget_event (event_widget, event);
1476       break;
1477
1478     case GDK_SCROLL:
1479     case GDK_BUTTON_PRESS:
1480     case GDK_2BUTTON_PRESS:
1481     case GDK_3BUTTON_PRESS:
1482       gtk_propagate_event (grab_widget, event);
1483       break;
1484
1485     case GDK_KEY_PRESS:
1486     case GDK_KEY_RELEASE:
1487       if (key_snoopers)
1488         {
1489           if (gtk_invoke_key_snoopers (grab_widget, event))
1490             break;
1491         }
1492       /* else fall through */
1493     case GDK_MOTION_NOTIFY:
1494     case GDK_BUTTON_RELEASE:
1495     case GDK_PROXIMITY_IN:
1496     case GDK_PROXIMITY_OUT:
1497       gtk_propagate_event (grab_widget, event);
1498       break;
1499       
1500     case GDK_ENTER_NOTIFY:
1501       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1502         {
1503           g_object_ref (event_widget);
1504           
1505           gtk_widget_event (grab_widget, event);
1506           if (event_widget == grab_widget)
1507             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1508           
1509           g_object_unref (event_widget);
1510         }
1511       break;
1512       
1513     case GDK_LEAVE_NOTIFY:
1514       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1515         {
1516           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1517           gtk_widget_event (event_widget, event);
1518         }
1519       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1520         gtk_widget_event (grab_widget, event);
1521       break;
1522       
1523     case GDK_DRAG_STATUS:
1524     case GDK_DROP_FINISHED:
1525       _gtk_drag_source_handle_event (event_widget, event);
1526       break;
1527     case GDK_DRAG_ENTER:
1528     case GDK_DRAG_LEAVE:
1529     case GDK_DRAG_MOTION:
1530     case GDK_DROP_START:
1531       _gtk_drag_dest_handle_event (event_widget, event);
1532       break;
1533     default:
1534       g_assert_not_reached ();
1535       break;
1536     }
1537   
1538   tmp_list = current_events;
1539   current_events = g_list_remove_link (current_events, tmp_list);
1540   g_list_free_1 (tmp_list);
1541
1542   if (rewritten_event)
1543     gdk_event_free (rewritten_event);
1544 }
1545
1546 gboolean
1547 gtk_true (void)
1548 {
1549   return TRUE;
1550 }
1551
1552 gboolean
1553 gtk_false (void)
1554 {
1555   return FALSE;
1556 }
1557
1558 static GtkWindowGroup *
1559 gtk_main_get_window_group (GtkWidget   *widget)
1560 {
1561   GtkWidget *toplevel = NULL;
1562
1563   if (widget)
1564     toplevel = gtk_widget_get_toplevel (widget);
1565
1566   if (toplevel && GTK_IS_WINDOW (toplevel))
1567     return _gtk_window_get_group (GTK_WINDOW (toplevel));
1568   else
1569     return _gtk_window_get_group (NULL);
1570 }
1571
1572 typedef struct
1573 {
1574   GtkWidget *old_grab_widget;
1575   GtkWidget *new_grab_widget;
1576 } GrabNotifyInfo;
1577
1578 static gboolean
1579 check_is_grabbed (GtkWidget *widget,
1580                   GtkWidget *grab_widget)
1581 {
1582   if (grab_widget)
1583     return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1584   else
1585     return FALSE;
1586 }
1587
1588 static void
1589 gtk_grab_notify_foreach (GtkWidget *child,
1590                          gpointer   data)
1591                         
1592 {
1593   GrabNotifyInfo *info = data;
1594   gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1595   gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1596
1597   if (was_grabbed != is_grabbed)
1598     {
1599       g_object_ref (child);
1600       
1601       g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1602       
1603       if (GTK_IS_CONTAINER (child))
1604         gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1605       
1606       g_object_unref (child);
1607     }
1608 }
1609
1610 static void
1611 gtk_grab_notify (GtkWindowGroup *group,
1612                  GtkWidget      *grab_widget,
1613                  gboolean        was_grabbed)
1614 {
1615   GList *toplevels;
1616   GrabNotifyInfo info;
1617
1618   if (was_grabbed)
1619     {
1620       info.old_grab_widget = grab_widget;
1621       info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1622     }
1623   else
1624     {
1625       info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1626       info.new_grab_widget = grab_widget;
1627     }
1628
1629   g_object_ref (group);
1630   g_object_ref (grab_widget);
1631
1632   toplevels = gtk_window_list_toplevels ();
1633   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1634                             
1635   while (toplevels)
1636     {
1637       GtkWindow *toplevel = toplevels->data;
1638       toplevels = g_list_delete_link (toplevels, toplevels);
1639
1640       if (group == _gtk_window_get_group (toplevel))
1641         gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1642       g_object_unref (toplevel);
1643     }
1644
1645   g_object_unref (group);
1646   g_object_unref (grab_widget);
1647 }
1648
1649 void
1650 gtk_grab_add (GtkWidget *widget)
1651 {
1652   GtkWindowGroup *group;
1653   gboolean was_grabbed;
1654   
1655   g_return_if_fail (widget != NULL);
1656   
1657   if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1658     {
1659       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1660       
1661       group = gtk_main_get_window_group (widget);
1662
1663       was_grabbed = (group->grabs != NULL);
1664       
1665       g_object_ref (widget);
1666       group->grabs = g_slist_prepend (group->grabs, widget);
1667
1668       gtk_grab_notify (group, widget, FALSE);
1669     }
1670 }
1671
1672 GtkWidget*
1673 gtk_grab_get_current (void)
1674 {
1675   GtkWindowGroup *group;
1676
1677   group = gtk_main_get_window_group (NULL);
1678
1679   if (group->grabs)
1680     return GTK_WIDGET (group->grabs->data);
1681   return NULL;
1682 }
1683
1684 void
1685 gtk_grab_remove (GtkWidget *widget)
1686 {
1687   GtkWindowGroup *group;
1688   
1689   g_return_if_fail (widget != NULL);
1690   
1691   if (GTK_WIDGET_HAS_GRAB (widget))
1692     {
1693       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1694
1695       group = gtk_main_get_window_group (widget);
1696       group->grabs = g_slist_remove (group->grabs, widget);
1697       
1698       g_object_unref (widget);
1699
1700       gtk_grab_notify (group, widget, TRUE);
1701     }
1702 }
1703
1704 void
1705 gtk_init_add (GtkFunction function,
1706               gpointer    data)
1707 {
1708   GtkInitFunction *init;
1709   
1710   init = g_new (GtkInitFunction, 1);
1711   init->function = function;
1712   init->data = data;
1713   
1714   init_functions = g_list_prepend (init_functions, init);
1715 }
1716
1717 guint
1718 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1719                          gpointer        func_data)
1720 {
1721   GtkKeySnooperData *data;
1722   static guint snooper_id = 1;
1723
1724   g_return_val_if_fail (snooper != NULL, 0);
1725
1726   data = g_new (GtkKeySnooperData, 1);
1727   data->func = snooper;
1728   data->func_data = func_data;
1729   data->id = snooper_id++;
1730   key_snoopers = g_slist_prepend (key_snoopers, data);
1731
1732   return data->id;
1733 }
1734
1735 void
1736 gtk_key_snooper_remove (guint            snooper_id)
1737 {
1738   GtkKeySnooperData *data = NULL;
1739   GSList *slist;
1740
1741   slist = key_snoopers;
1742   while (slist)
1743     {
1744       data = slist->data;
1745       if (data->id == snooper_id)
1746         break;
1747
1748       slist = slist->next;
1749       data = NULL;
1750     }
1751   if (data)
1752     key_snoopers = g_slist_remove (key_snoopers, data);
1753 }
1754
1755 static gint
1756 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1757                          GdkEvent  *event)
1758 {
1759   GSList *slist;
1760   gint return_val = FALSE;
1761
1762   slist = key_snoopers;
1763   while (slist && !return_val)
1764     {
1765       GtkKeySnooperData *data;
1766
1767       data = slist->data;
1768       slist = slist->next;
1769       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1770     }
1771
1772   return return_val;
1773 }
1774
1775 guint
1776 gtk_quit_add_full (guint                main_level,
1777                    GtkFunction          function,
1778                    GtkCallbackMarshal   marshal,
1779                    gpointer             data,
1780                    GtkDestroyNotify     destroy)
1781 {
1782   static guint quit_id = 1;
1783   GtkQuitFunction *quitf;
1784   
1785   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1786
1787   if (!quit_mem_chunk)
1788     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1789                                       512, G_ALLOC_AND_FREE);
1790   
1791   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1792   
1793   quitf->id = quit_id++;
1794   quitf->main_level = main_level;
1795   quitf->function = function;
1796   quitf->marshal = marshal;
1797   quitf->data = data;
1798   quitf->destroy = destroy;
1799
1800   quit_functions = g_list_prepend (quit_functions, quitf);
1801   
1802   return quitf->id;
1803 }
1804
1805 static void
1806 gtk_quit_destroy (GtkQuitFunction *quitf)
1807 {
1808   if (quitf->destroy)
1809     quitf->destroy (quitf->data);
1810   g_mem_chunk_free (quit_mem_chunk, quitf);
1811 }
1812
1813 static gint
1814 gtk_quit_destructor (GtkObject **object_p)
1815 {
1816   if (*object_p)
1817     gtk_object_destroy (*object_p);
1818   g_free (object_p);
1819
1820   return FALSE;
1821 }
1822
1823 void
1824 gtk_quit_add_destroy (guint              main_level,
1825                       GtkObject         *object)
1826 {
1827   GtkObject **object_p;
1828
1829   g_return_if_fail (main_level > 0);
1830   g_return_if_fail (GTK_IS_OBJECT (object));
1831
1832   object_p = g_new (GtkObject*, 1);
1833   *object_p = object;
1834   g_signal_connect (object,
1835                     "destroy",
1836                     G_CALLBACK (gtk_widget_destroyed),
1837                     object_p);
1838   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1839 }
1840
1841 guint
1842 gtk_quit_add (guint       main_level,
1843               GtkFunction function,
1844               gpointer    data)
1845 {
1846   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1847 }
1848
1849 void
1850 gtk_quit_remove (guint id)
1851 {
1852   GtkQuitFunction *quitf;
1853   GList *tmp_list;
1854   
1855   tmp_list = quit_functions;
1856   while (tmp_list)
1857     {
1858       quitf = tmp_list->data;
1859       
1860       if (quitf->id == id)
1861         {
1862           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1863           g_list_free (tmp_list);
1864           gtk_quit_destroy (quitf);
1865           
1866           return;
1867         }
1868       
1869       tmp_list = tmp_list->next;
1870     }
1871 }
1872
1873 void
1874 gtk_quit_remove_by_data (gpointer data)
1875 {
1876   GtkQuitFunction *quitf;
1877   GList *tmp_list;
1878   
1879   tmp_list = quit_functions;
1880   while (tmp_list)
1881     {
1882       quitf = tmp_list->data;
1883       
1884       if (quitf->data == data)
1885         {
1886           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1887           g_list_free (tmp_list);
1888           gtk_quit_destroy (quitf);
1889
1890           return;
1891         }
1892       
1893       tmp_list = tmp_list->next;
1894     }
1895 }
1896
1897 guint
1898 gtk_timeout_add_full (guint32            interval,
1899                       GtkFunction        function,
1900                       GtkCallbackMarshal marshal,
1901                       gpointer           data,
1902                       GtkDestroyNotify   destroy)
1903 {
1904   if (marshal)
1905     {
1906       GtkClosure *closure;
1907
1908       closure = g_new (GtkClosure, 1);
1909       closure->marshal = marshal;
1910       closure->data = data;
1911       closure->destroy = destroy;
1912
1913       return g_timeout_add_full (0, interval, 
1914                                  gtk_invoke_idle_timeout,
1915                                  closure,
1916                                  gtk_destroy_closure);
1917     }
1918   else
1919     return g_timeout_add_full (0, interval, function, data, destroy);
1920 }
1921
1922 guint
1923 gtk_timeout_add (guint32     interval,
1924                  GtkFunction function,
1925                  gpointer    data)
1926 {
1927   return g_timeout_add_full (0, interval, function, data, NULL);
1928 }
1929
1930 void
1931 gtk_timeout_remove (guint tag)
1932 {
1933   g_source_remove (tag);
1934 }
1935
1936 guint
1937 gtk_idle_add_full (gint                 priority,
1938                    GtkFunction          function,
1939                    GtkCallbackMarshal   marshal,
1940                    gpointer             data,
1941                    GtkDestroyNotify     destroy)
1942 {
1943   if (marshal)
1944     {
1945       GtkClosure *closure;
1946
1947       closure = g_new (GtkClosure, 1);
1948       closure->marshal = marshal;
1949       closure->data = data;
1950       closure->destroy = destroy;
1951
1952       return g_idle_add_full (priority,
1953                               gtk_invoke_idle_timeout,
1954                               closure,
1955                               gtk_destroy_closure);
1956     }
1957   else
1958     return g_idle_add_full (priority, function, data, destroy);
1959 }
1960
1961 guint
1962 gtk_idle_add (GtkFunction function,
1963               gpointer    data)
1964 {
1965   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1966 }
1967
1968 guint       
1969 gtk_idle_add_priority (gint        priority,
1970                        GtkFunction function,
1971                        gpointer    data)
1972 {
1973   return g_idle_add_full (priority, function, data, NULL);
1974 }
1975
1976 void
1977 gtk_idle_remove (guint tag)
1978 {
1979   g_source_remove (tag);
1980 }
1981
1982 void
1983 gtk_idle_remove_by_data (gpointer data)
1984 {
1985   if (!g_idle_remove_by_data (data))
1986     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1987 }
1988
1989 guint
1990 gtk_input_add_full (gint                source,
1991                     GdkInputCondition   condition,
1992                     GdkInputFunction    function,
1993                     GtkCallbackMarshal  marshal,
1994                     gpointer            data,
1995                     GtkDestroyNotify    destroy)
1996 {
1997   if (marshal)
1998     {
1999       GtkClosure *closure;
2000
2001       closure = g_new (GtkClosure, 1);
2002       closure->marshal = marshal;
2003       closure->data = data;
2004       closure->destroy = destroy;
2005
2006       return gdk_input_add_full (source,
2007                                  condition,
2008                                  (GdkInputFunction) gtk_invoke_input,
2009                                  closure,
2010                                  (GdkDestroyNotify) gtk_destroy_closure);
2011     }
2012   else
2013     return gdk_input_add_full (source, condition, function, data, destroy);
2014 }
2015
2016 void
2017 gtk_input_remove (guint tag)
2018 {
2019   g_source_remove (tag);
2020 }
2021
2022 static void
2023 gtk_destroy_closure (gpointer data)
2024 {
2025   GtkClosure *closure = data;
2026
2027   if (closure->destroy)
2028     (closure->destroy) (closure->data);
2029   g_free (closure);
2030 }
2031
2032 static gboolean
2033 gtk_invoke_idle_timeout (gpointer data)
2034 {
2035   GtkClosure *closure = data;
2036
2037   GtkArg args[1];
2038   gint ret_val = FALSE;
2039   args[0].name = NULL;
2040   args[0].type = G_TYPE_BOOLEAN;
2041   args[0].d.pointer_data = &ret_val;
2042   closure->marshal (NULL, closure->data,  0, args);
2043   return ret_val;
2044 }
2045
2046 static void
2047 gtk_invoke_input (gpointer          data,
2048                   gint              source,
2049                   GdkInputCondition condition)
2050 {
2051   GtkClosure *closure = data;
2052
2053   GtkArg args[3];
2054   args[0].type = G_TYPE_INT;
2055   args[0].name = NULL;
2056   GTK_VALUE_INT (args[0]) = source;
2057   args[1].type = GDK_TYPE_INPUT_CONDITION;
2058   args[1].name = NULL;
2059   GTK_VALUE_FLAGS (args[1]) = condition;
2060   args[2].type = G_TYPE_NONE;
2061   args[2].name = NULL;
2062
2063   closure->marshal (NULL, closure->data, 2, args);
2064 }
2065
2066 /**
2067  * gtk_get_current_event:
2068  * 
2069  * Obtains a copy of the event currently being processed by GTK+.  For
2070  * example, if you get a "clicked" signal from #GtkButton, the current
2071  * event will be the #GdkEventButton that triggered the "clicked"
2072  * signal. The returned event must be freed with gdk_event_free().
2073  * If there is no current event, the function returns %NULL.
2074  * 
2075  * Return value: a copy of the current event, or %NULL if no current event.
2076  **/
2077 GdkEvent*
2078 gtk_get_current_event (void)
2079 {
2080   if (current_events)
2081     return gdk_event_copy (current_events->data);
2082   else
2083     return NULL;
2084 }
2085
2086 /**
2087  * gtk_get_current_event_time:
2088  * 
2089  * If there is a current event and it has a timestamp, return that
2090  * timestamp, otherwise return %GDK_CURRENT_TIME.
2091  * 
2092  * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2093  **/
2094 guint32
2095 gtk_get_current_event_time (void)
2096 {
2097   if (current_events)
2098     return gdk_event_get_time (current_events->data);
2099   else
2100     return GDK_CURRENT_TIME;
2101 }
2102
2103 /**
2104  * gtk_get_current_event_state:
2105  * @state: a location to store the state of the current event
2106  * 
2107  * If there is a current event and it has a state field, place
2108  * that state field in @state and return %TRUE, otherwise return
2109  * %FALSE.
2110  * 
2111  * Return value: %TRUE if there was a current event and it had a state field
2112  **/
2113 gboolean
2114 gtk_get_current_event_state (GdkModifierType *state)
2115 {
2116   g_return_val_if_fail (state != NULL, FALSE);
2117   
2118   if (current_events)
2119     return gdk_event_get_state (current_events->data, state);
2120   else
2121     {
2122       *state = 0;
2123       return FALSE;
2124     }
2125 }
2126
2127 /**
2128  * gtk_get_event_widget:
2129  * @event: a #GdkEvent
2130  *
2131  * If @event is %NULL or the event was not associated with any widget,
2132  * returns %NULL, otherwise returns the widget that received the event
2133  * originally.
2134  * 
2135  * Return value: the widget that originally received @event, or %NULL
2136  **/
2137 GtkWidget*
2138 gtk_get_event_widget (GdkEvent *event)
2139 {
2140   GtkWidget *widget;
2141
2142   widget = NULL;
2143   if (event && event->any.window)
2144     gdk_window_get_user_data (event->any.window, (void**) &widget);
2145   
2146   return widget;
2147 }
2148
2149 static gint
2150 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2151 {
2152   if (!quitf->marshal)
2153     return quitf->function (quitf->data);
2154   else
2155     {
2156       GtkArg args[1];
2157       gint ret_val = FALSE;
2158
2159       args[0].name = NULL;
2160       args[0].type = G_TYPE_BOOLEAN;
2161       args[0].d.pointer_data = &ret_val;
2162       ((GtkCallbackMarshal) quitf->marshal) (NULL,
2163                                              quitf->data,
2164                                              0, args);
2165       return ret_val;
2166     }
2167 }
2168
2169 /**
2170  * gtk_propagate_event:
2171  * @widget: a #GtkWidget
2172  * @event: an event
2173  *
2174  * Sends an event to a widget, propagating the event to parent widgets
2175  * if the event remains unhandled. Events received by GTK+ from GDK
2176  * normally begin in gtk_main_do_event(). Depending on the type of
2177  * event, existence of modal dialogs, grabs, etc., the event may be
2178  * propagated; if so, this function is used. gtk_propagate_event()
2179  * calls gtk_widget_event() on each widget it decides to send the
2180  * event to.  So gtk_widget_event() is the lowest-level function; it
2181  * simply emits the "event" and possibly an event-specific signal on a
2182  * widget.  gtk_propagate_event() is a bit higher-level, and
2183  * gtk_main_do_event() is the highest level.
2184  *
2185  * All that said, you most likely don't want to use any of these
2186  * functions; synthesizing events is rarely needed. Consider asking on
2187  * the mailing list for better ways to achieve your goals. For
2188  * example, use gdk_window_invalidate_rect() or
2189  * gtk_widget_queue_draw() instead of making up expose events.
2190  * 
2191  **/
2192 void
2193 gtk_propagate_event (GtkWidget *widget,
2194                      GdkEvent  *event)
2195 {
2196   gint handled_event;
2197   
2198   g_return_if_fail (GTK_IS_WIDGET (widget));
2199   g_return_if_fail (event != NULL);
2200   
2201   handled_event = FALSE;
2202
2203   g_object_ref (widget);
2204       
2205   if ((event->type == GDK_KEY_PRESS) ||
2206       (event->type == GDK_KEY_RELEASE))
2207     {
2208       /* Only send key events within Window widgets to the Window
2209        *  The Window widget will in turn pass the
2210        *  key event on to the currently focused widget
2211        *  for that window.
2212        */
2213       GtkWidget *window;
2214
2215       window = gtk_widget_get_toplevel (widget);
2216       if (window && GTK_IS_WINDOW (window))
2217         {
2218           /* If there is a grab within the window, give the grab widget
2219            * a first crack at the key event
2220            */
2221           if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2222             handled_event = gtk_widget_event (widget, event);
2223           
2224           if (!handled_event)
2225             {
2226               window = gtk_widget_get_toplevel (widget);
2227               if (window && GTK_IS_WINDOW (window))
2228                 {
2229                   if (GTK_WIDGET_IS_SENSITIVE (window))
2230                     gtk_widget_event (window, event);
2231                 }
2232             }
2233                   
2234           handled_event = TRUE; /* don't send to widget */
2235         }
2236     }
2237   
2238   /* Other events get propagated up the widget tree
2239    *  so that parents can see the button and motion
2240    *  events of the children.
2241    */
2242   if (!handled_event)
2243     {
2244       while (TRUE)
2245         {
2246           GtkWidget *tmp;
2247           
2248           handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2249           tmp = widget->parent;
2250           g_object_unref (widget);
2251
2252           widget = tmp;
2253           
2254           if (!handled_event && widget)
2255             g_object_ref (widget);
2256           else
2257             break;
2258         }
2259     }
2260   else
2261     g_object_unref (widget);
2262 }
2263
2264 #if 0
2265 static void
2266 gtk_error (gchar *str)
2267 {
2268   gtk_print (str);
2269 }
2270
2271 static void
2272 gtk_warning (gchar *str)
2273 {
2274   gtk_print (str);
2275 }
2276
2277 static void
2278 gtk_message (gchar *str)
2279 {
2280   gtk_print (str);
2281 }
2282
2283 static void
2284 gtk_print (gchar *str)
2285 {
2286   static GtkWidget *window = NULL;
2287   static GtkWidget *text;
2288   static int level = 0;
2289   GtkWidget *box1;
2290   GtkWidget *box2;
2291   GtkWidget *table;
2292   GtkWidget *hscrollbar;
2293   GtkWidget *vscrollbar;
2294   GtkWidget *separator;
2295   GtkWidget *button;
2296   
2297   if (level > 0)
2298     {
2299       fputs (str, stdout);
2300       fflush (stdout);
2301       return;
2302     }
2303   
2304   if (!window)
2305     {
2306       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2307       
2308       gtk_signal_connect (GTK_OBJECT (window), "destroy",
2309                           (GtkSignalFunc) gtk_widget_destroyed,
2310                           &window);
2311       
2312       gtk_window_set_title (GTK_WINDOW (window), "Messages");
2313       
2314       box1 = gtk_vbox_new (FALSE, 0);
2315       gtk_container_add (GTK_CONTAINER (window), box1);
2316       gtk_widget_show (box1);
2317       
2318       
2319       box2 = gtk_vbox_new (FALSE, 10);
2320       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2321       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2322       gtk_widget_show (box2);
2323       
2324       
2325       table = gtk_table_new (2, 2, FALSE);
2326       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2327       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2328       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2329       gtk_widget_show (table);
2330       
2331       text = gtk_text_new (NULL, NULL);
2332       gtk_text_set_editable (GTK_TEXT (text), FALSE);
2333       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2334       gtk_widget_show (text);
2335       gtk_widget_realize (text);
2336       
2337       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2338       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2339                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2340       gtk_widget_show (hscrollbar);
2341       
2342       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2343       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2344                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2345       gtk_widget_show (vscrollbar);
2346       
2347       separator = gtk_hseparator_new ();
2348       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2349       gtk_widget_show (separator);
2350       
2351       
2352       box2 = gtk_vbox_new (FALSE, 10);
2353       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2354       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2355       gtk_widget_show (box2);
2356       
2357       
2358       button = gtk_button_new_with_label ("close");
2359       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2360                                  (GtkSignalFunc) gtk_widget_hide,
2361                                  GTK_OBJECT (window));
2362       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2363       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2364       gtk_widget_grab_default (button);
2365       gtk_widget_show (button);
2366     }
2367   
2368   level += 1;
2369   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2370   level -= 1;
2371   
2372   if (!GTK_WIDGET_VISIBLE (window))
2373     gtk_widget_show (window);
2374 }
2375 #endif
2376
2377 gboolean
2378 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2379                                   GValue                *return_accu,
2380                                   const GValue          *handler_return,
2381                                   gpointer               dummy)
2382 {
2383   gboolean continue_emission;
2384   gboolean signal_handled;
2385   
2386   signal_handled = g_value_get_boolean (handler_return);
2387   g_value_set_boolean (return_accu, signal_handled);
2388   continue_emission = !signal_handled;
2389   
2390   return continue_emission;
2391 }