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"
123 #include "gtkrecentmanager.h"
124 #include "gtkselectionprivate.h"
125 #include "gtksettingsprivate.h"
126 #include "gtkwidgetprivate.h"
127 #include "gtkwindowprivate.h"
128 #include "gtktooltip.h"
129 #include "gtkdebug.h"
134 static HMODULE gtk_dll;
137 DllMain (HINSTANCE hinstDLL,
143 case DLL_PROCESS_ATTACH:
144 gtk_dll = (HMODULE) hinstDLL;
151 /* These here before inclusion of gtkprivate.h so that the original
152 * GTK_LIBDIR and GTK_LOCALEDIR definitions are seen. Yeah, this is a
156 _gtk_get_libdir (void)
158 static char *gtk_libdir = NULL;
159 if (gtk_libdir == NULL)
161 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
162 gchar *slash = strrchr (root, '\\');
163 if (g_ascii_strcasecmp (slash + 1, ".libs") == 0)
164 gtk_libdir = GTK_LIBDIR;
166 gtk_libdir = g_build_filename (root, "lib", NULL);
174 _gtk_get_localedir (void)
176 static char *gtk_localedir = NULL;
177 if (gtk_localedir == NULL)
182 /* GTK_LOCALEDIR ends in either /lib/locale or
183 * /share/locale. Scan for that slash.
185 p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
191 root = g_win32_get_package_installation_directory_of_module (gtk_dll);
192 temp = g_build_filename (root, p, NULL);
195 /* gtk_localedir is passed to bindtextdomain() which isn't
198 gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
201 return gtk_localedir;
206 #include "gtkprivate.h"
208 /* Private type definitions
210 typedef struct _GtkKeySnooperData GtkKeySnooperData;
212 struct _GtkKeySnooperData
214 GtkKeySnoopFunc func;
219 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
222 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
224 static guint gtk_main_loop_level = 0;
225 static gint pre_initialized = FALSE;
226 static gint gtk_initialized = FALSE;
227 static GList *current_events = NULL;
229 static GSList *main_loops = NULL; /* stack of currently executing main loops */
231 static GSList *key_snoopers = NULL;
233 static guint debug_flags = 0; /* Global GTK debug flag */
235 #ifdef G_ENABLE_DEBUG
236 static const GDebugKey gtk_debug_keys[] = {
237 {"misc", GTK_DEBUG_MISC},
238 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
239 {"text", GTK_DEBUG_TEXT},
240 {"tree", GTK_DEBUG_TREE},
241 {"updates", GTK_DEBUG_UPDATES},
242 {"keybindings", GTK_DEBUG_KEYBINDINGS},
243 {"multihead", GTK_DEBUG_MULTIHEAD},
244 {"modules", GTK_DEBUG_MODULES},
245 {"geometry", GTK_DEBUG_GEOMETRY},
246 {"icontheme", GTK_DEBUG_ICONTHEME},
247 {"printing", GTK_DEBUG_PRINTING},
248 {"builder", GTK_DEBUG_BUILDER},
249 {"size-request", GTK_DEBUG_SIZE_REQUEST},
251 #endif /* G_ENABLE_DEBUG */
254 * gtk_get_major_version:
256 * Returns the major version number of the GTK+ library.
257 * (e.g. in GTK+ version 3.1.5 this is 3.)
259 * This function is in the library, so it represents the GTK+ library
260 * your code is running against. Contrast with the #GTK_MAJOR_VERSION
261 * macro, which represents the major version of the GTK+ headers you
262 * have included when compiling your code.
264 * Returns: the major version number of the GTK+ library
269 gtk_get_major_version (void)
271 return GTK_MAJOR_VERSION;
275 * gtk_get_minor_version:
277 * Returns the minor version number of the GTK+ library.
278 * (e.g. in GTK+ version 3.1.5 this is 1.)
280 * This function is in the library, so it represents the GTK+ library
281 * your code is are running against. Contrast with the
282 * #GTK_MINOR_VERSION macro, which represents the minor version of the
283 * GTK+ headers you have included when compiling your code.
285 * Returns: the minor version number of the GTK+ library
290 gtk_get_minor_version (void)
292 return GTK_MINOR_VERSION;
296 * gtk_get_micro_version:
298 * Returns the micro version number of the GTK+ library.
299 * (e.g. in GTK+ version 3.1.5 this is 5.)
301 * This function is in the library, so it represents the GTK+ library
302 * your code is are running against. Contrast with the
303 * #GTK_MICRO_VERSION macro, which represents the micro version of the
304 * GTK+ headers you have included when compiling your code.
306 * Returns: the micro version number of the GTK+ library
311 gtk_get_micro_version (void)
313 return GTK_MICRO_VERSION;
317 * gtk_get_binary_age:
319 * Returns the binary age as passed to <application>libtool</application>
320 * when building the GTK+ library the process is running against.
321 * If <application>libtool</application> means nothing to you, don't
324 * Returns: the binary age of the GTK+ library
329 gtk_get_binary_age (void)
331 return GTK_BINARY_AGE;
335 * gtk_get_interface_age:
337 * Returns the interface age as passed to <application>libtool</application>
338 * when building the GTK+ library the process is running against.
339 * If <application>libtool</application> means nothing to you, don't
342 * Returns: the interface age of the GTK+ library
347 gtk_get_interface_age (void)
349 return GTK_INTERFACE_AGE;
354 * @required_major: the required major version
355 * @required_minor: the required minor version
356 * @required_micro: the required micro version
358 * Checks that the GTK+ library in use is compatible with the
359 * given version. Generally you would pass in the constants
360 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
361 * as the three arguments to this function; that produces
362 * a check that the library in use is compatible with
363 * the version of GTK+ the application or module was compiled
366 * Compatibility is defined by two things: first the version
367 * of the running library is newer than the version
368 * @required_major.required_minor.@required_micro. Second
369 * the running library must be binary compatible with the
370 * version @required_major.required_minor.@required_micro
371 * (same major version.)
373 * This function is primarily for GTK+ modules; the module
374 * can call this function to check that it wasn't loaded
375 * into an incompatible version of GTK+. However, such a
376 * check isn't completely reliable, since the module may be
377 * linked against an old version of GTK+ and calling the
378 * old version of gtk_check_version(), but still get loaded
379 * into an application using a newer version of GTK+.
381 * Return value: %NULL if the GTK+ library is compatible with the
382 * given version, or a string describing the version mismatch.
383 * The returned string is owned by GTK+ and should not be modified
387 gtk_check_version (guint required_major,
388 guint required_minor,
389 guint required_micro)
391 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
392 gint required_effective_micro = 100 * required_minor + required_micro;
394 if (required_major > GTK_MAJOR_VERSION)
395 return "GTK+ version too old (major mismatch)";
396 if (required_major < GTK_MAJOR_VERSION)
397 return "GTK+ version too new (major mismatch)";
398 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
399 return "GTK+ version too new (micro mismatch)";
400 if (required_effective_micro > gtk_effective_micro)
401 return "GTK+ version too old (micro mismatch)";
405 /* This checks to see if the process is running suid or sgid
406 * at the current time. If so, we don't allow GTK+ to be initialized.
407 * This is meant to be a mild check - we only error out if we
408 * can prove the programmer is doing something wrong, not if
409 * they could be doing something wrong. For this reason, we
410 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
415 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
417 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
418 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
420 #ifdef HAVE_GETRESUID
421 /* These aren't in the header files, so we prototype them here.
423 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
424 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
426 if (getresuid (&ruid, &euid, &suid) != 0 ||
427 getresgid (&rgid, &egid, &sgid) != 0)
428 #endif /* HAVE_GETRESUID */
430 suid = ruid = getuid ();
431 sgid = rgid = getgid ();
436 if (ruid != euid || ruid != suid ||
437 rgid != egid || rgid != sgid)
439 g_warning ("This process is currently running setuid or setgid.\n"
440 "This is not a supported use of GTK+. You must create a helper\n"
441 "program instead. For further details, see:\n\n"
442 " http://www.gtk.org/setuid.html\n\n"
443 "Refusing to initialize GTK+.");
453 _gtk_get_datadir (void)
455 static char *gtk_datadir = NULL;
456 if (gtk_datadir == NULL)
458 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
459 gtk_datadir = g_build_filename (root, "share", NULL);
467 _gtk_get_sysconfdir (void)
469 static char *gtk_sysconfdir = NULL;
470 if (gtk_sysconfdir == NULL)
472 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
473 gtk_sysconfdir = g_build_filename (root, "etc", NULL);
477 return gtk_sysconfdir;
481 _gtk_get_data_prefix (void)
483 static char *gtk_data_prefix = NULL;
484 if (gtk_data_prefix == NULL)
485 gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);
487 return gtk_data_prefix;
490 #endif /* G_OS_WIN32 */
492 static gboolean do_setlocale = TRUE;
495 * gtk_disable_setlocale:
497 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
498 * gtk_parse_args() from automatically
499 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
500 * want to use this function if you wanted to set the locale for
501 * your program to something other than the user's locale, or if
502 * you wanted to set different values for different locale categories.
504 * Most programs should not need to call this function.
507 gtk_disable_setlocale (void)
510 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
512 do_setlocale = FALSE;
515 #ifdef G_PLATFORM_WIN32
516 #undef gtk_init_check
519 static GString *gtk_modules_string = NULL;
520 static gboolean g_fatal_warnings = FALSE;
522 #ifdef G_ENABLE_DEBUG
524 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
526 debug_flags |= g_parse_debug_string (value,
528 G_N_ELEMENTS (gtk_debug_keys));
534 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
536 debug_flags &= ~g_parse_debug_string (value,
538 G_N_ELEMENTS (gtk_debug_keys));
542 #endif /* G_ENABLE_DEBUG */
545 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
549 if (gtk_modules_string)
550 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
552 gtk_modules_string = g_string_new (NULL);
554 g_string_append (gtk_modules_string, value);
560 static const GOptionEntry gtk_args[] = {
561 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
562 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
563 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
564 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
565 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
566 #ifdef G_ENABLE_DEBUG
567 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
568 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
569 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
570 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
571 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
572 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
579 static char *iso639_to_check = NULL;
580 static char *iso3166_to_check = NULL;
581 static char *script_to_check = NULL;
582 static gboolean setlocale_called = FALSE;
585 enum_locale_proc (LPTSTR locale)
593 lcid = strtoul (locale, &endptr, 16);
594 if (*endptr == '\0' &&
595 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
596 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
598 if (strcmp (iso639, iso639_to_check) == 0 &&
599 ((iso3166_to_check != NULL &&
600 strcmp (iso3166, iso3166_to_check) == 0) ||
601 (iso3166_to_check == NULL &&
602 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
604 char language[100], country[100];
607 if (script_to_check != NULL)
609 /* If lcid is the "other" script for this language,
610 * return TRUE, i.e. continue looking.
612 if (strcmp (script_to_check, "Latn") == 0)
614 switch (LANGIDFROMLCID (lcid))
616 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
618 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
620 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
622 case MAKELANGID (LANG_SERBIAN, 0x07):
623 /* Serbian in Bosnia and Herzegovina, Cyrillic */
627 else if (strcmp (script_to_check, "Cyrl") == 0)
629 switch (LANGIDFROMLCID (lcid))
631 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
633 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
635 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
637 case MAKELANGID (LANG_SERBIAN, 0x06):
638 /* Serbian in Bosnia and Herzegovina, Latin */
644 SetThreadLocale (lcid);
646 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
647 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
649 strcpy (locale, language);
650 strcat (locale, "_");
651 strcat (locale, country);
653 if (setlocale (LC_ALL, locale) != NULL)
654 setlocale_called = TRUE;
667 setlocale_initialization (void)
669 static gboolean initialized = FALSE;
678 /* If some of the POSIXish environment variables are set, set
679 * the Win32 thread locale correspondingly.
681 char *p = getenv ("LC_ALL");
688 if (strcmp (p, "C") == 0)
689 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
692 /* Check if one of the supported locales match the
693 * environment variable. If so, use that locale.
696 iso3166_to_check = strchr (iso639_to_check, '_');
697 if (iso3166_to_check != NULL)
699 *iso3166_to_check++ = '\0';
701 script_to_check = strchr (iso3166_to_check, '@');
702 if (script_to_check != NULL)
703 *script_to_check++ = '\0';
705 /* Handle special cases. */
707 /* The standard code for Serbia and Montenegro was
708 * "CS", but MSFT uses for some reason "SP". By now
709 * (October 2006), SP has split into two, "RS" and
710 * "ME", but don't bother trying to handle those
711 * yet. Do handle the even older "YU", though.
713 if (strcmp (iso3166_to_check, "CS") == 0 ||
714 strcmp (iso3166_to_check, "YU") == 0)
715 iso3166_to_check = "SP";
719 script_to_check = strchr (iso639_to_check, '@');
720 if (script_to_check != NULL)
721 *script_to_check++ = '\0';
722 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
723 if (strcmp (iso639_to_check, "sr") == 0)
724 iso3166_to_check = "SP";
727 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
731 if (!setlocale_called)
732 setlocale (LC_ALL, "");
734 if (!setlocale (LC_ALL, ""))
735 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
740 /* Return TRUE if module_to_check causes version conflicts.
741 * If module_to_check is NULL, check the main module.
744 _gtk_module_has_mixed_deps (GModule *module_to_check)
750 if (!module_to_check)
751 module = g_module_open (NULL, 0);
753 module = module_to_check;
755 if (g_module_symbol (module, "gtk_progress_get_type", &func))
760 if (!module_to_check)
761 g_module_close (module);
767 do_pre_parse_initialization (int *argc,
770 const gchar *env_string;
775 pre_initialized = TRUE;
777 if (_gtk_module_has_mixed_deps (NULL))
778 g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
780 gdk_pre_parse_libgtk_only ();
781 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
783 #ifdef G_ENABLE_DEBUG
784 env_string = g_getenv ("GTK_DEBUG");
785 if (env_string != NULL)
787 debug_flags = g_parse_debug_string (env_string,
789 G_N_ELEMENTS (gtk_debug_keys));
792 #endif /* G_ENABLE_DEBUG */
794 env_string = g_getenv ("GTK_MODULES");
796 gtk_modules_string = g_string_new (env_string);
800 gettext_initialization (void)
802 setlocale_initialization ();
805 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
806 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
807 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
808 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
809 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
814 /* XXX: Remove me after getting rid of gail */
815 extern void _gtk_accessibility_init (void);
818 do_post_parse_initialization (int *argc,
824 gettext_initialization ();
827 signal (SIGPIPE, SIG_IGN);
830 if (g_fatal_warnings)
832 GLogLevelFlags fatal_mask;
834 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
835 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
836 g_log_set_always_fatal (fatal_mask);
839 if (debug_flags & GTK_DEBUG_UPDATES)
840 gdk_window_set_debug_updates (TRUE);
843 /* Translate to default:RTL if you want your widgets
844 * to be RTL, otherwise translate to default:LTR.
845 * Do *not* translate it to "predefinito:LTR", if it
846 * it isn't default:LTR or default:RTL it will not work
848 char *e = _("default:LTR");
849 if (strcmp (e, "default:RTL")==0)
850 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
851 else if (strcmp (e, "default:LTR"))
852 g_warning ("Whoever translated default:LTR did so wrongly.\n");
855 /* do what the call to gtk_type_init() used to do */
858 _gtk_accel_map_init ();
860 /* Set the 'initialized' flag.
862 gtk_initialized = TRUE;
864 /* load gtk modules */
865 if (gtk_modules_string)
867 _gtk_modules_init (argc, argv, gtk_modules_string->str);
868 g_string_free (gtk_modules_string, TRUE);
872 _gtk_modules_init (argc, argv, NULL);
875 _gtk_accessibility_init ();
881 gboolean open_default_display;
885 pre_parse_hook (GOptionContext *context,
890 do_pre_parse_initialization (NULL, NULL);
896 post_parse_hook (GOptionContext *context,
901 OptionGroupInfo *info = data;
904 do_post_parse_initialization (NULL, NULL);
906 if (info->open_default_display)
908 if (gdk_display_open_default_libgtk_only () == NULL)
910 const char *display_name = gdk_get_display_arg_name ();
913 G_OPTION_ERROR_FAILED,
914 _("Cannot open display: %s"),
915 display_name ? display_name : "" );
926 * gtk_get_debug_flags:
928 * Returns the GTK+ debug flags.
930 * This function is intended for GTK+ modules that want
931 * to adjust their debug output based on GTK+ debug flags.
933 * Returns: the GTK+ debug flags.
936 gtk_get_debug_flags (void)
942 * gtk_set_debug_flags:
944 * Sets the GTK+ debug flags.
947 gtk_set_debug_flags (guint flags)
953 * gtk_get_option_group: (skip)
954 * @open_default_display: whether to open the default display
955 * when parsing the commandline arguments
957 * Returns a #GOptionGroup for the commandline arguments recognized
960 * You should add this group to your #GOptionContext
961 * with g_option_context_add_group(), if you are using
962 * g_option_context_parse() to parse your commandline arguments.
964 * Returns: a #GOptionGroup for the commandline arguments recognized
970 gtk_get_option_group (gboolean open_default_display)
973 OptionGroupInfo *info;
975 gettext_initialization ();
977 info = g_new0 (OptionGroupInfo, 1);
978 info->open_default_display = open_default_display;
980 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
981 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
983 gdk_add_option_entries_libgtk_only (group);
984 g_option_group_add_entries (group, gtk_args);
985 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
991 * gtk_init_with_args:
992 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
993 * your main() function (or 0 if @argv is %NULL). This will be changed if
994 * any arguments were handled.
995 * @argv: (array length=argc) (inout) (allow-none): Address of the
996 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
997 * understood by GTK+ are stripped before return.
998 * @parameter_string: a string which is displayed in
999 * the first line of <option>--help</option> output, after
1000 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
1001 * @entries: (array zero-terminated=1): a %NULL-terminated array
1002 * of #GOptionEntrys describing the options of your program
1003 * @translation_domain: a translation domain to use for translating
1004 * the <option>--help</option> output for the options in @entries
1005 * and the @parameter_string with gettext(), or %NULL
1006 * @error: a return location for errors
1008 * This function does the same work as gtk_init_check().
1009 * Additionally, it allows you to add your own commandline options,
1010 * and it automatically generates nicely formatted
1011 * <option>--help</option> output. Note that your program will
1012 * be terminated after writing out the help output.
1014 * Returns: %TRUE if the windowing system has been successfully
1015 * initialized, %FALSE otherwise
1020 gtk_init_with_args (gint *argc,
1022 const gchar *parameter_string,
1023 const GOptionEntry *entries,
1024 const gchar *translation_domain,
1027 GOptionContext *context;
1028 GOptionGroup *gtk_group;
1031 if (gtk_initialized)
1032 return gdk_display_open_default_libgtk_only () != NULL;
1034 gettext_initialization ();
1036 if (!check_setugid ())
1039 gtk_group = gtk_get_option_group (TRUE);
1041 context = g_option_context_new (parameter_string);
1042 g_option_context_add_group (context, gtk_group);
1043 g_option_context_set_translation_domain (context, translation_domain);
1046 g_option_context_add_main_entries (context, entries, translation_domain);
1047 retval = g_option_context_parse (context, argc, argv, error);
1049 g_option_context_free (context);
1057 * @argc: (inout): a pointer to the number of command line arguments
1058 * @argv: (array length=argc) (inout): a pointer to the array of
1059 * command line arguments
1061 * Parses command line arguments, and initializes global
1062 * attributes of GTK+, but does not actually open a connection
1063 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
1065 * Any arguments used by GTK+ or GDK are removed from the array and
1066 * @argc and @argv are updated accordingly.
1068 * There is no need to call this function explicitely if you are using
1069 * gtk_init(), or gtk_init_check().
1071 * Return value: %TRUE if initialization succeeded, otherwise %FALSE
1074 gtk_parse_args (int *argc,
1077 GOptionContext *option_context;
1078 GOptionGroup *gtk_group;
1079 GError *error = NULL;
1081 if (gtk_initialized)
1084 gettext_initialization ();
1086 if (!check_setugid ())
1089 option_context = g_option_context_new (NULL);
1090 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1091 g_option_context_set_help_enabled (option_context, FALSE);
1092 gtk_group = gtk_get_option_group (FALSE);
1093 g_option_context_set_main_group (option_context, gtk_group);
1094 if (!g_option_context_parse (option_context, argc, argv, &error))
1096 g_warning ("%s", error->message);
1097 g_error_free (error);
1100 g_option_context_free (option_context);
1105 #ifdef G_PLATFORM_WIN32
1106 #undef gtk_init_check
1111 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1112 * your main() function (or 0 if @argv is %NULL). This will be changed if
1113 * any arguments were handled.
1114 * @argv: (array length=argc) (inout) (allow-none): Address of the
1115 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
1116 * understood by GTK+ are stripped before return.
1118 * This function does the same work as gtk_init() with only a single
1119 * change: It does not terminate the program if the windowing system
1120 * can't be initialized. Instead it returns %FALSE on failure.
1122 * This way the application can fall back to some other means of
1123 * communication with the user - for example a curses or command line
1126 * Return value: %TRUE if the windowing system has been successfully
1127 * initialized, %FALSE otherwise
1130 gtk_init_check (int *argc,
1133 if (!gtk_parse_args (argc, argv))
1136 return gdk_display_open_default_libgtk_only () != NULL;
1139 #ifdef G_PLATFORM_WIN32
1145 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of
1146 * your main() function (or 0 if @argv is %NULL). This will be changed if
1147 * any arguments were handled.
1148 * @argv: (array length=argc) (inout) (allow-none): Address of the
1149 * <parameter>argv</parameter> parameter of main(), or %NULL. Any options
1150 * understood by GTK+ are stripped before return.
1152 * Call this function before using any other GTK+ functions in your GUI
1153 * applications. It will initialize everything needed to operate the
1154 * toolkit and parses some standard command line options.
1156 * Although you are expected to pass the @argc, @argv parameters from main() to
1157 * this function, it is possible to pass %NULL if @argv is not available or
1158 * commandline handling is not required.
1160 * @argc and @argv are adjusted accordingly so your own code will
1161 * never see those standard arguments.
1163 * Note that there are some alternative ways to initialize GTK+:
1164 * if you are calling gtk_parse_args(), gtk_init_check(),
1165 * gtk_init_with_args() or g_option_context_parse() with
1166 * the option group returned by gtk_get_option_group(),
1167 * you <emphasis>don't</emphasis> have to call gtk_init().
1170 * This function will terminate your program if it was unable to
1171 * initialize the windowing system for some reason. If you want
1172 * your program to fall back to a textual interface you want to
1173 * call gtk_init_check() instead.
1177 * Since 2.18, GTK+ calls <literal>signal (SIGPIPE, SIG_IGN)</literal>
1178 * during initialization, to ignore SIGPIPE signals, since these are
1179 * almost never wanted in graphical applications. If you do need to
1180 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1181 * but notice that other libraries (e.g. libdbus or gvfs) might do
1186 gtk_init (int *argc, char ***argv)
1188 if (!gtk_init_check (argc, argv))
1190 const char *display_name_arg = gdk_get_display_arg_name ();
1191 if (display_name_arg == NULL)
1192 display_name_arg = getenv("DISPLAY");
1193 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1200 /* This is relevant when building with gcc for Windows (MinGW),
1201 * where we want to be struct packing compatible with MSVC,
1202 * i.e. use the -mms-bitfields switch.
1203 * For Cygwin there should be no need to be compatible with MSVC,
1204 * so no need to use G_PLATFORM_WIN32.
1208 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1210 if (sizeof_GtkWindow != sizeof (GtkWindow))
1211 g_error ("Incompatible build!\n"
1212 "The code using GTK+ thinks GtkWindow is of different\n"
1213 "size than it actually is in this build of GTK+.\n"
1214 "On Windows, this probably means that you have compiled\n"
1215 "your code with gcc without the -mms-bitfields switch,\n"
1216 "or that you are using an unsupported compiler.");
1219 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1220 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1221 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
1222 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1223 * -fnative-struct still matters. GtkBox is one such.
1226 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1228 if (sizeof_GtkBox != sizeof (GtkBox))
1229 g_error ("Incompatible build!\n"
1230 "The code using GTK+ thinks GtkBox is of different\n"
1231 "size than it actually is in this build of GTK+.\n"
1232 "On Windows, this probably means that you have compiled\n"
1233 "your code with gcc without the -mms-bitfields switch,\n"
1234 "or that you are using an unsupported compiler.");
1237 /* These two functions might get more checks added later, thus pass
1238 * in the number of extra args.
1241 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1243 check_sizeof_GtkWindow (sizeof_GtkWindow);
1244 if (num_checks >= 2)
1245 check_sizeof_GtkBox (sizeof_GtkBox);
1246 gtk_init (argc, argv);
1250 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1252 check_sizeof_GtkWindow (sizeof_GtkWindow);
1253 if (num_checks >= 2)
1254 check_sizeof_GtkBox (sizeof_GtkBox);
1255 return gtk_init_check (argc, argv);
1261 * _gtk_get_lc_ctype:
1263 * Return the Unix-style locale string for the language currently in
1264 * effect. On Unix systems, this is the return value from
1265 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1266 * affect this through the environment variables LC_ALL, LC_CTYPE or
1267 * LANG (checked in that order). The locale strings typically is in
1268 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1269 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1270 * Swedish as written in Finland or pt_BR for Portuguese as written in
1273 * On Windows, the C library doesn't use any such environment
1274 * variables, and setting them won't affect the behaviour of functions
1275 * like ctime(). The user sets the locale through the Regional Options
1276 * in the Control Panel. The C library (in the setlocale() function)
1277 * does not use country and language codes, but country and language
1278 * names spelled out in English.
1279 * However, this function does check the above environment
1280 * variables, and does return a Unix-style locale string based on
1281 * either said environment variables or the thread's current locale.
1283 * Return value: a dynamically allocated string, free with g_free().
1287 _gtk_get_lc_ctype (void)
1290 /* Somebody might try to set the locale for this process using the
1291 * LANG or LC_ environment variables. The Microsoft C library
1292 * doesn't know anything about them. You set the locale in the
1293 * Control Panel. Setting these env vars won't have any affect on
1294 * locale-dependent C library functions like ctime(). But just for
1295 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1296 * it easier to test GTK and Pango in various default languages, you
1297 * don't have to clickety-click in the Control Panel, you can simply
1298 * start the program with LC_ALL=something on the command line.)
1302 p = getenv ("LC_ALL");
1304 return g_strdup (p);
1306 p = getenv ("LC_CTYPE");
1308 return g_strdup (p);
1310 p = getenv ("LANG");
1312 return g_strdup (p);
1314 return g_win32_getlocale ();
1316 return g_strdup (setlocale (LC_CTYPE, NULL));
1321 * gtk_get_default_language:
1323 * Returns the #PangoLanguage for the default language currently in
1324 * effect. (Note that this can change over the life of an
1325 * application.) The default language is derived from the current
1326 * locale. It determines, for example, whether GTK+ uses the
1327 * right-to-left or left-to-right text direction.
1329 * This function is equivalent to pango_language_get_default().
1330 * See that function for details.
1332 * Return value: the default language as a #PangoLanguage,
1336 gtk_get_default_language (void)
1338 return pango_language_get_default ();
1344 * Runs the main loop until gtk_main_quit() is called.
1346 * You can nest calls to gtk_main(). In that case gtk_main_quit()
1347 * will make the innermost invocation of the main loop return.
1354 gtk_main_loop_level++;
1356 loop = g_main_loop_new (NULL, TRUE);
1357 main_loops = g_slist_prepend (main_loops, loop);
1359 if (g_main_loop_is_running (main_loops->data))
1361 GDK_THREADS_LEAVE ();
1362 g_main_loop_run (loop);
1363 GDK_THREADS_ENTER ();
1367 main_loops = g_slist_remove (main_loops, loop);
1369 g_main_loop_unref (loop);
1371 gtk_main_loop_level--;
1373 if (gtk_main_loop_level == 0)
1375 /* Try storing all clipboard data we have */
1376 _gtk_clipboard_store_all ();
1378 /* Synchronize the recent manager singleton */
1379 _gtk_recent_manager_sync ();
1386 * Asks for the current nesting level of the main loop.
1388 * Returns: the nesting level of the current invocation
1392 gtk_main_level (void)
1394 return gtk_main_loop_level;
1400 * Makes the innermost invocation of the main loop return
1401 * when it regains control.
1404 gtk_main_quit (void)
1406 g_return_if_fail (main_loops != NULL);
1408 g_main_loop_quit (main_loops->data);
1412 * gtk_events_pending:
1414 * Checks if any events are pending.
1416 * This can be used to update the UI and invoke timeouts etc.
1417 * while doing some time intensive computation.
1420 * <title>Updating the UI during a long computation</title>
1422 * /* computation going on... */
1424 * while (gtk_events_pending ())
1425 * gtk_main_iteration ();
1427 * /* ...computation continued */
1431 * Returns: %TRUE if any events are pending, %FALSE otherwise
1434 gtk_events_pending (void)
1438 GDK_THREADS_LEAVE ();
1439 result = g_main_context_pending (NULL);
1440 GDK_THREADS_ENTER ();
1446 * gtk_main_iteration:
1448 * Runs a single iteration of the mainloop.
1450 * If no events are waiting to be processed GTK+ will block
1451 * until the next event is noticed. If you don't want to block
1452 * look at gtk_main_iteration_do() or check if any events are
1453 * pending with gtk_events_pending() first.
1455 * Returns: %TRUE if gtk_main_quit() has been called for the
1456 * innermost mainloop
1459 gtk_main_iteration (void)
1461 GDK_THREADS_LEAVE ();
1462 g_main_context_iteration (NULL, TRUE);
1463 GDK_THREADS_ENTER ();
1466 return !g_main_loop_is_running (main_loops->data);
1472 * gtk_main_iteration_do:
1473 * @blocking: %TRUE if you want GTK+ to block if no events are pending
1475 * Runs a single iteration of the mainloop.
1476 * If no events are available either return or block depending on
1477 * the value of @blocking.
1479 * Returns: %TRUE if gtk_main_quit() has been called for the
1480 * innermost mainloop
1483 gtk_main_iteration_do (gboolean blocking)
1485 GDK_THREADS_LEAVE ();
1486 g_main_context_iteration (NULL, blocking);
1487 GDK_THREADS_ENTER ();
1490 return !g_main_loop_is_running (main_loops->data);
1495 /* private libgtk to libgdk interfaces */
1496 gboolean gdk_device_grab_info_libgtk_only (GdkDisplay *display,
1498 GdkWindow **grab_window,
1499 gboolean *owner_events);
1502 rewrite_events_translate (GdkWindow *old_window,
1503 GdkWindow *new_window,
1507 gint old_origin_x, old_origin_y;
1508 gint new_origin_x, new_origin_y;
1510 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1511 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1513 *x += old_origin_x - new_origin_x;
1514 *y += old_origin_y - new_origin_y;
1518 rewrite_event_for_window (GdkEvent *event,
1519 GdkWindow *new_window)
1521 event = gdk_event_copy (event);
1523 switch (event->type)
1526 rewrite_events_translate (event->any.window,
1528 &event->scroll.x, &event->scroll.y);
1530 case GDK_BUTTON_PRESS:
1531 case GDK_2BUTTON_PRESS:
1532 case GDK_3BUTTON_PRESS:
1533 case GDK_BUTTON_RELEASE:
1534 rewrite_events_translate (event->any.window,
1536 &event->button.x, &event->button.y);
1538 case GDK_MOTION_NOTIFY:
1539 rewrite_events_translate (event->any.window,
1541 &event->motion.x, &event->motion.y);
1544 case GDK_KEY_RELEASE:
1545 case GDK_PROXIMITY_IN:
1546 case GDK_PROXIMITY_OUT:
1553 g_object_unref (event->any.window);
1554 event->any.window = g_object_ref (new_window);
1559 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1560 * then what X11 does is deliver the event normally if it was going to this
1561 * client, otherwise, delivers it in terms of the grab window. This function
1562 * rewrites events to the effect that events going to the same window group
1563 * are delivered normally, otherwise, the event is delivered in terms of the
1567 rewrite_event_for_grabs (GdkEvent *event)
1569 GdkWindow *grab_window;
1570 GtkWidget *event_widget, *grab_widget;
1571 gpointer grab_widget_ptr;
1572 gboolean owner_events;
1573 GdkDisplay *display;
1576 switch (event->type)
1579 case GDK_BUTTON_PRESS:
1580 case GDK_2BUTTON_PRESS:
1581 case GDK_3BUTTON_PRESS:
1582 case GDK_BUTTON_RELEASE:
1583 case GDK_MOTION_NOTIFY:
1584 case GDK_PROXIMITY_IN:
1585 case GDK_PROXIMITY_OUT:
1587 case GDK_KEY_RELEASE:
1588 display = gdk_window_get_display (event->any.window);
1589 device = gdk_event_get_device (event);
1591 if (!gdk_device_grab_info_libgtk_only (display, device, &grab_window, &owner_events) ||
1599 event_widget = gtk_get_event_widget (event);
1600 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1601 grab_widget = grab_widget_ptr;
1604 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1605 return rewrite_event_for_window (event, grab_window);
1611 * gtk_main_do_event:
1612 * @event: An event to process (normally passed by GDK)
1614 * Processes a single GDK event.
1616 * This is public only to allow filtering of events between GDK and GTK+.
1617 * You will not usually need to call this function directly.
1619 * While you should not call this function directly, you might want to
1620 * know how exactly events are handled. So here is what this function
1621 * does with the event:
1625 * Compress enter/leave notify events. If the event passed build an
1626 * enter/leave pair together with the next event (peeked from GDK), both
1627 * events are thrown away. This is to avoid a backlog of (de-)highlighting
1628 * widgets crossed by the pointer.
1629 * </para></listitem>
1631 * Find the widget which got the event. If the widget can't be determined
1632 * the event is thrown away unless it belongs to a INCR transaction. In that
1633 * case it is passed to gtk_selection_incr_event().
1634 * </para></listitem>
1636 * Then the event is pushed onto a stack so you can query the currently
1637 * handled event with gtk_get_current_event().
1638 * </para></listitem>
1640 * The event is sent to a widget. If a grab is active all events for widgets
1641 * that are not in the contained in the grab widget are sent to the latter
1642 * with a few exceptions:
1645 * Deletion and destruction events are still sent to the event widget for
1647 * </para></listitem>
1649 * Events which directly relate to the visual representation of the event
1651 * </para></listitem>
1653 * Leave events are delivered to the event widget if there was an enter
1654 * event delivered to it before without the paired leave event.
1655 * </para></listitem>
1657 * Drag events are not redirected because it is unclear what the semantics
1659 * </para></listitem>
1661 * Another point of interest might be that all key events are first passed
1662 * through the key snooper functions if there are any. Read the description
1663 * of gtk_key_snooper_install() if you need this feature.
1664 * </para></listitem>
1666 * After finishing the delivery the event is popped from the event stack.
1667 * </para></listitem>
1671 gtk_main_do_event (GdkEvent *event)
1673 GtkWidget *event_widget;
1674 GtkWidget *grab_widget = NULL;
1675 GtkWindowGroup *window_group;
1676 GdkEvent *rewritten_event = NULL;
1680 if (event->type == GDK_SETTING)
1682 _gtk_settings_handle_event (&event->setting);
1686 if (event->type == GDK_OWNER_CHANGE)
1688 _gtk_clipboard_handle_event (&event->owner_change);
1692 /* Find the widget which got the event. We store the widget
1693 * in the user_data field of GdkWindow's. Ignore the event
1694 * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1695 * events which are handled specially. Though this happens rarely,
1696 * bogus events can occur for e.g. destroyed GdkWindows.
1698 event_widget = gtk_get_event_widget (event);
1701 /* To handle selection INCR transactions, we select
1702 * PropertyNotify events on the requestor window and create
1703 * a corresponding (fake) GdkWindow so that events get here.
1704 * There won't be a widget though, so we have to handle
1707 if (event->type == GDK_PROPERTY_NOTIFY)
1708 _gtk_selection_incr_event (event->any.window,
1714 /* If pointer or keyboard grabs are in effect, munge the events
1715 * so that each window group looks like a separate app.
1717 rewritten_event = rewrite_event_for_grabs (event);
1718 if (rewritten_event)
1720 event = rewritten_event;
1721 event_widget = gtk_get_event_widget (event);
1724 window_group = gtk_main_get_window_group (event_widget);
1725 device = gdk_event_get_device (event);
1727 /* check whether there is a (device) grab in effect... */
1729 grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1732 grab_widget = gtk_window_group_get_current_grab (window_group);
1734 /* If the grab widget is an ancestor of the event widget
1735 * then we send the event to the original event widget.
1736 * This is the key to implementing modality.
1739 (gtk_widget_is_sensitive (event_widget) &&
1740 gtk_widget_is_ancestor (event_widget, grab_widget)))
1741 grab_widget = event_widget;
1743 /* If the widget receiving events is actually blocked by another
1747 _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1749 if (rewritten_event)
1750 gdk_event_free (rewritten_event);
1755 /* Push the event onto a stack of current events for
1756 * gtk_current_event_get().
1758 current_events = g_list_prepend (current_events, event);
1760 /* Not all events get sent to the grabbing widget.
1761 * The delete, destroy, expose, focus change and resize
1762 * events still get sent to the event widget because
1763 * 1) these events have no meaning for the grabbing widget
1764 * and 2) redirecting these events to the grabbing widget
1765 * could cause the display to be messed up.
1767 * Drag events are also not redirected, since it isn't
1768 * clear what the semantics of that would be.
1770 switch (event->type)
1776 g_object_ref (event_widget);
1777 if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1778 !gtk_widget_event (event_widget, event))
1779 gtk_widget_destroy (event_widget);
1780 g_object_unref (event_widget);
1784 /* Unexpected GDK_DESTROY from the outside, ignore for
1785 * child windows, handle like a GDK_DELETE for toplevels
1787 if (!gtk_widget_get_parent (event_widget))
1789 g_object_ref (event_widget);
1790 if (!gtk_widget_event (event_widget, event) &&
1791 gtk_widget_get_realized (event_widget))
1792 gtk_widget_destroy (event_widget);
1793 g_object_unref (event_widget);
1798 if (event->any.window && gtk_widget_get_double_buffered (event_widget))
1800 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1801 gtk_widget_send_expose (event_widget, event);
1802 gdk_window_end_paint (event->any.window);
1806 /* The app may paint with a previously allocated cairo_t,
1807 * which will draw directly to the window. We can't catch cairo
1808 * draw operations to automatically flush the window, thus we
1809 * need to explicitly flush any outstanding moves or double
1812 gdk_window_flush (event->any.window);
1813 gtk_widget_send_expose (event_widget, event);
1817 case GDK_PROPERTY_NOTIFY:
1818 case GDK_FOCUS_CHANGE:
1822 case GDK_SELECTION_CLEAR:
1823 case GDK_SELECTION_REQUEST:
1824 case GDK_SELECTION_NOTIFY:
1825 case GDK_CLIENT_EVENT:
1826 case GDK_VISIBILITY_NOTIFY:
1827 case GDK_WINDOW_STATE:
1828 case GDK_GRAB_BROKEN:
1830 gtk_widget_event (event_widget, event);
1834 case GDK_BUTTON_PRESS:
1835 case GDK_2BUTTON_PRESS:
1836 case GDK_3BUTTON_PRESS:
1837 gtk_propagate_event (grab_widget, event);
1841 case GDK_KEY_RELEASE:
1844 if (gtk_invoke_key_snoopers (grab_widget, event))
1848 /* make focus visible in a window that receives a key event */
1851 GtkPolicyType visible_focus;
1853 window = gtk_widget_get_toplevel (grab_widget);
1854 g_object_get (gtk_widget_get_settings (grab_widget), "gtk-visible-focus", &visible_focus, NULL);
1855 if (GTK_IS_WINDOW (window) && visible_focus != GTK_POLICY_NEVER)
1856 gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
1859 /* Catch alt press to enable auto-mnemonics;
1860 * menus are handled elsewhere
1861 * FIXME: this does not work with mnemonic modifiers other than Alt
1863 if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1864 ((event->key.state & (gtk_accelerator_get_default_mod_mask ()) & ~(GDK_RELEASE_MASK|GDK_MOD1_MASK)) == 0) &&
1865 !GTK_IS_MENU_SHELL (grab_widget))
1867 gboolean auto_mnemonics;
1869 g_object_get (gtk_widget_get_settings (grab_widget),
1870 "gtk-auto-mnemonics", &auto_mnemonics, NULL);
1874 gboolean mnemonics_visible;
1877 mnemonics_visible = (event->type == GDK_KEY_PRESS);
1879 window = gtk_widget_get_toplevel (grab_widget);
1880 if (GTK_IS_WINDOW (window))
1881 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), mnemonics_visible);
1884 /* else fall through */
1885 case GDK_MOTION_NOTIFY:
1886 case GDK_BUTTON_RELEASE:
1887 case GDK_PROXIMITY_IN:
1888 case GDK_PROXIMITY_OUT:
1889 gtk_propagate_event (grab_widget, event);
1892 case GDK_ENTER_NOTIFY:
1893 _gtk_widget_set_device_window (event_widget,
1894 gdk_event_get_device (event),
1896 if (gtk_widget_is_sensitive (grab_widget))
1897 gtk_widget_event (grab_widget, event);
1900 case GDK_LEAVE_NOTIFY:
1901 _gtk_widget_set_device_window (event_widget,
1902 gdk_event_get_device (event),
1904 if (gtk_widget_is_sensitive (grab_widget))
1905 gtk_widget_event (grab_widget, event);
1908 case GDK_DRAG_STATUS:
1909 case GDK_DROP_FINISHED:
1910 _gtk_drag_source_handle_event (event_widget, event);
1912 case GDK_DRAG_ENTER:
1913 case GDK_DRAG_LEAVE:
1914 case GDK_DRAG_MOTION:
1915 case GDK_DROP_START:
1916 _gtk_drag_dest_handle_event (event_widget, event);
1919 g_assert_not_reached ();
1923 if (event->type == GDK_ENTER_NOTIFY
1924 || event->type == GDK_LEAVE_NOTIFY
1925 || event->type == GDK_BUTTON_PRESS
1926 || event->type == GDK_2BUTTON_PRESS
1927 || event->type == GDK_3BUTTON_PRESS
1928 || event->type == GDK_KEY_PRESS
1929 || event->type == GDK_DRAG_ENTER
1930 || event->type == GDK_GRAB_BROKEN
1931 || event->type == GDK_MOTION_NOTIFY
1932 || event->type == GDK_SCROLL)
1934 _gtk_tooltip_handle_event (event);
1937 tmp_list = current_events;
1938 current_events = g_list_remove_link (current_events, tmp_list);
1939 g_list_free_1 (tmp_list);
1941 if (rewritten_event)
1942 gdk_event_free (rewritten_event);
1948 * All this function does it to return %TRUE.
1950 * This can be useful for example if you want to inhibit the deletion
1951 * of a window. Of course you should not do this as the user expects
1952 * a reaction from clicking the close icon of the window...
1955 * <title>A persistent window</title>
1957 * #include <gtk/gtk.h><
1960 * main (int argc, char **argv)
1962 * GtkWidget *win, *but;
1964 * gtk_init (&argc, &argv);
1966 * win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1967 * g_signal_connect (win, "delete-event",
1968 * G_CALLBACK (gtk_true), NULL);
1969 * g_signal_connect (win, "destroy",
1970 * G_CALLBACK (gtk_main_quit), NULL);
1972 * but = gtk_button_new_with_label ("Close yourself. I mean it!");
1973 * g_signal_connect_swapped (but, "clicked",
1974 * G_CALLBACK (gtk_object_destroy), win);
1975 * gtk_container_add (GTK_CONTAINER (win), but);
1977 * gtk_widget_show_all (win);
1997 * Analogical to gtk_true(), this function does nothing
1998 * but always returns %FALSE.
2008 static GtkWindowGroup *
2009 gtk_main_get_window_group (GtkWidget *widget)
2011 GtkWidget *toplevel = NULL;
2014 toplevel = gtk_widget_get_toplevel (widget);
2016 if (GTK_IS_WINDOW (toplevel))
2017 return gtk_window_get_group (GTK_WINDOW (toplevel));
2019 return gtk_window_get_group (NULL);
2024 GtkWidget *old_grab_widget;
2025 GtkWidget *new_grab_widget;
2026 gboolean was_grabbed;
2027 gboolean is_grabbed;
2029 GList *notified_windows;
2034 synth_crossing_for_grab_notify (GtkWidget *from,
2036 GrabNotifyInfo *info,
2038 GdkCrossingMode mode)
2042 GdkDevice *device = devices->data;
2043 GdkWindow *from_window, *to_window;
2045 /* Do not propagate events more than once to
2046 * the same windows if non-multidevice aware.
2052 from_window = _gtk_widget_get_device_window (from, device);
2055 !gdk_window_get_support_multidevice (from_window) &&
2056 g_list_find (info->notified_windows, from_window))
2064 to_window = _gtk_widget_get_device_window (to, device);
2067 !gdk_window_get_support_multidevice (to_window) &&
2068 g_list_find (info->notified_windows, to_window))
2072 if (from_window || to_window)
2074 _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
2075 (to_window) ? to : NULL,
2079 info->notified_windows = g_list_prepend (info->notified_windows, from_window);
2082 info->notified_windows = g_list_prepend (info->notified_windows, to_window);
2085 devices = devices->next;
2090 gtk_grab_notify_foreach (GtkWidget *child,
2093 GrabNotifyInfo *info = data;
2094 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
2097 was_grabbed = info->was_grabbed;
2098 is_grabbed = info->is_grabbed;
2100 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
2101 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
2103 was_shadowed = info->old_grab_widget && !info->was_grabbed;
2104 is_shadowed = info->new_grab_widget && !info->is_grabbed;
2106 g_object_ref (child);
2108 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
2109 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
2112 _gtk_widget_get_device_window (child, info->device))
2114 /* Device specified and is on widget */
2115 devices = g_list_prepend (NULL, info->device);
2118 devices = _gtk_widget_list_devices (child);
2122 _gtk_widget_set_shadowed (child, TRUE);
2123 if (!was_shadowed && devices &&
2124 gtk_widget_is_sensitive (child))
2125 synth_crossing_for_grab_notify (child, info->new_grab_widget,
2127 GDK_CROSSING_GTK_GRAB);
2131 _gtk_widget_set_shadowed (child, FALSE);
2132 if (was_shadowed && devices &&
2133 gtk_widget_is_sensitive (child))
2134 synth_crossing_for_grab_notify (info->old_grab_widget, child,
2136 info->from_grab ? GDK_CROSSING_GTK_GRAB :
2137 GDK_CROSSING_GTK_UNGRAB);
2140 if (was_shadowed != is_shadowed)
2141 _gtk_widget_grab_notify (child, was_shadowed);
2143 g_object_unref (child);
2144 g_list_free (devices);
2146 info->was_grabbed = was_grabbed;
2147 info->is_grabbed = is_grabbed;
2151 gtk_grab_notify (GtkWindowGroup *group,
2153 GtkWidget *old_grab_widget,
2154 GtkWidget *new_grab_widget,
2158 GrabNotifyInfo info = { 0 };
2160 if (old_grab_widget == new_grab_widget)
2163 info.old_grab_widget = old_grab_widget;
2164 info.new_grab_widget = new_grab_widget;
2165 info.from_grab = from_grab;
2166 info.device = device;
2168 g_object_ref (group);
2170 toplevels = gtk_window_list_toplevels ();
2171 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
2175 GtkWindow *toplevel = toplevels->data;
2176 toplevels = g_list_delete_link (toplevels, toplevels);
2178 info.was_grabbed = FALSE;
2179 info.is_grabbed = FALSE;
2181 if (group == gtk_window_get_group (toplevel))
2182 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
2183 g_object_unref (toplevel);
2186 g_list_free (info.notified_windows);
2187 g_object_unref (group);
2191 * gtk_grab_add: (method)
2192 * @widget: The widget that grabs keyboard and pointer events
2194 * Makes @widget the current grabbed widget.
2196 * This means that interaction with other widgets in the same
2197 * application is blocked and mouse as well as keyboard events
2198 * are delivered to this widget.
2200 * If @widget is not sensitive, it is not set as the current
2201 * grabbed widget and this function does nothing.
2204 gtk_grab_add (GtkWidget *widget)
2206 GtkWindowGroup *group;
2207 GtkWidget *old_grab_widget;
2209 g_return_if_fail (widget != NULL);
2211 if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2213 _gtk_widget_set_has_grab (widget, TRUE);
2215 group = gtk_main_get_window_group (widget);
2217 old_grab_widget = gtk_window_group_get_current_grab (group);
2219 g_object_ref (widget);
2220 _gtk_window_group_add_grab (group, widget);
2222 gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2227 * gtk_grab_get_current:
2229 * Queries the current grab of the default window group.
2231 * Return value: (transfer none): The widget which currently
2232 * has the grab or %NULL if no grab is active
2235 gtk_grab_get_current (void)
2237 GtkWindowGroup *group;
2239 group = gtk_main_get_window_group (NULL);
2241 return gtk_window_group_get_current_grab (group);
2245 * gtk_grab_remove: (method)
2246 * @widget: The widget which gives up the grab
2248 * Removes the grab from the given widget.
2250 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2252 * If @widget does not have the grab, this function does nothing.
2255 gtk_grab_remove (GtkWidget *widget)
2257 GtkWindowGroup *group;
2258 GtkWidget *new_grab_widget;
2260 g_return_if_fail (widget != NULL);
2262 if (gtk_widget_has_grab (widget))
2264 _gtk_widget_set_has_grab (widget, FALSE);
2266 group = gtk_main_get_window_group (widget);
2267 _gtk_window_group_remove_grab (group, widget);
2268 new_grab_widget = gtk_window_group_get_current_grab (group);
2270 gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2272 g_object_unref (widget);
2277 * gtk_device_grab_add:
2278 * @widget: a #GtkWidget
2279 * @device: a #GtkDevice to grab on.
2280 * @block_others: %TRUE to prevent other devices to interact with @widget.
2282 * Adds a GTK+ grab on @device, so all the events on @device and its
2283 * associated pointer or keyboard (if any) are delivered to @widget.
2284 * If the @block_others parameter is %TRUE, any other devices will be
2285 * unable to interact with @widget during the grab.
2290 gtk_device_grab_add (GtkWidget *widget,
2292 gboolean block_others)
2294 GtkWindowGroup *group;
2295 GtkWidget *old_grab_widget;
2297 g_return_if_fail (GTK_IS_WIDGET (widget));
2298 g_return_if_fail (GDK_IS_DEVICE (device));
2300 group = gtk_main_get_window_group (widget);
2301 old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2303 if (old_grab_widget != widget)
2304 _gtk_window_group_add_device_grab (group, widget, device, block_others);
2306 gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2310 * gtk_device_grab_remove:
2311 * @widget: a #GtkWidget
2312 * @device: a #GdkDevice
2314 * Removes a device grab from the given widget.
2316 * You have to pair calls to gtk_device_grab_add() and
2317 * gtk_device_grab_remove().
2322 gtk_device_grab_remove (GtkWidget *widget,
2325 GtkWindowGroup *group;
2326 GtkWidget *new_grab_widget;
2328 g_return_if_fail (GTK_IS_WIDGET (widget));
2329 g_return_if_fail (GDK_IS_DEVICE (device));
2331 group = gtk_main_get_window_group (widget);
2332 _gtk_window_group_remove_device_grab (group, widget, device);
2333 new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2335 gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2339 * gtk_key_snooper_install: (skip)
2340 * @snooper: a #GtkKeySnoopFunc
2341 * @func_data: data to pass to @snooper
2343 * Installs a key snooper function, which will get called on all
2344 * key events before delivering them normally.
2346 * Returns: a unique id for this key snooper for use with
2347 * gtk_key_snooper_remove().
2350 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2353 GtkKeySnooperData *data;
2354 static guint snooper_id = 1;
2356 g_return_val_if_fail (snooper != NULL, 0);
2358 data = g_new (GtkKeySnooperData, 1);
2359 data->func = snooper;
2360 data->func_data = func_data;
2361 data->id = snooper_id++;
2362 key_snoopers = g_slist_prepend (key_snoopers, data);
2368 * gtk_key_snooper_remove:
2369 * @snooper_handler_id: Identifies the key snooper to remove
2371 * Removes the key snooper function with the given id.
2374 gtk_key_snooper_remove (guint snooper_id)
2376 GtkKeySnooperData *data = NULL;
2379 slist = key_snoopers;
2383 if (data->id == snooper_id)
2386 slist = slist->next;
2391 key_snoopers = g_slist_remove (key_snoopers, data);
2397 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2401 gint return_val = FALSE;
2403 slist = key_snoopers;
2404 while (slist && !return_val)
2406 GtkKeySnooperData *data;
2409 slist = slist->next;
2410 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2417 * gtk_get_current_event:
2419 * Obtains a copy of the event currently being processed by GTK+.
2421 * For example, if you are handling a #GtkButton::clicked signal,
2422 * the current event will be the #GdkEventButton that triggered
2423 * the ::clicked signal.
2425 * Return value: (transfer full): a copy of the current event, or
2426 * %NULL if there is no current event. The returned event must be
2427 * freed with gdk_event_free().
2430 gtk_get_current_event (void)
2433 return gdk_event_copy (current_events->data);
2439 * gtk_get_current_event_time:
2441 * If there is a current event and it has a timestamp,
2442 * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2444 * Return value: the timestamp from the current event,
2445 * or %GDK_CURRENT_TIME.
2448 gtk_get_current_event_time (void)
2451 return gdk_event_get_time (current_events->data);
2453 return GDK_CURRENT_TIME;
2457 * gtk_get_current_event_state:
2458 * @state: (out): a location to store the state of the current event
2460 * If there is a current event and it has a state field, place
2461 * that state field in @state and return %TRUE, otherwise return
2464 * Return value: %TRUE if there was a current event and it
2468 gtk_get_current_event_state (GdkModifierType *state)
2470 g_return_val_if_fail (state != NULL, FALSE);
2473 return gdk_event_get_state (current_events->data, state);
2482 * gtk_get_current_event_device:
2484 * If there is a current event and it has a device, return that
2485 * device, otherwise return %NULL.
2487 * Returns: (transfer none): a #GdkDevice, or %NULL
2490 gtk_get_current_event_device (void)
2493 return gdk_event_get_device (current_events->data);
2499 * gtk_get_event_widget:
2500 * @event: a #GdkEvent
2502 * If @event is %NULL or the event was not associated with any widget,
2503 * returns %NULL, otherwise returns the widget that received the event
2506 * Return value: (transfer none): the widget that originally
2507 * received @event, or %NULL
2510 gtk_get_event_widget (GdkEvent *event)
2513 gpointer widget_ptr;
2516 if (event && event->any.window &&
2517 (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2519 gdk_window_get_user_data (event->any.window, &widget_ptr);
2520 widget = widget_ptr;
2527 * gtk_propagate_event:
2528 * @widget: a #GtkWidget
2531 * Sends an event to a widget, propagating the event to parent widgets
2532 * if the event remains unhandled.
2534 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2535 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2536 * the event may be propagated; if so, this function is used.
2538 * gtk_propagate_event() calls gtk_widget_event() on each widget it
2539 * decides to send the event to. So gtk_widget_event() is the lowest-level
2540 * function; it simply emits the #GtkWidget::event and possibly an
2541 * event-specific signal on a widget. gtk_propagate_event() is a bit
2542 * higher-level, and gtk_main_do_event() is the highest level.
2544 * All that said, you most likely don't want to use any of these
2545 * functions; synthesizing events is rarely needed. There are almost
2546 * certainly better ways to achieve your goals. For example, use
2547 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2548 * of making up expose events.
2551 gtk_propagate_event (GtkWidget *widget,
2556 g_return_if_fail (GTK_IS_WIDGET (widget));
2557 g_return_if_fail (event != NULL);
2559 handled_event = FALSE;
2561 g_object_ref (widget);
2563 if ((event->type == GDK_KEY_PRESS) ||
2564 (event->type == GDK_KEY_RELEASE))
2566 /* Only send key events within Window widgets to the Window
2567 * The Window widget will in turn pass the
2568 * key event on to the currently focused widget
2573 window = gtk_widget_get_toplevel (widget);
2574 if (GTK_IS_WINDOW (window))
2576 /* If there is a grab within the window, give the grab widget
2577 * a first crack at the key event
2579 if (widget != window && gtk_widget_has_grab (widget))
2580 handled_event = gtk_widget_event (widget, event);
2584 window = gtk_widget_get_toplevel (widget);
2585 if (GTK_IS_WINDOW (window))
2587 if (gtk_widget_is_sensitive (window))
2588 gtk_widget_event (window, event);
2592 handled_event = TRUE; /* don't send to widget */
2596 /* Other events get propagated up the widget tree
2597 * so that parents can see the button and motion
2598 * events of the children.
2606 /* Scroll events are special cased here because it
2607 * feels wrong when scrolling a GtkViewport, say,
2608 * to have children of the viewport eat the scroll
2611 if (!gtk_widget_is_sensitive (widget))
2612 handled_event = event->type != GDK_SCROLL;
2614 handled_event = gtk_widget_event (widget, event);
2616 tmp = gtk_widget_get_parent (widget);
2617 g_object_unref (widget);
2621 if (!handled_event && widget)
2622 g_object_ref (widget);
2628 g_object_unref (widget);
2632 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2633 GValue *return_accu,
2634 const GValue *handler_return,
2637 gboolean continue_emission;
2638 gboolean signal_handled;
2640 signal_handled = g_value_get_boolean (handler_return);
2641 g_value_set_boolean (return_accu, signal_handled);
2642 continue_emission = !signal_handled;
2644 return continue_emission;
2648 _gtk_single_string_accumulator (GSignalInvocationHint *ihint,
2649 GValue *return_accu,
2650 const GValue *handler_return,
2653 gboolean continue_emission;
2656 str = g_value_get_string (handler_return);
2657 g_value_set_string (return_accu, str);
2658 continue_emission = str == NULL;
2660 return continue_emission;