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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: Library initialization, main event loop, and events
28 * @Title: Main loop and Events
29 * @See_also:See the GLib manual, especially #GMainLoop and signal-related
30 * functions such as g_signal_connect()
32 * Before using GTK+, you need to initialize it; initialization connects to the
33 * window system display, and parses some standard command line arguments. The
34 * gtk_init() macro initializes GTK+. gtk_init() exits the application if errors
35 * occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to
36 * recover from a failed GTK+ initialization - you might start up your
37 * application in text mode instead.
39 * Like all GUI toolkits, GTK+ uses an event-driven programming model. When the
40 * user is doing nothing, GTK+ sits in the <firstterm>main loop</firstterm> and
41 * waits for input. If the user performs some action - say, a mouse click - then
42 * the main loop "wakes up" and delivers an event to GTK+. GTK+ forwards the
43 * event to one or more widgets.
45 * When widgets receive an event, they frequently emit one or more
46 * <firstterm>signals</firstterm>. Signals notify your program that "something
47 * interesting happened" by invoking functions you've connected to the signal
48 * with g_signal_connect(). Functions connected to a signal are often termed
49 * <firstterm>callbacks</firstterm>.
51 * When your callbacks are invoked, you would typically take some action - for
52 * example, when an Open button is clicked you might display a
53 * #GtkFileChooserDialog. After a callback finishes, GTK+ will return to the
54 * main loop and await more user input.
57 * <title>Typical <function>main()</function> function for a GTK+ application</title>
60 * main (int argc, char **argv)
62 * /* Initialize i18n support */
65 * /* Initialize the widget set */
66 * gtk_init (&argc, &argv);
68 * /* Create the main window */
69 * mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
71 * /* Set up our GUI elements */
74 * /* Show the application window */
75 * gtk_widget_show_all (mainwin);
77 * /* Enter the main event loop, and wait for user interaction */
80 * /* The user lost interest */
86 * It's OK to use the GLib main loop directly instead of gtk_main(), though it
87 * involves slightly more typing. See #GMainLoop in the GLib documentation.
102 #include <sys/types.h> /* For uid_t, gid_t */
112 #include "gtkaccelmapprivate.h"
114 #include "gtkclipboard.h"
115 #include "gtkdebug.h"
119 #include "gtkmodules.h"
120 #include "gtkmodulesprivate.h"
121 #include "gtkprivate.h"
122 #include "gtkrecentmanager.h"
123 #include "gtkresources.h"
124 #include "gtkselectionprivate.h"
125 #include "gtksettingsprivate.h"
126 #include "gtktooltip.h"
127 #include "gtkversion.h"
128 #include "gtkwidgetprivate.h"
129 #include "gtkwindowprivate.h"
131 #include "a11y/gailutil.h"
133 /* Private type definitions
135 typedef struct _GtkKeySnooperData GtkKeySnooperData;
137 struct _GtkKeySnooperData
139 GtkKeySnoopFunc func;
144 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
147 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
149 static guint gtk_main_loop_level = 0;
150 static gint pre_initialized = FALSE;
151 static gint gtk_initialized = FALSE;
152 static GList *current_events = NULL;
154 static GSList *main_loops = NULL; /* stack of currently executing main loops */
156 static GSList *key_snoopers = NULL;
158 static guint debug_flags = 0; /* Global GTK debug flag */
160 #ifdef G_ENABLE_DEBUG
161 static const GDebugKey gtk_debug_keys[] = {
162 {"misc", GTK_DEBUG_MISC},
163 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
164 {"text", GTK_DEBUG_TEXT},
165 {"tree", GTK_DEBUG_TREE},
166 {"updates", GTK_DEBUG_UPDATES},
167 {"keybindings", GTK_DEBUG_KEYBINDINGS},
168 {"multihead", GTK_DEBUG_MULTIHEAD},
169 {"modules", GTK_DEBUG_MODULES},
170 {"geometry", GTK_DEBUG_GEOMETRY},
171 {"icontheme", GTK_DEBUG_ICONTHEME},
172 {"printing", GTK_DEBUG_PRINTING},
173 {"builder", GTK_DEBUG_BUILDER},
174 {"size-request", GTK_DEBUG_SIZE_REQUEST},
176 #endif /* G_ENABLE_DEBUG */
179 * gtk_get_major_version:
181 * Returns the major version number of the GTK+ library.
182 * (e.g. in GTK+ version 3.1.5 this is 3.)
184 * This function is in the library, so it represents the GTK+ library
185 * your code is running against. Contrast with the #GTK_MAJOR_VERSION
186 * macro, which represents the major version of the GTK+ headers you
187 * have included when compiling your code.
189 * Returns: the major version number of the GTK+ library
194 gtk_get_major_version (void)
196 return GTK_MAJOR_VERSION;
200 * gtk_get_minor_version:
202 * Returns the minor version number of the GTK+ library.
203 * (e.g. in GTK+ version 3.1.5 this is 1.)
205 * This function is in the library, so it represents the GTK+ library
206 * your code is are running against. Contrast with the
207 * #GTK_MINOR_VERSION macro, which represents the minor version of the
208 * GTK+ headers you have included when compiling your code.
210 * Returns: the minor version number of the GTK+ library
215 gtk_get_minor_version (void)
217 return GTK_MINOR_VERSION;
221 * gtk_get_micro_version:
223 * Returns the micro version number of the GTK+ library.
224 * (e.g. in GTK+ version 3.1.5 this is 5.)
226 * This function is in the library, so it represents the GTK+ library
227 * your code is are running against. Contrast with the
228 * #GTK_MICRO_VERSION macro, which represents the micro version of the
229 * GTK+ headers you have included when compiling your code.
231 * Returns: the micro version number of the GTK+ library
236 gtk_get_micro_version (void)
238 return GTK_MICRO_VERSION;
242 * gtk_get_binary_age:
244 * Returns the binary age as passed to <application>libtool</application>
245 * when building the GTK+ library the process is running against.
246 * If <application>libtool</application> means nothing to you, don't
249 * Returns: the binary age of the GTK+ library
254 gtk_get_binary_age (void)
256 return GTK_BINARY_AGE;
260 * gtk_get_interface_age:
262 * Returns the interface age as passed to <application>libtool</application>
263 * when building the GTK+ library the process is running against.
264 * If <application>libtool</application> means nothing to you, don't
267 * Returns: the interface age of the GTK+ library
272 gtk_get_interface_age (void)
274 return GTK_INTERFACE_AGE;
279 * @required_major: the required major version
280 * @required_minor: the required minor version
281 * @required_micro: the required micro version
283 * Checks that the GTK+ library in use is compatible with the
284 * given version. Generally you would pass in the constants
285 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
286 * as the three arguments to this function; that produces
287 * a check that the library in use is compatible with
288 * the version of GTK+ the application or module was compiled
291 * Compatibility is defined by two things: first the version
292 * of the running library is newer than the version
293 * @required_major.required_minor.@required_micro. Second
294 * the running library must be binary compatible with the
295 * version @required_major.required_minor.@required_micro
296 * (same major version.)
298 * This function is primarily for GTK+ modules; the module
299 * can call this function to check that it wasn't loaded
300 * into an incompatible version of GTK+. However, such a
301 * check isn't completely reliable, since the module may be
302 * linked against an old version of GTK+ and calling the
303 * old version of gtk_check_version(), but still get loaded
304 * into an application using a newer version of GTK+.
306 * Return value: %NULL if the GTK+ library is compatible with the
307 * given version, or a string describing the version mismatch.
308 * The returned string is owned by GTK+ and should not be modified
312 gtk_check_version (guint required_major,
313 guint required_minor,
314 guint required_micro)
316 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
317 gint required_effective_micro = 100 * required_minor + required_micro;
319 if (required_major > GTK_MAJOR_VERSION)
320 return "GTK+ version too old (major mismatch)";
321 if (required_major < GTK_MAJOR_VERSION)
322 return "GTK+ version too new (major mismatch)";
323 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
324 return "GTK+ version too new (micro mismatch)";
325 if (required_effective_micro > gtk_effective_micro)
326 return "GTK+ version too old (micro mismatch)";
330 /* This checks to see if the process is running suid or sgid
331 * at the current time. If so, we don't allow GTK+ to be initialized.
332 * This is meant to be a mild check - we only error out if we
333 * can prove the programmer is doing something wrong, not if
334 * they could be doing something wrong. For this reason, we
335 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
340 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
342 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
343 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
345 #ifdef HAVE_GETRESUID
346 /* These aren't in the header files, so we prototype them here.
348 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
349 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
351 if (getresuid (&ruid, &euid, &suid) != 0 ||
352 getresgid (&rgid, &egid, &sgid) != 0)
353 #endif /* HAVE_GETRESUID */
355 suid = ruid = getuid ();
356 sgid = rgid = getgid ();
361 if (ruid != euid || ruid != suid ||
362 rgid != egid || rgid != sgid)
364 g_warning ("This process is currently running setuid or setgid.\n"
365 "This is not a supported use of GTK+. You must create a helper\n"
366 "program instead. For further details, see:\n\n"
367 " http://www.gtk.org/setuid.html\n\n"
368 "Refusing to initialize GTK+.");
375 static gboolean do_setlocale = TRUE;
378 * gtk_disable_setlocale:
380 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
381 * gtk_parse_args() from automatically
382 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
383 * want to use this function if you wanted to set the locale for
384 * your program to something other than the user's locale, or if
385 * you wanted to set different values for different locale categories.
387 * Most programs should not need to call this function.
390 gtk_disable_setlocale (void)
393 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
395 do_setlocale = FALSE;
398 #ifdef G_PLATFORM_WIN32
399 #undef gtk_init_check
402 static GString *gtk_modules_string = NULL;
403 static gboolean g_fatal_warnings = FALSE;
405 #ifdef G_ENABLE_DEBUG
407 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
409 debug_flags |= g_parse_debug_string (value,
411 G_N_ELEMENTS (gtk_debug_keys));
417 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
419 debug_flags &= ~g_parse_debug_string (value,
421 G_N_ELEMENTS (gtk_debug_keys));
425 #endif /* G_ENABLE_DEBUG */
428 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
432 if (gtk_modules_string)
433 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
435 gtk_modules_string = g_string_new (NULL);
437 g_string_append (gtk_modules_string, value);
443 static const GOptionEntry gtk_args[] = {
444 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
445 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
446 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
447 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
448 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
449 #ifdef G_ENABLE_DEBUG
450 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
451 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
452 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
453 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
454 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
455 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
462 static char *iso639_to_check = NULL;
463 static char *iso3166_to_check = NULL;
464 static char *script_to_check = NULL;
465 static gboolean setlocale_called = FALSE;
468 enum_locale_proc (LPTSTR locale)
476 lcid = strtoul (locale, &endptr, 16);
477 if (*endptr == '\0' &&
478 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
479 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
481 if (strcmp (iso639, iso639_to_check) == 0 &&
482 ((iso3166_to_check != NULL &&
483 strcmp (iso3166, iso3166_to_check) == 0) ||
484 (iso3166_to_check == NULL &&
485 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
487 char language[100], country[100];
490 if (script_to_check != NULL)
492 /* If lcid is the "other" script for this language,
493 * return TRUE, i.e. continue looking.
495 if (strcmp (script_to_check, "Latn") == 0)
497 switch (LANGIDFROMLCID (lcid))
499 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
501 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
503 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
505 case MAKELANGID (LANG_SERBIAN, 0x07):
506 /* Serbian in Bosnia and Herzegovina, Cyrillic */
510 else if (strcmp (script_to_check, "Cyrl") == 0)
512 switch (LANGIDFROMLCID (lcid))
514 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
516 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
518 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
520 case MAKELANGID (LANG_SERBIAN, 0x06):
521 /* Serbian in Bosnia and Herzegovina, Latin */
527 SetThreadLocale (lcid);
529 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
530 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
532 strcpy (locale, language);
533 strcat (locale, "_");
534 strcat (locale, country);
536 if (setlocale (LC_ALL, locale) != NULL)
537 setlocale_called = TRUE;
550 setlocale_initialization (void)
552 static gboolean initialized = FALSE;
561 /* If some of the POSIXish environment variables are set, set
562 * the Win32 thread locale correspondingly.
564 char *p = getenv ("LC_ALL");
571 if (strcmp (p, "C") == 0)
572 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
575 /* Check if one of the supported locales match the
576 * environment variable. If so, use that locale.
579 iso3166_to_check = strchr (iso639_to_check, '_');
580 if (iso3166_to_check != NULL)
582 *iso3166_to_check++ = '\0';
584 script_to_check = strchr (iso3166_to_check, '@');
585 if (script_to_check != NULL)
586 *script_to_check++ = '\0';
588 /* Handle special cases. */
590 /* The standard code for Serbia and Montenegro was
591 * "CS", but MSFT uses for some reason "SP". By now
592 * (October 2006), SP has split into two, "RS" and
593 * "ME", but don't bother trying to handle those
594 * yet. Do handle the even older "YU", though.
596 if (strcmp (iso3166_to_check, "CS") == 0 ||
597 strcmp (iso3166_to_check, "YU") == 0)
598 iso3166_to_check = "SP";
602 script_to_check = strchr (iso639_to_check, '@');
603 if (script_to_check != NULL)
604 *script_to_check++ = '\0';
605 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
606 if (strcmp (iso639_to_check, "sr") == 0)
607 iso3166_to_check = "SP";
610 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
614 if (!setlocale_called)
615 setlocale (LC_ALL, "");
617 if (!setlocale (LC_ALL, ""))
618 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
624 do_pre_parse_initialization (int *argc,
627 const gchar *env_string;
632 pre_initialized = TRUE;
634 if (_gtk_module_has_mixed_deps (NULL))
635 g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
637 gdk_pre_parse_libgtk_only ();
638 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
640 #ifdef G_ENABLE_DEBUG
641 env_string = g_getenv ("GTK_DEBUG");
642 if (env_string != NULL)
644 debug_flags = g_parse_debug_string (env_string,
646 G_N_ELEMENTS (gtk_debug_keys));
649 #endif /* G_ENABLE_DEBUG */
651 env_string = g_getenv ("GTK_MODULES");
653 gtk_modules_string = g_string_new (env_string);
657 gettext_initialization (void)
659 setlocale_initialization ();
662 bindtextdomain (GETTEXT_PACKAGE, _gtk_get_localedir ());
663 bindtextdomain (GETTEXT_PACKAGE "-properties", _gtk_get_localedir ());
664 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
665 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
666 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
671 /* XXX: Remove me after getting rid of gail */
672 extern void _gtk_accessibility_init (void);
675 do_post_parse_initialization (int *argc,
681 gettext_initialization ();
684 signal (SIGPIPE, SIG_IGN);
687 if (g_fatal_warnings)
689 GLogLevelFlags fatal_mask;
691 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
692 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
693 g_log_set_always_fatal (fatal_mask);
696 if (debug_flags & GTK_DEBUG_UPDATES)
697 gdk_window_set_debug_updates (TRUE);
700 /* Translate to default:RTL if you want your widgets
701 * to be RTL, otherwise translate to default:LTR.
702 * Do *not* translate it to "predefinito:LTR", if it
703 * it isn't default:LTR or default:RTL it will not work
705 char *e = _("default:LTR");
706 if (strcmp (e, "default:RTL")==0)
707 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
708 else if (strcmp (e, "default:LTR"))
709 g_warning ("Whoever translated default:LTR did so wrongly.\n");
712 _gtk_register_resource ();
714 /* do what the call to gtk_type_init() used to do */
717 _gtk_accel_map_init ();
719 /* Set the 'initialized' flag.
721 gtk_initialized = TRUE;
723 /* load gtk modules */
724 if (gtk_modules_string)
726 _gtk_modules_init (argc, argv, gtk_modules_string->str);
727 g_string_free (gtk_modules_string, TRUE);
731 _gtk_modules_init (argc, argv, NULL);
734 _gtk_accessibility_init ();
740 gboolean open_default_display;
744 pre_parse_hook (GOptionContext *context,
749 do_pre_parse_initialization (NULL, NULL);
755 post_parse_hook (GOptionContext *context,
760 OptionGroupInfo *info = data;
763 do_post_parse_initialization (NULL, NULL);
765 if (info->open_default_display)
767 if (gdk_display_open_default_libgtk_only () == NULL)
769 const char *display_name = gdk_get_display_arg_name ();
772 G_OPTION_ERROR_FAILED,
773 _("Cannot open display: %s"),
774 display_name ? display_name : "" );
785 * gtk_get_debug_flags:
787 * Returns the GTK+ debug flags.
789 * This function is intended for GTK+ modules that want
790 * to adjust their debug output based on GTK+ debug flags.
792 * Returns: the GTK+ debug flags.
795 gtk_get_debug_flags (void)
801 * gtk_set_debug_flags:
803 * Sets the GTK+ debug flags.
806 gtk_set_debug_flags (guint flags)
812 * gtk_get_option_group: (skip)
813 * @open_default_display: whether to open the default display
814 * when parsing the commandline arguments
816 * Returns a #GOptionGroup for the commandline arguments recognized
819 * You should add this group to your #GOptionContext
820 * with g_option_context_add_group(), if you are using
821 * g_option_context_parse() to parse your commandline arguments.
823 * Returns: a #GOptionGroup for the commandline arguments recognized
829 gtk_get_option_group (gboolean open_default_display)
832 OptionGroupInfo *info;
834 gettext_initialization ();
836 info = g_new0 (OptionGroupInfo, 1);
837 info->open_default_display = open_default_display;
839 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
840 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
842 gdk_add_option_entries_libgtk_only (group);
843 g_option_group_add_entries (group, gtk_args);
844 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
850 * gtk_init_with_args:
851 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
852 * your main() function (or 0 if @argv is %NULL). This will be changed if
853 * any arguments were handled.
854 * @argv: (array length=argc) (inout) (allow-none): Address of the
855 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
856 * understood by GTK+ are stripped before return.
857 * @parameter_string: a string which is displayed in
858 * the first line of <option>--help</option> output, after
859 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
860 * @entries: (array zero-terminated=1): a %NULL-terminated array
861 * of #GOptionEntrys describing the options of your program
862 * @translation_domain: a translation domain to use for translating
863 * the <option>--help</option> output for the options in @entries
864 * and the @parameter_string with gettext(), or %NULL
865 * @error: a return location for errors
867 * This function does the same work as gtk_init_check().
868 * Additionally, it allows you to add your own commandline options,
869 * and it automatically generates nicely formatted
870 * <option>--help</option> output. Note that your program will
871 * be terminated after writing out the help output.
873 * Returns: %TRUE if the windowing system has been successfully
874 * initialized, %FALSE otherwise
879 gtk_init_with_args (gint *argc,
881 const gchar *parameter_string,
882 const GOptionEntry *entries,
883 const gchar *translation_domain,
886 GOptionContext *context;
887 GOptionGroup *gtk_group;
891 return gdk_display_open_default_libgtk_only () != NULL;
893 gettext_initialization ();
895 if (!check_setugid ())
898 gtk_group = gtk_get_option_group (TRUE);
900 context = g_option_context_new (parameter_string);
901 g_option_context_add_group (context, gtk_group);
902 g_option_context_set_translation_domain (context, translation_domain);
905 g_option_context_add_main_entries (context, entries, translation_domain);
906 retval = g_option_context_parse (context, argc, argv, error);
908 g_option_context_free (context);
916 * @argc: (inout): a pointer to the number of command line arguments
917 * @argv: (array length=argc) (inout): a pointer to the array of
918 * command line arguments
920 * Parses command line arguments, and initializes global
921 * attributes of GTK+, but does not actually open a connection
922 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
924 * Any arguments used by GTK+ or GDK are removed from the array and
925 * @argc and @argv are updated accordingly.
927 * There is no need to call this function explicitely if you are using
928 * gtk_init(), or gtk_init_check().
930 * Return value: %TRUE if initialization succeeded, otherwise %FALSE
933 gtk_parse_args (int *argc,
936 GOptionContext *option_context;
937 GOptionGroup *gtk_group;
938 GError *error = NULL;
943 gettext_initialization ();
945 if (!check_setugid ())
948 option_context = g_option_context_new (NULL);
949 g_option_context_set_ignore_unknown_options (option_context, TRUE);
950 g_option_context_set_help_enabled (option_context, FALSE);
951 gtk_group = gtk_get_option_group (FALSE);
952 g_option_context_set_main_group (option_context, gtk_group);
953 if (!g_option_context_parse (option_context, argc, argv, &error))
955 g_warning ("%s", error->message);
956 g_error_free (error);
959 g_option_context_free (option_context);
964 #ifdef G_PLATFORM_WIN32
965 #undef gtk_init_check
970 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
971 * your main() function (or 0 if @argv is %NULL). This will be changed if
972 * any arguments were handled.
973 * @argv: (array length=argc) (inout) (allow-none): Address of the
974 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
975 * understood by GTK+ are stripped before return.
977 * This function does the same work as gtk_init() with only a single
978 * change: It does not terminate the program if the windowing system
979 * can't be initialized. Instead it returns %FALSE on failure.
981 * This way the application can fall back to some other means of
982 * communication with the user - for example a curses or command line
985 * Return value: %TRUE if the windowing system has been successfully
986 * initialized, %FALSE otherwise
989 gtk_init_check (int *argc,
992 if (!gtk_parse_args (argc, argv))
995 return gdk_display_open_default_libgtk_only () != NULL;
998 #ifdef G_PLATFORM_WIN32
1004 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1005 * your main() function (or 0 if @argv is %NULL). This will be changed if
1006 * any arguments were handled.
1007 * @argv: (array length=argc) (inout) (allow-none): Address of the
1008 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
1009 * understood by GTK+ are stripped before return.
1011 * Call this function before using any other GTK+ functions in your GUI
1012 * applications. It will initialize everything needed to operate the
1013 * toolkit and parses some standard command line options.
1015 * Although you are expected to pass the @argc, @argv parameters from main() to
1016 * this function, it is possible to pass %NULL if @argv is not available or
1017 * commandline handling is not required.
1019 * @argc and @argv are adjusted accordingly so your own code will
1020 * never see those standard arguments.
1022 * Note that there are some alternative ways to initialize GTK+:
1023 * if you are calling gtk_parse_args(), gtk_init_check(),
1024 * gtk_init_with_args() or g_option_context_parse() with
1025 * the option group returned by gtk_get_option_group(),
1026 * you <emphasis>don't</emphasis> have to call gtk_init().
1029 * This function will terminate your program if it was unable to
1030 * initialize the windowing system for some reason. If you want
1031 * your program to fall back to a textual interface you want to
1032 * call gtk_init_check() instead.
1036 * Since 2.18, GTK+ calls <literal>signal (SIGPIPE, SIG_IGN)</literal>
1037 * during initialization, to ignore SIGPIPE signals, since these are
1038 * almost never wanted in graphical applications. If you do need to
1039 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1040 * but notice that other libraries (e.g. libdbus or gvfs) might do
1045 gtk_init (int *argc, char ***argv)
1047 if (!gtk_init_check (argc, argv))
1049 const char *display_name_arg = gdk_get_display_arg_name ();
1050 if (display_name_arg == NULL)
1051 display_name_arg = getenv("DISPLAY");
1052 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1059 /* This is relevant when building with gcc for Windows (MinGW),
1060 * where we want to be struct packing compatible with MSVC,
1061 * i.e. use the -mms-bitfields switch.
1062 * For Cygwin there should be no need to be compatible with MSVC,
1063 * so no need to use G_PLATFORM_WIN32.
1067 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1069 if (sizeof_GtkWindow != sizeof (GtkWindow))
1070 g_error ("Incompatible build!\n"
1071 "The code using GTK+ thinks GtkWindow is of different\n"
1072 "size than it actually is in this build of GTK+.\n"
1073 "On Windows, this probably means that you have compiled\n"
1074 "your code with gcc without the -mms-bitfields switch,\n"
1075 "or that you are using an unsupported compiler.");
1078 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1079 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1080 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
1081 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1082 * -fnative-struct still matters. GtkBox is one such.
1085 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1087 if (sizeof_GtkBox != sizeof (GtkBox))
1088 g_error ("Incompatible build!\n"
1089 "The code using GTK+ thinks GtkBox is of different\n"
1090 "size than it actually is in this build of GTK+.\n"
1091 "On Windows, this probably means that you have compiled\n"
1092 "your code with gcc without the -mms-bitfields switch,\n"
1093 "or that you are using an unsupported compiler.");
1096 /* These two functions might get more checks added later, thus pass
1097 * in the number of extra args.
1100 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1102 check_sizeof_GtkWindow (sizeof_GtkWindow);
1103 if (num_checks >= 2)
1104 check_sizeof_GtkBox (sizeof_GtkBox);
1105 gtk_init (argc, argv);
1109 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1111 check_sizeof_GtkWindow (sizeof_GtkWindow);
1112 if (num_checks >= 2)
1113 check_sizeof_GtkBox (sizeof_GtkBox);
1114 return gtk_init_check (argc, argv);
1120 * gtk_get_default_language:
1122 * Returns the #PangoLanguage for the default language currently in
1123 * effect. (Note that this can change over the life of an
1124 * application.) The default language is derived from the current
1125 * locale. It determines, for example, whether GTK+ uses the
1126 * right-to-left or left-to-right text direction.
1128 * This function is equivalent to pango_language_get_default().
1129 * See that function for details.
1131 * Return value: the default language as a #PangoLanguage,
1135 gtk_get_default_language (void)
1137 return pango_language_get_default ();
1143 * Runs the main loop until gtk_main_quit() is called.
1145 * You can nest calls to gtk_main(). In that case gtk_main_quit()
1146 * will make the innermost invocation of the main loop return.
1153 gtk_main_loop_level++;
1155 loop = g_main_loop_new (NULL, TRUE);
1156 main_loops = g_slist_prepend (main_loops, loop);
1158 if (g_main_loop_is_running (main_loops->data))
1160 GDK_THREADS_LEAVE ();
1161 g_main_loop_run (loop);
1162 GDK_THREADS_ENTER ();
1166 main_loops = g_slist_remove (main_loops, loop);
1168 g_main_loop_unref (loop);
1170 gtk_main_loop_level--;
1172 if (gtk_main_loop_level == 0)
1174 /* Try storing all clipboard data we have */
1175 _gtk_clipboard_store_all ();
1177 /* Synchronize the recent manager singleton */
1178 _gtk_recent_manager_sync ();
1185 * Asks for the current nesting level of the main loop.
1187 * Returns: the nesting level of the current invocation
1191 gtk_main_level (void)
1193 return gtk_main_loop_level;
1199 * Makes the innermost invocation of the main loop return
1200 * when it regains control.
1203 gtk_main_quit (void)
1205 g_return_if_fail (main_loops != NULL);
1207 g_main_loop_quit (main_loops->data);
1211 * gtk_events_pending:
1213 * Checks if any events are pending.
1215 * This can be used to update the UI and invoke timeouts etc.
1216 * while doing some time intensive computation.
1219 * <title>Updating the UI during a long computation</title>
1221 * /* computation going on... */
1223 * while (gtk_events_pending ())
1224 * gtk_main_iteration ();
1226 * /* ...computation continued */
1230 * Returns: %TRUE if any events are pending, %FALSE otherwise
1233 gtk_events_pending (void)
1237 GDK_THREADS_LEAVE ();
1238 result = g_main_context_pending (NULL);
1239 GDK_THREADS_ENTER ();
1245 * gtk_main_iteration:
1247 * Runs a single iteration of the mainloop.
1249 * If no events are waiting to be processed GTK+ will block
1250 * until the next event is noticed. If you don't want to block
1251 * look at gtk_main_iteration_do() or check if any events are
1252 * pending with gtk_events_pending() first.
1254 * Returns: %TRUE if gtk_main_quit() has been called for the
1255 * innermost mainloop
1258 gtk_main_iteration (void)
1260 GDK_THREADS_LEAVE ();
1261 g_main_context_iteration (NULL, TRUE);
1262 GDK_THREADS_ENTER ();
1265 return !g_main_loop_is_running (main_loops->data);
1271 * gtk_main_iteration_do:
1272 * @blocking: %TRUE if you want GTK+ to block if no events are pending
1274 * Runs a single iteration of the mainloop.
1275 * If no events are available either return or block depending on
1276 * the value of @blocking.
1278 * Returns: %TRUE if gtk_main_quit() has been called for the
1279 * innermost mainloop
1282 gtk_main_iteration_do (gboolean blocking)
1284 GDK_THREADS_LEAVE ();
1285 g_main_context_iteration (NULL, blocking);
1286 GDK_THREADS_ENTER ();
1289 return !g_main_loop_is_running (main_loops->data);
1294 /* private libgtk to libgdk interfaces */
1295 gboolean gdk_device_grab_info_libgtk_only (GdkDisplay *display,
1297 GdkWindow **grab_window,
1298 gboolean *owner_events);
1301 rewrite_events_translate (GdkWindow *old_window,
1302 GdkWindow *new_window,
1306 gint old_origin_x, old_origin_y;
1307 gint new_origin_x, new_origin_y;
1309 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1310 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1312 *x += old_origin_x - new_origin_x;
1313 *y += old_origin_y - new_origin_y;
1317 rewrite_event_for_window (GdkEvent *event,
1318 GdkWindow *new_window)
1320 event = gdk_event_copy (event);
1322 switch (event->type)
1325 rewrite_events_translate (event->any.window,
1327 &event->scroll.x, &event->scroll.y);
1329 case GDK_BUTTON_PRESS:
1330 case GDK_2BUTTON_PRESS:
1331 case GDK_3BUTTON_PRESS:
1332 case GDK_BUTTON_RELEASE:
1333 rewrite_events_translate (event->any.window,
1335 &event->button.x, &event->button.y);
1337 case GDK_MOTION_NOTIFY:
1338 rewrite_events_translate (event->any.window,
1340 &event->motion.x, &event->motion.y);
1343 case GDK_KEY_RELEASE:
1344 case GDK_PROXIMITY_IN:
1345 case GDK_PROXIMITY_OUT:
1352 g_object_unref (event->any.window);
1353 event->any.window = g_object_ref (new_window);
1358 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1359 * then what X11 does is deliver the event normally if it was going to this
1360 * client, otherwise, delivers it in terms of the grab window. This function
1361 * rewrites events to the effect that events going to the same window group
1362 * are delivered normally, otherwise, the event is delivered in terms of the
1366 rewrite_event_for_grabs (GdkEvent *event)
1368 GdkWindow *grab_window;
1369 GtkWidget *event_widget, *grab_widget;
1370 gpointer grab_widget_ptr;
1371 gboolean owner_events;
1372 GdkDisplay *display;
1375 switch (event->type)
1378 case GDK_BUTTON_PRESS:
1379 case GDK_2BUTTON_PRESS:
1380 case GDK_3BUTTON_PRESS:
1381 case GDK_BUTTON_RELEASE:
1382 case GDK_MOTION_NOTIFY:
1383 case GDK_PROXIMITY_IN:
1384 case GDK_PROXIMITY_OUT:
1386 case GDK_KEY_RELEASE:
1387 display = gdk_window_get_display (event->any.window);
1388 device = gdk_event_get_device (event);
1390 if (!gdk_device_grab_info_libgtk_only (display, device, &grab_window, &owner_events) ||
1398 event_widget = gtk_get_event_widget (event);
1399 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1400 grab_widget = grab_widget_ptr;
1403 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1404 return rewrite_event_for_window (event, grab_window);
1410 * gtk_main_do_event:
1411 * @event: An event to process (normally passed by GDK)
1413 * Processes a single GDK event.
1415 * This is public only to allow filtering of events between GDK and GTK+.
1416 * You will not usually need to call this function directly.
1418 * While you should not call this function directly, you might want to
1419 * know how exactly events are handled. So here is what this function
1420 * does with the event:
1424 * Compress enter/leave notify events. If the event passed build an
1425 * enter/leave pair together with the next event (peeked from GDK), both
1426 * events are thrown away. This is to avoid a backlog of (de-)highlighting
1427 * widgets crossed by the pointer.
1428 * </para></listitem>
1430 * Find the widget which got the event. If the widget can't be determined
1431 * the event is thrown away unless it belongs to a INCR transaction. In that
1432 * case it is passed to gtk_selection_incr_event().
1433 * </para></listitem>
1435 * Then the event is pushed onto a stack so you can query the currently
1436 * handled event with gtk_get_current_event().
1437 * </para></listitem>
1439 * The event is sent to a widget. If a grab is active all events for widgets
1440 * that are not in the contained in the grab widget are sent to the latter
1441 * with a few exceptions:
1444 * Deletion and destruction events are still sent to the event widget for
1446 * </para></listitem>
1448 * Events which directly relate to the visual representation of the event
1450 * </para></listitem>
1452 * Leave events are delivered to the event widget if there was an enter
1453 * event delivered to it before without the paired leave event.
1454 * </para></listitem>
1456 * Drag events are not redirected because it is unclear what the semantics
1458 * </para></listitem>
1460 * Another point of interest might be that all key events are first passed
1461 * through the key snooper functions if there are any. Read the description
1462 * of gtk_key_snooper_install() if you need this feature.
1463 * </para></listitem>
1465 * After finishing the delivery the event is popped from the event stack.
1466 * </para></listitem>
1470 gtk_main_do_event (GdkEvent *event)
1472 GtkWidget *event_widget;
1473 GtkWidget *grab_widget = NULL;
1474 GtkWindowGroup *window_group;
1475 GdkEvent *rewritten_event = NULL;
1479 if (event->type == GDK_SETTING)
1481 _gtk_settings_handle_event (&event->setting);
1485 if (event->type == GDK_OWNER_CHANGE)
1487 _gtk_clipboard_handle_event (&event->owner_change);
1491 /* Find the widget which got the event. We store the widget
1492 * in the user_data field of GdkWindow's. Ignore the event
1493 * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1494 * events which are handled specially. Though this happens rarely,
1495 * bogus events can occur for e.g. destroyed GdkWindows.
1497 event_widget = gtk_get_event_widget (event);
1500 /* To handle selection INCR transactions, we select
1501 * PropertyNotify events on the requestor window and create
1502 * a corresponding (fake) GdkWindow so that events get here.
1503 * There won't be a widget though, so we have to handle
1506 if (event->type == GDK_PROPERTY_NOTIFY)
1507 _gtk_selection_incr_event (event->any.window,
1513 /* If pointer or keyboard grabs are in effect, munge the events
1514 * so that each window group looks like a separate app.
1516 rewritten_event = rewrite_event_for_grabs (event);
1517 if (rewritten_event)
1519 event = rewritten_event;
1520 event_widget = gtk_get_event_widget (event);
1523 window_group = gtk_main_get_window_group (event_widget);
1524 device = gdk_event_get_device (event);
1526 /* check whether there is a (device) grab in effect... */
1528 grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1531 grab_widget = gtk_window_group_get_current_grab (window_group);
1533 /* If the grab widget is an ancestor of the event widget
1534 * then we send the event to the original event widget.
1535 * This is the key to implementing modality.
1538 (gtk_widget_is_sensitive (event_widget) &&
1539 gtk_widget_is_ancestor (event_widget, grab_widget)))
1540 grab_widget = event_widget;
1542 /* If the widget receiving events is actually blocked by another
1546 _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1548 if (rewritten_event)
1549 gdk_event_free (rewritten_event);
1554 /* Push the event onto a stack of current events for
1555 * gtk_current_event_get().
1557 current_events = g_list_prepend (current_events, event);
1559 /* Not all events get sent to the grabbing widget.
1560 * The delete, destroy, expose, focus change and resize
1561 * events still get sent to the event widget because
1562 * 1) these events have no meaning for the grabbing widget
1563 * and 2) redirecting these events to the grabbing widget
1564 * could cause the display to be messed up.
1566 * Drag events are also not redirected, since it isn't
1567 * clear what the semantics of that would be.
1569 switch (event->type)
1575 g_object_ref (event_widget);
1576 if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1577 !gtk_widget_event (event_widget, event))
1578 gtk_widget_destroy (event_widget);
1579 g_object_unref (event_widget);
1583 /* Unexpected GDK_DESTROY from the outside, ignore for
1584 * child windows, handle like a GDK_DELETE for toplevels
1586 if (!gtk_widget_get_parent (event_widget))
1588 g_object_ref (event_widget);
1589 if (!gtk_widget_event (event_widget, event) &&
1590 gtk_widget_get_realized (event_widget))
1591 gtk_widget_destroy (event_widget);
1592 g_object_unref (event_widget);
1597 if (event->any.window && gtk_widget_get_double_buffered (event_widget))
1599 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1600 gtk_widget_send_expose (event_widget, event);
1601 gdk_window_end_paint (event->any.window);
1605 /* The app may paint with a previously allocated cairo_t,
1606 * which will draw directly to the window. We can't catch cairo
1607 * draw operations to automatically flush the window, thus we
1608 * need to explicitly flush any outstanding moves or double
1611 gdk_window_flush (event->any.window);
1612 gtk_widget_send_expose (event_widget, event);
1616 case GDK_PROPERTY_NOTIFY:
1617 case GDK_FOCUS_CHANGE:
1621 case GDK_SELECTION_CLEAR:
1622 case GDK_SELECTION_REQUEST:
1623 case GDK_SELECTION_NOTIFY:
1624 case GDK_CLIENT_EVENT:
1625 case GDK_VISIBILITY_NOTIFY:
1626 case GDK_WINDOW_STATE:
1627 case GDK_GRAB_BROKEN:
1629 gtk_widget_event (event_widget, event);
1633 case GDK_BUTTON_PRESS:
1634 case GDK_2BUTTON_PRESS:
1635 case GDK_3BUTTON_PRESS:
1636 gtk_propagate_event (grab_widget, event);
1640 case GDK_KEY_RELEASE:
1641 if (gtk_invoke_key_snoopers (grab_widget, event))
1644 /* make focus visible in a window that receives a key event */
1647 GtkPolicyType visible_focus;
1649 window = gtk_widget_get_toplevel (grab_widget);
1650 g_object_get (gtk_widget_get_settings (grab_widget), "gtk-visible-focus", &visible_focus, NULL);
1651 if (GTK_IS_WINDOW (window) && visible_focus != GTK_POLICY_NEVER)
1652 gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
1655 /* Catch alt press to enable auto-mnemonics;
1656 * menus are handled elsewhere
1657 * FIXME: this does not work with mnemonic modifiers other than Alt
1659 if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1660 ((event->key.state & (gtk_accelerator_get_default_mod_mask ()) & ~(GDK_RELEASE_MASK|GDK_MOD1_MASK)) == 0) &&
1661 !GTK_IS_MENU_SHELL (grab_widget))
1663 gboolean auto_mnemonics;
1665 g_object_get (gtk_widget_get_settings (grab_widget),
1666 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
1670 gboolean mnemonics_visible;
1673 mnemonics_visible = (event->type == GDK_KEY_PRESS);
1675 window = gtk_widget_get_toplevel (grab_widget);
1676 if (GTK_IS_WINDOW (window))
1677 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), mnemonics_visible);
1680 /* else fall through */
1681 case GDK_MOTION_NOTIFY:
1682 case GDK_BUTTON_RELEASE:
1683 case GDK_PROXIMITY_IN:
1684 case GDK_PROXIMITY_OUT:
1685 gtk_propagate_event (grab_widget, event);
1688 case GDK_ENTER_NOTIFY:
1689 if (event->crossing.detail != GDK_NOTIFY_VIRTUAL &&
1690 event->crossing.detail != GDK_NOTIFY_NONLINEAR_VIRTUAL)
1691 _gtk_widget_set_device_window (event_widget,
1692 gdk_event_get_device (event),
1694 if (gtk_widget_is_sensitive (grab_widget))
1695 gtk_widget_event (grab_widget, event);
1698 case GDK_LEAVE_NOTIFY:
1699 if (event->crossing.detail != GDK_NOTIFY_VIRTUAL &&
1700 event->crossing.detail != GDK_NOTIFY_NONLINEAR_VIRTUAL)
1701 _gtk_widget_set_device_window (event_widget,
1702 gdk_event_get_device (event),
1704 if (gtk_widget_is_sensitive (grab_widget))
1705 gtk_widget_event (grab_widget, event);
1708 case GDK_DRAG_STATUS:
1709 case GDK_DROP_FINISHED:
1710 _gtk_drag_source_handle_event (event_widget, event);
1712 case GDK_DRAG_ENTER:
1713 case GDK_DRAG_LEAVE:
1714 case GDK_DRAG_MOTION:
1715 case GDK_DROP_START:
1716 _gtk_drag_dest_handle_event (event_widget, event);
1719 g_assert_not_reached ();
1723 if (event->type == GDK_ENTER_NOTIFY
1724 || event->type == GDK_LEAVE_NOTIFY
1725 || event->type == GDK_BUTTON_PRESS
1726 || event->type == GDK_2BUTTON_PRESS
1727 || event->type == GDK_3BUTTON_PRESS
1728 || event->type == GDK_KEY_PRESS
1729 || event->type == GDK_DRAG_ENTER
1730 || event->type == GDK_GRAB_BROKEN
1731 || event->type == GDK_MOTION_NOTIFY
1732 || event->type == GDK_SCROLL)
1734 _gtk_tooltip_handle_event (event);
1737 tmp_list = current_events;
1738 current_events = g_list_remove_link (current_events, tmp_list);
1739 g_list_free_1 (tmp_list);
1741 if (rewritten_event)
1742 gdk_event_free (rewritten_event);
1748 * All this function does it to return %TRUE.
1750 * This can be useful for example if you want to inhibit the deletion
1751 * of a window. Of course you should not do this as the user expects
1752 * a reaction from clicking the close icon of the window...
1755 * <title>A persistent window</title>
1757 * #include <gtk/gtk.h><
1760 * main (int argc, char **argv)
1762 * GtkWidget *win, *but;
1764 * gtk_init (&argc, &argv);
1766 * win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1767 * g_signal_connect (win, "delete-event",
1768 * G_CALLBACK (gtk_true), NULL);
1769 * g_signal_connect (win, "destroy",
1770 * G_CALLBACK (gtk_main_quit), NULL);
1772 * but = gtk_button_new_with_label ("Close yourself. I mean it!");
1773 * g_signal_connect_swapped (but, "clicked",
1774 * G_CALLBACK (gtk_object_destroy), win);
1775 * gtk_container_add (GTK_CONTAINER (win), but);
1777 * gtk_widget_show_all (win);
1797 * Analogical to gtk_true(), this function does nothing
1798 * but always returns %FALSE.
1808 static GtkWindowGroup *
1809 gtk_main_get_window_group (GtkWidget *widget)
1811 GtkWidget *toplevel = NULL;
1814 toplevel = gtk_widget_get_toplevel (widget);
1816 if (GTK_IS_WINDOW (toplevel))
1817 return gtk_window_get_group (GTK_WINDOW (toplevel));
1819 return gtk_window_get_group (NULL);
1824 GtkWidget *old_grab_widget;
1825 GtkWidget *new_grab_widget;
1826 gboolean was_grabbed;
1827 gboolean is_grabbed;
1829 GList *notified_windows;
1834 synth_crossing_for_grab_notify (GtkWidget *from,
1836 GrabNotifyInfo *info,
1838 GdkCrossingMode mode)
1842 GdkDevice *device = devices->data;
1843 GdkWindow *from_window, *to_window;
1845 /* Do not propagate events more than once to
1846 * the same windows if non-multidevice aware.
1852 from_window = _gtk_widget_get_device_window (from, device);
1855 !gdk_window_get_support_multidevice (from_window) &&
1856 g_list_find (info->notified_windows, from_window))
1864 to_window = _gtk_widget_get_device_window (to, device);
1867 !gdk_window_get_support_multidevice (to_window) &&
1868 g_list_find (info->notified_windows, to_window))
1872 if (from_window || to_window)
1874 _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
1875 (to_window) ? to : NULL,
1879 info->notified_windows = g_list_prepend (info->notified_windows, from_window);
1882 info->notified_windows = g_list_prepend (info->notified_windows, to_window);
1885 devices = devices->next;
1890 gtk_grab_notify_foreach (GtkWidget *child,
1893 GrabNotifyInfo *info = data;
1894 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
1897 was_grabbed = info->was_grabbed;
1898 is_grabbed = info->is_grabbed;
1900 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
1901 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
1903 was_shadowed = info->old_grab_widget && !info->was_grabbed;
1904 is_shadowed = info->new_grab_widget && !info->is_grabbed;
1906 g_object_ref (child);
1908 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
1909 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1912 _gtk_widget_get_device_window (child, info->device))
1914 /* Device specified and is on widget */
1915 devices = g_list_prepend (NULL, info->device);
1918 devices = _gtk_widget_list_devices (child);
1922 _gtk_widget_set_shadowed (child, TRUE);
1923 if (!was_shadowed && devices &&
1924 gtk_widget_is_sensitive (child))
1925 synth_crossing_for_grab_notify (child, info->new_grab_widget,
1927 GDK_CROSSING_GTK_GRAB);
1931 _gtk_widget_set_shadowed (child, FALSE);
1932 if (was_shadowed && devices &&
1933 gtk_widget_is_sensitive (child))
1934 synth_crossing_for_grab_notify (info->old_grab_widget, child,
1936 info->from_grab ? GDK_CROSSING_GTK_GRAB :
1937 GDK_CROSSING_GTK_UNGRAB);
1940 if (was_shadowed != is_shadowed)
1941 _gtk_widget_grab_notify (child, was_shadowed);
1943 g_object_unref (child);
1944 g_list_free (devices);
1946 info->was_grabbed = was_grabbed;
1947 info->is_grabbed = is_grabbed;
1951 gtk_grab_notify (GtkWindowGroup *group,
1953 GtkWidget *old_grab_widget,
1954 GtkWidget *new_grab_widget,
1958 GrabNotifyInfo info = { 0 };
1960 if (old_grab_widget == new_grab_widget)
1963 info.old_grab_widget = old_grab_widget;
1964 info.new_grab_widget = new_grab_widget;
1965 info.from_grab = from_grab;
1966 info.device = device;
1968 g_object_ref (group);
1970 toplevels = gtk_window_list_toplevels ();
1971 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1975 GtkWindow *toplevel = toplevels->data;
1976 toplevels = g_list_delete_link (toplevels, toplevels);
1978 info.was_grabbed = FALSE;
1979 info.is_grabbed = FALSE;
1981 if (group == gtk_window_get_group (toplevel))
1982 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
1983 g_object_unref (toplevel);
1986 g_list_free (info.notified_windows);
1987 g_object_unref (group);
1991 * gtk_grab_add: (method)
1992 * @widget: The widget that grabs keyboard and pointer events
1994 * Makes @widget the current grabbed widget.
1996 * This means that interaction with other widgets in the same
1997 * application is blocked and mouse as well as keyboard events
1998 * are delivered to this widget.
2000 * If @widget is not sensitive, it is not set as the current
2001 * grabbed widget and this function does nothing.
2004 gtk_grab_add (GtkWidget *widget)
2006 GtkWindowGroup *group;
2007 GtkWidget *old_grab_widget;
2009 g_return_if_fail (widget != NULL);
2011 if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2013 _gtk_widget_set_has_grab (widget, TRUE);
2015 group = gtk_main_get_window_group (widget);
2017 old_grab_widget = gtk_window_group_get_current_grab (group);
2019 g_object_ref (widget);
2020 _gtk_window_group_add_grab (group, widget);
2022 gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2027 * gtk_grab_get_current:
2029 * Queries the current grab of the default window group.
2031 * Return value: (transfer none): The widget which currently
2032 * has the grab or %NULL if no grab is active
2035 gtk_grab_get_current (void)
2037 GtkWindowGroup *group;
2039 group = gtk_main_get_window_group (NULL);
2041 return gtk_window_group_get_current_grab (group);
2045 * gtk_grab_remove: (method)
2046 * @widget: The widget which gives up the grab
2048 * Removes the grab from the given widget.
2050 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2052 * If @widget does not have the grab, this function does nothing.
2055 gtk_grab_remove (GtkWidget *widget)
2057 GtkWindowGroup *group;
2058 GtkWidget *new_grab_widget;
2060 g_return_if_fail (widget != NULL);
2062 if (gtk_widget_has_grab (widget))
2064 _gtk_widget_set_has_grab (widget, FALSE);
2066 group = gtk_main_get_window_group (widget);
2067 _gtk_window_group_remove_grab (group, widget);
2068 new_grab_widget = gtk_window_group_get_current_grab (group);
2070 gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2072 g_object_unref (widget);
2077 * gtk_device_grab_add:
2078 * @widget: a #GtkWidget
2079 * @device: a #GtkDevice to grab on.
2080 * @block_others: %TRUE to prevent other devices to interact with @widget.
2082 * Adds a GTK+ grab on @device, so all the events on @device and its
2083 * associated pointer or keyboard (if any) are delivered to @widget.
2084 * If the @block_others parameter is %TRUE, any other devices will be
2085 * unable to interact with @widget during the grab.
2090 gtk_device_grab_add (GtkWidget *widget,
2092 gboolean block_others)
2094 GtkWindowGroup *group;
2095 GtkWidget *old_grab_widget;
2097 g_return_if_fail (GTK_IS_WIDGET (widget));
2098 g_return_if_fail (GDK_IS_DEVICE (device));
2100 group = gtk_main_get_window_group (widget);
2101 old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2103 if (old_grab_widget != widget)
2104 _gtk_window_group_add_device_grab (group, widget, device, block_others);
2106 gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2110 * gtk_device_grab_remove:
2111 * @widget: a #GtkWidget
2112 * @device: a #GdkDevice
2114 * Removes a device grab from the given widget.
2116 * You have to pair calls to gtk_device_grab_add() and
2117 * gtk_device_grab_remove().
2122 gtk_device_grab_remove (GtkWidget *widget,
2125 GtkWindowGroup *group;
2126 GtkWidget *new_grab_widget;
2128 g_return_if_fail (GTK_IS_WIDGET (widget));
2129 g_return_if_fail (GDK_IS_DEVICE (device));
2131 group = gtk_main_get_window_group (widget);
2132 _gtk_window_group_remove_device_grab (group, widget, device);
2133 new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2135 gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2139 * gtk_key_snooper_install: (skip)
2140 * @snooper: a #GtkKeySnoopFunc
2141 * @func_data: data to pass to @snooper
2143 * Installs a key snooper function, which will get called on all
2144 * key events before delivering them normally.
2146 * Returns: a unique id for this key snooper for use with
2147 * gtk_key_snooper_remove().
2149 * Deprecated: 3.4: Key snooping should not be done. Events should
2150 * be handled by widgets.
2153 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2156 GtkKeySnooperData *data;
2157 static guint snooper_id = 1;
2159 g_return_val_if_fail (snooper != NULL, 0);
2161 data = g_new (GtkKeySnooperData, 1);
2162 data->func = snooper;
2163 data->func_data = func_data;
2164 data->id = snooper_id++;
2165 key_snoopers = g_slist_prepend (key_snoopers, data);
2171 * gtk_key_snooper_remove:
2172 * @snooper_handler_id: Identifies the key snooper to remove
2174 * Removes the key snooper function with the given id.
2176 * Deprecated: 3.4: Key snooping should not be done. Events should
2177 * be handled by widgets.
2180 gtk_key_snooper_remove (guint snooper_id)
2182 GtkKeySnooperData *data = NULL;
2185 slist = key_snoopers;
2189 if (data->id == snooper_id)
2192 slist = slist->next;
2197 key_snoopers = g_slist_remove (key_snoopers, data);
2203 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2207 gint return_val = FALSE;
2209 return_val = _gail_util_key_snooper (grab_widget, (GdkEventKey *) event);
2211 slist = key_snoopers;
2212 while (slist && !return_val)
2214 GtkKeySnooperData *data;
2217 slist = slist->next;
2218 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2225 * gtk_get_current_event:
2227 * Obtains a copy of the event currently being processed by GTK+.
2229 * For example, if you are handling a #GtkButton::clicked signal,
2230 * the current event will be the #GdkEventButton that triggered
2231 * the ::clicked signal.
2233 * Return value: (transfer full): a copy of the current event, or
2234 * %NULL if there is no current event. The returned event must be
2235 * freed with gdk_event_free().
2238 gtk_get_current_event (void)
2241 return gdk_event_copy (current_events->data);
2247 * gtk_get_current_event_time:
2249 * If there is a current event and it has a timestamp,
2250 * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2252 * Return value: the timestamp from the current event,
2253 * or %GDK_CURRENT_TIME.
2256 gtk_get_current_event_time (void)
2259 return gdk_event_get_time (current_events->data);
2261 return GDK_CURRENT_TIME;
2265 * gtk_get_current_event_state:
2266 * @state: (out): a location to store the state of the current event
2268 * If there is a current event and it has a state field, place
2269 * that state field in @state and return %TRUE, otherwise return
2272 * Return value: %TRUE if there was a current event and it
2276 gtk_get_current_event_state (GdkModifierType *state)
2278 g_return_val_if_fail (state != NULL, FALSE);
2281 return gdk_event_get_state (current_events->data, state);
2290 * gtk_get_current_event_device:
2292 * If there is a current event and it has a device, return that
2293 * device, otherwise return %NULL.
2295 * Returns: (transfer none): a #GdkDevice, or %NULL
2298 gtk_get_current_event_device (void)
2301 return gdk_event_get_device (current_events->data);
2307 * gtk_get_event_widget:
2308 * @event: a #GdkEvent
2310 * If @event is %NULL or the event was not associated with any widget,
2311 * returns %NULL, otherwise returns the widget that received the event
2314 * Return value: (transfer none): the widget that originally
2315 * received @event, or %NULL
2318 gtk_get_event_widget (GdkEvent *event)
2321 gpointer widget_ptr;
2324 if (event && event->any.window &&
2325 (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2327 gdk_window_get_user_data (event->any.window, &widget_ptr);
2328 widget = widget_ptr;
2335 * gtk_propagate_event:
2336 * @widget: a #GtkWidget
2339 * Sends an event to a widget, propagating the event to parent widgets
2340 * if the event remains unhandled.
2342 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2343 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2344 * the event may be propagated; if so, this function is used.
2346 * gtk_propagate_event() calls gtk_widget_event() on each widget it
2347 * decides to send the event to. So gtk_widget_event() is the lowest-level
2348 * function; it simply emits the #GtkWidget::event and possibly an
2349 * event-specific signal on a widget. gtk_propagate_event() is a bit
2350 * higher-level, and gtk_main_do_event() is the highest level.
2352 * All that said, you most likely don't want to use any of these
2353 * functions; synthesizing events is rarely needed. There are almost
2354 * certainly better ways to achieve your goals. For example, use
2355 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2356 * of making up expose events.
2359 gtk_propagate_event (GtkWidget *widget,
2364 g_return_if_fail (GTK_IS_WIDGET (widget));
2365 g_return_if_fail (event != NULL);
2367 handled_event = FALSE;
2369 g_object_ref (widget);
2371 if ((event->type == GDK_KEY_PRESS) ||
2372 (event->type == GDK_KEY_RELEASE))
2374 /* Only send key events within Window widgets to the Window
2375 * The Window widget will in turn pass the
2376 * key event on to the currently focused widget
2381 window = gtk_widget_get_toplevel (widget);
2382 if (GTK_IS_WINDOW (window))
2384 /* If there is a grab within the window, give the grab widget
2385 * a first crack at the key event
2387 if (widget != window && gtk_widget_has_grab (widget))
2388 handled_event = gtk_widget_event (widget, event);
2392 window = gtk_widget_get_toplevel (widget);
2393 if (GTK_IS_WINDOW (window))
2395 if (gtk_widget_is_sensitive (window))
2396 gtk_widget_event (window, event);
2400 handled_event = TRUE; /* don't send to widget */
2404 /* Other events get propagated up the widget tree
2405 * so that parents can see the button and motion
2406 * events of the children.
2414 /* Scroll events are special cased here because it
2415 * feels wrong when scrolling a GtkViewport, say,
2416 * to have children of the viewport eat the scroll
2419 if (!gtk_widget_is_sensitive (widget))
2420 handled_event = event->type != GDK_SCROLL;
2422 handled_event = gtk_widget_event (widget, event);
2424 tmp = gtk_widget_get_parent (widget);
2425 g_object_unref (widget);
2429 if (!handled_event && widget)
2430 g_object_ref (widget);
2436 g_object_unref (widget);