1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include "gdkconfig.h"
40 #include <sys/types.h> /* For uid_t, gid_t */
50 #include "gtkaccelmap.h"
52 #include "gtkclipboard.h"
54 #include "gtkversion.h"
56 #include "gtkmodules.h"
58 #include "gtkrecentmanager.h"
59 #include "gtkselection.h"
60 #include "gtksettings.h"
61 #include "gtkwidget.h"
62 #include "gtkwindow.h"
63 #include "gtktooltip.h"
67 #include "gdk/gdkprivate.h" /* for GDK_WINDOW_DESTROYED */
71 static HMODULE gtk_dll;
74 DllMain (HINSTANCE hinstDLL,
80 case DLL_PROCESS_ATTACH:
81 gtk_dll = (HMODULE) hinstDLL;
88 /* These here before inclusion of gtkprivate.h so that the original
89 * GTK_LIBDIR and GTK_LOCALEDIR definitions are seen. Yeah, this is a
93 _gtk_get_libdir (void)
95 static char *gtk_libdir = NULL;
96 if (gtk_libdir == NULL)
98 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
99 gchar *slash = strrchr (root, '\\');
100 if (g_ascii_strcasecmp (slash + 1, ".libs") == 0)
101 gtk_libdir = GTK_LIBDIR;
103 gtk_libdir = g_build_filename (root, "lib", NULL);
111 _gtk_get_localedir (void)
113 static char *gtk_localedir = NULL;
114 if (gtk_localedir == NULL)
119 /* GTK_LOCALEDIR ends in either /lib/locale or
120 * /share/locale. Scan for that slash.
122 p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
128 root = g_win32_get_package_installation_directory_of_module (gtk_dll);
129 temp = g_build_filename (root, p, NULL);
132 /* gtk_localedir is passed to bindtextdomain() which isn't
135 gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
138 return gtk_localedir;
143 #include "gtkprivate.h"
145 /* Private type definitions
147 typedef struct _GtkInitFunction GtkInitFunction;
148 typedef struct _GtkQuitFunction GtkQuitFunction;
149 typedef struct _GtkClosure GtkClosure;
150 typedef struct _GtkKeySnooperData GtkKeySnooperData;
152 struct _GtkInitFunction
154 GtkFunction function;
158 struct _GtkQuitFunction
162 GtkCallbackMarshal marshal;
163 GtkFunction function;
165 GDestroyNotify destroy;
170 GtkCallbackMarshal marshal;
172 GDestroyNotify destroy;
175 struct _GtkKeySnooperData
177 GtkKeySnoopFunc func;
182 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
183 static void gtk_quit_destroy (GtkQuitFunction *quitf);
184 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
187 static void gtk_destroy_closure (gpointer data);
188 static gboolean gtk_invoke_idle_timeout (gpointer data);
189 static void gtk_invoke_input (gpointer data,
191 GdkInputCondition condition);
194 static void gtk_error (gchar *str);
195 static void gtk_warning (gchar *str);
196 static void gtk_message (gchar *str);
197 static void gtk_print (gchar *str);
200 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
202 const guint gtk_major_version = GTK_MAJOR_VERSION;
203 const guint gtk_minor_version = GTK_MINOR_VERSION;
204 const guint gtk_micro_version = GTK_MICRO_VERSION;
205 const guint gtk_binary_age = GTK_BINARY_AGE;
206 const guint gtk_interface_age = GTK_INTERFACE_AGE;
208 static guint gtk_main_loop_level = 0;
209 static gint pre_initialized = FALSE;
210 static gint gtk_initialized = FALSE;
211 static GList *current_events = NULL;
213 static GSList *main_loops = NULL; /* stack of currently executing main loops */
215 static GList *init_functions = NULL; /* A list of init functions.
217 static GList *quit_functions = NULL; /* A list of quit functions.
219 static GSList *key_snoopers = NULL;
221 guint gtk_debug_flags = 0; /* Global GTK debug flag */
223 #ifdef G_ENABLE_DEBUG
224 static const GDebugKey gtk_debug_keys[] = {
225 {"misc", GTK_DEBUG_MISC},
226 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
227 {"text", GTK_DEBUG_TEXT},
228 {"tree", GTK_DEBUG_TREE},
229 {"updates", GTK_DEBUG_UPDATES},
230 {"keybindings", GTK_DEBUG_KEYBINDINGS},
231 {"multihead", GTK_DEBUG_MULTIHEAD},
232 {"modules", GTK_DEBUG_MODULES},
233 {"geometry", GTK_DEBUG_GEOMETRY},
234 {"icontheme", GTK_DEBUG_ICONTHEME},
235 {"printing", GTK_DEBUG_PRINTING},
236 {"builder", GTK_DEBUG_BUILDER}
238 #endif /* G_ENABLE_DEBUG */
242 * @required_major: the required major version.
243 * @required_minor: the required minor version.
244 * @required_micro: the required micro version.
246 * Checks that the GTK+ library in use is compatible with the
247 * given version. Generally you would pass in the constants
248 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
249 * as the three arguments to this function; that produces
250 * a check that the library in use is compatible with
251 * the version of GTK+ the application or module was compiled
254 * Compatibility is defined by two things: first the version
255 * of the running library is newer than the version
256 * @required_major.required_minor.@required_micro. Second
257 * the running library must be binary compatible with the
258 * version @required_major.required_minor.@required_micro
259 * (same major version.)
261 * This function is primarily for GTK+ modules; the module
262 * can call this function to check that it wasn't loaded
263 * into an incompatible version of GTK+. However, such a
264 * a check isn't completely reliable, since the module may be
265 * linked against an old version of GTK+ and calling the
266 * old version of gtk_check_version(), but still get loaded
267 * into an application using a newer version of GTK+.
269 * Return value: %NULL if the GTK+ library is compatible with the
270 * given version, or a string describing the version mismatch.
271 * The returned string is owned by GTK+ and should not be modified
275 gtk_check_version (guint required_major,
276 guint required_minor,
277 guint required_micro)
279 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
280 gint required_effective_micro = 100 * required_minor + required_micro;
282 if (required_major > GTK_MAJOR_VERSION)
283 return "Gtk+ version too old (major mismatch)";
284 if (required_major < GTK_MAJOR_VERSION)
285 return "Gtk+ version too new (major mismatch)";
286 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
287 return "Gtk+ version too new (micro mismatch)";
288 if (required_effective_micro > gtk_effective_micro)
289 return "Gtk+ version too old (micro mismatch)";
293 /* This checks to see if the process is running suid or sgid
294 * at the current time. If so, we don't allow GTK+ to be initialized.
295 * This is meant to be a mild check - we only error out if we
296 * can prove the programmer is doing something wrong, not if
297 * they could be doing something wrong. For this reason, we
298 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
303 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
305 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
306 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
308 #ifdef HAVE_GETRESUID
309 /* These aren't in the header files, so we prototype them here.
311 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
312 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
314 if (getresuid (&ruid, &euid, &suid) != 0 ||
315 getresgid (&rgid, &egid, &sgid) != 0)
316 #endif /* HAVE_GETRESUID */
318 suid = ruid = getuid ();
319 sgid = rgid = getgid ();
324 if (ruid != euid || ruid != suid ||
325 rgid != egid || rgid != sgid)
327 g_warning ("This process is currently running setuid or setgid.\n"
328 "This is not a supported use of GTK+. You must create a helper\n"
329 "program instead. For further details, see:\n\n"
330 " http://www.gtk.org/setuid.html\n\n"
331 "Refusing to initialize GTK+.");
341 _gtk_get_datadir (void)
343 static char *gtk_datadir = NULL;
344 if (gtk_datadir == NULL)
346 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
347 gtk_datadir = g_build_filename (root, "share", NULL);
355 _gtk_get_sysconfdir (void)
357 static char *gtk_sysconfdir = NULL;
358 if (gtk_sysconfdir == NULL)
360 gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
361 gtk_sysconfdir = g_build_filename (root, "etc", NULL);
365 return gtk_sysconfdir;
369 _gtk_get_data_prefix (void)
371 static char *gtk_data_prefix = NULL;
372 if (gtk_data_prefix == NULL)
373 gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);
375 return gtk_data_prefix;
378 #endif /* G_OS_WIN32 */
380 static gboolean do_setlocale = TRUE;
383 * gtk_disable_setlocale:
385 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
386 * gtk_parse_args() from automatically
387 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
388 * want to use this function if you wanted to set the locale for
389 * your program to something other than the user's locale, or if
390 * you wanted to set different values for different locale categories.
392 * Most programs should not need to call this function.
395 gtk_disable_setlocale (void)
398 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
400 do_setlocale = FALSE;
403 #ifdef G_PLATFORM_WIN32
404 #undef gtk_init_check
407 static GString *gtk_modules_string = NULL;
408 static gboolean g_fatal_warnings = FALSE;
410 #ifdef G_ENABLE_DEBUG
412 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
414 gtk_debug_flags |= g_parse_debug_string (value,
416 G_N_ELEMENTS (gtk_debug_keys));
422 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
424 gtk_debug_flags &= ~g_parse_debug_string (value,
426 G_N_ELEMENTS (gtk_debug_keys));
430 #endif /* G_ENABLE_DEBUG */
433 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
437 if (gtk_modules_string)
438 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
440 gtk_modules_string = g_string_new (NULL);
442 g_string_append (gtk_modules_string, value);
448 static const GOptionEntry gtk_args[] = {
449 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
450 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
451 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
452 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
453 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
454 #ifdef G_ENABLE_DEBUG
455 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
456 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
457 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
458 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
459 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
460 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
467 static char *iso639_to_check = NULL;
468 static char *iso3166_to_check = NULL;
469 static char *script_to_check = NULL;
470 static gboolean setlocale_called = FALSE;
473 enum_locale_proc (LPTSTR locale)
481 lcid = strtoul (locale, &endptr, 16);
482 if (*endptr == '\0' &&
483 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
484 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
486 if (strcmp (iso639, iso639_to_check) == 0 &&
487 ((iso3166_to_check != NULL &&
488 strcmp (iso3166, iso3166_to_check) == 0) ||
489 (iso3166_to_check == NULL &&
490 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
492 char language[100], country[100];
495 if (script_to_check != NULL)
497 /* If lcid is the "other" script for this language,
498 * return TRUE, i.e. continue looking.
500 if (strcmp (script_to_check, "Latn") == 0)
502 switch (LANGIDFROMLCID (lcid))
504 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
506 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
508 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
510 case MAKELANGID (LANG_SERBIAN, 0x07):
511 /* Serbian in Bosnia and Herzegovina, Cyrillic */
515 else if (strcmp (script_to_check, "Cyrl") == 0)
517 switch (LANGIDFROMLCID (lcid))
519 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
521 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
523 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
525 case MAKELANGID (LANG_SERBIAN, 0x06):
526 /* Serbian in Bosnia and Herzegovina, Latin */
532 SetThreadLocale (lcid);
534 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
535 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
537 strcpy (locale, language);
538 strcat (locale, "_");
539 strcat (locale, country);
541 if (setlocale (LC_ALL, locale) != NULL)
542 setlocale_called = TRUE;
555 setlocale_initialization (void)
557 static gboolean initialized = FALSE;
566 /* If some of the POSIXish environment variables are set, set
567 * the Win32 thread locale correspondingly.
569 char *p = getenv ("LC_ALL");
576 if (strcmp (p, "C") == 0)
577 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
580 /* Check if one of the supported locales match the
581 * environment variable. If so, use that locale.
584 iso3166_to_check = strchr (iso639_to_check, '_');
585 if (iso3166_to_check != NULL)
587 *iso3166_to_check++ = '\0';
589 script_to_check = strchr (iso3166_to_check, '@');
590 if (script_to_check != NULL)
591 *script_to_check++ = '\0';
593 /* Handle special cases. */
595 /* The standard code for Serbia and Montenegro was
596 * "CS", but MSFT uses for some reason "SP". By now
597 * (October 2006), SP has split into two, "RS" and
598 * "ME", but don't bother trying to handle those
599 * yet. Do handle the even older "YU", though.
601 if (strcmp (iso3166_to_check, "CS") == 0 ||
602 strcmp (iso3166_to_check, "YU") == 0)
603 iso3166_to_check = "SP";
607 script_to_check = strchr (iso639_to_check, '@');
608 if (script_to_check != NULL)
609 *script_to_check++ = '\0';
610 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
611 if (strcmp (iso639_to_check, "sr") == 0)
612 iso3166_to_check = "SP";
615 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
619 if (!setlocale_called)
620 setlocale (LC_ALL, "");
622 if (!setlocale (LC_ALL, ""))
623 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
629 do_pre_parse_initialization (int *argc,
632 const gchar *env_string;
635 g_set_error_handler (gtk_error);
636 g_set_warning_handler (gtk_warning);
637 g_set_message_handler (gtk_message);
638 g_set_print_handler (gtk_print);
644 pre_initialized = TRUE;
646 gdk_pre_parse_libgtk_only ();
647 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
649 #ifdef G_ENABLE_DEBUG
650 env_string = g_getenv ("GTK_DEBUG");
651 if (env_string != NULL)
653 gtk_debug_flags = g_parse_debug_string (env_string,
655 G_N_ELEMENTS (gtk_debug_keys));
658 #endif /* G_ENABLE_DEBUG */
660 env_string = g_getenv ("GTK_MODULES");
662 gtk_modules_string = g_string_new (env_string);
666 gettext_initialization (void)
668 setlocale_initialization ();
671 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
672 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
673 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
674 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
675 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
681 do_post_parse_initialization (int *argc,
687 gettext_initialization ();
689 if (g_fatal_warnings)
691 GLogLevelFlags fatal_mask;
693 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
694 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
695 g_log_set_always_fatal (fatal_mask);
698 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
699 gdk_window_set_debug_updates (TRUE);
702 /* Translate to default:RTL if you want your widgets
703 * to be RTL, otherwise translate to default:LTR.
704 * Do *not* translate it to "predefinito:LTR", if it
705 * it isn't default:LTR or default:RTL it will not work
707 char *e = _("default:LTR");
708 if (strcmp (e, "default:RTL")==0)
709 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
710 else if (strcmp (e, "default:LTR"))
711 g_warning ("Whoever translated default:LTR did so wrongly.\n");
714 /* do what the call to gtk_type_init() used to do */
716 gtk_object_get_type ();
718 _gtk_accel_map_init ();
721 /* Set the 'initialized' flag.
723 gtk_initialized = TRUE;
725 /* load gtk modules */
726 if (gtk_modules_string)
728 _gtk_modules_init (argc, argv, gtk_modules_string->str);
729 g_string_free (gtk_modules_string, TRUE);
733 _gtk_modules_init (argc, argv, NULL);
740 gboolean open_default_display;
744 pre_parse_hook (GOptionContext *context,
749 do_pre_parse_initialization (NULL, NULL);
755 post_parse_hook (GOptionContext *context,
760 OptionGroupInfo *info = data;
763 do_post_parse_initialization (NULL, NULL);
765 if (info->open_default_display)
767 if (gdk_display_open_default_libgtk_only () == NULL)
769 const char *display_name = gdk_get_display_arg_name ();
772 G_OPTION_ERROR_FAILED,
773 _("Cannot open display: %s"),
774 display_name ? display_name : "" );
785 * gtk_get_option_group:
786 * @open_default_display: whether to open the default display
787 * when parsing the commandline arguments
789 * Returns a #GOptionGroup for the commandline arguments recognized
790 * by GTK+ and GDK. You should add this group to your #GOptionContext
791 * with g_option_context_add_group(), if you are using
792 * g_option_context_parse() to parse your commandline arguments.
794 * Returns: a #GOptionGroup for the commandline arguments recognized
800 gtk_get_option_group (gboolean open_default_display)
803 OptionGroupInfo *info;
805 gettext_initialization ();
807 info = g_new0 (OptionGroupInfo, 1);
808 info->open_default_display = open_default_display;
810 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
811 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
813 gdk_add_option_entries_libgtk_only (group);
814 g_option_group_add_entries (group, gtk_args);
815 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
821 * gtk_init_with_args:
822 * @argc: a pointer to the number of command line arguments.
823 * @argv: a pointer to the array of command line arguments.
824 * @parameter_string: a string which is displayed in
825 * the first line of <option>--help</option> output, after
826 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
827 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
828 * describing the options of your program
829 * @translation_domain: a translation domain to use for translating
830 * the <option>--help</option> output for the options in @entries
831 * with gettext(), or %NULL
832 * @error: a return location for errors
834 * This function does the same work as gtk_init_check().
835 * Additionally, it allows you to add your own commandline options,
836 * and it automatically generates nicely formatted
837 * <option>--help</option> output. Note that your program will
838 * be terminated after writing out the help output.
840 * Returns: %TRUE if the GUI has been successfully initialized,
846 gtk_init_with_args (int *argc,
848 char *parameter_string,
849 GOptionEntry *entries,
850 char *translation_domain,
853 GOptionContext *context;
854 GOptionGroup *gtk_group;
858 return gdk_display_open_default_libgtk_only () != NULL;
860 gettext_initialization ();
862 if (!check_setugid ())
865 gtk_group = gtk_get_option_group (TRUE);
867 context = g_option_context_new (parameter_string);
868 g_option_context_add_group (context, gtk_group);
871 g_option_context_add_main_entries (context, entries, translation_domain);
872 retval = g_option_context_parse (context, argc, argv, error);
874 g_option_context_free (context);
882 * @argc: a pointer to the number of command line arguments.
883 * @argv: a pointer to the array of command line arguments.
885 * Parses command line arguments, and initializes global
886 * attributes of GTK+, but does not actually open a connection
887 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
889 * Any arguments used by GTK+ or GDK are removed from the array and
890 * @argc and @argv are updated accordingly.
892 * You shouldn't call this function explicitely if you are using
893 * gtk_init(), or gtk_init_check().
895 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
898 gtk_parse_args (int *argc,
901 GOptionContext *option_context;
902 GOptionGroup *gtk_group;
903 GError *error = NULL;
908 gettext_initialization ();
910 if (!check_setugid ())
913 option_context = g_option_context_new (NULL);
914 g_option_context_set_ignore_unknown_options (option_context, TRUE);
915 g_option_context_set_help_enabled (option_context, FALSE);
916 gtk_group = gtk_get_option_group (FALSE);
917 g_option_context_set_main_group (option_context, gtk_group);
918 if (!g_option_context_parse (option_context, argc, argv, &error))
920 g_warning ("%s", error->message);
921 g_error_free (error);
924 g_option_context_free (option_context);
929 #ifdef G_PLATFORM_WIN32
930 #undef gtk_init_check
935 * @argc: Address of the <parameter>argc</parameter> parameter of your
936 * main() function. Changed if any arguments were handled.
937 * @argv: Address of the <parameter>argv</parameter> parameter of main().
938 * Any parameters understood by gtk_init() are stripped before return.
940 * This function does the same work as gtk_init() with only
941 * a single change: It does not terminate the program if the GUI can't be
942 * initialized. Instead it returns %FALSE on failure.
944 * This way the application can fall back to some other means of communication
945 * with the user - for example a curses or command line interface.
947 * Return value: %TRUE if the GUI has been successfully initialized,
951 gtk_init_check (int *argc,
954 if (!gtk_parse_args (argc, argv))
957 return gdk_display_open_default_libgtk_only () != NULL;
960 #ifdef G_PLATFORM_WIN32
966 * @argc: Address of the <parameter>argc</parameter> parameter of your
967 * main() function. Changed if any arguments were handled.
968 * @argv: Address of the <parameter>argv</parameter> parameter of main().
969 * Any parameters understood by gtk_init() are stripped before return.
971 * Call this function before using any other GTK+ functions in your GUI
972 * applications. It will initialize everything needed to operate the
973 * toolkit and parses some standard command line options. @argc and
974 * @argv are adjusted accordingly so your own code will
975 * never see those standard arguments.
977 * Note that there are some alternative ways to initialize GTK+:
978 * if you are calling gtk_parse_args(), gtk_init_check(),
979 * gtk_init_with_args() or g_option_context_parse() with
980 * the option group returned by gtk_get_option_group(), you
981 * <emphasis>don't</emphasis> have to call gtk_init().
984 * This function will terminate your program if it was unable to initialize
985 * the GUI for some reason. If you want your program to fall back to a
986 * textual interface you want to call gtk_init_check() instead.
990 gtk_init (int *argc, char ***argv)
992 if (!gtk_init_check (argc, argv))
994 const char *display_name_arg = gdk_get_display_arg_name ();
995 if (display_name_arg == NULL)
996 display_name_arg = getenv("DISPLAY");
997 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1002 #ifdef G_PLATFORM_WIN32
1005 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1007 if (sizeof_GtkWindow != sizeof (GtkWindow))
1008 g_error ("Incompatible build!\n"
1009 "The code using GTK+ thinks GtkWindow is of different\n"
1010 "size than it actually is in this build of GTK+.\n"
1011 "On Windows, this probably means that you have compiled\n"
1012 "your code with gcc without the -mms-bitfields switch,\n"
1013 "or that you are using an unsupported compiler.");
1016 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1017 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1018 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
1019 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1020 * -fnative-struct still matters. GtkBox is one such.
1023 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1025 if (sizeof_GtkBox != sizeof (GtkBox))
1026 g_error ("Incompatible build!\n"
1027 "The code using GTK+ thinks GtkBox is of different\n"
1028 "size than it actually is in this build of GTK+.\n"
1029 "On Windows, this probably means that you have compiled\n"
1030 "your code with gcc without the -mms-bitfields switch,\n"
1031 "or that you are using an unsupported compiler.");
1034 /* These two functions might get more checks added later, thus pass
1035 * in the number of extra args.
1038 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1040 check_sizeof_GtkWindow (sizeof_GtkWindow);
1041 if (num_checks >= 2)
1042 check_sizeof_GtkBox (sizeof_GtkBox);
1043 gtk_init (argc, argv);
1047 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1049 check_sizeof_GtkWindow (sizeof_GtkWindow);
1050 if (num_checks >= 2)
1051 check_sizeof_GtkBox (sizeof_GtkBox);
1052 return gtk_init_check (argc, argv);
1058 gtk_exit (gint errorcode)
1067 * Initializes internationalization support for GTK+. gtk_init()
1068 * automatically does this, so there is typically no point
1069 * in calling this function.
1071 * If you are calling this function because you changed the locale
1072 * after GTK+ is was initialized, then calling this function
1073 * may help a bit. (Note, however, that changing the locale
1074 * after GTK+ is initialized may produce inconsistent results and
1075 * is not really supported.)
1077 * In detail - sets the current locale according to the
1078 * program environment. This is the same as calling the C library function
1079 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
1080 * locale specific setup of the windowing system used by GDK.
1082 * Returns: a string corresponding to the locale set, typically in the
1083 * form lang_COUNTRY, where lang is an ISO-639 language code, and
1084 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
1085 * result of the setlocale(); it is also used on other machines, such as
1086 * Windows, where the C library returns a different result. The string is
1087 * owned by GTK+ and should not be modified or freed.
1090 gtk_set_locale (void)
1092 return gdk_set_locale ();
1096 * _gtk_get_lc_ctype:
1098 * Return the Unix-style locale string for the language currently in
1099 * effect. On Unix systems, this is the return value from
1100 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1101 * affect this through the environment variables LC_ALL, LC_CTYPE or
1102 * LANG (checked in that order). The locale strings typically is in
1103 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1104 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1105 * Swedish as written in Finland or pt_BR for Portuguese as written in
1108 * On Windows, the C library doesn't use any such environment
1109 * variables, and setting them won't affect the behaviour of functions
1110 * like ctime(). The user sets the locale through the Regional Options
1111 * in the Control Panel. The C library (in the setlocale() function)
1112 * does not use country and language codes, but country and language
1113 * names spelled out in English.
1114 * However, this function does check the above environment
1115 * variables, and does return a Unix-style locale string based on
1116 * either said environment variables or the thread's current locale.
1118 * Return value: a dynamically allocated string, free with g_free().
1122 _gtk_get_lc_ctype (void)
1125 /* Somebody might try to set the locale for this process using the
1126 * LANG or LC_ environment variables. The Microsoft C library
1127 * doesn't know anything about them. You set the locale in the
1128 * Control Panel. Setting these env vars won't have any affect on
1129 * locale-dependent C library functions like ctime(). But just for
1130 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1131 * it easier to test GTK and Pango in various default languages, you
1132 * don't have to clickety-click in the Control Panel, you can simply
1133 * start the program with LC_ALL=something on the command line.)
1137 p = getenv ("LC_ALL");
1139 return g_strdup (p);
1141 p = getenv ("LC_CTYPE");
1143 return g_strdup (p);
1145 p = getenv ("LANG");
1147 return g_strdup (p);
1149 return g_win32_getlocale ();
1151 return g_strdup (setlocale (LC_CTYPE, NULL));
1156 * gtk_get_default_language:
1158 * Returns the #PangoLanguage for the default language currently in
1159 * effect. (Note that this can change over the life of an
1160 * application.) The default language is derived from the current
1161 * locale. It determines, for example, whether GTK+ uses the
1162 * right-to-left or left-to-right text direction.
1164 * This function is equivalent to pango_language_get_default(). See
1165 * that function for details.
1167 * Return value: the default language as a #PangoLanguage, must not be
1171 gtk_get_default_language (void)
1173 return pango_language_get_default ();
1181 GtkInitFunction *init;
1184 gtk_main_loop_level++;
1186 loop = g_main_loop_new (NULL, TRUE);
1187 main_loops = g_slist_prepend (main_loops, loop);
1189 tmp_list = functions = init_functions;
1190 init_functions = NULL;
1194 init = tmp_list->data;
1195 tmp_list = tmp_list->next;
1197 (* init->function) (init->data);
1200 g_list_free (functions);
1202 if (g_main_loop_is_running (main_loops->data))
1204 GDK_THREADS_LEAVE ();
1205 g_main_loop_run (loop);
1206 GDK_THREADS_ENTER ();
1212 GList *reinvoke_list = NULL;
1213 GtkQuitFunction *quitf;
1215 while (quit_functions)
1217 quitf = quit_functions->data;
1219 tmp_list = quit_functions;
1220 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1221 g_list_free_1 (tmp_list);
1223 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1224 gtk_quit_invoke_function (quitf))
1226 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1230 gtk_quit_destroy (quitf);
1237 work = g_list_last (reinvoke_list);
1239 quit_functions->prev = work;
1240 work->next = quit_functions;
1241 quit_functions = work;
1247 main_loops = g_slist_remove (main_loops, loop);
1249 g_main_loop_unref (loop);
1251 gtk_main_loop_level--;
1253 if (gtk_main_loop_level == 0)
1255 /* Try storing all clipboard data we have */
1256 _gtk_clipboard_store_all ();
1258 /* Synchronize the recent manager singleton */
1259 _gtk_recent_manager_sync ();
1264 gtk_main_level (void)
1266 return gtk_main_loop_level;
1270 gtk_main_quit (void)
1272 g_return_if_fail (main_loops != NULL);
1274 g_main_loop_quit (main_loops->data);
1278 gtk_events_pending (void)
1282 GDK_THREADS_LEAVE ();
1283 result = g_main_context_pending (NULL);
1284 GDK_THREADS_ENTER ();
1290 gtk_main_iteration (void)
1292 GDK_THREADS_LEAVE ();
1293 g_main_context_iteration (NULL, TRUE);
1294 GDK_THREADS_ENTER ();
1297 return !g_main_loop_is_running (main_loops->data);
1303 gtk_main_iteration_do (gboolean blocking)
1305 GDK_THREADS_LEAVE ();
1306 g_main_context_iteration (NULL, blocking);
1307 GDK_THREADS_ENTER ();
1310 return !g_main_loop_is_running (main_loops->data);
1315 /* private libgtk to libgdk interfaces
1317 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1318 GdkWindow **grab_window,
1319 gboolean *owner_events);
1320 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1321 GdkWindow **grab_window,
1322 gboolean *owner_events);
1325 rewrite_events_translate (GdkWindow *old_window,
1326 GdkWindow *new_window,
1330 gint old_origin_x, old_origin_y;
1331 gint new_origin_x, new_origin_y;
1333 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1334 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1336 *x += old_origin_x - new_origin_x;
1337 *y += old_origin_y - new_origin_y;
1341 rewrite_event_for_window (GdkEvent *event,
1342 GdkWindow *new_window)
1344 event = gdk_event_copy (event);
1346 switch (event->type)
1349 rewrite_events_translate (event->any.window,
1351 &event->scroll.x, &event->scroll.y);
1353 case GDK_BUTTON_PRESS:
1354 case GDK_2BUTTON_PRESS:
1355 case GDK_3BUTTON_PRESS:
1356 case GDK_BUTTON_RELEASE:
1357 rewrite_events_translate (event->any.window,
1359 &event->button.x, &event->button.y);
1361 case GDK_MOTION_NOTIFY:
1362 rewrite_events_translate (event->any.window,
1364 &event->motion.x, &event->motion.y);
1367 case GDK_KEY_RELEASE:
1368 case GDK_PROXIMITY_IN:
1369 case GDK_PROXIMITY_OUT:
1376 g_object_unref (event->any.window);
1377 event->any.window = g_object_ref (new_window);
1382 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1383 * then what X11 does is deliver the event normally if it was going to this
1384 * client, otherwise, delivers it in terms of the grab window. This function
1385 * rewrites events to the effect that events going to the same window group
1386 * are delivered normally, otherwise, the event is delivered in terms of the
1390 rewrite_event_for_grabs (GdkEvent *event)
1392 GdkWindow *grab_window;
1393 GtkWidget *event_widget, *grab_widget;
1394 gpointer grab_widget_ptr;
1395 gboolean owner_events;
1396 GdkDisplay *display;
1398 switch (event->type)
1401 case GDK_BUTTON_PRESS:
1402 case GDK_2BUTTON_PRESS:
1403 case GDK_3BUTTON_PRESS:
1404 case GDK_BUTTON_RELEASE:
1405 case GDK_MOTION_NOTIFY:
1406 case GDK_PROXIMITY_IN:
1407 case GDK_PROXIMITY_OUT:
1408 display = gdk_drawable_get_display (event->proximity.window);
1409 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1415 case GDK_KEY_RELEASE:
1416 display = gdk_drawable_get_display (event->key.window);
1417 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1426 event_widget = gtk_get_event_widget (event);
1427 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1428 grab_widget = grab_widget_ptr;
1431 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1432 return rewrite_event_for_window (event, grab_window);
1438 gtk_main_do_event (GdkEvent *event)
1440 GtkWidget *event_widget;
1441 GtkWidget *grab_widget;
1442 GtkWindowGroup *window_group;
1443 GdkEvent *rewritten_event = NULL;
1446 if (event->type == GDK_SETTING)
1448 _gtk_settings_handle_event (&event->setting);
1452 if (event->type == GDK_OWNER_CHANGE)
1454 _gtk_clipboard_handle_event (&event->owner_change);
1458 /* Find the widget which got the event. We store the widget
1459 * in the user_data field of GdkWindow's.
1460 * Ignore the event if we don't have a widget for it, except
1461 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1462 * Though this happens rarely, bogus events can occour
1463 * for e.g. destroyed GdkWindows.
1465 event_widget = gtk_get_event_widget (event);
1468 /* To handle selection INCR transactions, we select
1469 * PropertyNotify events on the requestor window and create
1470 * a corresponding (fake) GdkWindow so that events get
1471 * here. There won't be a widget though, so we have to handle
1474 if (event->type == GDK_PROPERTY_NOTIFY)
1475 _gtk_selection_incr_event (event->any.window,
1481 /* If pointer or keyboard grabs are in effect, munge the events
1482 * so that each window group looks like a separate app.
1484 rewritten_event = rewrite_event_for_grabs (event);
1485 if (rewritten_event)
1487 event = rewritten_event;
1488 event_widget = gtk_get_event_widget (event);
1491 window_group = gtk_main_get_window_group (event_widget);
1493 /* Push the event onto a stack of current events for
1494 * gtk_current_event_get().
1496 current_events = g_list_prepend (current_events, event);
1498 /* If there is a grab in effect...
1500 if (window_group->grabs)
1502 grab_widget = window_group->grabs->data;
1504 /* If the grab widget is an ancestor of the event widget
1505 * then we send the event to the original event widget.
1506 * This is the key to implementing modality.
1508 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1509 gtk_widget_is_ancestor (event_widget, grab_widget))
1510 grab_widget = event_widget;
1514 grab_widget = event_widget;
1517 /* Not all events get sent to the grabbing widget.
1518 * The delete, destroy, expose, focus change and resize
1519 * events still get sent to the event widget because
1520 * 1) these events have no meaning for the grabbing widget
1521 * and 2) redirecting these events to the grabbing widget
1522 * could cause the display to be messed up.
1524 * Drag events are also not redirected, since it isn't
1525 * clear what the semantics of that would be.
1527 switch (event->type)
1533 g_object_ref (event_widget);
1534 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1535 !gtk_widget_event (event_widget, event))
1536 gtk_widget_destroy (event_widget);
1537 g_object_unref (event_widget);
1541 /* Unexpected GDK_DESTROY from the outside, ignore for
1542 * child windows, handle like a GDK_DELETE for toplevels
1544 if (!event_widget->parent)
1546 g_object_ref (event_widget);
1547 if (!gtk_widget_event (event_widget, event) &&
1548 GTK_WIDGET_REALIZED (event_widget))
1549 gtk_widget_destroy (event_widget);
1550 g_object_unref (event_widget);
1555 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1557 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1558 gtk_widget_send_expose (event_widget, event);
1559 gdk_window_end_paint (event->any.window);
1562 gtk_widget_send_expose (event_widget, event);
1565 case GDK_PROPERTY_NOTIFY:
1567 case GDK_FOCUS_CHANGE:
1571 case GDK_SELECTION_CLEAR:
1572 case GDK_SELECTION_REQUEST:
1573 case GDK_SELECTION_NOTIFY:
1574 case GDK_CLIENT_EVENT:
1575 case GDK_VISIBILITY_NOTIFY:
1576 case GDK_WINDOW_STATE:
1577 case GDK_GRAB_BROKEN:
1579 gtk_widget_event (event_widget, event);
1583 case GDK_BUTTON_PRESS:
1584 case GDK_2BUTTON_PRESS:
1585 case GDK_3BUTTON_PRESS:
1586 gtk_propagate_event (grab_widget, event);
1590 case GDK_KEY_RELEASE:
1593 if (gtk_invoke_key_snoopers (grab_widget, event))
1596 /* else fall through */
1597 case GDK_MOTION_NOTIFY:
1598 case GDK_BUTTON_RELEASE:
1599 case GDK_PROXIMITY_IN:
1600 case GDK_PROXIMITY_OUT:
1601 gtk_propagate_event (grab_widget, event);
1604 case GDK_ENTER_NOTIFY:
1605 GTK_PRIVATE_SET_FLAG (event_widget, GTK_HAS_POINTER);
1606 _gtk_widget_set_pointer_window (event_widget, event->any.window);
1607 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1608 gtk_widget_event (grab_widget, event);
1611 case GDK_LEAVE_NOTIFY:
1612 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_HAS_POINTER);
1613 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1614 gtk_widget_event (grab_widget, event);
1617 case GDK_DRAG_STATUS:
1618 case GDK_DROP_FINISHED:
1619 _gtk_drag_source_handle_event (event_widget, event);
1621 case GDK_DRAG_ENTER:
1622 case GDK_DRAG_LEAVE:
1623 case GDK_DRAG_MOTION:
1624 case GDK_DROP_START:
1625 _gtk_drag_dest_handle_event (event_widget, event);
1628 g_assert_not_reached ();
1632 if (event->type == GDK_ENTER_NOTIFY
1633 || event->type == GDK_LEAVE_NOTIFY
1634 || event->type == GDK_BUTTON_PRESS
1635 || event->type == GDK_2BUTTON_PRESS
1636 || event->type == GDK_3BUTTON_PRESS
1637 || event->type == GDK_KEY_PRESS
1638 || event->type == GDK_DRAG_ENTER
1639 || event->type == GDK_GRAB_BROKEN
1640 || event->type == GDK_MOTION_NOTIFY
1641 || event->type == GDK_SCROLL)
1643 _gtk_tooltip_handle_event (event);
1646 tmp_list = current_events;
1647 current_events = g_list_remove_link (current_events, tmp_list);
1648 g_list_free_1 (tmp_list);
1650 if (rewritten_event)
1651 gdk_event_free (rewritten_event);
1666 static GtkWindowGroup *
1667 gtk_main_get_window_group (GtkWidget *widget)
1669 GtkWidget *toplevel = NULL;
1672 toplevel = gtk_widget_get_toplevel (widget);
1674 if (GTK_IS_WINDOW (toplevel))
1675 return gtk_window_get_group (GTK_WINDOW (toplevel));
1677 return gtk_window_get_group (NULL);
1682 GtkWidget *old_grab_widget;
1683 GtkWidget *new_grab_widget;
1684 gboolean was_grabbed;
1685 gboolean is_grabbed;
1690 gtk_grab_notify_foreach (GtkWidget *child,
1694 GrabNotifyInfo *info = data;
1696 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
1698 was_grabbed = info->was_grabbed;
1699 is_grabbed = info->is_grabbed;
1701 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
1702 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
1704 was_shadowed = info->old_grab_widget && !info->was_grabbed;
1705 is_shadowed = info->new_grab_widget && !info->is_grabbed;
1707 g_object_ref (child);
1709 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
1710 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1714 GTK_PRIVATE_SET_FLAG (child, GTK_SHADOWED);
1715 if (!was_shadowed && GTK_WIDGET_HAS_POINTER (child)
1716 && GTK_WIDGET_IS_SENSITIVE (child))
1717 _gtk_widget_synthesize_crossing (child, info->new_grab_widget,
1718 GDK_CROSSING_GTK_GRAB);
1722 GTK_PRIVATE_UNSET_FLAG (child, GTK_SHADOWED);
1723 if (was_shadowed && GTK_WIDGET_HAS_POINTER (child)
1724 && GTK_WIDGET_IS_SENSITIVE (child))
1725 _gtk_widget_synthesize_crossing (info->old_grab_widget, child,
1726 info->from_grab ? GDK_CROSSING_GTK_GRAB
1727 : GDK_CROSSING_GTK_UNGRAB);
1730 if (was_shadowed != is_shadowed)
1731 _gtk_widget_grab_notify (child, was_shadowed);
1733 g_object_unref (child);
1735 info->was_grabbed = was_grabbed;
1736 info->is_grabbed = is_grabbed;
1740 gtk_grab_notify (GtkWindowGroup *group,
1741 GtkWidget *old_grab_widget,
1742 GtkWidget *new_grab_widget,
1746 GrabNotifyInfo info;
1748 if (old_grab_widget == new_grab_widget)
1751 info.old_grab_widget = old_grab_widget;
1752 info.new_grab_widget = new_grab_widget;
1753 info.from_grab = from_grab;
1755 g_object_ref (group);
1757 toplevels = gtk_window_list_toplevels ();
1758 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1762 GtkWindow *toplevel = toplevels->data;
1763 toplevels = g_list_delete_link (toplevels, toplevels);
1765 info.was_grabbed = FALSE;
1766 info.is_grabbed = FALSE;
1768 if (group == gtk_window_get_group (toplevel))
1769 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
1770 g_object_unref (toplevel);
1773 g_object_unref (group);
1777 gtk_grab_add (GtkWidget *widget)
1779 GtkWindowGroup *group;
1780 GtkWidget *old_grab_widget;
1782 g_return_if_fail (widget != NULL);
1784 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1786 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1788 group = gtk_main_get_window_group (widget);
1791 old_grab_widget = (GtkWidget *)group->grabs->data;
1793 old_grab_widget = NULL;
1795 g_object_ref (widget);
1796 group->grabs = g_slist_prepend (group->grabs, widget);
1798 gtk_grab_notify (group, old_grab_widget, widget, TRUE);
1803 gtk_grab_get_current (void)
1805 GtkWindowGroup *group;
1807 group = gtk_main_get_window_group (NULL);
1810 return GTK_WIDGET (group->grabs->data);
1815 gtk_grab_remove (GtkWidget *widget)
1817 GtkWindowGroup *group;
1818 GtkWidget *new_grab_widget;
1820 g_return_if_fail (widget != NULL);
1822 if (GTK_WIDGET_HAS_GRAB (widget))
1824 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1826 group = gtk_main_get_window_group (widget);
1827 group->grabs = g_slist_remove (group->grabs, widget);
1830 new_grab_widget = (GtkWidget *)group->grabs->data;
1832 new_grab_widget = NULL;
1834 gtk_grab_notify (group, widget, new_grab_widget, FALSE);
1836 g_object_unref (widget);
1841 gtk_init_add (GtkFunction function,
1844 GtkInitFunction *init;
1846 init = g_new (GtkInitFunction, 1);
1847 init->function = function;
1850 init_functions = g_list_prepend (init_functions, init);
1854 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1857 GtkKeySnooperData *data;
1858 static guint snooper_id = 1;
1860 g_return_val_if_fail (snooper != NULL, 0);
1862 data = g_new (GtkKeySnooperData, 1);
1863 data->func = snooper;
1864 data->func_data = func_data;
1865 data->id = snooper_id++;
1866 key_snoopers = g_slist_prepend (key_snoopers, data);
1872 gtk_key_snooper_remove (guint snooper_id)
1874 GtkKeySnooperData *data = NULL;
1877 slist = key_snoopers;
1881 if (data->id == snooper_id)
1884 slist = slist->next;
1889 key_snoopers = g_slist_remove (key_snoopers, data);
1895 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1899 gint return_val = FALSE;
1901 slist = key_snoopers;
1902 while (slist && !return_val)
1904 GtkKeySnooperData *data;
1907 slist = slist->next;
1908 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1915 gtk_quit_add_full (guint main_level,
1916 GtkFunction function,
1917 GtkCallbackMarshal marshal,
1919 GDestroyNotify destroy)
1921 static guint quit_id = 1;
1922 GtkQuitFunction *quitf;
1924 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1926 quitf = g_slice_new (GtkQuitFunction);
1928 quitf->id = quit_id++;
1929 quitf->main_level = main_level;
1930 quitf->function = function;
1931 quitf->marshal = marshal;
1933 quitf->destroy = destroy;
1935 quit_functions = g_list_prepend (quit_functions, quitf);
1941 gtk_quit_destroy (GtkQuitFunction *quitf)
1944 quitf->destroy (quitf->data);
1945 g_slice_free (GtkQuitFunction, quitf);
1949 gtk_quit_destructor (GtkObject **object_p)
1952 gtk_object_destroy (*object_p);
1959 gtk_quit_add_destroy (guint main_level,
1962 GtkObject **object_p;
1964 g_return_if_fail (main_level > 0);
1965 g_return_if_fail (GTK_IS_OBJECT (object));
1967 object_p = g_new (GtkObject*, 1);
1969 g_signal_connect (object,
1971 G_CALLBACK (gtk_widget_destroyed),
1973 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1977 gtk_quit_add (guint main_level,
1978 GtkFunction function,
1981 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1985 gtk_quit_remove (guint id)
1987 GtkQuitFunction *quitf;
1990 tmp_list = quit_functions;
1993 quitf = tmp_list->data;
1995 if (quitf->id == id)
1997 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1998 g_list_free (tmp_list);
1999 gtk_quit_destroy (quitf);
2004 tmp_list = tmp_list->next;
2009 gtk_quit_remove_by_data (gpointer data)
2011 GtkQuitFunction *quitf;
2014 tmp_list = quit_functions;
2017 quitf = tmp_list->data;
2019 if (quitf->data == data)
2021 quit_functions = g_list_remove_link (quit_functions, tmp_list);
2022 g_list_free (tmp_list);
2023 gtk_quit_destroy (quitf);
2028 tmp_list = tmp_list->next;
2033 gtk_timeout_add_full (guint32 interval,
2034 GtkFunction function,
2035 GtkCallbackMarshal marshal,
2037 GDestroyNotify destroy)
2041 GtkClosure *closure;
2043 closure = g_new (GtkClosure, 1);
2044 closure->marshal = marshal;
2045 closure->data = data;
2046 closure->destroy = destroy;
2048 return g_timeout_add_full (0, interval,
2049 gtk_invoke_idle_timeout,
2051 gtk_destroy_closure);
2054 return g_timeout_add_full (0, interval, function, data, destroy);
2058 gtk_timeout_add (guint32 interval,
2059 GtkFunction function,
2062 return g_timeout_add_full (0, interval, function, data, NULL);
2066 gtk_timeout_remove (guint tag)
2068 g_source_remove (tag);
2072 gtk_idle_add_full (gint priority,
2073 GtkFunction function,
2074 GtkCallbackMarshal marshal,
2076 GDestroyNotify destroy)
2080 GtkClosure *closure;
2082 closure = g_new (GtkClosure, 1);
2083 closure->marshal = marshal;
2084 closure->data = data;
2085 closure->destroy = destroy;
2087 return g_idle_add_full (priority,
2088 gtk_invoke_idle_timeout,
2090 gtk_destroy_closure);
2093 return g_idle_add_full (priority, function, data, destroy);
2097 gtk_idle_add (GtkFunction function,
2100 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2104 gtk_idle_add_priority (gint priority,
2105 GtkFunction function,
2108 return g_idle_add_full (priority, function, data, NULL);
2112 gtk_idle_remove (guint tag)
2114 g_source_remove (tag);
2118 gtk_idle_remove_by_data (gpointer data)
2120 if (!g_idle_remove_by_data (data))
2121 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2125 gtk_input_add_full (gint source,
2126 GdkInputCondition condition,
2127 GdkInputFunction function,
2128 GtkCallbackMarshal marshal,
2130 GDestroyNotify destroy)
2134 GtkClosure *closure;
2136 closure = g_new (GtkClosure, 1);
2137 closure->marshal = marshal;
2138 closure->data = data;
2139 closure->destroy = destroy;
2141 return gdk_input_add_full (source,
2143 (GdkInputFunction) gtk_invoke_input,
2145 (GDestroyNotify) gtk_destroy_closure);
2148 return gdk_input_add_full (source, condition, function, data, destroy);
2152 gtk_input_remove (guint tag)
2154 g_source_remove (tag);
2158 gtk_destroy_closure (gpointer data)
2160 GtkClosure *closure = data;
2162 if (closure->destroy)
2163 (closure->destroy) (closure->data);
2168 gtk_invoke_idle_timeout (gpointer data)
2170 GtkClosure *closure = data;
2173 gint ret_val = FALSE;
2174 args[0].name = NULL;
2175 args[0].type = G_TYPE_BOOLEAN;
2176 args[0].d.pointer_data = &ret_val;
2177 closure->marshal (NULL, closure->data, 0, args);
2182 gtk_invoke_input (gpointer data,
2184 GdkInputCondition condition)
2186 GtkClosure *closure = data;
2189 args[0].type = G_TYPE_INT;
2190 args[0].name = NULL;
2191 GTK_VALUE_INT (args[0]) = source;
2192 args[1].type = GDK_TYPE_INPUT_CONDITION;
2193 args[1].name = NULL;
2194 GTK_VALUE_FLAGS (args[1]) = condition;
2195 args[2].type = G_TYPE_NONE;
2196 args[2].name = NULL;
2198 closure->marshal (NULL, closure->data, 2, args);
2202 * gtk_get_current_event:
2204 * Obtains a copy of the event currently being processed by GTK+. For
2205 * example, if you get a "clicked" signal from #GtkButton, the current
2206 * event will be the #GdkEventButton that triggered the "clicked"
2207 * signal. The returned event must be freed with gdk_event_free().
2208 * If there is no current event, the function returns %NULL.
2210 * Return value: a copy of the current event, or %NULL if no current event.
2213 gtk_get_current_event (void)
2216 return gdk_event_copy (current_events->data);
2222 * gtk_get_current_event_time:
2224 * If there is a current event and it has a timestamp, return that
2225 * timestamp, otherwise return %GDK_CURRENT_TIME.
2227 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2230 gtk_get_current_event_time (void)
2233 return gdk_event_get_time (current_events->data);
2235 return GDK_CURRENT_TIME;
2239 * gtk_get_current_event_state:
2240 * @state: a location to store the state of the current event
2242 * If there is a current event and it has a state field, place
2243 * that state field in @state and return %TRUE, otherwise return
2246 * Return value: %TRUE if there was a current event and it had a state field
2249 gtk_get_current_event_state (GdkModifierType *state)
2251 g_return_val_if_fail (state != NULL, FALSE);
2254 return gdk_event_get_state (current_events->data, state);
2263 * gtk_get_event_widget:
2264 * @event: a #GdkEvent
2266 * If @event is %NULL or the event was not associated with any widget,
2267 * returns %NULL, otherwise returns the widget that received the event
2270 * Return value: the widget that originally received @event, or %NULL
2273 gtk_get_event_widget (GdkEvent *event)
2276 gpointer widget_ptr;
2279 if (event && event->any.window &&
2280 (event->type == GDK_DESTROY || !GDK_WINDOW_DESTROYED (event->any.window)))
2282 gdk_window_get_user_data (event->any.window, &widget_ptr);
2283 widget = widget_ptr;
2290 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2292 if (!quitf->marshal)
2293 return quitf->function (quitf->data);
2297 gint ret_val = FALSE;
2299 args[0].name = NULL;
2300 args[0].type = G_TYPE_BOOLEAN;
2301 args[0].d.pointer_data = &ret_val;
2302 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2310 * gtk_propagate_event:
2311 * @widget: a #GtkWidget
2314 * Sends an event to a widget, propagating the event to parent widgets
2315 * if the event remains unhandled. Events received by GTK+ from GDK
2316 * normally begin in gtk_main_do_event(). Depending on the type of
2317 * event, existence of modal dialogs, grabs, etc., the event may be
2318 * propagated; if so, this function is used. gtk_propagate_event()
2319 * calls gtk_widget_event() on each widget it decides to send the
2320 * event to. So gtk_widget_event() is the lowest-level function; it
2321 * simply emits the "event" and possibly an event-specific signal on a
2322 * widget. gtk_propagate_event() is a bit higher-level, and
2323 * gtk_main_do_event() is the highest level.
2325 * All that said, you most likely don't want to use any of these
2326 * functions; synthesizing events is rarely needed. Consider asking on
2327 * the mailing list for better ways to achieve your goals. For
2328 * example, use gdk_window_invalidate_rect() or
2329 * gtk_widget_queue_draw() instead of making up expose events.
2333 gtk_propagate_event (GtkWidget *widget,
2338 g_return_if_fail (GTK_IS_WIDGET (widget));
2339 g_return_if_fail (event != NULL);
2341 handled_event = FALSE;
2343 g_object_ref (widget);
2345 if ((event->type == GDK_KEY_PRESS) ||
2346 (event->type == GDK_KEY_RELEASE))
2348 /* Only send key events within Window widgets to the Window
2349 * The Window widget will in turn pass the
2350 * key event on to the currently focused widget
2355 window = gtk_widget_get_toplevel (widget);
2356 if (GTK_IS_WINDOW (window))
2358 /* If there is a grab within the window, give the grab widget
2359 * a first crack at the key event
2361 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2362 handled_event = gtk_widget_event (widget, event);
2366 window = gtk_widget_get_toplevel (widget);
2367 if (GTK_IS_WINDOW (window))
2369 if (GTK_WIDGET_IS_SENSITIVE (window))
2370 gtk_widget_event (window, event);
2374 handled_event = TRUE; /* don't send to widget */
2378 /* Other events get propagated up the widget tree
2379 * so that parents can see the button and motion
2380 * events of the children.
2388 /* Scroll events are special cased here because it
2389 * feels wrong when scrolling a GtkViewport, say,
2390 * to have children of the viewport eat the scroll
2393 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2394 handled_event = event->type != GDK_SCROLL;
2396 handled_event = gtk_widget_event (widget, event);
2398 tmp = widget->parent;
2399 g_object_unref (widget);
2403 if (!handled_event && widget)
2404 g_object_ref (widget);
2410 g_object_unref (widget);
2415 gtk_error (gchar *str)
2421 gtk_warning (gchar *str)
2427 gtk_message (gchar *str)
2433 gtk_print (gchar *str)
2435 static GtkWidget *window = NULL;
2436 static GtkWidget *text;
2437 static int level = 0;
2441 GtkWidget *hscrollbar;
2442 GtkWidget *vscrollbar;
2443 GtkWidget *separator;
2448 fputs (str, stdout);
2455 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2457 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2458 G_CALLBACK (gtk_widget_destroyed),
2461 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2463 box1 = gtk_vbox_new (FALSE, 0);
2464 gtk_container_add (GTK_CONTAINER (window), box1);
2465 gtk_widget_show (box1);
2468 box2 = gtk_vbox_new (FALSE, 10);
2469 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2470 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2471 gtk_widget_show (box2);
2474 table = gtk_table_new (2, 2, FALSE);
2475 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2476 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2477 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2478 gtk_widget_show (table);
2480 text = gtk_text_new (NULL, NULL);
2481 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2482 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2483 gtk_widget_show (text);
2484 gtk_widget_realize (text);
2486 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2487 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2488 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2489 gtk_widget_show (hscrollbar);
2491 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2492 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2493 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2494 gtk_widget_show (vscrollbar);
2496 separator = gtk_hseparator_new ();
2497 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2498 gtk_widget_show (separator);
2501 box2 = gtk_vbox_new (FALSE, 10);
2502 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2503 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2504 gtk_widget_show (box2);
2507 button = gtk_button_new_with_label ("close");
2508 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2509 G_CALLBACK (gtk_widget_hide),
2510 GTK_OBJECT (window));
2511 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2512 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2513 gtk_widget_grab_default (button);
2514 gtk_widget_show (button);
2518 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2521 if (!GTK_WIDGET_VISIBLE (window))
2522 gtk_widget_show (window);
2527 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2528 GValue *return_accu,
2529 const GValue *handler_return,
2532 gboolean continue_emission;
2533 gboolean signal_handled;
2535 signal_handled = g_value_get_boolean (handler_return);
2536 g_value_set_boolean (return_accu, signal_handled);
2537 continue_emission = !signal_handled;
2539 return continue_emission;
2542 #define __GTK_MAIN_C__
2543 #include "gtkaliasdef.c"