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"
34 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
44 #include <sys/types.h> /* For uid_t, gid_t */
52 #include <pango/pango-types.h> /* For pango_language_from_string */
56 #include "gtkaccelmap.h"
58 #include "gtkclipboard.h"
60 #include "gtkversion.h"
62 #include "gtkmodules.h"
64 #include "gtkselection.h"
65 #include "gtksettings.h"
66 #include "gtkwidget.h"
67 #include "gtkwindow.h"
68 #include "gtkprivate.h"
72 /* Private type definitions
74 typedef struct _GtkInitFunction GtkInitFunction;
75 typedef struct _GtkQuitFunction GtkQuitFunction;
76 typedef struct _GtkClosure GtkClosure;
77 typedef struct _GtkKeySnooperData GtkKeySnooperData;
79 struct _GtkInitFunction
85 struct _GtkQuitFunction
89 GtkCallbackMarshal marshal;
92 GtkDestroyNotify destroy;
97 GtkCallbackMarshal marshal;
99 GtkDestroyNotify destroy;
102 struct _GtkKeySnooperData
104 GtkKeySnoopFunc func;
109 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
110 static void gtk_quit_destroy (GtkQuitFunction *quitf);
111 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
114 static void gtk_destroy_closure (gpointer data);
115 static gboolean gtk_invoke_idle_timeout (gpointer data);
116 static void gtk_invoke_input (gpointer data,
118 GdkInputCondition condition);
121 static void gtk_error (gchar *str);
122 static void gtk_warning (gchar *str);
123 static void gtk_message (gchar *str);
124 static void gtk_print (gchar *str);
127 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
129 const guint gtk_major_version = GTK_MAJOR_VERSION;
130 const guint gtk_minor_version = GTK_MINOR_VERSION;
131 const guint gtk_micro_version = GTK_MICRO_VERSION;
132 const guint gtk_binary_age = GTK_BINARY_AGE;
133 const guint gtk_interface_age = GTK_INTERFACE_AGE;
135 static guint gtk_main_loop_level = 0;
136 static gint gtk_initialized = FALSE;
137 static GList *current_events = NULL;
139 static GSList *main_loops = NULL; /* stack of currently executing main loops */
141 static GList *init_functions = NULL; /* A list of init functions.
143 static GList *quit_functions = NULL; /* A list of quit functions.
145 static GMemChunk *quit_mem_chunk = NULL;
147 static GSList *key_snoopers = NULL;
149 guint gtk_debug_flags = 0; /* Global GTK debug flag */
151 #ifdef G_ENABLE_DEBUG
152 static const GDebugKey gtk_debug_keys[] = {
153 {"misc", GTK_DEBUG_MISC},
154 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
155 {"text", GTK_DEBUG_TEXT},
156 {"tree", GTK_DEBUG_TREE},
157 {"updates", GTK_DEBUG_UPDATES},
158 {"keybindings", GTK_DEBUG_KEYBINDINGS},
159 {"multihead", GTK_DEBUG_MULTIHEAD},
160 {"modules", GTK_DEBUG_MODULES},
161 {"geometry", GTK_DEBUG_GEOMETRY},
162 {"icontheme", GTK_DEBUG_ICONTHEME}
165 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
167 #endif /* G_ENABLE_DEBUG */
171 * @required_major: the required major version.
172 * @required_minor: the required major version.
173 * @required_micro: the required major version.
175 * Checks that the GTK+ library in use is compatible with the
176 * given version. Generally you would pass in the constants
177 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
178 * as the three arguments to this function; that produces
179 * a check that the library in use is compatible with
180 * the version of GTK+ the application or module was compiled
183 * Compatibility is defined by two things: first the version
184 * of the running library is newer than the version
185 * @required_major.required_minor.@required_micro. Second
186 * the running library must be binary compatible with the
187 * version @required_major.required_minor.@required_micro
188 * (same major version.)
190 * This function is primarily for GTK+ modules; the module
191 * can call this function to check that it wasn't loaded
192 * into an incompatible version of GTK+. However, such a
193 * a check isn't completely reliable, since the module may be
194 * linked against an old version of GTK+ and calling the
195 * old version of gtk_check_version(), but still get loaded
196 * into an application using a newer version of GTK+.
198 * Return value: %NULL if the GTK+ library is compatible with the
199 * given version, or a string describing the version mismatch.
200 * The returned string is owned by GTK+ and should not be modified
204 gtk_check_version (guint required_major,
205 guint required_minor,
206 guint required_micro)
208 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
209 gint required_effective_micro = 100 * required_minor + required_micro;
211 if (required_major > GTK_MAJOR_VERSION)
212 return "Gtk+ version too old (major mismatch)";
213 if (required_major < GTK_MAJOR_VERSION)
214 return "Gtk+ version too new (major mismatch)";
215 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
216 return "Gtk+ version too new (micro mismatch)";
217 if (required_effective_micro > gtk_effective_micro)
218 return "Gtk+ version too old (micro mismatch)";
222 /* This checks to see if the process is running suid or sgid
223 * at the current time. If so, we don't allow GTK+ to be initialized.
224 * This is meant to be a mild check - we only error out if we
225 * can prove the programmer is doing something wrong, not if
226 * they could be doing something wrong. For this reason, we
227 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
232 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
234 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
235 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
237 #ifdef HAVE_GETRESUID
238 /* These aren't in the header files, so we prototype them here.
240 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
241 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
243 if (getresuid (&ruid, &euid, &suid) != 0 ||
244 getresgid (&rgid, &egid, &sgid) != 0)
245 #endif /* HAVE_GETRESUID */
247 suid = ruid = getuid ();
248 sgid = rgid = getgid ();
253 if (ruid != euid || ruid != suid ||
254 rgid != egid || rgid != sgid)
256 g_warning ("This process is currently running setuid or setgid.\n"
257 "This is not a supported use of GTK+. You must create a helper\n"
258 "program instead. For further details, see:\n\n"
259 " http://www.gtk.org/setuid.html\n\n"
260 "Refusing to initialize GTK+.");
269 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
272 _gtk_get_datadir (void)
274 static char *gtk_datadir = NULL;
275 if (gtk_datadir == NULL)
276 gtk_datadir = g_win32_get_package_installation_subdirectory
277 (GETTEXT_PACKAGE, dll_name, "share");
283 _gtk_get_libdir (void)
285 static char *gtk_libdir = NULL;
286 if (gtk_libdir == NULL)
287 gtk_libdir = g_win32_get_package_installation_subdirectory
288 (GETTEXT_PACKAGE, dll_name, "lib");
294 _gtk_get_localedir (void)
296 static char *gtk_localedir = NULL;
297 if (gtk_localedir == NULL)
298 gtk_localedir = g_win32_get_package_installation_subdirectory
299 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
301 return gtk_localedir;
305 _gtk_get_sysconfdir (void)
307 static char *gtk_sysconfdir = NULL;
308 if (gtk_sysconfdir == NULL)
309 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
310 (GETTEXT_PACKAGE, dll_name, "etc");
312 return gtk_sysconfdir;
316 _gtk_get_data_prefix (void)
318 static char *gtk_data_prefix = NULL;
319 if (gtk_data_prefix == NULL)
320 gtk_data_prefix = g_win32_get_package_installation_directory
321 (GETTEXT_PACKAGE, dll_name);
323 return gtk_data_prefix;
326 #endif /* G_OS_WIN32 */
328 static gboolean do_setlocale = TRUE;
331 * gtk_disable_setlocale:
333 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
334 * gtk_parse_args() from automatically
335 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
336 * want to use this function if you wanted to set the locale for
337 * your program to something other than the user's locale, or if
338 * you wanted to set different values for different locale categories.
340 * Most programs should not need to call this function.
343 gtk_disable_setlocale (void)
346 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
348 do_setlocale = FALSE;
351 #ifdef G_PLATFORM_WIN32
352 #undef gtk_init_check
355 static GString *gtk_modules_string = NULL;
356 static gboolean g_fatal_warnings = FALSE;
358 #ifdef G_ENABLE_DEBUG
360 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
362 gtk_debug_flags |= g_parse_debug_string (value,
370 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
372 gtk_debug_flags &= ~g_parse_debug_string (value,
378 #endif /* G_ENABLE_DEBUG */
381 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
385 if (gtk_modules_string)
386 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
388 gtk_modules_string = g_string_new (NULL);
390 g_string_append (gtk_modules_string, value);
396 static GOptionEntry gtk_args[] = {
397 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
398 /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
399 /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
400 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
401 /* Description of --g-fatal-warnings in --help output */ N_("Make all warnings fatal"), NULL },
402 #ifdef G_ENABLE_DEBUG
403 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
404 /* Description of --gtk-debug=FLAGS in --help output */ N_("GTK+ debugging flags to set"),
405 /* Placeholder in --gtk-debug=FLAGS in --help output */ N_("FLAGS") },
406 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
407 /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
408 /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
414 do_pre_parse_initialization (int *argc,
417 const gchar *env_string;
420 g_set_error_handler (gtk_error);
421 g_set_warning_handler (gtk_warning);
422 g_set_message_handler (gtk_message);
423 g_set_print_handler (gtk_print);
428 if (!setlocale (LC_ALL, ""))
429 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
432 gdk_pre_parse_libgtk_only ();
433 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
435 #ifdef G_ENABLE_DEBUG
436 env_string = g_getenv ("GTK_DEBUG");
437 if (env_string != NULL)
439 gtk_debug_flags = g_parse_debug_string (env_string,
444 #endif /* G_ENABLE_DEBUG */
446 env_string = g_getenv ("GTK_MODULES");
448 gtk_modules_string = g_string_new (env_string);
452 do_post_parse_initialization (int *argc,
458 if (g_fatal_warnings)
460 GLogLevelFlags fatal_mask;
462 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
463 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
464 g_log_set_always_fatal (fatal_mask);
467 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
468 gdk_window_set_debug_updates (TRUE);
471 /* Translate to default:RTL if you want your widgets
472 * to be RTL, otherwise translate to default:LTR.
473 * Do *not* translate it to "predefinito:LTR", if it
474 * it isn't default:LTR or default:RTL it will not work
476 char *e = _("default:LTR");
477 if (strcmp (e, "default:RTL")==0) {
478 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
479 } else if (strcmp (e, "default:LTR")) {
480 g_warning ("Whoever translated default:LTR did so wrongly.\n");
485 _gtk_accel_map_init ();
488 /* Set the 'initialized' flag.
490 gtk_initialized = TRUE;
492 /* load gtk modules */
493 if (gtk_modules_string)
495 _gtk_modules_init (argc, argv, gtk_modules_string->str);
496 g_string_free (gtk_modules_string, TRUE);
503 gboolean open_default_display;
507 pre_parse_hook (GOptionContext *context,
512 do_pre_parse_initialization (NULL, NULL);
518 post_parse_hook (GOptionContext *context,
523 OptionGroupInfo *info = data;
526 do_post_parse_initialization (NULL, NULL);
528 if (info->open_default_display)
529 return gdk_display_open_default_libgtk_only () != NULL;
536 * gtk_get_option_group:
537 * @open_default_display: whether to open the default display
538 * when parsing the commandline arguments
540 * Returns a #GOptionGroup for the commandline arguments recognized
541 * by GTK+ and GDK. You should add this group to your #GOptionContext
542 * with g_option_context_add_group(), if you are using
543 * g_option_context_parse() to parse your commandline arguments.
545 * Returns a #GOptionGroup for the commandline arguments recognized
551 gtk_get_option_group (gboolean open_default_display)
554 OptionGroupInfo *info;
556 info = g_new0 (OptionGroupInfo, 1);
557 info->open_default_display = open_default_display;
559 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
560 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
562 gdk_add_option_entries_libgtk_only (group);
563 g_option_group_add_entries (group, gtk_args);
564 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
570 gettext_initialization (void)
573 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
574 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
575 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
576 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
577 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
583 * gtk_init_with_args:
584 * @argc: a pointer to the number of command line arguments.
585 * @argv: a pointer to the array of command line arguments.
586 * @parameter_string: a string which is displayed in
587 * the first line of <option>--help</option> output, after
588 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
589 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
590 * describing the options of your program
591 * @translation_domain: a translation domain to use for translating
592 * the <option>--help</option> output for the options in @entries
593 * with gettext(), or %NULL
594 * @error: a return location for errors
596 * This function does the same work as gtk_init_check().
597 * Additionally, it allows you to add your own commandline options,
598 * and it automatically generates nicely formatted
599 * <option>--help</option> output. Note that your program will
600 * be terminated after writing out the help output.
602 * Returns: %TRUE if the GUI has been successfully initialized,
608 gtk_init_with_args (int *argc,
610 char *parameter_string,
611 GOptionEntry *entries,
612 char *translation_domain,
615 GOptionContext *context;
616 GOptionGroup *gtk_group;
622 if (!check_setugid ())
625 gettext_initialization ();
627 gtk_group = gtk_get_option_group (TRUE);
629 context = g_option_context_new (parameter_string);
630 g_option_context_add_group (context, gtk_group);
633 g_option_context_add_main_entries (context, entries, translation_domain);
634 retval = g_option_context_parse (context, argc, argv, error);
636 g_option_context_free (context);
644 * @argc: a pointer to the number of command line arguments.
645 * @argv: a pointer to the array of command line arguments.
647 * Parses command line arguments, and initializes global
648 * attributes of GTK+, but does not actually open a connection
649 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
651 * Any arguments used by GTK+ or GDK are removed from the array and
652 * @argc and @argv are updated accordingly.
654 * You shouldn't call this function explicitely if you are using
655 * gtk_init(), or gtk_init_check().
657 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
660 gtk_parse_args (int *argc,
663 GOptionContext *option_context;
664 GOptionGroup *gtk_group;
669 if (!check_setugid ())
672 gettext_initialization ();
674 option_context = g_option_context_new (NULL);
675 g_option_context_set_ignore_unknown_options (option_context, TRUE);
676 g_option_context_set_help_enabled (option_context, FALSE);
677 gtk_group = gtk_get_option_group (FALSE);
678 g_option_context_set_main_group (option_context, gtk_group);
679 g_option_context_parse (option_context, argc, argv, NULL);
680 g_option_context_free (option_context);
685 #ifdef G_PLATFORM_WIN32
686 #undef gtk_init_check
691 * @argc: Address of the <parameter>argc</parameter> parameter of your
692 * main() function. Changed if any arguments were handled.
693 * @argv: Address of the <parameter>argv</parameter> parameter of main().
694 * Any parameters understood by gtk_init() are stripped before return.
696 * This function does the same work as gtk_init() with only
697 * a single change: It does not terminate the program if the GUI can't be
698 * initialized. Instead it returns %FALSE on failure.
700 * This way the application can fall back to some other means of communication
701 * with the user - for example a curses or command line interface.
703 * Return value: %TRUE if the GUI has been successfully initialized,
707 gtk_init_check (int *argc,
710 if (!gtk_parse_args (argc, argv))
713 return gdk_display_open_default_libgtk_only () != NULL;
716 #ifdef G_PLATFORM_WIN32
722 * @argc: Address of the <parameter>argc</parameter> parameter of your
723 * main() function. Changed if any arguments were handled.
724 * @argv: Address of the <parameter>argv</parameter> parameter of main().
725 * Any parameters understood by gtk_init() are stripped before return.
727 * Call this function before using any other GTK+ functions in your GUI
728 * applications. It will initialize everything needed to operate the
729 * toolkit and parses some standard command line options. @argc and
730 * @argv are adjusted accordingly so your own code will
731 * never see those standard arguments.
733 * Note that there are some alternative ways to initialize GTK+:
734 * if you are calling gtk_parse_args(), gtk_init_check(),
735 * gtk_init_with_args() or g_option_context_parse() with
736 * the option group returned by gtk_get_option_group(), you
737 * <emphasis>don't</emphasis> have to call gtk_init().
740 * This function will terminate your program if it was unable to initialize
741 * the GUI for some reason. If you want your program to fall back to a
742 * textual interface you want to call gtk_init_check() instead.
746 gtk_init (int *argc, char ***argv)
748 if (!gtk_init_check (argc, argv))
750 const char *display_name_arg = gdk_get_display_arg_name ();
751 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
756 #ifdef G_PLATFORM_WIN32
759 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
761 if (sizeof_GtkWindow != sizeof (GtkWindow))
762 g_error ("Incompatible build!\n"
763 "The code using GTK+ thinks GtkWindow is of different\n"
764 "size than it actually is in this build of GTK+.\n"
765 "On Windows, this probably means that you have compiled\n"
766 "your code with gcc without the -fnative-struct\n"
767 "(or -mms-bitfields) switch, or that you are using\n"
768 "an unsupported compiler.");
771 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
772 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
773 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
774 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
775 * -fnative-struct still matters. GtkBox is one such.
778 check_sizeof_GtkBox (size_t sizeof_GtkBox)
780 if (sizeof_GtkBox != sizeof (GtkBox))
781 g_error ("Incompatible build!\n"
782 "The code using GTK+ thinks GtkBox is of different\n"
783 "size than it actually is in this build of GTK+.\n"
784 "On Windows, this probably means that you have compiled\n"
785 "your code with gcc without the -fnative-struct\n"
786 "(or -mms-bitfields) switch, or that you are using\n"
787 "an unsupported compiler.");
790 /* These two functions might get more checks added later, thus pass
791 * in the number of extra args.
794 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
796 check_sizeof_GtkWindow (sizeof_GtkWindow);
798 check_sizeof_GtkBox (sizeof_GtkBox);
799 gtk_init (argc, argv);
803 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
805 check_sizeof_GtkWindow (sizeof_GtkWindow);
807 check_sizeof_GtkBox (sizeof_GtkBox);
808 return gtk_init_check (argc, argv);
814 gtk_exit (gint errorcode)
823 * Initializes internationalization support for GTK+. gtk_init()
824 * automatically does this, so there is typically no point
825 * in calling this function.
827 * If you are calling this function because you changed the locale
828 * after GTK+ is was initialized, then calling this function
829 * may help a bit. (Note, however, that changing the locale
830 * after GTK+ is initialized may produce inconsistent results and
831 * is not really supported.)
833 * In detail - sets the current locale according to the
834 * program environment. This is the same as calling the C library function
835 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
836 * locale specific setup of the windowing system used by GDK.
838 * Returns: a string corresponding to the locale set, typically in the
839 * form lang_COUNTRY, where lang is an ISO-639 language code, and
840 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
841 * result of the setlocale(); it is also used on other machines, such as
842 * Windows, where the C library returns a different result. The string is
843 * owned by GTK+ and should not be modified or freed.
846 gtk_set_locale (void)
848 return gdk_set_locale ();
854 * Return the Unix-style locale string for the language currently in
855 * effect. On Unix systems, this is the return value from
856 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
857 * affect this through the environment variables LC_ALL, LC_CTYPE or
858 * LANG (checked in that order). The locale strings typically is in
859 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
860 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
861 * Swedish as written in Finland or pt_BR for Portuguese as written in
864 * On Windows, the C library doesn't use any such environment
865 * variables, and setting them won't affect the behaviour of functions
866 * like ctime(). The user sets the locale through the Regional Options
867 * in the Control Panel. The C library (in the setlocale() function)
868 * does not use country and language codes, but country and language
869 * names spelled out in English.
870 * However, this function does check the above environment
871 * variables, and does return a Unix-style locale string based on
872 * either said environment variables or the thread's current locale.
874 * Return value: a dynamically allocated string, free with g_free().
878 _gtk_get_lc_ctype (void)
881 /* Somebody might try to set the locale for this process using the
882 * LANG or LC_ environment variables. The Microsoft C library
883 * doesn't know anything about them. You set the locale in the
884 * Control Panel. Setting these env vars won't have any affect on
885 * locale-dependent C library functions like ctime(). But just for
886 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
887 * it easier to test GTK and Pango in various default languages, you
888 * don't have to clickety-click in the Control Panel, you can simply
889 * start the program with LC_ALL=something on the command line.)
893 p = getenv ("LC_ALL");
897 p = getenv ("LC_CTYPE");
905 return g_win32_getlocale ();
907 return g_strdup (setlocale (LC_CTYPE, NULL));
912 * gtk_get_default_language:
914 * Returns the #PangoLanguage for the default language currently in
915 * effect. (Note that this can change over the life of an
916 * application.) The default language is derived from the current
917 * locale. It determines, for example, whether GTK+ uses the
918 * right-to-left or left-to-right text direction. See
919 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
921 * Return value: the default language as a #PangoLanguage, must not be
925 gtk_get_default_language (void)
928 PangoLanguage *result;
931 lang = _gtk_get_lc_ctype ();
932 p = strchr (lang, '.');
935 p = strchr (lang, '@');
939 result = pango_language_from_string (lang);
950 GtkInitFunction *init;
953 gtk_main_loop_level++;
955 loop = g_main_loop_new (NULL, TRUE);
956 main_loops = g_slist_prepend (main_loops, loop);
958 tmp_list = functions = init_functions;
959 init_functions = NULL;
963 init = tmp_list->data;
964 tmp_list = tmp_list->next;
966 (* init->function) (init->data);
969 g_list_free (functions);
971 if (g_main_loop_is_running (main_loops->data))
973 GDK_THREADS_LEAVE ();
974 g_main_loop_run (loop);
975 GDK_THREADS_ENTER ();
981 GList *reinvoke_list = NULL;
982 GtkQuitFunction *quitf;
984 while (quit_functions)
986 quitf = quit_functions->data;
988 tmp_list = quit_functions;
989 quit_functions = g_list_remove_link (quit_functions, quit_functions);
990 g_list_free_1 (tmp_list);
992 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
993 gtk_quit_invoke_function (quitf))
995 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
999 gtk_quit_destroy (quitf);
1006 work = g_list_last (reinvoke_list);
1008 quit_functions->prev = work;
1009 work->next = quit_functions;
1010 quit_functions = work;
1016 main_loops = g_slist_remove (main_loops, loop);
1018 g_main_loop_unref (loop);
1020 gtk_main_loop_level--;
1022 /* Try storing all clipboard data we have */
1023 if (gtk_main_loop_level == 0)
1024 _gtk_clipboard_store_all ();
1028 gtk_main_level (void)
1030 return gtk_main_loop_level;
1034 gtk_main_quit (void)
1036 g_return_if_fail (main_loops != NULL);
1038 g_main_loop_quit (main_loops->data);
1042 gtk_events_pending (void)
1046 GDK_THREADS_LEAVE ();
1047 result = g_main_context_pending (NULL);
1048 GDK_THREADS_ENTER ();
1054 gtk_main_iteration (void)
1056 GDK_THREADS_LEAVE ();
1057 g_main_context_iteration (NULL, TRUE);
1058 GDK_THREADS_ENTER ();
1061 return !g_main_loop_is_running (main_loops->data);
1067 gtk_main_iteration_do (gboolean blocking)
1069 GDK_THREADS_LEAVE ();
1070 g_main_context_iteration (NULL, blocking);
1071 GDK_THREADS_ENTER ();
1074 return !g_main_loop_is_running (main_loops->data);
1079 /* private libgtk to libgdk interfaces
1081 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1082 GdkWindow **grab_window,
1083 gboolean *owner_events);
1084 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1085 GdkWindow **grab_window,
1086 gboolean *owner_events);
1089 rewrite_events_translate (GdkWindow *old_window,
1090 GdkWindow *new_window,
1094 gint old_origin_x, old_origin_y;
1095 gint new_origin_x, new_origin_y;
1097 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1098 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1100 *x += old_origin_x - new_origin_x;
1101 *y += old_origin_y - new_origin_y;
1105 rewrite_event_for_window (GdkEvent *event,
1106 GdkWindow *new_window)
1108 event = gdk_event_copy (event);
1110 switch (event->type)
1113 rewrite_events_translate (event->any.window,
1115 &event->scroll.x, &event->scroll.y);
1117 case GDK_BUTTON_PRESS:
1118 case GDK_2BUTTON_PRESS:
1119 case GDK_3BUTTON_PRESS:
1120 case GDK_BUTTON_RELEASE:
1121 rewrite_events_translate (event->any.window,
1123 &event->button.x, &event->button.y);
1125 case GDK_MOTION_NOTIFY:
1126 rewrite_events_translate (event->any.window,
1128 &event->motion.x, &event->motion.y);
1131 case GDK_KEY_RELEASE:
1132 case GDK_PROXIMITY_IN:
1133 case GDK_PROXIMITY_OUT:
1140 g_object_unref (event->any.window);
1141 event->any.window = g_object_ref (new_window);
1146 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1147 * then what X11 does is deliver the event normally if it was going to this
1148 * client, otherwise, delivers it in terms of the grab window. This function
1149 * rewrites events to the effect that events going to the same window group
1150 * are delivered normally, otherwise, the event is delivered in terms of the
1154 rewrite_event_for_grabs (GdkEvent *event)
1156 GdkWindow *grab_window;
1157 GtkWidget *event_widget, *grab_widget;
1158 gboolean owner_events;
1159 GdkDisplay *display;
1161 switch (event->type)
1164 case GDK_BUTTON_PRESS:
1165 case GDK_2BUTTON_PRESS:
1166 case GDK_3BUTTON_PRESS:
1167 case GDK_BUTTON_RELEASE:
1168 case GDK_MOTION_NOTIFY:
1169 case GDK_PROXIMITY_IN:
1170 case GDK_PROXIMITY_OUT:
1171 display = gdk_drawable_get_display (event->proximity.window);
1172 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1178 case GDK_KEY_RELEASE:
1179 display = gdk_drawable_get_display (event->key.window);
1180 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1189 event_widget = gtk_get_event_widget (event);
1190 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1193 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1194 return rewrite_event_for_window (event, grab_window);
1200 gtk_main_do_event (GdkEvent *event)
1202 GtkWidget *event_widget;
1203 GtkWidget *grab_widget;
1204 GtkWindowGroup *window_group;
1205 GdkEvent *next_event;
1206 GdkEvent *rewritten_event = NULL;
1209 /* If there are any events pending then get the next one.
1211 next_event = gdk_event_peek ();
1213 /* Try to compress enter/leave notify events. These event
1214 * pairs occur when the mouse is dragged quickly across
1215 * a window with many buttons (or through a menu). Instead
1216 * of highlighting and de-highlighting each widget that
1217 * is crossed it is better to simply de-highlight the widget
1218 * which contained the mouse initially and highlight the
1219 * widget which ends up containing the mouse.
1222 if (((event->type == GDK_ENTER_NOTIFY) ||
1223 (event->type == GDK_LEAVE_NOTIFY)) &&
1224 ((next_event->type == GDK_ENTER_NOTIFY) ||
1225 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1226 (next_event->type != event->type) &&
1227 (next_event->any.window == event->any.window))
1229 /* Throw both the peeked copy and the queued copy away
1231 gdk_event_free (next_event);
1232 next_event = gdk_event_get ();
1233 gdk_event_free (next_event);
1239 gdk_event_free (next_event);
1241 if (event->type == GDK_SETTING)
1243 _gtk_settings_handle_event (&event->setting);
1247 if (event->type == GDK_OWNER_CHANGE)
1249 _gtk_clipboard_handle_event (&event->owner_change);
1253 /* Find the widget which got the event. We store the widget
1254 * in the user_data field of GdkWindow's.
1255 * Ignore the event if we don't have a widget for it, except
1256 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1257 * Though this happens rarely, bogus events can occour
1258 * for e.g. destroyed GdkWindows.
1260 event_widget = gtk_get_event_widget (event);
1263 /* To handle selection INCR transactions, we select
1264 * PropertyNotify events on the requestor window and create
1265 * a corresponding (fake) GdkWindow so that events get
1266 * here. There won't be a widget though, so we have to handle
1269 if (event->type == GDK_PROPERTY_NOTIFY)
1270 _gtk_selection_incr_event (event->any.window,
1276 /* If pointer or keyboard grabs are in effect, munge the events
1277 * so that each window group looks like a separate app.
1279 rewritten_event = rewrite_event_for_grabs (event);
1280 if (rewritten_event)
1282 event = rewritten_event;
1283 event_widget = gtk_get_event_widget (event);
1286 window_group = gtk_main_get_window_group (event_widget);
1288 /* Push the event onto a stack of current events for
1289 * gtk_current_event_get().
1291 current_events = g_list_prepend (current_events, event);
1293 /* If there is a grab in effect...
1295 if (window_group->grabs)
1297 grab_widget = window_group->grabs->data;
1299 /* If the grab widget is an ancestor of the event widget
1300 * then we send the event to the original event widget.
1301 * This is the key to implementing modality.
1303 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1304 gtk_widget_is_ancestor (event_widget, grab_widget))
1305 grab_widget = event_widget;
1309 grab_widget = event_widget;
1312 /* Not all events get sent to the grabbing widget.
1313 * The delete, destroy, expose, focus change and resize
1314 * events still get sent to the event widget because
1315 * 1) these events have no meaning for the grabbing widget
1316 * and 2) redirecting these events to the grabbing widget
1317 * could cause the display to be messed up.
1319 * Drag events are also not redirected, since it isn't
1320 * clear what the semantics of that would be.
1322 switch (event->type)
1328 g_object_ref (event_widget);
1329 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1330 !gtk_widget_event (event_widget, event))
1331 gtk_widget_destroy (event_widget);
1332 g_object_unref (event_widget);
1336 /* Unexpected GDK_DESTROY from the outside, ignore for
1337 * child windows, handle like a GDK_DELETE for toplevels
1339 if (!event_widget->parent)
1341 g_object_ref (event_widget);
1342 if (!gtk_widget_event (event_widget, event) &&
1343 GTK_WIDGET_REALIZED (event_widget))
1344 gtk_widget_destroy (event_widget);
1345 g_object_unref (event_widget);
1350 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1352 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1353 gtk_widget_send_expose (event_widget, event);
1354 gdk_window_end_paint (event->any.window);
1357 gtk_widget_send_expose (event_widget, event);
1360 case GDK_PROPERTY_NOTIFY:
1362 case GDK_FOCUS_CHANGE:
1366 case GDK_SELECTION_CLEAR:
1367 case GDK_SELECTION_REQUEST:
1368 case GDK_SELECTION_NOTIFY:
1369 case GDK_CLIENT_EVENT:
1370 case GDK_VISIBILITY_NOTIFY:
1371 case GDK_WINDOW_STATE:
1372 gtk_widget_event (event_widget, event);
1376 case GDK_BUTTON_PRESS:
1377 case GDK_2BUTTON_PRESS:
1378 case GDK_3BUTTON_PRESS:
1379 gtk_propagate_event (grab_widget, event);
1383 case GDK_KEY_RELEASE:
1386 if (gtk_invoke_key_snoopers (grab_widget, event))
1389 /* else fall through */
1390 case GDK_MOTION_NOTIFY:
1391 case GDK_BUTTON_RELEASE:
1392 case GDK_PROXIMITY_IN:
1393 case GDK_PROXIMITY_OUT:
1394 gtk_propagate_event (grab_widget, event);
1397 case GDK_ENTER_NOTIFY:
1398 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1400 g_object_ref (event_widget);
1402 gtk_widget_event (grab_widget, event);
1403 if (event_widget == grab_widget)
1404 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1406 g_object_unref (event_widget);
1410 case GDK_LEAVE_NOTIFY:
1411 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1413 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1414 gtk_widget_event (event_widget, event);
1416 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1417 gtk_widget_event (grab_widget, event);
1420 case GDK_DRAG_STATUS:
1421 case GDK_DROP_FINISHED:
1422 _gtk_drag_source_handle_event (event_widget, event);
1424 case GDK_DRAG_ENTER:
1425 case GDK_DRAG_LEAVE:
1426 case GDK_DRAG_MOTION:
1427 case GDK_DROP_START:
1428 _gtk_drag_dest_handle_event (event_widget, event);
1431 g_assert_not_reached ();
1435 tmp_list = current_events;
1436 current_events = g_list_remove_link (current_events, tmp_list);
1437 g_list_free_1 (tmp_list);
1439 if (rewritten_event)
1440 gdk_event_free (rewritten_event);
1455 static GtkWindowGroup *
1456 gtk_main_get_window_group (GtkWidget *widget)
1458 GtkWidget *toplevel = NULL;
1461 toplevel = gtk_widget_get_toplevel (widget);
1463 if (toplevel && GTK_IS_WINDOW (toplevel))
1464 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1466 return _gtk_window_get_group (NULL);
1471 GtkWidget *old_grab_widget;
1472 GtkWidget *new_grab_widget;
1476 check_is_grabbed (GtkWidget *widget,
1477 GtkWidget *grab_widget)
1480 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1486 gtk_grab_notify_foreach (GtkWidget *child,
1490 GrabNotifyInfo *info = data;
1491 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1492 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1494 if (was_grabbed != is_grabbed)
1496 g_object_ref (child);
1498 _gtk_widget_grab_notify (child, was_grabbed);
1500 if (GTK_IS_CONTAINER (child))
1501 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1503 g_object_unref (child);
1508 gtk_grab_notify (GtkWindowGroup *group,
1509 GtkWidget *grab_widget,
1510 gboolean was_grabbed)
1513 GrabNotifyInfo info;
1517 info.old_grab_widget = grab_widget;
1518 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1522 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1523 info.new_grab_widget = grab_widget;
1526 g_object_ref (group);
1527 g_object_ref (grab_widget);
1529 toplevels = gtk_window_list_toplevels ();
1530 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1534 GtkWindow *toplevel = toplevels->data;
1535 toplevels = g_list_delete_link (toplevels, toplevels);
1537 if (group == _gtk_window_get_group (toplevel))
1538 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1539 g_object_unref (toplevel);
1542 g_object_unref (group);
1543 g_object_unref (grab_widget);
1547 gtk_grab_add (GtkWidget *widget)
1549 GtkWindowGroup *group;
1550 gboolean was_grabbed;
1552 g_return_if_fail (widget != NULL);
1554 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1556 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1558 group = gtk_main_get_window_group (widget);
1560 was_grabbed = (group->grabs != NULL);
1562 g_object_ref (widget);
1563 group->grabs = g_slist_prepend (group->grabs, widget);
1565 gtk_grab_notify (group, widget, FALSE);
1570 gtk_grab_get_current (void)
1572 GtkWindowGroup *group;
1574 group = gtk_main_get_window_group (NULL);
1577 return GTK_WIDGET (group->grabs->data);
1582 gtk_grab_remove (GtkWidget *widget)
1584 GtkWindowGroup *group;
1586 g_return_if_fail (widget != NULL);
1588 if (GTK_WIDGET_HAS_GRAB (widget))
1590 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1592 group = gtk_main_get_window_group (widget);
1593 group->grabs = g_slist_remove (group->grabs, widget);
1595 g_object_unref (widget);
1597 gtk_grab_notify (group, widget, TRUE);
1602 gtk_init_add (GtkFunction function,
1605 GtkInitFunction *init;
1607 init = g_new (GtkInitFunction, 1);
1608 init->function = function;
1611 init_functions = g_list_prepend (init_functions, init);
1615 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1618 GtkKeySnooperData *data;
1619 static guint snooper_id = 1;
1621 g_return_val_if_fail (snooper != NULL, 0);
1623 data = g_new (GtkKeySnooperData, 1);
1624 data->func = snooper;
1625 data->func_data = func_data;
1626 data->id = snooper_id++;
1627 key_snoopers = g_slist_prepend (key_snoopers, data);
1633 gtk_key_snooper_remove (guint snooper_id)
1635 GtkKeySnooperData *data = NULL;
1638 slist = key_snoopers;
1642 if (data->id == snooper_id)
1645 slist = slist->next;
1650 key_snoopers = g_slist_remove (key_snoopers, data);
1656 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1660 gint return_val = FALSE;
1662 slist = key_snoopers;
1663 while (slist && !return_val)
1665 GtkKeySnooperData *data;
1668 slist = slist->next;
1669 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1676 gtk_quit_add_full (guint main_level,
1677 GtkFunction function,
1678 GtkCallbackMarshal marshal,
1680 GtkDestroyNotify destroy)
1682 static guint quit_id = 1;
1683 GtkQuitFunction *quitf;
1685 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1687 if (!quit_mem_chunk)
1688 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1689 512, G_ALLOC_AND_FREE);
1691 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1693 quitf->id = quit_id++;
1694 quitf->main_level = main_level;
1695 quitf->function = function;
1696 quitf->marshal = marshal;
1698 quitf->destroy = destroy;
1700 quit_functions = g_list_prepend (quit_functions, quitf);
1706 gtk_quit_destroy (GtkQuitFunction *quitf)
1709 quitf->destroy (quitf->data);
1710 g_mem_chunk_free (quit_mem_chunk, quitf);
1714 gtk_quit_destructor (GtkObject **object_p)
1717 gtk_object_destroy (*object_p);
1724 gtk_quit_add_destroy (guint main_level,
1727 GtkObject **object_p;
1729 g_return_if_fail (main_level > 0);
1730 g_return_if_fail (GTK_IS_OBJECT (object));
1732 object_p = g_new (GtkObject*, 1);
1734 g_signal_connect (object,
1736 G_CALLBACK (gtk_widget_destroyed),
1738 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1742 gtk_quit_add (guint main_level,
1743 GtkFunction function,
1746 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1750 gtk_quit_remove (guint id)
1752 GtkQuitFunction *quitf;
1755 tmp_list = quit_functions;
1758 quitf = tmp_list->data;
1760 if (quitf->id == id)
1762 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1763 g_list_free (tmp_list);
1764 gtk_quit_destroy (quitf);
1769 tmp_list = tmp_list->next;
1774 gtk_quit_remove_by_data (gpointer data)
1776 GtkQuitFunction *quitf;
1779 tmp_list = quit_functions;
1782 quitf = tmp_list->data;
1784 if (quitf->data == data)
1786 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1787 g_list_free (tmp_list);
1788 gtk_quit_destroy (quitf);
1793 tmp_list = tmp_list->next;
1798 gtk_timeout_add_full (guint32 interval,
1799 GtkFunction function,
1800 GtkCallbackMarshal marshal,
1802 GtkDestroyNotify destroy)
1806 GtkClosure *closure;
1808 closure = g_new (GtkClosure, 1);
1809 closure->marshal = marshal;
1810 closure->data = data;
1811 closure->destroy = destroy;
1813 return g_timeout_add_full (0, interval,
1814 gtk_invoke_idle_timeout,
1816 gtk_destroy_closure);
1819 return g_timeout_add_full (0, interval, function, data, destroy);
1823 gtk_timeout_add (guint32 interval,
1824 GtkFunction function,
1827 return g_timeout_add_full (0, interval, function, data, NULL);
1831 gtk_timeout_remove (guint tag)
1833 g_source_remove (tag);
1837 gtk_idle_add_full (gint priority,
1838 GtkFunction function,
1839 GtkCallbackMarshal marshal,
1841 GtkDestroyNotify destroy)
1845 GtkClosure *closure;
1847 closure = g_new (GtkClosure, 1);
1848 closure->marshal = marshal;
1849 closure->data = data;
1850 closure->destroy = destroy;
1852 return g_idle_add_full (priority,
1853 gtk_invoke_idle_timeout,
1855 gtk_destroy_closure);
1858 return g_idle_add_full (priority, function, data, destroy);
1862 gtk_idle_add (GtkFunction function,
1865 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1869 gtk_idle_add_priority (gint priority,
1870 GtkFunction function,
1873 return g_idle_add_full (priority, function, data, NULL);
1877 gtk_idle_remove (guint tag)
1879 g_source_remove (tag);
1883 gtk_idle_remove_by_data (gpointer data)
1885 if (!g_idle_remove_by_data (data))
1886 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1890 gtk_input_add_full (gint source,
1891 GdkInputCondition condition,
1892 GdkInputFunction function,
1893 GtkCallbackMarshal marshal,
1895 GtkDestroyNotify destroy)
1899 GtkClosure *closure;
1901 closure = g_new (GtkClosure, 1);
1902 closure->marshal = marshal;
1903 closure->data = data;
1904 closure->destroy = destroy;
1906 return gdk_input_add_full (source,
1908 (GdkInputFunction) gtk_invoke_input,
1910 (GdkDestroyNotify) gtk_destroy_closure);
1913 return gdk_input_add_full (source, condition, function, data, destroy);
1917 gtk_input_remove (guint tag)
1919 g_source_remove (tag);
1923 gtk_destroy_closure (gpointer data)
1925 GtkClosure *closure = data;
1927 if (closure->destroy)
1928 (closure->destroy) (closure->data);
1933 gtk_invoke_idle_timeout (gpointer data)
1935 GtkClosure *closure = data;
1938 gint ret_val = FALSE;
1939 args[0].name = NULL;
1940 args[0].type = G_TYPE_BOOLEAN;
1941 args[0].d.pointer_data = &ret_val;
1942 closure->marshal (NULL, closure->data, 0, args);
1947 gtk_invoke_input (gpointer data,
1949 GdkInputCondition condition)
1951 GtkClosure *closure = data;
1954 args[0].type = G_TYPE_INT;
1955 args[0].name = NULL;
1956 GTK_VALUE_INT (args[0]) = source;
1957 args[1].type = GDK_TYPE_INPUT_CONDITION;
1958 args[1].name = NULL;
1959 GTK_VALUE_FLAGS (args[1]) = condition;
1960 args[2].type = G_TYPE_NONE;
1961 args[2].name = NULL;
1963 closure->marshal (NULL, closure->data, 2, args);
1967 * gtk_get_current_event:
1969 * Obtains a copy of the event currently being processed by GTK+. For
1970 * example, if you get a "clicked" signal from #GtkButton, the current
1971 * event will be the #GdkEventButton that triggered the "clicked"
1972 * signal. The returned event must be freed with gdk_event_free().
1973 * If there is no current event, the function returns %NULL.
1975 * Return value: a copy of the current event, or %NULL if no current event.
1978 gtk_get_current_event (void)
1981 return gdk_event_copy (current_events->data);
1987 * gtk_get_current_event_time:
1989 * If there is a current event and it has a timestamp, return that
1990 * timestamp, otherwise return %GDK_CURRENT_TIME.
1992 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1995 gtk_get_current_event_time (void)
1998 return gdk_event_get_time (current_events->data);
2000 return GDK_CURRENT_TIME;
2004 * gtk_get_current_event_state:
2005 * @state: a location to store the state of the current event
2007 * If there is a current event and it has a state field, place
2008 * that state field in @state and return %TRUE, otherwise return
2011 * Return value: %TRUE if there was a current event and it had a state field
2014 gtk_get_current_event_state (GdkModifierType *state)
2016 g_return_val_if_fail (state != NULL, FALSE);
2019 return gdk_event_get_state (current_events->data, state);
2028 * gtk_get_event_widget:
2029 * @event: a #GdkEvent
2031 * If @event is %NULL or the event was not associated with any widget,
2032 * returns %NULL, otherwise returns the widget that received the event
2035 * Return value: the widget that originally received @event, or %NULL
2038 gtk_get_event_widget (GdkEvent *event)
2043 if (event && event->any.window)
2044 gdk_window_get_user_data (event->any.window, (void**) &widget);
2050 gtk_quit_invoke_function (GtkQuitFunction *quitf)
2052 if (!quitf->marshal)
2053 return quitf->function (quitf->data);
2057 gint ret_val = FALSE;
2059 args[0].name = NULL;
2060 args[0].type = G_TYPE_BOOLEAN;
2061 args[0].d.pointer_data = &ret_val;
2062 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2070 * gtk_propagate_event:
2071 * @widget: a #GtkWidget
2074 * Sends an event to a widget, propagating the event to parent widgets
2075 * if the event remains unhandled. Events received by GTK+ from GDK
2076 * normally begin in gtk_main_do_event(). Depending on the type of
2077 * event, existence of modal dialogs, grabs, etc., the event may be
2078 * propagated; if so, this function is used. gtk_propagate_event()
2079 * calls gtk_widget_event() on each widget it decides to send the
2080 * event to. So gtk_widget_event() is the lowest-level function; it
2081 * simply emits the "event" and possibly an event-specific signal on a
2082 * widget. gtk_propagate_event() is a bit higher-level, and
2083 * gtk_main_do_event() is the highest level.
2085 * All that said, you most likely don't want to use any of these
2086 * functions; synthesizing events is rarely needed. Consider asking on
2087 * the mailing list for better ways to achieve your goals. For
2088 * example, use gdk_window_invalidate_rect() or
2089 * gtk_widget_queue_draw() instead of making up expose events.
2093 gtk_propagate_event (GtkWidget *widget,
2098 g_return_if_fail (GTK_IS_WIDGET (widget));
2099 g_return_if_fail (event != NULL);
2101 handled_event = FALSE;
2103 g_object_ref (widget);
2105 if ((event->type == GDK_KEY_PRESS) ||
2106 (event->type == GDK_KEY_RELEASE))
2108 /* Only send key events within Window widgets to the Window
2109 * The Window widget will in turn pass the
2110 * key event on to the currently focused widget
2115 window = gtk_widget_get_toplevel (widget);
2116 if (window && GTK_IS_WINDOW (window))
2118 /* If there is a grab within the window, give the grab widget
2119 * a first crack at the key event
2121 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2122 handled_event = gtk_widget_event (widget, event);
2126 window = gtk_widget_get_toplevel (widget);
2127 if (window && GTK_IS_WINDOW (window))
2129 if (GTK_WIDGET_IS_SENSITIVE (window))
2130 gtk_widget_event (window, event);
2134 handled_event = TRUE; /* don't send to widget */
2138 /* Other events get propagated up the widget tree
2139 * so that parents can see the button and motion
2140 * events of the children.
2148 /* Scroll events are special cased here because it
2149 * feels wrong when scrolling a GtkViewport, say,
2150 * to have children of the viewport eat the scroll
2153 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2154 handled_event = event->type != GDK_SCROLL;
2156 handled_event = gtk_widget_event (widget, event);
2158 tmp = widget->parent;
2159 g_object_unref (widget);
2163 if (!handled_event && widget)
2164 g_object_ref (widget);
2170 g_object_unref (widget);
2175 gtk_error (gchar *str)
2181 gtk_warning (gchar *str)
2187 gtk_message (gchar *str)
2193 gtk_print (gchar *str)
2195 static GtkWidget *window = NULL;
2196 static GtkWidget *text;
2197 static int level = 0;
2201 GtkWidget *hscrollbar;
2202 GtkWidget *vscrollbar;
2203 GtkWidget *separator;
2208 fputs (str, stdout);
2215 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2217 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2218 (GtkSignalFunc) gtk_widget_destroyed,
2221 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2223 box1 = gtk_vbox_new (FALSE, 0);
2224 gtk_container_add (GTK_CONTAINER (window), box1);
2225 gtk_widget_show (box1);
2228 box2 = gtk_vbox_new (FALSE, 10);
2229 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2230 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2231 gtk_widget_show (box2);
2234 table = gtk_table_new (2, 2, FALSE);
2235 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2236 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2237 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2238 gtk_widget_show (table);
2240 text = gtk_text_new (NULL, NULL);
2241 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2242 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2243 gtk_widget_show (text);
2244 gtk_widget_realize (text);
2246 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2247 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2248 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2249 gtk_widget_show (hscrollbar);
2251 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2252 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2253 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2254 gtk_widget_show (vscrollbar);
2256 separator = gtk_hseparator_new ();
2257 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2258 gtk_widget_show (separator);
2261 box2 = gtk_vbox_new (FALSE, 10);
2262 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2263 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2264 gtk_widget_show (box2);
2267 button = gtk_button_new_with_label ("close");
2268 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2269 (GtkSignalFunc) gtk_widget_hide,
2270 GTK_OBJECT (window));
2271 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2272 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2273 gtk_widget_grab_default (button);
2274 gtk_widget_show (button);
2278 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2281 if (!GTK_WIDGET_VISIBLE (window))
2282 gtk_widget_show (window);
2287 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2288 GValue *return_accu,
2289 const GValue *handler_return,
2292 gboolean continue_emission;
2293 gboolean signal_handled;
2295 signal_handled = g_value_get_boolean (handler_return);
2296 g_value_set_boolean (return_accu, signal_handled);
2297 continue_emission = !signal_handled;
2299 return continue_emission;
2302 #define __GTK_MAIN_C__
2303 #include "gtkaliasdef.c"