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.
94 #include "gtkmainprivate.h"
107 #include <sys/types.h> /* For uid_t, gid_t */
117 #include "gtkaccelmap.h"
119 #include "gtkclipboard.h"
121 #include "gtkversion.h"
122 #include "gtkmodules.h"
124 #include "gtkrecentmanager.h"
125 #include "gtkselectionprivate.h"
126 #include "gtksettingsprivate.h"
127 #include "gtkwidgetprivate.h"
128 #include "gtkwindowprivate.h"
129 #include "gtktooltip.h"
130 #include "gtkdebug.h"
135 static HMODULE gtk_dll;
138 DllMain (HINSTANCE hinstDLL,
144 case DLL_PROCESS_ATTACH:
145 gtk_dll = (HMODULE) hinstDLL;
152 /* These here before inclusion of gtkprivate.h so that the original
153 * GTK_LIBDIR and GTK_LOCALEDIR definitions are seen. Yeah, this is a
157 _gtk_get_libdir (void)
159 static char *gtk_libdir = NULL;
160 if (gtk_libdir == NULL)
162 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
163 gchar *slash = strrchr (root, '\\');
164 if (g_ascii_strcasecmp (slash + 1, ".libs") == 0)
165 gtk_libdir = GTK_LIBDIR;
167 gtk_libdir = g_build_filename (root, "lib", NULL);
175 _gtk_get_localedir (void)
177 static char *gtk_localedir = NULL;
178 if (gtk_localedir == NULL)
183 /* GTK_LOCALEDIR ends in either /lib/locale or
184 * /share/locale. Scan for that slash.
186 p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
192 root = g_win32_get_package_installation_directory_of_module (gtk_dll);
193 temp = g_build_filename (root, p, NULL);
196 /* gtk_localedir is passed to bindtextdomain() which isn't
199 gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
202 return gtk_localedir;
207 #include "gtkprivate.h"
209 /* Private type definitions
211 typedef struct _GtkKeySnooperData GtkKeySnooperData;
213 struct _GtkKeySnooperData
215 GtkKeySnoopFunc func;
220 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
223 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
225 static guint gtk_main_loop_level = 0;
226 static gint pre_initialized = FALSE;
227 static gint gtk_initialized = FALSE;
228 static GList *current_events = NULL;
230 static GSList *main_loops = NULL; /* stack of currently executing main loops */
232 static GSList *key_snoopers = NULL;
234 static guint debug_flags = 0; /* Global GTK debug flag */
236 #ifdef G_ENABLE_DEBUG
237 static const GDebugKey gtk_debug_keys[] = {
238 {"misc", GTK_DEBUG_MISC},
239 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
240 {"text", GTK_DEBUG_TEXT},
241 {"tree", GTK_DEBUG_TREE},
242 {"updates", GTK_DEBUG_UPDATES},
243 {"keybindings", GTK_DEBUG_KEYBINDINGS},
244 {"multihead", GTK_DEBUG_MULTIHEAD},
245 {"modules", GTK_DEBUG_MODULES},
246 {"geometry", GTK_DEBUG_GEOMETRY},
247 {"icontheme", GTK_DEBUG_ICONTHEME},
248 {"printing", GTK_DEBUG_PRINTING},
249 {"builder", GTK_DEBUG_BUILDER},
250 {"size-request", GTK_DEBUG_SIZE_REQUEST},
252 #endif /* G_ENABLE_DEBUG */
255 * gtk_get_major_version:
257 * Returns the major version number of the GTK+ library.
258 * (e.g. in GTK+ version 3.1.5 this is 3.)
260 * This function is in the library, so it represents the GTK+ library
261 * your code is running against. Contrast with the #GTK_MAJOR_VERSION
262 * macro, which represents the major version of the GTK+ headers you
263 * have included when compiling your code.
265 * Returns: the major version number of the GTK+ library
270 gtk_get_major_version (void)
272 return GTK_MAJOR_VERSION;
276 * gtk_get_minor_version:
278 * Returns the minor version number of the GTK+ library.
279 * (e.g. in GTK+ version 3.1.5 this is 1.)
281 * This function is in the library, so it represents the GTK+ library
282 * your code is are running against. Contrast with the
283 * #GTK_MINOR_VERSION macro, which represents the minor version of the
284 * GTK+ headers you have included when compiling your code.
286 * Returns: the minor version number of the GTK+ library
291 gtk_get_minor_version (void)
293 return GTK_MINOR_VERSION;
297 * gtk_get_micro_version:
299 * Returns the micro version number of the GTK+ library.
300 * (e.g. in GTK+ version 3.1.5 this is 5.)
302 * This function is in the library, so it represents the GTK+ library
303 * your code is are running against. Contrast with the
304 * #GTK_MICRO_VERSION macro, which represents the micro version of the
305 * GTK+ headers you have included when compiling your code.
307 * Returns: the micro version number of the GTK+ library
312 gtk_get_micro_version (void)
314 return GTK_MICRO_VERSION;
318 * gtk_get_binary_age:
320 * Returns the binary age as passed to <application>libtool</application>
321 * when building the GTK+ library the process is running against.
322 * If <application>libtool</application> means nothing to you, don't
325 * Returns: the binary age of the GTK+ library
330 gtk_get_binary_age (void)
332 return GTK_BINARY_AGE;
336 * gtk_get_interface_age:
338 * Returns the interface age as passed to <application>libtool</application>
339 * when building the GTK+ library the process is running against.
340 * If <application>libtool</application> means nothing to you, don't
343 * Returns: the interface age of the GTK+ library
348 gtk_get_interface_age (void)
350 return GTK_INTERFACE_AGE;
355 * @required_major: the required major version
356 * @required_minor: the required minor version
357 * @required_micro: the required micro version
359 * Checks that the GTK+ library in use is compatible with the
360 * given version. Generally you would pass in the constants
361 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
362 * as the three arguments to this function; that produces
363 * a check that the library in use is compatible with
364 * the version of GTK+ the application or module was compiled
367 * Compatibility is defined by two things: first the version
368 * of the running library is newer than the version
369 * @required_major.required_minor.@required_micro. Second
370 * the running library must be binary compatible with the
371 * version @required_major.required_minor.@required_micro
372 * (same major version.)
374 * This function is primarily for GTK+ modules; the module
375 * can call this function to check that it wasn't loaded
376 * into an incompatible version of GTK+. However, such a
377 * check isn't completely reliable, since the module may be
378 * linked against an old version of GTK+ and calling the
379 * old version of gtk_check_version(), but still get loaded
380 * into an application using a newer version of GTK+.
382 * Return value: %NULL if the GTK+ library is compatible with the
383 * given version, or a string describing the version mismatch.
384 * The returned string is owned by GTK+ and should not be modified
388 gtk_check_version (guint required_major,
389 guint required_minor,
390 guint required_micro)
392 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
393 gint required_effective_micro = 100 * required_minor + required_micro;
395 if (required_major > GTK_MAJOR_VERSION)
396 return "GTK+ version too old (major mismatch)";
397 if (required_major < GTK_MAJOR_VERSION)
398 return "GTK+ version too new (major mismatch)";
399 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
400 return "GTK+ version too new (micro mismatch)";
401 if (required_effective_micro > gtk_effective_micro)
402 return "GTK+ version too old (micro mismatch)";
406 /* This checks to see if the process is running suid or sgid
407 * at the current time. If so, we don't allow GTK+ to be initialized.
408 * This is meant to be a mild check - we only error out if we
409 * can prove the programmer is doing something wrong, not if
410 * they could be doing something wrong. For this reason, we
411 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
416 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
418 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
419 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
421 #ifdef HAVE_GETRESUID
422 /* These aren't in the header files, so we prototype them here.
424 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
425 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
427 if (getresuid (&ruid, &euid, &suid) != 0 ||
428 getresgid (&rgid, &egid, &sgid) != 0)
429 #endif /* HAVE_GETRESUID */
431 suid = ruid = getuid ();
432 sgid = rgid = getgid ();
437 if (ruid != euid || ruid != suid ||
438 rgid != egid || rgid != sgid)
440 g_warning ("This process is currently running setuid or setgid.\n"
441 "This is not a supported use of GTK+. You must create a helper\n"
442 "program instead. For further details, see:\n\n"
443 " http://www.gtk.org/setuid.html\n\n"
444 "Refusing to initialize GTK+.");
454 _gtk_get_datadir (void)
456 static char *gtk_datadir = NULL;
457 if (gtk_datadir == NULL)
459 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
460 gtk_datadir = g_build_filename (root, "share", NULL);
468 _gtk_get_sysconfdir (void)
470 static char *gtk_sysconfdir = NULL;
471 if (gtk_sysconfdir == NULL)
473 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
474 gtk_sysconfdir = g_build_filename (root, "etc", NULL);
478 return gtk_sysconfdir;
482 _gtk_get_data_prefix (void)
484 static char *gtk_data_prefix = NULL;
485 if (gtk_data_prefix == NULL)
486 gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);
488 return gtk_data_prefix;
491 #endif /* G_OS_WIN32 */
493 static gboolean do_setlocale = TRUE;
496 * gtk_disable_setlocale:
498 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
499 * gtk_parse_args() from automatically
500 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
501 * want to use this function if you wanted to set the locale for
502 * your program to something other than the user's locale, or if
503 * you wanted to set different values for different locale categories.
505 * Most programs should not need to call this function.
508 gtk_disable_setlocale (void)
511 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
513 do_setlocale = FALSE;
516 #ifdef G_PLATFORM_WIN32
517 #undef gtk_init_check
520 static GString *gtk_modules_string = NULL;
521 static gboolean g_fatal_warnings = FALSE;
523 #ifdef G_ENABLE_DEBUG
525 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
527 debug_flags |= g_parse_debug_string (value,
529 G_N_ELEMENTS (gtk_debug_keys));
535 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
537 debug_flags &= ~g_parse_debug_string (value,
539 G_N_ELEMENTS (gtk_debug_keys));
543 #endif /* G_ENABLE_DEBUG */
546 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
550 if (gtk_modules_string)
551 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
553 gtk_modules_string = g_string_new (NULL);
555 g_string_append (gtk_modules_string, value);
561 static const GOptionEntry gtk_args[] = {
562 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
563 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
564 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
565 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
566 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
567 #ifdef G_ENABLE_DEBUG
568 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
569 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
570 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
571 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
572 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
573 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
580 static char *iso639_to_check = NULL;
581 static char *iso3166_to_check = NULL;
582 static char *script_to_check = NULL;
583 static gboolean setlocale_called = FALSE;
586 enum_locale_proc (LPTSTR locale)
594 lcid = strtoul (locale, &endptr, 16);
595 if (*endptr == '\0' &&
596 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
597 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
599 if (strcmp (iso639, iso639_to_check) == 0 &&
600 ((iso3166_to_check != NULL &&
601 strcmp (iso3166, iso3166_to_check) == 0) ||
602 (iso3166_to_check == NULL &&
603 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
605 char language[100], country[100];
608 if (script_to_check != NULL)
610 /* If lcid is the "other" script for this language,
611 * return TRUE, i.e. continue looking.
613 if (strcmp (script_to_check, "Latn") == 0)
615 switch (LANGIDFROMLCID (lcid))
617 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
619 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
621 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
623 case MAKELANGID (LANG_SERBIAN, 0x07):
624 /* Serbian in Bosnia and Herzegovina, Cyrillic */
628 else if (strcmp (script_to_check, "Cyrl") == 0)
630 switch (LANGIDFROMLCID (lcid))
632 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
634 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
636 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
638 case MAKELANGID (LANG_SERBIAN, 0x06):
639 /* Serbian in Bosnia and Herzegovina, Latin */
645 SetThreadLocale (lcid);
647 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
648 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
650 strcpy (locale, language);
651 strcat (locale, "_");
652 strcat (locale, country);
654 if (setlocale (LC_ALL, locale) != NULL)
655 setlocale_called = TRUE;
668 setlocale_initialization (void)
670 static gboolean initialized = FALSE;
679 /* If some of the POSIXish environment variables are set, set
680 * the Win32 thread locale correspondingly.
682 char *p = getenv ("LC_ALL");
689 if (strcmp (p, "C") == 0)
690 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
693 /* Check if one of the supported locales match the
694 * environment variable. If so, use that locale.
697 iso3166_to_check = strchr (iso639_to_check, '_');
698 if (iso3166_to_check != NULL)
700 *iso3166_to_check++ = '\0';
702 script_to_check = strchr (iso3166_to_check, '@');
703 if (script_to_check != NULL)
704 *script_to_check++ = '\0';
706 /* Handle special cases. */
708 /* The standard code for Serbia and Montenegro was
709 * "CS", but MSFT uses for some reason "SP". By now
710 * (October 2006), SP has split into two, "RS" and
711 * "ME", but don't bother trying to handle those
712 * yet. Do handle the even older "YU", though.
714 if (strcmp (iso3166_to_check, "CS") == 0 ||
715 strcmp (iso3166_to_check, "YU") == 0)
716 iso3166_to_check = "SP";
720 script_to_check = strchr (iso639_to_check, '@');
721 if (script_to_check != NULL)
722 *script_to_check++ = '\0';
723 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
724 if (strcmp (iso639_to_check, "sr") == 0)
725 iso3166_to_check = "SP";
728 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
732 if (!setlocale_called)
733 setlocale (LC_ALL, "");
735 if (!setlocale (LC_ALL, ""))
736 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
741 /* Return TRUE if module_to_check causes version conflicts.
742 * If module_to_check is NULL, check the main module.
745 _gtk_module_has_mixed_deps (GModule *module_to_check)
751 if (!module_to_check)
752 module = g_module_open (NULL, 0);
754 module = module_to_check;
756 if (g_module_symbol (module, "gtk_progress_get_type", &func))
761 if (!module_to_check)
762 g_module_close (module);
768 do_pre_parse_initialization (int *argc,
771 const gchar *env_string;
776 pre_initialized = TRUE;
778 if (_gtk_module_has_mixed_deps (NULL))
779 g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
781 gdk_pre_parse_libgtk_only ();
782 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
784 #ifdef G_ENABLE_DEBUG
785 env_string = g_getenv ("GTK_DEBUG");
786 if (env_string != NULL)
788 debug_flags = g_parse_debug_string (env_string,
790 G_N_ELEMENTS (gtk_debug_keys));
793 #endif /* G_ENABLE_DEBUG */
795 env_string = g_getenv ("GTK_MODULES");
797 gtk_modules_string = g_string_new (env_string);
801 gettext_initialization (void)
803 setlocale_initialization ();
806 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
807 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
808 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
809 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
810 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
816 do_post_parse_initialization (int *argc,
822 gettext_initialization ();
825 signal (SIGPIPE, SIG_IGN);
828 if (g_fatal_warnings)
830 GLogLevelFlags fatal_mask;
832 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
833 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
834 g_log_set_always_fatal (fatal_mask);
837 if (debug_flags & GTK_DEBUG_UPDATES)
838 gdk_window_set_debug_updates (TRUE);
841 /* Translate to default:RTL if you want your widgets
842 * to be RTL, otherwise translate to default:LTR.
843 * Do *not* translate it to "predefinito:LTR", if it
844 * it isn't default:LTR or default:RTL it will not work
846 char *e = _("default:LTR");
847 if (strcmp (e, "default:RTL")==0)
848 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
849 else if (strcmp (e, "default:LTR"))
850 g_warning ("Whoever translated default:LTR did so wrongly.\n");
853 /* do what the call to gtk_type_init() used to do */
856 _gtk_accel_map_init ();
858 /* Set the 'initialized' flag.
860 gtk_initialized = TRUE;
862 /* load gtk modules */
863 if (gtk_modules_string)
865 _gtk_modules_init (argc, argv, gtk_modules_string->str);
866 g_string_free (gtk_modules_string, TRUE);
870 _gtk_modules_init (argc, argv, NULL);
877 gboolean open_default_display;
881 pre_parse_hook (GOptionContext *context,
886 do_pre_parse_initialization (NULL, NULL);
892 post_parse_hook (GOptionContext *context,
897 OptionGroupInfo *info = data;
900 do_post_parse_initialization (NULL, NULL);
902 if (info->open_default_display)
904 if (gdk_display_open_default_libgtk_only () == NULL)
906 const char *display_name = gdk_get_display_arg_name ();
909 G_OPTION_ERROR_FAILED,
910 _("Cannot open display: %s"),
911 display_name ? display_name : "" );
922 * gtk_get_debug_flags:
924 * Returns the GTK+ debug flags.
926 * This function is intended for GTK+ modules that want
927 * to adjust their debug output based on GTK+ debug flags.
929 * Returns: the GTK+ debug flags.
932 gtk_get_debug_flags (void)
938 * gtk_set_debug_flags:
940 * Sets the GTK+ debug flags.
943 gtk_set_debug_flags (guint flags)
949 * gtk_get_option_group: (skip)
950 * @open_default_display: whether to open the default display
951 * when parsing the commandline arguments
953 * Returns a #GOptionGroup for the commandline arguments recognized
956 * You should add this group to your #GOptionContext
957 * with g_option_context_add_group(), if you are using
958 * g_option_context_parse() to parse your commandline arguments.
960 * Returns: a #GOptionGroup for the commandline arguments recognized
966 gtk_get_option_group (gboolean open_default_display)
969 OptionGroupInfo *info;
971 gettext_initialization ();
973 info = g_new0 (OptionGroupInfo, 1);
974 info->open_default_display = open_default_display;
976 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
977 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
979 gdk_add_option_entries_libgtk_only (group);
980 g_option_group_add_entries (group, gtk_args);
981 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
987 * gtk_init_with_args:
988 * @argc: a pointer to the number of command line arguments
989 * @argv: (inout) (array length=argc): a pointer to the array of
990 * command line arguments
991 * @parameter_string: a string which is displayed in
992 * the first line of <option>--help</option> output, after
993 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
994 * @entries: (array zero-terminated=1): a %NULL-terminated array
995 * of #GOptionEntrys describing the options of your program
996 * @translation_domain: a translation domain to use for translating
997 * the <option>--help</option> output for the options in @entries
998 * and the @parameter_string with gettext(), or %NULL
999 * @error: a return location for errors
1001 * This function does the same work as gtk_init_check().
1002 * Additionally, it allows you to add your own commandline options,
1003 * and it automatically generates nicely formatted
1004 * <option>--help</option> output. Note that your program will
1005 * be terminated after writing out the help output.
1007 * Returns: %TRUE if the windowing system has been successfully
1008 * initialized, %FALSE otherwise
1013 gtk_init_with_args (gint *argc,
1015 const gchar *parameter_string,
1016 const GOptionEntry *entries,
1017 const gchar *translation_domain,
1020 GOptionContext *context;
1021 GOptionGroup *gtk_group;
1024 if (gtk_initialized)
1025 return gdk_display_open_default_libgtk_only () != NULL;
1027 gettext_initialization ();
1029 if (!check_setugid ())
1032 gtk_group = gtk_get_option_group (TRUE);
1034 context = g_option_context_new (parameter_string);
1035 g_option_context_add_group (context, gtk_group);
1036 g_option_context_set_translation_domain (context, translation_domain);
1039 g_option_context_add_main_entries (context, entries, translation_domain);
1040 retval = g_option_context_parse (context, argc, argv, error);
1042 g_option_context_free (context);
1050 * @argc: (inout): a pointer to the number of command line arguments
1051 * @argv: (array length=argc) (inout): a pointer to the array of
1052 * command line arguments
1054 * Parses command line arguments, and initializes global
1055 * attributes of GTK+, but does not actually open a connection
1056 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
1058 * Any arguments used by GTK+ or GDK are removed from the array and
1059 * @argc and @argv are updated accordingly.
1061 * There is no need to call this function explicitely if you are using
1062 * gtk_init(), or gtk_init_check().
1064 * Return value: %TRUE if initialization succeeded, otherwise %FALSE
1067 gtk_parse_args (int *argc,
1070 GOptionContext *option_context;
1071 GOptionGroup *gtk_group;
1072 GError *error = NULL;
1074 if (gtk_initialized)
1077 gettext_initialization ();
1079 if (!check_setugid ())
1082 option_context = g_option_context_new (NULL);
1083 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1084 g_option_context_set_help_enabled (option_context, FALSE);
1085 gtk_group = gtk_get_option_group (FALSE);
1086 g_option_context_set_main_group (option_context, gtk_group);
1087 if (!g_option_context_parse (option_context, argc, argv, &error))
1089 g_warning ("%s", error->message);
1090 g_error_free (error);
1093 g_option_context_free (option_context);
1098 #ifdef G_PLATFORM_WIN32
1099 #undef gtk_init_check
1104 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1105 * your main() function. Changed if any arguments were handled
1106 * @argv: (array length=argc) (inout) (allow-none): Address of the
1107 * <parameter>argv</parameter> parameter of main()
1108 * Any parameters understood by gtk_init() are stripped before return
1110 * This function does the same work as gtk_init() with only a single
1111 * change: It does not terminate the program if the windowing system
1112 * can't be initialized. Instead it returns %FALSE on failure.
1114 * This way the application can fall back to some other means of
1115 * communication with the user - for example a curses or command line
1118 * Return value: %TRUE if the windowing system has been successfully
1119 * initialized, %FALSE otherwise
1122 gtk_init_check (int *argc,
1125 if (!gtk_parse_args (argc, argv))
1128 return gdk_display_open_default_libgtk_only () != NULL;
1131 #ifdef G_PLATFORM_WIN32
1137 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1138 * your main() function. Changed if any arguments were handled
1139 * @argv: (array length=argc) (inout) (allow-none): Address of the
1140 * <parameter>argv</parameter> parameter of main(). Any options
1141 * understood by GTK+ are stripped before return.
1143 * Call this function before using any other GTK+ functions in your GUI
1144 * applications. It will initialize everything needed to operate the
1145 * toolkit and parses some standard command line options.
1147 * @argc and @argv are adjusted accordingly so your own code will
1148 * never see those standard arguments.
1150 * Note that there are some alternative ways to initialize GTK+:
1151 * if you are calling gtk_parse_args(), gtk_init_check(),
1152 * gtk_init_with_args() or g_option_context_parse() with
1153 * the option group returned by gtk_get_option_group(),
1154 * you <emphasis>don't</emphasis> have to call gtk_init().
1157 * This function will terminate your program if it was unable to
1158 * initialize the windowing system for some reason. If you want
1159 * your program to fall back to a textual interface you want to
1160 * call gtk_init_check() instead.
1164 * Since 2.18, GTK+ calls <literal>signal (SIGPIPE, SIG_IGN)</literal>
1165 * during initialization, to ignore SIGPIPE signals, since these are
1166 * almost never wanted in graphical applications. If you do need to
1167 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1168 * but notice that other libraries (e.g. libdbus or gvfs) might do
1173 gtk_init (int *argc, char ***argv)
1175 if (!gtk_init_check (argc, argv))
1177 const char *display_name_arg = gdk_get_display_arg_name ();
1178 if (display_name_arg == NULL)
1179 display_name_arg = getenv("DISPLAY");
1180 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1187 /* This is relevant when building with gcc for Windows (MinGW),
1188 * where we want to be struct packing compatible with MSVC,
1189 * i.e. use the -mms-bitfields switch.
1190 * For Cygwin there should be no need to be compatible with MSVC,
1191 * so no need to use G_PLATFORM_WIN32.
1195 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1197 if (sizeof_GtkWindow != sizeof (GtkWindow))
1198 g_error ("Incompatible build!\n"
1199 "The code using GTK+ thinks GtkWindow is of different\n"
1200 "size than it actually is in this build of GTK+.\n"
1201 "On Windows, this probably means that you have compiled\n"
1202 "your code with gcc without the -mms-bitfields switch,\n"
1203 "or that you are using an unsupported compiler.");
1206 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1207 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1208 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
1209 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1210 * -fnative-struct still matters. GtkBox is one such.
1213 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1215 if (sizeof_GtkBox != sizeof (GtkBox))
1216 g_error ("Incompatible build!\n"
1217 "The code using GTK+ thinks GtkBox is of different\n"
1218 "size than it actually is in this build of GTK+.\n"
1219 "On Windows, this probably means that you have compiled\n"
1220 "your code with gcc without the -mms-bitfields switch,\n"
1221 "or that you are using an unsupported compiler.");
1224 /* These two functions might get more checks added later, thus pass
1225 * in the number of extra args.
1228 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1230 check_sizeof_GtkWindow (sizeof_GtkWindow);
1231 if (num_checks >= 2)
1232 check_sizeof_GtkBox (sizeof_GtkBox);
1233 gtk_init (argc, argv);
1237 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1239 check_sizeof_GtkWindow (sizeof_GtkWindow);
1240 if (num_checks >= 2)
1241 check_sizeof_GtkBox (sizeof_GtkBox);
1242 return gtk_init_check (argc, argv);
1248 * _gtk_get_lc_ctype:
1250 * Return the Unix-style locale string for the language currently in
1251 * effect. On Unix systems, this is the return value from
1252 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1253 * affect this through the environment variables LC_ALL, LC_CTYPE or
1254 * LANG (checked in that order). The locale strings typically is in
1255 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1256 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1257 * Swedish as written in Finland or pt_BR for Portuguese as written in
1260 * On Windows, the C library doesn't use any such environment
1261 * variables, and setting them won't affect the behaviour of functions
1262 * like ctime(). The user sets the locale through the Regional Options
1263 * in the Control Panel. The C library (in the setlocale() function)
1264 * does not use country and language codes, but country and language
1265 * names spelled out in English.
1266 * However, this function does check the above environment
1267 * variables, and does return a Unix-style locale string based on
1268 * either said environment variables or the thread's current locale.
1270 * Return value: a dynamically allocated string, free with g_free().
1274 _gtk_get_lc_ctype (void)
1277 /* Somebody might try to set the locale for this process using the
1278 * LANG or LC_ environment variables. The Microsoft C library
1279 * doesn't know anything about them. You set the locale in the
1280 * Control Panel. Setting these env vars won't have any affect on
1281 * locale-dependent C library functions like ctime(). But just for
1282 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1283 * it easier to test GTK and Pango in various default languages, you
1284 * don't have to clickety-click in the Control Panel, you can simply
1285 * start the program with LC_ALL=something on the command line.)
1289 p = getenv ("LC_ALL");
1291 return g_strdup (p);
1293 p = getenv ("LC_CTYPE");
1295 return g_strdup (p);
1297 p = getenv ("LANG");
1299 return g_strdup (p);
1301 return g_win32_getlocale ();
1303 return g_strdup (setlocale (LC_CTYPE, NULL));
1308 * gtk_get_default_language:
1310 * Returns the #PangoLanguage for the default language currently in
1311 * effect. (Note that this can change over the life of an
1312 * application.) The default language is derived from the current
1313 * locale. It determines, for example, whether GTK+ uses the
1314 * right-to-left or left-to-right text direction.
1316 * This function is equivalent to pango_language_get_default().
1317 * See that function for details.
1319 * Return value: the default language as a #PangoLanguage,
1323 gtk_get_default_language (void)
1325 return pango_language_get_default ();
1331 * Runs the main loop until gtk_main_quit() is called.
1333 * You can nest calls to gtk_main(). In that case gtk_main_quit()
1334 * will make the innermost invocation of the main loop return.
1341 gtk_main_loop_level++;
1343 loop = g_main_loop_new (NULL, TRUE);
1344 main_loops = g_slist_prepend (main_loops, loop);
1346 if (g_main_loop_is_running (main_loops->data))
1348 GDK_THREADS_LEAVE ();
1349 g_main_loop_run (loop);
1350 GDK_THREADS_ENTER ();
1354 main_loops = g_slist_remove (main_loops, loop);
1356 g_main_loop_unref (loop);
1358 gtk_main_loop_level--;
1360 if (gtk_main_loop_level == 0)
1362 /* Try storing all clipboard data we have */
1363 _gtk_clipboard_store_all ();
1365 /* Synchronize the recent manager singleton */
1366 _gtk_recent_manager_sync ();
1373 * Asks for the current nesting level of the main loop.
1375 * Returns: the nesting level of the current invocation
1379 gtk_main_level (void)
1381 return gtk_main_loop_level;
1387 * Makes the innermost invocation of the main loop return
1388 * when it regains control.
1391 gtk_main_quit (void)
1393 g_return_if_fail (main_loops != NULL);
1395 g_main_loop_quit (main_loops->data);
1399 * gtk_events_pending:
1401 * Checks if any events are pending.
1403 * This can be used to update the UI and invoke timeouts etc.
1404 * while doing some time intensive computation.
1407 * <title>Updating the UI during a long computation</title>
1409 * /* computation going on... */
1411 * while (gtk_events_pending ())
1412 * gtk_main_iteration ();
1414 * /* ...computation continued */
1418 * Returns: %TRUE if any events are pending, %FALSE otherwise
1421 gtk_events_pending (void)
1425 GDK_THREADS_LEAVE ();
1426 result = g_main_context_pending (NULL);
1427 GDK_THREADS_ENTER ();
1433 * gtk_main_iteration:
1435 * Runs a single iteration of the mainloop.
1437 * If no events are waiting to be processed GTK+ will block
1438 * until the next event is noticed. If you don't want to block
1439 * look at gtk_main_iteration_do() or check if any events are
1440 * pending with gtk_events_pending() first.
1442 * Returns: %TRUE if gtk_main_quit() has been called for the
1443 * innermost mainloop
1446 gtk_main_iteration (void)
1448 GDK_THREADS_LEAVE ();
1449 g_main_context_iteration (NULL, TRUE);
1450 GDK_THREADS_ENTER ();
1453 return !g_main_loop_is_running (main_loops->data);
1459 * gtk_main_iteration_do:
1460 * @blocking: %TRUE if you want GTK+ to block if no events are pending
1462 * Runs a single iteration of the mainloop.
1463 * If no events are available either return or block depending on
1464 * the value of @blocking.
1466 * Returns: %TRUE if gtk_main_quit() has been called for the
1467 * innermost mainloop
1470 gtk_main_iteration_do (gboolean blocking)
1472 GDK_THREADS_LEAVE ();
1473 g_main_context_iteration (NULL, blocking);
1474 GDK_THREADS_ENTER ();
1477 return !g_main_loop_is_running (main_loops->data);
1482 /* private libgtk to libgdk interfaces */
1483 gboolean gdk_device_grab_info_libgtk_only (GdkDisplay *display,
1485 GdkWindow **grab_window,
1486 gboolean *owner_events);
1489 rewrite_events_translate (GdkWindow *old_window,
1490 GdkWindow *new_window,
1494 gint old_origin_x, old_origin_y;
1495 gint new_origin_x, new_origin_y;
1497 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1498 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1500 *x += old_origin_x - new_origin_x;
1501 *y += old_origin_y - new_origin_y;
1505 rewrite_event_for_window (GdkEvent *event,
1506 GdkWindow *new_window)
1508 event = gdk_event_copy (event);
1510 switch (event->type)
1513 rewrite_events_translate (event->any.window,
1515 &event->scroll.x, &event->scroll.y);
1517 case GDK_BUTTON_PRESS:
1518 case GDK_2BUTTON_PRESS:
1519 case GDK_3BUTTON_PRESS:
1520 case GDK_BUTTON_RELEASE:
1521 rewrite_events_translate (event->any.window,
1523 &event->button.x, &event->button.y);
1525 case GDK_MOTION_NOTIFY:
1526 rewrite_events_translate (event->any.window,
1528 &event->motion.x, &event->motion.y);
1531 case GDK_KEY_RELEASE:
1532 case GDK_PROXIMITY_IN:
1533 case GDK_PROXIMITY_OUT:
1540 g_object_unref (event->any.window);
1541 event->any.window = g_object_ref (new_window);
1546 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1547 * then what X11 does is deliver the event normally if it was going to this
1548 * client, otherwise, delivers it in terms of the grab window. This function
1549 * rewrites events to the effect that events going to the same window group
1550 * are delivered normally, otherwise, the event is delivered in terms of the
1554 rewrite_event_for_grabs (GdkEvent *event)
1556 GdkWindow *grab_window;
1557 GtkWidget *event_widget, *grab_widget;
1558 gpointer grab_widget_ptr;
1559 gboolean owner_events;
1560 GdkDisplay *display;
1563 switch (event->type)
1566 case GDK_BUTTON_PRESS:
1567 case GDK_2BUTTON_PRESS:
1568 case GDK_3BUTTON_PRESS:
1569 case GDK_BUTTON_RELEASE:
1570 case GDK_MOTION_NOTIFY:
1571 case GDK_PROXIMITY_IN:
1572 case GDK_PROXIMITY_OUT:
1574 case GDK_KEY_RELEASE:
1575 display = gdk_window_get_display (event->any.window);
1576 device = gdk_event_get_device (event);
1578 if (!gdk_device_grab_info_libgtk_only (display, device, &grab_window, &owner_events) ||
1586 event_widget = gtk_get_event_widget (event);
1587 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1588 grab_widget = grab_widget_ptr;
1591 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1592 return rewrite_event_for_window (event, grab_window);
1598 * gtk_main_do_event:
1599 * @event: An event to process (normally passed by GDK)
1601 * Processes a single GDK event.
1603 * This is public only to allow filtering of events between GDK and GTK+.
1604 * You will not usually need to call this function directly.
1606 * While you should not call this function directly, you might want to
1607 * know how exactly events are handled. So here is what this function
1608 * does with the event:
1612 * Compress enter/leave notify events. If the event passed build an
1613 * enter/leave pair together with the next event (peeked from GDK), both
1614 * events are thrown away. This is to avoid a backlog of (de-)highlighting
1615 * widgets crossed by the pointer.
1616 * </para></listitem>
1618 * Find the widget which got the event. If the widget can't be determined
1619 * the event is thrown away unless it belongs to a INCR transaction. In that
1620 * case it is passed to gtk_selection_incr_event().
1621 * </para></listitem>
1623 * Then the event is pushed onto a stack so you can query the currently
1624 * handled event with gtk_get_current_event().
1625 * </para></listitem>
1627 * The event is sent to a widget. If a grab is active all events for widgets
1628 * that are not in the contained in the grab widget are sent to the latter
1629 * with a few exceptions:
1632 * Deletion and destruction events are still sent to the event widget for
1634 * </para></listitem>
1636 * Events which directly relate to the visual representation of the event
1638 * </para></listitem>
1640 * Leave events are delivered to the event widget if there was an enter
1641 * event delivered to it before without the paired leave event.
1642 * </para></listitem>
1644 * Drag events are not redirected because it is unclear what the semantics
1646 * </para></listitem>
1648 * Another point of interest might be that all key events are first passed
1649 * through the key snooper functions if there are any. Read the description
1650 * of gtk_key_snooper_install() if you need this feature.
1651 * </para></listitem>
1653 * After finishing the delivery the event is popped from the event stack.
1654 * </para></listitem>
1658 gtk_main_do_event (GdkEvent *event)
1660 GtkWidget *event_widget;
1661 GtkWidget *grab_widget = NULL;
1662 GtkWindowGroup *window_group;
1663 GdkEvent *rewritten_event = NULL;
1667 if (event->type == GDK_SETTING)
1669 _gtk_settings_handle_event (&event->setting);
1673 if (event->type == GDK_OWNER_CHANGE)
1675 _gtk_clipboard_handle_event (&event->owner_change);
1679 /* Find the widget which got the event. We store the widget
1680 * in the user_data field of GdkWindow's. Ignore the event
1681 * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1682 * events which are handled specially. Though this happens rarely,
1683 * bogus events can occur for e.g. destroyed GdkWindows.
1685 event_widget = gtk_get_event_widget (event);
1688 /* To handle selection INCR transactions, we select
1689 * PropertyNotify events on the requestor window and create
1690 * a corresponding (fake) GdkWindow so that events get here.
1691 * There won't be a widget though, so we have to handle
1694 if (event->type == GDK_PROPERTY_NOTIFY)
1695 _gtk_selection_incr_event (event->any.window,
1701 /* If pointer or keyboard grabs are in effect, munge the events
1702 * so that each window group looks like a separate app.
1704 rewritten_event = rewrite_event_for_grabs (event);
1705 if (rewritten_event)
1707 event = rewritten_event;
1708 event_widget = gtk_get_event_widget (event);
1711 window_group = gtk_main_get_window_group (event_widget);
1712 device = gdk_event_get_device (event);
1714 /* check whether there is a (device) grab in effect... */
1716 grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1719 grab_widget = gtk_window_group_get_current_grab (window_group);
1721 /* If the grab widget is an ancestor of the event widget
1722 * then we send the event to the original event widget.
1723 * This is the key to implementing modality.
1726 (gtk_widget_is_sensitive (event_widget) &&
1727 gtk_widget_is_ancestor (event_widget, grab_widget)))
1728 grab_widget = event_widget;
1730 /* If the widget receiving events is actually blocked by another
1734 _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1736 if (rewritten_event)
1737 gdk_event_free (rewritten_event);
1742 /* Push the event onto a stack of current events for
1743 * gtk_current_event_get().
1745 current_events = g_list_prepend (current_events, event);
1747 /* Not all events get sent to the grabbing widget.
1748 * The delete, destroy, expose, focus change and resize
1749 * events still get sent to the event widget because
1750 * 1) these events have no meaning for the grabbing widget
1751 * and 2) redirecting these events to the grabbing widget
1752 * could cause the display to be messed up.
1754 * Drag events are also not redirected, since it isn't
1755 * clear what the semantics of that would be.
1757 switch (event->type)
1763 g_object_ref (event_widget);
1764 if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1765 !gtk_widget_event (event_widget, event))
1766 gtk_widget_destroy (event_widget);
1767 g_object_unref (event_widget);
1771 /* Unexpected GDK_DESTROY from the outside, ignore for
1772 * child windows, handle like a GDK_DELETE for toplevels
1774 if (!gtk_widget_get_parent (event_widget))
1776 g_object_ref (event_widget);
1777 if (!gtk_widget_event (event_widget, event) &&
1778 gtk_widget_get_realized (event_widget))
1779 gtk_widget_destroy (event_widget);
1780 g_object_unref (event_widget);
1785 if (event->any.window && gtk_widget_get_double_buffered (event_widget))
1787 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1788 gtk_widget_send_expose (event_widget, event);
1789 gdk_window_end_paint (event->any.window);
1793 /* The app may paint with a previously allocated cairo_t,
1794 * which will draw directly to the window. We can't catch cairo
1795 * draw operations to automatically flush the window, thus we
1796 * need to explicitly flush any outstanding moves or double
1799 gdk_window_flush (event->any.window);
1800 gtk_widget_send_expose (event_widget, event);
1804 case GDK_PROPERTY_NOTIFY:
1805 case GDK_FOCUS_CHANGE:
1809 case GDK_SELECTION_CLEAR:
1810 case GDK_SELECTION_REQUEST:
1811 case GDK_SELECTION_NOTIFY:
1812 case GDK_CLIENT_EVENT:
1813 case GDK_VISIBILITY_NOTIFY:
1814 case GDK_WINDOW_STATE:
1815 case GDK_GRAB_BROKEN:
1817 gtk_widget_event (event_widget, event);
1821 case GDK_BUTTON_PRESS:
1822 case GDK_2BUTTON_PRESS:
1823 case GDK_3BUTTON_PRESS:
1824 gtk_propagate_event (grab_widget, event);
1828 case GDK_KEY_RELEASE:
1831 if (gtk_invoke_key_snoopers (grab_widget, event))
1834 /* Catch alt press to enable auto-mnemonics;
1835 * menus are handled elsewhere
1837 if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1838 !GTK_IS_MENU_SHELL (grab_widget))
1840 gboolean auto_mnemonics;
1842 g_object_get (gtk_widget_get_settings (grab_widget),
1843 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
1847 gboolean mnemonics_visible;
1850 mnemonics_visible = (event->type == GDK_KEY_PRESS);
1852 window = gtk_widget_get_toplevel (grab_widget);
1854 if (GTK_IS_WINDOW (window))
1855 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), mnemonics_visible);
1858 /* else fall through */
1859 case GDK_MOTION_NOTIFY:
1860 case GDK_BUTTON_RELEASE:
1861 case GDK_PROXIMITY_IN:
1862 case GDK_PROXIMITY_OUT:
1863 gtk_propagate_event (grab_widget, event);
1866 case GDK_ENTER_NOTIFY:
1867 _gtk_widget_set_device_window (event_widget,
1868 gdk_event_get_device (event),
1870 if (gtk_widget_is_sensitive (grab_widget))
1871 gtk_widget_event (grab_widget, event);
1874 case GDK_LEAVE_NOTIFY:
1875 _gtk_widget_set_device_window (event_widget,
1876 gdk_event_get_device (event),
1878 if (gtk_widget_is_sensitive (grab_widget))
1879 gtk_widget_event (grab_widget, event);
1882 case GDK_DRAG_STATUS:
1883 case GDK_DROP_FINISHED:
1884 _gtk_drag_source_handle_event (event_widget, event);
1886 case GDK_DRAG_ENTER:
1887 case GDK_DRAG_LEAVE:
1888 case GDK_DRAG_MOTION:
1889 case GDK_DROP_START:
1890 _gtk_drag_dest_handle_event (event_widget, event);
1893 g_assert_not_reached ();
1897 if (event->type == GDK_ENTER_NOTIFY
1898 || event->type == GDK_LEAVE_NOTIFY
1899 || event->type == GDK_BUTTON_PRESS
1900 || event->type == GDK_2BUTTON_PRESS
1901 || event->type == GDK_3BUTTON_PRESS
1902 || event->type == GDK_KEY_PRESS
1903 || event->type == GDK_DRAG_ENTER
1904 || event->type == GDK_GRAB_BROKEN
1905 || event->type == GDK_MOTION_NOTIFY
1906 || event->type == GDK_SCROLL)
1908 _gtk_tooltip_handle_event (event);
1911 tmp_list = current_events;
1912 current_events = g_list_remove_link (current_events, tmp_list);
1913 g_list_free_1 (tmp_list);
1915 if (rewritten_event)
1916 gdk_event_free (rewritten_event);
1922 * All this function does it to return %TRUE.
1924 * This can be useful for example if you want to inhibit the deletion
1925 * of a window. Of course you should not do this as the user expects
1926 * a reaction from clicking the close icon of the window...
1929 * <title>A persistent window</title>
1931 * #include <gtk/gtk.h><
1934 * main (int argc, char **argv)
1936 * GtkWidget *win, *but;
1938 * gtk_init (&argc, &argv);
1940 * win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1941 * g_signal_connect (win, "delete-event",
1942 * G_CALLBACK (gtk_true), NULL);
1943 * g_signal_connect (win, "destroy",
1944 * G_CALLBACK (gtk_main_quit), NULL);
1946 * but = gtk_button_new_with_label ("Close yourself. I mean it!");
1947 * g_signal_connect_swapped (but, "clicked",
1948 * G_CALLBACK (gtk_object_destroy), win);
1949 * gtk_container_add (GTK_CONTAINER (win), but);
1951 * gtk_widget_show_all (win);
1971 * Analogical to gtk_true(), this function does nothing
1972 * but always returns %FALSE.
1982 static GtkWindowGroup *
1983 gtk_main_get_window_group (GtkWidget *widget)
1985 GtkWidget *toplevel = NULL;
1988 toplevel = gtk_widget_get_toplevel (widget);
1990 if (GTK_IS_WINDOW (toplevel))
1991 return gtk_window_get_group (GTK_WINDOW (toplevel));
1993 return gtk_window_get_group (NULL);
1998 GtkWidget *old_grab_widget;
1999 GtkWidget *new_grab_widget;
2000 gboolean was_grabbed;
2001 gboolean is_grabbed;
2003 GList *notified_windows;
2008 synth_crossing_for_grab_notify (GtkWidget *from,
2010 GrabNotifyInfo *info,
2012 GdkCrossingMode mode)
2016 GdkDevice *device = devices->data;
2017 GdkWindow *from_window, *to_window;
2019 /* Do not propagate events more than once to
2020 * the same windows if non-multidevice aware.
2026 from_window = _gtk_widget_get_device_window (from, device);
2029 !gdk_window_get_support_multidevice (from_window) &&
2030 g_list_find (info->notified_windows, from_window))
2038 to_window = _gtk_widget_get_device_window (to, device);
2041 !gdk_window_get_support_multidevice (to_window) &&
2042 g_list_find (info->notified_windows, to_window))
2046 if (from_window || to_window)
2048 _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
2049 (to_window) ? to : NULL,
2053 info->notified_windows = g_list_prepend (info->notified_windows, from_window);
2056 info->notified_windows = g_list_prepend (info->notified_windows, to_window);
2059 devices = devices->next;
2064 gtk_grab_notify_foreach (GtkWidget *child,
2067 GrabNotifyInfo *info = data;
2068 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
2071 was_grabbed = info->was_grabbed;
2072 is_grabbed = info->is_grabbed;
2074 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
2075 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
2077 was_shadowed = info->old_grab_widget && !info->was_grabbed;
2078 is_shadowed = info->new_grab_widget && !info->is_grabbed;
2080 g_object_ref (child);
2082 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
2083 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
2086 _gtk_widget_get_device_window (child, info->device))
2088 /* Device specified and is on widget */
2089 devices = g_list_prepend (NULL, info->device);
2092 devices = _gtk_widget_list_devices (child);
2096 _gtk_widget_set_shadowed (child, TRUE);
2097 if (!was_shadowed && devices &&
2098 gtk_widget_is_sensitive (child))
2099 synth_crossing_for_grab_notify (child, info->new_grab_widget,
2101 GDK_CROSSING_GTK_GRAB);
2105 _gtk_widget_set_shadowed (child, FALSE);
2106 if (was_shadowed && devices &&
2107 gtk_widget_is_sensitive (child))
2108 synth_crossing_for_grab_notify (info->old_grab_widget, child,
2110 info->from_grab ? GDK_CROSSING_GTK_GRAB :
2111 GDK_CROSSING_GTK_UNGRAB);
2114 if (was_shadowed != is_shadowed)
2115 _gtk_widget_grab_notify (child, was_shadowed);
2117 g_object_unref (child);
2118 g_list_free (devices);
2120 info->was_grabbed = was_grabbed;
2121 info->is_grabbed = is_grabbed;
2125 gtk_grab_notify (GtkWindowGroup *group,
2127 GtkWidget *old_grab_widget,
2128 GtkWidget *new_grab_widget,
2132 GrabNotifyInfo info = { 0 };
2134 if (old_grab_widget == new_grab_widget)
2137 info.old_grab_widget = old_grab_widget;
2138 info.new_grab_widget = new_grab_widget;
2139 info.from_grab = from_grab;
2140 info.device = device;
2142 g_object_ref (group);
2144 toplevels = gtk_window_list_toplevels ();
2145 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
2149 GtkWindow *toplevel = toplevels->data;
2150 toplevels = g_list_delete_link (toplevels, toplevels);
2152 info.was_grabbed = FALSE;
2153 info.is_grabbed = FALSE;
2155 if (group == gtk_window_get_group (toplevel))
2156 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
2157 g_object_unref (toplevel);
2160 g_list_free (info.notified_windows);
2161 g_object_unref (group);
2165 * gtk_grab_add: (method)
2166 * @widget: The widget that grabs keyboard and pointer events
2168 * Makes @widget the current grabbed widget.
2170 * This means that interaction with other widgets in the same
2171 * application is blocked and mouse as well as keyboard events
2172 * are delivered to this widget.
2174 * If @widget is not sensitive, it is not set as the current
2175 * grabbed widget and this function does nothing.
2178 gtk_grab_add (GtkWidget *widget)
2180 GtkWindowGroup *group;
2181 GtkWidget *old_grab_widget;
2183 g_return_if_fail (widget != NULL);
2185 if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2187 _gtk_widget_set_has_grab (widget, TRUE);
2189 group = gtk_main_get_window_group (widget);
2191 old_grab_widget = gtk_window_group_get_current_grab (group);
2193 g_object_ref (widget);
2194 _gtk_window_group_add_grab (group, widget);
2196 gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2201 * gtk_grab_get_current:
2203 * Queries the current grab of the default window group.
2205 * Return value: (transfer none): The widget which currently
2206 * has the grab or %NULL if no grab is active
2209 gtk_grab_get_current (void)
2211 GtkWindowGroup *group;
2213 group = gtk_main_get_window_group (NULL);
2215 return gtk_window_group_get_current_grab (group);
2219 * gtk_grab_remove: (method)
2220 * @widget: The widget which gives up the grab
2222 * Removes the grab from the given widget.
2224 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2226 * If @widget does not have the grab, this function does nothing.
2229 gtk_grab_remove (GtkWidget *widget)
2231 GtkWindowGroup *group;
2232 GtkWidget *new_grab_widget;
2234 g_return_if_fail (widget != NULL);
2236 if (gtk_widget_has_grab (widget))
2238 _gtk_widget_set_has_grab (widget, FALSE);
2240 group = gtk_main_get_window_group (widget);
2241 _gtk_window_group_remove_grab (group, widget);
2242 new_grab_widget = gtk_window_group_get_current_grab (group);
2244 gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2246 g_object_unref (widget);
2251 * gtk_device_grab_add:
2252 * @widget: a #GtkWidget
2253 * @device: a #GtkDevice to grab on.
2254 * @block_others: %TRUE to prevent other devices to interact with @widget.
2256 * Adds a GTK+ grab on @device, so all the events on @device and its
2257 * associated pointer or keyboard (if any) are delivered to @widget.
2258 * If the @block_others parameter is %TRUE, any other devices will be
2259 * unable to interact with @widget during the grab.
2264 gtk_device_grab_add (GtkWidget *widget,
2266 gboolean block_others)
2268 GtkWindowGroup *group;
2269 GtkWidget *old_grab_widget;
2271 g_return_if_fail (GTK_IS_WIDGET (widget));
2272 g_return_if_fail (GDK_IS_DEVICE (device));
2274 group = gtk_main_get_window_group (widget);
2275 old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2277 if (old_grab_widget != widget)
2278 _gtk_window_group_add_device_grab (group, widget, device, block_others);
2280 gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2284 * gtk_device_grab_remove:
2285 * @widget: a #GtkWidget
2286 * @device: a #GdkDevice
2288 * Removes a device grab from the given widget.
2290 * You have to pair calls to gtk_device_grab_add() and
2291 * gtk_device_grab_remove().
2296 gtk_device_grab_remove (GtkWidget *widget,
2299 GtkWindowGroup *group;
2300 GtkWidget *new_grab_widget;
2302 g_return_if_fail (GTK_IS_WIDGET (widget));
2303 g_return_if_fail (GDK_IS_DEVICE (device));
2305 group = gtk_main_get_window_group (widget);
2306 _gtk_window_group_remove_device_grab (group, widget, device);
2307 new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2309 gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2313 * gtk_key_snooper_install: (skip)
2314 * @snooper: a #GtkKeySnoopFunc
2315 * @func_data: data to pass to @snooper
2317 * Installs a key snooper function, which will get called on all
2318 * key events before delivering them normally.
2320 * Returns: a unique id for this key snooper for use with
2321 * gtk_key_snooper_remove().
2324 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2327 GtkKeySnooperData *data;
2328 static guint snooper_id = 1;
2330 g_return_val_if_fail (snooper != NULL, 0);
2332 data = g_new (GtkKeySnooperData, 1);
2333 data->func = snooper;
2334 data->func_data = func_data;
2335 data->id = snooper_id++;
2336 key_snoopers = g_slist_prepend (key_snoopers, data);
2342 * gtk_key_snooper_remove:
2343 * @snooper_handler_id: Identifies the key snooper to remove
2345 * Removes the key snooper function with the given id.
2348 gtk_key_snooper_remove (guint snooper_id)
2350 GtkKeySnooperData *data = NULL;
2353 slist = key_snoopers;
2357 if (data->id == snooper_id)
2360 slist = slist->next;
2365 key_snoopers = g_slist_remove (key_snoopers, data);
2371 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2375 gint return_val = FALSE;
2377 slist = key_snoopers;
2378 while (slist && !return_val)
2380 GtkKeySnooperData *data;
2383 slist = slist->next;
2384 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2391 * gtk_get_current_event:
2393 * Obtains a copy of the event currently being processed by GTK+.
2395 * For example, if you are handling a #GtkButton::clicked signal,
2396 * the current event will be the #GdkEventButton that triggered
2397 * the ::clicked signal.
2399 * Return value: (transfer full): a copy of the current event, or
2400 * %NULL if there is no current event. The returned event must be
2401 * freed with gdk_event_free().
2404 gtk_get_current_event (void)
2407 return gdk_event_copy (current_events->data);
2413 * gtk_get_current_event_time:
2415 * If there is a current event and it has a timestamp,
2416 * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2418 * Return value: the timestamp from the current event,
2419 * or %GDK_CURRENT_TIME.
2422 gtk_get_current_event_time (void)
2425 return gdk_event_get_time (current_events->data);
2427 return GDK_CURRENT_TIME;
2431 * gtk_get_current_event_state:
2432 * @state: (out): a location to store the state of the current event
2434 * If there is a current event and it has a state field, place
2435 * that state field in @state and return %TRUE, otherwise return
2438 * Return value: %TRUE if there was a current event and it
2442 gtk_get_current_event_state (GdkModifierType *state)
2444 g_return_val_if_fail (state != NULL, FALSE);
2447 return gdk_event_get_state (current_events->data, state);
2456 * gtk_get_current_event_device:
2458 * If there is a current event and it has a device, return that
2459 * device, otherwise return %NULL.
2461 * Returns: (transfer none): a #GdkDevice, or %NULL
2464 gtk_get_current_event_device (void)
2467 return gdk_event_get_device (current_events->data);
2473 * gtk_get_event_widget:
2474 * @event: a #GdkEvent
2476 * If @event is %NULL or the event was not associated with any widget,
2477 * returns %NULL, otherwise returns the widget that received the event
2480 * Return value: (transfer none): the widget that originally
2481 * received @event, or %NULL
2484 gtk_get_event_widget (GdkEvent *event)
2487 gpointer widget_ptr;
2490 if (event && event->any.window &&
2491 (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2493 gdk_window_get_user_data (event->any.window, &widget_ptr);
2494 widget = widget_ptr;
2501 * gtk_propagate_event:
2502 * @widget: a #GtkWidget
2505 * Sends an event to a widget, propagating the event to parent widgets
2506 * if the event remains unhandled.
2508 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2509 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2510 * the event may be propagated; if so, this function is used.
2512 * gtk_propagate_event() calls gtk_widget_event() on each widget it
2513 * decides to send the event to. So gtk_widget_event() is the lowest-level
2514 * function; it simply emits the #GtkWidget::event and possibly an
2515 * event-specific signal on a widget. gtk_propagate_event() is a bit
2516 * higher-level, and gtk_main_do_event() is the highest level.
2518 * All that said, you most likely don't want to use any of these
2519 * functions; synthesizing events is rarely needed. There are almost
2520 * certainly better ways to achieve your goals. For example, use
2521 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2522 * of making up expose events.
2525 gtk_propagate_event (GtkWidget *widget,
2530 g_return_if_fail (GTK_IS_WIDGET (widget));
2531 g_return_if_fail (event != NULL);
2533 handled_event = FALSE;
2535 g_object_ref (widget);
2537 if ((event->type == GDK_KEY_PRESS) ||
2538 (event->type == GDK_KEY_RELEASE))
2540 /* Only send key events within Window widgets to the Window
2541 * The Window widget will in turn pass the
2542 * key event on to the currently focused widget
2547 window = gtk_widget_get_toplevel (widget);
2548 if (GTK_IS_WINDOW (window))
2550 /* If there is a grab within the window, give the grab widget
2551 * a first crack at the key event
2553 if (widget != window && gtk_widget_has_grab (widget))
2554 handled_event = gtk_widget_event (widget, event);
2558 window = gtk_widget_get_toplevel (widget);
2559 if (GTK_IS_WINDOW (window))
2561 if (gtk_widget_is_sensitive (window))
2562 gtk_widget_event (window, event);
2566 handled_event = TRUE; /* don't send to widget */
2570 /* Other events get propagated up the widget tree
2571 * so that parents can see the button and motion
2572 * events of the children.
2580 /* Scroll events are special cased here because it
2581 * feels wrong when scrolling a GtkViewport, say,
2582 * to have children of the viewport eat the scroll
2585 if (!gtk_widget_is_sensitive (widget))
2586 handled_event = event->type != GDK_SCROLL;
2588 handled_event = gtk_widget_event (widget, event);
2590 tmp = gtk_widget_get_parent (widget);
2591 g_object_unref (widget);
2595 if (!handled_event && widget)
2596 g_object_ref (widget);
2602 g_object_unref (widget);
2606 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2607 GValue *return_accu,
2608 const GValue *handler_return,
2611 gboolean continue_emission;
2612 gboolean signal_handled;
2614 signal_handled = g_value_get_boolean (handler_return);
2615 g_value_set_boolean (return_accu, signal_handled);
2616 continue_emission = !signal_handled;
2618 return continue_emission;