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 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
73 /* This here before inclusion of gtkprivate.h so that it sees the
74 * original GTK_LOCALEDIR definition. Yeah, this is a bit sucky.
77 _gtk_get_localedir (void)
79 static char *gtk_localedir = NULL;
80 if (gtk_localedir == NULL)
85 /* GTK_LOCALEDIR ends in either /lib/locale or
86 * /share/locale. Scan for that slash.
88 p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
94 temp = g_win32_get_package_installation_subdirectory
95 (GETTEXT_PACKAGE, dll_name, p);
97 /* gtk_localedir is passed to bindtextdomain() which isn't
100 gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
103 return gtk_localedir;
108 #include "gtkprivate.h"
110 /* Private type definitions
112 typedef struct _GtkInitFunction GtkInitFunction;
113 typedef struct _GtkQuitFunction GtkQuitFunction;
114 typedef struct _GtkClosure GtkClosure;
115 typedef struct _GtkKeySnooperData GtkKeySnooperData;
117 struct _GtkInitFunction
119 GtkFunction function;
123 struct _GtkQuitFunction
127 GtkCallbackMarshal marshal;
128 GtkFunction function;
130 GDestroyNotify destroy;
135 GtkCallbackMarshal marshal;
137 GDestroyNotify destroy;
140 struct _GtkKeySnooperData
142 GtkKeySnoopFunc func;
147 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
148 static void gtk_quit_destroy (GtkQuitFunction *quitf);
149 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
152 static void gtk_destroy_closure (gpointer data);
153 static gboolean gtk_invoke_idle_timeout (gpointer data);
154 static void gtk_invoke_input (gpointer data,
156 GdkInputCondition condition);
159 static void gtk_error (gchar *str);
160 static void gtk_warning (gchar *str);
161 static void gtk_message (gchar *str);
162 static void gtk_print (gchar *str);
165 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
167 const guint gtk_major_version = GTK_MAJOR_VERSION;
168 const guint gtk_minor_version = GTK_MINOR_VERSION;
169 const guint gtk_micro_version = GTK_MICRO_VERSION;
170 const guint gtk_binary_age = GTK_BINARY_AGE;
171 const guint gtk_interface_age = GTK_INTERFACE_AGE;
173 static guint gtk_main_loop_level = 0;
174 static gint pre_initialized = FALSE;
175 static gint gtk_initialized = FALSE;
176 static GList *current_events = NULL;
178 static GSList *main_loops = NULL; /* stack of currently executing main loops */
180 static GList *init_functions = NULL; /* A list of init functions.
182 static GList *quit_functions = NULL; /* A list of quit functions.
184 static GSList *key_snoopers = NULL;
186 guint gtk_debug_flags = 0; /* Global GTK debug flag */
188 #ifdef G_ENABLE_DEBUG
189 static const GDebugKey gtk_debug_keys[] = {
190 {"misc", GTK_DEBUG_MISC},
191 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
192 {"text", GTK_DEBUG_TEXT},
193 {"tree", GTK_DEBUG_TREE},
194 {"updates", GTK_DEBUG_UPDATES},
195 {"keybindings", GTK_DEBUG_KEYBINDINGS},
196 {"multihead", GTK_DEBUG_MULTIHEAD},
197 {"modules", GTK_DEBUG_MODULES},
198 {"geometry", GTK_DEBUG_GEOMETRY},
199 {"icontheme", GTK_DEBUG_ICONTHEME},
200 {"printing", GTK_DEBUG_PRINTING},
201 {"builder", GTK_DEBUG_BUILDER}
203 #endif /* G_ENABLE_DEBUG */
207 * @required_major: the required major version.
208 * @required_minor: the required minor version.
209 * @required_micro: the required micro version.
211 * Checks that the GTK+ library in use is compatible with the
212 * given version. Generally you would pass in the constants
213 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
214 * as the three arguments to this function; that produces
215 * a check that the library in use is compatible with
216 * the version of GTK+ the application or module was compiled
219 * Compatibility is defined by two things: first the version
220 * of the running library is newer than the version
221 * @required_major.required_minor.@required_micro. Second
222 * the running library must be binary compatible with the
223 * version @required_major.required_minor.@required_micro
224 * (same major version.)
226 * This function is primarily for GTK+ modules; the module
227 * can call this function to check that it wasn't loaded
228 * into an incompatible version of GTK+. However, such a
229 * a check isn't completely reliable, since the module may be
230 * linked against an old version of GTK+ and calling the
231 * old version of gtk_check_version(), but still get loaded
232 * into an application using a newer version of GTK+.
234 * Return value: %NULL if the GTK+ library is compatible with the
235 * given version, or a string describing the version mismatch.
236 * The returned string is owned by GTK+ and should not be modified
240 gtk_check_version (guint required_major,
241 guint required_minor,
242 guint required_micro)
244 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
245 gint required_effective_micro = 100 * required_minor + required_micro;
247 if (required_major > GTK_MAJOR_VERSION)
248 return "Gtk+ version too old (major mismatch)";
249 if (required_major < GTK_MAJOR_VERSION)
250 return "Gtk+ version too new (major mismatch)";
251 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
252 return "Gtk+ version too new (micro mismatch)";
253 if (required_effective_micro > gtk_effective_micro)
254 return "Gtk+ version too old (micro mismatch)";
258 /* This checks to see if the process is running suid or sgid
259 * at the current time. If so, we don't allow GTK+ to be initialized.
260 * This is meant to be a mild check - we only error out if we
261 * can prove the programmer is doing something wrong, not if
262 * they could be doing something wrong. For this reason, we
263 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
268 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
270 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
271 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
273 #ifdef HAVE_GETRESUID
274 /* These aren't in the header files, so we prototype them here.
276 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
277 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
279 if (getresuid (&ruid, &euid, &suid) != 0 ||
280 getresgid (&rgid, &egid, &sgid) != 0)
281 #endif /* HAVE_GETRESUID */
283 suid = ruid = getuid ();
284 sgid = rgid = getgid ();
289 if (ruid != euid || ruid != suid ||
290 rgid != egid || rgid != sgid)
292 g_warning ("This process is currently running setuid or setgid.\n"
293 "This is not a supported use of GTK+. You must create a helper\n"
294 "program instead. For further details, see:\n\n"
295 " http://www.gtk.org/setuid.html\n\n"
296 "Refusing to initialize GTK+.");
306 _gtk_get_datadir (void)
308 static char *gtk_datadir = NULL;
309 if (gtk_datadir == NULL)
310 gtk_datadir = g_win32_get_package_installation_subdirectory
311 (GETTEXT_PACKAGE, dll_name, "share");
317 _gtk_get_libdir (void)
319 static char *gtk_libdir = NULL;
320 if (gtk_libdir == NULL)
321 gtk_libdir = g_win32_get_package_installation_subdirectory
322 (GETTEXT_PACKAGE, dll_name, "lib");
328 _gtk_get_sysconfdir (void)
330 static char *gtk_sysconfdir = NULL;
331 if (gtk_sysconfdir == NULL)
332 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
333 (GETTEXT_PACKAGE, dll_name, "etc");
335 return gtk_sysconfdir;
339 _gtk_get_data_prefix (void)
341 static char *gtk_data_prefix = NULL;
342 if (gtk_data_prefix == NULL)
343 gtk_data_prefix = g_win32_get_package_installation_directory
344 (GETTEXT_PACKAGE, dll_name);
346 return gtk_data_prefix;
349 #endif /* G_OS_WIN32 */
351 static gboolean do_setlocale = TRUE;
354 * gtk_disable_setlocale:
356 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
357 * gtk_parse_args() from automatically
358 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
359 * want to use this function if you wanted to set the locale for
360 * your program to something other than the user's locale, or if
361 * you wanted to set different values for different locale categories.
363 * Most programs should not need to call this function.
366 gtk_disable_setlocale (void)
369 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
371 do_setlocale = FALSE;
374 #ifdef G_PLATFORM_WIN32
375 #undef gtk_init_check
378 static GString *gtk_modules_string = NULL;
379 static gboolean g_fatal_warnings = FALSE;
381 #ifdef G_ENABLE_DEBUG
383 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
385 gtk_debug_flags |= g_parse_debug_string (value,
387 G_N_ELEMENTS (gtk_debug_keys));
393 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
395 gtk_debug_flags &= ~g_parse_debug_string (value,
397 G_N_ELEMENTS (gtk_debug_keys));
401 #endif /* G_ENABLE_DEBUG */
404 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
408 if (gtk_modules_string)
409 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
411 gtk_modules_string = g_string_new (NULL);
413 g_string_append (gtk_modules_string, value);
419 static const GOptionEntry gtk_args[] = {
420 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
421 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
422 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
423 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
424 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
425 #ifdef G_ENABLE_DEBUG
426 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
427 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
428 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
429 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
430 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
431 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
438 static char *iso639_to_check = NULL;
439 static char *iso3166_to_check = NULL;
440 static char *script_to_check = NULL;
441 static gboolean setlocale_called = FALSE;
444 enum_locale_proc (LPTSTR locale)
452 lcid = strtoul (locale, &endptr, 16);
453 if (*endptr == '\0' &&
454 GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
455 GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
457 if (strcmp (iso639, iso639_to_check) == 0 &&
458 ((iso3166_to_check != NULL &&
459 strcmp (iso3166, iso3166_to_check) == 0) ||
460 (iso3166_to_check == NULL &&
461 SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
463 char language[100], country[100];
466 if (script_to_check != NULL)
468 /* If lcid is the "other" script for this language,
469 * return TRUE, i.e. continue looking.
471 if (strcmp (script_to_check, "Latn") == 0)
473 switch (LANGIDFROMLCID (lcid))
475 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
477 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
479 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
481 case MAKELANGID (LANG_SERBIAN, 0x07):
482 /* Serbian in Bosnia and Herzegovina, Cyrillic */
486 else if (strcmp (script_to_check, "Cyrl") == 0)
488 switch (LANGIDFROMLCID (lcid))
490 case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
492 case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
494 case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
496 case MAKELANGID (LANG_SERBIAN, 0x06):
497 /* Serbian in Bosnia and Herzegovina, Latin */
503 SetThreadLocale (lcid);
505 if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
506 GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
508 strcpy (locale, language);
509 strcat (locale, "_");
510 strcat (locale, country);
512 if (setlocale (LC_ALL, locale) != NULL)
513 setlocale_called = TRUE;
526 setlocale_initialization (void)
528 static gboolean initialized = FALSE;
537 /* If some of the POSIXish environment variables are set, set
538 * the Win32 thread locale correspondingly.
540 char *p = getenv ("LC_ALL");
547 if (strcmp (p, "C") == 0)
548 SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
551 /* Check if one of the supported locales match the
552 * environment variable. If so, use that locale.
555 iso3166_to_check = strchr (iso639_to_check, '_');
556 if (iso3166_to_check != NULL)
558 *iso3166_to_check++ = '\0';
560 script_to_check = strchr (iso3166_to_check, '@');
561 if (script_to_check != NULL)
562 *script_to_check++ = '\0';
564 /* Handle special cases. */
566 /* The standard code for Serbia and Montenegro was
567 * "CS", but MSFT uses for some reason "SP". By now
568 * (October 2006), SP has split into two, "RS" and
569 * "ME", but don't bother trying to handle those
570 * yet. Do handle the even older "YU", though.
572 if (strcmp (iso3166_to_check, "CS") == 0 ||
573 strcmp (iso3166_to_check, "YU") == 0)
574 iso3166_to_check = "SP";
578 script_to_check = strchr (iso639_to_check, '@');
579 if (script_to_check != NULL)
580 *script_to_check++ = '\0';
581 /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
582 if (strcmp (iso639_to_check, "sr") == 0)
583 iso3166_to_check = "SP";
586 EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
590 if (!setlocale_called)
591 setlocale (LC_ALL, "");
593 if (!setlocale (LC_ALL, ""))
594 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
600 do_pre_parse_initialization (int *argc,
603 const gchar *env_string;
606 g_set_error_handler (gtk_error);
607 g_set_warning_handler (gtk_warning);
608 g_set_message_handler (gtk_message);
609 g_set_print_handler (gtk_print);
615 pre_initialized = TRUE;
617 gdk_pre_parse_libgtk_only ();
618 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
620 #ifdef G_ENABLE_DEBUG
621 env_string = g_getenv ("GTK_DEBUG");
622 if (env_string != NULL)
624 gtk_debug_flags = g_parse_debug_string (env_string,
626 G_N_ELEMENTS (gtk_debug_keys));
629 #endif /* G_ENABLE_DEBUG */
631 env_string = g_getenv ("GTK_MODULES");
633 gtk_modules_string = g_string_new (env_string);
637 gettext_initialization (void)
639 setlocale_initialization ();
642 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
643 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
644 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
645 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
646 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
652 do_post_parse_initialization (int *argc,
658 gettext_initialization ();
660 if (g_fatal_warnings)
662 GLogLevelFlags fatal_mask;
664 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
665 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
666 g_log_set_always_fatal (fatal_mask);
669 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
670 gdk_window_set_debug_updates (TRUE);
673 /* Translate to default:RTL if you want your widgets
674 * to be RTL, otherwise translate to default:LTR.
675 * Do *not* translate it to "predefinito:LTR", if it
676 * it isn't default:LTR or default:RTL it will not work
678 char *e = _("default:LTR");
679 if (strcmp (e, "default:RTL")==0)
680 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
681 else if (strcmp (e, "default:LTR"))
682 g_warning ("Whoever translated default:LTR did so wrongly.\n");
685 /* do what the call to gtk_type_init() used to do */
687 gtk_object_get_type ();
689 _gtk_accel_map_init ();
692 /* Set the 'initialized' flag.
694 gtk_initialized = TRUE;
696 /* load gtk modules */
697 if (gtk_modules_string)
699 _gtk_modules_init (argc, argv, gtk_modules_string->str);
700 g_string_free (gtk_modules_string, TRUE);
707 gboolean open_default_display;
711 pre_parse_hook (GOptionContext *context,
716 do_pre_parse_initialization (NULL, NULL);
722 post_parse_hook (GOptionContext *context,
727 OptionGroupInfo *info = data;
730 do_post_parse_initialization (NULL, NULL);
732 if (info->open_default_display)
734 if (gdk_display_open_default_libgtk_only () == NULL)
736 const char *display_name = gdk_get_display_arg_name ();
739 G_OPTION_ERROR_FAILED,
740 _("Cannot open display: %s"),
741 display_name ? display_name : "" );
752 * gtk_get_option_group:
753 * @open_default_display: whether to open the default display
754 * when parsing the commandline arguments
756 * Returns a #GOptionGroup for the commandline arguments recognized
757 * by GTK+ and GDK. You should add this group to your #GOptionContext
758 * with g_option_context_add_group(), if you are using
759 * g_option_context_parse() to parse your commandline arguments.
761 * Returns: a #GOptionGroup for the commandline arguments recognized
767 gtk_get_option_group (gboolean open_default_display)
770 OptionGroupInfo *info;
772 gettext_initialization ();
774 info = g_new0 (OptionGroupInfo, 1);
775 info->open_default_display = open_default_display;
777 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
778 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
780 gdk_add_option_entries_libgtk_only (group);
781 g_option_group_add_entries (group, gtk_args);
782 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
788 * gtk_init_with_args:
789 * @argc: a pointer to the number of command line arguments.
790 * @argv: a pointer to the array of command line arguments.
791 * @parameter_string: a string which is displayed in
792 * the first line of <option>--help</option> output, after
793 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
794 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
795 * describing the options of your program
796 * @translation_domain: a translation domain to use for translating
797 * the <option>--help</option> output for the options in @entries
798 * with gettext(), or %NULL
799 * @error: a return location for errors
801 * This function does the same work as gtk_init_check().
802 * Additionally, it allows you to add your own commandline options,
803 * and it automatically generates nicely formatted
804 * <option>--help</option> output. Note that your program will
805 * be terminated after writing out the help output.
807 * Returns: %TRUE if the GUI has been successfully initialized,
813 gtk_init_with_args (int *argc,
815 char *parameter_string,
816 GOptionEntry *entries,
817 char *translation_domain,
820 GOptionContext *context;
821 GOptionGroup *gtk_group;
825 return gdk_display_open_default_libgtk_only () != NULL;
827 gettext_initialization ();
829 if (!check_setugid ())
832 gtk_group = gtk_get_option_group (TRUE);
834 context = g_option_context_new (parameter_string);
835 g_option_context_add_group (context, gtk_group);
838 g_option_context_add_main_entries (context, entries, translation_domain);
839 retval = g_option_context_parse (context, argc, argv, error);
841 g_option_context_free (context);
849 * @argc: a pointer to the number of command line arguments.
850 * @argv: a pointer to the array of command line arguments.
852 * Parses command line arguments, and initializes global
853 * attributes of GTK+, but does not actually open a connection
854 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
856 * Any arguments used by GTK+ or GDK are removed from the array and
857 * @argc and @argv are updated accordingly.
859 * You shouldn't call this function explicitely if you are using
860 * gtk_init(), or gtk_init_check().
862 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
865 gtk_parse_args (int *argc,
868 GOptionContext *option_context;
869 GOptionGroup *gtk_group;
870 GError *error = NULL;
875 gettext_initialization ();
877 if (!check_setugid ())
880 option_context = g_option_context_new (NULL);
881 g_option_context_set_ignore_unknown_options (option_context, TRUE);
882 g_option_context_set_help_enabled (option_context, FALSE);
883 gtk_group = gtk_get_option_group (FALSE);
884 g_option_context_set_main_group (option_context, gtk_group);
885 if (!g_option_context_parse (option_context, argc, argv, &error))
887 g_warning ("%s", error->message);
888 g_error_free (error);
891 g_option_context_free (option_context);
896 #ifdef G_PLATFORM_WIN32
897 #undef gtk_init_check
902 * @argc: Address of the <parameter>argc</parameter> parameter of your
903 * main() function. Changed if any arguments were handled.
904 * @argv: Address of the <parameter>argv</parameter> parameter of main().
905 * Any parameters understood by gtk_init() are stripped before return.
907 * This function does the same work as gtk_init() with only
908 * a single change: It does not terminate the program if the GUI can't be
909 * initialized. Instead it returns %FALSE on failure.
911 * This way the application can fall back to some other means of communication
912 * with the user - for example a curses or command line interface.
914 * Return value: %TRUE if the GUI has been successfully initialized,
918 gtk_init_check (int *argc,
921 if (!gtk_parse_args (argc, argv))
924 return gdk_display_open_default_libgtk_only () != NULL;
927 #ifdef G_PLATFORM_WIN32
933 * @argc: Address of the <parameter>argc</parameter> parameter of your
934 * main() function. Changed if any arguments were handled.
935 * @argv: Address of the <parameter>argv</parameter> parameter of main().
936 * Any parameters understood by gtk_init() are stripped before return.
938 * Call this function before using any other GTK+ functions in your GUI
939 * applications. It will initialize everything needed to operate the
940 * toolkit and parses some standard command line options. @argc and
941 * @argv are adjusted accordingly so your own code will
942 * never see those standard arguments.
944 * Note that there are some alternative ways to initialize GTK+:
945 * if you are calling gtk_parse_args(), gtk_init_check(),
946 * gtk_init_with_args() or g_option_context_parse() with
947 * the option group returned by gtk_get_option_group(), you
948 * <emphasis>don't</emphasis> have to call gtk_init().
951 * This function will terminate your program if it was unable to initialize
952 * the GUI for some reason. If you want your program to fall back to a
953 * textual interface you want to call gtk_init_check() instead.
957 gtk_init (int *argc, char ***argv)
959 if (!gtk_init_check (argc, argv))
961 const char *display_name_arg = gdk_get_display_arg_name ();
962 if (display_name_arg == NULL)
963 display_name_arg = getenv("DISPLAY");
964 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
969 #ifdef G_PLATFORM_WIN32
972 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
974 if (sizeof_GtkWindow != sizeof (GtkWindow))
975 g_error ("Incompatible build!\n"
976 "The code using GTK+ thinks GtkWindow is of different\n"
977 "size than it actually is in this build of GTK+.\n"
978 "On Windows, this probably means that you have compiled\n"
979 "your code with gcc without the -mms-bitfields switch,\n"
980 "or that you are using an unsupported compiler.");
983 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
984 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
985 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
986 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
987 * -fnative-struct still matters. GtkBox is one such.
990 check_sizeof_GtkBox (size_t sizeof_GtkBox)
992 if (sizeof_GtkBox != sizeof (GtkBox))
993 g_error ("Incompatible build!\n"
994 "The code using GTK+ thinks GtkBox is of different\n"
995 "size than it actually is in this build of GTK+.\n"
996 "On Windows, this probably means that you have compiled\n"
997 "your code with gcc without the -mms-bitfields switch,\n"
998 "or that you are using an unsupported compiler.");
1001 /* These two functions might get more checks added later, thus pass
1002 * in the number of extra args.
1005 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1007 check_sizeof_GtkWindow (sizeof_GtkWindow);
1008 if (num_checks >= 2)
1009 check_sizeof_GtkBox (sizeof_GtkBox);
1010 gtk_init (argc, argv);
1014 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1016 check_sizeof_GtkWindow (sizeof_GtkWindow);
1017 if (num_checks >= 2)
1018 check_sizeof_GtkBox (sizeof_GtkBox);
1019 return gtk_init_check (argc, argv);
1025 gtk_exit (gint errorcode)
1034 * Initializes internationalization support for GTK+. gtk_init()
1035 * automatically does this, so there is typically no point
1036 * in calling this function.
1038 * If you are calling this function because you changed the locale
1039 * after GTK+ is was initialized, then calling this function
1040 * may help a bit. (Note, however, that changing the locale
1041 * after GTK+ is initialized may produce inconsistent results and
1042 * is not really supported.)
1044 * In detail - sets the current locale according to the
1045 * program environment. This is the same as calling the C library function
1046 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
1047 * locale specific setup of the windowing system used by GDK.
1049 * Returns: a string corresponding to the locale set, typically in the
1050 * form lang_COUNTRY, where lang is an ISO-639 language code, and
1051 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
1052 * result of the setlocale(); it is also used on other machines, such as
1053 * Windows, where the C library returns a different result. The string is
1054 * owned by GTK+ and should not be modified or freed.
1057 gtk_set_locale (void)
1059 return gdk_set_locale ();
1063 * _gtk_get_lc_ctype:
1065 * Return the Unix-style locale string for the language currently in
1066 * effect. On Unix systems, this is the return value from
1067 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
1068 * affect this through the environment variables LC_ALL, LC_CTYPE or
1069 * LANG (checked in that order). The locale strings typically is in
1070 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
1071 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
1072 * Swedish as written in Finland or pt_BR for Portuguese as written in
1075 * On Windows, the C library doesn't use any such environment
1076 * variables, and setting them won't affect the behaviour of functions
1077 * like ctime(). The user sets the locale through the Regional Options
1078 * in the Control Panel. The C library (in the setlocale() function)
1079 * does not use country and language codes, but country and language
1080 * names spelled out in English.
1081 * However, this function does check the above environment
1082 * variables, and does return a Unix-style locale string based on
1083 * either said environment variables or the thread's current locale.
1085 * Return value: a dynamically allocated string, free with g_free().
1089 _gtk_get_lc_ctype (void)
1092 /* Somebody might try to set the locale for this process using the
1093 * LANG or LC_ environment variables. The Microsoft C library
1094 * doesn't know anything about them. You set the locale in the
1095 * Control Panel. Setting these env vars won't have any affect on
1096 * locale-dependent C library functions like ctime(). But just for
1097 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1098 * it easier to test GTK and Pango in various default languages, you
1099 * don't have to clickety-click in the Control Panel, you can simply
1100 * start the program with LC_ALL=something on the command line.)
1104 p = getenv ("LC_ALL");
1106 return g_strdup (p);
1108 p = getenv ("LC_CTYPE");
1110 return g_strdup (p);
1112 p = getenv ("LANG");
1114 return g_strdup (p);
1116 return g_win32_getlocale ();
1118 return g_strdup (setlocale (LC_CTYPE, NULL));
1123 * gtk_get_default_language:
1125 * Returns the #PangoLanguage for the default language currently in
1126 * effect. (Note that this can change over the life of an
1127 * application.) The default language is derived from the current
1128 * locale. It determines, for example, whether GTK+ uses the
1129 * right-to-left or left-to-right text direction.
1131 * This function is equivalent to pango_language_get_default(). See
1132 * that function for details.
1134 * Return value: the default language as a #PangoLanguage, must not be
1138 gtk_get_default_language (void)
1140 return pango_language_get_default ();
1148 GtkInitFunction *init;
1151 gtk_main_loop_level++;
1153 loop = g_main_loop_new (NULL, TRUE);
1154 main_loops = g_slist_prepend (main_loops, loop);
1156 tmp_list = functions = init_functions;
1157 init_functions = NULL;
1161 init = tmp_list->data;
1162 tmp_list = tmp_list->next;
1164 (* init->function) (init->data);
1167 g_list_free (functions);
1169 if (g_main_loop_is_running (main_loops->data))
1171 GDK_THREADS_LEAVE ();
1172 g_main_loop_run (loop);
1173 GDK_THREADS_ENTER ();
1179 GList *reinvoke_list = NULL;
1180 GtkQuitFunction *quitf;
1182 while (quit_functions)
1184 quitf = quit_functions->data;
1186 tmp_list = quit_functions;
1187 quit_functions = g_list_remove_link (quit_functions, quit_functions);
1188 g_list_free_1 (tmp_list);
1190 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1191 gtk_quit_invoke_function (quitf))
1193 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1197 gtk_quit_destroy (quitf);
1204 work = g_list_last (reinvoke_list);
1206 quit_functions->prev = work;
1207 work->next = quit_functions;
1208 quit_functions = work;
1214 main_loops = g_slist_remove (main_loops, loop);
1216 g_main_loop_unref (loop);
1218 gtk_main_loop_level--;
1220 if (gtk_main_loop_level == 0)
1222 /* Try storing all clipboard data we have */
1223 _gtk_clipboard_store_all ();
1225 /* Synchronize the recent manager singleton */
1226 _gtk_recent_manager_sync ();
1231 gtk_main_level (void)
1233 return gtk_main_loop_level;
1237 gtk_main_quit (void)
1239 g_return_if_fail (main_loops != NULL);
1241 g_main_loop_quit (main_loops->data);
1245 gtk_events_pending (void)
1249 GDK_THREADS_LEAVE ();
1250 result = g_main_context_pending (NULL);
1251 GDK_THREADS_ENTER ();
1257 gtk_main_iteration (void)
1259 GDK_THREADS_LEAVE ();
1260 g_main_context_iteration (NULL, TRUE);
1261 GDK_THREADS_ENTER ();
1264 return !g_main_loop_is_running (main_loops->data);
1270 gtk_main_iteration_do (gboolean blocking)
1272 GDK_THREADS_LEAVE ();
1273 g_main_context_iteration (NULL, blocking);
1274 GDK_THREADS_ENTER ();
1277 return !g_main_loop_is_running (main_loops->data);
1282 /* private libgtk to libgdk interfaces
1284 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1285 GdkWindow **grab_window,
1286 gboolean *owner_events);
1287 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1288 GdkWindow **grab_window,
1289 gboolean *owner_events);
1292 rewrite_events_translate (GdkWindow *old_window,
1293 GdkWindow *new_window,
1297 gint old_origin_x, old_origin_y;
1298 gint new_origin_x, new_origin_y;
1300 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1301 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1303 *x += old_origin_x - new_origin_x;
1304 *y += old_origin_y - new_origin_y;
1308 rewrite_event_for_window (GdkEvent *event,
1309 GdkWindow *new_window)
1311 event = gdk_event_copy (event);
1313 switch (event->type)
1316 rewrite_events_translate (event->any.window,
1318 &event->scroll.x, &event->scroll.y);
1320 case GDK_BUTTON_PRESS:
1321 case GDK_2BUTTON_PRESS:
1322 case GDK_3BUTTON_PRESS:
1323 case GDK_BUTTON_RELEASE:
1324 rewrite_events_translate (event->any.window,
1326 &event->button.x, &event->button.y);
1328 case GDK_MOTION_NOTIFY:
1329 rewrite_events_translate (event->any.window,
1331 &event->motion.x, &event->motion.y);
1334 case GDK_KEY_RELEASE:
1335 case GDK_PROXIMITY_IN:
1336 case GDK_PROXIMITY_OUT:
1343 g_object_unref (event->any.window);
1344 event->any.window = g_object_ref (new_window);
1349 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1350 * then what X11 does is deliver the event normally if it was going to this
1351 * client, otherwise, delivers it in terms of the grab window. This function
1352 * rewrites events to the effect that events going to the same window group
1353 * are delivered normally, otherwise, the event is delivered in terms of the
1357 rewrite_event_for_grabs (GdkEvent *event)
1359 GdkWindow *grab_window;
1360 GtkWidget *event_widget, *grab_widget;
1361 gpointer grab_widget_ptr;
1362 gboolean owner_events;
1363 GdkDisplay *display;
1365 switch (event->type)
1368 case GDK_BUTTON_PRESS:
1369 case GDK_2BUTTON_PRESS:
1370 case GDK_3BUTTON_PRESS:
1371 case GDK_BUTTON_RELEASE:
1372 case GDK_MOTION_NOTIFY:
1373 case GDK_PROXIMITY_IN:
1374 case GDK_PROXIMITY_OUT:
1375 display = gdk_drawable_get_display (event->proximity.window);
1376 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1382 case GDK_KEY_RELEASE:
1383 display = gdk_drawable_get_display (event->key.window);
1384 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1393 event_widget = gtk_get_event_widget (event);
1394 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1395 grab_widget = grab_widget_ptr;
1398 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1399 return rewrite_event_for_window (event, grab_window);
1405 gtk_main_do_event (GdkEvent *event)
1407 GtkWidget *event_widget;
1408 GtkWidget *grab_widget;
1409 GtkWindowGroup *window_group;
1410 GdkEvent *rewritten_event = NULL;
1413 if (event->type == GDK_SETTING)
1415 _gtk_settings_handle_event (&event->setting);
1419 if (event->type == GDK_OWNER_CHANGE)
1421 _gtk_clipboard_handle_event (&event->owner_change);
1425 /* Find the widget which got the event. We store the widget
1426 * in the user_data field of GdkWindow's.
1427 * Ignore the event if we don't have a widget for it, except
1428 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1429 * Though this happens rarely, bogus events can occour
1430 * for e.g. destroyed GdkWindows.
1432 event_widget = gtk_get_event_widget (event);
1435 /* To handle selection INCR transactions, we select
1436 * PropertyNotify events on the requestor window and create
1437 * a corresponding (fake) GdkWindow so that events get
1438 * here. There won't be a widget though, so we have to handle
1441 if (event->type == GDK_PROPERTY_NOTIFY)
1442 _gtk_selection_incr_event (event->any.window,
1448 /* If pointer or keyboard grabs are in effect, munge the events
1449 * so that each window group looks like a separate app.
1451 rewritten_event = rewrite_event_for_grabs (event);
1452 if (rewritten_event)
1454 event = rewritten_event;
1455 event_widget = gtk_get_event_widget (event);
1458 window_group = gtk_main_get_window_group (event_widget);
1460 /* Push the event onto a stack of current events for
1461 * gtk_current_event_get().
1463 current_events = g_list_prepend (current_events, event);
1465 /* If there is a grab in effect...
1467 if (window_group->grabs)
1469 grab_widget = window_group->grabs->data;
1471 /* If the grab widget is an ancestor of the event widget
1472 * then we send the event to the original event widget.
1473 * This is the key to implementing modality.
1475 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1476 gtk_widget_is_ancestor (event_widget, grab_widget))
1477 grab_widget = event_widget;
1481 grab_widget = event_widget;
1484 /* Not all events get sent to the grabbing widget.
1485 * The delete, destroy, expose, focus change and resize
1486 * events still get sent to the event widget because
1487 * 1) these events have no meaning for the grabbing widget
1488 * and 2) redirecting these events to the grabbing widget
1489 * could cause the display to be messed up.
1491 * Drag events are also not redirected, since it isn't
1492 * clear what the semantics of that would be.
1494 switch (event->type)
1500 g_object_ref (event_widget);
1501 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1502 !gtk_widget_event (event_widget, event))
1503 gtk_widget_destroy (event_widget);
1504 g_object_unref (event_widget);
1508 /* Unexpected GDK_DESTROY from the outside, ignore for
1509 * child windows, handle like a GDK_DELETE for toplevels
1511 if (!event_widget->parent)
1513 g_object_ref (event_widget);
1514 if (!gtk_widget_event (event_widget, event) &&
1515 GTK_WIDGET_REALIZED (event_widget))
1516 gtk_widget_destroy (event_widget);
1517 g_object_unref (event_widget);
1522 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1524 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1525 gtk_widget_send_expose (event_widget, event);
1526 gdk_window_end_paint (event->any.window);
1529 gtk_widget_send_expose (event_widget, event);
1532 case GDK_PROPERTY_NOTIFY:
1534 case GDK_FOCUS_CHANGE:
1538 case GDK_SELECTION_CLEAR:
1539 case GDK_SELECTION_REQUEST:
1540 case GDK_SELECTION_NOTIFY:
1541 case GDK_CLIENT_EVENT:
1542 case GDK_VISIBILITY_NOTIFY:
1543 case GDK_WINDOW_STATE:
1544 case GDK_GRAB_BROKEN:
1546 gtk_widget_event (event_widget, event);
1550 case GDK_BUTTON_PRESS:
1551 case GDK_2BUTTON_PRESS:
1552 case GDK_3BUTTON_PRESS:
1553 gtk_propagate_event (grab_widget, event);
1557 case GDK_KEY_RELEASE:
1560 if (gtk_invoke_key_snoopers (grab_widget, event))
1563 /* else fall through */
1564 case GDK_MOTION_NOTIFY:
1565 case GDK_BUTTON_RELEASE:
1566 case GDK_PROXIMITY_IN:
1567 case GDK_PROXIMITY_OUT:
1568 gtk_propagate_event (grab_widget, event);
1571 case GDK_ENTER_NOTIFY:
1572 GTK_PRIVATE_SET_FLAG (event_widget, GTK_HAS_POINTER);
1573 _gtk_widget_set_pointer_window (event_widget, event->any.window);
1574 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1575 gtk_widget_event (grab_widget, event);
1578 case GDK_LEAVE_NOTIFY:
1579 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_HAS_POINTER);
1580 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1581 gtk_widget_event (grab_widget, event);
1584 case GDK_DRAG_STATUS:
1585 case GDK_DROP_FINISHED:
1586 _gtk_drag_source_handle_event (event_widget, event);
1588 case GDK_DRAG_ENTER:
1589 case GDK_DRAG_LEAVE:
1590 case GDK_DRAG_MOTION:
1591 case GDK_DROP_START:
1592 _gtk_drag_dest_handle_event (event_widget, event);
1595 g_assert_not_reached ();
1599 if (event->type == GDK_ENTER_NOTIFY
1600 || event->type == GDK_LEAVE_NOTIFY
1601 || event->type == GDK_BUTTON_PRESS
1602 || event->type == GDK_2BUTTON_PRESS
1603 || event->type == GDK_3BUTTON_PRESS
1604 || event->type == GDK_KEY_PRESS
1605 || event->type == GDK_DRAG_ENTER
1606 || event->type == GDK_GRAB_BROKEN
1607 || event->type == GDK_MOTION_NOTIFY
1608 || event->type == GDK_SCROLL)
1610 _gtk_tooltip_handle_event (event);
1613 tmp_list = current_events;
1614 current_events = g_list_remove_link (current_events, tmp_list);
1615 g_list_free_1 (tmp_list);
1617 if (rewritten_event)
1618 gdk_event_free (rewritten_event);
1633 static GtkWindowGroup *
1634 gtk_main_get_window_group (GtkWidget *widget)
1636 GtkWidget *toplevel = NULL;
1639 toplevel = gtk_widget_get_toplevel (widget);
1641 if (GTK_IS_WINDOW (toplevel))
1642 return gtk_window_get_group (GTK_WINDOW (toplevel));
1644 return gtk_window_get_group (NULL);
1649 GtkWidget *old_grab_widget;
1650 GtkWidget *new_grab_widget;
1651 gboolean was_grabbed;
1652 gboolean is_grabbed;
1657 gtk_grab_notify_foreach (GtkWidget *child,
1661 GrabNotifyInfo *info = data;
1663 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
1665 was_grabbed = info->was_grabbed;
1666 is_grabbed = info->is_grabbed;
1668 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
1669 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
1671 was_shadowed = info->old_grab_widget && !info->was_grabbed;
1672 is_shadowed = info->new_grab_widget && !info->is_grabbed;
1674 g_object_ref (child);
1676 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
1677 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1681 GTK_PRIVATE_SET_FLAG (child, GTK_SHADOWED);
1682 if (!was_shadowed && GTK_WIDGET_HAS_POINTER (child)
1683 && GTK_WIDGET_IS_SENSITIVE (child))
1684 _gtk_widget_synthesize_crossing (child, info->new_grab_widget,
1685 GDK_CROSSING_GTK_GRAB);
1689 GTK_PRIVATE_UNSET_FLAG (child, GTK_SHADOWED);
1690 if (was_shadowed && GTK_WIDGET_HAS_POINTER (child)
1691 && GTK_WIDGET_IS_SENSITIVE (child))
1692 _gtk_widget_synthesize_crossing (info->old_grab_widget, child,
1693 info->from_grab ? GDK_CROSSING_GTK_GRAB
1694 : GDK_CROSSING_GTK_UNGRAB);
1697 if (was_shadowed != is_shadowed)
1698 _gtk_widget_grab_notify (child, was_shadowed);
1700 g_object_unref (child);
1702 info->was_grabbed = was_grabbed;
1703 info->is_grabbed = is_grabbed;
1707 gtk_grab_notify (GtkWindowGroup *group,
1708 GtkWidget *old_grab_widget,
1709 GtkWidget *new_grab_widget,
1713 GrabNotifyInfo info;
1715 if (old_grab_widget == new_grab_widget)
1718 info.old_grab_widget = old_grab_widget;
1719 info.new_grab_widget = new_grab_widget;
1720 info.from_grab = from_grab;
1722 g_object_ref (group);
1724 toplevels = gtk_window_list_toplevels ();
1725 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1729 GtkWindow *toplevel = toplevels->data;
1730 toplevels = g_list_delete_link (toplevels, toplevels);
1732 info.was_grabbed = FALSE;
1733 info.is_grabbed = FALSE;
1735 if (group == gtk_window_get_group (toplevel))
1736 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
1737 g_object_unref (toplevel);
1740 g_object_unref (group);
1744 gtk_grab_add (GtkWidget *widget)
1746 GtkWindowGroup *group;
1747 GtkWidget *old_grab_widget;
1749 g_return_if_fail (widget != NULL);
1751 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1753 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1755 group = gtk_main_get_window_group (widget);
1758 old_grab_widget = (GtkWidget *)group->grabs->data;
1760 old_grab_widget = NULL;
1762 g_object_ref (widget);
1763 group->grabs = g_slist_prepend (group->grabs, widget);
1765 gtk_grab_notify (group, old_grab_widget, widget, TRUE);
1770 gtk_grab_get_current (void)
1772 GtkWindowGroup *group;
1774 group = gtk_main_get_window_group (NULL);
1777 return GTK_WIDGET (group->grabs->data);
1782 gtk_grab_remove (GtkWidget *widget)
1784 GtkWindowGroup *group;
1785 GtkWidget *new_grab_widget;
1787 g_return_if_fail (widget != NULL);
1789 if (GTK_WIDGET_HAS_GRAB (widget))
1791 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1793 group = gtk_main_get_window_group (widget);
1794 group->grabs = g_slist_remove (group->grabs, widget);
1797 new_grab_widget = (GtkWidget *)group->grabs->data;
1799 new_grab_widget = NULL;
1801 gtk_grab_notify (group, widget, new_grab_widget, FALSE);
1803 g_object_unref (widget);
1808 gtk_init_add (GtkFunction function,
1811 GtkInitFunction *init;
1813 init = g_new (GtkInitFunction, 1);
1814 init->function = function;
1817 init_functions = g_list_prepend (init_functions, init);
1821 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1824 GtkKeySnooperData *data;
1825 static guint snooper_id = 1;
1827 g_return_val_if_fail (snooper != NULL, 0);
1829 data = g_new (GtkKeySnooperData, 1);
1830 data->func = snooper;
1831 data->func_data = func_data;
1832 data->id = snooper_id++;
1833 key_snoopers = g_slist_prepend (key_snoopers, data);
1839 gtk_key_snooper_remove (guint snooper_id)
1841 GtkKeySnooperData *data = NULL;
1844 slist = key_snoopers;
1848 if (data->id == snooper_id)
1851 slist = slist->next;
1856 key_snoopers = g_slist_remove (key_snoopers, data);
1862 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1866 gint return_val = FALSE;
1868 slist = key_snoopers;
1869 while (slist && !return_val)
1871 GtkKeySnooperData *data;
1874 slist = slist->next;
1875 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1882 gtk_quit_add_full (guint main_level,
1883 GtkFunction function,
1884 GtkCallbackMarshal marshal,
1886 GDestroyNotify destroy)
1888 static guint quit_id = 1;
1889 GtkQuitFunction *quitf;
1891 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1893 quitf = g_slice_new (GtkQuitFunction);
1895 quitf->id = quit_id++;
1896 quitf->main_level = main_level;
1897 quitf->function = function;
1898 quitf->marshal = marshal;
1900 quitf->destroy = destroy;
1902 quit_functions = g_list_prepend (quit_functions, quitf);
1908 gtk_quit_destroy (GtkQuitFunction *quitf)
1911 quitf->destroy (quitf->data);
1912 g_slice_free (GtkQuitFunction, quitf);
1916 gtk_quit_destructor (GtkObject **object_p)
1919 gtk_object_destroy (*object_p);
1926 gtk_quit_add_destroy (guint main_level,
1929 GtkObject **object_p;
1931 g_return_if_fail (main_level > 0);
1932 g_return_if_fail (GTK_IS_OBJECT (object));
1934 object_p = g_new (GtkObject*, 1);
1936 g_signal_connect (object,
1938 G_CALLBACK (gtk_widget_destroyed),
1940 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1944 gtk_quit_add (guint main_level,
1945 GtkFunction function,
1948 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1952 gtk_quit_remove (guint id)
1954 GtkQuitFunction *quitf;
1957 tmp_list = quit_functions;
1960 quitf = tmp_list->data;
1962 if (quitf->id == id)
1964 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1965 g_list_free (tmp_list);
1966 gtk_quit_destroy (quitf);
1971 tmp_list = tmp_list->next;
1976 gtk_quit_remove_by_data (gpointer data)
1978 GtkQuitFunction *quitf;
1981 tmp_list = quit_functions;
1984 quitf = tmp_list->data;
1986 if (quitf->data == data)
1988 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1989 g_list_free (tmp_list);
1990 gtk_quit_destroy (quitf);
1995 tmp_list = tmp_list->next;
2000 gtk_timeout_add_full (guint32 interval,
2001 GtkFunction function,
2002 GtkCallbackMarshal marshal,
2004 GDestroyNotify destroy)
2008 GtkClosure *closure;
2010 closure = g_new (GtkClosure, 1);
2011 closure->marshal = marshal;
2012 closure->data = data;
2013 closure->destroy = destroy;
2015 return g_timeout_add_full (0, interval,
2016 gtk_invoke_idle_timeout,
2018 gtk_destroy_closure);
2021 return g_timeout_add_full (0, interval, function, data, destroy);
2025 gtk_timeout_add (guint32 interval,
2026 GtkFunction function,
2029 return g_timeout_add_full (0, interval, function, data, NULL);
2033 gtk_timeout_remove (guint tag)
2035 g_source_remove (tag);
2039 gtk_idle_add_full (gint priority,
2040 GtkFunction function,
2041 GtkCallbackMarshal marshal,
2043 GDestroyNotify destroy)
2047 GtkClosure *closure;
2049 closure = g_new (GtkClosure, 1);
2050 closure->marshal = marshal;
2051 closure->data = data;
2052 closure->destroy = destroy;
2054 return g_idle_add_full (priority,
2055 gtk_invoke_idle_timeout,
2057 gtk_destroy_closure);
2060 return g_idle_add_full (priority, function, data, destroy);
2064 gtk_idle_add (GtkFunction function,
2067 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2071 gtk_idle_add_priority (gint priority,
2072 GtkFunction function,
2075 return g_idle_add_full (priority, function, data, NULL);
2079 gtk_idle_remove (guint tag)
2081 g_source_remove (tag);
2085 gtk_idle_remove_by_data (gpointer data)
2087 if (!g_idle_remove_by_data (data))
2088 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
2092 gtk_input_add_full (gint source,
2093 GdkInputCondition condition,
2094 GdkInputFunction function,
2095 GtkCallbackMarshal marshal,
2097 GDestroyNotify destroy)
2101 GtkClosure *closure;
2103 closure = g_new (GtkClosure, 1);
2104 closure->marshal = marshal;
2105 closure->data = data;
2106 closure->destroy = destroy;
2108 return gdk_input_add_full (source,
2110 (GdkInputFunction) gtk_invoke_input,
2112 (GDestroyNotify) gtk_destroy_closure);
2115 return gdk_input_add_full (source, condition, function, data, destroy);
2119 gtk_input_remove (guint tag)
2121 g_source_remove (tag);
2125 gtk_destroy_closure (gpointer data)
2127 GtkClosure *closure = data;
2129 if (closure->destroy)
2130 (closure->destroy) (closure->data);
2135 gtk_invoke_idle_timeout (gpointer data)
2137 GtkClosure *closure = data;
2140 gint ret_val = FALSE;
2141 args[0].name = NULL;
2142 args[0].type = G_TYPE_BOOLEAN;
2143 args[0].d.pointer_data = &ret_val;
2144 closure->marshal (NULL, closure->data, 0, args);
2149 gtk_invoke_input (gpointer data,
2151 GdkInputCondition condition)
2153 GtkClosure *closure = data;
2156 args[0].type = G_TYPE_INT;
2157 args[0].name = NULL;
2158 GTK_VALUE_INT (args[0]) = source;
2159 args[1].type = GDK_TYPE_INPUT_CONDITION;
2160 args[1].name = NULL;
2161 GTK_VALUE_FLAGS (args[1]) = condition;
2162 args[2].type = G_TYPE_NONE;
2163 args[2].name = NULL;
2165 closure->marshal (NULL, closure->data, 2, args);
2169 * gtk_get_current_event:
2171 * Obtains a copy of the event currently being processed by GTK+. For
2172 * example, if you get a "clicked" signal from #GtkButton, the current
2173 * event will be the #GdkEventButton that triggered the "clicked"
2174 * signal. The returned event must be freed with gdk_event_free().
2175 * If there is no current event, the function returns %NULL.
2177 * Return value: a copy of the current event, or %NULL if no current event.
2180 gtk_get_current_event (void)
2183 return gdk_event_copy (current_events->data);
2189 * gtk_get_current_event_time:
2191 * If there is a current event and it has a timestamp, return that
2192 * timestamp, otherwise return %GDK_CURRENT_TIME.
2194 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
2197 gtk_get_current_event_time (void)
2200 return gdk_event_get_time (current_events->data);
2202 return GDK_CURRENT_TIME;
2206 * gtk_get_current_event_state:
2207 * @state: a location to store the state of the current event
2209 * If there is a current event and it has a state field, place
2210 * that state field in @state and return %TRUE, otherwise return
2213 * Return value: %TRUE if there was a current event and it had a state field
2216 gtk_get_current_event_state (GdkModifierType *state)
2218 g_return_val_if_fail (state != NULL, FALSE);
2221 return gdk_event_get_state (current_events->data, state);
2230 * gtk_get_event_widget:
2231 * @event: a #GdkEvent
2233 * If @event is %NULL or the event was not associated with any widget,
2234 * returns %NULL, otherwise returns the widget that received the event
2237 * Return value: the widget that originally received @event, or %NULL
2240 gtk_get_event_widget (GdkEvent *event)
2243 gpointer widget_ptr;
2246 if (event && event->any.window &&
2247 (event->type == GDK_DESTROY || !GDK_WINDOW_DESTROYED (event->any.window)))
2249 gdk_window_get_user_data (event->any.window, &widget_ptr);
2250 widget = widget_ptr;
2257 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2259 if (!quitf->marshal)
2260 return quitf->function (quitf->data);
2264 gint ret_val = FALSE;
2266 args[0].name = NULL;
2267 args[0].type = G_TYPE_BOOLEAN;
2268 args[0].d.pointer_data = &ret_val;
2269 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2277 * gtk_propagate_event:
2278 * @widget: a #GtkWidget
2281 * Sends an event to a widget, propagating the event to parent widgets
2282 * if the event remains unhandled. Events received by GTK+ from GDK
2283 * normally begin in gtk_main_do_event(). Depending on the type of
2284 * event, existence of modal dialogs, grabs, etc., the event may be
2285 * propagated; if so, this function is used. gtk_propagate_event()
2286 * calls gtk_widget_event() on each widget it decides to send the
2287 * event to. So gtk_widget_event() is the lowest-level function; it
2288 * simply emits the "event" and possibly an event-specific signal on a
2289 * widget. gtk_propagate_event() is a bit higher-level, and
2290 * gtk_main_do_event() is the highest level.
2292 * All that said, you most likely don't want to use any of these
2293 * functions; synthesizing events is rarely needed. Consider asking on
2294 * the mailing list for better ways to achieve your goals. For
2295 * example, use gdk_window_invalidate_rect() or
2296 * gtk_widget_queue_draw() instead of making up expose events.
2300 gtk_propagate_event (GtkWidget *widget,
2305 g_return_if_fail (GTK_IS_WIDGET (widget));
2306 g_return_if_fail (event != NULL);
2308 handled_event = FALSE;
2310 g_object_ref (widget);
2312 if ((event->type == GDK_KEY_PRESS) ||
2313 (event->type == GDK_KEY_RELEASE))
2315 /* Only send key events within Window widgets to the Window
2316 * The Window widget will in turn pass the
2317 * key event on to the currently focused widget
2322 window = gtk_widget_get_toplevel (widget);
2323 if (GTK_IS_WINDOW (window))
2325 /* If there is a grab within the window, give the grab widget
2326 * a first crack at the key event
2328 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2329 handled_event = gtk_widget_event (widget, event);
2333 window = gtk_widget_get_toplevel (widget);
2334 if (GTK_IS_WINDOW (window))
2336 if (GTK_WIDGET_IS_SENSITIVE (window))
2337 gtk_widget_event (window, event);
2341 handled_event = TRUE; /* don't send to widget */
2345 /* Other events get propagated up the widget tree
2346 * so that parents can see the button and motion
2347 * events of the children.
2355 /* Scroll events are special cased here because it
2356 * feels wrong when scrolling a GtkViewport, say,
2357 * to have children of the viewport eat the scroll
2360 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2361 handled_event = event->type != GDK_SCROLL;
2363 handled_event = gtk_widget_event (widget, event);
2365 tmp = widget->parent;
2366 g_object_unref (widget);
2370 if (!handled_event && widget)
2371 g_object_ref (widget);
2377 g_object_unref (widget);
2382 gtk_error (gchar *str)
2388 gtk_warning (gchar *str)
2394 gtk_message (gchar *str)
2400 gtk_print (gchar *str)
2402 static GtkWidget *window = NULL;
2403 static GtkWidget *text;
2404 static int level = 0;
2408 GtkWidget *hscrollbar;
2409 GtkWidget *vscrollbar;
2410 GtkWidget *separator;
2415 fputs (str, stdout);
2422 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2424 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2425 (GtkSignalFunc) gtk_widget_destroyed,
2428 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2430 box1 = gtk_vbox_new (FALSE, 0);
2431 gtk_container_add (GTK_CONTAINER (window), box1);
2432 gtk_widget_show (box1);
2435 box2 = gtk_vbox_new (FALSE, 10);
2436 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2437 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2438 gtk_widget_show (box2);
2441 table = gtk_table_new (2, 2, FALSE);
2442 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2443 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2444 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2445 gtk_widget_show (table);
2447 text = gtk_text_new (NULL, NULL);
2448 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2449 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2450 gtk_widget_show (text);
2451 gtk_widget_realize (text);
2453 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2454 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2455 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2456 gtk_widget_show (hscrollbar);
2458 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2459 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2460 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2461 gtk_widget_show (vscrollbar);
2463 separator = gtk_hseparator_new ();
2464 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2465 gtk_widget_show (separator);
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, FALSE, TRUE, 0);
2471 gtk_widget_show (box2);
2474 button = gtk_button_new_with_label ("close");
2475 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2476 (GtkSignalFunc) gtk_widget_hide,
2477 GTK_OBJECT (window));
2478 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2479 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2480 gtk_widget_grab_default (button);
2481 gtk_widget_show (button);
2485 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2488 if (!GTK_WIDGET_VISIBLE (window))
2489 gtk_widget_show (window);
2494 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2495 GValue *return_accu,
2496 const GValue *handler_return,
2499 gboolean continue_emission;
2500 gboolean signal_handled;
2502 signal_handled = g_value_get_boolean (handler_return);
2503 g_value_set_boolean (return_accu, signal_handled);
2504 continue_emission = !signal_handled;
2506 return continue_emission;
2509 #define __GTK_MAIN_C__
2510 #include "gtkaliasdef.c"