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 "gtkwidget.h"
60 #include "gtkwindow.h"
61 #include "gtkprivate.h"
66 /* Private type definitions
68 typedef struct _GtkInitFunction GtkInitFunction;
69 typedef struct _GtkQuitFunction GtkQuitFunction;
70 typedef struct _GtkClosure GtkClosure;
71 typedef struct _GtkKeySnooperData GtkKeySnooperData;
72 typedef struct _GtkModuleInfo GtkModuleInfo;
74 struct _GtkInitFunction
80 struct _GtkQuitFunction
84 GtkCallbackMarshal marshal;
87 GtkDestroyNotify destroy;
92 GtkCallbackMarshal marshal;
94 GtkDestroyNotify destroy;
97 struct _GtkKeySnooperData
104 struct _GtkModuleInfo
106 GtkModuleInitFunc init_func;
107 GtkModuleDisplayInitFunc display_init_func;
110 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
111 static void gtk_quit_destroy (GtkQuitFunction *quitf);
112 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
115 static void gtk_destroy_closure (gpointer data);
116 static gboolean gtk_invoke_idle_timeout (gpointer data);
117 static void gtk_invoke_input (gpointer data,
119 GdkInputCondition condition);
122 static void gtk_error (gchar *str);
123 static void gtk_warning (gchar *str);
124 static void gtk_message (gchar *str);
125 static void gtk_print (gchar *str);
128 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
130 const guint gtk_major_version = GTK_MAJOR_VERSION;
131 const guint gtk_minor_version = GTK_MINOR_VERSION;
132 const guint gtk_micro_version = GTK_MICRO_VERSION;
133 const guint gtk_binary_age = GTK_BINARY_AGE;
134 const guint gtk_interface_age = GTK_INTERFACE_AGE;
136 static GSList *gtk_modules;
138 /* Saved argc,argv for delayed module initialization
140 static gint gtk_argc = 0;
141 static gchar **gtk_argv = NULL;
143 static guint gtk_main_loop_level = 0;
144 static gint gtk_initialized = FALSE;
145 static GList *current_events = NULL;
147 static GSList *main_loops = NULL; /* stack of currently executing main loops */
149 static GList *init_functions = NULL; /* A list of init functions.
151 static GList *quit_functions = NULL; /* A list of quit functions.
153 static GMemChunk *quit_mem_chunk = NULL;
155 static GSList *key_snoopers = NULL;
157 guint gtk_debug_flags = 0; /* Global GTK debug flag */
159 #ifdef G_ENABLE_DEBUG
160 static const GDebugKey gtk_debug_keys[] = {
161 {"misc", GTK_DEBUG_MISC},
162 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
163 {"text", GTK_DEBUG_TEXT},
164 {"tree", GTK_DEBUG_TREE},
165 {"updates", GTK_DEBUG_UPDATES},
166 {"keybindings", GTK_DEBUG_KEYBINDINGS},
167 {"multihead", GTK_DEBUG_MULTIHEAD}
170 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
172 #endif /* G_ENABLE_DEBUG */
175 gtk_check_version (guint required_major,
176 guint required_minor,
177 guint required_micro)
179 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
180 gint required_effective_micro = 100 * required_minor + required_micro;
182 if (required_major > GTK_MAJOR_VERSION)
183 return "Gtk+ version too old (major mismatch)";
184 if (required_major < GTK_MAJOR_VERSION)
185 return "Gtk+ version too new (major mismatch)";
186 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
187 return "Gtk+ version too new (micro mismatch)";
188 if (required_effective_micro > gtk_effective_micro)
189 return "Gtk+ version too old (micro mismatch)";
193 /* This checks to see if the process is running suid or sgid
194 * at the current time. If so, we don't allow GTK+ to be initialized.
195 * This is meant to be a mild check - we only error out if we
196 * can prove the programmer is doing something wrong, not if
197 * they could be doing something wrong. For this reason, we
198 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
203 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
205 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
206 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
208 #ifdef HAVE_GETRESUID
209 /* These aren't in the header files, so we prototype them here.
211 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
212 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
214 if (getresuid (&ruid, &euid, &suid) != 0 ||
215 getresgid (&rgid, &egid, &sgid) != 0)
216 #endif /* HAVE_GETRESUID */
218 suid = ruid = getuid ();
219 sgid = rgid = getgid ();
224 if (ruid != euid || ruid != suid ||
225 rgid != egid || rgid != sgid)
227 g_warning ("This process is currently running setuid or setgid.\n"
228 "This is not a supported use of GTK+. You must create a helper\n"
229 "program instead. For further details, see:\n\n"
230 " http://www.gtk.org/setuid.html\n\n"
231 "Refusing to initialize GTK+.");
240 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
243 _gtk_get_libdir (void)
245 static char *gtk_libdir = NULL;
246 if (gtk_libdir == NULL)
247 gtk_libdir = g_win32_get_package_installation_subdirectory
248 (GETTEXT_PACKAGE, dll_name, "lib");
254 _gtk_get_localedir (void)
256 static char *gtk_localedir = NULL;
257 if (gtk_localedir == NULL)
258 gtk_localedir = g_win32_get_package_installation_subdirectory
259 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
261 return gtk_localedir;
265 _gtk_get_sysconfdir (void)
267 static char *gtk_sysconfdir = NULL;
268 if (gtk_sysconfdir == NULL)
269 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
270 (GETTEXT_PACKAGE, dll_name, "etc");
272 return gtk_sysconfdir;
276 _gtk_get_data_prefix (void)
278 static char *gtk_data_prefix = NULL;
279 if (gtk_data_prefix == NULL)
280 gtk_data_prefix = g_win32_get_package_installation_directory
281 (GETTEXT_PACKAGE, dll_name);
283 return gtk_data_prefix;
286 #endif /* G_OS_WIN32 */
289 get_module_path (void)
291 const gchar *module_path_env;
292 const gchar *exe_prefix;
293 const gchar *home_dir;
294 gchar *home_gtk_dir = NULL;
297 static gchar **result = NULL;
302 home_dir = g_get_home_dir();
304 home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);
306 module_path_env = g_getenv ("GTK_PATH");
307 exe_prefix = g_getenv ("GTK_EXE_PREFIX");
310 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
312 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);
314 if (module_path_env && home_gtk_dir)
315 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
316 module_path_env, home_gtk_dir, default_dir, NULL);
317 else if (module_path_env)
318 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
319 module_path_env, default_dir, NULL);
320 else if (home_gtk_dir)
321 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
322 home_gtk_dir, default_dir, NULL);
324 module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
327 g_free (home_gtk_dir);
328 g_free (default_dir);
330 result = pango_split_file_list (module_path);
331 g_free (module_path);
337 * _gtk_get_module_path:
338 * @type: the type of the module, for instance 'modules', 'engines', immodules'
340 * Determines the search path for a particular type of module.
342 * Return value: the search path for the module type. Free with g_strfreev().
345 _gtk_get_module_path (const gchar *type)
347 gchar **paths = get_module_path();
352 for (path = paths; *path; path++)
355 result = g_new (gchar *, count * 4 + 1);
358 for (path = get_module_path (); *path; path++)
360 gint use_version, use_host;
362 for (use_version = TRUE; use_version >= FALSE; use_version--)
363 for (use_host = TRUE; use_host >= FALSE; use_host--)
367 if (use_version && use_host)
368 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
369 else if (use_version)
370 tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
372 tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
374 tmp_dir = g_build_filename (*path, type, NULL);
376 result[count++] = tmp_dir;
380 result[count++] = NULL;
385 /* Like g_module_path, but use .la as the suffix
388 module_build_la_path (const gchar *directory,
389 const gchar *module_name)
394 if (strncmp (module_name, "lib", 3) == 0)
395 filename = (gchar *)module_name;
397 filename = g_strconcat ("lib", module_name, ".la", NULL);
399 if (directory && *directory)
400 result = g_build_filename (directory, filename, NULL);
402 result = g_strdup (filename);
404 if (filename != module_name)
412 * @name: the name of the module
413 * @type: the type of the module, for instance 'modules', 'engines', immodules'
415 * Looks for a dynamically module named @name of type @type in the standard GTK+
416 * module search path.
418 * Return value: the pathname to the found module, or %NULL if it wasn't found.
419 * Free with g_free().
422 _gtk_find_module (const gchar *name,
427 gchar *module_name = NULL;
429 if (g_path_is_absolute (name))
430 return g_strdup (name);
432 paths = _gtk_get_module_path (type);
433 for (path = paths; *path; path++)
437 tmp_name = g_module_build_path (*path, name);
438 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
440 module_name = tmp_name;
445 tmp_name = module_build_la_path (*path, name);
446 if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
448 module_name = tmp_name;
460 find_module (const gchar *name)
465 module_name = _gtk_find_module (name, "modules");
468 /* As last resort, try loading without an absolute path (using system
471 module_name = g_module_build_path (NULL, name);
474 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
481 load_module (GSList *module_list,
484 GtkModuleInitFunc modinit_func = NULL;
486 GModule *module = NULL;
488 if (g_module_supported ())
490 module = find_module (name);
492 g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
495 if (!g_slist_find (module_list, (gconstpointer) modinit_func))
497 g_module_make_resident (module);
498 info = g_new (GtkModuleInfo, 1);
500 info->init_func = modinit_func;
501 g_module_symbol (module, "gtk_module_display_init",
502 (gpointer *) &info->display_init_func);
504 module_list = g_slist_prepend (module_list, info);
508 g_module_close (module);
515 g_message ("Failed to load module \"%s\": %s",
516 module ? g_module_name (module) : name,
519 g_module_close (module);
526 load_modules (const char *module_str)
528 gchar **module_names = pango_split_file_list (module_str);
529 GSList *module_list = NULL;
532 for (i = 0; module_names[i]; i++)
533 module_list = load_module (module_list, module_names[i]);
535 module_list = g_slist_reverse (module_list);
537 g_strfreev (module_names);
542 static gboolean do_setlocale = TRUE;
545 * gtk_disable_setlocale:
547 * Prevents gtk_init() and gtk_init_check() from automatically
548 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
549 * want to use this function if you wanted to set the locale for
550 * your program to something other than the user's locale, or if
551 * you wanted to set different values for different locale categories.
553 * Most programs should not need to call this function.
556 gtk_disable_setlocale (void)
559 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
561 do_setlocale = FALSE;
564 #undef gtk_init_check
567 default_display_notify_cb (GdkDisplayManager *display_manager)
571 /* Initialize non-multihead-aware modules when the
572 * default display is first set to a non-NULL value.
574 static gboolean initialized = FALSE;
576 if (!gdk_display_get_default () || initialized)
581 for (slist = gtk_modules; slist; slist = slist->next)
585 GtkModuleInfo *info = slist->data;
587 if (!info->display_init_func)
588 info->init_func (>k_argc, >k_argv);
594 display_opened_cb (GdkDisplayManager *display_manager,
599 for (slist = gtk_modules; slist; slist = slist->next)
603 GtkModuleInfo *info = slist->data;
605 if (info->display_init_func)
606 info->display_init_func (display);
613 * @argc: the number of command line arguments.
614 * @argv: the array of command line arguments.
616 * Parses command line arguments, and initializes global
617 * attributes of GTK+, but does not actually open a connection
618 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
620 * Any arguments used by GTK or GDK are removed from the array and
621 * @argc and @argv are updated accordingly.
623 * You shouldn't call this function explicitely if you are using
624 * gtk_init(), or gtk_init_check().
626 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
629 gtk_parse_args (int *argc,
632 GString *gtk_modules_string = NULL;
634 GdkDisplayManager *display_manager;
635 const gchar *env_string;
640 if (!check_setugid ())
644 g_set_error_handler (gtk_error);
645 g_set_warning_handler (gtk_warning);
646 g_set_message_handler (gtk_message);
647 g_set_print_handler (gtk_print);
652 if (!setlocale (LC_ALL, ""))
653 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
656 gdk_parse_args (argc, argv);
657 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
659 #ifdef G_ENABLE_DEBUG
660 env_string = g_getenv ("GTK_DEBUG");
661 if (env_string != NULL)
663 gtk_debug_flags = g_parse_debug_string (env_string,
668 #endif /* G_ENABLE_DEBUG */
670 env_string = g_getenv ("GTK_MODULES");
672 gtk_modules_string = g_string_new (env_string);
678 for (i = 1; i < *argc;)
680 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
681 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
683 gchar *module_name = (*argv)[i] + 12;
685 if (*module_name == '=')
687 else if (i + 1 < *argc)
691 module_name = (*argv)[i];
695 if (module_name && *module_name)
697 if (gtk_modules_string)
698 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
700 gtk_modules_string = g_string_new (NULL);
702 g_string_append (gtk_modules_string, module_name);
705 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
707 GLogLevelFlags fatal_mask;
709 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
710 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
711 g_log_set_always_fatal (fatal_mask);
714 #ifdef G_ENABLE_DEBUG
715 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
716 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
718 gchar *equal_pos = strchr ((*argv)[i], '=');
720 if (equal_pos != NULL)
722 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
726 else if ((i + 1) < *argc && (*argv)[i + 1])
728 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
736 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
737 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
739 gchar *equal_pos = strchr ((*argv)[i], '=');
741 if (equal_pos != NULL)
743 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
747 else if ((i + 1) < *argc && (*argv)[i + 1])
749 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
757 #endif /* G_ENABLE_DEBUG */
761 for (i = 1; i < *argc; i++)
763 for (k = i; k < *argc; k++)
764 if ((*argv)[k] != NULL)
770 for (j = i + k; j < *argc; j++)
771 (*argv)[j-k] = (*argv)[j];
776 gtk_argv = g_malloc ((gtk_argc + 1) * sizeof (char*));
777 for (i = 0; i < gtk_argc; i++)
778 gtk_argv[i] = g_strdup ((*argv)[i]);
779 gtk_argv[gtk_argc] = NULL;
782 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
783 gdk_window_set_debug_updates (TRUE);
785 /* load gtk modules */
786 if (gtk_modules_string)
788 gtk_modules = load_modules (gtk_modules_string->str);
789 g_string_free (gtk_modules_string, TRUE);
793 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
794 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
795 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
800 /* Translate to default:RTL if you want your widgets
801 * to be RTL, otherwise translate to default:LTR.
802 * Do *not* translate it to "predefinito:LTR", if it
803 * it isn't default:LTR or default:RTL it will not work
805 char *e = _("default:LTR");
806 if (strcmp (e, "default:RTL")==0) {
807 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
808 } else if (strcmp (e, "default:LTR")) {
809 g_warning ("Whoever translated default:LTR did so wrongly.\n");
814 _gtk_accel_map_init ();
817 /* Set the 'initialized' flag.
819 gtk_initialized = TRUE;
821 display_manager = gdk_display_manager_get ();
822 g_signal_connect (display_manager, "notify::default-display",
823 G_CALLBACK (default_display_notify_cb), NULL);
824 g_signal_connect (display_manager, "display-opened",
825 G_CALLBACK (display_opened_cb), NULL);
827 /* initialize multhead aware gtk modules; for other modules,
828 * we wait until we have a display open;
830 for (slist = gtk_modules; slist; slist = slist->next)
834 GtkModuleInfo *info = slist->data;
836 if (info->display_init_func)
837 info->init_func (argc, argv);
844 #undef gtk_init_check
848 * @argc: Address of the <parameter>argc</parameter> parameter of your
849 * <function>main()</function> function. Changed if any arguments were
851 * @argv: Address of the <parameter>argv</parameter> parameter of
852 * <function>main()</function>. Any parameters understood by gtk_init()
853 * are stripped before return.
855 * This function does the same work as gtk_init() with only
856 * a single change: It does not terminate the program if the GUI can't be
857 * initialized. Instead it returns %FALSE on failure.
859 * This way the application can fall back to some other means of communication
860 * with the user - for example a curses or command line interface.
862 * Return value: %TRUE if the GUI has been successfully initialized,
866 gtk_init_check (int *argc,
869 if (!gtk_parse_args (argc, argv))
872 return gdk_display_open_default_libgtk_only () != NULL;
879 * @argc: Address of the <parameter>argc</parameter> parameter of your
880 * <function>main()</function> function. Changed if any arguments were
882 * @argv: Address of the <parameter>argv</parameter> parameter of
883 * <function>main()</function>. Any parameters understood by gtk_init()
884 * are stripped before return.
886 * Call this function before using any other GTK+ functions in your GUI
887 * applications. It will initialize everything needed to operate the toolkit and
888 * parses some standard command line options. @argc and
889 * @argv are adjusted accordingly so your own code will
890 * never see those standard arguments.
893 * This function will terminate your program if it was unable to initialize
894 * the GUI for some reason. If you want your program to fall back to a
895 * textual interface you want to call gtk_init_check() instead.
899 gtk_init (int *argc, char ***argv)
901 if (!gtk_init_check (argc, argv))
903 const char *display_name_arg = gdk_get_display_arg_name ();
904 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
909 #ifdef G_PLATFORM_WIN32
912 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
914 if (sizeof_GtkWindow != sizeof (GtkWindow))
915 g_error ("Incompatible build!\n"
916 "The code using GTK+ thinks GtkWindow is of different\n"
917 "size than it actually is in this build of GTK+.\n"
918 "On Windows, this probably means that you have compiled\n"
919 "your code with gcc without the -fnative-struct\n"
920 "(or -mms-bitfields) switch, or that you are using\n"
921 "an unsupported compiler.");
924 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
925 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
926 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
927 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
928 * -fnative-struct still matters. GtkBox is one such.
931 check_sizeof_GtkBox (size_t sizeof_GtkBox)
933 if (sizeof_GtkBox != sizeof (GtkBox))
934 g_error ("Incompatible build!\n"
935 "The code using GTK+ thinks GtkBox is of different\n"
936 "size than it actually is in this build of GTK+.\n"
937 "On Windows, this probably means that you have compiled\n"
938 "your code with gcc without the -fnative-struct\n"
939 "(or -mms-bitfields) switch, or that you are using\n"
940 "an unsupported compiler.");
943 /* These two functions might get more checks added later, thus pass
944 * in the number of extra args.
947 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
949 check_sizeof_GtkWindow (sizeof_GtkWindow);
951 check_sizeof_GtkBox (sizeof_GtkBox);
952 gtk_init (argc, argv);
956 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
958 check_sizeof_GtkWindow (sizeof_GtkWindow);
960 check_sizeof_GtkBox (sizeof_GtkBox);
961 return gtk_init_check (argc, argv);
967 gtk_exit (gint errorcode)
976 * Initializes internationalization support for GTK+. gtk_init()
977 * automatically does this, so there is typically no point
978 * in calling this function.
980 * If you are calling this function because you changed the locale
981 * after GTK+ is was initialized, then calling this function
982 * may help a bit. (Note, however, that changing the locale
983 * after GTK+ is initialized may produce inconsistent results and
984 * is not really supported.)
986 * In detail - sets the current locale according to the
987 * program environment. This is the same as calling the C library function
988 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
989 * locale specific setup of the windowing system used by GDK.
991 * Return value: a string corresponding to the locale set, as with the
992 * C library function <function>setlocale()</function>.
995 gtk_set_locale (void)
997 return gdk_set_locale ();
1001 * gtk_get_default_language:
1003 * Returns the ISO language code for the default language currently in
1004 * effect. (Note that this can change over the life of an
1005 * application.) The default language is derived from the current
1006 * locale. It determines, for example, whether GTK+ uses the
1007 * right-to-left or left-to-right text direction.
1009 * Return value: the default language as an allocated string, must be freed
1012 gtk_get_default_language (void)
1015 PangoLanguage *result;
1019 /* Somebody might try to set the locale for this process using the
1020 * LANG or LC_ environment variables. The Microsoft C library
1021 * doesn't know anything about them. You set the locale in the
1022 * Control Panel. Setting these env vars won't have any affect on
1023 * locale-dependent C library functions like ctime. But just for
1024 * kicks, do obey LC_ALL, LANG and LC_CTYPE in GTK. (This also makes
1025 * it easier to test GTK and Pango in various default languages, you
1026 * don't have to clickety-click in the Control Panel, you can simply
1027 * start the program with LC_ALL=something on the command line.)
1029 p = getenv ("LC_ALL");
1031 lang = g_strdup (p);
1034 p = getenv ("LANG");
1036 lang = g_strdup (p);
1039 p = getenv ("LC_CTYPE");
1041 lang = g_strdup (p);
1043 lang = g_win32_getlocale ();
1047 lang = g_strdup (setlocale (LC_CTYPE, NULL));
1049 p = strchr (lang, '.');
1052 p = strchr (lang, '@');
1056 result = pango_language_from_string (lang);
1067 GtkInitFunction *init;
1070 gtk_main_loop_level++;
1072 loop = g_main_loop_new (NULL, TRUE);
1073 main_loops = g_slist_prepend (main_loops, loop);
1075 tmp_list = functions = init_functions;
1076 init_functions = NULL;
1080 init = tmp_list->data;
1081 tmp_list = tmp_list->next;
1083 (* init->function) (init->data);
1086 g_list_free (functions);
1088 if (g_main_loop_is_running (main_loops->data))
1090 GDK_THREADS_LEAVE ();
1091 g_main_loop_run (loop);
1092 GDK_THREADS_ENTER ();
1098 GList *reinvoke_list = NULL;
1099 GtkQuitFunction *quitf;
1101 while (quit_functions)
1103 quitf = quit_functions->data;
1105 tmp_list = quit_functions;
1106 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1107 g_list_free_1 (tmp_list);
1109 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1110 gtk_quit_invoke_function (quitf))
1112 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1116 gtk_quit_destroy (quitf);
1123 work = g_list_last (reinvoke_list);
1125 quit_functions->prev = work;
1126 work->next = quit_functions;
1127 quit_functions = work;
1133 main_loops = g_slist_remove (main_loops, loop);
1135 g_main_loop_unref (loop);
1137 gtk_main_loop_level--;
1141 gtk_main_level (void)
1143 return gtk_main_loop_level;
1147 gtk_main_quit (void)
1149 g_return_if_fail (main_loops != NULL);
1151 g_main_loop_quit (main_loops->data);
1155 gtk_events_pending (void)
1159 GDK_THREADS_LEAVE ();
1160 result = g_main_context_pending (NULL);
1161 GDK_THREADS_ENTER ();
1167 gtk_main_iteration (void)
1169 GDK_THREADS_LEAVE ();
1170 g_main_context_iteration (NULL, TRUE);
1171 GDK_THREADS_ENTER ();
1174 return !g_main_loop_is_running (main_loops->data);
1180 gtk_main_iteration_do (gboolean blocking)
1182 GDK_THREADS_LEAVE ();
1183 g_main_context_iteration (NULL, blocking);
1184 GDK_THREADS_ENTER ();
1187 return !g_main_loop_is_running (main_loops->data);
1192 /* private libgtk to libgdk interfaces
1194 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1195 GdkWindow **grab_window,
1196 gboolean *owner_events);
1197 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1198 GdkWindow **grab_window,
1199 gboolean *owner_events);
1202 rewrite_events_translate (GdkWindow *old_window,
1203 GdkWindow *new_window,
1207 gint old_origin_x, old_origin_y;
1208 gint new_origin_x, new_origin_y;
1210 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1211 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1213 *x += old_origin_x - new_origin_x;
1214 *y += old_origin_y - new_origin_y;
1218 rewrite_event_for_window (GdkEvent *event,
1219 GdkWindow *new_window)
1221 event = gdk_event_copy (event);
1223 switch (event->type)
1226 rewrite_events_translate (event->any.window,
1228 &event->scroll.x, &event->scroll.y);
1230 case GDK_BUTTON_PRESS:
1231 case GDK_2BUTTON_PRESS:
1232 case GDK_3BUTTON_PRESS:
1233 case GDK_BUTTON_RELEASE:
1234 rewrite_events_translate (event->any.window,
1236 &event->button.x, &event->button.y);
1238 case GDK_MOTION_NOTIFY:
1239 rewrite_events_translate (event->any.window,
1241 &event->motion.x, &event->motion.y);
1244 case GDK_KEY_RELEASE:
1245 case GDK_PROXIMITY_IN:
1246 case GDK_PROXIMITY_OUT:
1253 g_object_unref (event->any.window);
1254 event->any.window = g_object_ref (new_window);
1259 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1260 * then what X11 does is deliver the event normally if it was going to this
1261 * client, otherwise, delivers it in terms of the grab window. This function
1262 * rewrites events to the effect that events going to the same window group
1263 * are delivered normally, otherwise, the event is delivered in terms of the
1267 rewrite_event_for_grabs (GdkEvent *event)
1269 GdkWindow *grab_window;
1270 GtkWidget *event_widget, *grab_widget;
1271 gboolean owner_events;
1272 GdkDisplay *display;
1274 switch (event->type)
1277 case GDK_BUTTON_PRESS:
1278 case GDK_2BUTTON_PRESS:
1279 case GDK_3BUTTON_PRESS:
1280 case GDK_BUTTON_RELEASE:
1281 case GDK_MOTION_NOTIFY:
1282 case GDK_PROXIMITY_IN:
1283 case GDK_PROXIMITY_OUT:
1284 display = gdk_drawable_get_display (event->proximity.window);
1285 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1291 case GDK_KEY_RELEASE:
1292 display = gdk_drawable_get_display (event->key.window);
1293 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1302 event_widget = gtk_get_event_widget (event);
1303 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1306 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1307 return rewrite_event_for_window (event, grab_window);
1313 gtk_main_do_event (GdkEvent *event)
1315 GtkWidget *event_widget;
1316 GtkWidget *grab_widget;
1317 GtkWindowGroup *window_group;
1318 GdkEvent *next_event;
1319 GdkEvent *rewritten_event = NULL;
1322 /* If there are any events pending then get the next one.
1324 next_event = gdk_event_peek ();
1326 /* Try to compress enter/leave notify events. These event
1327 * pairs occur when the mouse is dragged quickly across
1328 * a window with many buttons (or through a menu). Instead
1329 * of highlighting and de-highlighting each widget that
1330 * is crossed it is better to simply de-highlight the widget
1331 * which contained the mouse initially and highlight the
1332 * widget which ends up containing the mouse.
1335 if (((event->type == GDK_ENTER_NOTIFY) ||
1336 (event->type == GDK_LEAVE_NOTIFY)) &&
1337 ((next_event->type == GDK_ENTER_NOTIFY) ||
1338 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1339 (next_event->type != event->type) &&
1340 (next_event->any.window == event->any.window))
1342 /* Throw both the peeked copy and the queued copy away
1344 gdk_event_free (next_event);
1345 next_event = gdk_event_get ();
1346 gdk_event_free (next_event);
1352 gdk_event_free (next_event);
1354 /* Find the widget which got the event. We store the widget
1355 * in the user_data field of GdkWindow's.
1356 * Ignore the event if we don't have a widget for it, except
1357 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1358 * Though this happens rarely, bogus events can occour
1359 * for e.g. destroyed GdkWindows.
1361 event_widget = gtk_get_event_widget (event);
1364 /* To handle selection INCR transactions, we select
1365 * PropertyNotify events on the requestor window and create
1366 * a corresponding (fake) GdkWindow so that events get
1367 * here. There won't be a widget though, so we have to handle
1370 if (event->type == GDK_PROPERTY_NOTIFY)
1371 _gtk_selection_incr_event (event->any.window,
1373 else if (event->type == GDK_SETTING)
1374 _gtk_settings_handle_event (&event->setting);
1379 /* If pointer or keyboard grabs are in effect, munge the events
1380 * so that each window group looks like a separate app.
1382 rewritten_event = rewrite_event_for_grabs (event);
1383 if (rewritten_event)
1385 event = rewritten_event;
1386 event_widget = gtk_get_event_widget (event);
1389 window_group = gtk_main_get_window_group (event_widget);
1391 /* Push the event onto a stack of current events for
1392 * gtk_current_event_get().
1394 current_events = g_list_prepend (current_events, event);
1396 /* If there is a grab in effect...
1398 if (window_group->grabs)
1400 grab_widget = window_group->grabs->data;
1402 /* If the grab widget is an ancestor of the event widget
1403 * then we send the event to the original event widget.
1404 * This is the key to implementing modality.
1406 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1407 gtk_widget_is_ancestor (event_widget, grab_widget))
1408 grab_widget = event_widget;
1412 grab_widget = event_widget;
1415 /* Not all events get sent to the grabbing widget.
1416 * The delete, destroy, expose, focus change and resize
1417 * events still get sent to the event widget because
1418 * 1) these events have no meaning for the grabbing widget
1419 * and 2) redirecting these events to the grabbing widget
1420 * could cause the display to be messed up.
1422 * Drag events are also not redirected, since it isn't
1423 * clear what the semantics of that would be.
1425 switch (event->type)
1431 g_object_ref (event_widget);
1432 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1433 !gtk_widget_event (event_widget, event))
1434 gtk_widget_destroy (event_widget);
1435 g_object_unref (event_widget);
1439 /* Unexpected GDK_DESTROY from the outside, ignore for
1440 * child windows, handle like a GDK_DELETE for toplevels
1442 if (!event_widget->parent)
1444 g_object_ref (event_widget);
1445 if (!gtk_widget_event (event_widget, event) &&
1446 GTK_WIDGET_REALIZED (event_widget))
1447 gtk_widget_destroy (event_widget);
1448 g_object_unref (event_widget);
1453 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1455 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1456 gtk_widget_send_expose (event_widget, event);
1457 gdk_window_end_paint (event->any.window);
1460 gtk_widget_send_expose (event_widget, event);
1463 case GDK_PROPERTY_NOTIFY:
1465 case GDK_FOCUS_CHANGE:
1469 case GDK_SELECTION_CLEAR:
1470 case GDK_SELECTION_REQUEST:
1471 case GDK_SELECTION_NOTIFY:
1472 case GDK_CLIENT_EVENT:
1473 case GDK_VISIBILITY_NOTIFY:
1474 case GDK_WINDOW_STATE:
1475 gtk_widget_event (event_widget, event);
1479 case GDK_BUTTON_PRESS:
1480 case GDK_2BUTTON_PRESS:
1481 case GDK_3BUTTON_PRESS:
1482 gtk_propagate_event (grab_widget, event);
1486 case GDK_KEY_RELEASE:
1489 if (gtk_invoke_key_snoopers (grab_widget, event))
1492 /* else fall through */
1493 case GDK_MOTION_NOTIFY:
1494 case GDK_BUTTON_RELEASE:
1495 case GDK_PROXIMITY_IN:
1496 case GDK_PROXIMITY_OUT:
1497 gtk_propagate_event (grab_widget, event);
1500 case GDK_ENTER_NOTIFY:
1501 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1503 g_object_ref (event_widget);
1505 gtk_widget_event (grab_widget, event);
1506 if (event_widget == grab_widget)
1507 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1509 g_object_unref (event_widget);
1513 case GDK_LEAVE_NOTIFY:
1514 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1516 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1517 gtk_widget_event (event_widget, event);
1519 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1520 gtk_widget_event (grab_widget, event);
1523 case GDK_DRAG_STATUS:
1524 case GDK_DROP_FINISHED:
1525 _gtk_drag_source_handle_event (event_widget, event);
1527 case GDK_DRAG_ENTER:
1528 case GDK_DRAG_LEAVE:
1529 case GDK_DRAG_MOTION:
1530 case GDK_DROP_START:
1531 _gtk_drag_dest_handle_event (event_widget, event);
1534 g_assert_not_reached ();
1538 tmp_list = current_events;
1539 current_events = g_list_remove_link (current_events, tmp_list);
1540 g_list_free_1 (tmp_list);
1542 if (rewritten_event)
1543 gdk_event_free (rewritten_event);
1558 static GtkWindowGroup *
1559 gtk_main_get_window_group (GtkWidget *widget)
1561 GtkWidget *toplevel = NULL;
1564 toplevel = gtk_widget_get_toplevel (widget);
1566 if (toplevel && GTK_IS_WINDOW (toplevel))
1567 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1569 return _gtk_window_get_group (NULL);
1574 GtkWidget *old_grab_widget;
1575 GtkWidget *new_grab_widget;
1579 check_is_grabbed (GtkWidget *widget,
1580 GtkWidget *grab_widget)
1583 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1589 gtk_grab_notify_foreach (GtkWidget *child,
1593 GrabNotifyInfo *info = data;
1594 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1595 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1597 if (was_grabbed != is_grabbed)
1599 g_object_ref (child);
1601 g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1603 if (GTK_IS_CONTAINER (child))
1604 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1606 g_object_unref (child);
1611 gtk_grab_notify (GtkWindowGroup *group,
1612 GtkWidget *grab_widget,
1613 gboolean was_grabbed)
1616 GrabNotifyInfo info;
1620 info.old_grab_widget = grab_widget;
1621 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1625 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1626 info.new_grab_widget = grab_widget;
1629 g_object_ref (group);
1630 g_object_ref (grab_widget);
1632 toplevels = gtk_window_list_toplevels ();
1633 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1637 GtkWindow *toplevel = toplevels->data;
1638 toplevels = g_list_delete_link (toplevels, toplevels);
1640 if (group == _gtk_window_get_group (toplevel))
1641 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1642 g_object_unref (toplevel);
1645 g_object_unref (group);
1646 g_object_unref (grab_widget);
1650 gtk_grab_add (GtkWidget *widget)
1652 GtkWindowGroup *group;
1653 gboolean was_grabbed;
1655 g_return_if_fail (widget != NULL);
1657 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1659 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1661 group = gtk_main_get_window_group (widget);
1663 was_grabbed = (group->grabs != NULL);
1665 g_object_ref (widget);
1666 group->grabs = g_slist_prepend (group->grabs, widget);
1668 gtk_grab_notify (group, widget, FALSE);
1673 gtk_grab_get_current (void)
1675 GtkWindowGroup *group;
1677 group = gtk_main_get_window_group (NULL);
1680 return GTK_WIDGET (group->grabs->data);
1685 gtk_grab_remove (GtkWidget *widget)
1687 GtkWindowGroup *group;
1689 g_return_if_fail (widget != NULL);
1691 if (GTK_WIDGET_HAS_GRAB (widget))
1693 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1695 group = gtk_main_get_window_group (widget);
1696 group->grabs = g_slist_remove (group->grabs, widget);
1698 g_object_unref (widget);
1700 gtk_grab_notify (group, widget, TRUE);
1705 gtk_init_add (GtkFunction function,
1708 GtkInitFunction *init;
1710 init = g_new (GtkInitFunction, 1);
1711 init->function = function;
1714 init_functions = g_list_prepend (init_functions, init);
1718 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1721 GtkKeySnooperData *data;
1722 static guint snooper_id = 1;
1724 g_return_val_if_fail (snooper != NULL, 0);
1726 data = g_new (GtkKeySnooperData, 1);
1727 data->func = snooper;
1728 data->func_data = func_data;
1729 data->id = snooper_id++;
1730 key_snoopers = g_slist_prepend (key_snoopers, data);
1736 gtk_key_snooper_remove (guint snooper_id)
1738 GtkKeySnooperData *data = NULL;
1741 slist = key_snoopers;
1745 if (data->id == snooper_id)
1748 slist = slist->next;
1752 key_snoopers = g_slist_remove (key_snoopers, data);
1756 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1760 gint return_val = FALSE;
1762 slist = key_snoopers;
1763 while (slist && !return_val)
1765 GtkKeySnooperData *data;
1768 slist = slist->next;
1769 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1776 gtk_quit_add_full (guint main_level,
1777 GtkFunction function,
1778 GtkCallbackMarshal marshal,
1780 GtkDestroyNotify destroy)
1782 static guint quit_id = 1;
1783 GtkQuitFunction *quitf;
1785 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1787 if (!quit_mem_chunk)
1788 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1789 512, G_ALLOC_AND_FREE);
1791 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1793 quitf->id = quit_id++;
1794 quitf->main_level = main_level;
1795 quitf->function = function;
1796 quitf->marshal = marshal;
1798 quitf->destroy = destroy;
1800 quit_functions = g_list_prepend (quit_functions, quitf);
1806 gtk_quit_destroy (GtkQuitFunction *quitf)
1809 quitf->destroy (quitf->data);
1810 g_mem_chunk_free (quit_mem_chunk, quitf);
1814 gtk_quit_destructor (GtkObject **object_p)
1817 gtk_object_destroy (*object_p);
1824 gtk_quit_add_destroy (guint main_level,
1827 GtkObject **object_p;
1829 g_return_if_fail (main_level > 0);
1830 g_return_if_fail (GTK_IS_OBJECT (object));
1832 object_p = g_new (GtkObject*, 1);
1834 g_signal_connect (object,
1836 G_CALLBACK (gtk_widget_destroyed),
1838 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1842 gtk_quit_add (guint main_level,
1843 GtkFunction function,
1846 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1850 gtk_quit_remove (guint id)
1852 GtkQuitFunction *quitf;
1855 tmp_list = quit_functions;
1858 quitf = tmp_list->data;
1860 if (quitf->id == id)
1862 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1863 g_list_free (tmp_list);
1864 gtk_quit_destroy (quitf);
1869 tmp_list = tmp_list->next;
1874 gtk_quit_remove_by_data (gpointer data)
1876 GtkQuitFunction *quitf;
1879 tmp_list = quit_functions;
1882 quitf = tmp_list->data;
1884 if (quitf->data == data)
1886 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1887 g_list_free (tmp_list);
1888 gtk_quit_destroy (quitf);
1893 tmp_list = tmp_list->next;
1898 gtk_timeout_add_full (guint32 interval,
1899 GtkFunction function,
1900 GtkCallbackMarshal marshal,
1902 GtkDestroyNotify destroy)
1906 GtkClosure *closure;
1908 closure = g_new (GtkClosure, 1);
1909 closure->marshal = marshal;
1910 closure->data = data;
1911 closure->destroy = destroy;
1913 return g_timeout_add_full (0, interval,
1914 gtk_invoke_idle_timeout,
1916 gtk_destroy_closure);
1919 return g_timeout_add_full (0, interval, function, data, destroy);
1923 gtk_timeout_add (guint32 interval,
1924 GtkFunction function,
1927 return g_timeout_add_full (0, interval, function, data, NULL);
1931 gtk_timeout_remove (guint tag)
1933 g_source_remove (tag);
1937 gtk_idle_add_full (gint priority,
1938 GtkFunction function,
1939 GtkCallbackMarshal marshal,
1941 GtkDestroyNotify destroy)
1945 GtkClosure *closure;
1947 closure = g_new (GtkClosure, 1);
1948 closure->marshal = marshal;
1949 closure->data = data;
1950 closure->destroy = destroy;
1952 return g_idle_add_full (priority,
1953 gtk_invoke_idle_timeout,
1955 gtk_destroy_closure);
1958 return g_idle_add_full (priority, function, data, destroy);
1962 gtk_idle_add (GtkFunction function,
1965 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1969 gtk_idle_add_priority (gint priority,
1970 GtkFunction function,
1973 return g_idle_add_full (priority, function, data, NULL);
1977 gtk_idle_remove (guint tag)
1979 g_source_remove (tag);
1983 gtk_idle_remove_by_data (gpointer data)
1985 if (!g_idle_remove_by_data (data))
1986 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1990 gtk_input_add_full (gint source,
1991 GdkInputCondition condition,
1992 GdkInputFunction function,
1993 GtkCallbackMarshal marshal,
1995 GtkDestroyNotify destroy)
1999 GtkClosure *closure;
2001 closure = g_new (GtkClosure, 1);
2002 closure->marshal = marshal;
2003 closure->data = data;
2004 closure->destroy = destroy;
2006 return gdk_input_add_full (source,
2008 (GdkInputFunction) gtk_invoke_input,
2010 (GdkDestroyNotify) gtk_destroy_closure);
2013 return gdk_input_add_full (source, condition, function, data, destroy);
2017 gtk_input_remove (guint tag)
2019 g_source_remove (tag);
2023 gtk_destroy_closure (gpointer data)
2025 GtkClosure *closure = data;
2027 if (closure->destroy)
2028 (closure->destroy) (closure->data);
2033 gtk_invoke_idle_timeout (gpointer data)
2035 GtkClosure *closure = data;
2038 gint ret_val = FALSE;
2039 args[0].name = NULL;
2040 args[0].type = G_TYPE_BOOLEAN;
2041 args[0].d.pointer_data = &ret_val;
2042 closure->marshal (NULL, closure->data, 0, args);
2047 gtk_invoke_input (gpointer data,
2049 GdkInputCondition condition)
2051 GtkClosure *closure = data;
2054 args[0].type = G_TYPE_INT;
2055 args[0].name = NULL;
2056 GTK_VALUE_INT (args[0]) = source;
2057 args[1].type = GDK_TYPE_INPUT_CONDITION;
2058 args[1].name = NULL;
2059 GTK_VALUE_FLAGS (args[1]) = condition;
2060 args[2].type = G_TYPE_NONE;
2061 args[2].name = NULL;
2063 closure->marshal (NULL, closure->data, 2, args);
2067 * gtk_get_current_event:
2069 * Obtains a copy of the event currently being processed by GTK+. For
2070 * example, if you get a "clicked" signal from #GtkButton, the current
2071 * event will be the #GdkEventButton that triggered the "clicked"
2072 * signal. The returned event must be freed with gdk_event_free().
2073 * If there is no current event, the function returns %NULL.
2075 * Return value: a copy of the current event, or %NULL if no current event.
2078 gtk_get_current_event (void)
2081 return gdk_event_copy (current_events->data);
2087 * gtk_get_current_event_time:
2089 * If there is a current event and it has a timestamp, return that
2090 * timestamp, otherwise return %GDK_CURRENT_TIME.
2092 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2095 gtk_get_current_event_time (void)
2098 return gdk_event_get_time (current_events->data);
2100 return GDK_CURRENT_TIME;
2104 * gtk_get_current_event_state:
2105 * @state: a location to store the state of the current event
2107 * If there is a current event and it has a state field, place
2108 * that state field in @state and return %TRUE, otherwise return
2111 * Return value: %TRUE if there was a current event and it had a state field
2114 gtk_get_current_event_state (GdkModifierType *state)
2116 g_return_val_if_fail (state != NULL, FALSE);
2119 return gdk_event_get_state (current_events->data, state);
2128 * gtk_get_event_widget:
2129 * @event: a #GdkEvent
2131 * If @event is %NULL or the event was not associated with any widget,
2132 * returns %NULL, otherwise returns the widget that received the event
2135 * Return value: the widget that originally received @event, or %NULL
2138 gtk_get_event_widget (GdkEvent *event)
2143 if (event && event->any.window)
2144 gdk_window_get_user_data (event->any.window, (void**) &widget);
2150 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2152 if (!quitf->marshal)
2153 return quitf->function (quitf->data);
2157 gint ret_val = FALSE;
2159 args[0].name = NULL;
2160 args[0].type = G_TYPE_BOOLEAN;
2161 args[0].d.pointer_data = &ret_val;
2162 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2170 * gtk_propagate_event:
2171 * @widget: a #GtkWidget
2174 * Sends an event to a widget, propagating the event to parent widgets
2175 * if the event remains unhandled. Events received by GTK+ from GDK
2176 * normally begin in gtk_main_do_event(). Depending on the type of
2177 * event, existence of modal dialogs, grabs, etc., the event may be
2178 * propagated; if so, this function is used. gtk_propagate_event()
2179 * calls gtk_widget_event() on each widget it decides to send the
2180 * event to. So gtk_widget_event() is the lowest-level function; it
2181 * simply emits the "event" and possibly an event-specific signal on a
2182 * widget. gtk_propagate_event() is a bit higher-level, and
2183 * gtk_main_do_event() is the highest level.
2185 * All that said, you most likely don't want to use any of these
2186 * functions; synthesizing events is rarely needed. Consider asking on
2187 * the mailing list for better ways to achieve your goals. For
2188 * example, use gdk_window_invalidate_rect() or
2189 * gtk_widget_queue_draw() instead of making up expose events.
2193 gtk_propagate_event (GtkWidget *widget,
2198 g_return_if_fail (GTK_IS_WIDGET (widget));
2199 g_return_if_fail (event != NULL);
2201 handled_event = FALSE;
2203 g_object_ref (widget);
2205 if ((event->type == GDK_KEY_PRESS) ||
2206 (event->type == GDK_KEY_RELEASE))
2208 /* Only send key events within Window widgets to the Window
2209 * The Window widget will in turn pass the
2210 * key event on to the currently focused widget
2215 window = gtk_widget_get_toplevel (widget);
2216 if (window && GTK_IS_WINDOW (window))
2218 /* If there is a grab within the window, give the grab widget
2219 * a first crack at the key event
2221 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2222 handled_event = gtk_widget_event (widget, event);
2226 window = gtk_widget_get_toplevel (widget);
2227 if (window && GTK_IS_WINDOW (window))
2229 if (GTK_WIDGET_IS_SENSITIVE (window))
2230 gtk_widget_event (window, event);
2234 handled_event = TRUE; /* don't send to widget */
2238 /* Other events get propagated up the widget tree
2239 * so that parents can see the button and motion
2240 * events of the children.
2248 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2249 tmp = widget->parent;
2250 g_object_unref (widget);
2254 if (!handled_event && widget)
2255 g_object_ref (widget);
2261 g_object_unref (widget);
2266 gtk_error (gchar *str)
2272 gtk_warning (gchar *str)
2278 gtk_message (gchar *str)
2284 gtk_print (gchar *str)
2286 static GtkWidget *window = NULL;
2287 static GtkWidget *text;
2288 static int level = 0;
2292 GtkWidget *hscrollbar;
2293 GtkWidget *vscrollbar;
2294 GtkWidget *separator;
2299 fputs (str, stdout);
2306 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2308 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2309 (GtkSignalFunc) gtk_widget_destroyed,
2312 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2314 box1 = gtk_vbox_new (FALSE, 0);
2315 gtk_container_add (GTK_CONTAINER (window), box1);
2316 gtk_widget_show (box1);
2319 box2 = gtk_vbox_new (FALSE, 10);
2320 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2321 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2322 gtk_widget_show (box2);
2325 table = gtk_table_new (2, 2, FALSE);
2326 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2327 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2328 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2329 gtk_widget_show (table);
2331 text = gtk_text_new (NULL, NULL);
2332 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2333 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2334 gtk_widget_show (text);
2335 gtk_widget_realize (text);
2337 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2338 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2339 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2340 gtk_widget_show (hscrollbar);
2342 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2343 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2344 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2345 gtk_widget_show (vscrollbar);
2347 separator = gtk_hseparator_new ();
2348 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2349 gtk_widget_show (separator);
2352 box2 = gtk_vbox_new (FALSE, 10);
2353 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2354 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2355 gtk_widget_show (box2);
2358 button = gtk_button_new_with_label ("close");
2359 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2360 (GtkSignalFunc) gtk_widget_hide,
2361 GTK_OBJECT (window));
2362 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2363 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2364 gtk_widget_grab_default (button);
2365 gtk_widget_show (button);
2369 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2372 if (!GTK_WIDGET_VISIBLE (window))
2373 gtk_widget_show (window);
2378 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2379 GValue *return_accu,
2380 const GValue *handler_return,
2383 gboolean continue_emission;
2384 gboolean signal_handled;
2386 signal_handled = g_value_get_boolean (handler_return);
2387 g_value_set_boolean (return_accu, signal_handled);
2388 continue_emission = !signal_handled;
2390 return continue_emission;