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/.
27 #include "gdkconfig.h"
31 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
41 #include <sys/types.h> /* For uid_t, gid_t */
49 #include <pango/pango-utils.h> /* For pango_split_file_list */
51 #include "gtkaccelmap.h"
53 #include "gtkversion.h"
56 #include "gtkselection.h"
57 #include "gtksettings.h"
58 #include "gtksignal.h"
59 #include "gtkwidget.h"
60 #include "gtkwindow.h"
61 #include "gtkprivate.h"
62 #include "gdk/gdki18n.h"
67 /* Private type definitions
69 typedef struct _GtkInitFunction GtkInitFunction;
70 typedef struct _GtkQuitFunction GtkQuitFunction;
71 typedef struct _GtkClosure GtkClosure;
72 typedef struct _GtkKeySnooperData GtkKeySnooperData;
74 struct _GtkInitFunction
80 struct _GtkQuitFunction
84 GtkCallbackMarshal marshal;
87 GtkDestroyNotify destroy;
92 GtkCallbackMarshal marshal;
94 GtkDestroyNotify destroy;
97 struct _GtkKeySnooperData
104 static void gtk_exit_func (void);
105 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
106 static void gtk_quit_destroy (GtkQuitFunction *quitf);
107 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
110 static void gtk_destroy_closure (gpointer data);
111 static gboolean gtk_invoke_idle_timeout (gpointer data);
112 static void gtk_invoke_input (gpointer data,
114 GdkInputCondition condition);
117 static void gtk_error (gchar *str);
118 static void gtk_warning (gchar *str);
119 static void gtk_message (gchar *str);
120 static void gtk_print (gchar *str);
123 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
125 const guint gtk_major_version = GTK_MAJOR_VERSION;
126 const guint gtk_minor_version = GTK_MINOR_VERSION;
127 const guint gtk_micro_version = GTK_MICRO_VERSION;
128 const guint gtk_binary_age = GTK_BINARY_AGE;
129 const guint gtk_interface_age = GTK_INTERFACE_AGE;
131 static guint gtk_main_loop_level = 0;
132 static gint gtk_initialized = FALSE;
133 static GList *current_events = NULL;
135 static GSList *main_loops = NULL; /* stack of currently executing main loops */
137 static GList *init_functions = NULL; /* A list of init functions.
139 static GList *quit_functions = NULL; /* A list of quit functions.
141 static GMemChunk *quit_mem_chunk = NULL;
143 static GSList *key_snoopers = NULL;
145 static GdkVisual *gtk_visual; /* The visual to be used in creating new
148 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
152 guint gtk_debug_flags = 0; /* Global GTK debug flag */
154 #ifdef G_ENABLE_DEBUG
155 static const GDebugKey gtk_debug_keys[] = {
156 {"misc", GTK_DEBUG_MISC},
157 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
158 {"text", GTK_DEBUG_TEXT},
159 {"tree", GTK_DEBUG_TREE},
160 {"updates", GTK_DEBUG_UPDATES}
163 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
165 #endif /* G_ENABLE_DEBUG */
168 gtk_check_version (guint required_major,
169 guint required_minor,
170 guint required_micro)
172 if (required_major > GTK_MAJOR_VERSION)
173 return "Gtk+ version too old (major mismatch)";
174 if (required_major < GTK_MAJOR_VERSION)
175 return "Gtk+ version too new (major mismatch)";
176 if (required_minor > GTK_MINOR_VERSION)
177 return "Gtk+ version too old (minor mismatch)";
178 if (required_minor < GTK_MINOR_VERSION)
179 return "Gtk+ version too new (minor mismatch)";
180 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
181 return "Gtk+ version too new (micro mismatch)";
182 if (required_micro > GTK_MICRO_VERSION)
183 return "Gtk+ version too old (micro mismatch)";
187 #undef gtk_init_check
189 /* This checks to see if the process is running suid or sgid
190 * at the current time. If so, we don't allow GTK+ to be initialized.
191 * This is meant to be a mild check - we only error out if we
192 * can prove the programmer is doing something wrong, not if
193 * they could be doing something wrong. For this reason, we
194 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
199 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
201 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
202 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
204 #ifdef HAVE_GETRESUID
205 /* These aren't in the header files, so we prototype them here.
207 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
208 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
210 if (getresuid (&ruid, &euid, &suid) != 0 ||
211 getresgid (&rgid, &egid, &sgid) != 0)
212 #endif /* HAVE_GETRESUID */
214 suid = ruid = getuid ();
215 sgid = rgid = getgid ();
220 if (ruid != euid || ruid != suid ||
221 rgid != egid || rgid != sgid)
223 g_warning ("This process is currently running setuid or setgid.\n"
224 "This is not a supported use of GTK+. You must create a helper\n"
225 "program instead. For further details, see:\n\n"
226 " http://www.gtk.org/setuid.html\n\n"
227 "Refusing to initialize GTK+.");
236 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
239 _gtk_get_libdir (void)
241 static char *gtk_libdir = NULL;
242 if (gtk_libdir == NULL)
243 gtk_libdir = g_win32_get_package_installation_subdirectory
244 (GETTEXT_PACKAGE, dll_name, "lib");
250 _gtk_get_localedir (void)
252 static char *gtk_localedir = NULL;
253 if (gtk_localedir == NULL)
254 gtk_localedir = g_win32_get_package_installation_subdirectory
255 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
257 return gtk_localedir;
261 _gtk_get_sysconfdir (void)
263 static char *gtk_sysconfdir = NULL;
264 if (gtk_sysconfdir == NULL)
265 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
266 (GETTEXT_PACKAGE, dll_name, "etc");
268 return gtk_sysconfdir;
272 _gtk_get_data_prefix (void)
274 static char *gtk_data_prefix = NULL;
275 if (gtk_data_prefix == NULL)
276 gtk_data_prefix = g_win32_get_package_installation_directory
277 (GETTEXT_PACKAGE, dll_name);
279 return gtk_data_prefix;
282 #endif /* G_OS_WIN32 */
285 get_module_path (void)
287 const gchar *module_path_env = g_getenv ("GTK_MODULE_PATH");
288 const gchar *exe_prefix = g_getenv ("GTK_EXE_PREFIX");
294 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", "modules", NULL);
296 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", "modules", NULL);
298 module_path = g_strconcat (module_path_env ? module_path_env : "",
299 module_path_env ? G_SEARCHPATH_SEPARATOR_S : "",
302 result = pango_split_file_list (module_path);
304 g_free (default_dir);
305 g_free (module_path);
311 find_module (gchar **module_path,
318 if (g_path_is_absolute (name))
319 return g_module_open (name, G_MODULE_BIND_LAZY);
321 for (i = 0; module_path[i]; i++)
323 gchar *version_directory;
325 version_directory = g_build_filename (module_path[i], GTK_BINARY_VERSION, NULL);
326 module_name = g_module_build_path (version_directory, name);
327 g_free (version_directory);
329 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
331 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
332 g_free (module_name);
336 g_free (module_name);
338 module_name = g_module_build_path (module_path[i], name);
340 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
342 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
343 g_free (module_name);
347 g_free (module_name);
350 /* As last resort, try loading without an absolute path (using system
353 module_name = g_module_build_path (NULL, name);
354 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
361 load_module (GSList *gtk_modules,
365 GtkModuleInitFunc modinit_func = NULL;
366 GModule *module = NULL;
368 if (g_module_supported ())
370 module = find_module (module_path, name);
372 g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
375 if (!g_slist_find (gtk_modules, modinit_func))
377 g_module_make_resident (module);
378 gtk_modules = g_slist_prepend (gtk_modules, modinit_func);
382 g_module_close (module);
389 g_message ("Failed to load module \"%s\": %s",
390 module ? g_module_name (module) : name,
393 g_module_close (module);
400 load_modules (const char *module_str)
402 gchar **module_path = get_module_path ();
403 gchar **module_names = pango_split_file_list (module_str);
404 GSList *gtk_modules = NULL;
407 for (i = 0; module_names[i]; i++)
408 gtk_modules = load_module (gtk_modules, module_path, module_names[i]);
410 gtk_modules = g_slist_reverse (gtk_modules);
412 g_strfreev (module_names);
413 g_strfreev (module_path);
418 static gboolean do_setlocale = TRUE;
421 * gtk_disable_setlocale:
423 * Prevents gtk_init() and gtk_init_check() from automatically
424 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
425 * want to use this function if you wanted to set the locale for
426 * your program to something other than the user's locale, or if
427 * you wanted to set different values for different locale categories.
429 * Most programs should not need to call this function.
432 gtk_disable_setlocale (void)
435 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
437 do_setlocale = FALSE;
441 gtk_init_check (int *argc,
444 GString *gtk_modules_string = NULL;
445 GSList *gtk_modules = NULL;
452 if (!check_setugid ())
456 g_set_error_handler (gtk_error);
457 g_set_warning_handler (gtk_warning);
458 g_set_message_handler (gtk_message);
459 g_set_print_handler (gtk_print);
463 setlocale (LC_ALL, "");
465 /* Initialize "gdk". We pass along the 'argc' and 'argv'
466 * parameters as they contain information that GDK uses
468 if (!gdk_init_check (argc, argv))
471 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
473 #ifdef G_ENABLE_DEBUG
474 env_string = g_getenv ("GTK_DEBUG");
475 if (env_string != NULL)
477 gtk_debug_flags = g_parse_debug_string (env_string,
482 #endif /* G_ENABLE_DEBUG */
484 env_string = g_getenv ("GTK_MODULES");
486 gtk_modules_string = g_string_new (env_string);
492 for (i = 1; i < *argc;)
494 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
495 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
497 gchar *module_name = (*argv)[i] + 12;
499 if (*module_name == '=')
501 else if (i + 1 < *argc)
505 module_name = (*argv)[i];
509 if (module_name && *module_name)
511 if (gtk_modules_string)
512 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
514 gtk_modules_string = g_string_new (NULL);
516 g_string_append (gtk_modules_string, module_name);
519 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
521 GLogLevelFlags fatal_mask;
523 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
524 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
525 g_log_set_always_fatal (fatal_mask);
528 #ifdef G_ENABLE_DEBUG
529 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
530 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
532 gchar *equal_pos = strchr ((*argv)[i], '=');
534 if (equal_pos != NULL)
536 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
540 else if ((i + 1) < *argc && (*argv)[i + 1])
542 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
550 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
551 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
553 gchar *equal_pos = strchr ((*argv)[i], '=');
555 if (equal_pos != NULL)
557 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
561 else if ((i + 1) < *argc && (*argv)[i + 1])
563 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
571 #endif /* G_ENABLE_DEBUG */
575 for (i = 1; i < *argc; i++)
577 for (k = i; k < *argc; k++)
578 if ((*argv)[k] != NULL)
584 for (j = i + k; j < *argc; j++)
585 (*argv)[j-k] = (*argv)[j];
591 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
592 gdk_window_set_debug_updates (TRUE);
594 /* load gtk modules */
595 if (gtk_modules_string)
597 gtk_modules = load_modules (gtk_modules_string->str);
598 g_string_free (gtk_modules_string, TRUE);
602 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
603 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
604 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
609 /* Translate to default:RTL if you want your widgets
610 * to be RTL, otherwise translate to default:LTR.
611 * Do *not* translate it to "predefinito:LTR", if it
612 * it isn't default:LTR or default:RTL it will not work
614 char *e = _("default:LTR");
615 if (strcmp (e, "default:RTL")==0) {
616 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
617 } else if (strcmp (e, "default:LTR")) {
618 g_warning ("Whoever translated default:LTR did so wrongly.\n");
622 /* Initialize the default visual and colormap to be
623 * used in creating widgets. (We want to use the system
624 * defaults so as to be nice to the colormap).
626 gtk_visual = gdk_visual_get_system ();
627 gtk_colormap = gdk_colormap_get_system ();
630 _gtk_accel_map_init ();
633 /* Register an exit function to make sure we are able to cleanup.
635 g_atexit (gtk_exit_func);
637 /* Set the 'initialized' flag.
639 gtk_initialized = TRUE;
641 /* initialize gtk modules
643 for (slist = gtk_modules; slist; slist = slist->next)
647 GtkModuleInitFunc modinit;
649 modinit = slist->data;
650 modinit (argc, argv);
653 g_slist_free (gtk_modules);
656 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
657 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
658 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
659 " if you don't know why you are getting this, you probably want to\n"
660 " use the stable branch which can be retrieved from\n"
661 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
662 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
671 gtk_init (int *argc, char ***argv)
673 if (!gtk_init_check (argc, argv))
675 g_warning ("cannot open display: %s", gdk_get_display ());
683 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
685 if (sizeof_GtkWindow != sizeof (GtkWindow))
686 g_error ("Incompatible build!\n"
687 "The code using GTK+ thinks GtkWindow is of different\n"
688 "size than it actually is in this build of GTK+.\n"
689 "On Windows, this probably means that you have compiled\n"
690 "your code with gcc without the -fnative-struct switch.");
693 /* These two functions might get more checks added later, thus pass
694 * in the number of extra args.
697 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
699 check_sizeof_GtkWindow (sizeof_GtkWindow);
700 gtk_init (argc, argv);
704 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
706 check_sizeof_GtkWindow (sizeof_GtkWindow);
707 return gtk_init_check (argc, argv);
713 gtk_exit (gint errorcode)
715 /* Only if "gtk" has been initialized should we de-initialize.
717 /* de-initialisation is done by the gtk_exit_funct(),
718 * no need to do this here (Alex J.)
720 gdk_exit (errorcode);
727 * Initializes internationalization support for GTK+. gtk_init()
728 * automatically does this, so there is typically no point
729 * in calling this function.
731 * If you are calling this function because you changed the locale
732 * after GTK+ is was initialized, then calling this function
733 * may help a bit. (Note, however, that changing the locale
734 * after GTK+ is initialized may produce inconsistent results and
735 * is not really supported.)
737 * In detail - sets the current locale according to the
738 * program environment. This is the same as calling the C library function
739 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
740 * locale specific setup of the windowing system used by GDK.
742 * Return value: a string corresponding to the locale set, as with the
743 * C library function <function>setlocale()</function>.
746 gtk_set_locale (void)
748 return gdk_set_locale ();
752 * gtk_get_default_language:
754 * Returns the ISO language code for the default language currently in
755 * effect. (Note that this can change over the life of an
756 * application.) The default language is derived from the current
757 * locale. It determines, for example, whether GTK+ uses the
758 * right-to-left or left-to-right text direction.
760 * Return value: the default language as an allocated string, must be freed
763 gtk_get_default_language (void)
766 PangoLanguage *result;
769 lang = g_strdup (setlocale (LC_CTYPE, NULL));
770 p = strchr (lang, '.');
773 p = strchr (lang, '@');
777 result = pango_language_from_string (lang);
788 GtkInitFunction *init;
791 gtk_main_loop_level++;
793 loop = g_main_new (TRUE);
794 main_loops = g_slist_prepend (main_loops, loop);
796 tmp_list = functions = init_functions;
797 init_functions = NULL;
801 init = tmp_list->data;
802 tmp_list = tmp_list->next;
804 (* init->function) (init->data);
807 g_list_free (functions);
809 if (g_main_is_running (main_loops->data))
811 GDK_THREADS_LEAVE ();
813 GDK_THREADS_ENTER ();
819 GList *reinvoke_list = NULL;
820 GtkQuitFunction *quitf;
822 while (quit_functions)
824 quitf = quit_functions->data;
826 tmp_list = quit_functions;
827 quit_functions = g_list_remove_link (quit_functions, quit_functions);
828 g_list_free_1 (tmp_list);
830 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
831 gtk_quit_invoke_function (quitf))
833 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
837 gtk_quit_destroy (quitf);
844 work = g_list_last (reinvoke_list);
846 quit_functions->prev = work;
847 work->next = quit_functions;
848 quit_functions = work;
854 main_loops = g_slist_remove (main_loops, loop);
856 g_main_destroy (loop);
858 gtk_main_loop_level--;
862 gtk_main_level (void)
864 return gtk_main_loop_level;
870 g_return_if_fail (main_loops != NULL);
872 g_main_quit (main_loops->data);
876 gtk_events_pending (void)
880 GDK_THREADS_LEAVE ();
881 result = g_main_pending ();
882 GDK_THREADS_ENTER ();
888 gtk_main_iteration (void)
890 GDK_THREADS_LEAVE ();
891 g_main_iteration (TRUE);
892 GDK_THREADS_ENTER ();
895 return !g_main_is_running (main_loops->data);
901 gtk_main_iteration_do (gboolean blocking)
903 GDK_THREADS_LEAVE ();
904 g_main_iteration (blocking);
905 GDK_THREADS_ENTER ();
908 return !g_main_is_running (main_loops->data);
914 gtk_main_do_event (GdkEvent *event)
916 GtkWidget *event_widget;
917 GtkWidget *grab_widget;
918 GtkWindowGroup *window_group;
919 GdkEvent *next_event;
922 /* If there are any events pending then get the next one.
924 next_event = gdk_event_peek ();
926 /* Try to compress enter/leave notify events. These event
927 * pairs occur when the mouse is dragged quickly across
928 * a window with many buttons (or through a menu). Instead
929 * of highlighting and de-highlighting each widget that
930 * is crossed it is better to simply de-highlight the widget
931 * which contained the mouse initially and highlight the
932 * widget which ends up containing the mouse.
935 if (((event->type == GDK_ENTER_NOTIFY) ||
936 (event->type == GDK_LEAVE_NOTIFY)) &&
937 ((next_event->type == GDK_ENTER_NOTIFY) ||
938 (next_event->type == GDK_LEAVE_NOTIFY)) &&
939 (next_event->type != event->type) &&
940 (next_event->any.window == event->any.window))
942 /* Throw both the peeked copy and the queued copy away
944 gdk_event_free (next_event);
945 next_event = gdk_event_get ();
946 gdk_event_free (next_event);
952 gdk_event_free (next_event);
954 /* Find the widget which got the event. We store the widget
955 * in the user_data field of GdkWindow's.
956 * Ignore the event if we don't have a widget for it, except
957 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
958 * Though this happens rarely, bogus events can occour
959 * for e.g. destroyed GdkWindows.
961 event_widget = gtk_get_event_widget (event);
964 /* To handle selection INCR transactions, we select
965 * PropertyNotify events on the requestor window and create
966 * a corresponding (fake) GdkWindow so that events get
967 * here. There won't be a widget though, so we have to handle
970 if (event->type == GDK_PROPERTY_NOTIFY)
971 gtk_selection_incr_event (event->any.window,
973 else if (event->type == GDK_SETTING)
974 _gtk_settings_handle_event (&event->setting);
979 /* Push the event onto a stack of current events for
980 * gtk_current_event_get().
982 current_events = g_list_prepend (current_events, event);
984 window_group = gtk_main_get_window_group (event_widget);
986 /* If there is a grab in effect...
988 if (window_group->grabs)
990 grab_widget = window_group->grabs->data;
992 /* If the grab widget is an ancestor of the event widget
993 * then we send the event to the original event widget.
994 * This is the key to implementing modality.
996 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
997 gtk_widget_is_ancestor (event_widget, grab_widget))
998 grab_widget = event_widget;
1002 grab_widget = event_widget;
1005 /* Not all events get sent to the grabbing widget.
1006 * The delete, destroy, expose, focus change and resize
1007 * events still get sent to the event widget because
1008 * 1) these events have no meaning for the grabbing widget
1009 * and 2) redirecting these events to the grabbing widget
1010 * could cause the display to be messed up.
1012 * Drag events are also not redirected, since it isn't
1013 * clear what the semantics of that would be.
1015 switch (event->type)
1021 gtk_widget_ref (event_widget);
1022 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1023 !gtk_widget_event (event_widget, event))
1024 gtk_widget_destroy (event_widget);
1025 gtk_widget_unref (event_widget);
1029 /* Unexpected GDK_DESTROY from the outside, ignore for
1030 * child windows, handle like a GDK_DELETE for toplevels
1032 if (!event_widget->parent)
1034 gtk_widget_ref (event_widget);
1035 if (!gtk_widget_event (event_widget, event) &&
1036 GTK_WIDGET_REALIZED (event_widget))
1037 gtk_widget_destroy (event_widget);
1038 gtk_widget_unref (event_widget);
1043 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1045 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1046 gtk_widget_send_expose (event_widget, event);
1047 gdk_window_end_paint (event->any.window);
1050 gtk_widget_send_expose (event_widget, event);
1053 case GDK_PROPERTY_NOTIFY:
1055 case GDK_FOCUS_CHANGE:
1059 case GDK_SELECTION_CLEAR:
1060 case GDK_SELECTION_REQUEST:
1061 case GDK_SELECTION_NOTIFY:
1062 case GDK_CLIENT_EVENT:
1063 case GDK_VISIBILITY_NOTIFY:
1064 case GDK_WINDOW_STATE:
1065 gtk_widget_event (event_widget, event);
1069 case GDK_BUTTON_PRESS:
1070 case GDK_2BUTTON_PRESS:
1071 case GDK_3BUTTON_PRESS:
1072 gtk_propagate_event (grab_widget, event);
1076 case GDK_KEY_RELEASE:
1079 if (gtk_invoke_key_snoopers (grab_widget, event))
1082 /* else fall through */
1083 case GDK_MOTION_NOTIFY:
1084 case GDK_BUTTON_RELEASE:
1085 case GDK_PROXIMITY_IN:
1086 case GDK_PROXIMITY_OUT:
1087 gtk_propagate_event (grab_widget, event);
1090 case GDK_ENTER_NOTIFY:
1091 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1093 g_object_ref (event_widget);
1095 gtk_widget_event (grab_widget, event);
1096 if (event_widget == grab_widget)
1097 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1099 g_object_unref (event_widget);
1103 case GDK_LEAVE_NOTIFY:
1104 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1106 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1107 gtk_widget_event (event_widget, event);
1109 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1110 gtk_widget_event (grab_widget, event);
1113 case GDK_DRAG_STATUS:
1114 case GDK_DROP_FINISHED:
1115 _gtk_drag_source_handle_event (event_widget, event);
1117 case GDK_DRAG_ENTER:
1118 case GDK_DRAG_LEAVE:
1119 case GDK_DRAG_MOTION:
1120 case GDK_DROP_START:
1121 _gtk_drag_dest_handle_event (event_widget, event);
1124 g_assert_not_reached ();
1128 tmp_list = current_events;
1129 current_events = g_list_remove_link (current_events, tmp_list);
1130 g_list_free_1 (tmp_list);
1145 static GtkWindowGroup *
1146 gtk_main_get_window_group (GtkWidget *widget)
1148 GtkWidget *toplevel = NULL;
1151 toplevel = gtk_widget_get_toplevel (widget);
1153 if (toplevel && GTK_IS_WINDOW (toplevel))
1154 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1156 return _gtk_window_get_group (NULL);
1161 GtkWidget *old_grab_widget;
1162 GtkWidget *new_grab_widget;
1166 check_is_grabbed (GtkWidget *widget,
1167 GtkWidget *grab_widget)
1170 return !(widget == grab_widget || gtk_widget_is_ancestor (widget, grab_widget));
1176 gtk_grab_notify_foreach (GtkWidget *child,
1180 GrabNotifyInfo *info = data;
1181 gboolean was_grabbed = check_is_grabbed (child, info->old_grab_widget);
1182 gboolean is_grabbed = check_is_grabbed (child, info->new_grab_widget);
1184 if (was_grabbed != is_grabbed)
1186 g_object_ref (G_OBJECT (child));
1188 gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", was_grabbed);
1190 if (GTK_IS_CONTAINER (child))
1191 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1193 g_object_unref (G_OBJECT (child));
1198 gtk_grab_notify (GtkWindowGroup *group,
1199 GtkWidget *grab_widget,
1200 gboolean was_grabbed)
1203 GrabNotifyInfo info;
1207 info.old_grab_widget = grab_widget;
1208 info.new_grab_widget = group->grabs ? group->grabs->data : NULL;
1212 info.old_grab_widget = (group->grabs && group->grabs->next) ? group->grabs->next->data : NULL;
1213 info.new_grab_widget = grab_widget;
1216 g_object_ref (group);
1217 g_object_ref (grab_widget);
1219 toplevels = gtk_window_list_toplevels ();
1220 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1224 GtkWindow *toplevel = toplevels->data;
1225 toplevels = g_list_delete_link (toplevels, toplevels);
1227 if (group == _gtk_window_get_group (toplevel))
1228 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1229 g_object_unref (toplevel);
1232 g_object_unref (group);
1233 g_object_unref (grab_widget);
1237 gtk_grab_add (GtkWidget *widget)
1239 GtkWindowGroup *group;
1240 gboolean was_grabbed;
1242 g_return_if_fail (widget != NULL);
1244 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1246 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1248 group = gtk_main_get_window_group (widget);
1250 was_grabbed = (group->grabs != NULL);
1252 gtk_widget_ref (widget);
1253 group->grabs = g_slist_prepend (group->grabs, widget);
1255 gtk_grab_notify (group, widget, FALSE);
1260 gtk_grab_get_current (void)
1262 GtkWindowGroup *group;
1264 group = gtk_main_get_window_group (NULL);
1267 return GTK_WIDGET (group->grabs->data);
1272 gtk_grab_remove (GtkWidget *widget)
1274 GtkWindowGroup *group;
1276 g_return_if_fail (widget != NULL);
1278 if (GTK_WIDGET_HAS_GRAB (widget))
1280 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1282 group = gtk_main_get_window_group (widget);
1283 group->grabs = g_slist_remove (group->grabs, widget);
1285 gtk_widget_unref (widget);
1287 gtk_grab_notify (group, widget, TRUE);
1292 gtk_init_add (GtkFunction function,
1295 GtkInitFunction *init;
1297 init = g_new (GtkInitFunction, 1);
1298 init->function = function;
1301 init_functions = g_list_prepend (init_functions, init);
1305 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1308 GtkKeySnooperData *data;
1309 static guint snooper_id = 1;
1311 g_return_val_if_fail (snooper != NULL, 0);
1313 data = g_new (GtkKeySnooperData, 1);
1314 data->func = snooper;
1315 data->func_data = func_data;
1316 data->id = snooper_id++;
1317 key_snoopers = g_slist_prepend (key_snoopers, data);
1323 gtk_key_snooper_remove (guint snooper_id)
1325 GtkKeySnooperData *data = NULL;
1328 slist = key_snoopers;
1332 if (data->id == snooper_id)
1335 slist = slist->next;
1339 key_snoopers = g_slist_remove (key_snoopers, data);
1343 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1347 gint return_val = FALSE;
1349 slist = key_snoopers;
1350 while (slist && !return_val)
1352 GtkKeySnooperData *data;
1355 slist = slist->next;
1356 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1363 gtk_quit_add_full (guint main_level,
1364 GtkFunction function,
1365 GtkCallbackMarshal marshal,
1367 GtkDestroyNotify destroy)
1369 static guint quit_id = 1;
1370 GtkQuitFunction *quitf;
1372 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1374 if (!quit_mem_chunk)
1375 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1376 512, G_ALLOC_AND_FREE);
1378 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1380 quitf->id = quit_id++;
1381 quitf->main_level = main_level;
1382 quitf->function = function;
1383 quitf->marshal = marshal;
1385 quitf->destroy = destroy;
1387 quit_functions = g_list_prepend (quit_functions, quitf);
1393 gtk_quit_destroy (GtkQuitFunction *quitf)
1396 quitf->destroy (quitf->data);
1397 g_mem_chunk_free (quit_mem_chunk, quitf);
1401 gtk_quit_destructor (GtkObject **object_p)
1404 gtk_object_destroy (*object_p);
1411 gtk_quit_add_destroy (guint main_level,
1414 GtkObject **object_p;
1416 g_return_if_fail (main_level > 0);
1417 g_return_if_fail (GTK_IS_OBJECT (object));
1419 object_p = g_new (GtkObject*, 1);
1421 gtk_signal_connect (object,
1423 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1425 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1429 gtk_quit_add (guint main_level,
1430 GtkFunction function,
1433 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1437 gtk_quit_remove (guint id)
1439 GtkQuitFunction *quitf;
1442 tmp_list = quit_functions;
1445 quitf = tmp_list->data;
1447 if (quitf->id == id)
1449 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1450 g_list_free (tmp_list);
1451 gtk_quit_destroy (quitf);
1456 tmp_list = tmp_list->next;
1461 gtk_quit_remove_by_data (gpointer data)
1463 GtkQuitFunction *quitf;
1466 tmp_list = quit_functions;
1469 quitf = tmp_list->data;
1471 if (quitf->data == data)
1473 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1474 g_list_free (tmp_list);
1475 gtk_quit_destroy (quitf);
1480 tmp_list = tmp_list->next;
1485 gtk_timeout_add_full (guint32 interval,
1486 GtkFunction function,
1487 GtkCallbackMarshal marshal,
1489 GtkDestroyNotify destroy)
1493 GtkClosure *closure;
1495 closure = g_new (GtkClosure, 1);
1496 closure->marshal = marshal;
1497 closure->data = data;
1498 closure->destroy = destroy;
1500 return g_timeout_add_full (0, interval,
1501 gtk_invoke_idle_timeout,
1503 gtk_destroy_closure);
1506 return g_timeout_add_full (0, interval, function, data, destroy);
1510 gtk_timeout_add (guint32 interval,
1511 GtkFunction function,
1514 return g_timeout_add_full (0, interval, function, data, NULL);
1518 gtk_timeout_remove (guint tag)
1520 g_source_remove (tag);
1524 gtk_idle_add_full (gint priority,
1525 GtkFunction function,
1526 GtkCallbackMarshal marshal,
1528 GtkDestroyNotify destroy)
1532 GtkClosure *closure;
1534 closure = g_new (GtkClosure, 1);
1535 closure->marshal = marshal;
1536 closure->data = data;
1537 closure->destroy = destroy;
1539 return g_idle_add_full (priority,
1540 gtk_invoke_idle_timeout,
1542 gtk_destroy_closure);
1545 return g_idle_add_full (priority, function, data, destroy);
1549 gtk_idle_add (GtkFunction function,
1552 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1556 gtk_idle_add_priority (gint priority,
1557 GtkFunction function,
1560 return g_idle_add_full (priority, function, data, NULL);
1564 gtk_idle_remove (guint tag)
1566 g_source_remove (tag);
1570 gtk_idle_remove_by_data (gpointer data)
1572 if (!g_idle_remove_by_data (data))
1573 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1577 gtk_input_add_full (gint source,
1578 GdkInputCondition condition,
1579 GdkInputFunction function,
1580 GtkCallbackMarshal marshal,
1582 GtkDestroyNotify destroy)
1586 GtkClosure *closure;
1588 closure = g_new (GtkClosure, 1);
1589 closure->marshal = marshal;
1590 closure->data = data;
1591 closure->destroy = destroy;
1593 return gdk_input_add_full (source,
1595 (GdkInputFunction) gtk_invoke_input,
1597 (GdkDestroyNotify) gtk_destroy_closure);
1600 return gdk_input_add_full (source, condition, function, data, destroy);
1604 gtk_input_remove (guint tag)
1606 g_source_remove (tag);
1610 gtk_destroy_closure (gpointer data)
1612 GtkClosure *closure = data;
1614 if (closure->destroy)
1615 (closure->destroy) (closure->data);
1620 gtk_invoke_idle_timeout (gpointer data)
1622 GtkClosure *closure = data;
1625 gint ret_val = FALSE;
1626 args[0].name = NULL;
1627 args[0].type = GTK_TYPE_BOOL;
1628 args[0].d.pointer_data = &ret_val;
1629 closure->marshal (NULL, closure->data, 0, args);
1634 gtk_invoke_input (gpointer data,
1636 GdkInputCondition condition)
1638 GtkClosure *closure = data;
1641 args[0].type = GTK_TYPE_INT;
1642 args[0].name = NULL;
1643 GTK_VALUE_INT (args[0]) = source;
1644 args[1].type = GDK_TYPE_INPUT_CONDITION;
1645 args[1].name = NULL;
1646 GTK_VALUE_FLAGS (args[1]) = condition;
1647 args[2].type = GTK_TYPE_NONE;
1648 args[2].name = NULL;
1650 closure->marshal (NULL, closure->data, 2, args);
1654 * gtk_get_current_event:
1656 * Obtains a copy of the event currently being processed by GTK+. For
1657 * example, if you get a "clicked" signal from #GtkButton, the current
1658 * event will be the #GdkEventButton that triggered the "clicked"
1659 * signal. The returned event must be freed with gdk_event_free().
1660 * If there is no current event, the function returns %NULL.
1662 * Return value: a copy of the current event, or %NULL if no current event.
1665 gtk_get_current_event (void)
1668 return gdk_event_copy (current_events->data);
1674 * gtk_get_current_event_time:
1676 * If there is a current event and it has a timestamp, return that
1677 * timestamp, otherwise return %GDK_CURRENT_TIME.
1679 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1682 gtk_get_current_event_time (void)
1685 return gdk_event_get_time (current_events->data);
1687 return GDK_CURRENT_TIME;
1691 * gtk_get_current_event_state:
1692 * @state: a location to store the state of the current event
1694 * If there is a current event and it has a state field, place
1695 * that state field in @state and return %TRUE, otherwise return
1698 * Return value: %TRUE if there was a current event and it had a state field
1701 gtk_get_current_event_state (GdkModifierType *state)
1703 g_return_val_if_fail (state != NULL, FALSE);
1706 return gdk_event_get_state (current_events->data, state);
1715 * gtk_get_event_widget:
1716 * @event: a #GdkEvent
1718 * If @event is %NULL or the event was not associated with any widget,
1719 * returns %NULL, otherwise returns the widget that received the event
1722 * Return value: the widget that originally received @event, or %NULL
1725 gtk_get_event_widget (GdkEvent *event)
1730 if (event && event->any.window)
1731 gdk_window_get_user_data (event->any.window, (void**) &widget);
1737 gtk_exit_func (void)
1739 if (gtk_initialized)
1741 gtk_initialized = FALSE;
1747 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1749 if (!quitf->marshal)
1750 return quitf->function (quitf->data);
1754 gint ret_val = FALSE;
1756 args[0].name = NULL;
1757 args[0].type = GTK_TYPE_BOOL;
1758 args[0].d.pointer_data = &ret_val;
1759 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1767 * gtk_propagate_event:
1768 * @widget: a #GtkWidget
1771 * Sends an event to a widget, propagating the event to parent widgets
1772 * if the event remains unhandled. Events received by GTK+ from GDK
1773 * normally begin in gtk_main_do_event(). Depending on the type of
1774 * event, existence of modal dialogs, grabs, etc., the event may be
1775 * propagated; if so, this function is used. gtk_propagate_event()
1776 * calls gtk_widget_event() on each widget it decides to send the
1777 * event to. So gtk_widget_event() is the lowest-level function; it
1778 * simply emits the "event" and possibly an event-specific signal on a
1779 * widget. gtk_propagate_event() is a bit higher-level, and
1780 * gtk_main_do_event() is the highest level.
1782 * All that said, you most likely don't want to use any of these
1783 * functions; synthesizing events is rarely needed. Consider asking on
1784 * the mailing list for better ways to achieve your goals. For
1785 * example, use gdk_window_invalidate_rect() or
1786 * gtk_widget_queue_draw() instead of making up expose events.
1790 gtk_propagate_event (GtkWidget *widget,
1795 g_return_if_fail (GTK_IS_WIDGET (widget));
1796 g_return_if_fail (event != NULL);
1798 handled_event = FALSE;
1800 gtk_widget_ref (widget);
1802 if ((event->type == GDK_KEY_PRESS) ||
1803 (event->type == GDK_KEY_RELEASE))
1805 /* Only send key events within Window widgets to the Window
1806 * The Window widget will in turn pass the
1807 * key event on to the currently focused widget
1812 window = gtk_widget_get_toplevel (widget);
1813 if (window && GTK_IS_WINDOW (window))
1815 /* If there is a grab within the window, give the grab widget
1816 * a first crack at the key event
1818 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1819 handled_event = gtk_widget_event (widget, event);
1823 window = gtk_widget_get_toplevel (widget);
1824 if (window && GTK_IS_WINDOW (window))
1826 if (GTK_WIDGET_IS_SENSITIVE (window))
1827 gtk_widget_event (window, event);
1831 handled_event = TRUE; /* don't send to widget */
1835 /* Other events get propagated up the widget tree
1836 * so that parents can see the button and motion
1837 * events of the children.
1845 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1846 tmp = widget->parent;
1847 gtk_widget_unref (widget);
1851 if (!handled_event && widget)
1852 gtk_widget_ref (widget);
1858 gtk_widget_unref (widget);
1863 gtk_error (gchar *str)
1869 gtk_warning (gchar *str)
1875 gtk_message (gchar *str)
1881 gtk_print (gchar *str)
1883 static GtkWidget *window = NULL;
1884 static GtkWidget *text;
1885 static int level = 0;
1889 GtkWidget *hscrollbar;
1890 GtkWidget *vscrollbar;
1891 GtkWidget *separator;
1896 fputs (str, stdout);
1903 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1905 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1906 (GtkSignalFunc) gtk_widget_destroyed,
1909 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1911 box1 = gtk_vbox_new (FALSE, 0);
1912 gtk_container_add (GTK_CONTAINER (window), box1);
1913 gtk_widget_show (box1);
1916 box2 = gtk_vbox_new (FALSE, 10);
1917 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1918 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1919 gtk_widget_show (box2);
1922 table = gtk_table_new (2, 2, FALSE);
1923 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1924 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1925 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1926 gtk_widget_show (table);
1928 text = gtk_text_new (NULL, NULL);
1929 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1930 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1931 gtk_widget_show (text);
1932 gtk_widget_realize (text);
1934 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1935 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1936 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1937 gtk_widget_show (hscrollbar);
1939 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1940 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1941 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1942 gtk_widget_show (vscrollbar);
1944 separator = gtk_hseparator_new ();
1945 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1946 gtk_widget_show (separator);
1949 box2 = gtk_vbox_new (FALSE, 10);
1950 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1951 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1952 gtk_widget_show (box2);
1955 button = gtk_button_new_with_label ("close");
1956 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1957 (GtkSignalFunc) gtk_widget_hide,
1958 GTK_OBJECT (window));
1959 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1960 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1961 gtk_widget_grab_default (button);
1962 gtk_widget_show (button);
1966 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1969 if (!GTK_WIDGET_VISIBLE (window))
1970 gtk_widget_show (window);
1975 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1976 GValue *return_accu,
1977 const GValue *handler_return,
1980 gboolean continue_emission;
1981 gboolean signal_handled;
1983 signal_handled = g_value_get_boolean (handler_return);
1984 g_value_set_boolean (return_accu, signal_handled);
1985 continue_emission = !signal_handled;
1987 return continue_emission;