1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkconfig.h"
33 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
43 #include <sys/types.h> /* For uid_t, gid_t */
51 #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"
71 /* Private type definitions
73 typedef struct _GtkInitFunction GtkInitFunction;
74 typedef struct _GtkQuitFunction GtkQuitFunction;
75 typedef struct _GtkClosure GtkClosure;
76 typedef struct _GtkKeySnooperData GtkKeySnooperData;
78 struct _GtkInitFunction
84 struct _GtkQuitFunction
88 GtkCallbackMarshal marshal;
91 GtkDestroyNotify destroy;
96 GtkCallbackMarshal marshal;
98 GtkDestroyNotify destroy;
101 struct _GtkKeySnooperData
103 GtkKeySnoopFunc func;
108 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
109 static void gtk_quit_destroy (GtkQuitFunction *quitf);
110 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
113 static void gtk_destroy_closure (gpointer data);
114 static gboolean gtk_invoke_idle_timeout (gpointer data);
115 static void gtk_invoke_input (gpointer data,
117 GdkInputCondition condition);
120 static void gtk_error (gchar *str);
121 static void gtk_warning (gchar *str);
122 static void gtk_message (gchar *str);
123 static void gtk_print (gchar *str);
126 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
128 const guint gtk_major_version = GTK_MAJOR_VERSION;
129 const guint gtk_minor_version = GTK_MINOR_VERSION;
130 const guint gtk_micro_version = GTK_MICRO_VERSION;
131 const guint gtk_binary_age = GTK_BINARY_AGE;
132 const guint gtk_interface_age = GTK_INTERFACE_AGE;
134 static guint gtk_main_loop_level = 0;
135 static gint gtk_initialized = FALSE;
136 static GList *current_events = NULL;
138 static GSList *main_loops = NULL; /* stack of currently executing main loops */
140 static GList *init_functions = NULL; /* A list of init functions.
142 static GList *quit_functions = NULL; /* A list of quit functions.
144 static GMemChunk *quit_mem_chunk = NULL;
146 static GSList *key_snoopers = NULL;
148 guint gtk_debug_flags = 0; /* Global GTK debug flag */
150 #ifdef G_ENABLE_DEBUG
151 static const GDebugKey gtk_debug_keys[] = {
152 {"misc", GTK_DEBUG_MISC},
153 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
154 {"text", GTK_DEBUG_TEXT},
155 {"tree", GTK_DEBUG_TREE},
156 {"updates", GTK_DEBUG_UPDATES},
157 {"keybindings", GTK_DEBUG_KEYBINDINGS},
158 {"multihead", GTK_DEBUG_MULTIHEAD},
159 {"modules", GTK_DEBUG_MODULES}
162 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
164 #endif /* G_ENABLE_DEBUG */
168 * @required_major: the required major version.
169 * @required_minor: the required major version.
170 * @required_micro: the required major version.
172 * Checks that the GTK+ library in use is compatible with the
173 * given version. Generally you would pass in the constants
174 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
175 * as the three arguments to this function; that produces
176 * a check that the library in use is compatible with
177 * the version of GTK+ the application or module was compiled
180 * Compatibility is defined by two things: first the version
181 * of the running library is newer than the version
182 * @required_major.required_minor.@required_micro. Second
183 * the running library must be binary compatible with the
184 * version @required_major.required_minor.@required_micro
185 * (same major version.)
187 * This function is primarily for GTK+ modules; the module
188 * can call this function to check that it wasn't loaded
189 * into an incompatible version of GTK+. However, such a
190 * a check isn't completely reliable, since the module may be
191 * linked against an old version of GTK+ and calling the
192 * old version of gtk_check_version(), but still get loaded
193 * into an application using a newer version of GTK+.
195 * Return value: %NULL if the GTK+ library is compatible with the
196 * given version, or a string describing the version mismatch.
197 * The returned string is owned by GTK+ and should not be modified
201 gtk_check_version (guint required_major,
202 guint required_minor,
203 guint required_micro)
205 gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
206 gint required_effective_micro = 100 * required_minor + required_micro;
208 if (required_major > GTK_MAJOR_VERSION)
209 return "Gtk+ version too old (major mismatch)";
210 if (required_major < GTK_MAJOR_VERSION)
211 return "Gtk+ version too new (major mismatch)";
212 if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
213 return "Gtk+ version too new (micro mismatch)";
214 if (required_effective_micro > gtk_effective_micro)
215 return "Gtk+ version too old (micro mismatch)";
219 /* This checks to see if the process is running suid or sgid
220 * at the current time. If so, we don't allow GTK+ to be initialized.
221 * This is meant to be a mild check - we only error out if we
222 * can prove the programmer is doing something wrong, not if
223 * they could be doing something wrong. For this reason, we
224 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
229 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
231 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
232 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
234 #ifdef HAVE_GETRESUID
235 /* These aren't in the header files, so we prototype them here.
237 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
238 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
240 if (getresuid (&ruid, &euid, &suid) != 0 ||
241 getresgid (&rgid, &egid, &sgid) != 0)
242 #endif /* HAVE_GETRESUID */
244 suid = ruid = getuid ();
245 sgid = rgid = getgid ();
250 if (ruid != euid || ruid != suid ||
251 rgid != egid || rgid != sgid)
253 g_warning ("This process is currently running setuid or setgid.\n"
254 "This is not a supported use of GTK+. You must create a helper\n"
255 "program instead. For further details, see:\n\n"
256 " http://www.gtk.org/setuid.html\n\n"
257 "Refusing to initialize GTK+.");
266 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
269 _gtk_get_datadir (void)
271 static char *gtk_datadir = NULL;
272 if (gtk_datadir == NULL)
273 gtk_datadir = g_win32_get_package_installation_subdirectory
274 (GETTEXT_PACKAGE, dll_name, "share");
280 _gtk_get_libdir (void)
282 static char *gtk_libdir = NULL;
283 if (gtk_libdir == NULL)
284 gtk_libdir = g_win32_get_package_installation_subdirectory
285 (GETTEXT_PACKAGE, dll_name, "lib");
291 _gtk_get_localedir (void)
293 static char *gtk_localedir = NULL;
294 if (gtk_localedir == NULL)
295 gtk_localedir = g_win32_get_package_installation_subdirectory
296 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
298 return gtk_localedir;
302 _gtk_get_sysconfdir (void)
304 static char *gtk_sysconfdir = NULL;
305 if (gtk_sysconfdir == NULL)
306 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
307 (GETTEXT_PACKAGE, dll_name, "etc");
309 return gtk_sysconfdir;
313 _gtk_get_data_prefix (void)
315 static char *gtk_data_prefix = NULL;
316 if (gtk_data_prefix == NULL)
317 gtk_data_prefix = g_win32_get_package_installation_directory
318 (GETTEXT_PACKAGE, dll_name);
320 return gtk_data_prefix;
323 #endif /* G_OS_WIN32 */
325 static gboolean do_setlocale = TRUE;
328 * gtk_disable_setlocale:
330 * Prevents gtk_init() and gtk_init_check() from automatically
331 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
332 * want to use this function if you wanted to set the locale for
333 * your program to something other than the user's locale, or if
334 * you wanted to set different values for different locale categories.
336 * Most programs should not need to call this function.
339 gtk_disable_setlocale (void)
342 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
344 do_setlocale = FALSE;
347 #ifdef G_PLATFORM_WIN32
348 #undef gtk_init_check
351 static GString *gtk_modules_string = NULL;
353 #ifdef G_ENABLE_DEBUG
355 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
357 gtk_debug_flags |= g_parse_debug_string (value,
365 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
367 gtk_debug_flags &= ~g_parse_debug_string (value,
371 #endif /* G_ENABLE_DEBUG */
374 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
378 if (gtk_modules_string)
379 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
381 gtk_modules_string = g_string_new (NULL);
383 g_string_append (gtk_modules_string, value);
390 gtk_arg_warnings_cb (const char *key, const char *value, gpointer user_data)
392 GLogLevelFlags fatal_mask;
394 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
395 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
396 g_log_set_always_fatal (fatal_mask);
401 static GOptionEntry gtk_args[] = {
402 { "gtk-module", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb, NULL, NULL },
403 { "g-fatal-warnings", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_warnings_cb, NULL, NULL },
404 #ifdef G_ENABLE_DEBUG
405 { "gtk-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb, NULL, NULL },
406 { "gtk-no-debug", 0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb, NULL, NULL },
407 #endif /* G_ENABLE_DEBUG */
412 do_pre_parse_initialization (int *argc,
415 const gchar *env_string;
418 g_set_error_handler (gtk_error);
419 g_set_warning_handler (gtk_warning);
420 g_set_message_handler (gtk_message);
421 g_set_print_handler (gtk_print);
426 if (!setlocale (LC_ALL, ""))
427 g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
430 gdk_pre_parse_libgtk_only ();
431 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
433 #ifdef G_ENABLE_DEBUG
434 env_string = g_getenv ("GTK_DEBUG");
435 if (env_string != NULL)
437 gtk_debug_flags = g_parse_debug_string (env_string,
442 #endif /* G_ENABLE_DEBUG */
444 env_string = g_getenv ("GTK_MODULES");
446 gtk_modules_string = g_string_new (env_string);
450 do_post_parse_initialization (int *argc,
455 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
456 gdk_window_set_debug_updates (TRUE);
459 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
460 bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
461 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
462 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
463 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
468 /* Translate to default:RTL if you want your widgets
469 * to be RTL, otherwise translate to default:LTR.
470 * Do *not* translate it to "predefinito:LTR", if it
471 * it isn't default:LTR or default:RTL it will not work
473 char *e = _("default:LTR");
474 if (strcmp (e, "default:RTL")==0) {
475 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
476 } else if (strcmp (e, "default:LTR")) {
477 g_warning ("Whoever translated default:LTR did so wrongly.\n");
482 _gtk_accel_map_init ();
485 /* Set the 'initialized' flag.
487 gtk_initialized = TRUE;
489 /* load gtk modules */
490 if (gtk_modules_string)
492 _gtk_modules_init (argc, argv, gtk_modules_string->str);
493 g_string_free (gtk_modules_string, TRUE);
500 gboolean open_default_display;
504 pre_parse_hook (GOptionContext *context,
509 do_pre_parse_initialization (NULL, NULL);
515 post_parse_hook (GOptionContext *context,
520 OptionGroupInfo *info = data;
523 do_post_parse_initialization (NULL, NULL);
525 if (info->open_default_display)
526 return gdk_display_open_default_libgtk_only () != NULL;
533 gtk_get_option_group (gboolean open_default_display)
536 OptionGroupInfo *info;
538 info = g_new0 (OptionGroupInfo, 1);
539 info->open_default_display = open_default_display;
541 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
542 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
544 gdk_add_option_entries_libgtk_only (group);
545 g_option_group_add_entries (group, gtk_args);
551 gtk_init_with_args (int *argc,
553 char *parameter_string,
554 GOptionEntry *entries,
555 char *translation_domain,
558 GOptionContext *context;
559 GOptionGroup *gtk_group;
565 gtk_group = gtk_get_option_group (TRUE);
567 context = g_option_context_new (parameter_string);
568 g_option_context_add_group (context, gtk_group);
571 g_option_context_add_main_entries (context, entries, translation_domain);
573 retval = g_option_context_parse (context, argc, argv, error);
575 g_option_context_free (context);
583 * @argc: a pointer to the number of command line arguments.
584 * @argv: a pointer to the array of command line arguments.
586 * Parses command line arguments, and initializes global
587 * attributes of GTK+, but does not actually open a connection
588 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
590 * Any arguments used by GTK+ or GDK are removed from the array and
591 * @argc and @argv are updated accordingly.
593 * You shouldn't call this function explicitely if you are using
594 * gtk_init(), or gtk_init_check().
596 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
599 gtk_parse_args (int *argc,
602 GOptionContext *option_context;
607 if (!check_setugid ())
610 do_pre_parse_initialization (argc, argv);
612 option_context = g_option_context_new (NULL);
613 g_option_context_set_ignore_unknown_options (option_context, TRUE);
614 g_option_context_set_help_enabled (option_context, FALSE);
616 g_option_context_add_main_entries (option_context, gtk_args, NULL);
618 g_option_context_parse (option_context, argc, argv, NULL);
619 g_option_context_free (option_context);
621 do_post_parse_initialization (argc, argv);
626 #ifdef G_PLATFORM_WIN32
627 #undef gtk_init_check
632 * @argc: Address of the <parameter>argc</parameter> parameter of your
633 * main() function. Changed if any arguments were handled.
634 * @argv: Address of the <parameter>argv</parameter> parameter of main().
635 * Any parameters understood by gtk_init() are stripped before return.
637 * This function does the same work as gtk_init() with only
638 * a single change: It does not terminate the program if the GUI can't be
639 * initialized. Instead it returns %FALSE on failure.
641 * This way the application can fall back to some other means of communication
642 * with the user - for example a curses or command line interface.
644 * Return value: %TRUE if the GUI has been successfully initialized,
648 gtk_init_check (int *argc,
651 if (!gtk_parse_args (argc, argv))
654 return gdk_display_open_default_libgtk_only () != NULL;
657 #ifdef G_PLATFORM_WIN32
663 * @argc: Address of the <parameter>argc</parameter> parameter of your
664 * main() function. Changed if any arguments were handled.
665 * @argv: Address of the <parameter>argv</parameter> parameter of main().
666 * Any parameters understood by gtk_init() are stripped before return.
668 * Call this function before using any other GTK+ functions in your GUI
669 * applications. It will initialize everything needed to operate the
670 * toolkit and parses some standard command line options. @argc and
671 * @argv are adjusted accordingly so your own code will
672 * never see those standard arguments.
675 * This function will terminate your program if it was unable to initialize
676 * the GUI for some reason. If you want your program to fall back to a
677 * textual interface you want to call gtk_init_check() instead.
681 gtk_init (int *argc, char ***argv)
683 if (!gtk_init_check (argc, argv))
685 const char *display_name_arg = gdk_get_display_arg_name ();
686 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
691 #ifdef G_PLATFORM_WIN32
694 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
696 if (sizeof_GtkWindow != sizeof (GtkWindow))
697 g_error ("Incompatible build!\n"
698 "The code using GTK+ thinks GtkWindow is of different\n"
699 "size than it actually is in this build of GTK+.\n"
700 "On Windows, this probably means that you have compiled\n"
701 "your code with gcc without the -fnative-struct\n"
702 "(or -mms-bitfields) switch, or that you are using\n"
703 "an unsupported compiler.");
706 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
707 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
708 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
709 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
710 * -fnative-struct still matters. GtkBox is one such.
713 check_sizeof_GtkBox (size_t sizeof_GtkBox)
715 if (sizeof_GtkBox != sizeof (GtkBox))
716 g_error ("Incompatible build!\n"
717 "The code using GTK+ thinks GtkBox is of different\n"
718 "size than it actually is in this build of GTK+.\n"
719 "On Windows, this probably means that you have compiled\n"
720 "your code with gcc without the -fnative-struct\n"
721 "(or -mms-bitfields) switch, or that you are using\n"
722 "an unsupported compiler.");
725 /* These two functions might get more checks added later, thus pass
726 * in the number of extra args.
729 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
731 check_sizeof_GtkWindow (sizeof_GtkWindow);
733 check_sizeof_GtkBox (sizeof_GtkBox);
734 gtk_init (argc, argv);
738 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
740 check_sizeof_GtkWindow (sizeof_GtkWindow);
742 check_sizeof_GtkBox (sizeof_GtkBox);
743 return gtk_init_check (argc, argv);
749 gtk_exit (gint errorcode)
758 * Initializes internationalization support for GTK+. gtk_init()
759 * automatically does this, so there is typically no point
760 * in calling this function.
762 * If you are calling this function because you changed the locale
763 * after GTK+ is was initialized, then calling this function
764 * may help a bit. (Note, however, that changing the locale
765 * after GTK+ is initialized may produce inconsistent results and
766 * is not really supported.)
768 * In detail - sets the current locale according to the
769 * program environment. This is the same as calling the C library function
770 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
771 * locale specific setup of the windowing system used by GDK.
773 * Returns: a string corresponding to the locale set, typically in the
774 * form lang_COUNTRY, where lang is an ISO-639 language code, and
775 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
776 * result of the setlocale(); it is also used on other machines, such as
777 * Windows, where the C library returns a different result. The string is
778 * owned by GTK+ and should not be modified or freed.
781 gtk_set_locale (void)
783 return gdk_set_locale ();
789 * Return the Unix-style locale string for the language currently in
790 * effect. On Unix systems, this is the return value from
791 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
792 * affect this through the environment variables LC_ALL, LC_CTYPE or
793 * LANG (checked in that order). The locale strings typically is in
794 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
795 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
796 * Swedish as written in Finland or pt_BR for Portuguese as written in
799 * On Windows, the C library doesn't use any such environment
800 * variables, and setting them won't affect the behaviour of functions
801 * like ctime(). The user sets the locale through the Regional Options
802 * in the Control Panel. The C library (in the setlocale() function)
803 * does not use country and language codes, but country and language
804 * names spelled out in English.
805 * However, this function does check the above environment
806 * variables, and does return a Unix-style locale string based on
807 * either said environment variables or the thread's current locale.
809 * Return value: a dynamically allocated string, free with g_free().
813 _gtk_get_lc_ctype (void)
816 /* Somebody might try to set the locale for this process using the
817 * LANG or LC_ environment variables. The Microsoft C library
818 * doesn't know anything about them. You set the locale in the
819 * Control Panel. Setting these env vars won't have any affect on
820 * locale-dependent C library functions like ctime(). But just for
821 * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
822 * it easier to test GTK and Pango in various default languages, you
823 * don't have to clickety-click in the Control Panel, you can simply
824 * start the program with LC_ALL=something on the command line.)
828 p = getenv ("LC_ALL");
832 p = getenv ("LC_CTYPE");
840 return g_win32_getlocale ();
842 return g_strdup (setlocale (LC_CTYPE, NULL));
847 * gtk_get_default_language:
849 * Returns the #PangoLanguage for the default language currently in
850 * effect. (Note that this can change over the life of an
851 * application.) The default language is derived from the current
852 * locale. It determines, for example, whether GTK+ uses the
853 * right-to-left or left-to-right text direction. See
854 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
856 * Return value: the default language as a #PangoLanguage, must not be
860 gtk_get_default_language (void)
863 PangoLanguage *result;
866 lang = _gtk_get_lc_ctype ();
867 p = strchr (lang, '.');
870 p = strchr (lang, '@');
874 result = pango_language_from_string (lang);
885 GtkInitFunction *init;
888 gtk_main_loop_level++;
890 loop = g_main_loop_new (NULL, TRUE);
891 main_loops = g_slist_prepend (main_loops, loop);
893 tmp_list = functions = init_functions;
894 init_functions = NULL;
898 init = tmp_list->data;
899 tmp_list = tmp_list->next;
901 (* init->function) (init->data);
904 g_list_free (functions);
906 if (g_main_loop_is_running (main_loops->data))
908 GDK_THREADS_LEAVE ();
909 g_main_loop_run (loop);
910 GDK_THREADS_ENTER ();
916 GList *reinvoke_list = NULL;
917 GtkQuitFunction *quitf;
919 while (quit_functions)
921 quitf = quit_functions->data;
923 tmp_list = quit_functions;
924 quit_functions = g_list_remove_link (quit_functions, quit_functions);
925 g_list_free_1 (tmp_list);
927 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
928 gtk_quit_invoke_function (quitf))
930 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
934 gtk_quit_destroy (quitf);
941 work = g_list_last (reinvoke_list);
943 quit_functions->prev = work;
944 work->next = quit_functions;
945 quit_functions = work;
951 main_loops = g_slist_remove (main_loops, loop);
953 g_main_loop_unref (loop);
955 gtk_main_loop_level--;
959 gtk_main_level (void)
961 return gtk_main_loop_level;
967 g_return_if_fail (main_loops != NULL);
969 g_main_loop_quit (main_loops->data);
973 gtk_events_pending (void)
977 GDK_THREADS_LEAVE ();
978 result = g_main_context_pending (NULL);
979 GDK_THREADS_ENTER ();
985 gtk_main_iteration (void)
987 GDK_THREADS_LEAVE ();
988 g_main_context_iteration (NULL, TRUE);
989 GDK_THREADS_ENTER ();
992 return !g_main_loop_is_running (main_loops->data);
998 gtk_main_iteration_do (gboolean blocking)
1000 GDK_THREADS_LEAVE ();
1001 g_main_context_iteration (NULL, blocking);
1002 GDK_THREADS_ENTER ();
1005 return !g_main_loop_is_running (main_loops->data);
1010 /* private libgtk to libgdk interfaces
1012 gboolean gdk_pointer_grab_info_libgtk_only (GdkDisplay *display,
1013 GdkWindow **grab_window,
1014 gboolean *owner_events);
1015 gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
1016 GdkWindow **grab_window,
1017 gboolean *owner_events);
1020 rewrite_events_translate (GdkWindow *old_window,
1021 GdkWindow *new_window,
1025 gint old_origin_x, old_origin_y;
1026 gint new_origin_x, new_origin_y;
1028 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1029 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1031 *x += old_origin_x - new_origin_x;
1032 *y += old_origin_y - new_origin_y;
1036 rewrite_event_for_window (GdkEvent *event,
1037 GdkWindow *new_window)
1039 event = gdk_event_copy (event);
1041 switch (event->type)
1044 rewrite_events_translate (event->any.window,
1046 &event->scroll.x, &event->scroll.y);
1048 case GDK_BUTTON_PRESS:
1049 case GDK_2BUTTON_PRESS:
1050 case GDK_3BUTTON_PRESS:
1051 case GDK_BUTTON_RELEASE:
1052 rewrite_events_translate (event->any.window,
1054 &event->button.x, &event->button.y);
1056 case GDK_MOTION_NOTIFY:
1057 rewrite_events_translate (event->any.window,
1059 &event->motion.x, &event->motion.y);
1062 case GDK_KEY_RELEASE:
1063 case GDK_PROXIMITY_IN:
1064 case GDK_PROXIMITY_OUT:
1071 g_object_unref (event->any.window);
1072 event->any.window = g_object_ref (new_window);
1077 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1078 * then what X11 does is deliver the event normally if it was going to this
1079 * client, otherwise, delivers it in terms of the grab window. This function
1080 * rewrites events to the effect that events going to the same window group
1081 * are delivered normally, otherwise, the event is delivered in terms of the
1085 rewrite_event_for_grabs (GdkEvent *event)
1087 GdkWindow *grab_window;
1088 GtkWidget *event_widget, *grab_widget;
1089 gboolean owner_events;
1090 GdkDisplay *display;
1092 switch (event->type)
1095 case GDK_BUTTON_PRESS:
1096 case GDK_2BUTTON_PRESS:
1097 case GDK_3BUTTON_PRESS:
1098 case GDK_BUTTON_RELEASE:
1099 case GDK_MOTION_NOTIFY:
1100 case GDK_PROXIMITY_IN:
1101 case GDK_PROXIMITY_OUT:
1102 display = gdk_drawable_get_display (event->proximity.window);
1103 if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1109 case GDK_KEY_RELEASE:
1110 display = gdk_drawable_get_display (event->key.window);
1111 if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1120 event_widget = gtk_get_event_widget (event);
1121 gdk_window_get_user_data (grab_window, (void**) &grab_widget);
1124 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1125 return rewrite_event_for_window (event, grab_window);
1131 gtk_main_do_event (GdkEvent *event)
1133 GtkWidget *event_widget;
1134 GtkWidget *grab_widget;
1135 GtkWindowGroup *window_group;
1136 GdkEvent *next_event;
1137 GdkEvent *rewritten_event = NULL;
1140 /* If there are any events pending then get the next one.
1142 next_event = gdk_event_peek ();
1144 /* Try to compress enter/leave notify events. These event
1145 * pairs occur when the mouse is dragged quickly across
1146 * a window with many buttons (or through a menu). Instead
1147 * of highlighting and de-highlighting each widget that
1148 * is crossed it is better to simply de-highlight the widget
1149 * which contained the mouse initially and highlight the
1150 * widget which ends up containing the mouse.
1153 if (((event->type == GDK_ENTER_NOTIFY) ||
1154 (event->type == GDK_LEAVE_NOTIFY)) &&
1155 ((next_event->type == GDK_ENTER_NOTIFY) ||
1156 (next_event->type == GDK_LEAVE_NOTIFY)) &&
1157 (next_event->type != event->type) &&
1158 (next_event->any.window == event->any.window))
1160 /* Throw both the peeked copy and the queued copy away
1162 gdk_event_free (next_event);
1163 next_event = gdk_event_get ();
1164 gdk_event_free (next_event);
1170 gdk_event_free (next_event);
1172 if (event->type == GDK_SETTING)
1174 _gtk_settings_handle_event (&event->setting);
1178 if (event->type == GDK_OWNER_CHANGE)
1180 _gtk_clipboard_handle_event (&event->owner_change);
1184 /* Find the widget which got the event. We store the widget
1185 * in the user_data field of GdkWindow's.
1186 * Ignore the event if we don't have a widget for it, except
1187 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
1188 * Though this happens rarely, bogus events can occour
1189 * for e.g. destroyed GdkWindows.
1191 event_widget = gtk_get_event_widget (event);
1194 /* To handle selection INCR transactions, we select
1195 * PropertyNotify events on the requestor window and create
1196 * a corresponding (fake) GdkWindow so that events get
1197 * here. There won't be a widget though, so we have to handle
1200 if (event->type == GDK_PROPERTY_NOTIFY)
1201 _gtk_selection_incr_event (event->any.window,
1207 /* If pointer or keyboard grabs are in effect, munge the events
1208 * so that each window group looks like a separate app.
1210 rewritten_event = rewrite_event_for_grabs (event);
1211 if (rewritten_event)
1213 event = rewritten_event;
1214 event_widget = gtk_get_event_widget (event);
1217 window_group = gtk_main_get_window_group (event_widget);
1219 /* Push the event onto a stack of current events for
1220 * gtk_current_event_get().
1222 current_events = g_list_prepend (current_events, event);
1224 /* If there is a grab in effect...
1226 if (window_group->grabs)
1228 grab_widget = window_group->grabs->data;
1230 /* If the grab widget is an ancestor of the event widget
1231 * then we send the event to the original event widget.
1232 * This is the key to implementing modality.
1234 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
1235 gtk_widget_is_ancestor (event_widget, grab_widget))
1236 grab_widget = event_widget;
1240 grab_widget = event_widget;
1243 /* Not all events get sent to the grabbing widget.
1244 * The delete, destroy, expose, focus change and resize
1245 * events still get sent to the event widget because
1246 * 1) these events have no meaning for the grabbing widget
1247 * and 2) redirecting these events to the grabbing widget
1248 * could cause the display to be messed up.
1250 * Drag events are also not redirected, since it isn't
1251 * clear what the semantics of that would be.
1253 switch (event->type)
1259 g_object_ref (event_widget);
1260 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1261 !gtk_widget_event (event_widget, event))
1262 gtk_widget_destroy (event_widget);
1263 g_object_unref (event_widget);
1267 /* Unexpected GDK_DESTROY from the outside, ignore for
1268 * child windows, handle like a GDK_DELETE for toplevels
1270 if (!event_widget->parent)
1272 g_object_ref (event_widget);
1273 if (!gtk_widget_event (event_widget, event) &&
1274 GTK_WIDGET_REALIZED (event_widget))
1275 gtk_widget_destroy (event_widget);
1276 g_object_unref (event_widget);
1281 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1283 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1284 gtk_widget_send_expose (event_widget, event);
1285 gdk_window_end_paint (event->any.window);
1288 gtk_widget_send_expose (event_widget, event);
1291 case GDK_PROPERTY_NOTIFY:
1293 case GDK_FOCUS_CHANGE:
1297 case GDK_SELECTION_CLEAR:
1298 case GDK_SELECTION_REQUEST:
1299 case GDK_SELECTION_NOTIFY:
1300 case GDK_CLIENT_EVENT:
1301 case GDK_VISIBILITY_NOTIFY:
1302 case GDK_WINDOW_STATE:
1303 gtk_widget_event (event_widget, event);
1307 case GDK_BUTTON_PRESS:
1308 case GDK_2BUTTON_PRESS:
1309 case GDK_3BUTTON_PRESS:
1310 gtk_propagate_event (grab_widget, event);
1314 case GDK_KEY_RELEASE:
1317 if (gtk_invoke_key_snoopers (grab_widget, event))
1320 /* else fall through */
1321 case GDK_MOTION_NOTIFY:
1322 case GDK_BUTTON_RELEASE:
1323 case GDK_PROXIMITY_IN:
1324 case GDK_PROXIMITY_OUT:
1325 gtk_propagate_event (grab_widget, event);
1328 case GDK_ENTER_NOTIFY:
1329 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1331 g_object_ref (event_widget);
1333 gtk_widget_event (grab_widget, event);
1334 if (event_widget == grab_widget)
1335 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1337 g_object_unref (event_widget);
1341 case GDK_LEAVE_NOTIFY:
1342 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1344 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1345 gtk_widget_event (event_widget, event);
1347 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1348 gtk_widget_event (grab_widget, event);
1351 case GDK_DRAG_STATUS:
1352 case GDK_DROP_FINISHED:
1353 _gtk_drag_source_handle_event (event_widget, event);
1355 case GDK_DRAG_ENTER:
1356 case GDK_DRAG_LEAVE:
1357 case GDK_DRAG_MOTION:
1358 case GDK_DROP_START:
1359 _gtk_drag_dest_handle_event (event_widget, event);
1362 g_assert_not_reached ();
1366 tmp_list = current_events;
1367 current_events = g_list_remove_link (current_events, tmp_list);
1368 g_list_free_1 (tmp_list);
1370 if (rewritten_event)
1371 gdk_event_free (rewritten_event);
1386 static GtkWindowGroup *
1387 gtk_main_get_window_group (GtkWidget *widget)
1389 GtkWidget *toplevel = NULL;
1392 toplevel = gtk_widget_get_toplevel (widget);
1394 if (toplevel && GTK_IS_WINDOW (toplevel))
1395 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1397 return _gtk_window_get_group (NULL);
1402 GtkWidget *old_grab_widget;
1403 GtkWidget *new_grab_widget;
1407 check_is_grabbed (GtkWidget *widget,
1408 GtkWidget *grab_widget)
1411 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1417 gtk_grab_notify_foreach (GtkWidget *child,
1421 GrabNotifyInfo *info = data;
1422 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1423 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1425 if (was_grabbed != is_grabbed)
1427 g_object_ref (child);
1429 _gtk_widget_grab_notify (child, was_grabbed);
1431 if (GTK_IS_CONTAINER (child))
1432 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1434 g_object_unref (child);
1439 gtk_grab_notify (GtkWindowGroup *group,
1440 GtkWidget *grab_widget,
1441 gboolean was_grabbed)
1444 GrabNotifyInfo info;
1448 info.old_grab_widget = grab_widget;
1449 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1453 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1454 info.new_grab_widget = grab_widget;
1457 g_object_ref (group);
1458 g_object_ref (grab_widget);
1460 toplevels = gtk_window_list_toplevels ();
1461 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1465 GtkWindow *toplevel = toplevels->data;
1466 toplevels = g_list_delete_link (toplevels, toplevels);
1468 if (group == _gtk_window_get_group (toplevel))
1469 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1470 g_object_unref (toplevel);
1473 g_object_unref (group);
1474 g_object_unref (grab_widget);
1478 gtk_grab_add (GtkWidget *widget)
1480 GtkWindowGroup *group;
1481 gboolean was_grabbed;
1483 g_return_if_fail (widget != NULL);
1485 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1487 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1489 group = gtk_main_get_window_group (widget);
1491 was_grabbed = (group->grabs != NULL);
1493 g_object_ref (widget);
1494 group->grabs = g_slist_prepend (group->grabs, widget);
1496 gtk_grab_notify (group, widget, FALSE);
1501 gtk_grab_get_current (void)
1503 GtkWindowGroup *group;
1505 group = gtk_main_get_window_group (NULL);
1508 return GTK_WIDGET (group->grabs->data);
1513 gtk_grab_remove (GtkWidget *widget)
1515 GtkWindowGroup *group;
1517 g_return_if_fail (widget != NULL);
1519 if (GTK_WIDGET_HAS_GRAB (widget))
1521 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1523 group = gtk_main_get_window_group (widget);
1524 group->grabs = g_slist_remove (group->grabs, widget);
1526 g_object_unref (widget);
1528 gtk_grab_notify (group, widget, TRUE);
1533 gtk_init_add (GtkFunction function,
1536 GtkInitFunction *init;
1538 init = g_new (GtkInitFunction, 1);
1539 init->function = function;
1542 init_functions = g_list_prepend (init_functions, init);
1546 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1549 GtkKeySnooperData *data;
1550 static guint snooper_id = 1;
1552 g_return_val_if_fail (snooper != NULL, 0);
1554 data = g_new (GtkKeySnooperData, 1);
1555 data->func = snooper;
1556 data->func_data = func_data;
1557 data->id = snooper_id++;
1558 key_snoopers = g_slist_prepend (key_snoopers, data);
1564 gtk_key_snooper_remove (guint snooper_id)
1566 GtkKeySnooperData *data = NULL;
1569 slist = key_snoopers;
1573 if (data->id == snooper_id)
1576 slist = slist->next;
1581 key_snoopers = g_slist_remove (key_snoopers, data);
1587 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1591 gint return_val = FALSE;
1593 slist = key_snoopers;
1594 while (slist && !return_val)
1596 GtkKeySnooperData *data;
1599 slist = slist->next;
1600 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1607 gtk_quit_add_full (guint main_level,
1608 GtkFunction function,
1609 GtkCallbackMarshal marshal,
1611 GtkDestroyNotify destroy)
1613 static guint quit_id = 1;
1614 GtkQuitFunction *quitf;
1616 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1618 if (!quit_mem_chunk)
1619 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1620 512, G_ALLOC_AND_FREE);
1622 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1624 quitf->id = quit_id++;
1625 quitf->main_level = main_level;
1626 quitf->function = function;
1627 quitf->marshal = marshal;
1629 quitf->destroy = destroy;
1631 quit_functions = g_list_prepend (quit_functions, quitf);
1637 gtk_quit_destroy (GtkQuitFunction *quitf)
1640 quitf->destroy (quitf->data);
1641 g_mem_chunk_free (quit_mem_chunk, quitf);
1645 gtk_quit_destructor (GtkObject **object_p)
1648 gtk_object_destroy (*object_p);
1655 gtk_quit_add_destroy (guint main_level,
1658 GtkObject **object_p;
1660 g_return_if_fail (main_level > 0);
1661 g_return_if_fail (GTK_IS_OBJECT (object));
1663 object_p = g_new (GtkObject*, 1);
1665 g_signal_connect (object,
1667 G_CALLBACK (gtk_widget_destroyed),
1669 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1673 gtk_quit_add (guint main_level,
1674 GtkFunction function,
1677 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1681 gtk_quit_remove (guint id)
1683 GtkQuitFunction *quitf;
1686 tmp_list = quit_functions;
1689 quitf = tmp_list->data;
1691 if (quitf->id == id)
1693 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1694 g_list_free (tmp_list);
1695 gtk_quit_destroy (quitf);
1700 tmp_list = tmp_list->next;
1705 gtk_quit_remove_by_data (gpointer data)
1707 GtkQuitFunction *quitf;
1710 tmp_list = quit_functions;
1713 quitf = tmp_list->data;
1715 if (quitf->data == data)
1717 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1718 g_list_free (tmp_list);
1719 gtk_quit_destroy (quitf);
1724 tmp_list = tmp_list->next;
1729 gtk_timeout_add_full (guint32 interval,
1730 GtkFunction function,
1731 GtkCallbackMarshal marshal,
1733 GtkDestroyNotify destroy)
1737 GtkClosure *closure;
1739 closure = g_new (GtkClosure, 1);
1740 closure->marshal = marshal;
1741 closure->data = data;
1742 closure->destroy = destroy;
1744 return g_timeout_add_full (0, interval,
1745 gtk_invoke_idle_timeout,
1747 gtk_destroy_closure);
1750 return g_timeout_add_full (0, interval, function, data, destroy);
1754 gtk_timeout_add (guint32 interval,
1755 GtkFunction function,
1758 return g_timeout_add_full (0, interval, function, data, NULL);
1762 gtk_timeout_remove (guint tag)
1764 g_source_remove (tag);
1768 gtk_idle_add_full (gint priority,
1769 GtkFunction function,
1770 GtkCallbackMarshal marshal,
1772 GtkDestroyNotify destroy)
1776 GtkClosure *closure;
1778 closure = g_new (GtkClosure, 1);
1779 closure->marshal = marshal;
1780 closure->data = data;
1781 closure->destroy = destroy;
1783 return g_idle_add_full (priority,
1784 gtk_invoke_idle_timeout,
1786 gtk_destroy_closure);
1789 return g_idle_add_full (priority, function, data, destroy);
1793 gtk_idle_add (GtkFunction function,
1796 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1800 gtk_idle_add_priority (gint priority,
1801 GtkFunction function,
1804 return g_idle_add_full (priority, function, data, NULL);
1808 gtk_idle_remove (guint tag)
1810 g_source_remove (tag);
1814 gtk_idle_remove_by_data (gpointer data)
1816 if (!g_idle_remove_by_data (data))
1817 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1821 gtk_input_add_full (gint source,
1822 GdkInputCondition condition,
1823 GdkInputFunction function,
1824 GtkCallbackMarshal marshal,
1826 GtkDestroyNotify destroy)
1830 GtkClosure *closure;
1832 closure = g_new (GtkClosure, 1);
1833 closure->marshal = marshal;
1834 closure->data = data;
1835 closure->destroy = destroy;
1837 return gdk_input_add_full (source,
1839 (GdkInputFunction) gtk_invoke_input,
1841 (GdkDestroyNotify) gtk_destroy_closure);
1844 return gdk_input_add_full (source, condition, function, data, destroy);
1848 gtk_input_remove (guint tag)
1850 g_source_remove (tag);
1854 gtk_destroy_closure (gpointer data)
1856 GtkClosure *closure = data;
1858 if (closure->destroy)
1859 (closure->destroy) (closure->data);
1864 gtk_invoke_idle_timeout (gpointer data)
1866 GtkClosure *closure = data;
1869 gint ret_val = FALSE;
1870 args[0].name = NULL;
1871 args[0].type = G_TYPE_BOOLEAN;
1872 args[0].d.pointer_data = &ret_val;
1873 closure->marshal (NULL, closure->data, 0, args);
1878 gtk_invoke_input (gpointer data,
1880 GdkInputCondition condition)
1882 GtkClosure *closure = data;
1885 args[0].type = G_TYPE_INT;
1886 args[0].name = NULL;
1887 GTK_VALUE_INT (args[0]) = source;
1888 args[1].type = GDK_TYPE_INPUT_CONDITION;
1889 args[1].name = NULL;
1890 GTK_VALUE_FLAGS (args[1]) = condition;
1891 args[2].type = G_TYPE_NONE;
1892 args[2].name = NULL;
1894 closure->marshal (NULL, closure->data, 2, args);
1898 * gtk_get_current_event:
1900 * Obtains a copy of the event currently being processed by GTK+. For
1901 * example, if you get a "clicked" signal from #GtkButton, the current
1902 * event will be the #GdkEventButton that triggered the "clicked"
1903 * signal. The returned event must be freed with gdk_event_free().
1904 * If there is no current event, the function returns %NULL.
1906 * Return value: a copy of the current event, or %NULL if no current event.
1909 gtk_get_current_event (void)
1912 return gdk_event_copy (current_events->data);
1918 * gtk_get_current_event_time:
1920 * If there is a current event and it has a timestamp, return that
1921 * timestamp, otherwise return %GDK_CURRENT_TIME.
1923 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1926 gtk_get_current_event_time (void)
1929 return gdk_event_get_time (current_events->data);
1931 return GDK_CURRENT_TIME;
1935 * gtk_get_current_event_state:
1936 * @state: a location to store the state of the current event
1938 * If there is a current event and it has a state field, place
1939 * that state field in @state and return %TRUE, otherwise return
1942 * Return value: %TRUE if there was a current event and it had a state field
1945 gtk_get_current_event_state (GdkModifierType *state)
1947 g_return_val_if_fail (state != NULL, FALSE);
1950 return gdk_event_get_state (current_events->data, state);
1959 * gtk_get_event_widget:
1960 * @event: a #GdkEvent
1962 * If @event is %NULL or the event was not associated with any widget,
1963 * returns %NULL, otherwise returns the widget that received the event
1966 * Return value: the widget that originally received @event, or %NULL
1969 gtk_get_event_widget (GdkEvent *event)
1974 if (event && event->any.window)
1975 gdk_window_get_user_data (event->any.window, (void**) &widget);
1981 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1983 if (!quitf->marshal)
1984 return quitf->function (quitf->data);
1988 gint ret_val = FALSE;
1990 args[0].name = NULL;
1991 args[0].type = G_TYPE_BOOLEAN;
1992 args[0].d.pointer_data = &ret_val;
1993 ((GtkCallbackMarshal) quitf->marshal) (NULL,
2001 * gtk_propagate_event:
2002 * @widget: a #GtkWidget
2005 * Sends an event to a widget, propagating the event to parent widgets
2006 * if the event remains unhandled. Events received by GTK+ from GDK
2007 * normally begin in gtk_main_do_event(). Depending on the type of
2008 * event, existence of modal dialogs, grabs, etc., the event may be
2009 * propagated; if so, this function is used. gtk_propagate_event()
2010 * calls gtk_widget_event() on each widget it decides to send the
2011 * event to. So gtk_widget_event() is the lowest-level function; it
2012 * simply emits the "event" and possibly an event-specific signal on a
2013 * widget. gtk_propagate_event() is a bit higher-level, and
2014 * gtk_main_do_event() is the highest level.
2016 * All that said, you most likely don't want to use any of these
2017 * functions; synthesizing events is rarely needed. Consider asking on
2018 * the mailing list for better ways to achieve your goals. For
2019 * example, use gdk_window_invalidate_rect() or
2020 * gtk_widget_queue_draw() instead of making up expose events.
2024 gtk_propagate_event (GtkWidget *widget,
2029 g_return_if_fail (GTK_IS_WIDGET (widget));
2030 g_return_if_fail (event != NULL);
2032 handled_event = FALSE;
2034 g_object_ref (widget);
2036 if ((event->type == GDK_KEY_PRESS) ||
2037 (event->type == GDK_KEY_RELEASE))
2039 /* Only send key events within Window widgets to the Window
2040 * The Window widget will in turn pass the
2041 * key event on to the currently focused widget
2046 window = gtk_widget_get_toplevel (widget);
2047 if (window && GTK_IS_WINDOW (window))
2049 /* If there is a grab within the window, give the grab widget
2050 * a first crack at the key event
2052 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
2053 handled_event = gtk_widget_event (widget, event);
2057 window = gtk_widget_get_toplevel (widget);
2058 if (window && GTK_IS_WINDOW (window))
2060 if (GTK_WIDGET_IS_SENSITIVE (window))
2061 gtk_widget_event (window, event);
2065 handled_event = TRUE; /* don't send to widget */
2069 /* Other events get propagated up the widget tree
2070 * so that parents can see the button and motion
2071 * events of the children.
2079 /* Scroll events are special cased here because it
2080 * feels wrong when scrolling a GtkViewport, say,
2081 * to have children of the viewport eat the scroll
2084 if (!GTK_WIDGET_IS_SENSITIVE (widget))
2085 handled_event = event->type != GDK_SCROLL;
2087 handled_event = gtk_widget_event (widget, event);
2089 tmp = widget->parent;
2090 g_object_unref (widget);
2094 if (!handled_event && widget)
2095 g_object_ref (widget);
2101 g_object_unref (widget);
2106 gtk_error (gchar *str)
2112 gtk_warning (gchar *str)
2118 gtk_message (gchar *str)
2124 gtk_print (gchar *str)
2126 static GtkWidget *window = NULL;
2127 static GtkWidget *text;
2128 static int level = 0;
2132 GtkWidget *hscrollbar;
2133 GtkWidget *vscrollbar;
2134 GtkWidget *separator;
2139 fputs (str, stdout);
2146 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2148 gtk_signal_connect (GTK_OBJECT (window), "destroy",
2149 (GtkSignalFunc) gtk_widget_destroyed,
2152 gtk_window_set_title (GTK_WINDOW (window), "Messages");
2154 box1 = gtk_vbox_new (FALSE, 0);
2155 gtk_container_add (GTK_CONTAINER (window), box1);
2156 gtk_widget_show (box1);
2159 box2 = gtk_vbox_new (FALSE, 10);
2160 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2161 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2162 gtk_widget_show (box2);
2165 table = gtk_table_new (2, 2, FALSE);
2166 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
2167 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
2168 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
2169 gtk_widget_show (table);
2171 text = gtk_text_new (NULL, NULL);
2172 gtk_text_set_editable (GTK_TEXT (text), FALSE);
2173 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
2174 gtk_widget_show (text);
2175 gtk_widget_realize (text);
2177 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
2178 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
2179 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
2180 gtk_widget_show (hscrollbar);
2182 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
2183 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
2184 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
2185 gtk_widget_show (vscrollbar);
2187 separator = gtk_hseparator_new ();
2188 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2189 gtk_widget_show (separator);
2192 box2 = gtk_vbox_new (FALSE, 10);
2193 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
2194 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2195 gtk_widget_show (box2);
2198 button = gtk_button_new_with_label ("close");
2199 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2200 (GtkSignalFunc) gtk_widget_hide,
2201 GTK_OBJECT (window));
2202 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2203 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2204 gtk_widget_grab_default (button);
2205 gtk_widget_show (button);
2209 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
2212 if (!GTK_WIDGET_VISIBLE (window))
2213 gtk_widget_show (window);
2218 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
2219 GValue *return_accu,
2220 const GValue *handler_return,
2223 gboolean continue_emission;
2224 gboolean signal_handled;
2226 signal_handled = g_value_get_boolean (handler_return);
2227 g_value_set_boolean (return_accu, signal_handled);
2228 continue_emission = !signal_handled;
2230 return continue_emission;