1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gdkconfig.h"
31 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
41 #include <sys/types.h> /* For uid_t, gid_t */
49 #include <pango/pango-utils.h> /* For pango_split_file_list */
51 #include "gtkaccelmap.h"
54 #include "gtkversion.h"
57 #include "gtkselection.h"
58 #include "gtksettings.h"
59 #include "gtksignal.h"
60 #include "gtkwidget.h"
61 #include "gtkwindow.h"
62 #include "gtkprivate.h"
67 /* Private type definitions
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;
75 struct _GtkInitFunction
81 struct _GtkQuitFunction
85 GtkCallbackMarshal marshal;
88 GtkDestroyNotify destroy;
93 GtkCallbackMarshal marshal;
95 GtkDestroyNotify destroy;
98 struct _GtkKeySnooperData
100 GtkKeySnoopFunc func;
105 struct _GtkModuleInfo
107 GtkModuleInitFunc init_func;
108 GtkModuleDisplayInitFunc display_init_func;
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,
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,
120 GdkInputCondition condition);
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);
129 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
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;
137 static GSList *gtk_modules;
139 /* Saved argc,argv for delayed module initialization
141 static gint gtk_argc = 0;
142 static gchar **gtk_argv = NULL;
144 static guint gtk_main_loop_level = 0;
145 static gint gtk_initialized = FALSE;
146 static GList *current_events = NULL;
148 static GSList *main_loops = NULL; /* stack of currently executing main loops */
150 static GList *init_functions = NULL; /* A list of init functions.
152 static GList *quit_functions = NULL; /* A list of quit functions.
154 static GMemChunk *quit_mem_chunk = NULL;
156 static GSList *key_snoopers = NULL;
158 guint gtk_debug_flags = 0; /* Global GTK debug flag */
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}
171 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
173 #endif /* G_ENABLE_DEBUG */
176 gtk_check_version (guint required_major,
177 guint required_minor,
178 guint required_micro)
180 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
181 gint required_effective_micro = 100 * required_minor + required_micro;
183 if (required_major > GTK_MAJOR_VERSION)
184 return "Gtk+ version too old (major mismatch)";
185 if (required_major < GTK_MAJOR_VERSION)
186 return "Gtk+ version too new (major mismatch)";
187 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
188 return "Gtk+ version too new (micro mismatch)";
189 if (required_effective_micro > gtk_effective_micro)
190 return "Gtk+ version too old (micro mismatch)";
194 /* This checks to see if the process is running suid or sgid
195 * at the current time. If so, we don't allow GTK+ to be initialized.
196 * This is meant to be a mild check - we only error out if we
197 * can prove the programmer is doing something wrong, not if
198 * they could be doing something wrong. For this reason, we
199 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
204 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
206 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
207 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
209 #ifdef HAVE_GETRESUID
210 /* These aren't in the header files, so we prototype them here.
212 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
213 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
215 if (getresuid (&ruid, &euid, &suid) != 0 ||
216 getresgid (&rgid, &egid, &sgid) != 0)
217 #endif /* HAVE_GETRESUID */
219 suid = ruid = getuid ();
220 sgid = rgid = getgid ();
225 if (ruid != euid || ruid != suid ||
226 rgid != egid || rgid != sgid)
228 g_warning ("This process is currently running setuid or setgid.\n"
229 "This is not a supported use of GTK+. You must create a helper\n"
230 "program instead. For further details, see:\n\n"
231 " http://www.gtk.org/setuid.html\n\n"
232 "Refusing to initialize GTK+.");
241 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
244 _gtk_get_libdir (void)
246 static char *gtk_libdir = NULL;
247 if (gtk_libdir == NULL)
248 gtk_libdir = g_win32_get_package_installation_subdirectory
249 (GETTEXT_PACKAGE, dll_name, "lib");
255 _gtk_get_localedir (void)
257 static char *gtk_localedir = NULL;
258 if (gtk_localedir == NULL)
259 gtk_localedir = g_win32_get_package_installation_subdirectory
260 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
262 return gtk_localedir;
266 _gtk_get_sysconfdir (void)
268 static char *gtk_sysconfdir = NULL;
269 if (gtk_sysconfdir == NULL)
270 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
271 (GETTEXT_PACKAGE, dll_name, "etc");
273 return gtk_sysconfdir;
277 _gtk_get_data_prefix (void)
279 static char *gtk_data_prefix = NULL;
280 if (gtk_data_prefix == NULL)
281 gtk_data_prefix = g_win32_get_package_installation_directory
282 (GETTEXT_PACKAGE, dll_name);
284 return gtk_data_prefix;
287 #endif /* G_OS_WIN32 */
290 get_module_path (void)
292 const gchar *module_path_env;
293 const gchar *exe_prefix;
294 const gchar *home_dir;
295 gchar *home_gtk_dir = NULL;
298 static gchar **result = NULL;
303 home_dir = g_get_home_dir();
305 home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);
307 module_path_env = g_getenv ("GTK_PATH");
308 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
311 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
313 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);
315 if (module_path_env && home_gtk_dir)
316 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
317 module_path_env, home_gtk_dir, default_dir, NULL);
318 else if (module_path_env)
319 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
320 module_path_env, default_dir, NULL);
321 else if (home_gtk_dir)
322 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
323 home_gtk_dir, default_dir, NULL);
325 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
328 g_free (home_gtk_dir);
329 g_free (default_dir);
331 result = pango_split_file_list (module_path);
332 g_free (module_path);
338 * _gtk_get_module_path:
339 * @type: the type of the module, for instance 'modules', 'engines', immodules'
341 * Determines the search path for a particular type of module.
343 * Return value: the search path for the module type. Free with g_strfreev().
346 _gtk_get_module_path (const gchar *type)
348 gchar **paths = get_module_path();
353 for (path = paths; *path; path++)
356 result = g_new (gchar *, count * 4 + 1);
359 for (path = get_module_path (); *path; path++)
361 gint use_version, use_host;
363 for (use_version = TRUE; use_version >= FALSE; use_version--)
364 for (use_host = TRUE; use_host >= FALSE; use_host--)
368 if (use_version && use_host)
369 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
370 else if (use_version)
371 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
373 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
375 tmp_dir = g_build_filename (*path, type, NULL);
377 result[count++] = tmp_dir;
381 result[count++] = NULL;
386 /* Like g_module_path, but use .la as the suffix
389 module_build_la_path (const gchar *directory,
390 const gchar *module_name)
395 if (strncmp (module_name, "lib", 3) == 0)
396 filename = (gchar *)module_name;
398 filename = g_strconcat ("lib", module_name, ".la", NULL);
400 if (directory && *directory)
401 result = g_build_filename (directory, filename, NULL);
403 result = g_strdup (filename);
405 if (filename != module_name)
413 * @name: the name of the module
414 * @type: the type of the module, for instance 'modules', 'engines', immodules'
416 * Looks for a dynamically module named @name of type @type in the standard GTK+
417 * module search path.
419 * Return value: the pathname to the found module, or %NULL if it wasn't found.
420 * Free with g_free().
423 _gtk_find_module (const gchar *name,
428 gchar *module_name = NULL;
430 if (g_path_is_absolute (name))
431 return g_strdup (name);
433 paths = _gtk_get_module_path (type);
434 for (path = paths; *path; path++)
438 tmp_name = g_module_build_path (*path, name);
439 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
441 module_name = tmp_name;
446 tmp_name = module_build_la_path (*path, name);
447 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
449 module_name = tmp_name;
461 find_module (const gchar *name)
466 module_name = _gtk_find_module (name, "modules");
469 /* As last resort, try loading without an absolute path (using system
472 module_name = g_module_build_path (NULL, name);
475 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
482 load_module (GSList *module_list,
485 GtkModuleInitFunc modinit_func = NULL;
487 GModule *module = NULL;
489 if (g_module_supported ())
491 module = find_module (name);
493 g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
496 if (!g_slist_find (module_list, (gconstpointer) modinit_func))
498 g_module_make_resident (module);
499 info = g_new (GtkModuleInfo, 1);
501 info->init_func = modinit_func;
502 g_module_symbol (module, "gtk_module_display_init",
503 (gpointer *) &info->display_init_func);
505 module_list = g_slist_prepend (module_list, info);
509 g_module_close (module);
516 g_message ("Failed to load module \"%s\": %s",
517 module ? g_module_name (module) : name,
520 g_module_close (module);
527 load_modules (const char *module_str)
529 gchar **module_names = pango_split_file_list (module_str);
530 GSList *module_list = NULL;
533 for (i = 0; module_names[i]; i++)
534 module_list = load_module (module_list, module_names[i]);
536 module_list = g_slist_reverse (module_list);
538 g_strfreev (module_names);
543 static gboolean do_setlocale = TRUE;
546 * gtk_disable_setlocale:
548 * Prevents gtk_init() and gtk_init_check() from automatically
549 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
550 * want to use this function if you wanted to set the locale for
551 * your program to something other than the user's locale, or if
552 * you wanted to set different values for different locale categories.
554 * Most programs should not need to call this function.
557 gtk_disable_setlocale (void)
560 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
562 do_setlocale = FALSE;
565 #undef gtk_init_check
568 default_display_notify_cb (GdkDisplayManager *display_manager)
572 /* Initialize non-multihead-aware modules when the
573 * default display is first set to a non-NULL value.
575 static gboolean initialized = FALSE;
577 if (!gdk_display_get_default () || initialized)
582 for (slist = gtk_modules; slist; slist = slist->next)
586 GtkModuleInfo *info = slist->data;
588 if (!info->display_init_func)
589 info->init_func (>k_argc, >k_argv);
595 display_opened_cb (GdkDisplayManager *display_manager,
600 for (slist = gtk_modules; slist; slist = slist->next)
604 GtkModuleInfo *info = slist->data;
606 if (info->display_init_func)
607 info->display_init_func (display);
614 * @argc: the number of command line arguments.
615 * @argv: the array of command line arguments.
617 * Parses command line arguments, and initializes global
618 * attributes of GTK+, but does not actually open a connection
619 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
621 * Any arguments used by GTK or GDK are removed from the array and
622 * @argc and @argv are updated accordingly.
624 * You shouldn't call this function explicitely if you are using
625 * gtk_init(), or gtk_init_check().
627 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
630 gtk_parse_args (int *argc,
633 GString *gtk_modules_string = NULL;
635 GdkDisplayManager *display_manager;
636 const gchar *env_string;
641 if (!check_setugid ())
645 g_set_error_handler (gtk_error);
646 g_set_warning_handler (gtk_warning);
647 g_set_message_handler (gtk_message);
648 g_set_print_handler (gtk_print);
653 if (!setlocale (LC_ALL, ""))
654 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
657 gdk_parse_args (argc, argv);
658 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
660 #ifdef G_ENABLE_DEBUG
661 env_string = g_getenv ("GTK_DEBUG");
662 if (env_string != NULL)
664 gtk_debug_flags = g_parse_debug_string (env_string,
669 #endif /* G_ENABLE_DEBUG */
671 env_string = g_getenv ("GTK_MODULES");
673 gtk_modules_string = g_string_new (env_string);
679 for (i = 1; i < *argc;)
681 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
682 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
684 gchar *module_name = (*argv)[i] + 12;
686 if (*module_name == '=')
688 else if (i + 1 < *argc)
692 module_name = (*argv)[i];
696 if (module_name && *module_name)
698 if (gtk_modules_string)
699 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
701 gtk_modules_string = g_string_new (NULL);
703 g_string_append (gtk_modules_string, module_name);
706 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
708 GLogLevelFlags fatal_mask;
710 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
711 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
712 g_log_set_always_fatal (fatal_mask);
715 #ifdef G_ENABLE_DEBUG
716 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
717 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
719 gchar *equal_pos = strchr ((*argv)[i], '=');
721 if (equal_pos != NULL)
723 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
727 else if ((i + 1) < *argc && (*argv)[i + 1])
729 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
737 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
738 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
740 gchar *equal_pos = strchr ((*argv)[i], '=');
742 if (equal_pos != NULL)
744 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
748 else if ((i + 1) < *argc && (*argv)[i + 1])
750 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
758 #endif /* G_ENABLE_DEBUG */
762 for (i = 1; i < *argc; i++)
764 for (k = i; k < *argc; k++)
765 if ((*argv)[k] != NULL)
771 for (j = i + k; j < *argc; j++)
772 (*argv)[j-k] = (*argv)[j];
777 gtk_argv = g_malloc ((gtk_argc + 1) * sizeof (char*));
778 for (i = 0; i < gtk_argc; i++)
779 gtk_argv[i] = g_strdup ((*argv)[i]);
780 gtk_argv[gtk_argc] = NULL;
783 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
784 gdk_window_set_debug_updates (TRUE);
786 /* load gtk modules */
787 if (gtk_modules_string)
789 gtk_modules = load_modules (gtk_modules_string->str);
790 g_string_free (gtk_modules_string, TRUE);
794 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
795 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
796 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
801 /* Translate to default:RTL if you want your widgets
802 * to be RTL, otherwise translate to default:LTR.
803 * Do *not* translate it to "predefinito:LTR", if it
804 * it isn't default:LTR or default:RTL it will not work
806 char *e = _("default:LTR");
807 if (strcmp (e, "default:RTL")==0) {
808 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
809 } else if (strcmp (e, "default:LTR")) {
810 g_warning ("Whoever translated default:LTR did so wrongly.\n");
815 _gtk_accel_map_init ();
818 /* Set the 'initialized' flag.
820 gtk_initialized = TRUE;
822 display_manager = gdk_display_manager_get ();
823 g_signal_connect (display_manager, "notify::default-display",
824 G_CALLBACK (default_display_notify_cb), NULL);
825 g_signal_connect (display_manager, "display-opened",
826 G_CALLBACK (display_opened_cb), NULL);
828 /* initialize multhead aware gtk modules; for other modules,
829 * we wait until we have a display open;
831 for (slist = gtk_modules; slist; slist = slist->next)
835 GtkModuleInfo *info = slist->data;
837 if (info->display_init_func)
838 info->init_func (argc, argv);
845 #undef gtk_init_check
849 * @argc: Address of the <parameter>argc</parameter> parameter of your
850 * <function>main()</function> function. Changed if any arguments were
852 * @argv: Address of the <parameter>argv</parameter> parameter of
853 * <function>main()</function>. Any parameters understood by gtk_init()
854 * are stripped before return.
856 * This function does the same work as gtk_init() with only
857 * a single change: It does not terminate the program if the GUI can't be
858 * initialized. Instead it returns %FALSE on failure.
860 * This way the application can fall back to some other means of communication
861 * with the user - for example a curses or command line interface.
863 * Return value: %TRUE if the GUI has been successfully initialized,
867 gtk_init_check (int *argc,
872 if (!gtk_parse_args (argc, argv))
875 if (gdk_display_get_default ())
878 display = gdk_display_open (gdk_get_display_arg_name ());
882 gdk_display_manager_set_default_display (gdk_display_manager_get (),
894 * @argc: Address of the <parameter>argc</parameter> parameter of your
895 * <function>main()</function> function. Changed if any arguments were
897 * @argv: Address of the <parameter>argv</parameter> parameter of
898 * <function>main()</function>. Any parameters understood by gtk_init()
899 * are stripped before return.
901 * Call this function before using any other GTK+ functions in your GUI
902 * applications. It will initialize everything needed to operate the toolkit and
903 * parses some standard command line options. @argc and
904 * @argv are adjusted accordingly so your own code will
905 * never see those standard arguments.
908 * This function will terminate your program if it was unable to initialize
909 * the GUI for some reason. If you want your program to fall back to a
910 * textual interface you want to call gtk_init_check() instead.
914 gtk_init (int *argc, char ***argv)
916 if (!gtk_init_check (argc, argv))
918 char *display_name_arg = gdk_get_display_arg_name ();
919 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
924 #ifdef G_PLATFORM_WIN32
927 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
929 if (sizeof_GtkWindow != sizeof (GtkWindow))
930 g_error ("Incompatible build!\n"
931 "The code using GTK+ thinks GtkWindow is of different\n"
932 "size than it actually is in this build of GTK+.\n"
933 "On Windows, this probably means that you have compiled\n"
934 "your code with gcc without the -fnative-struct\n"
935 "(or -mms-bitfields) switch, or that you are using\n"
936 "an unsupported compiler.");
939 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
940 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
941 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
942 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
943 * -fnative-struct still matters. GtkBox is one such.
946 check_sizeof_GtkBox (size_t sizeof_GtkBox)
948 if (sizeof_GtkBox != sizeof (GtkBox))
949 g_error ("Incompatible build!\n"
950 "The code using GTK+ thinks GtkBox is of different\n"
951 "size than it actually is in this build of GTK+.\n"
952 "On Windows, this probably means that you have compiled\n"
953 "your code with gcc without the -fnative-struct\n"
954 "(or -mms-bitfields) switch, or that you are using\n"
955 "an unsupported compiler.");
958 /* These two functions might get more checks added later, thus pass
959 * in the number of extra args.
962 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
964 check_sizeof_GtkWindow (sizeof_GtkWindow);
966 check_sizeof_GtkBox (sizeof_GtkBox);
967 gtk_init (argc, argv);
971 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
973 check_sizeof_GtkWindow (sizeof_GtkWindow);
975 check_sizeof_GtkBox (sizeof_GtkBox);
976 return gtk_init_check (argc, argv);
982 gtk_exit (gint errorcode)
984 /* Only if "gtk" has been initialized should we de-initialize.
986 gdk_exit (errorcode);
993 * Initializes internationalization support for GTK+. gtk_init()
994 * automatically does this, so there is typically no point
995 * in calling this function.
997 * If you are calling this function because you changed the locale
998 * after GTK+ is was initialized, then calling this function
999 * may help a bit. (Note, however, that changing the locale
1000 * after GTK+ is initialized may produce inconsistent results and
1001 * is not really supported.)
1003 * In detail - sets the current locale according to the
1004 * program environment. This is the same as calling the C library function
1005 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
1006 * locale specific setup of the windowing system used by GDK.
1008 * Return value: a string corresponding to the locale set, as with the
1009 * C library function <function>setlocale()</function>.
1012 gtk_set_locale (void)
1014 return gdk_set_locale ();
1018 * gtk_get_default_language:
1020 * Returns the ISO language code for the default language currently in
1021 * effect. (Note that this can change over the life of an
1022 * application.) The default language is derived from the current
1023 * locale. It determines, for example, whether GTK+ uses the
1024 * right-to-left or left-to-right text direction.
1026 * Return value: the default language as an allocated string, must be freed
1029 gtk_get_default_language (void)
1032 PangoLanguage *result;
1036 /* Somebody might try to set the locale for this process using the
1037 * LANG or LC_ environment variables. The Microsoft C library
1038 * doesn't know anything about them. You set the locale in the
1039 * Control Panel. Setting these env vars won't have any affect on
1040 * locale-dependent C library functions like ctime. But just for
1041 * kicks, do obey LC_ALL, LANG and LC_CTYPE in GTK. (This also makes
1042 * it easier to test GTK and Pango in various default languages, you
1043 * don't have to clickety-click in the Control Panel, you can simply
1044 * start the program with LC_ALL=something on the command line.)
1046 p = getenv ("LC_ALL");
1048 lang = g_strdup (p);
1051 p = getenv ("LANG");
1053 lang = g_strdup (p);
1056 p = getenv ("LC_CTYPE");
1058 lang = g_strdup (p);
1060 lang = g_win32_getlocale ();
1064 lang = g_strdup (setlocale (LC_CTYPE, NULL));
1066 p = strchr (lang, '.');
1069 p = strchr (lang, '@');
1073 result = pango_language_from_string (lang);
1084 GtkInitFunction *init;
1087 gtk_main_loop_level++;
1089 loop = g_main_new (TRUE);
1090 main_loops = g_slist_prepend (main_loops, loop);
1092 tmp_list = functions = init_functions;
1093 init_functions = NULL;
1097 init = tmp_list->data;
1098 tmp_list = tmp_list->next;
1100 (* init->function) (init->data);
1103 g_list_free (functions);
1105 if (g_main_is_running (main_loops->data))
1107 GDK_THREADS_LEAVE ();
1109 GDK_THREADS_ENTER ();
1115 GList *reinvoke_list = NULL;
1116 GtkQuitFunction *quitf;
1118 while (quit_functions)
1120 quitf = quit_functions->data;
1122 tmp_list = quit_functions;
1123 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1124 g_list_free_1 (tmp_list);
1126 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1127 gtk_quit_invoke_function (quitf))
1129 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1133 gtk_quit_destroy (quitf);
1140 work = g_list_last (reinvoke_list);
1142 quit_functions->prev = work;
1143 work->next = quit_functions;
1144 quit_functions = work;
1150 main_loops = g_slist_remove (main_loops, loop);
1152 g_main_destroy (loop);
1154 gtk_main_loop_level--;
1158 gtk_main_level (void)
1160 return gtk_main_loop_level;
1164 gtk_main_quit (void)
1166 g_return_if_fail (main_loops != NULL);
1168 g_main_quit (main_loops->data);
1172 gtk_events_pending (void)
1176 GDK_THREADS_LEAVE ();
1177 result = g_main_pending ();
1178 GDK_THREADS_ENTER ();
1184 gtk_main_iteration (void)
1186 GDK_THREADS_LEAVE ();
1187 g_main_iteration (TRUE);
1188 GDK_THREADS_ENTER ();
1191 return !g_main_is_running (main_loops->data);
1197 gtk_main_iteration_do (gboolean blocking)
1199 GDK_THREADS_LEAVE ();
1200 g_main_iteration (blocking);
1201 GDK_THREADS_ENTER ();
1204 return !g_main_is_running (main_loops->data);
1209 /* private libgtk to libgdk interfaces
1211 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1212 GdkWindow **grab_window,
1213 gboolean *owner_events);
1214 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1215 GdkWindow **grab_window,
1216 gboolean *owner_events);
1219 rewrite_events_translate (GdkWindow *old_window,
1220 GdkWindow *new_window,
1224 gint old_origin_x, old_origin_y;
1225 gint new_origin_x, new_origin_y;
1227 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1228 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1230 *x += old_origin_x - new_origin_x;
1231 *y += old_origin_y - new_origin_y;
1235 rewrite_event_for_window (GdkEvent *event,
1236 GdkWindow *new_window)
1238 event = gdk_event_copy (event);
1240 switch (event->type)
1243 rewrite_events_translate (event->any.window,
1245 &event->scroll.x, &event->scroll.y);
1247 case GDK_BUTTON_PRESS:
1248 case GDK_2BUTTON_PRESS:
1249 case GDK_3BUTTON_PRESS:
1250 case GDK_BUTTON_RELEASE:
1251 rewrite_events_translate (event->any.window,
1253 &event->button.x, &event->button.y);
1255 case GDK_MOTION_NOTIFY:
1256 rewrite_events_translate (event->any.window,
1258 &event->motion.x, &event->motion.y);
1261 case GDK_KEY_RELEASE:
1262 case GDK_PROXIMITY_IN:
1263 case GDK_PROXIMITY_OUT:
1270 g_object_unref (event->any.window);
1271 event->any.window = g_object_ref (new_window);
1276 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1277 * then what X11 does is deliver the event normally if it was going to this
1278 * client, otherwise, delivers it in terms of the grab window. This function
1279 * rewrites events to the effect that events going to the same window group
1280 * are delivered normally, otherwise, the event is delivered in terms of the
1284 rewrite_event_for_grabs (GdkEvent *event)
1286 GdkWindow *grab_window;
1287 GtkWidget *event_widget, *grab_widget;
1288 gboolean owner_events;
1289 GdkDisplay *display;
1291 switch (event->type)
1294 case GDK_BUTTON_PRESS:
1295 case GDK_2BUTTON_PRESS:
1296 case GDK_3BUTTON_PRESS:
1297 case GDK_BUTTON_RELEASE:
1298 case GDK_MOTION_NOTIFY:
1299 case GDK_PROXIMITY_IN:
1300 case GDK_PROXIMITY_OUT:
1301 display = gdk_drawable_get_display (event->proximity.window);
1302 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1308 case GDK_KEY_RELEASE:
1309 display = gdk_drawable_get_display (event->key.window);
1310 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1319 event_widget = gtk_get_event_widget (event);
1320 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1323 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1324 return rewrite_event_for_window (event, grab_window);
1330 gtk_main_do_event (GdkEvent *event)
1332 GtkWidget *event_widget;
1333 GtkWidget *grab_widget;
1334 GtkWindowGroup *window_group;
1335 GdkEvent *next_event;
1336 GdkEvent *rewritten_event = NULL;
1339 /* If there are any events pending then get the next one.
1341 next_event = gdk_event_peek ();
1343 /* Try to compress enter/leave notify events. These event
1344 * pairs occur when the mouse is dragged quickly across
1345 * a window with many buttons (or through a menu). Instead
1346 * of highlighting and de-highlighting each widget that
1347 * is crossed it is better to simply de-highlight the widget
1348 * which contained the mouse initially and highlight the
1349 * widget which ends up containing the mouse.
1352 if (((event->type == GDK_ENTER_NOTIFY) ||
1353 (event->type == GDK_LEAVE_NOTIFY)) &&
1354 ((next_event->type == GDK_ENTER_NOTIFY) ||
1355 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1356 (next_event->type != event->type) &&
1357 (next_event->any.window == event->any.window))
1359 /* Throw both the peeked copy and the queued copy away
1361 gdk_event_free (next_event);
1362 next_event = gdk_event_get ();
1363 gdk_event_free (next_event);
1369 gdk_event_free (next_event);
1371 /* Find the widget which got the event. We store the widget
1372 * in the user_data field of GdkWindow's.
1373 * Ignore the event if we don't have a widget for it, except
1374 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1375 * Though this happens rarely, bogus events can occour
1376 * for e.g. destroyed GdkWindows.
1378 event_widget = gtk_get_event_widget (event);
1381 /* To handle selection INCR transactions, we select
1382 * PropertyNotify events on the requestor window and create
1383 * a corresponding (fake) GdkWindow so that events get
1384 * here. There won't be a widget though, so we have to handle
1387 if (event->type == GDK_PROPERTY_NOTIFY)
1388 gtk_selection_incr_event (event->any.window,
1390 else if (event->type == GDK_SETTING)
1391 _gtk_settings_handle_event (&event->setting);
1396 /* If pointer or keyboard grabs are in effect, munge the events
1397 * so that each window group looks like a separate app.
1399 rewritten_event = rewrite_event_for_grabs (event);
1400 if (rewritten_event)
1402 event = rewritten_event;
1403 event_widget = gtk_get_event_widget (event);
1406 window_group = gtk_main_get_window_group (event_widget);
1408 /* Push the event onto a stack of current events for
1409 * gtk_current_event_get().
1411 current_events = g_list_prepend (current_events, event);
1413 /* If there is a grab in effect...
1415 if (window_group->grabs)
1417 grab_widget = window_group->grabs->data;
1419 /* If the grab widget is an ancestor of the event widget
1420 * then we send the event to the original event widget.
1421 * This is the key to implementing modality.
1423 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1424 gtk_widget_is_ancestor (event_widget, grab_widget))
1425 grab_widget = event_widget;
1429 grab_widget = event_widget;
1432 /* Not all events get sent to the grabbing widget.
1433 * The delete, destroy, expose, focus change and resize
1434 * events still get sent to the event widget because
1435 * 1) these events have no meaning for the grabbing widget
1436 * and 2) redirecting these events to the grabbing widget
1437 * could cause the display to be messed up.
1439 * Drag events are also not redirected, since it isn't
1440 * clear what the semantics of that would be.
1442 switch (event->type)
1448 gtk_widget_ref (event_widget);
1449 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1450 !gtk_widget_event (event_widget, event))
1451 gtk_widget_destroy (event_widget);
1452 gtk_widget_unref (event_widget);
1456 /* Unexpected GDK_DESTROY from the outside, ignore for
1457 * child windows, handle like a GDK_DELETE for toplevels
1459 if (!event_widget->parent)
1461 gtk_widget_ref (event_widget);
1462 if (!gtk_widget_event (event_widget, event) &&
1463 GTK_WIDGET_REALIZED (event_widget))
1464 gtk_widget_destroy (event_widget);
1465 gtk_widget_unref (event_widget);
1470 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1472 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1473 gtk_widget_send_expose (event_widget, event);
1474 gdk_window_end_paint (event->any.window);
1477 gtk_widget_send_expose (event_widget, event);
1480 case GDK_PROPERTY_NOTIFY:
1482 case GDK_FOCUS_CHANGE:
1486 case GDK_SELECTION_CLEAR:
1487 case GDK_SELECTION_REQUEST:
1488 case GDK_SELECTION_NOTIFY:
1489 case GDK_CLIENT_EVENT:
1490 case GDK_VISIBILITY_NOTIFY:
1491 case GDK_WINDOW_STATE:
1492 gtk_widget_event (event_widget, event);
1496 case GDK_BUTTON_PRESS:
1497 case GDK_2BUTTON_PRESS:
1498 case GDK_3BUTTON_PRESS:
1499 gtk_propagate_event (grab_widget, event);
1503 case GDK_KEY_RELEASE:
1506 if (gtk_invoke_key_snoopers (grab_widget, event))
1509 /* else fall through */
1510 case GDK_MOTION_NOTIFY:
1511 case GDK_BUTTON_RELEASE:
1512 case GDK_PROXIMITY_IN:
1513 case GDK_PROXIMITY_OUT:
1514 gtk_propagate_event (grab_widget, event);
1517 case GDK_ENTER_NOTIFY:
1518 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1520 g_object_ref (event_widget);
1522 gtk_widget_event (grab_widget, event);
1523 if (event_widget == grab_widget)
1524 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1526 g_object_unref (event_widget);
1530 case GDK_LEAVE_NOTIFY:
1531 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1533 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1534 gtk_widget_event (event_widget, event);
1536 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1537 gtk_widget_event (grab_widget, event);
1540 case GDK_DRAG_STATUS:
1541 case GDK_DROP_FINISHED:
1542 _gtk_drag_source_handle_event (event_widget, event);
1544 case GDK_DRAG_ENTER:
1545 case GDK_DRAG_LEAVE:
1546 case GDK_DRAG_MOTION:
1547 case GDK_DROP_START:
1548 _gtk_drag_dest_handle_event (event_widget, event);
1551 g_assert_not_reached ();
1555 tmp_list = current_events;
1556 current_events = g_list_remove_link (current_events, tmp_list);
1557 g_list_free_1 (tmp_list);
1559 if (rewritten_event)
1560 gdk_event_free (rewritten_event);
1575 static GtkWindowGroup *
1576 gtk_main_get_window_group (GtkWidget *widget)
1578 GtkWidget *toplevel = NULL;
1581 toplevel = gtk_widget_get_toplevel (widget);
1583 if (toplevel && GTK_IS_WINDOW (toplevel))
1584 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1586 return _gtk_window_get_group (NULL);
1591 GtkWidget *old_grab_widget;
1592 GtkWidget *new_grab_widget;
1596 check_is_grabbed (GtkWidget *widget,
1597 GtkWidget *grab_widget)
1600 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1606 gtk_grab_notify_foreach (GtkWidget *child,
1610 GrabNotifyInfo *info = data;
1611 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1612 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1614 if (was_grabbed != is_grabbed)
1616 g_object_ref (G_OBJECT (child));
1618 gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", was_grabbed);
1620 if (GTK_IS_CONTAINER (child))
1621 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1623 g_object_unref (G_OBJECT (child));
1628 gtk_grab_notify (GtkWindowGroup *group,
1629 GtkWidget *grab_widget,
1630 gboolean was_grabbed)
1633 GrabNotifyInfo info;
1637 info.old_grab_widget = grab_widget;
1638 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1642 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1643 info.new_grab_widget = grab_widget;
1646 g_object_ref (group);
1647 g_object_ref (grab_widget);
1649 toplevels = gtk_window_list_toplevels ();
1650 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1654 GtkWindow *toplevel = toplevels->data;
1655 toplevels = g_list_delete_link (toplevels, toplevels);
1657 if (group == _gtk_window_get_group (toplevel))
1658 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1659 g_object_unref (toplevel);
1662 g_object_unref (group);
1663 g_object_unref (grab_widget);
1667 gtk_grab_add (GtkWidget *widget)
1669 GtkWindowGroup *group;
1670 gboolean was_grabbed;
1672 g_return_if_fail (widget != NULL);
1674 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1676 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1678 group = gtk_main_get_window_group (widget);
1680 was_grabbed = (group->grabs != NULL);
1682 gtk_widget_ref (widget);
1683 group->grabs = g_slist_prepend (group->grabs, widget);
1685 gtk_grab_notify (group, widget, FALSE);
1690 gtk_grab_get_current (void)
1692 GtkWindowGroup *group;
1694 group = gtk_main_get_window_group (NULL);
1697 return GTK_WIDGET (group->grabs->data);
1702 gtk_grab_remove (GtkWidget *widget)
1704 GtkWindowGroup *group;
1706 g_return_if_fail (widget != NULL);
1708 if (GTK_WIDGET_HAS_GRAB (widget))
1710 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1712 group = gtk_main_get_window_group (widget);
1713 group->grabs = g_slist_remove (group->grabs, widget);
1715 gtk_widget_unref (widget);
1717 gtk_grab_notify (group, widget, TRUE);
1722 gtk_init_add (GtkFunction function,
1725 GtkInitFunction *init;
1727 init = g_new (GtkInitFunction, 1);
1728 init->function = function;
1731 init_functions = g_list_prepend (init_functions, init);
1735 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1738 GtkKeySnooperData *data;
1739 static guint snooper_id = 1;
1741 g_return_val_if_fail (snooper != NULL, 0);
1743 data = g_new (GtkKeySnooperData, 1);
1744 data->func = snooper;
1745 data->func_data = func_data;
1746 data->id = snooper_id++;
1747 key_snoopers = g_slist_prepend (key_snoopers, data);
1753 gtk_key_snooper_remove (guint snooper_id)
1755 GtkKeySnooperData *data = NULL;
1758 slist = key_snoopers;
1762 if (data->id == snooper_id)
1765 slist = slist->next;
1769 key_snoopers = g_slist_remove (key_snoopers, data);
1773 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1777 gint return_val = FALSE;
1779 slist = key_snoopers;
1780 while (slist && !return_val)
1782 GtkKeySnooperData *data;
1785 slist = slist->next;
1786 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1793 gtk_quit_add_full (guint main_level,
1794 GtkFunction function,
1795 GtkCallbackMarshal marshal,
1797 GtkDestroyNotify destroy)
1799 static guint quit_id = 1;
1800 GtkQuitFunction *quitf;
1802 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1804 if (!quit_mem_chunk)
1805 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1806 512, G_ALLOC_AND_FREE);
1808 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1810 quitf->id = quit_id++;
1811 quitf->main_level = main_level;
1812 quitf->function = function;
1813 quitf->marshal = marshal;
1815 quitf->destroy = destroy;
1817 quit_functions = g_list_prepend (quit_functions, quitf);
1823 gtk_quit_destroy (GtkQuitFunction *quitf)
1826 quitf->destroy (quitf->data);
1827 g_mem_chunk_free (quit_mem_chunk, quitf);
1831 gtk_quit_destructor (GtkObject **object_p)
1834 gtk_object_destroy (*object_p);
1841 gtk_quit_add_destroy (guint main_level,
1844 GtkObject **object_p;
1846 g_return_if_fail (main_level > 0);
1847 g_return_if_fail (GTK_IS_OBJECT (object));
1849 object_p = g_new (GtkObject*, 1);
1851 gtk_signal_connect (object,
1853 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1855 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1859 gtk_quit_add (guint main_level,
1860 GtkFunction function,
1863 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1867 gtk_quit_remove (guint id)
1869 GtkQuitFunction *quitf;
1872 tmp_list = quit_functions;
1875 quitf = tmp_list->data;
1877 if (quitf->id == id)
1879 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1880 g_list_free (tmp_list);
1881 gtk_quit_destroy (quitf);
1886 tmp_list = tmp_list->next;
1891 gtk_quit_remove_by_data (gpointer data)
1893 GtkQuitFunction *quitf;
1896 tmp_list = quit_functions;
1899 quitf = tmp_list->data;
1901 if (quitf->data == data)
1903 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1904 g_list_free (tmp_list);
1905 gtk_quit_destroy (quitf);
1910 tmp_list = tmp_list->next;
1915 gtk_timeout_add_full (guint32 interval,
1916 GtkFunction function,
1917 GtkCallbackMarshal marshal,
1919 GtkDestroyNotify destroy)
1923 GtkClosure *closure;
1925 closure = g_new (GtkClosure, 1);
1926 closure->marshal = marshal;
1927 closure->data = data;
1928 closure->destroy = destroy;
1930 return g_timeout_add_full (0, interval,
1931 gtk_invoke_idle_timeout,
1933 gtk_destroy_closure);
1936 return g_timeout_add_full (0, interval, function, data, destroy);
1940 gtk_timeout_add (guint32 interval,
1941 GtkFunction function,
1944 return g_timeout_add_full (0, interval, function, data, NULL);
1948 gtk_timeout_remove (guint tag)
1950 g_source_remove (tag);
1954 gtk_idle_add_full (gint priority,
1955 GtkFunction function,
1956 GtkCallbackMarshal marshal,
1958 GtkDestroyNotify destroy)
1962 GtkClosure *closure;
1964 closure = g_new (GtkClosure, 1);
1965 closure->marshal = marshal;
1966 closure->data = data;
1967 closure->destroy = destroy;
1969 return g_idle_add_full (priority,
1970 gtk_invoke_idle_timeout,
1972 gtk_destroy_closure);
1975 return g_idle_add_full (priority, function, data, destroy);
1979 gtk_idle_add (GtkFunction function,
1982 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1986 gtk_idle_add_priority (gint priority,
1987 GtkFunction function,
1990 return g_idle_add_full (priority, function, data, NULL);
1994 gtk_idle_remove (guint tag)
1996 g_source_remove (tag);
2000 gtk_idle_remove_by_data (gpointer data)
2002 if (!g_idle_remove_by_data (data))
2003 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2007 gtk_input_add_full (gint source,
2008 GdkInputCondition condition,
2009 GdkInputFunction function,
2010 GtkCallbackMarshal marshal,
2012 GtkDestroyNotify destroy)
2016 GtkClosure *closure;
2018 closure = g_new (GtkClosure, 1);
2019 closure->marshal = marshal;
2020 closure->data = data;
2021 closure->destroy = destroy;
2023 return gdk_input_add_full (source,
2025 (GdkInputFunction) gtk_invoke_input,
2027 (GdkDestroyNotify) gtk_destroy_closure);
2030 return gdk_input_add_full (source, condition, function, data, destroy);
2034 gtk_input_remove (guint tag)
2036 g_source_remove (tag);
2040 gtk_destroy_closure (gpointer data)
2042 GtkClosure *closure = data;
2044 if (closure->destroy)
2045 (closure->destroy) (closure->data);
2050 gtk_invoke_idle_timeout (gpointer data)
2052 GtkClosure *closure = data;
2055 gint ret_val = FALSE;
2056 args[0].name = NULL;
2057 args[0].type = GTK_TYPE_BOOL;
2058 args[0].d.pointer_data = &ret_val;
2059 closure->marshal (NULL, closure->data, 0, args);
2064 gtk_invoke_input (gpointer data,
2066 GdkInputCondition condition)
2068 GtkClosure *closure = data;
2071 args[0].type = GTK_TYPE_INT;
2072 args[0].name = NULL;
2073 GTK_VALUE_INT (args[0]) = source;
2074 args[1].type = GDK_TYPE_INPUT_CONDITION;
2075 args[1].name = NULL;
2076 GTK_VALUE_FLAGS (args[1]) = condition;
2077 args[2].type = GTK_TYPE_NONE;
2078 args[2].name = NULL;
2080 closure->marshal (NULL, closure->data, 2, args);
2084 * gtk_get_current_event:
2086 * Obtains a copy of the event currently being processed by GTK+. For
2087 * example, if you get a "clicked" signal from #GtkButton, the current
2088 * event will be the #GdkEventButton that triggered the "clicked"
2089 * signal. The returned event must be freed with gdk_event_free().
2090 * If there is no current event, the function returns %NULL.
2092 * Return value: a copy of the current event, or %NULL if no current event.
2095 gtk_get_current_event (void)
2098 return gdk_event_copy (current_events->data);
2104 * gtk_get_current_event_time:
2106 * If there is a current event and it has a timestamp, return that
2107 * timestamp, otherwise return %GDK_CURRENT_TIME.
2109 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2112 gtk_get_current_event_time (void)
2115 return gdk_event_get_time (current_events->data);
2117 return GDK_CURRENT_TIME;
2121 * gtk_get_current_event_state:
2122 * @state: a location to store the state of the current event
2124 * If there is a current event and it has a state field, place
2125 * that state field in @state and return %TRUE, otherwise return
2128 * Return value: %TRUE if there was a current event and it had a state field
2131 gtk_get_current_event_state (GdkModifierType *state)
2133 g_return_val_if_fail (state != NULL, FALSE);
2136 return gdk_event_get_state (current_events->data, state);
2145 * gtk_get_event_widget:
2146 * @event: a #GdkEvent
2148 * If @event is %NULL or the event was not associated with any widget,
2149 * returns %NULL, otherwise returns the widget that received the event
2152 * Return value: the widget that originally received @event, or %NULL
2155 gtk_get_event_widget (GdkEvent *event)
2160 if (event && event->any.window)
2161 gdk_window_get_user_data (event->any.window, (void**) &widget);
2167 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2169 if (!quitf->marshal)
2170 return quitf->function (quitf->data);
2174 gint ret_val = FALSE;
2176 args[0].name = NULL;
2177 args[0].type = GTK_TYPE_BOOL;
2178 args[0].d.pointer_data = &ret_val;
2179 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2187 * gtk_propagate_event:
2188 * @widget: a #GtkWidget
2191 * Sends an event to a widget, propagating the event to parent widgets
2192 * if the event remains unhandled. Events received by GTK+ from GDK
2193 * normally begin in gtk_main_do_event(). Depending on the type of
2194 * event, existence of modal dialogs, grabs, etc., the event may be
2195 * propagated; if so, this function is used. gtk_propagate_event()
2196 * calls gtk_widget_event() on each widget it decides to send the
2197 * event to. So gtk_widget_event() is the lowest-level function; it
2198 * simply emits the "event" and possibly an event-specific signal on a
2199 * widget. gtk_propagate_event() is a bit higher-level, and
2200 * gtk_main_do_event() is the highest level.
2202 * All that said, you most likely don't want to use any of these
2203 * functions; synthesizing events is rarely needed. Consider asking on
2204 * the mailing list for better ways to achieve your goals. For
2205 * example, use gdk_window_invalidate_rect() or
2206 * gtk_widget_queue_draw() instead of making up expose events.
2210 gtk_propagate_event (GtkWidget *widget,
2215 g_return_if_fail (GTK_IS_WIDGET (widget));
2216 g_return_if_fail (event != NULL);
2218 handled_event = FALSE;
2220 gtk_widget_ref (widget);
2222 if ((event->type == GDK_KEY_PRESS) ||
2223 (event->type == GDK_KEY_RELEASE))
2225 /* Only send key events within Window widgets to the Window
2226 * The Window widget will in turn pass the
2227 * key event on to the currently focused widget
2232 window = gtk_widget_get_toplevel (widget);
2233 if (window && GTK_IS_WINDOW (window))
2235 /* If there is a grab within the window, give the grab widget
2236 * a first crack at the key event
2238 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2239 handled_event = gtk_widget_event (widget, event);
2243 window = gtk_widget_get_toplevel (widget);
2244 if (window && GTK_IS_WINDOW (window))
2246 if (GTK_WIDGET_IS_SENSITIVE (window))
2247 gtk_widget_event (window, event);
2251 handled_event = TRUE; /* don't send to widget */
2255 /* Other events get propagated up the widget tree
2256 * so that parents can see the button and motion
2257 * events of the children.
2265 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2266 tmp = widget->parent;
2267 gtk_widget_unref (widget);
2271 if (!handled_event && widget)
2272 gtk_widget_ref (widget);
2278 gtk_widget_unref (widget);
2283 gtk_error (gchar *str)
2289 gtk_warning (gchar *str)
2295 gtk_message (gchar *str)
2301 gtk_print (gchar *str)
2303 static GtkWidget *window = NULL;
2304 static GtkWidget *text;
2305 static int level = 0;
2309 GtkWidget *hscrollbar;
2310 GtkWidget *vscrollbar;
2311 GtkWidget *separator;
2316 fputs (str, stdout);
2323 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2325 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2326 (GtkSignalFunc) gtk_widget_destroyed,
2329 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2331 box1 = gtk_vbox_new (FALSE, 0);
2332 gtk_container_add (GTK_CONTAINER (window), box1);
2333 gtk_widget_show (box1);
2336 box2 = gtk_vbox_new (FALSE, 10);
2337 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2338 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2339 gtk_widget_show (box2);
2342 table = gtk_table_new (2, 2, FALSE);
2343 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2344 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2345 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2346 gtk_widget_show (table);
2348 text = gtk_text_new (NULL, NULL);
2349 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2350 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2351 gtk_widget_show (text);
2352 gtk_widget_realize (text);
2354 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2355 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2356 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2357 gtk_widget_show (hscrollbar);
2359 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2360 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2361 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2362 gtk_widget_show (vscrollbar);
2364 separator = gtk_hseparator_new ();
2365 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2366 gtk_widget_show (separator);
2369 box2 = gtk_vbox_new (FALSE, 10);
2370 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2371 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2372 gtk_widget_show (box2);
2375 button = gtk_button_new_with_label ("close");
2376 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2377 (GtkSignalFunc) gtk_widget_hide,
2378 GTK_OBJECT (window));
2379 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2380 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2381 gtk_widget_grab_default (button);
2382 gtk_widget_show (button);
2386 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2389 if (!GTK_WIDGET_VISIBLE (window))
2390 gtk_widget_show (window);
2395 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2396 GValue *return_accu,
2397 const GValue *handler_return,
2400 gboolean continue_emission;
2401 gboolean signal_handled;
2403 signal_handled = g_value_get_boolean (handler_return);
2404 g_value_set_boolean (return_accu, signal_handled);
2405 continue_emission = !signal_handled;
2407 return continue_emission;