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/.
30 #include "gdkconfig.h"
34 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
44 #include <sys/types.h> /* For uid_t, gid_t */
52 #include <pango/pango-types.h> /* For pango_language_from_string */
57 #include "gtkaccelmap.h"
59 #include "gtkclipboard.h"
61 #include "gtkversion.h"
63 #include "gtkmodules.h"
65 #include "gtkselection.h"
66 #include "gtksettings.h"
67 #include "gtkwidget.h"
68 #include "gtkwindow.h"
69 #include "gtkprivate.h"
72 /* Private type definitions
74 typedef struct _GtkInitFunction GtkInitFunction;
75 typedef struct _GtkQuitFunction GtkQuitFunction;
76 typedef struct _GtkClosure GtkClosure;
77 typedef struct _GtkKeySnooperData GtkKeySnooperData;
79 struct _GtkInitFunction
85 struct _GtkQuitFunction
89 GtkCallbackMarshal marshal;
92 GtkDestroyNotify destroy;
97 GtkCallbackMarshal marshal;
99 GtkDestroyNotify destroy;
102 struct _GtkKeySnooperData
104 GtkKeySnoopFunc func;
109 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
110 static void gtk_quit_destroy (GtkQuitFunction *quitf);
111 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
114 static void gtk_destroy_closure (gpointer data);
115 static gboolean gtk_invoke_idle_timeout (gpointer data);
116 static void gtk_invoke_input (gpointer data,
118 GdkInputCondition condition);
121 static void gtk_error (gchar *str);
122 static void gtk_warning (gchar *str);
123 static void gtk_message (gchar *str);
124 static void gtk_print (gchar *str);
127 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
129 const guint gtk_major_version = GTK_MAJOR_VERSION;
130 const guint gtk_minor_version = GTK_MINOR_VERSION;
131 const guint gtk_micro_version = GTK_MICRO_VERSION;
132 const guint gtk_binary_age = GTK_BINARY_AGE;
133 const guint gtk_interface_age = GTK_INTERFACE_AGE;
135 static guint gtk_main_loop_level = 0;
136 static gint gtk_initialized = FALSE;
137 static GList *current_events = NULL;
139 static GSList *main_loops = NULL; /* stack of currently executing main loops */
141 static GList *init_functions = NULL; /* A list of init functions.
143 static GList *quit_functions = NULL; /* A list of quit functions.
145 static GMemChunk *quit_mem_chunk = NULL;
147 static GSList *key_snoopers = NULL;
149 guint gtk_debug_flags = 0; /* Global GTK debug flag */
151 #ifdef G_ENABLE_DEBUG
152 static const GDebugKey gtk_debug_keys[] = {
153 {"misc", GTK_DEBUG_MISC},
154 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
155 {"text", GTK_DEBUG_TEXT},
156 {"tree", GTK_DEBUG_TREE},
157 {"updates", GTK_DEBUG_UPDATES},
158 {"keybindings", GTK_DEBUG_KEYBINDINGS},
159 {"multihead", GTK_DEBUG_MULTIHEAD},
160 {"modules", GTK_DEBUG_MODULES}
163 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
165 #endif /* G_ENABLE_DEBUG */
169 * @required_major: the required major version.
170 * @required_minor: the required major version.
171 * @required_micro: the required major version.
173 * Checks that the GTK+ library in use is compatible with the
174 * given version. Generally you would pass in the constants
175 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
176 * as the three arguments to this function; that produces
177 * a check that the library in use is compatible with
178 * the version of GTK+ the application or module was compiled
181 * Compatibility is defined by two things: first the version
182 * of the running library is newer than the version
183 * @required_major.required_minor.@required_micro. Second
184 * the running library must be binary compatible with the
185 * version @required_major.required_minor.@required_micro
186 * (same major version.)
188 * This function is primarily for GTK+ modules; the module
189 * can call this function to check that it wasn't loaded
190 * into an incompatible version of GTK+. However, such a
191 * a check isn't completely reliable, since the module may be
192 * linked against an old version of GTK+ and calling the
193 * old version of gtk_check_version(), but still get loaded
194 * into an application using a newer version of GTK+.
196 * Return value: %NULL if the GTK+ library is compatible with the
197 * given version, or a string describing the version mismatch.
198 * The returned string is owned by GTK+ and should not be modified
202 gtk_check_version (guint required_major,
203 guint required_minor,
204 guint required_micro)
206 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
207 gint required_effective_micro = 100 * required_minor + required_micro;
209 if (required_major > GTK_MAJOR_VERSION)
210 return "Gtk+ version too old (major mismatch)";
211 if (required_major < GTK_MAJOR_VERSION)
212 return "Gtk+ version too new (major mismatch)";
213 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
214 return "Gtk+ version too new (micro mismatch)";
215 if (required_effective_micro > gtk_effective_micro)
216 return "Gtk+ version too old (micro mismatch)";
220 /* This checks to see if the process is running suid or sgid
221 * at the current time. If so, we don't allow GTK+ to be initialized.
222 * This is meant to be a mild check - we only error out if we
223 * can prove the programmer is doing something wrong, not if
224 * they could be doing something wrong. For this reason, we
225 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
230 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
232 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
233 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
235 #ifdef HAVE_GETRESUID
236 /* These aren't in the header files, so we prototype them here.
238 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
239 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
241 if (getresuid (&ruid, &euid, &suid) != 0 ||
242 getresgid (&rgid, &egid, &sgid) != 0)
243 #endif /* HAVE_GETRESUID */
245 suid = ruid = getuid ();
246 sgid = rgid = getgid ();
251 if (ruid != euid || ruid != suid ||
252 rgid != egid || rgid != sgid)
254 g_warning ("This process is currently running setuid or setgid.\n"
255 "This is not a supported use of GTK+. You must create a helper\n"
256 "program instead. For further details, see:\n\n"
257 " http://www.gtk.org/setuid.html\n\n"
258 "Refusing to initialize GTK+.");
267 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
270 _gtk_get_datadir (void)
272 static char *gtk_datadir = NULL;
273 if (gtk_datadir == NULL)
274 gtk_datadir = g_win32_get_package_installation_subdirectory
275 (GETTEXT_PACKAGE, dll_name, "share");
281 _gtk_get_libdir (void)
283 static char *gtk_libdir = NULL;
284 if (gtk_libdir == NULL)
285 gtk_libdir = g_win32_get_package_installation_subdirectory
286 (GETTEXT_PACKAGE, dll_name, "lib");
292 _gtk_get_localedir (void)
294 static char *gtk_localedir = NULL;
295 if (gtk_localedir == NULL)
296 gtk_localedir = g_win32_get_package_installation_subdirectory
297 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
299 return gtk_localedir;
303 _gtk_get_sysconfdir (void)
305 static char *gtk_sysconfdir = NULL;
306 if (gtk_sysconfdir == NULL)
307 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
308 (GETTEXT_PACKAGE, dll_name, "etc");
310 return gtk_sysconfdir;
314 _gtk_get_data_prefix (void)
316 static char *gtk_data_prefix = NULL;
317 if (gtk_data_prefix == NULL)
318 gtk_data_prefix = g_win32_get_package_installation_directory
319 (GETTEXT_PACKAGE, dll_name);
321 return gtk_data_prefix;
324 #endif /* G_OS_WIN32 */
326 static gboolean do_setlocale = TRUE;
329 * gtk_disable_setlocale:
331 * Prevents gtk_init() and gtk_init_check() from automatically
332 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
333 * want to use this function if you wanted to set the locale for
334 * your program to something other than the user's locale, or if
335 * you wanted to set different values for different locale categories.
337 * Most programs should not need to call this function.
340 gtk_disable_setlocale (void)
343 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
345 do_setlocale = FALSE;
348 #ifdef G_PLATFORM_WIN32
349 #undef gtk_init_check
352 static GString *gtk_modules_string = NULL;
354 #ifdef G_ENABLE_DEBUG
356 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
358 gtk_debug_flags |= g_parse_debug_string (value,
366 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
368 gtk_debug_flags &= ~g_parse_debug_string (value,
374 #endif /* G_ENABLE_DEBUG */
377 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
381 if (gtk_modules_string)
382 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
384 gtk_modules_string = g_string_new (NULL);
386 g_string_append (gtk_modules_string, value);
393 gtk_arg_warnings_cb (const char *key, const char *value, gpointer user_data)
395 GLogLevelFlags fatal_mask;
397 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
398 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
399 g_log_set_always_fatal (fatal_mask);
404 static GOptionEntry gtk_args[] = {
405 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
406 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
407 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
408 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_warnings_cb,
409 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
410 #ifdef G_ENABLE_DEBUG
411 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
412 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
413 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
414 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
415 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
416 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
422 do_pre_parse_initialization (int *argc,
425 const gchar *env_string;
428 g_set_error_handler (gtk_error);
429 g_set_warning_handler (gtk_warning);
430 g_set_message_handler (gtk_message);
431 g_set_print_handler (gtk_print);
436 if (!setlocale (LC_ALL, ""))
437 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
440 gdk_pre_parse_libgtk_only ();
441 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
443 #ifdef G_ENABLE_DEBUG
444 env_string = g_getenv ("GTK_DEBUG");
445 if (env_string != NULL)
447 gtk_debug_flags = g_parse_debug_string (env_string,
452 #endif /* G_ENABLE_DEBUG */
454 env_string = g_getenv ("GTK_MODULES");
456 gtk_modules_string = g_string_new (env_string);
460 do_post_parse_initialization (int *argc,
463 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
464 gdk_window_set_debug_updates (TRUE);
467 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
468 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
469 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
470 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
471 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
476 /* Translate to default:RTL if you want your widgets
477 * to be RTL, otherwise translate to default:LTR.
478 * Do *not* translate it to "predefinito:LTR", if it
479 * it isn't default:LTR or default:RTL it will not work
481 char *e = _("default:LTR");
482 if (strcmp (e, "default:RTL")==0) {
483 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
484 } else if (strcmp (e, "default:LTR")) {
485 g_warning ("Whoever translated default:LTR did so wrongly.\n");
490 _gtk_accel_map_init ();
493 /* Set the 'initialized' flag.
495 gtk_initialized = TRUE;
497 /* load gtk modules */
498 if (gtk_modules_string)
500 _gtk_modules_init (argc, argv, gtk_modules_string->str);
501 g_string_free (gtk_modules_string, TRUE);
508 gboolean open_default_display;
512 pre_parse_hook (GOptionContext *context,
517 do_pre_parse_initialization (NULL, NULL);
523 post_parse_hook (GOptionContext *context,
528 OptionGroupInfo *info = data;
531 do_post_parse_initialization (NULL, NULL);
533 if (info->open_default_display)
534 return gdk_display_open_default_libgtk_only () != NULL;
541 gtk_get_option_group (gboolean open_default_display)
544 OptionGroupInfo *info;
546 info = g_new0 (OptionGroupInfo, 1);
547 info->open_default_display = open_default_display;
549 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
550 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
552 gdk_add_option_entries_libgtk_only (group);
553 g_option_group_add_entries (group, gtk_args);
554 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
560 gtk_init_with_args (int *argc,
562 char *parameter_string,
563 GOptionEntry *entries,
564 char *translation_domain,
567 GOptionContext *context;
568 GOptionGroup *gtk_group;
574 if (!check_setugid ())
577 gtk_group = gtk_get_option_group (TRUE);
579 context = g_option_context_new (parameter_string);
580 g_option_context_add_group (context, gtk_group);
583 g_option_context_add_main_entries (context, entries, translation_domain);
584 retval = g_option_context_parse (context, argc, argv, error);
586 g_option_context_free (context);
594 * @argc: a pointer to the number of command line arguments.
595 * @argv: a pointer to the array of command line arguments.
597 * Parses command line arguments, and initializes global
598 * attributes of GTK+, but does not actually open a connection
599 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
601 * Any arguments used by GTK+ or GDK are removed from the array and
602 * @argc and @argv are updated accordingly.
604 * You shouldn't call this function explicitely if you are using
605 * gtk_init(), or gtk_init_check().
607 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
610 gtk_parse_args (int *argc,
613 GOptionContext *option_context;
614 GOptionGroup *gtk_group;
619 if (!check_setugid ())
622 option_context = g_option_context_new (NULL);
623 g_option_context_set_ignore_unknown_options (option_context, TRUE);
624 g_option_context_set_help_enabled (option_context, FALSE);
625 gtk_group = gtk_get_option_group (FALSE);
626 g_option_context_set_main_group (option_context, gtk_group);
627 g_option_context_parse (option_context, argc, argv, NULL);
628 g_option_context_free (option_context);
633 #ifdef G_PLATFORM_WIN32
634 #undef gtk_init_check
639 * @argc: Address of the <parameter>argc</parameter> parameter of your
640 * main() function. Changed if any arguments were handled.
641 * @argv: Address of the <parameter>argv</parameter> parameter of main().
642 * Any parameters understood by gtk_init() are stripped before return.
644 * This function does the same work as gtk_init() with only
645 * a single change: It does not terminate the program if the GUI can't be
646 * initialized. Instead it returns %FALSE on failure.
648 * This way the application can fall back to some other means of communication
649 * with the user - for example a curses or command line interface.
651 * Return value: %TRUE if the GUI has been successfully initialized,
655 gtk_init_check (int *argc,
658 if (!gtk_parse_args (argc, argv))
661 return gdk_display_open_default_libgtk_only () != NULL;
664 #ifdef G_PLATFORM_WIN32
670 * @argc: Address of the <parameter>argc</parameter> parameter of your
671 * main() function. Changed if any arguments were handled.
672 * @argv: Address of the <parameter>argv</parameter> parameter of main().
673 * Any parameters understood by gtk_init() are stripped before return.
675 * Call this function before using any other GTK+ functions in your GUI
676 * applications. It will initialize everything needed to operate the
677 * toolkit and parses some standard command line options. @argc and
678 * @argv are adjusted accordingly so your own code will
679 * never see those standard arguments.
682 * This function will terminate your program if it was unable to initialize
683 * the GUI for some reason. If you want your program to fall back to a
684 * textual interface you want to call gtk_init_check() instead.
688 gtk_init (int *argc, char ***argv)
690 if (!gtk_init_check (argc, argv))
692 const char *display_name_arg = gdk_get_display_arg_name ();
693 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
698 #ifdef G_PLATFORM_WIN32
701 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
703 if (sizeof_GtkWindow != sizeof (GtkWindow))
704 g_error ("Incompatible build!\n"
705 "The code using GTK+ thinks GtkWindow is of different\n"
706 "size than it actually is in this build of GTK+.\n"
707 "On Windows, this probably means that you have compiled\n"
708 "your code with gcc without the -fnative-struct\n"
709 "(or -mms-bitfields) switch, or that you are using\n"
710 "an unsupported compiler.");
713 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
714 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
715 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
716 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
717 * -fnative-struct still matters. GtkBox is one such.
720 check_sizeof_GtkBox (size_t sizeof_GtkBox)
722 if (sizeof_GtkBox != sizeof (GtkBox))
723 g_error ("Incompatible build!\n"
724 "The code using GTK+ thinks GtkBox is of different\n"
725 "size than it actually is in this build of GTK+.\n"
726 "On Windows, this probably means that you have compiled\n"
727 "your code with gcc without the -fnative-struct\n"
728 "(or -mms-bitfields) switch, or that you are using\n"
729 "an unsupported compiler.");
732 /* These two functions might get more checks added later, thus pass
733 * in the number of extra args.
736 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
738 check_sizeof_GtkWindow (sizeof_GtkWindow);
740 check_sizeof_GtkBox (sizeof_GtkBox);
741 gtk_init (argc, argv);
745 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
747 check_sizeof_GtkWindow (sizeof_GtkWindow);
749 check_sizeof_GtkBox (sizeof_GtkBox);
750 return gtk_init_check (argc, argv);
756 gtk_exit (gint errorcode)
765 * Initializes internationalization support for GTK+. gtk_init()
766 * automatically does this, so there is typically no point
767 * in calling this function.
769 * If you are calling this function because you changed the locale
770 * after GTK+ is was initialized, then calling this function
771 * may help a bit. (Note, however, that changing the locale
772 * after GTK+ is initialized may produce inconsistent results and
773 * is not really supported.)
775 * In detail - sets the current locale according to the
776 * program environment. This is the same as calling the C library function
777 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
778 * locale specific setup of the windowing system used by GDK.
780 * Returns: a string corresponding to the locale set, typically in the
781 * form lang_COUNTRY, where lang is an ISO-639 language code, and
782 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
783 * result of the setlocale(); it is also used on other machines, such as
784 * Windows, where the C library returns a different result. The string is
785 * owned by GTK+ and should not be modified or freed.
788 gtk_set_locale (void)
790 return gdk_set_locale ();
796 * Return the Unix-style locale string for the language currently in
797 * effect. On Unix systems, this is the return value from
798 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
799 * affect this through the environment variables LC_ALL, LC_CTYPE or
800 * LANG (checked in that order). The locale strings typically is in
801 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
802 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
803 * Swedish as written in Finland or pt_BR for Portuguese as written in
806 * On Windows, the C library doesn't use any such environment
807 * variables, and setting them won't affect the behaviour of functions
808 * like ctime(). The user sets the locale through the Regional Options
809 * in the Control Panel. The C library (in the setlocale() function)
810 * does not use country and language codes, but country and language
811 * names spelled out in English.
812 * However, this function does check the above environment
813 * variables, and does return a Unix-style locale string based on
814 * either said environment variables or the thread's current locale.
816 * Return value: a dynamically allocated string, free with g_free().
820 _gtk_get_lc_ctype (void)
823 /* Somebody might try to set the locale for this process using the
824 * LANG or LC_ environment variables. The Microsoft C library
825 * doesn't know anything about them. You set the locale in the
826 * Control Panel. Setting these env vars won't have any affect on
827 * locale-dependent C library functions like ctime(). But just for
828 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
829 * it easier to test GTK and Pango in various default languages, you
830 * don't have to clickety-click in the Control Panel, you can simply
831 * start the program with LC_ALL=something on the command line.)
835 p = getenv ("LC_ALL");
839 p = getenv ("LC_CTYPE");
847 return g_win32_getlocale ();
849 return g_strdup (setlocale (LC_CTYPE, NULL));
854 * gtk_get_default_language:
856 * Returns the #PangoLanguage for the default language currently in
857 * effect. (Note that this can change over the life of an
858 * application.) The default language is derived from the current
859 * locale. It determines, for example, whether GTK+ uses the
860 * right-to-left or left-to-right text direction. See
861 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
863 * Return value: the default language as a #PangoLanguage, must not be
867 gtk_get_default_language (void)
870 PangoLanguage *result;
873 lang = _gtk_get_lc_ctype ();
874 p = strchr (lang, '.');
877 p = strchr (lang, '@');
881 result = pango_language_from_string (lang);
892 GtkInitFunction *init;
895 gtk_main_loop_level++;
897 loop = g_main_loop_new (NULL, TRUE);
898 main_loops = g_slist_prepend (main_loops, loop);
900 tmp_list = functions = init_functions;
901 init_functions = NULL;
905 init = tmp_list->data;
906 tmp_list = tmp_list->next;
908 (* init->function) (init->data);
911 g_list_free (functions);
913 if (g_main_loop_is_running (main_loops->data))
915 GDK_THREADS_LEAVE ();
916 g_main_loop_run (loop);
917 GDK_THREADS_ENTER ();
923 GList *reinvoke_list = NULL;
924 GtkQuitFunction *quitf;
926 while (quit_functions)
928 quitf = quit_functions->data;
930 tmp_list = quit_functions;
931 quit_functions = g_list_remove_link (quit_functions, quit_functions);
932 g_list_free_1 (tmp_list);
934 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
935 gtk_quit_invoke_function (quitf))
937 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
941 gtk_quit_destroy (quitf);
948 work = g_list_last (reinvoke_list);
950 quit_functions->prev = work;
951 work->next = quit_functions;
952 quit_functions = work;
958 main_loops = g_slist_remove (main_loops, loop);
960 g_main_loop_unref (loop);
962 gtk_main_loop_level--;
966 gtk_main_level (void)
968 return gtk_main_loop_level;
974 g_return_if_fail (main_loops != NULL);
976 g_main_loop_quit (main_loops->data);
980 gtk_events_pending (void)
984 GDK_THREADS_LEAVE ();
985 result = g_main_context_pending (NULL);
986 GDK_THREADS_ENTER ();
992 gtk_main_iteration (void)
994 GDK_THREADS_LEAVE ();
995 g_main_context_iteration (NULL, TRUE);
996 GDK_THREADS_ENTER ();
999 return !g_main_loop_is_running (main_loops->data);
1005 gtk_main_iteration_do (gboolean blocking)
1007 GDK_THREADS_LEAVE ();
1008 g_main_context_iteration (NULL, blocking);
1009 GDK_THREADS_ENTER ();
1012 return !g_main_loop_is_running (main_loops->data);
1017 /* private libgtk to libgdk interfaces
1019 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1020 GdkWindow **grab_window,
1021 gboolean *owner_events);
1022 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1023 GdkWindow **grab_window,
1024 gboolean *owner_events);
1027 rewrite_events_translate (GdkWindow *old_window,
1028 GdkWindow *new_window,
1032 gint old_origin_x, old_origin_y;
1033 gint new_origin_x, new_origin_y;
1035 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1036 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1038 *x += old_origin_x - new_origin_x;
1039 *y += old_origin_y - new_origin_y;
1043 rewrite_event_for_window (GdkEvent *event,
1044 GdkWindow *new_window)
1046 event = gdk_event_copy (event);
1048 switch (event->type)
1051 rewrite_events_translate (event->any.window,
1053 &event->scroll.x, &event->scroll.y);
1055 case GDK_BUTTON_PRESS:
1056 case GDK_2BUTTON_PRESS:
1057 case GDK_3BUTTON_PRESS:
1058 case GDK_BUTTON_RELEASE:
1059 rewrite_events_translate (event->any.window,
1061 &event->button.x, &event->button.y);
1063 case GDK_MOTION_NOTIFY:
1064 rewrite_events_translate (event->any.window,
1066 &event->motion.x, &event->motion.y);
1069 case GDK_KEY_RELEASE:
1070 case GDK_PROXIMITY_IN:
1071 case GDK_PROXIMITY_OUT:
1078 g_object_unref (event->any.window);
1079 event->any.window = g_object_ref (new_window);
1084 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1085 * then what X11 does is deliver the event normally if it was going to this
1086 * client, otherwise, delivers it in terms of the grab window. This function
1087 * rewrites events to the effect that events going to the same window group
1088 * are delivered normally, otherwise, the event is delivered in terms of the
1092 rewrite_event_for_grabs (GdkEvent *event)
1094 GdkWindow *grab_window;
1095 GtkWidget *event_widget, *grab_widget;
1096 gboolean owner_events;
1097 GdkDisplay *display;
1099 switch (event->type)
1102 case GDK_BUTTON_PRESS:
1103 case GDK_2BUTTON_PRESS:
1104 case GDK_3BUTTON_PRESS:
1105 case GDK_BUTTON_RELEASE:
1106 case GDK_MOTION_NOTIFY:
1107 case GDK_PROXIMITY_IN:
1108 case GDK_PROXIMITY_OUT:
1109 display = gdk_drawable_get_display (event->proximity.window);
1110 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1116 case GDK_KEY_RELEASE:
1117 display = gdk_drawable_get_display (event->key.window);
1118 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1127 event_widget = gtk_get_event_widget (event);
1128 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1131 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1132 return rewrite_event_for_window (event, grab_window);
1138 gtk_main_do_event (GdkEvent *event)
1140 GtkWidget *event_widget;
1141 GtkWidget *grab_widget;
1142 GtkWindowGroup *window_group;
1143 GdkEvent *next_event;
1144 GdkEvent *rewritten_event = NULL;
1147 /* If there are any events pending then get the next one.
1149 next_event = gdk_event_peek ();
1151 /* Try to compress enter/leave notify events. These event
1152 * pairs occur when the mouse is dragged quickly across
1153 * a window with many buttons (or through a menu). Instead
1154 * of highlighting and de-highlighting each widget that
1155 * is crossed it is better to simply de-highlight the widget
1156 * which contained the mouse initially and highlight the
1157 * widget which ends up containing the mouse.
1160 if (((event->type == GDK_ENTER_NOTIFY) ||
1161 (event->type == GDK_LEAVE_NOTIFY)) &&
1162 ((next_event->type == GDK_ENTER_NOTIFY) ||
1163 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1164 (next_event->type != event->type) &&
1165 (next_event->any.window == event->any.window))
1167 /* Throw both the peeked copy and the queued copy away
1169 gdk_event_free (next_event);
1170 next_event = gdk_event_get ();
1171 gdk_event_free (next_event);
1177 gdk_event_free (next_event);
1179 if (event->type == GDK_SETTING)
1181 _gtk_settings_handle_event (&event->setting);
1185 if (event->type == GDK_OWNER_CHANGE)
1187 _gtk_clipboard_handle_event (&event->owner_change);
1191 /* Find the widget which got the event. We store the widget
1192 * in the user_data field of GdkWindow's.
1193 * Ignore the event if we don't have a widget for it, except
1194 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1195 * Though this happens rarely, bogus events can occour
1196 * for e.g. destroyed GdkWindows.
1198 event_widget = gtk_get_event_widget (event);
1201 /* To handle selection INCR transactions, we select
1202 * PropertyNotify events on the requestor window and create
1203 * a corresponding (fake) GdkWindow so that events get
1204 * here. There won't be a widget though, so we have to handle
1207 if (event->type == GDK_PROPERTY_NOTIFY)
1208 _gtk_selection_incr_event (event->any.window,
1214 /* If pointer or keyboard grabs are in effect, munge the events
1215 * so that each window group looks like a separate app.
1217 rewritten_event = rewrite_event_for_grabs (event);
1218 if (rewritten_event)
1220 event = rewritten_event;
1221 event_widget = gtk_get_event_widget (event);
1224 window_group = gtk_main_get_window_group (event_widget);
1226 /* Push the event onto a stack of current events for
1227 * gtk_current_event_get().
1229 current_events = g_list_prepend (current_events, event);
1231 /* If there is a grab in effect...
1233 if (window_group->grabs)
1235 grab_widget = window_group->grabs->data;
1237 /* If the grab widget is an ancestor of the event widget
1238 * then we send the event to the original event widget.
1239 * This is the key to implementing modality.
1241 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1242 gtk_widget_is_ancestor (event_widget, grab_widget))
1243 grab_widget = event_widget;
1247 grab_widget = event_widget;
1250 /* Not all events get sent to the grabbing widget.
1251 * The delete, destroy, expose, focus change and resize
1252 * events still get sent to the event widget because
1253 * 1) these events have no meaning for the grabbing widget
1254 * and 2) redirecting these events to the grabbing widget
1255 * could cause the display to be messed up.
1257 * Drag events are also not redirected, since it isn't
1258 * clear what the semantics of that would be.
1260 switch (event->type)
1266 g_object_ref (event_widget);
1267 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1268 !gtk_widget_event (event_widget, event))
1269 gtk_widget_destroy (event_widget);
1270 g_object_unref (event_widget);
1274 /* Unexpected GDK_DESTROY from the outside, ignore for
1275 * child windows, handle like a GDK_DELETE for toplevels
1277 if (!event_widget->parent)
1279 g_object_ref (event_widget);
1280 if (!gtk_widget_event (event_widget, event) &&
1281 GTK_WIDGET_REALIZED (event_widget))
1282 gtk_widget_destroy (event_widget);
1283 g_object_unref (event_widget);
1288 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1290 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1291 gtk_widget_send_expose (event_widget, event);
1292 gdk_window_end_paint (event->any.window);
1295 gtk_widget_send_expose (event_widget, event);
1298 case GDK_PROPERTY_NOTIFY:
1300 case GDK_FOCUS_CHANGE:
1304 case GDK_SELECTION_CLEAR:
1305 case GDK_SELECTION_REQUEST:
1306 case GDK_SELECTION_NOTIFY:
1307 case GDK_CLIENT_EVENT:
1308 case GDK_VISIBILITY_NOTIFY:
1309 case GDK_WINDOW_STATE:
1310 gtk_widget_event (event_widget, event);
1314 case GDK_BUTTON_PRESS:
1315 case GDK_2BUTTON_PRESS:
1316 case GDK_3BUTTON_PRESS:
1317 gtk_propagate_event (grab_widget, event);
1321 case GDK_KEY_RELEASE:
1324 if (gtk_invoke_key_snoopers (grab_widget, event))
1327 /* else fall through */
1328 case GDK_MOTION_NOTIFY:
1329 case GDK_BUTTON_RELEASE:
1330 case GDK_PROXIMITY_IN:
1331 case GDK_PROXIMITY_OUT:
1332 gtk_propagate_event (grab_widget, event);
1335 case GDK_ENTER_NOTIFY:
1336 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1338 g_object_ref (event_widget);
1340 gtk_widget_event (grab_widget, event);
1341 if (event_widget == grab_widget)
1342 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1344 g_object_unref (event_widget);
1348 case GDK_LEAVE_NOTIFY:
1349 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1351 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1352 gtk_widget_event (event_widget, event);
1354 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1355 gtk_widget_event (grab_widget, event);
1358 case GDK_DRAG_STATUS:
1359 case GDK_DROP_FINISHED:
1360 _gtk_drag_source_handle_event (event_widget, event);
1362 case GDK_DRAG_ENTER:
1363 case GDK_DRAG_LEAVE:
1364 case GDK_DRAG_MOTION:
1365 case GDK_DROP_START:
1366 _gtk_drag_dest_handle_event (event_widget, event);
1369 g_assert_not_reached ();
1373 tmp_list = current_events;
1374 current_events = g_list_remove_link (current_events, tmp_list);
1375 g_list_free_1 (tmp_list);
1377 if (rewritten_event)
1378 gdk_event_free (rewritten_event);
1393 static GtkWindowGroup *
1394 gtk_main_get_window_group (GtkWidget *widget)
1396 GtkWidget *toplevel = NULL;
1399 toplevel = gtk_widget_get_toplevel (widget);
1401 if (toplevel && GTK_IS_WINDOW (toplevel))
1402 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1404 return _gtk_window_get_group (NULL);
1409 GtkWidget *old_grab_widget;
1410 GtkWidget *new_grab_widget;
1414 check_is_grabbed (GtkWidget *widget,
1415 GtkWidget *grab_widget)
1418 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1424 gtk_grab_notify_foreach (GtkWidget *child,
1428 GrabNotifyInfo *info = data;
1429 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1430 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1432 if (was_grabbed != is_grabbed)
1434 g_object_ref (child);
1436 _gtk_widget_grab_notify (child, was_grabbed);
1438 if (GTK_IS_CONTAINER (child))
1439 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1441 g_object_unref (child);
1446 gtk_grab_notify (GtkWindowGroup *group,
1447 GtkWidget *grab_widget,
1448 gboolean was_grabbed)
1451 GrabNotifyInfo info;
1455 info.old_grab_widget = grab_widget;
1456 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1460 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1461 info.new_grab_widget = grab_widget;
1464 g_object_ref (group);
1465 g_object_ref (grab_widget);
1467 toplevels = gtk_window_list_toplevels ();
1468 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1472 GtkWindow *toplevel = toplevels->data;
1473 toplevels = g_list_delete_link (toplevels, toplevels);
1475 if (group == _gtk_window_get_group (toplevel))
1476 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1477 g_object_unref (toplevel);
1480 g_object_unref (group);
1481 g_object_unref (grab_widget);
1485 gtk_grab_add (GtkWidget *widget)
1487 GtkWindowGroup *group;
1488 gboolean was_grabbed;
1490 g_return_if_fail (widget != NULL);
1492 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1494 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1496 group = gtk_main_get_window_group (widget);
1498 was_grabbed = (group->grabs != NULL);
1500 g_object_ref (widget);
1501 group->grabs = g_slist_prepend (group->grabs, widget);
1503 gtk_grab_notify (group, widget, FALSE);
1508 gtk_grab_get_current (void)
1510 GtkWindowGroup *group;
1512 group = gtk_main_get_window_group (NULL);
1515 return GTK_WIDGET (group->grabs->data);
1520 gtk_grab_remove (GtkWidget *widget)
1522 GtkWindowGroup *group;
1524 g_return_if_fail (widget != NULL);
1526 if (GTK_WIDGET_HAS_GRAB (widget))
1528 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1530 group = gtk_main_get_window_group (widget);
1531 group->grabs = g_slist_remove (group->grabs, widget);
1533 g_object_unref (widget);
1535 gtk_grab_notify (group, widget, TRUE);
1540 gtk_init_add (GtkFunction function,
1543 GtkInitFunction *init;
1545 init = g_new (GtkInitFunction, 1);
1546 init->function = function;
1549 init_functions = g_list_prepend (init_functions, init);
1553 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1556 GtkKeySnooperData *data;
1557 static guint snooper_id = 1;
1559 g_return_val_if_fail (snooper != NULL, 0);
1561 data = g_new (GtkKeySnooperData, 1);
1562 data->func = snooper;
1563 data->func_data = func_data;
1564 data->id = snooper_id++;
1565 key_snoopers = g_slist_prepend (key_snoopers, data);
1571 gtk_key_snooper_remove (guint snooper_id)
1573 GtkKeySnooperData *data = NULL;
1576 slist = key_snoopers;
1580 if (data->id == snooper_id)
1583 slist = slist->next;
1588 key_snoopers = g_slist_remove (key_snoopers, data);
1594 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1598 gint return_val = FALSE;
1600 slist = key_snoopers;
1601 while (slist && !return_val)
1603 GtkKeySnooperData *data;
1606 slist = slist->next;
1607 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1614 gtk_quit_add_full (guint main_level,
1615 GtkFunction function,
1616 GtkCallbackMarshal marshal,
1618 GtkDestroyNotify destroy)
1620 static guint quit_id = 1;
1621 GtkQuitFunction *quitf;
1623 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1625 if (!quit_mem_chunk)
1626 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1627 512, G_ALLOC_AND_FREE);
1629 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1631 quitf->id = quit_id++;
1632 quitf->main_level = main_level;
1633 quitf->function = function;
1634 quitf->marshal = marshal;
1636 quitf->destroy = destroy;
1638 quit_functions = g_list_prepend (quit_functions, quitf);
1644 gtk_quit_destroy (GtkQuitFunction *quitf)
1647 quitf->destroy (quitf->data);
1648 g_mem_chunk_free (quit_mem_chunk, quitf);
1652 gtk_quit_destructor (GtkObject **object_p)
1655 gtk_object_destroy (*object_p);
1662 gtk_quit_add_destroy (guint main_level,
1665 GtkObject **object_p;
1667 g_return_if_fail (main_level > 0);
1668 g_return_if_fail (GTK_IS_OBJECT (object));
1670 object_p = g_new (GtkObject*, 1);
1672 g_signal_connect (object,
1674 G_CALLBACK (gtk_widget_destroyed),
1676 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1680 gtk_quit_add (guint main_level,
1681 GtkFunction function,
1684 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1688 gtk_quit_remove (guint id)
1690 GtkQuitFunction *quitf;
1693 tmp_list = quit_functions;
1696 quitf = tmp_list->data;
1698 if (quitf->id == id)
1700 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1701 g_list_free (tmp_list);
1702 gtk_quit_destroy (quitf);
1707 tmp_list = tmp_list->next;
1712 gtk_quit_remove_by_data (gpointer data)
1714 GtkQuitFunction *quitf;
1717 tmp_list = quit_functions;
1720 quitf = tmp_list->data;
1722 if (quitf->data == data)
1724 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1725 g_list_free (tmp_list);
1726 gtk_quit_destroy (quitf);
1731 tmp_list = tmp_list->next;
1736 gtk_timeout_add_full (guint32 interval,
1737 GtkFunction function,
1738 GtkCallbackMarshal marshal,
1740 GtkDestroyNotify destroy)
1744 GtkClosure *closure;
1746 closure = g_new (GtkClosure, 1);
1747 closure->marshal = marshal;
1748 closure->data = data;
1749 closure->destroy = destroy;
1751 return g_timeout_add_full (0, interval,
1752 gtk_invoke_idle_timeout,
1754 gtk_destroy_closure);
1757 return g_timeout_add_full (0, interval, function, data, destroy);
1761 gtk_timeout_add (guint32 interval,
1762 GtkFunction function,
1765 return g_timeout_add_full (0, interval, function, data, NULL);
1769 gtk_timeout_remove (guint tag)
1771 g_source_remove (tag);
1775 gtk_idle_add_full (gint priority,
1776 GtkFunction function,
1777 GtkCallbackMarshal marshal,
1779 GtkDestroyNotify destroy)
1783 GtkClosure *closure;
1785 closure = g_new (GtkClosure, 1);
1786 closure->marshal = marshal;
1787 closure->data = data;
1788 closure->destroy = destroy;
1790 return g_idle_add_full (priority,
1791 gtk_invoke_idle_timeout,
1793 gtk_destroy_closure);
1796 return g_idle_add_full (priority, function, data, destroy);
1800 gtk_idle_add (GtkFunction function,
1803 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1807 gtk_idle_add_priority (gint priority,
1808 GtkFunction function,
1811 return g_idle_add_full (priority, function, data, NULL);
1815 gtk_idle_remove (guint tag)
1817 g_source_remove (tag);
1821 gtk_idle_remove_by_data (gpointer data)
1823 if (!g_idle_remove_by_data (data))
1824 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1828 gtk_input_add_full (gint source,
1829 GdkInputCondition condition,
1830 GdkInputFunction function,
1831 GtkCallbackMarshal marshal,
1833 GtkDestroyNotify destroy)
1837 GtkClosure *closure;
1839 closure = g_new (GtkClosure, 1);
1840 closure->marshal = marshal;
1841 closure->data = data;
1842 closure->destroy = destroy;
1844 return gdk_input_add_full (source,
1846 (GdkInputFunction) gtk_invoke_input,
1848 (GdkDestroyNotify) gtk_destroy_closure);
1851 return gdk_input_add_full (source, condition, function, data, destroy);
1855 gtk_input_remove (guint tag)
1857 g_source_remove (tag);
1861 gtk_destroy_closure (gpointer data)
1863 GtkClosure *closure = data;
1865 if (closure->destroy)
1866 (closure->destroy) (closure->data);
1871 gtk_invoke_idle_timeout (gpointer data)
1873 GtkClosure *closure = data;
1876 gint ret_val = FALSE;
1877 args[0].name = NULL;
1878 args[0].type = G_TYPE_BOOLEAN;
1879 args[0].d.pointer_data = &ret_val;
1880 closure->marshal (NULL, closure->data, 0, args);
1885 gtk_invoke_input (gpointer data,
1887 GdkInputCondition condition)
1889 GtkClosure *closure = data;
1892 args[0].type = G_TYPE_INT;
1893 args[0].name = NULL;
1894 GTK_VALUE_INT (args[0]) = source;
1895 args[1].type = GDK_TYPE_INPUT_CONDITION;
1896 args[1].name = NULL;
1897 GTK_VALUE_FLAGS (args[1]) = condition;
1898 args[2].type = G_TYPE_NONE;
1899 args[2].name = NULL;
1901 closure->marshal (NULL, closure->data, 2, args);
1905 * gtk_get_current_event:
1907 * Obtains a copy of the event currently being processed by GTK+. For
1908 * example, if you get a "clicked" signal from #GtkButton, the current
1909 * event will be the #GdkEventButton that triggered the "clicked"
1910 * signal. The returned event must be freed with gdk_event_free().
1911 * If there is no current event, the function returns %NULL.
1913 * Return value: a copy of the current event, or %NULL if no current event.
1916 gtk_get_current_event (void)
1919 return gdk_event_copy (current_events->data);
1925 * gtk_get_current_event_time:
1927 * If there is a current event and it has a timestamp, return that
1928 * timestamp, otherwise return %GDK_CURRENT_TIME.
1930 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1933 gtk_get_current_event_time (void)
1936 return gdk_event_get_time (current_events->data);
1938 return GDK_CURRENT_TIME;
1942 * gtk_get_current_event_state:
1943 * @state: a location to store the state of the current event
1945 * If there is a current event and it has a state field, place
1946 * that state field in @state and return %TRUE, otherwise return
1949 * Return value: %TRUE if there was a current event and it had a state field
1952 gtk_get_current_event_state (GdkModifierType *state)
1954 g_return_val_if_fail (state != NULL, FALSE);
1957 return gdk_event_get_state (current_events->data, state);
1966 * gtk_get_event_widget:
1967 * @event: a #GdkEvent
1969 * If @event is %NULL or the event was not associated with any widget,
1970 * returns %NULL, otherwise returns the widget that received the event
1973 * Return value: the widget that originally received @event, or %NULL
1976 gtk_get_event_widget (GdkEvent *event)
1981 if (event && event->any.window)
1982 gdk_window_get_user_data (event->any.window, (void**) &widget);
1988 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1990 if (!quitf->marshal)
1991 return quitf->function (quitf->data);
1995 gint ret_val = FALSE;
1997 args[0].name = NULL;
1998 args[0].type = G_TYPE_BOOLEAN;
1999 args[0].d.pointer_data = &ret_val;
2000 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2008 * gtk_propagate_event:
2009 * @widget: a #GtkWidget
2012 * Sends an event to a widget, propagating the event to parent widgets
2013 * if the event remains unhandled. Events received by GTK+ from GDK
2014 * normally begin in gtk_main_do_event(). Depending on the type of
2015 * event, existence of modal dialogs, grabs, etc., the event may be
2016 * propagated; if so, this function is used. gtk_propagate_event()
2017 * calls gtk_widget_event() on each widget it decides to send the
2018 * event to. So gtk_widget_event() is the lowest-level function; it
2019 * simply emits the "event" and possibly an event-specific signal on a
2020 * widget. gtk_propagate_event() is a bit higher-level, and
2021 * gtk_main_do_event() is the highest level.
2023 * All that said, you most likely don't want to use any of these
2024 * functions; synthesizing events is rarely needed. Consider asking on
2025 * the mailing list for better ways to achieve your goals. For
2026 * example, use gdk_window_invalidate_rect() or
2027 * gtk_widget_queue_draw() instead of making up expose events.
2031 gtk_propagate_event (GtkWidget *widget,
2036 g_return_if_fail (GTK_IS_WIDGET (widget));
2037 g_return_if_fail (event != NULL);
2039 handled_event = FALSE;
2041 g_object_ref (widget);
2043 if ((event->type == GDK_KEY_PRESS) ||
2044 (event->type == GDK_KEY_RELEASE))
2046 /* Only send key events within Window widgets to the Window
2047 * The Window widget will in turn pass the
2048 * key event on to the currently focused widget
2053 window = gtk_widget_get_toplevel (widget);
2054 if (window && GTK_IS_WINDOW (window))
2056 /* If there is a grab within the window, give the grab widget
2057 * a first crack at the key event
2059 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2060 handled_event = gtk_widget_event (widget, event);
2064 window = gtk_widget_get_toplevel (widget);
2065 if (window && GTK_IS_WINDOW (window))
2067 if (GTK_WIDGET_IS_SENSITIVE (window))
2068 gtk_widget_event (window, event);
2072 handled_event = TRUE; /* don't send to widget */
2076 /* Other events get propagated up the widget tree
2077 * so that parents can see the button and motion
2078 * events of the children.
2086 /* Scroll events are special cased here because it
2087 * feels wrong when scrolling a GtkViewport, say,
2088 * to have children of the viewport eat the scroll
2091 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2092 handled_event = event->type != GDK_SCROLL;
2094 handled_event = gtk_widget_event (widget, event);
2096 tmp = widget->parent;
2097 g_object_unref (widget);
2101 if (!handled_event && widget)
2102 g_object_ref (widget);
2108 g_object_unref (widget);
2113 gtk_error (gchar *str)
2119 gtk_warning (gchar *str)
2125 gtk_message (gchar *str)
2131 gtk_print (gchar *str)
2133 static GtkWidget *window = NULL;
2134 static GtkWidget *text;
2135 static int level = 0;
2139 GtkWidget *hscrollbar;
2140 GtkWidget *vscrollbar;
2141 GtkWidget *separator;
2146 fputs (str, stdout);
2153 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2155 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2156 (GtkSignalFunc) gtk_widget_destroyed,
2159 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2161 box1 = gtk_vbox_new (FALSE, 0);
2162 gtk_container_add (GTK_CONTAINER (window), box1);
2163 gtk_widget_show (box1);
2166 box2 = gtk_vbox_new (FALSE, 10);
2167 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2168 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2169 gtk_widget_show (box2);
2172 table = gtk_table_new (2, 2, FALSE);
2173 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2174 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2175 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2176 gtk_widget_show (table);
2178 text = gtk_text_new (NULL, NULL);
2179 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2180 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2181 gtk_widget_show (text);
2182 gtk_widget_realize (text);
2184 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2185 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2186 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2187 gtk_widget_show (hscrollbar);
2189 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2190 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2191 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2192 gtk_widget_show (vscrollbar);
2194 separator = gtk_hseparator_new ();
2195 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2196 gtk_widget_show (separator);
2199 box2 = gtk_vbox_new (FALSE, 10);
2200 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2201 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2202 gtk_widget_show (box2);
2205 button = gtk_button_new_with_label ("close");
2206 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2207 (GtkSignalFunc) gtk_widget_hide,
2208 GTK_OBJECT (window));
2209 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2210 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2211 gtk_widget_grab_default (button);
2212 gtk_widget_show (button);
2216 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2219 if (!GTK_WIDGET_VISIBLE (window))
2220 gtk_widget_show (window);
2225 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2226 GValue *return_accu,
2227 const GValue *handler_return,
2230 gboolean continue_emission;
2231 gboolean signal_handled;
2233 signal_handled = g_value_get_boolean (handler_return);
2234 g_value_set_boolean (return_accu, signal_handled);
2235 continue_emission = !signal_handled;
2237 return continue_emission;