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