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 */
54 #include "gtkaccelmap.h"
57 #include "gtkversion.h"
60 #include "gtkselection.h"
61 #include "gtksettings.h"
62 #include "gtkwidget.h"
63 #include "gtkwindow.h"
64 #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,
870 if (!gtk_parse_args (argc, argv))
873 return gdk_display_open_default_libgtk_only () != NULL;
880 * @argc: Address of the <parameter>argc</parameter> parameter of your
881 * <function>main()</function> function. Changed if any arguments were
883 * @argv: Address of the <parameter>argv</parameter> parameter of
884 * <function>main()</function>. Any parameters understood by gtk_init()
885 * are stripped before return.
887 * Call this function before using any other GTK+ functions in your GUI
888 * applications. It will initialize everything needed to operate the toolkit and
889 * parses some standard command line options. @argc and
890 * @argv are adjusted accordingly so your own code will
891 * never see those standard arguments.
894 * This function will terminate your program if it was unable to initialize
895 * the GUI for some reason. If you want your program to fall back to a
896 * textual interface you want to call gtk_init_check() instead.
900 gtk_init (int *argc, char ***argv)
902 if (!gtk_init_check (argc, argv))
904 const char *display_name_arg = gdk_get_display_arg_name ();
905 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
910 #ifdef G_PLATFORM_WIN32
913 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
915 if (sizeof_GtkWindow != sizeof (GtkWindow))
916 g_error ("Incompatible build!\n"
917 "The code using GTK+ thinks GtkWindow is of different\n"
918 "size than it actually is in this build of GTK+.\n"
919 "On Windows, this probably means that you have compiled\n"
920 "your code with gcc without the -fnative-struct\n"
921 "(or -mms-bitfields) switch, or that you are using\n"
922 "an unsupported compiler.");
925 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
926 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
927 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
928 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
929 * -fnative-struct still matters. GtkBox is one such.
932 check_sizeof_GtkBox (size_t sizeof_GtkBox)
934 if (sizeof_GtkBox != sizeof (GtkBox))
935 g_error ("Incompatible build!\n"
936 "The code using GTK+ thinks GtkBox is of different\n"
937 "size than it actually is in this build of GTK+.\n"
938 "On Windows, this probably means that you have compiled\n"
939 "your code with gcc without the -fnative-struct\n"
940 "(or -mms-bitfields) switch, or that you are using\n"
941 "an unsupported compiler.");
944 /* These two functions might get more checks added later, thus pass
945 * in the number of extra args.
948 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
950 check_sizeof_GtkWindow (sizeof_GtkWindow);
952 check_sizeof_GtkBox (sizeof_GtkBox);
953 gtk_init (argc, argv);
957 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
959 check_sizeof_GtkWindow (sizeof_GtkWindow);
961 check_sizeof_GtkBox (sizeof_GtkBox);
962 return gtk_init_check (argc, argv);
968 gtk_exit (gint errorcode)
977 * Initializes internationalization support for GTK+. gtk_init()
978 * automatically does this, so there is typically no point
979 * in calling this function.
981 * If you are calling this function because you changed the locale
982 * after GTK+ is was initialized, then calling this function
983 * may help a bit. (Note, however, that changing the locale
984 * after GTK+ is initialized may produce inconsistent results and
985 * is not really supported.)
987 * In detail - sets the current locale according to the
988 * program environment. This is the same as calling the C library function
989 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
990 * locale specific setup of the windowing system used by GDK.
992 * Return: a string corresponding to the locale set, typically in the
993 * form lang_COUNTRY, where lang is an ISO-639 language code, and
994 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
995 * result of the <function>setlocale()</function>; it is also used on
996 * other machines, such as Windows, where the C library returns a
997 * different result. The string is owned by GTK+ and should not be
1001 gtk_set_locale (void)
1003 return gdk_set_locale ();
1007 * _gtk_get_lc_ctype:
1009 * Return the Unix-style locale string for the language currently in
1010 * effect. On Unix systems, this is the return value from
1011 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1012 * affect this through the environment variables LC_ALL, LC_CTYPE or
1013 * LANG (checked in that order). The locale strings typically is in
1014 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1015 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1016 * Swedish as written in Finland or pt_BR for Portuguese as written in
1019 * On Windows, the C library doesn't use any such environment
1020 * variables, and setting them won't affect the behaviour of functions
1021 * like <function>ctime()</function>. The user sets the locale through
1022 * the Regional Options in the Control Panel. The C library (in the
1023 * <function>setlocale()</function> function) does not use country and
1024 * language codes, but country and language names spelled out in
1025 * English. However, this function does check the above environment
1026 * variables, and does return a Unix-style locale string based on
1027 * either said environment variables or the thread's current locale.
1029 * Return value: a dynamically allocated string, free with g_free().
1033 _gtk_get_lc_ctype (void)
1038 /* Somebody might try to set the locale for this process using the
1039 * LANG or LC_ environment variables. The Microsoft C library
1040 * doesn't know anything about them. You set the locale in the
1041 * Control Panel. Setting these env vars won't have any affect on
1042 * locale-dependent C library functions like ctime(). But just for
1043 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1044 * it easier to test GTK and Pango in various default languages, you
1045 * don't have to clickety-click in the Control Panel, you can simply
1046 * start the program with LC_ALL=something on the command line.)
1048 p = getenv ("LC_ALL");
1050 return g_strdup (p);
1052 p = getenv ("LC_CTYPE");
1054 return g_strdup (p);
1056 p = getenv ("LANG");
1058 return g_strdup (p);
1060 return g_win32_getlocale ();
1062 return g_strdup (setlocale (LC_CTYPE, NULL));
1067 * gtk_get_default_language:
1069 * Returns the #PangoLanguage for the default language currently in
1070 * effect. (Note that this can change over the life of an
1071 * application.) The default language is derived from the current
1072 * locale. It determines, for example, whether GTK+ uses the
1073 * right-to-left or left-to-right text direction. See
1074 * _gtk_get_lc_ctype for notes on behaviour on Windows.
1076 * Return value: the default language as a #PangoLanguage, must not be
1080 gtk_get_default_language (void)
1083 PangoLanguage *result;
1086 lang = _gtk_get_lc_ctype ();
1087 p = strchr (lang, '.');
1090 p = strchr (lang, '@');
1094 result = pango_language_from_string (lang);
1105 GtkInitFunction *init;
1108 gtk_main_loop_level++;
1110 loop = g_main_loop_new (NULL, TRUE);
1111 main_loops = g_slist_prepend (main_loops, loop);
1113 tmp_list = functions = init_functions;
1114 init_functions = NULL;
1118 init = tmp_list->data;
1119 tmp_list = tmp_list->next;
1121 (* init->function) (init->data);
1124 g_list_free (functions);
1126 if (g_main_loop_is_running (main_loops->data))
1128 GDK_THREADS_LEAVE ();
1129 g_main_loop_run (loop);
1130 GDK_THREADS_ENTER ();
1136 GList *reinvoke_list = NULL;
1137 GtkQuitFunction *quitf;
1139 while (quit_functions)
1141 quitf = quit_functions->data;
1143 tmp_list = quit_functions;
1144 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1145 g_list_free_1 (tmp_list);
1147 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1148 gtk_quit_invoke_function (quitf))
1150 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1154 gtk_quit_destroy (quitf);
1161 work = g_list_last (reinvoke_list);
1163 quit_functions->prev = work;
1164 work->next = quit_functions;
1165 quit_functions = work;
1171 main_loops = g_slist_remove (main_loops, loop);
1173 g_main_loop_unref (loop);
1175 gtk_main_loop_level--;
1179 gtk_main_level (void)
1181 return gtk_main_loop_level;
1185 gtk_main_quit (void)
1187 g_return_if_fail (main_loops != NULL);
1189 g_main_loop_quit (main_loops->data);
1193 gtk_events_pending (void)
1197 GDK_THREADS_LEAVE ();
1198 result = g_main_context_pending (NULL);
1199 GDK_THREADS_ENTER ();
1205 gtk_main_iteration (void)
1207 GDK_THREADS_LEAVE ();
1208 g_main_context_iteration (NULL, TRUE);
1209 GDK_THREADS_ENTER ();
1212 return !g_main_loop_is_running (main_loops->data);
1218 gtk_main_iteration_do (gboolean blocking)
1220 GDK_THREADS_LEAVE ();
1221 g_main_context_iteration (NULL, blocking);
1222 GDK_THREADS_ENTER ();
1225 return !g_main_loop_is_running (main_loops->data);
1230 /* private libgtk to libgdk interfaces
1232 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1233 GdkWindow **grab_window,
1234 gboolean *owner_events);
1235 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1236 GdkWindow **grab_window,
1237 gboolean *owner_events);
1240 rewrite_events_translate (GdkWindow *old_window,
1241 GdkWindow *new_window,
1245 gint old_origin_x, old_origin_y;
1246 gint new_origin_x, new_origin_y;
1248 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1249 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1251 *x += old_origin_x - new_origin_x;
1252 *y += old_origin_y - new_origin_y;
1256 rewrite_event_for_window (GdkEvent *event,
1257 GdkWindow *new_window)
1259 event = gdk_event_copy (event);
1261 switch (event->type)
1264 rewrite_events_translate (event->any.window,
1266 &event->scroll.x, &event->scroll.y);
1268 case GDK_BUTTON_PRESS:
1269 case GDK_2BUTTON_PRESS:
1270 case GDK_3BUTTON_PRESS:
1271 case GDK_BUTTON_RELEASE:
1272 rewrite_events_translate (event->any.window,
1274 &event->button.x, &event->button.y);
1276 case GDK_MOTION_NOTIFY:
1277 rewrite_events_translate (event->any.window,
1279 &event->motion.x, &event->motion.y);
1282 case GDK_KEY_RELEASE:
1283 case GDK_PROXIMITY_IN:
1284 case GDK_PROXIMITY_OUT:
1291 g_object_unref (event->any.window);
1292 event->any.window = g_object_ref (new_window);
1297 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1298 * then what X11 does is deliver the event normally if it was going to this
1299 * client, otherwise, delivers it in terms of the grab window. This function
1300 * rewrites events to the effect that events going to the same window group
1301 * are delivered normally, otherwise, the event is delivered in terms of the
1305 rewrite_event_for_grabs (GdkEvent *event)
1307 GdkWindow *grab_window;
1308 GtkWidget *event_widget, *grab_widget;
1309 gboolean owner_events;
1310 GdkDisplay *display;
1312 switch (event->type)
1315 case GDK_BUTTON_PRESS:
1316 case GDK_2BUTTON_PRESS:
1317 case GDK_3BUTTON_PRESS:
1318 case GDK_BUTTON_RELEASE:
1319 case GDK_MOTION_NOTIFY:
1320 case GDK_PROXIMITY_IN:
1321 case GDK_PROXIMITY_OUT:
1322 display = gdk_drawable_get_display (event->proximity.window);
1323 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1329 case GDK_KEY_RELEASE:
1330 display = gdk_drawable_get_display (event->key.window);
1331 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1340 event_widget = gtk_get_event_widget (event);
1341 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1344 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1345 return rewrite_event_for_window (event, grab_window);
1351 gtk_main_do_event (GdkEvent *event)
1353 GtkWidget *event_widget;
1354 GtkWidget *grab_widget;
1355 GtkWindowGroup *window_group;
1356 GdkEvent *next_event;
1357 GdkEvent *rewritten_event = NULL;
1360 /* If there are any events pending then get the next one.
1362 next_event = gdk_event_peek ();
1364 /* Try to compress enter/leave notify events. These event
1365 * pairs occur when the mouse is dragged quickly across
1366 * a window with many buttons (or through a menu). Instead
1367 * of highlighting and de-highlighting each widget that
1368 * is crossed it is better to simply de-highlight the widget
1369 * which contained the mouse initially and highlight the
1370 * widget which ends up containing the mouse.
1373 if (((event->type == GDK_ENTER_NOTIFY) ||
1374 (event->type == GDK_LEAVE_NOTIFY)) &&
1375 ((next_event->type == GDK_ENTER_NOTIFY) ||
1376 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1377 (next_event->type != event->type) &&
1378 (next_event->any.window == event->any.window))
1380 /* Throw both the peeked copy and the queued copy away
1382 gdk_event_free (next_event);
1383 next_event = gdk_event_get ();
1384 gdk_event_free (next_event);
1390 gdk_event_free (next_event);
1392 if (event->type == GDK_SETTING)
1394 _gtk_settings_handle_event (&event->setting);
1398 /* Find the widget which got the event. We store the widget
1399 * in the user_data field of GdkWindow's.
1400 * Ignore the event if we don't have a widget for it, except
1401 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1402 * Though this happens rarely, bogus events can occour
1403 * for e.g. destroyed GdkWindows.
1405 event_widget = gtk_get_event_widget (event);
1408 /* To handle selection INCR transactions, we select
1409 * PropertyNotify events on the requestor window and create
1410 * a corresponding (fake) GdkWindow so that events get
1411 * here. There won't be a widget though, so we have to handle
1414 if (event->type == GDK_PROPERTY_NOTIFY)
1415 _gtk_selection_incr_event (event->any.window,
1421 /* If pointer or keyboard grabs are in effect, munge the events
1422 * so that each window group looks like a separate app.
1424 rewritten_event = rewrite_event_for_grabs (event);
1425 if (rewritten_event)
1427 event = rewritten_event;
1428 event_widget = gtk_get_event_widget (event);
1431 window_group = gtk_main_get_window_group (event_widget);
1433 /* Push the event onto a stack of current events for
1434 * gtk_current_event_get().
1436 current_events = g_list_prepend (current_events, event);
1438 /* If there is a grab in effect...
1440 if (window_group->grabs)
1442 grab_widget = window_group->grabs->data;
1444 /* If the grab widget is an ancestor of the event widget
1445 * then we send the event to the original event widget.
1446 * This is the key to implementing modality.
1448 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1449 gtk_widget_is_ancestor (event_widget, grab_widget))
1450 grab_widget = event_widget;
1454 grab_widget = event_widget;
1457 /* Not all events get sent to the grabbing widget.
1458 * The delete, destroy, expose, focus change and resize
1459 * events still get sent to the event widget because
1460 * 1) these events have no meaning for the grabbing widget
1461 * and 2) redirecting these events to the grabbing widget
1462 * could cause the display to be messed up.
1464 * Drag events are also not redirected, since it isn't
1465 * clear what the semantics of that would be.
1467 switch (event->type)
1473 g_object_ref (event_widget);
1474 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1475 !gtk_widget_event (event_widget, event))
1476 gtk_widget_destroy (event_widget);
1477 g_object_unref (event_widget);
1481 /* Unexpected GDK_DESTROY from the outside, ignore for
1482 * child windows, handle like a GDK_DELETE for toplevels
1484 if (!event_widget->parent)
1486 g_object_ref (event_widget);
1487 if (!gtk_widget_event (event_widget, event) &&
1488 GTK_WIDGET_REALIZED (event_widget))
1489 gtk_widget_destroy (event_widget);
1490 g_object_unref (event_widget);
1495 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1497 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1498 gtk_widget_send_expose (event_widget, event);
1499 gdk_window_end_paint (event->any.window);
1502 gtk_widget_send_expose (event_widget, event);
1505 case GDK_PROPERTY_NOTIFY:
1507 case GDK_FOCUS_CHANGE:
1511 case GDK_SELECTION_CLEAR:
1512 case GDK_SELECTION_REQUEST:
1513 case GDK_SELECTION_NOTIFY:
1514 case GDK_CLIENT_EVENT:
1515 case GDK_VISIBILITY_NOTIFY:
1516 case GDK_WINDOW_STATE:
1517 gtk_widget_event (event_widget, event);
1521 case GDK_BUTTON_PRESS:
1522 case GDK_2BUTTON_PRESS:
1523 case GDK_3BUTTON_PRESS:
1524 gtk_propagate_event (grab_widget, event);
1528 case GDK_KEY_RELEASE:
1531 if (gtk_invoke_key_snoopers (grab_widget, event))
1534 /* else fall through */
1535 case GDK_MOTION_NOTIFY:
1536 case GDK_BUTTON_RELEASE:
1537 case GDK_PROXIMITY_IN:
1538 case GDK_PROXIMITY_OUT:
1539 gtk_propagate_event (grab_widget, event);
1542 case GDK_ENTER_NOTIFY:
1543 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1545 g_object_ref (event_widget);
1547 gtk_widget_event (grab_widget, event);
1548 if (event_widget == grab_widget)
1549 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1551 g_object_unref (event_widget);
1555 case GDK_LEAVE_NOTIFY:
1556 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1558 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1559 gtk_widget_event (event_widget, event);
1561 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1562 gtk_widget_event (grab_widget, event);
1565 case GDK_DRAG_STATUS:
1566 case GDK_DROP_FINISHED:
1567 _gtk_drag_source_handle_event (event_widget, event);
1569 case GDK_DRAG_ENTER:
1570 case GDK_DRAG_LEAVE:
1571 case GDK_DRAG_MOTION:
1572 case GDK_DROP_START:
1573 _gtk_drag_dest_handle_event (event_widget, event);
1576 g_assert_not_reached ();
1580 tmp_list = current_events;
1581 current_events = g_list_remove_link (current_events, tmp_list);
1582 g_list_free_1 (tmp_list);
1584 if (rewritten_event)
1585 gdk_event_free (rewritten_event);
1600 static GtkWindowGroup *
1601 gtk_main_get_window_group (GtkWidget *widget)
1603 GtkWidget *toplevel = NULL;
1606 toplevel = gtk_widget_get_toplevel (widget);
1608 if (toplevel && GTK_IS_WINDOW (toplevel))
1609 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1611 return _gtk_window_get_group (NULL);
1616 GtkWidget *old_grab_widget;
1617 GtkWidget *new_grab_widget;
1621 check_is_grabbed (GtkWidget *widget,
1622 GtkWidget *grab_widget)
1625 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1631 gtk_grab_notify_foreach (GtkWidget *child,
1635 GrabNotifyInfo *info = data;
1636 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1637 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1639 if (was_grabbed != is_grabbed)
1641 g_object_ref (child);
1643 g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1645 if (GTK_IS_CONTAINER (child))
1646 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1648 g_object_unref (child);
1653 gtk_grab_notify (GtkWindowGroup *group,
1654 GtkWidget *grab_widget,
1655 gboolean was_grabbed)
1658 GrabNotifyInfo info;
1662 info.old_grab_widget = grab_widget;
1663 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1667 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1668 info.new_grab_widget = grab_widget;
1671 g_object_ref (group);
1672 g_object_ref (grab_widget);
1674 toplevels = gtk_window_list_toplevels ();
1675 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1679 GtkWindow *toplevel = toplevels->data;
1680 toplevels = g_list_delete_link (toplevels, toplevels);
1682 if (group == _gtk_window_get_group (toplevel))
1683 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1684 g_object_unref (toplevel);
1687 g_object_unref (group);
1688 g_object_unref (grab_widget);
1692 gtk_grab_add (GtkWidget *widget)
1694 GtkWindowGroup *group;
1695 gboolean was_grabbed;
1697 g_return_if_fail (widget != NULL);
1699 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1701 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1703 group = gtk_main_get_window_group (widget);
1705 was_grabbed = (group->grabs != NULL);
1707 g_object_ref (widget);
1708 group->grabs = g_slist_prepend (group->grabs, widget);
1710 gtk_grab_notify (group, widget, FALSE);
1715 gtk_grab_get_current (void)
1717 GtkWindowGroup *group;
1719 group = gtk_main_get_window_group (NULL);
1722 return GTK_WIDGET (group->grabs->data);
1727 gtk_grab_remove (GtkWidget *widget)
1729 GtkWindowGroup *group;
1731 g_return_if_fail (widget != NULL);
1733 if (GTK_WIDGET_HAS_GRAB (widget))
1735 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1737 group = gtk_main_get_window_group (widget);
1738 group->grabs = g_slist_remove (group->grabs, widget);
1740 g_object_unref (widget);
1742 gtk_grab_notify (group, widget, TRUE);
1747 gtk_init_add (GtkFunction function,
1750 GtkInitFunction *init;
1752 init = g_new (GtkInitFunction, 1);
1753 init->function = function;
1756 init_functions = g_list_prepend (init_functions, init);
1760 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1763 GtkKeySnooperData *data;
1764 static guint snooper_id = 1;
1766 g_return_val_if_fail (snooper != NULL, 0);
1768 data = g_new (GtkKeySnooperData, 1);
1769 data->func = snooper;
1770 data->func_data = func_data;
1771 data->id = snooper_id++;
1772 key_snoopers = g_slist_prepend (key_snoopers, data);
1778 gtk_key_snooper_remove (guint snooper_id)
1780 GtkKeySnooperData *data = NULL;
1783 slist = key_snoopers;
1787 if (data->id == snooper_id)
1790 slist = slist->next;
1794 key_snoopers = g_slist_remove (key_snoopers, data);
1798 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1802 gint return_val = FALSE;
1804 slist = key_snoopers;
1805 while (slist && !return_val)
1807 GtkKeySnooperData *data;
1810 slist = slist->next;
1811 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1818 gtk_quit_add_full (guint main_level,
1819 GtkFunction function,
1820 GtkCallbackMarshal marshal,
1822 GtkDestroyNotify destroy)
1824 static guint quit_id = 1;
1825 GtkQuitFunction *quitf;
1827 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1829 if (!quit_mem_chunk)
1830 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1831 512, G_ALLOC_AND_FREE);
1833 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1835 quitf->id = quit_id++;
1836 quitf->main_level = main_level;
1837 quitf->function = function;
1838 quitf->marshal = marshal;
1840 quitf->destroy = destroy;
1842 quit_functions = g_list_prepend (quit_functions, quitf);
1848 gtk_quit_destroy (GtkQuitFunction *quitf)
1851 quitf->destroy (quitf->data);
1852 g_mem_chunk_free (quit_mem_chunk, quitf);
1856 gtk_quit_destructor (GtkObject **object_p)
1859 gtk_object_destroy (*object_p);
1866 gtk_quit_add_destroy (guint main_level,
1869 GtkObject **object_p;
1871 g_return_if_fail (main_level > 0);
1872 g_return_if_fail (GTK_IS_OBJECT (object));
1874 object_p = g_new (GtkObject*, 1);
1876 g_signal_connect (object,
1878 G_CALLBACK (gtk_widget_destroyed),
1880 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1884 gtk_quit_add (guint main_level,
1885 GtkFunction function,
1888 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1892 gtk_quit_remove (guint id)
1894 GtkQuitFunction *quitf;
1897 tmp_list = quit_functions;
1900 quitf = tmp_list->data;
1902 if (quitf->id == id)
1904 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1905 g_list_free (tmp_list);
1906 gtk_quit_destroy (quitf);
1911 tmp_list = tmp_list->next;
1916 gtk_quit_remove_by_data (gpointer data)
1918 GtkQuitFunction *quitf;
1921 tmp_list = quit_functions;
1924 quitf = tmp_list->data;
1926 if (quitf->data == data)
1928 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1929 g_list_free (tmp_list);
1930 gtk_quit_destroy (quitf);
1935 tmp_list = tmp_list->next;
1940 gtk_timeout_add_full (guint32 interval,
1941 GtkFunction function,
1942 GtkCallbackMarshal marshal,
1944 GtkDestroyNotify destroy)
1948 GtkClosure *closure;
1950 closure = g_new (GtkClosure, 1);
1951 closure->marshal = marshal;
1952 closure->data = data;
1953 closure->destroy = destroy;
1955 return g_timeout_add_full (0, interval,
1956 gtk_invoke_idle_timeout,
1958 gtk_destroy_closure);
1961 return g_timeout_add_full (0, interval, function, data, destroy);
1965 gtk_timeout_add (guint32 interval,
1966 GtkFunction function,
1969 return g_timeout_add_full (0, interval, function, data, NULL);
1973 gtk_timeout_remove (guint tag)
1975 g_source_remove (tag);
1979 gtk_idle_add_full (gint priority,
1980 GtkFunction function,
1981 GtkCallbackMarshal marshal,
1983 GtkDestroyNotify destroy)
1987 GtkClosure *closure;
1989 closure = g_new (GtkClosure, 1);
1990 closure->marshal = marshal;
1991 closure->data = data;
1992 closure->destroy = destroy;
1994 return g_idle_add_full (priority,
1995 gtk_invoke_idle_timeout,
1997 gtk_destroy_closure);
2000 return g_idle_add_full (priority, function, data, destroy);
2004 gtk_idle_add (GtkFunction function,
2007 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2011 gtk_idle_add_priority (gint priority,
2012 GtkFunction function,
2015 return g_idle_add_full (priority, function, data, NULL);
2019 gtk_idle_remove (guint tag)
2021 g_source_remove (tag);
2025 gtk_idle_remove_by_data (gpointer data)
2027 if (!g_idle_remove_by_data (data))
2028 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2032 gtk_input_add_full (gint source,
2033 GdkInputCondition condition,
2034 GdkInputFunction function,
2035 GtkCallbackMarshal marshal,
2037 GtkDestroyNotify destroy)
2041 GtkClosure *closure;
2043 closure = g_new (GtkClosure, 1);
2044 closure->marshal = marshal;
2045 closure->data = data;
2046 closure->destroy = destroy;
2048 return gdk_input_add_full (source,
2050 (GdkInputFunction) gtk_invoke_input,
2052 (GdkDestroyNotify) gtk_destroy_closure);
2055 return gdk_input_add_full (source, condition, function, data, destroy);
2059 gtk_input_remove (guint tag)
2061 g_source_remove (tag);
2065 gtk_destroy_closure (gpointer data)
2067 GtkClosure *closure = data;
2069 if (closure->destroy)
2070 (closure->destroy) (closure->data);
2075 gtk_invoke_idle_timeout (gpointer data)
2077 GtkClosure *closure = data;
2080 gint ret_val = FALSE;
2081 args[0].name = NULL;
2082 args[0].type = G_TYPE_BOOLEAN;
2083 args[0].d.pointer_data = &ret_val;
2084 closure->marshal (NULL, closure->data, 0, args);
2089 gtk_invoke_input (gpointer data,
2091 GdkInputCondition condition)
2093 GtkClosure *closure = data;
2096 args[0].type = G_TYPE_INT;
2097 args[0].name = NULL;
2098 GTK_VALUE_INT (args[0]) = source;
2099 args[1].type = GDK_TYPE_INPUT_CONDITION;
2100 args[1].name = NULL;
2101 GTK_VALUE_FLAGS (args[1]) = condition;
2102 args[2].type = G_TYPE_NONE;
2103 args[2].name = NULL;
2105 closure->marshal (NULL, closure->data, 2, args);
2109 * gtk_get_current_event:
2111 * Obtains a copy of the event currently being processed by GTK+. For
2112 * example, if you get a "clicked" signal from #GtkButton, the current
2113 * event will be the #GdkEventButton that triggered the "clicked"
2114 * signal. The returned event must be freed with gdk_event_free().
2115 * If there is no current event, the function returns %NULL.
2117 * Return value: a copy of the current event, or %NULL if no current event.
2120 gtk_get_current_event (void)
2123 return gdk_event_copy (current_events->data);
2129 * gtk_get_current_event_time:
2131 * If there is a current event and it has a timestamp, return that
2132 * timestamp, otherwise return %GDK_CURRENT_TIME.
2134 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2137 gtk_get_current_event_time (void)
2140 return gdk_event_get_time (current_events->data);
2142 return GDK_CURRENT_TIME;
2146 * gtk_get_current_event_state:
2147 * @state: a location to store the state of the current event
2149 * If there is a current event and it has a state field, place
2150 * that state field in @state and return %TRUE, otherwise return
2153 * Return value: %TRUE if there was a current event and it had a state field
2156 gtk_get_current_event_state (GdkModifierType *state)
2158 g_return_val_if_fail (state != NULL, FALSE);
2161 return gdk_event_get_state (current_events->data, state);
2170 * gtk_get_event_widget:
2171 * @event: a #GdkEvent
2173 * If @event is %NULL or the event was not associated with any widget,
2174 * returns %NULL, otherwise returns the widget that received the event
2177 * Return value: the widget that originally received @event, or %NULL
2180 gtk_get_event_widget (GdkEvent *event)
2185 if (event && event->any.window)
2186 gdk_window_get_user_data (event->any.window, (void**) &widget);
2192 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2194 if (!quitf->marshal)
2195 return quitf->function (quitf->data);
2199 gint ret_val = FALSE;
2201 args[0].name = NULL;
2202 args[0].type = G_TYPE_BOOLEAN;
2203 args[0].d.pointer_data = &ret_val;
2204 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2212 * gtk_propagate_event:
2213 * @widget: a #GtkWidget
2216 * Sends an event to a widget, propagating the event to parent widgets
2217 * if the event remains unhandled. Events received by GTK+ from GDK
2218 * normally begin in gtk_main_do_event(). Depending on the type of
2219 * event, existence of modal dialogs, grabs, etc., the event may be
2220 * propagated; if so, this function is used. gtk_propagate_event()
2221 * calls gtk_widget_event() on each widget it decides to send the
2222 * event to. So gtk_widget_event() is the lowest-level function; it
2223 * simply emits the "event" and possibly an event-specific signal on a
2224 * widget. gtk_propagate_event() is a bit higher-level, and
2225 * gtk_main_do_event() is the highest level.
2227 * All that said, you most likely don't want to use any of these
2228 * functions; synthesizing events is rarely needed. Consider asking on
2229 * the mailing list for better ways to achieve your goals. For
2230 * example, use gdk_window_invalidate_rect() or
2231 * gtk_widget_queue_draw() instead of making up expose events.
2235 gtk_propagate_event (GtkWidget *widget,
2240 g_return_if_fail (GTK_IS_WIDGET (widget));
2241 g_return_if_fail (event != NULL);
2243 handled_event = FALSE;
2245 g_object_ref (widget);
2247 if ((event->type == GDK_KEY_PRESS) ||
2248 (event->type == GDK_KEY_RELEASE))
2250 /* Only send key events within Window widgets to the Window
2251 * The Window widget will in turn pass the
2252 * key event on to the currently focused widget
2257 window = gtk_widget_get_toplevel (widget);
2258 if (window && GTK_IS_WINDOW (window))
2260 /* If there is a grab within the window, give the grab widget
2261 * a first crack at the key event
2263 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2264 handled_event = gtk_widget_event (widget, event);
2268 window = gtk_widget_get_toplevel (widget);
2269 if (window && GTK_IS_WINDOW (window))
2271 if (GTK_WIDGET_IS_SENSITIVE (window))
2272 gtk_widget_event (window, event);
2276 handled_event = TRUE; /* don't send to widget */
2280 /* Other events get propagated up the widget tree
2281 * so that parents can see the button and motion
2282 * events of the children.
2290 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2291 tmp = widget->parent;
2292 g_object_unref (widget);
2296 if (!handled_event && widget)
2297 g_object_ref (widget);
2303 g_object_unref (widget);
2308 gtk_error (gchar *str)
2314 gtk_warning (gchar *str)
2320 gtk_message (gchar *str)
2326 gtk_print (gchar *str)
2328 static GtkWidget *window = NULL;
2329 static GtkWidget *text;
2330 static int level = 0;
2334 GtkWidget *hscrollbar;
2335 GtkWidget *vscrollbar;
2336 GtkWidget *separator;
2341 fputs (str, stdout);
2348 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2350 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2351 (GtkSignalFunc) gtk_widget_destroyed,
2354 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2356 box1 = gtk_vbox_new (FALSE, 0);
2357 gtk_container_add (GTK_CONTAINER (window), box1);
2358 gtk_widget_show (box1);
2361 box2 = gtk_vbox_new (FALSE, 10);
2362 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2363 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2364 gtk_widget_show (box2);
2367 table = gtk_table_new (2, 2, FALSE);
2368 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2369 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2370 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2371 gtk_widget_show (table);
2373 text = gtk_text_new (NULL, NULL);
2374 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2375 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2376 gtk_widget_show (text);
2377 gtk_widget_realize (text);
2379 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2380 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2381 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2382 gtk_widget_show (hscrollbar);
2384 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2385 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2386 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2387 gtk_widget_show (vscrollbar);
2389 separator = gtk_hseparator_new ();
2390 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2391 gtk_widget_show (separator);
2394 box2 = gtk_vbox_new (FALSE, 10);
2395 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2396 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2397 gtk_widget_show (box2);
2400 button = gtk_button_new_with_label ("close");
2401 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2402 (GtkSignalFunc) gtk_widget_hide,
2403 GTK_OBJECT (window));
2404 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2405 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2406 gtk_widget_grab_default (button);
2407 gtk_widget_show (button);
2411 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2414 if (!GTK_WIDGET_VISIBLE (window))
2415 gtk_widget_show (window);
2420 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2421 GValue *return_accu,
2422 const GValue *handler_return,
2425 gboolean continue_emission;
2426 gboolean signal_handled;
2428 signal_handled = g_value_get_boolean (handler_return);
2429 g_value_set_boolean (return_accu, signal_handled);
2430 continue_emission = !signal_handled;
2432 return continue_emission;