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/.
29 * @Short_description: Library initialization, main event loop, and events
30 * @Title: Main loop and Events
31 * @See_also:See the GLib manual, especially #GMainLoop and signal-related
32 * functions such as g_signal_connect()
34 * Before using GTK+, you need to initialize it; initialization connects to the
35 * window system display, and parses some standard command line arguments. The
36 * gtk_init() macro initializes GTK+. gtk_init() exits the application if errors
37 * occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to
38 * recover from a failed GTK+ initialization - you might start up your
39 * application in text mode instead.
41 * Like all GUI toolkits, GTK+ uses an event-driven programming model. When the
42 * user is doing nothing, GTK+ sits in the <firstterm>main loop</firstterm> and
43 * waits for input. If the user performs some action - say, a mouse click - then
44 * the main loop "wakes up" and delivers an event to GTK+. GTK+ forwards the
45 * event to one or more widgets.
47 * When widgets receive an event, they frequently emit one or more
48 * <firstterm>signals</firstterm>. Signals notify your program that "something
49 * interesting happened" by invoking functions you've connected to the signal
50 * with g_signal_connect(). Functions connected to a signal are often termed
51 * <firstterm>callbacks</firstterm>.
53 * When your callbacks are invoked, you would typically take some action - for
54 * example, when an Open button is clicked you might display a
55 * #GtkFileChooserDialog. After a callback finishes, GTK+ will return to the
56 * main loop and await more user input.
59 * <title>Typical <function>main()</function> function for a GTK+ application</title>
62 * main (int argc, char **argv)
64 * /* Initialize i18n support */
67 * /* Initialize the widget set */
68 * gtk_init (&argc, &argv);
70 * /* Create the main window */
71 * mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
73 * /* Set up our GUI elements */
76 * /* Show the application window */
77 * gtk_widget_show_all (mainwin);
79 * /* Enter the main event loop, and wait for user interaction */
82 * /* The user lost interest */
88 * It's OK to use the GLib main loop directly instead of gtk_main(), though it
89 * involves slightly more typing. See #GMainLoop in the GLib documentation.
104 #include <sys/types.h> /* For uid_t, gid_t */
114 #include "gtkaccelmapprivate.h"
116 #include "gtkclipboard.h"
117 #include "gtkdebug.h"
121 #include "gtkmodules.h"
122 #include "gtkmodulesprivate.h"
123 #include "gtkprivate.h"
124 #include "gtkrecentmanager.h"
125 #include "gtkresources.h"
126 #include "gtkselectionprivate.h"
127 #include "gtksettingsprivate.h"
128 #include "gtktooltip.h"
129 #include "gtkversion.h"
130 #include "gtkwidgetprivate.h"
131 #include "gtkwindowprivate.h"
133 #include "a11y/gailutil.h"
135 /* Private type definitions
137 typedef struct _GtkKeySnooperData GtkKeySnooperData;
139 struct _GtkKeySnooperData
141 GtkKeySnoopFunc func;
146 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
149 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
151 static guint gtk_main_loop_level = 0;
152 static gint pre_initialized = FALSE;
153 static gint gtk_initialized = FALSE;
154 static GList *current_events = NULL;
156 static GSList *main_loops = NULL; /* stack of currently executing main loops */
158 static GSList *key_snoopers = NULL;
160 static guint debug_flags = 0; /* Global GTK debug flag */
162 #ifdef G_ENABLE_DEBUG
163 static const GDebugKey gtk_debug_keys[] = {
164 {"misc", GTK_DEBUG_MISC},
165 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
166 {"text", GTK_DEBUG_TEXT},
167 {"tree", GTK_DEBUG_TREE},
168 {"updates", GTK_DEBUG_UPDATES},
169 {"keybindings", GTK_DEBUG_KEYBINDINGS},
170 {"multihead", GTK_DEBUG_MULTIHEAD},
171 {"modules", GTK_DEBUG_MODULES},
172 {"geometry", GTK_DEBUG_GEOMETRY},
173 {"icontheme", GTK_DEBUG_ICONTHEME},
174 {"printing", GTK_DEBUG_PRINTING},
175 {"builder", GTK_DEBUG_BUILDER},
176 {"size-request", GTK_DEBUG_SIZE_REQUEST},
178 #endif /* G_ENABLE_DEBUG */
181 * gtk_get_major_version:
183 * Returns the major version number of the GTK+ library.
184 * (e.g. in GTK+ version 3.1.5 this is 3.)
186 * This function is in the library, so it represents the GTK+ library
187 * your code is running against. Contrast with the #GTK_MAJOR_VERSION
188 * macro, which represents the major version of the GTK+ headers you
189 * have included when compiling your code.
191 * Returns: the major version number of the GTK+ library
196 gtk_get_major_version (void)
198 return GTK_MAJOR_VERSION;
202 * gtk_get_minor_version:
204 * Returns the minor version number of the GTK+ library.
205 * (e.g. in GTK+ version 3.1.5 this is 1.)
207 * This function is in the library, so it represents the GTK+ library
208 * your code is are running against. Contrast with the
209 * #GTK_MINOR_VERSION macro, which represents the minor version of the
210 * GTK+ headers you have included when compiling your code.
212 * Returns: the minor version number of the GTK+ library
217 gtk_get_minor_version (void)
219 return GTK_MINOR_VERSION;
223 * gtk_get_micro_version:
225 * Returns the micro version number of the GTK+ library.
226 * (e.g. in GTK+ version 3.1.5 this is 5.)
228 * This function is in the library, so it represents the GTK+ library
229 * your code is are running against. Contrast with the
230 * #GTK_MICRO_VERSION macro, which represents the micro version of the
231 * GTK+ headers you have included when compiling your code.
233 * Returns: the micro version number of the GTK+ library
238 gtk_get_micro_version (void)
240 return GTK_MICRO_VERSION;
244 * gtk_get_binary_age:
246 * Returns the binary age as passed to <application>libtool</application>
247 * when building the GTK+ library the process is running against.
248 * If <application>libtool</application> means nothing to you, don't
251 * Returns: the binary age of the GTK+ library
256 gtk_get_binary_age (void)
258 return GTK_BINARY_AGE;
262 * gtk_get_interface_age:
264 * Returns the interface age as passed to <application>libtool</application>
265 * when building the GTK+ library the process is running against.
266 * If <application>libtool</application> means nothing to you, don't
269 * Returns: the interface age of the GTK+ library
274 gtk_get_interface_age (void)
276 return GTK_INTERFACE_AGE;
281 * @required_major: the required major version
282 * @required_minor: the required minor version
283 * @required_micro: the required micro version
285 * Checks that the GTK+ library in use is compatible with the
286 * given version. Generally you would pass in the constants
287 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
288 * as the three arguments to this function; that produces
289 * a check that the library in use is compatible with
290 * the version of GTK+ the application or module was compiled
293 * Compatibility is defined by two things: first the version
294 * of the running library is newer than the version
295 * @required_major.required_minor.@required_micro. Second
296 * the running library must be binary compatible with the
297 * version @required_major.required_minor.@required_micro
298 * (same major version.)
300 * This function is primarily for GTK+ modules; the module
301 * can call this function to check that it wasn't loaded
302 * into an incompatible version of GTK+. However, such a
303 * check isn't completely reliable, since the module may be
304 * linked against an old version of GTK+ and calling the
305 * old version of gtk_check_version(), but still get loaded
306 * into an application using a newer version of GTK+.
308 * Return value: %NULL if the GTK+ library is compatible with the
309 * given version, or a string describing the version mismatch.
310 * The returned string is owned by GTK+ and should not be modified
314 gtk_check_version (guint required_major,
315 guint required_minor,
316 guint required_micro)
318 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
319 gint required_effective_micro = 100 * required_minor + required_micro;
321 if (required_major > GTK_MAJOR_VERSION)
322 return "GTK+ version too old (major mismatch)";
323 if (required_major < GTK_MAJOR_VERSION)
324 return "GTK+ version too new (major mismatch)";
325 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
326 return "GTK+ version too new (micro mismatch)";
327 if (required_effective_micro > gtk_effective_micro)
328 return "GTK+ version too old (micro mismatch)";
332 /* This checks to see if the process is running suid or sgid
333 * at the current time. If so, we don't allow GTK+ to be initialized.
334 * This is meant to be a mild check - we only error out if we
335 * can prove the programmer is doing something wrong, not if
336 * they could be doing something wrong. For this reason, we
337 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
342 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
344 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
345 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
347 #ifdef HAVE_GETRESUID
348 /* These aren't in the header files, so we prototype them here.
350 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
351 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
353 if (getresuid (&ruid, &euid, &suid) != 0 ||
354 getresgid (&rgid, &egid, &sgid) != 0)
355 #endif /* HAVE_GETRESUID */
357 suid = ruid = getuid ();
358 sgid = rgid = getgid ();
363 if (ruid != euid || ruid != suid ||
364 rgid != egid || rgid != sgid)
366 g_warning ("This process is currently running setuid or setgid.\n"
367 "This is not a supported use of GTK+. You must create a helper\n"
368 "program instead. For further details, see:\n\n"
369 " http://www.gtk.org/setuid.html\n\n"
370 "Refusing to initialize GTK+.");
377 static gboolean do_setlocale = TRUE;
380 * gtk_disable_setlocale:
382 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
383 * gtk_parse_args() from automatically
384 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
385 * want to use this function if you wanted to set the locale for
386 * your program to something other than the user's locale, or if
387 * you wanted to set different values for different locale categories.
389 * Most programs should not need to call this function.
392 gtk_disable_setlocale (void)
395 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
397 do_setlocale = FALSE;
400 #ifdef G_PLATFORM_WIN32
401 #undef gtk_init_check
404 static GString *gtk_modules_string = NULL;
405 static gboolean g_fatal_warnings = FALSE;
407 #ifdef G_ENABLE_DEBUG
409 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
411 debug_flags |= g_parse_debug_string (value,
413 G_N_ELEMENTS (gtk_debug_keys));
419 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
421 debug_flags &= ~g_parse_debug_string (value,
423 G_N_ELEMENTS (gtk_debug_keys));
427 #endif /* G_ENABLE_DEBUG */
430 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
434 if (gtk_modules_string)
435 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
437 gtk_modules_string = g_string_new (NULL);
439 g_string_append (gtk_modules_string, value);
445 static const GOptionEntry gtk_args[] = {
446 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
447 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
448 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
449 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
450 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
451 #ifdef G_ENABLE_DEBUG
452 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
453 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
454 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
455 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
456 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
457 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
464 static char *iso639_to_check = NULL;
465 static char *iso3166_to_check = NULL;
466 static char *script_to_check = NULL;
467 static gboolean setlocale_called = FALSE;
470 enum_locale_proc (LPTSTR locale)
478 lcid = strtoul (locale, &endptr, 16);
479 if (*endptr == '\0' &&
480 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
481 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
483 if (strcmp (iso639, iso639_to_check) == 0 &&
484 ((iso3166_to_check != NULL &&
485 strcmp (iso3166, iso3166_to_check) == 0) ||
486 (iso3166_to_check == NULL &&
487 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
489 char language[100], country[100];
492 if (script_to_check != NULL)
494 /* If lcid is the "other" script for this language,
495 * return TRUE, i.e. continue looking.
497 if (strcmp (script_to_check, "Latn") == 0)
499 switch (LANGIDFROMLCID (lcid))
501 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
503 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
505 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
507 case MAKELANGID (LANG_SERBIAN, 0x07):
508 /* Serbian in Bosnia and Herzegovina, Cyrillic */
512 else if (strcmp (script_to_check, "Cyrl") == 0)
514 switch (LANGIDFROMLCID (lcid))
516 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
518 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
520 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
522 case MAKELANGID (LANG_SERBIAN, 0x06):
523 /* Serbian in Bosnia and Herzegovina, Latin */
529 SetThreadLocale (lcid);
531 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
532 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
534 strcpy (locale, language);
535 strcat (locale, "_");
536 strcat (locale, country);
538 if (setlocale (LC_ALL, locale) != NULL)
539 setlocale_called = TRUE;
552 setlocale_initialization (void)
554 static gboolean initialized = FALSE;
563 /* If some of the POSIXish environment variables are set, set
564 * the Win32 thread locale correspondingly.
566 char *p = getenv ("LC_ALL");
573 if (strcmp (p, "C") == 0)
574 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
577 /* Check if one of the supported locales match the
578 * environment variable. If so, use that locale.
581 iso3166_to_check = strchr (iso639_to_check, '_');
582 if (iso3166_to_check != NULL)
584 *iso3166_to_check++ = '\0';
586 script_to_check = strchr (iso3166_to_check, '@');
587 if (script_to_check != NULL)
588 *script_to_check++ = '\0';
590 /* Handle special cases. */
592 /* The standard code for Serbia and Montenegro was
593 * "CS", but MSFT uses for some reason "SP". By now
594 * (October 2006), SP has split into two, "RS" and
595 * "ME", but don't bother trying to handle those
596 * yet. Do handle the even older "YU", though.
598 if (strcmp (iso3166_to_check, "CS") == 0 ||
599 strcmp (iso3166_to_check, "YU") == 0)
600 iso3166_to_check = "SP";
604 script_to_check = strchr (iso639_to_check, '@');
605 if (script_to_check != NULL)
606 *script_to_check++ = '\0';
607 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
608 if (strcmp (iso639_to_check, "sr") == 0)
609 iso3166_to_check = "SP";
612 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
616 if (!setlocale_called)
617 setlocale (LC_ALL, "");
619 if (!setlocale (LC_ALL, ""))
620 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
626 do_pre_parse_initialization (int *argc,
629 const gchar *env_string;
634 pre_initialized = TRUE;
636 if (_gtk_module_has_mixed_deps (NULL))
637 g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
639 gdk_pre_parse_libgtk_only ();
640 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
642 #ifdef G_ENABLE_DEBUG
643 env_string = g_getenv ("GTK_DEBUG");
644 if (env_string != NULL)
646 debug_flags = g_parse_debug_string (env_string,
648 G_N_ELEMENTS (gtk_debug_keys));
651 #endif /* G_ENABLE_DEBUG */
653 env_string = g_getenv ("GTK_MODULES");
655 gtk_modules_string = g_string_new (env_string);
659 gettext_initialization (void)
661 setlocale_initialization ();
664 bindtextdomain (GETTEXT_PACKAGE, _gtk_get_localedir ());
665 bindtextdomain (GETTEXT_PACKAGE "-properties", _gtk_get_localedir ());
666 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
667 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
668 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
673 /* XXX: Remove me after getting rid of gail */
674 extern void _gtk_accessibility_init (void);
677 do_post_parse_initialization (int *argc,
683 gettext_initialization ();
686 signal (SIGPIPE, SIG_IGN);
689 if (g_fatal_warnings)
691 GLogLevelFlags fatal_mask;
693 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
694 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
695 g_log_set_always_fatal (fatal_mask);
698 if (debug_flags & GTK_DEBUG_UPDATES)
699 gdk_window_set_debug_updates (TRUE);
702 /* Translate to default:RTL if you want your widgets
703 * to be RTL, otherwise translate to default:LTR.
704 * Do *not* translate it to "predefinito:LTR", if it
705 * it isn't default:LTR or default:RTL it will not work
707 char *e = _("default:LTR");
708 if (strcmp (e, "default:RTL")==0)
709 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
710 else if (strcmp (e, "default:LTR"))
711 g_warning ("Whoever translated default:LTR did so wrongly.\n");
714 _gtk_register_resource ();
716 /* do what the call to gtk_type_init() used to do */
719 _gtk_accel_map_init ();
721 /* Set the 'initialized' flag.
723 gtk_initialized = TRUE;
725 /* load gtk modules */
726 if (gtk_modules_string)
728 _gtk_modules_init (argc, argv, gtk_modules_string->str);
729 g_string_free (gtk_modules_string, TRUE);
733 _gtk_modules_init (argc, argv, NULL);
736 _gtk_accessibility_init ();
742 gboolean open_default_display;
746 pre_parse_hook (GOptionContext *context,
751 do_pre_parse_initialization (NULL, NULL);
757 post_parse_hook (GOptionContext *context,
762 OptionGroupInfo *info = data;
765 do_post_parse_initialization (NULL, NULL);
767 if (info->open_default_display)
769 if (gdk_display_open_default_libgtk_only () == NULL)
771 const char *display_name = gdk_get_display_arg_name ();
774 G_OPTION_ERROR_FAILED,
775 _("Cannot open display: %s"),
776 display_name ? display_name : "" );
787 * gtk_get_debug_flags:
789 * Returns the GTK+ debug flags.
791 * This function is intended for GTK+ modules that want
792 * to adjust their debug output based on GTK+ debug flags.
794 * Returns: the GTK+ debug flags.
797 gtk_get_debug_flags (void)
803 * gtk_set_debug_flags:
805 * Sets the GTK+ debug flags.
808 gtk_set_debug_flags (guint flags)
814 * gtk_get_option_group: (skip)
815 * @open_default_display: whether to open the default display
816 * when parsing the commandline arguments
818 * Returns a #GOptionGroup for the commandline arguments recognized
821 * You should add this group to your #GOptionContext
822 * with g_option_context_add_group(), if you are using
823 * g_option_context_parse() to parse your commandline arguments.
825 * Returns: a #GOptionGroup for the commandline arguments recognized
831 gtk_get_option_group (gboolean open_default_display)
834 OptionGroupInfo *info;
836 gettext_initialization ();
838 info = g_new0 (OptionGroupInfo, 1);
839 info->open_default_display = open_default_display;
841 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
842 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
844 gdk_add_option_entries_libgtk_only (group);
845 g_option_group_add_entries (group, gtk_args);
846 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
852 * gtk_init_with_args:
853 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
854 * your main() function (or 0 if @argv is %NULL). This will be changed if
855 * any arguments were handled.
856 * @argv: (array length=argc) (inout) (allow-none): Address of the
857 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
858 * understood by GTK+ are stripped before return.
859 * @parameter_string: a string which is displayed in
860 * the first line of <option>--help</option> output, after
861 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
862 * @entries: (array zero-terminated=1): a %NULL-terminated array
863 * of #GOptionEntrys describing the options of your program
864 * @translation_domain: a translation domain to use for translating
865 * the <option>--help</option> output for the options in @entries
866 * and the @parameter_string with gettext(), or %NULL
867 * @error: a return location for errors
869 * This function does the same work as gtk_init_check().
870 * Additionally, it allows you to add your own commandline options,
871 * and it automatically generates nicely formatted
872 * <option>--help</option> output. Note that your program will
873 * be terminated after writing out the help output.
875 * Returns: %TRUE if the windowing system has been successfully
876 * initialized, %FALSE otherwise
881 gtk_init_with_args (gint *argc,
883 const gchar *parameter_string,
884 const GOptionEntry *entries,
885 const gchar *translation_domain,
888 GOptionContext *context;
889 GOptionGroup *gtk_group;
893 return gdk_display_open_default_libgtk_only () != NULL;
895 gettext_initialization ();
897 if (!check_setugid ())
900 gtk_group = gtk_get_option_group (TRUE);
902 context = g_option_context_new (parameter_string);
903 g_option_context_add_group (context, gtk_group);
904 g_option_context_set_translation_domain (context, translation_domain);
907 g_option_context_add_main_entries (context, entries, translation_domain);
908 retval = g_option_context_parse (context, argc, argv, error);
910 g_option_context_free (context);
918 * @argc: (inout): a pointer to the number of command line arguments
919 * @argv: (array length=argc) (inout): a pointer to the array of
920 * command line arguments
922 * Parses command line arguments, and initializes global
923 * attributes of GTK+, but does not actually open a connection
924 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
926 * Any arguments used by GTK+ or GDK are removed from the array and
927 * @argc and @argv are updated accordingly.
929 * There is no need to call this function explicitely if you are using
930 * gtk_init(), or gtk_init_check().
932 * Return value: %TRUE if initialization succeeded, otherwise %FALSE
935 gtk_parse_args (int *argc,
938 GOptionContext *option_context;
939 GOptionGroup *gtk_group;
940 GError *error = NULL;
945 gettext_initialization ();
947 if (!check_setugid ())
950 option_context = g_option_context_new (NULL);
951 g_option_context_set_ignore_unknown_options (option_context, TRUE);
952 g_option_context_set_help_enabled (option_context, FALSE);
953 gtk_group = gtk_get_option_group (FALSE);
954 g_option_context_set_main_group (option_context, gtk_group);
955 if (!g_option_context_parse (option_context, argc, argv, &error))
957 g_warning ("%s", error->message);
958 g_error_free (error);
961 g_option_context_free (option_context);
966 #ifdef G_PLATFORM_WIN32
967 #undef gtk_init_check
972 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
973 * your main() function (or 0 if @argv is %NULL). This will be changed if
974 * any arguments were handled.
975 * @argv: (array length=argc) (inout) (allow-none): Address of the
976 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
977 * understood by GTK+ are stripped before return.
979 * This function does the same work as gtk_init() with only a single
980 * change: It does not terminate the program if the windowing system
981 * can't be initialized. Instead it returns %FALSE on failure.
983 * This way the application can fall back to some other means of
984 * communication with the user - for example a curses or command line
987 * Return value: %TRUE if the windowing system has been successfully
988 * initialized, %FALSE otherwise
991 gtk_init_check (int *argc,
994 if (!gtk_parse_args (argc, argv))
997 return gdk_display_open_default_libgtk_only () != NULL;
1000 #ifdef G_PLATFORM_WIN32
1006 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1007 * your main() function (or 0 if @argv is %NULL). This will be changed if
1008 * any arguments were handled.
1009 * @argv: (array length=argc) (inout) (allow-none): Address of the
1010 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
1011 * understood by GTK+ are stripped before return.
1013 * Call this function before using any other GTK+ functions in your GUI
1014 * applications. It will initialize everything needed to operate the
1015 * toolkit and parses some standard command line options.
1017 * Although you are expected to pass the @argc, @argv parameters from main() to
1018 * this function, it is possible to pass %NULL if @argv is not available or
1019 * commandline handling is not required.
1021 * @argc and @argv are adjusted accordingly so your own code will
1022 * never see those standard arguments.
1024 * Note that there are some alternative ways to initialize GTK+:
1025 * if you are calling gtk_parse_args(), gtk_init_check(),
1026 * gtk_init_with_args() or g_option_context_parse() with
1027 * the option group returned by gtk_get_option_group(),
1028 * you <emphasis>don't</emphasis> have to call gtk_init().
1031 * This function will terminate your program if it was unable to
1032 * initialize the windowing system for some reason. If you want
1033 * your program to fall back to a textual interface you want to
1034 * call gtk_init_check() instead.
1038 * Since 2.18, GTK+ calls <literal>signal (SIGPIPE, SIG_IGN)</literal>
1039 * during initialization, to ignore SIGPIPE signals, since these are
1040 * almost never wanted in graphical applications. If you do need to
1041 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1042 * but notice that other libraries (e.g. libdbus or gvfs) might do
1047 gtk_init (int *argc, char ***argv)
1049 if (!gtk_init_check (argc, argv))
1051 const char *display_name_arg = gdk_get_display_arg_name ();
1052 if (display_name_arg == NULL)
1053 display_name_arg = getenv("DISPLAY");
1054 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1061 /* This is relevant when building with gcc for Windows (MinGW),
1062 * where we want to be struct packing compatible with MSVC,
1063 * i.e. use the -mms-bitfields switch.
1064 * For Cygwin there should be no need to be compatible with MSVC,
1065 * so no need to use G_PLATFORM_WIN32.
1069 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1071 if (sizeof_GtkWindow != sizeof (GtkWindow))
1072 g_error ("Incompatible build!\n"
1073 "The code using GTK+ thinks GtkWindow is of different\n"
1074 "size than it actually is in this build of GTK+.\n"
1075 "On Windows, this probably means that you have compiled\n"
1076 "your code with gcc without the -mms-bitfields switch,\n"
1077 "or that you are using an unsupported compiler.");
1080 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1081 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1082 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
1083 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1084 * -fnative-struct still matters. GtkBox is one such.
1087 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1089 if (sizeof_GtkBox != sizeof (GtkBox))
1090 g_error ("Incompatible build!\n"
1091 "The code using GTK+ thinks GtkBox is of different\n"
1092 "size than it actually is in this build of GTK+.\n"
1093 "On Windows, this probably means that you have compiled\n"
1094 "your code with gcc without the -mms-bitfields switch,\n"
1095 "or that you are using an unsupported compiler.");
1098 /* These two functions might get more checks added later, thus pass
1099 * in the number of extra args.
1102 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1104 check_sizeof_GtkWindow (sizeof_GtkWindow);
1105 if (num_checks >= 2)
1106 check_sizeof_GtkBox (sizeof_GtkBox);
1107 gtk_init (argc, argv);
1111 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1113 check_sizeof_GtkWindow (sizeof_GtkWindow);
1114 if (num_checks >= 2)
1115 check_sizeof_GtkBox (sizeof_GtkBox);
1116 return gtk_init_check (argc, argv);
1122 * gtk_get_default_language:
1124 * Returns the #PangoLanguage for the default language currently in
1125 * effect. (Note that this can change over the life of an
1126 * application.) The default language is derived from the current
1127 * locale. It determines, for example, whether GTK+ uses the
1128 * right-to-left or left-to-right text direction.
1130 * This function is equivalent to pango_language_get_default().
1131 * See that function for details.
1133 * Return value: the default language as a #PangoLanguage,
1137 gtk_get_default_language (void)
1139 return pango_language_get_default ();
1145 * Runs the main loop until gtk_main_quit() is called.
1147 * You can nest calls to gtk_main(). In that case gtk_main_quit()
1148 * will make the innermost invocation of the main loop return.
1155 gtk_main_loop_level++;
1157 loop = g_main_loop_new (NULL, TRUE);
1158 main_loops = g_slist_prepend (main_loops, loop);
1160 if (g_main_loop_is_running (main_loops->data))
1162 GDK_THREADS_LEAVE ();
1163 g_main_loop_run (loop);
1164 GDK_THREADS_ENTER ();
1168 main_loops = g_slist_remove (main_loops, loop);
1170 g_main_loop_unref (loop);
1172 gtk_main_loop_level--;
1174 if (gtk_main_loop_level == 0)
1176 /* Try storing all clipboard data we have */
1177 _gtk_clipboard_store_all ();
1179 /* Synchronize the recent manager singleton */
1180 _gtk_recent_manager_sync ();
1187 * Asks for the current nesting level of the main loop.
1189 * Returns: the nesting level of the current invocation
1193 gtk_main_level (void)
1195 return gtk_main_loop_level;
1201 * Makes the innermost invocation of the main loop return
1202 * when it regains control.
1205 gtk_main_quit (void)
1207 g_return_if_fail (main_loops != NULL);
1209 g_main_loop_quit (main_loops->data);
1213 * gtk_events_pending:
1215 * Checks if any events are pending.
1217 * This can be used to update the UI and invoke timeouts etc.
1218 * while doing some time intensive computation.
1221 * <title>Updating the UI during a long computation</title>
1223 * /* computation going on... */
1225 * while (gtk_events_pending ())
1226 * gtk_main_iteration ();
1228 * /* ...computation continued */
1232 * Returns: %TRUE if any events are pending, %FALSE otherwise
1235 gtk_events_pending (void)
1239 GDK_THREADS_LEAVE ();
1240 result = g_main_context_pending (NULL);
1241 GDK_THREADS_ENTER ();
1247 * gtk_main_iteration:
1249 * Runs a single iteration of the mainloop.
1251 * If no events are waiting to be processed GTK+ will block
1252 * until the next event is noticed. If you don't want to block
1253 * look at gtk_main_iteration_do() or check if any events are
1254 * pending with gtk_events_pending() first.
1256 * Returns: %TRUE if gtk_main_quit() has been called for the
1257 * innermost mainloop
1260 gtk_main_iteration (void)
1262 GDK_THREADS_LEAVE ();
1263 g_main_context_iteration (NULL, TRUE);
1264 GDK_THREADS_ENTER ();
1267 return !g_main_loop_is_running (main_loops->data);
1273 * gtk_main_iteration_do:
1274 * @blocking: %TRUE if you want GTK+ to block if no events are pending
1276 * Runs a single iteration of the mainloop.
1277 * If no events are available either return or block depending on
1278 * the value of @blocking.
1280 * Returns: %TRUE if gtk_main_quit() has been called for the
1281 * innermost mainloop
1284 gtk_main_iteration_do (gboolean blocking)
1286 GDK_THREADS_LEAVE ();
1287 g_main_context_iteration (NULL, blocking);
1288 GDK_THREADS_ENTER ();
1291 return !g_main_loop_is_running (main_loops->data);
1296 /* private libgtk to libgdk interfaces */
1297 gboolean gdk_device_grab_info_libgtk_only (GdkDisplay *display,
1299 GdkWindow **grab_window,
1300 gboolean *owner_events);
1303 rewrite_events_translate (GdkWindow *old_window,
1304 GdkWindow *new_window,
1308 gint old_origin_x, old_origin_y;
1309 gint new_origin_x, new_origin_y;
1311 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1312 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1314 *x += old_origin_x - new_origin_x;
1315 *y += old_origin_y - new_origin_y;
1319 rewrite_event_for_window (GdkEvent *event,
1320 GdkWindow *new_window)
1322 event = gdk_event_copy (event);
1324 switch (event->type)
1327 rewrite_events_translate (event->any.window,
1329 &event->scroll.x, &event->scroll.y);
1331 case GDK_BUTTON_PRESS:
1332 case GDK_2BUTTON_PRESS:
1333 case GDK_3BUTTON_PRESS:
1334 case GDK_BUTTON_RELEASE:
1335 rewrite_events_translate (event->any.window,
1337 &event->button.x, &event->button.y);
1339 case GDK_MOTION_NOTIFY:
1340 rewrite_events_translate (event->any.window,
1342 &event->motion.x, &event->motion.y);
1345 case GDK_KEY_RELEASE:
1346 case GDK_PROXIMITY_IN:
1347 case GDK_PROXIMITY_OUT:
1354 g_object_unref (event->any.window);
1355 event->any.window = g_object_ref (new_window);
1360 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1361 * then what X11 does is deliver the event normally if it was going to this
1362 * client, otherwise, delivers it in terms of the grab window. This function
1363 * rewrites events to the effect that events going to the same window group
1364 * are delivered normally, otherwise, the event is delivered in terms of the
1368 rewrite_event_for_grabs (GdkEvent *event)
1370 GdkWindow *grab_window;
1371 GtkWidget *event_widget, *grab_widget;
1372 gpointer grab_widget_ptr;
1373 gboolean owner_events;
1374 GdkDisplay *display;
1377 switch (event->type)
1380 case GDK_BUTTON_PRESS:
1381 case GDK_2BUTTON_PRESS:
1382 case GDK_3BUTTON_PRESS:
1383 case GDK_BUTTON_RELEASE:
1384 case GDK_MOTION_NOTIFY:
1385 case GDK_PROXIMITY_IN:
1386 case GDK_PROXIMITY_OUT:
1388 case GDK_KEY_RELEASE:
1389 display = gdk_window_get_display (event->any.window);
1390 device = gdk_event_get_device (event);
1392 if (!gdk_device_grab_info_libgtk_only (display, device, &grab_window, &owner_events) ||
1400 event_widget = gtk_get_event_widget (event);
1401 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1402 grab_widget = grab_widget_ptr;
1405 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1406 return rewrite_event_for_window (event, grab_window);
1412 * gtk_main_do_event:
1413 * @event: An event to process (normally passed by GDK)
1415 * Processes a single GDK event.
1417 * This is public only to allow filtering of events between GDK and GTK+.
1418 * You will not usually need to call this function directly.
1420 * While you should not call this function directly, you might want to
1421 * know how exactly events are handled. So here is what this function
1422 * does with the event:
1426 * Compress enter/leave notify events. If the event passed build an
1427 * enter/leave pair together with the next event (peeked from GDK), both
1428 * events are thrown away. This is to avoid a backlog of (de-)highlighting
1429 * widgets crossed by the pointer.
1430 * </para></listitem>
1432 * Find the widget which got the event. If the widget can't be determined
1433 * the event is thrown away unless it belongs to a INCR transaction. In that
1434 * case it is passed to gtk_selection_incr_event().
1435 * </para></listitem>
1437 * Then the event is pushed onto a stack so you can query the currently
1438 * handled event with gtk_get_current_event().
1439 * </para></listitem>
1441 * The event is sent to a widget. If a grab is active all events for widgets
1442 * that are not in the contained in the grab widget are sent to the latter
1443 * with a few exceptions:
1446 * Deletion and destruction events are still sent to the event widget for
1448 * </para></listitem>
1450 * Events which directly relate to the visual representation of the event
1452 * </para></listitem>
1454 * Leave events are delivered to the event widget if there was an enter
1455 * event delivered to it before without the paired leave event.
1456 * </para></listitem>
1458 * Drag events are not redirected because it is unclear what the semantics
1460 * </para></listitem>
1462 * Another point of interest might be that all key events are first passed
1463 * through the key snooper functions if there are any. Read the description
1464 * of gtk_key_snooper_install() if you need this feature.
1465 * </para></listitem>
1467 * After finishing the delivery the event is popped from the event stack.
1468 * </para></listitem>
1472 gtk_main_do_event (GdkEvent *event)
1474 GtkWidget *event_widget;
1475 GtkWidget *grab_widget = NULL;
1476 GtkWindowGroup *window_group;
1477 GdkEvent *rewritten_event = NULL;
1481 if (event->type == GDK_SETTING)
1483 _gtk_settings_handle_event (&event->setting);
1487 if (event->type == GDK_OWNER_CHANGE)
1489 _gtk_clipboard_handle_event (&event->owner_change);
1493 /* Find the widget which got the event. We store the widget
1494 * in the user_data field of GdkWindow's. Ignore the event
1495 * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1496 * events which are handled specially. Though this happens rarely,
1497 * bogus events can occur for e.g. destroyed GdkWindows.
1499 event_widget = gtk_get_event_widget (event);
1502 /* To handle selection INCR transactions, we select
1503 * PropertyNotify events on the requestor window and create
1504 * a corresponding (fake) GdkWindow so that events get here.
1505 * There won't be a widget though, so we have to handle
1508 if (event->type == GDK_PROPERTY_NOTIFY)
1509 _gtk_selection_incr_event (event->any.window,
1515 /* If pointer or keyboard grabs are in effect, munge the events
1516 * so that each window group looks like a separate app.
1518 rewritten_event = rewrite_event_for_grabs (event);
1519 if (rewritten_event)
1521 event = rewritten_event;
1522 event_widget = gtk_get_event_widget (event);
1525 window_group = gtk_main_get_window_group (event_widget);
1526 device = gdk_event_get_device (event);
1528 /* check whether there is a (device) grab in effect... */
1530 grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1533 grab_widget = gtk_window_group_get_current_grab (window_group);
1535 /* If the grab widget is an ancestor of the event widget
1536 * then we send the event to the original event widget.
1537 * This is the key to implementing modality.
1540 (gtk_widget_is_sensitive (event_widget) &&
1541 gtk_widget_is_ancestor (event_widget, grab_widget)))
1542 grab_widget = event_widget;
1544 /* If the widget receiving events is actually blocked by another
1548 _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1550 if (rewritten_event)
1551 gdk_event_free (rewritten_event);
1556 /* Push the event onto a stack of current events for
1557 * gtk_current_event_get().
1559 current_events = g_list_prepend (current_events, event);
1561 /* Not all events get sent to the grabbing widget.
1562 * The delete, destroy, expose, focus change and resize
1563 * events still get sent to the event widget because
1564 * 1) these events have no meaning for the grabbing widget
1565 * and 2) redirecting these events to the grabbing widget
1566 * could cause the display to be messed up.
1568 * Drag events are also not redirected, since it isn't
1569 * clear what the semantics of that would be.
1571 switch (event->type)
1577 g_object_ref (event_widget);
1578 if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1579 !gtk_widget_event (event_widget, event))
1580 gtk_widget_destroy (event_widget);
1581 g_object_unref (event_widget);
1585 /* Unexpected GDK_DESTROY from the outside, ignore for
1586 * child windows, handle like a GDK_DELETE for toplevels
1588 if (!gtk_widget_get_parent (event_widget))
1590 g_object_ref (event_widget);
1591 if (!gtk_widget_event (event_widget, event) &&
1592 gtk_widget_get_realized (event_widget))
1593 gtk_widget_destroy (event_widget);
1594 g_object_unref (event_widget);
1599 if (event->any.window && gtk_widget_get_double_buffered (event_widget))
1601 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1602 gtk_widget_send_expose (event_widget, event);
1603 gdk_window_end_paint (event->any.window);
1607 /* The app may paint with a previously allocated cairo_t,
1608 * which will draw directly to the window. We can't catch cairo
1609 * draw operations to automatically flush the window, thus we
1610 * need to explicitly flush any outstanding moves or double
1613 gdk_window_flush (event->any.window);
1614 gtk_widget_send_expose (event_widget, event);
1618 case GDK_PROPERTY_NOTIFY:
1619 case GDK_FOCUS_CHANGE:
1623 case GDK_SELECTION_CLEAR:
1624 case GDK_SELECTION_REQUEST:
1625 case GDK_SELECTION_NOTIFY:
1626 case GDK_CLIENT_EVENT:
1627 case GDK_VISIBILITY_NOTIFY:
1628 case GDK_WINDOW_STATE:
1629 case GDK_GRAB_BROKEN:
1631 gtk_widget_event (event_widget, event);
1635 case GDK_BUTTON_PRESS:
1636 case GDK_2BUTTON_PRESS:
1637 case GDK_3BUTTON_PRESS:
1638 gtk_propagate_event (grab_widget, event);
1642 case GDK_KEY_RELEASE:
1643 if (gtk_invoke_key_snoopers (grab_widget, event))
1646 /* make focus visible in a window that receives a key event */
1649 GtkPolicyType visible_focus;
1651 window = gtk_widget_get_toplevel (grab_widget);
1652 g_object_get (gtk_widget_get_settings (grab_widget), "gtk-visible-focus", &visible_focus, NULL);
1653 if (GTK_IS_WINDOW (window) && visible_focus != GTK_POLICY_NEVER)
1654 gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
1657 /* Catch alt press to enable auto-mnemonics;
1658 * menus are handled elsewhere
1659 * FIXME: this does not work with mnemonic modifiers other than Alt
1661 if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1662 ((event->key.state & (gtk_accelerator_get_default_mod_mask ()) & ~(GDK_RELEASE_MASK|GDK_MOD1_MASK)) == 0) &&
1663 !GTK_IS_MENU_SHELL (grab_widget))
1665 gboolean auto_mnemonics;
1667 g_object_get (gtk_widget_get_settings (grab_widget),
1668 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
1672 gboolean mnemonics_visible;
1675 mnemonics_visible = (event->type == GDK_KEY_PRESS);
1677 window = gtk_widget_get_toplevel (grab_widget);
1678 if (GTK_IS_WINDOW (window))
1679 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), mnemonics_visible);
1682 /* else fall through */
1683 case GDK_MOTION_NOTIFY:
1684 case GDK_BUTTON_RELEASE:
1685 case GDK_PROXIMITY_IN:
1686 case GDK_PROXIMITY_OUT:
1687 gtk_propagate_event (grab_widget, event);
1690 case GDK_ENTER_NOTIFY:
1691 if (event->crossing.detail != GDK_NOTIFY_VIRTUAL &&
1692 event->crossing.detail != GDK_NOTIFY_NONLINEAR_VIRTUAL)
1693 _gtk_widget_set_device_window (event_widget,
1694 gdk_event_get_device (event),
1696 if (gtk_widget_is_sensitive (grab_widget))
1697 gtk_widget_event (grab_widget, event);
1700 case GDK_LEAVE_NOTIFY:
1701 if (event->crossing.detail != GDK_NOTIFY_VIRTUAL &&
1702 event->crossing.detail != GDK_NOTIFY_NONLINEAR_VIRTUAL)
1703 _gtk_widget_set_device_window (event_widget,
1704 gdk_event_get_device (event),
1706 if (gtk_widget_is_sensitive (grab_widget))
1707 gtk_widget_event (grab_widget, event);
1710 case GDK_DRAG_STATUS:
1711 case GDK_DROP_FINISHED:
1712 _gtk_drag_source_handle_event (event_widget, event);
1714 case GDK_DRAG_ENTER:
1715 case GDK_DRAG_LEAVE:
1716 case GDK_DRAG_MOTION:
1717 case GDK_DROP_START:
1718 _gtk_drag_dest_handle_event (event_widget, event);
1721 g_assert_not_reached ();
1725 if (event->type == GDK_ENTER_NOTIFY
1726 || event->type == GDK_LEAVE_NOTIFY
1727 || event->type == GDK_BUTTON_PRESS
1728 || event->type == GDK_2BUTTON_PRESS
1729 || event->type == GDK_3BUTTON_PRESS
1730 || event->type == GDK_KEY_PRESS
1731 || event->type == GDK_DRAG_ENTER
1732 || event->type == GDK_GRAB_BROKEN
1733 || event->type == GDK_MOTION_NOTIFY
1734 || event->type == GDK_SCROLL)
1736 _gtk_tooltip_handle_event (event);
1739 tmp_list = current_events;
1740 current_events = g_list_remove_link (current_events, tmp_list);
1741 g_list_free_1 (tmp_list);
1743 if (rewritten_event)
1744 gdk_event_free (rewritten_event);
1750 * All this function does it to return %TRUE.
1752 * This can be useful for example if you want to inhibit the deletion
1753 * of a window. Of course you should not do this as the user expects
1754 * a reaction from clicking the close icon of the window...
1757 * <title>A persistent window</title>
1759 * #include <gtk/gtk.h><
1762 * main (int argc, char **argv)
1764 * GtkWidget *win, *but;
1766 * gtk_init (&argc, &argv);
1768 * win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1769 * g_signal_connect (win, "delete-event",
1770 * G_CALLBACK (gtk_true), NULL);
1771 * g_signal_connect (win, "destroy",
1772 * G_CALLBACK (gtk_main_quit), NULL);
1774 * but = gtk_button_new_with_label ("Close yourself. I mean it!");
1775 * g_signal_connect_swapped (but, "clicked",
1776 * G_CALLBACK (gtk_object_destroy), win);
1777 * gtk_container_add (GTK_CONTAINER (win), but);
1779 * gtk_widget_show_all (win);
1799 * Analogical to gtk_true(), this function does nothing
1800 * but always returns %FALSE.
1810 static GtkWindowGroup *
1811 gtk_main_get_window_group (GtkWidget *widget)
1813 GtkWidget *toplevel = NULL;
1816 toplevel = gtk_widget_get_toplevel (widget);
1818 if (GTK_IS_WINDOW (toplevel))
1819 return gtk_window_get_group (GTK_WINDOW (toplevel));
1821 return gtk_window_get_group (NULL);
1826 GtkWidget *old_grab_widget;
1827 GtkWidget *new_grab_widget;
1828 gboolean was_grabbed;
1829 gboolean is_grabbed;
1831 GList *notified_windows;
1836 synth_crossing_for_grab_notify (GtkWidget *from,
1838 GrabNotifyInfo *info,
1840 GdkCrossingMode mode)
1844 GdkDevice *device = devices->data;
1845 GdkWindow *from_window, *to_window;
1847 /* Do not propagate events more than once to
1848 * the same windows if non-multidevice aware.
1854 from_window = _gtk_widget_get_device_window (from, device);
1857 !gdk_window_get_support_multidevice (from_window) &&
1858 g_list_find (info->notified_windows, from_window))
1866 to_window = _gtk_widget_get_device_window (to, device);
1869 !gdk_window_get_support_multidevice (to_window) &&
1870 g_list_find (info->notified_windows, to_window))
1874 if (from_window || to_window)
1876 _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
1877 (to_window) ? to : NULL,
1881 info->notified_windows = g_list_prepend (info->notified_windows, from_window);
1884 info->notified_windows = g_list_prepend (info->notified_windows, to_window);
1887 devices = devices->next;
1892 gtk_grab_notify_foreach (GtkWidget *child,
1895 GrabNotifyInfo *info = data;
1896 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
1899 was_grabbed = info->was_grabbed;
1900 is_grabbed = info->is_grabbed;
1902 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
1903 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
1905 was_shadowed = info->old_grab_widget && !info->was_grabbed;
1906 is_shadowed = info->new_grab_widget && !info->is_grabbed;
1908 g_object_ref (child);
1910 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
1911 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1914 _gtk_widget_get_device_window (child, info->device))
1916 /* Device specified and is on widget */
1917 devices = g_list_prepend (NULL, info->device);
1920 devices = _gtk_widget_list_devices (child);
1924 _gtk_widget_set_shadowed (child, TRUE);
1925 if (!was_shadowed && devices &&
1926 gtk_widget_is_sensitive (child))
1927 synth_crossing_for_grab_notify (child, info->new_grab_widget,
1929 GDK_CROSSING_GTK_GRAB);
1933 _gtk_widget_set_shadowed (child, FALSE);
1934 if (was_shadowed && devices &&
1935 gtk_widget_is_sensitive (child))
1936 synth_crossing_for_grab_notify (info->old_grab_widget, child,
1938 info->from_grab ? GDK_CROSSING_GTK_GRAB :
1939 GDK_CROSSING_GTK_UNGRAB);
1942 if (was_shadowed != is_shadowed)
1943 _gtk_widget_grab_notify (child, was_shadowed);
1945 g_object_unref (child);
1946 g_list_free (devices);
1948 info->was_grabbed = was_grabbed;
1949 info->is_grabbed = is_grabbed;
1953 gtk_grab_notify (GtkWindowGroup *group,
1955 GtkWidget *old_grab_widget,
1956 GtkWidget *new_grab_widget,
1960 GrabNotifyInfo info = { 0 };
1962 if (old_grab_widget == new_grab_widget)
1965 info.old_grab_widget = old_grab_widget;
1966 info.new_grab_widget = new_grab_widget;
1967 info.from_grab = from_grab;
1968 info.device = device;
1970 g_object_ref (group);
1972 toplevels = gtk_window_list_toplevels ();
1973 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1977 GtkWindow *toplevel = toplevels->data;
1978 toplevels = g_list_delete_link (toplevels, toplevels);
1980 info.was_grabbed = FALSE;
1981 info.is_grabbed = FALSE;
1983 if (group == gtk_window_get_group (toplevel))
1984 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
1985 g_object_unref (toplevel);
1988 g_list_free (info.notified_windows);
1989 g_object_unref (group);
1993 * gtk_grab_add: (method)
1994 * @widget: The widget that grabs keyboard and pointer events
1996 * Makes @widget the current grabbed widget.
1998 * This means that interaction with other widgets in the same
1999 * application is blocked and mouse as well as keyboard events
2000 * are delivered to this widget.
2002 * If @widget is not sensitive, it is not set as the current
2003 * grabbed widget and this function does nothing.
2006 gtk_grab_add (GtkWidget *widget)
2008 GtkWindowGroup *group;
2009 GtkWidget *old_grab_widget;
2011 g_return_if_fail (widget != NULL);
2013 if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2015 _gtk_widget_set_has_grab (widget, TRUE);
2017 group = gtk_main_get_window_group (widget);
2019 old_grab_widget = gtk_window_group_get_current_grab (group);
2021 g_object_ref (widget);
2022 _gtk_window_group_add_grab (group, widget);
2024 gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2029 * gtk_grab_get_current:
2031 * Queries the current grab of the default window group.
2033 * Return value: (transfer none): The widget which currently
2034 * has the grab or %NULL if no grab is active
2037 gtk_grab_get_current (void)
2039 GtkWindowGroup *group;
2041 group = gtk_main_get_window_group (NULL);
2043 return gtk_window_group_get_current_grab (group);
2047 * gtk_grab_remove: (method)
2048 * @widget: The widget which gives up the grab
2050 * Removes the grab from the given widget.
2052 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2054 * If @widget does not have the grab, this function does nothing.
2057 gtk_grab_remove (GtkWidget *widget)
2059 GtkWindowGroup *group;
2060 GtkWidget *new_grab_widget;
2062 g_return_if_fail (widget != NULL);
2064 if (gtk_widget_has_grab (widget))
2066 _gtk_widget_set_has_grab (widget, FALSE);
2068 group = gtk_main_get_window_group (widget);
2069 _gtk_window_group_remove_grab (group, widget);
2070 new_grab_widget = gtk_window_group_get_current_grab (group);
2072 gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2074 g_object_unref (widget);
2079 * gtk_device_grab_add:
2080 * @widget: a #GtkWidget
2081 * @device: a #GtkDevice to grab on.
2082 * @block_others: %TRUE to prevent other devices to interact with @widget.
2084 * Adds a GTK+ grab on @device, so all the events on @device and its
2085 * associated pointer or keyboard (if any) are delivered to @widget.
2086 * If the @block_others parameter is %TRUE, any other devices will be
2087 * unable to interact with @widget during the grab.
2092 gtk_device_grab_add (GtkWidget *widget,
2094 gboolean block_others)
2096 GtkWindowGroup *group;
2097 GtkWidget *old_grab_widget;
2099 g_return_if_fail (GTK_IS_WIDGET (widget));
2100 g_return_if_fail (GDK_IS_DEVICE (device));
2102 group = gtk_main_get_window_group (widget);
2103 old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2105 if (old_grab_widget != widget)
2106 _gtk_window_group_add_device_grab (group, widget, device, block_others);
2108 gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2112 * gtk_device_grab_remove:
2113 * @widget: a #GtkWidget
2114 * @device: a #GdkDevice
2116 * Removes a device grab from the given widget.
2118 * You have to pair calls to gtk_device_grab_add() and
2119 * gtk_device_grab_remove().
2124 gtk_device_grab_remove (GtkWidget *widget,
2127 GtkWindowGroup *group;
2128 GtkWidget *new_grab_widget;
2130 g_return_if_fail (GTK_IS_WIDGET (widget));
2131 g_return_if_fail (GDK_IS_DEVICE (device));
2133 group = gtk_main_get_window_group (widget);
2134 _gtk_window_group_remove_device_grab (group, widget, device);
2135 new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2137 gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2141 * gtk_key_snooper_install: (skip)
2142 * @snooper: a #GtkKeySnoopFunc
2143 * @func_data: data to pass to @snooper
2145 * Installs a key snooper function, which will get called on all
2146 * key events before delivering them normally.
2148 * Returns: a unique id for this key snooper for use with
2149 * gtk_key_snooper_remove().
2151 * Deprecated: 3.4: Key snooping should not be done. Events should
2152 * be handled by widgets.
2155 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2158 GtkKeySnooperData *data;
2159 static guint snooper_id = 1;
2161 g_return_val_if_fail (snooper != NULL, 0);
2163 data = g_new (GtkKeySnooperData, 1);
2164 data->func = snooper;
2165 data->func_data = func_data;
2166 data->id = snooper_id++;
2167 key_snoopers = g_slist_prepend (key_snoopers, data);
2173 * gtk_key_snooper_remove:
2174 * @snooper_handler_id: Identifies the key snooper to remove
2176 * Removes the key snooper function with the given id.
2178 * Deprecated: 3.4: Key snooping should not be done. Events should
2179 * be handled by widgets.
2182 gtk_key_snooper_remove (guint snooper_id)
2184 GtkKeySnooperData *data = NULL;
2187 slist = key_snoopers;
2191 if (data->id == snooper_id)
2194 slist = slist->next;
2199 key_snoopers = g_slist_remove (key_snoopers, data);
2205 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2209 gint return_val = FALSE;
2211 return_val = _gail_util_key_snooper (grab_widget, (GdkEventKey *) event);
2213 slist = key_snoopers;
2214 while (slist && !return_val)
2216 GtkKeySnooperData *data;
2219 slist = slist->next;
2220 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2227 * gtk_get_current_event:
2229 * Obtains a copy of the event currently being processed by GTK+.
2231 * For example, if you are handling a #GtkButton::clicked signal,
2232 * the current event will be the #GdkEventButton that triggered
2233 * the ::clicked signal.
2235 * Return value: (transfer full): a copy of the current event, or
2236 * %NULL if there is no current event. The returned event must be
2237 * freed with gdk_event_free().
2240 gtk_get_current_event (void)
2243 return gdk_event_copy (current_events->data);
2249 * gtk_get_current_event_time:
2251 * If there is a current event and it has a timestamp,
2252 * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2254 * Return value: the timestamp from the current event,
2255 * or %GDK_CURRENT_TIME.
2258 gtk_get_current_event_time (void)
2261 return gdk_event_get_time (current_events->data);
2263 return GDK_CURRENT_TIME;
2267 * gtk_get_current_event_state:
2268 * @state: (out): a location to store the state of the current event
2270 * If there is a current event and it has a state field, place
2271 * that state field in @state and return %TRUE, otherwise return
2274 * Return value: %TRUE if there was a current event and it
2278 gtk_get_current_event_state (GdkModifierType *state)
2280 g_return_val_if_fail (state != NULL, FALSE);
2283 return gdk_event_get_state (current_events->data, state);
2292 * gtk_get_current_event_device:
2294 * If there is a current event and it has a device, return that
2295 * device, otherwise return %NULL.
2297 * Returns: (transfer none): a #GdkDevice, or %NULL
2300 gtk_get_current_event_device (void)
2303 return gdk_event_get_device (current_events->data);
2309 * gtk_get_event_widget:
2310 * @event: a #GdkEvent
2312 * If @event is %NULL or the event was not associated with any widget,
2313 * returns %NULL, otherwise returns the widget that received the event
2316 * Return value: (transfer none): the widget that originally
2317 * received @event, or %NULL
2320 gtk_get_event_widget (GdkEvent *event)
2323 gpointer widget_ptr;
2326 if (event && event->any.window &&
2327 (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2329 gdk_window_get_user_data (event->any.window, &widget_ptr);
2330 widget = widget_ptr;
2337 * gtk_propagate_event:
2338 * @widget: a #GtkWidget
2341 * Sends an event to a widget, propagating the event to parent widgets
2342 * if the event remains unhandled.
2344 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2345 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2346 * the event may be propagated; if so, this function is used.
2348 * gtk_propagate_event() calls gtk_widget_event() on each widget it
2349 * decides to send the event to. So gtk_widget_event() is the lowest-level
2350 * function; it simply emits the #GtkWidget::event and possibly an
2351 * event-specific signal on a widget. gtk_propagate_event() is a bit
2352 * higher-level, and gtk_main_do_event() is the highest level.
2354 * All that said, you most likely don't want to use any of these
2355 * functions; synthesizing events is rarely needed. There are almost
2356 * certainly better ways to achieve your goals. For example, use
2357 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2358 * of making up expose events.
2361 gtk_propagate_event (GtkWidget *widget,
2366 g_return_if_fail (GTK_IS_WIDGET (widget));
2367 g_return_if_fail (event != NULL);
2369 handled_event = FALSE;
2371 g_object_ref (widget);
2373 if ((event->type == GDK_KEY_PRESS) ||
2374 (event->type == GDK_KEY_RELEASE))
2376 /* Only send key events within Window widgets to the Window
2377 * The Window widget will in turn pass the
2378 * key event on to the currently focused widget
2383 window = gtk_widget_get_toplevel (widget);
2384 if (GTK_IS_WINDOW (window))
2386 /* If there is a grab within the window, give the grab widget
2387 * a first crack at the key event
2389 if (widget != window && gtk_widget_has_grab (widget))
2390 handled_event = gtk_widget_event (widget, event);
2394 window = gtk_widget_get_toplevel (widget);
2395 if (GTK_IS_WINDOW (window))
2397 if (gtk_widget_is_sensitive (window))
2398 gtk_widget_event (window, event);
2402 handled_event = TRUE; /* don't send to widget */
2406 /* Other events get propagated up the widget tree
2407 * so that parents can see the button and motion
2408 * events of the children.
2416 /* Scroll events are special cased here because it
2417 * feels wrong when scrolling a GtkViewport, say,
2418 * to have children of the viewport eat the scroll
2421 if (!gtk_widget_is_sensitive (widget))
2422 handled_event = event->type != GDK_SCROLL;
2424 handled_event = gtk_widget_event (widget, event);
2426 tmp = gtk_widget_get_parent (widget);
2427 g_object_unref (widget);
2431 if (!handled_event && widget)
2432 g_object_ref (widget);
2438 g_object_unref (widget);