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 if (event->type == GDK_SETTING)
1356 _gtk_settings_handle_event (&event->setting);
1360 /* Find the widget which got the event. We store the widget
1361 * in the user_data field of GdkWindow's.
1362 * Ignore the event if we don't have a widget for it, except
1363 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1364 * Though this happens rarely, bogus events can occour
1365 * for e.g. destroyed GdkWindows.
1367 event_widget = gtk_get_event_widget (event);
1370 /* To handle selection INCR transactions, we select
1371 * PropertyNotify events on the requestor window and create
1372 * a corresponding (fake) GdkWindow so that events get
1373 * here. There won't be a widget though, so we have to handle
1376 if (event->type == GDK_PROPERTY_NOTIFY)
1377 _gtk_selection_incr_event (event->any.window,
1383 /* If pointer or keyboard grabs are in effect, munge the events
1384 * so that each window group looks like a separate app.
1386 rewritten_event = rewrite_event_for_grabs (event);
1387 if (rewritten_event)
1389 event = rewritten_event;
1390 event_widget = gtk_get_event_widget (event);
1393 window_group = gtk_main_get_window_group (event_widget);
1395 /* Push the event onto a stack of current events for
1396 * gtk_current_event_get().
1398 current_events = g_list_prepend (current_events, event);
1400 /* If there is a grab in effect...
1402 if (window_group->grabs)
1404 grab_widget = window_group->grabs->data;
1406 /* If the grab widget is an ancestor of the event widget
1407 * then we send the event to the original event widget.
1408 * This is the key to implementing modality.
1410 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1411 gtk_widget_is_ancestor (event_widget, grab_widget))
1412 grab_widget = event_widget;
1416 grab_widget = event_widget;
1419 /* Not all events get sent to the grabbing widget.
1420 * The delete, destroy, expose, focus change and resize
1421 * events still get sent to the event widget because
1422 * 1) these events have no meaning for the grabbing widget
1423 * and 2) redirecting these events to the grabbing widget
1424 * could cause the display to be messed up.
1426 * Drag events are also not redirected, since it isn't
1427 * clear what the semantics of that would be.
1429 switch (event->type)
1435 g_object_ref (event_widget);
1436 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1437 !gtk_widget_event (event_widget, event))
1438 gtk_widget_destroy (event_widget);
1439 g_object_unref (event_widget);
1443 /* Unexpected GDK_DESTROY from the outside, ignore for
1444 * child windows, handle like a GDK_DELETE for toplevels
1446 if (!event_widget->parent)
1448 g_object_ref (event_widget);
1449 if (!gtk_widget_event (event_widget, event) &&
1450 GTK_WIDGET_REALIZED (event_widget))
1451 gtk_widget_destroy (event_widget);
1452 g_object_unref (event_widget);
1457 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1459 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1460 gtk_widget_send_expose (event_widget, event);
1461 gdk_window_end_paint (event->any.window);
1464 gtk_widget_send_expose (event_widget, event);
1467 case GDK_PROPERTY_NOTIFY:
1469 case GDK_FOCUS_CHANGE:
1473 case GDK_SELECTION_CLEAR:
1474 case GDK_SELECTION_REQUEST:
1475 case GDK_SELECTION_NOTIFY:
1476 case GDK_CLIENT_EVENT:
1477 case GDK_VISIBILITY_NOTIFY:
1478 case GDK_WINDOW_STATE:
1479 gtk_widget_event (event_widget, event);
1483 case GDK_BUTTON_PRESS:
1484 case GDK_2BUTTON_PRESS:
1485 case GDK_3BUTTON_PRESS:
1486 gtk_propagate_event (grab_widget, event);
1490 case GDK_KEY_RELEASE:
1493 if (gtk_invoke_key_snoopers (grab_widget, event))
1496 /* else fall through */
1497 case GDK_MOTION_NOTIFY:
1498 case GDK_BUTTON_RELEASE:
1499 case GDK_PROXIMITY_IN:
1500 case GDK_PROXIMITY_OUT:
1501 gtk_propagate_event (grab_widget, event);
1504 case GDK_ENTER_NOTIFY:
1505 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1507 g_object_ref (event_widget);
1509 gtk_widget_event (grab_widget, event);
1510 if (event_widget == grab_widget)
1511 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1513 g_object_unref (event_widget);
1517 case GDK_LEAVE_NOTIFY:
1518 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1520 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1521 gtk_widget_event (event_widget, event);
1523 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1524 gtk_widget_event (grab_widget, event);
1527 case GDK_DRAG_STATUS:
1528 case GDK_DROP_FINISHED:
1529 _gtk_drag_source_handle_event (event_widget, event);
1531 case GDK_DRAG_ENTER:
1532 case GDK_DRAG_LEAVE:
1533 case GDK_DRAG_MOTION:
1534 case GDK_DROP_START:
1535 _gtk_drag_dest_handle_event (event_widget, event);
1538 g_assert_not_reached ();
1542 tmp_list = current_events;
1543 current_events = g_list_remove_link (current_events, tmp_list);
1544 g_list_free_1 (tmp_list);
1546 if (rewritten_event)
1547 gdk_event_free (rewritten_event);
1562 static GtkWindowGroup *
1563 gtk_main_get_window_group (GtkWidget *widget)
1565 GtkWidget *toplevel = NULL;
1568 toplevel = gtk_widget_get_toplevel (widget);
1570 if (toplevel && GTK_IS_WINDOW (toplevel))
1571 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1573 return _gtk_window_get_group (NULL);
1578 GtkWidget *old_grab_widget;
1579 GtkWidget *new_grab_widget;
1583 check_is_grabbed (GtkWidget *widget,
1584 GtkWidget *grab_widget)
1587 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1593 gtk_grab_notify_foreach (GtkWidget *child,
1597 GrabNotifyInfo *info = data;
1598 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1599 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1601 if (was_grabbed != is_grabbed)
1603 g_object_ref (child);
1605 g_signal_emit_by_name (child, "grab_notify", was_grabbed);
1607 if (GTK_IS_CONTAINER (child))
1608 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1610 g_object_unref (child);
1615 gtk_grab_notify (GtkWindowGroup *group,
1616 GtkWidget *grab_widget,
1617 gboolean was_grabbed)
1620 GrabNotifyInfo info;
1624 info.old_grab_widget = grab_widget;
1625 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1629 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1630 info.new_grab_widget = grab_widget;
1633 g_object_ref (group);
1634 g_object_ref (grab_widget);
1636 toplevels = gtk_window_list_toplevels ();
1637 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1641 GtkWindow *toplevel = toplevels->data;
1642 toplevels = g_list_delete_link (toplevels, toplevels);
1644 if (group == _gtk_window_get_group (toplevel))
1645 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1646 g_object_unref (toplevel);
1649 g_object_unref (group);
1650 g_object_unref (grab_widget);
1654 gtk_grab_add (GtkWidget *widget)
1656 GtkWindowGroup *group;
1657 gboolean was_grabbed;
1659 g_return_if_fail (widget != NULL);
1661 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1663 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1665 group = gtk_main_get_window_group (widget);
1667 was_grabbed = (group->grabs != NULL);
1669 g_object_ref (widget);
1670 group->grabs = g_slist_prepend (group->grabs, widget);
1672 gtk_grab_notify (group, widget, FALSE);
1677 gtk_grab_get_current (void)
1679 GtkWindowGroup *group;
1681 group = gtk_main_get_window_group (NULL);
1684 return GTK_WIDGET (group->grabs->data);
1689 gtk_grab_remove (GtkWidget *widget)
1691 GtkWindowGroup *group;
1693 g_return_if_fail (widget != NULL);
1695 if (GTK_WIDGET_HAS_GRAB (widget))
1697 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1699 group = gtk_main_get_window_group (widget);
1700 group->grabs = g_slist_remove (group->grabs, widget);
1702 g_object_unref (widget);
1704 gtk_grab_notify (group, widget, TRUE);
1709 gtk_init_add (GtkFunction function,
1712 GtkInitFunction *init;
1714 init = g_new (GtkInitFunction, 1);
1715 init->function = function;
1718 init_functions = g_list_prepend (init_functions, init);
1722 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1725 GtkKeySnooperData *data;
1726 static guint snooper_id = 1;
1728 g_return_val_if_fail (snooper != NULL, 0);
1730 data = g_new (GtkKeySnooperData, 1);
1731 data->func = snooper;
1732 data->func_data = func_data;
1733 data->id = snooper_id++;
1734 key_snoopers = g_slist_prepend (key_snoopers, data);
1740 gtk_key_snooper_remove (guint snooper_id)
1742 GtkKeySnooperData *data = NULL;
1745 slist = key_snoopers;
1749 if (data->id == snooper_id)
1752 slist = slist->next;
1756 key_snoopers = g_slist_remove (key_snoopers, data);
1760 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1764 gint return_val = FALSE;
1766 slist = key_snoopers;
1767 while (slist && !return_val)
1769 GtkKeySnooperData *data;
1772 slist = slist->next;
1773 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1780 gtk_quit_add_full (guint main_level,
1781 GtkFunction function,
1782 GtkCallbackMarshal marshal,
1784 GtkDestroyNotify destroy)
1786 static guint quit_id = 1;
1787 GtkQuitFunction *quitf;
1789 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1791 if (!quit_mem_chunk)
1792 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1793 512, G_ALLOC_AND_FREE);
1795 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1797 quitf->id = quit_id++;
1798 quitf->main_level = main_level;
1799 quitf->function = function;
1800 quitf->marshal = marshal;
1802 quitf->destroy = destroy;
1804 quit_functions = g_list_prepend (quit_functions, quitf);
1810 gtk_quit_destroy (GtkQuitFunction *quitf)
1813 quitf->destroy (quitf->data);
1814 g_mem_chunk_free (quit_mem_chunk, quitf);
1818 gtk_quit_destructor (GtkObject **object_p)
1821 gtk_object_destroy (*object_p);
1828 gtk_quit_add_destroy (guint main_level,
1831 GtkObject **object_p;
1833 g_return_if_fail (main_level > 0);
1834 g_return_if_fail (GTK_IS_OBJECT (object));
1836 object_p = g_new (GtkObject*, 1);
1838 g_signal_connect (object,
1840 G_CALLBACK (gtk_widget_destroyed),
1842 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1846 gtk_quit_add (guint main_level,
1847 GtkFunction function,
1850 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1854 gtk_quit_remove (guint id)
1856 GtkQuitFunction *quitf;
1859 tmp_list = quit_functions;
1862 quitf = tmp_list->data;
1864 if (quitf->id == id)
1866 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1867 g_list_free (tmp_list);
1868 gtk_quit_destroy (quitf);
1873 tmp_list = tmp_list->next;
1878 gtk_quit_remove_by_data (gpointer data)
1880 GtkQuitFunction *quitf;
1883 tmp_list = quit_functions;
1886 quitf = tmp_list->data;
1888 if (quitf->data == data)
1890 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1891 g_list_free (tmp_list);
1892 gtk_quit_destroy (quitf);
1897 tmp_list = tmp_list->next;
1902 gtk_timeout_add_full (guint32 interval,
1903 GtkFunction function,
1904 GtkCallbackMarshal marshal,
1906 GtkDestroyNotify destroy)
1910 GtkClosure *closure;
1912 closure = g_new (GtkClosure, 1);
1913 closure->marshal = marshal;
1914 closure->data = data;
1915 closure->destroy = destroy;
1917 return g_timeout_add_full (0, interval,
1918 gtk_invoke_idle_timeout,
1920 gtk_destroy_closure);
1923 return g_timeout_add_full (0, interval, function, data, destroy);
1927 gtk_timeout_add (guint32 interval,
1928 GtkFunction function,
1931 return g_timeout_add_full (0, interval, function, data, NULL);
1935 gtk_timeout_remove (guint tag)
1937 g_source_remove (tag);
1941 gtk_idle_add_full (gint priority,
1942 GtkFunction function,
1943 GtkCallbackMarshal marshal,
1945 GtkDestroyNotify destroy)
1949 GtkClosure *closure;
1951 closure = g_new (GtkClosure, 1);
1952 closure->marshal = marshal;
1953 closure->data = data;
1954 closure->destroy = destroy;
1956 return g_idle_add_full (priority,
1957 gtk_invoke_idle_timeout,
1959 gtk_destroy_closure);
1962 return g_idle_add_full (priority, function, data, destroy);
1966 gtk_idle_add (GtkFunction function,
1969 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1973 gtk_idle_add_priority (gint priority,
1974 GtkFunction function,
1977 return g_idle_add_full (priority, function, data, NULL);
1981 gtk_idle_remove (guint tag)
1983 g_source_remove (tag);
1987 gtk_idle_remove_by_data (gpointer data)
1989 if (!g_idle_remove_by_data (data))
1990 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1994 gtk_input_add_full (gint source,
1995 GdkInputCondition condition,
1996 GdkInputFunction function,
1997 GtkCallbackMarshal marshal,
1999 GtkDestroyNotify destroy)
2003 GtkClosure *closure;
2005 closure = g_new (GtkClosure, 1);
2006 closure->marshal = marshal;
2007 closure->data = data;
2008 closure->destroy = destroy;
2010 return gdk_input_add_full (source,
2012 (GdkInputFunction) gtk_invoke_input,
2014 (GdkDestroyNotify) gtk_destroy_closure);
2017 return gdk_input_add_full (source, condition, function, data, destroy);
2021 gtk_input_remove (guint tag)
2023 g_source_remove (tag);
2027 gtk_destroy_closure (gpointer data)
2029 GtkClosure *closure = data;
2031 if (closure->destroy)
2032 (closure->destroy) (closure->data);
2037 gtk_invoke_idle_timeout (gpointer data)
2039 GtkClosure *closure = data;
2042 gint ret_val = FALSE;
2043 args[0].name = NULL;
2044 args[0].type = G_TYPE_BOOLEAN;
2045 args[0].d.pointer_data = &ret_val;
2046 closure->marshal (NULL, closure->data, 0, args);
2051 gtk_invoke_input (gpointer data,
2053 GdkInputCondition condition)
2055 GtkClosure *closure = data;
2058 args[0].type = G_TYPE_INT;
2059 args[0].name = NULL;
2060 GTK_VALUE_INT (args[0]) = source;
2061 args[1].type = GDK_TYPE_INPUT_CONDITION;
2062 args[1].name = NULL;
2063 GTK_VALUE_FLAGS (args[1]) = condition;
2064 args[2].type = G_TYPE_NONE;
2065 args[2].name = NULL;
2067 closure->marshal (NULL, closure->data, 2, args);
2071 * gtk_get_current_event:
2073 * Obtains a copy of the event currently being processed by GTK+. For
2074 * example, if you get a "clicked" signal from #GtkButton, the current
2075 * event will be the #GdkEventButton that triggered the "clicked"
2076 * signal. The returned event must be freed with gdk_event_free().
2077 * If there is no current event, the function returns %NULL.
2079 * Return value: a copy of the current event, or %NULL if no current event.
2082 gtk_get_current_event (void)
2085 return gdk_event_copy (current_events->data);
2091 * gtk_get_current_event_time:
2093 * If there is a current event and it has a timestamp, return that
2094 * timestamp, otherwise return %GDK_CURRENT_TIME.
2096 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2099 gtk_get_current_event_time (void)
2102 return gdk_event_get_time (current_events->data);
2104 return GDK_CURRENT_TIME;
2108 * gtk_get_current_event_state:
2109 * @state: a location to store the state of the current event
2111 * If there is a current event and it has a state field, place
2112 * that state field in @state and return %TRUE, otherwise return
2115 * Return value: %TRUE if there was a current event and it had a state field
2118 gtk_get_current_event_state (GdkModifierType *state)
2120 g_return_val_if_fail (state != NULL, FALSE);
2123 return gdk_event_get_state (current_events->data, state);
2132 * gtk_get_event_widget:
2133 * @event: a #GdkEvent
2135 * If @event is %NULL or the event was not associated with any widget,
2136 * returns %NULL, otherwise returns the widget that received the event
2139 * Return value: the widget that originally received @event, or %NULL
2142 gtk_get_event_widget (GdkEvent *event)
2147 if (event && event->any.window)
2148 gdk_window_get_user_data (event->any.window, (void**) &widget);
2154 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2156 if (!quitf->marshal)
2157 return quitf->function (quitf->data);
2161 gint ret_val = FALSE;
2163 args[0].name = NULL;
2164 args[0].type = G_TYPE_BOOLEAN;
2165 args[0].d.pointer_data = &ret_val;
2166 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2174 * gtk_propagate_event:
2175 * @widget: a #GtkWidget
2178 * Sends an event to a widget, propagating the event to parent widgets
2179 * if the event remains unhandled. Events received by GTK+ from GDK
2180 * normally begin in gtk_main_do_event(). Depending on the type of
2181 * event, existence of modal dialogs, grabs, etc., the event may be
2182 * propagated; if so, this function is used. gtk_propagate_event()
2183 * calls gtk_widget_event() on each widget it decides to send the
2184 * event to. So gtk_widget_event() is the lowest-level function; it
2185 * simply emits the "event" and possibly an event-specific signal on a
2186 * widget. gtk_propagate_event() is a bit higher-level, and
2187 * gtk_main_do_event() is the highest level.
2189 * All that said, you most likely don't want to use any of these
2190 * functions; synthesizing events is rarely needed. Consider asking on
2191 * the mailing list for better ways to achieve your goals. For
2192 * example, use gdk_window_invalidate_rect() or
2193 * gtk_widget_queue_draw() instead of making up expose events.
2197 gtk_propagate_event (GtkWidget *widget,
2202 g_return_if_fail (GTK_IS_WIDGET (widget));
2203 g_return_if_fail (event != NULL);
2205 handled_event = FALSE;
2207 g_object_ref (widget);
2209 if ((event->type == GDK_KEY_PRESS) ||
2210 (event->type == GDK_KEY_RELEASE))
2212 /* Only send key events within Window widgets to the Window
2213 * The Window widget will in turn pass the
2214 * key event on to the currently focused widget
2219 window = gtk_widget_get_toplevel (widget);
2220 if (window && GTK_IS_WINDOW (window))
2222 /* If there is a grab within the window, give the grab widget
2223 * a first crack at the key event
2225 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2226 handled_event = gtk_widget_event (widget, event);
2230 window = gtk_widget_get_toplevel (widget);
2231 if (window && GTK_IS_WINDOW (window))
2233 if (GTK_WIDGET_IS_SENSITIVE (window))
2234 gtk_widget_event (window, event);
2238 handled_event = TRUE; /* don't send to widget */
2242 /* Other events get propagated up the widget tree
2243 * so that parents can see the button and motion
2244 * events of the children.
2252 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
2253 tmp = widget->parent;
2254 g_object_unref (widget);
2258 if (!handled_event && widget)
2259 g_object_ref (widget);
2265 g_object_unref (widget);
2270 gtk_error (gchar *str)
2276 gtk_warning (gchar *str)
2282 gtk_message (gchar *str)
2288 gtk_print (gchar *str)
2290 static GtkWidget *window = NULL;
2291 static GtkWidget *text;
2292 static int level = 0;
2296 GtkWidget *hscrollbar;
2297 GtkWidget *vscrollbar;
2298 GtkWidget *separator;
2303 fputs (str, stdout);
2310 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2312 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2313 (GtkSignalFunc) gtk_widget_destroyed,
2316 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2318 box1 = gtk_vbox_new (FALSE, 0);
2319 gtk_container_add (GTK_CONTAINER (window), box1);
2320 gtk_widget_show (box1);
2323 box2 = gtk_vbox_new (FALSE, 10);
2324 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2325 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2326 gtk_widget_show (box2);
2329 table = gtk_table_new (2, 2, FALSE);
2330 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2331 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2332 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2333 gtk_widget_show (table);
2335 text = gtk_text_new (NULL, NULL);
2336 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2337 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2338 gtk_widget_show (text);
2339 gtk_widget_realize (text);
2341 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2342 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2343 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2344 gtk_widget_show (hscrollbar);
2346 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2347 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2348 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2349 gtk_widget_show (vscrollbar);
2351 separator = gtk_hseparator_new ();
2352 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2353 gtk_widget_show (separator);
2356 box2 = gtk_vbox_new (FALSE, 10);
2357 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2358 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2359 gtk_widget_show (box2);
2362 button = gtk_button_new_with_label ("close");
2363 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2364 (GtkSignalFunc) gtk_widget_hide,
2365 GTK_OBJECT (window));
2366 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2367 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2368 gtk_widget_grab_default (button);
2369 gtk_widget_show (button);
2373 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2376 if (!GTK_WIDGET_VISIBLE (window))
2377 gtk_widget_show (window);
2382 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2383 GValue *return_accu,
2384 const GValue *handler_return,
2387 gboolean continue_emission;
2388 gboolean signal_handled;
2390 signal_handled = g_value_get_boolean (handler_return);
2391 g_value_set_boolean (return_accu, signal_handled);
2392 continue_emission = !signal_handled;
2394 return continue_emission;