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
48 #include <pango/pango-utils.h> /* For pango_split_file_list */
50 #include "gtkaccelmap.h"
52 #include "gtkversion.h"
55 #include "gtkselection.h"
56 #include "gtksettings.h"
57 #include "gtksignal.h"
58 #include "gtkwidget.h"
59 #include "gtkwindow.h"
60 #include "gtkprivate.h"
61 #include "gdk/gdki18n.h"
66 /* Private type definitions
68 typedef struct _GtkInitFunction GtkInitFunction;
69 typedef struct _GtkQuitFunction GtkQuitFunction;
70 typedef struct _GtkClosure GtkClosure;
71 typedef struct _GtkKeySnooperData GtkKeySnooperData;
73 struct _GtkInitFunction
79 struct _GtkQuitFunction
83 GtkCallbackMarshal marshal;
86 GtkDestroyNotify destroy;
91 GtkCallbackMarshal marshal;
93 GtkDestroyNotify destroy;
96 struct _GtkKeySnooperData
103 static void gtk_exit_func (void);
104 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
105 static void gtk_quit_destroy (GtkQuitFunction *quitf);
106 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
109 static void gtk_destroy_closure (gpointer data);
110 static gboolean gtk_invoke_idle_timeout (gpointer data);
111 static void gtk_invoke_input (gpointer data,
113 GdkInputCondition condition);
116 static void gtk_error (gchar *str);
117 static void gtk_warning (gchar *str);
118 static void gtk_message (gchar *str);
119 static void gtk_print (gchar *str);
122 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
124 const guint gtk_major_version = GTK_MAJOR_VERSION;
125 const guint gtk_minor_version = GTK_MINOR_VERSION;
126 const guint gtk_micro_version = GTK_MICRO_VERSION;
127 const guint gtk_binary_age = GTK_BINARY_AGE;
128 const guint gtk_interface_age = GTK_INTERFACE_AGE;
130 static guint gtk_main_loop_level = 0;
131 static gint gtk_initialized = FALSE;
132 static GList *current_events = NULL;
134 static GSList *main_loops = NULL; /* stack of currently executing main loops */
136 static GList *init_functions = NULL; /* A list of init functions.
138 static GList *quit_functions = NULL; /* A list of quit functions.
140 static GMemChunk *quit_mem_chunk = NULL;
142 static GSList *key_snoopers = NULL;
144 static GdkVisual *gtk_visual; /* The visual to be used in creating new
147 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
151 guint gtk_debug_flags = 0; /* Global GTK debug flag */
153 #ifdef G_ENABLE_DEBUG
154 static const GDebugKey gtk_debug_keys[] = {
155 {"misc", GTK_DEBUG_MISC},
156 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
157 {"text", GTK_DEBUG_TEXT},
158 {"tree", GTK_DEBUG_TREE},
159 {"updates", GTK_DEBUG_UPDATES}
162 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
164 #endif /* G_ENABLE_DEBUG */
167 gtk_check_version (guint required_major,
168 guint required_minor,
169 guint required_micro)
171 if (required_major > GTK_MAJOR_VERSION)
172 return "Gtk+ version too old (major mismatch)";
173 if (required_major < GTK_MAJOR_VERSION)
174 return "Gtk+ version too new (major mismatch)";
175 if (required_minor > GTK_MINOR_VERSION)
176 return "Gtk+ version too old (minor mismatch)";
177 if (required_minor < GTK_MINOR_VERSION)
178 return "Gtk+ version too new (minor mismatch)";
179 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
180 return "Gtk+ version too new (micro mismatch)";
181 if (required_micro > GTK_MICRO_VERSION)
182 return "Gtk+ version too old (micro mismatch)";
186 #undef gtk_init_check
188 /* This checks to see if the process is running suid or sgid
189 * at the current time. If so, we don't allow GTK+ to be initialized.
190 * This is meant to be a mild check - we only error out if we
191 * can prove the programmer is doing something wrong, not if
192 * they could be doing something wrong. For this reason, we
193 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
198 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
200 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
201 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
203 #ifdef HAVE_GETRESUID
204 /* These aren't in the header files, so we prototype them here.
206 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
207 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
209 if (getresuid (&ruid, &euid, &suid) != 0 ||
210 getresgid (&rgid, &egid, &sgid) != 0)
211 #endif /* HAVE_GETRESUID */
213 suid = ruid = getuid ();
214 sgid = rgid = getgid ();
219 if (ruid != euid || ruid != suid ||
220 rgid != egid || rgid != sgid)
222 g_warning ("This process is currently running setuid or setgid.\n"
223 "This is not a supported use of GTK+. You must create a helper\n"
224 "program instead. For further details, see:\n\n"
225 " http://www.gtk.org/setuid.html\n\n"
226 "Refusing to initialize GTK+.");
235 G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
238 _gtk_get_libdir (void)
240 static char *gtk_libdir = NULL;
241 if (gtk_libdir == NULL)
242 gtk_libdir = g_win32_get_package_installation_subdirectory
243 (GETTEXT_PACKAGE, dll_name, "lib");
249 _gtk_get_localedir (void)
251 static char *gtk_localedir = NULL;
252 if (gtk_localedir == NULL)
253 gtk_localedir = g_win32_get_package_installation_subdirectory
254 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
256 return gtk_localedir;
260 _gtk_get_sysconfdir (void)
262 static char *gtk_sysconfdir = NULL;
263 if (gtk_sysconfdir == NULL)
264 gtk_sysconfdir = g_win32_get_package_installation_subdirectory
265 (GETTEXT_PACKAGE, dll_name, "etc");
267 return gtk_sysconfdir;
271 _gtk_get_data_prefix (void)
273 static char *gtk_data_prefix = NULL;
274 if (gtk_data_prefix == NULL)
275 gtk_data_prefix = g_win32_get_package_installation_directory
276 (GETTEXT_PACKAGE, dll_name);
278 return gtk_data_prefix;
281 #endif /* G_OS_WIN32 */
284 get_module_path (void)
286 const gchar *module_path_env = g_getenv ("GTK_MODULE_PATH");
287 const gchar *exe_prefix = g_getenv("GTK_EXE_PREFIX");
293 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", "modules", NULL);
295 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", "modules", NULL);
297 module_path = g_strconcat (module_path_env ? module_path_env : "",
298 module_path_env ? G_SEARCHPATH_SEPARATOR_S : "",
301 result = pango_split_file_list (module_path);
303 g_free (default_dir);
304 g_free (module_path);
310 find_module (gchar **module_path,
317 if (g_path_is_absolute (name))
318 return g_module_open (name, G_MODULE_BIND_LAZY);
320 for (i = 0; module_path[i]; i++)
322 gchar *version_directory;
324 version_directory = g_build_filename (module_path[i], GTK_BINARY_VERSION, NULL);
325 module_name = g_module_build_path (version_directory, name);
326 g_free (version_directory);
328 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
330 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
331 g_free (module_name);
335 g_free (module_name);
337 module_name = g_module_build_path (module_path[i], name);
339 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
341 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
342 g_free (module_name);
346 g_free (module_name);
349 /* As last resort, try loading without an absolute path (using system
352 module_name = g_module_build_path (NULL, name);
353 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
360 load_module (GSList *gtk_modules,
364 GtkModuleInitFunc modinit_func = NULL;
365 GModule *module = NULL;
367 if (g_module_supported ())
369 module = find_module (module_path, name);
371 g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
374 if (!g_slist_find (gtk_modules, modinit_func))
376 g_module_make_resident (module);
377 gtk_modules = g_slist_prepend (gtk_modules, modinit_func);
381 g_module_close (module);
388 g_message ("Failed to load module \"%s\": %s",
389 module ? g_module_name (module) : name,
392 g_module_close (module);
399 load_modules (const char *module_str)
401 gchar **module_path = get_module_path ();
402 gchar **module_names = pango_split_file_list (module_str);
403 GSList *gtk_modules = NULL;
406 for (i = 0; module_names[i]; i++)
407 gtk_modules = load_module (gtk_modules, module_path, module_names[i]);
409 gtk_modules = g_slist_reverse (gtk_modules);
411 g_strfreev (module_names);
412 g_strfreev (module_path);
417 static gboolean do_setlocale = TRUE;
420 * gtk_disable_setlocale:
422 * Prevents gtk_init() and gtk_init_check() from automatically
423 * calling <literal>setlocale (LC_ALL, "")</literal>. You would
424 * want to use this function if you wanted to set the locale for
425 * your program to something other than the user's locale, or if
426 * you wanted to set different values for different locale categories.
428 * Most programs should not need to call this function.
431 gtk_disable_setlocale (void)
434 g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
436 do_setlocale = FALSE;
440 gtk_init_check (int *argc,
443 GString *gtk_modules_string = NULL;
444 GSList *gtk_modules = NULL;
451 if (!check_setugid ())
455 g_set_error_handler (gtk_error);
456 g_set_warning_handler (gtk_warning);
457 g_set_message_handler (gtk_message);
458 g_set_print_handler (gtk_print);
462 setlocale (LC_ALL, "");
464 /* Initialize "gdk". We pass along the 'argc' and 'argv'
465 * parameters as they contain information that GDK uses
467 if (!gdk_init_check (argc, argv))
470 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
472 #ifdef G_ENABLE_DEBUG
473 env_string = getenv ("GTK_DEBUG");
474 if (env_string != NULL)
476 gtk_debug_flags = g_parse_debug_string (env_string,
481 #endif /* G_ENABLE_DEBUG */
483 env_string = getenv ("GTK_MODULES");
485 gtk_modules_string = g_string_new (env_string);
491 for (i = 1; i < *argc;)
493 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
494 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
496 gchar *module_name = (*argv)[i] + 12;
498 if (*module_name == '=')
500 else if (i + 1 < *argc)
504 module_name = (*argv)[i];
508 if (module_name && *module_name)
510 if (gtk_modules_string)
511 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
513 gtk_modules_string = g_string_new (NULL);
515 g_string_append (gtk_modules_string, module_name);
518 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
520 GLogLevelFlags fatal_mask;
522 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
523 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
524 g_log_set_always_fatal (fatal_mask);
527 #ifdef G_ENABLE_DEBUG
528 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
529 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
531 gchar *equal_pos = strchr ((*argv)[i], '=');
533 if (equal_pos != NULL)
535 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
539 else if ((i + 1) < *argc && (*argv)[i + 1])
541 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
549 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
550 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
552 gchar *equal_pos = strchr ((*argv)[i], '=');
554 if (equal_pos != NULL)
556 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
560 else if ((i + 1) < *argc && (*argv)[i + 1])
562 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
570 #endif /* G_ENABLE_DEBUG */
574 for (i = 1; i < *argc; i++)
576 for (k = i; k < *argc; k++)
577 if ((*argv)[k] != NULL)
583 for (j = i + k; j < *argc; j++)
584 (*argv)[j-k] = (*argv)[j];
590 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
591 gdk_window_set_debug_updates (TRUE);
593 /* load gtk modules */
594 if (gtk_modules_string)
596 gtk_modules = load_modules (gtk_modules_string->str);
597 g_string_free (gtk_modules_string, TRUE);
601 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
602 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
603 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
608 /* Translate to default:RTL if you want your widgets
609 * to be RTL, otherwise translate to default:LTR.
610 * Do *not* translate it to "predefinito:LTR", if it
611 * it isn't default:LTR or default:RTL it will not work
613 char *e = _("default:LTR");
614 if (strcmp (e, "default:RTL")==0) {
615 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
616 } else if (strcmp (e, "default:LTR")) {
617 g_warning ("Whoever translated default:LTR did so wrongly.\n");
621 /* Initialize the default visual and colormap to be
622 * used in creating widgets. (We want to use the system
623 * defaults so as to be nice to the colormap).
625 gtk_visual = gdk_visual_get_system ();
626 gtk_colormap = gdk_colormap_get_system ();
629 _gtk_accel_map_init ();
632 /* Register an exit function to make sure we are able to cleanup.
634 g_atexit (gtk_exit_func);
636 /* Set the 'initialized' flag.
638 gtk_initialized = TRUE;
640 /* initialize gtk modules
642 for (slist = gtk_modules; slist; slist = slist->next)
646 GtkModuleInitFunc modinit;
648 modinit = slist->data;
649 modinit (argc, argv);
652 g_slist_free (gtk_modules);
655 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
656 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
657 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
658 " if you don't know why you are getting this, you probably want to\n"
659 " use the stable branch which can be retrieved from\n"
660 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
661 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
670 gtk_init (int *argc, char ***argv)
672 if (!gtk_init_check (argc, argv))
674 g_warning ("cannot open display: %s", gdk_get_display ());
682 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
684 if (sizeof_GtkWindow != sizeof (GtkWindow))
685 g_error ("Incompatible build!\n"
686 "The code using GTK+ thinks GtkWindow is of different\n"
687 "size than it actually is in this build of GTK+.\n"
688 "On Windows, this probably means that you have compiled\n"
689 "your code with gcc without the -fnative-struct switch.");
692 /* These two functions might get more checks added later, thus pass
693 * in the number of extra args.
696 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
698 check_sizeof_GtkWindow (sizeof_GtkWindow);
699 gtk_init (argc, argv);
703 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
705 check_sizeof_GtkWindow (sizeof_GtkWindow);
706 return gtk_init_check (argc, argv);
712 gtk_exit (gint errorcode)
714 /* Only if "gtk" has been initialized should we de-initialize.
716 /* de-initialisation is done by the gtk_exit_funct(),
717 * no need to do this here (Alex J.)
719 gdk_exit (errorcode);
726 * Initializes internationalization support for GTK+. gtk_init()
727 * automatically does this, so there is typically no point
728 * in calling this function.
730 * If you are calling this function because you changed the locale
731 * after GTK+ is was initialized, then calling this function
732 * may help a bit. (Note, however, that changing the locale
733 * after GTK+ is initialized may produce inconsistent results and
734 * is not really supported.)
736 * In detail - sets the current locale according to the
737 * program environment. This is the same as calling the C library function
738 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the
739 * locale specific setup of the windowing system used by GDK.
741 * Return value: a string corresponding to the locale set, as with the
742 * C library function <function>setlocale()</function>.
745 gtk_set_locale (void)
747 return gdk_set_locale ();
751 * gtk_get_default_language:
753 * Returns the ISO language code for the default language currently in
754 * effect. (Note that this can change over the life of an
755 * application.) The default language is derived from the current
756 * locale. It determines, for example, whether GTK+ uses the
757 * right-to-left or left-to-right text direction.
759 * Return value: the default language as an allocated string, must be freed
762 gtk_get_default_language (void)
765 PangoLanguage *result;
768 lang = g_strdup (setlocale (LC_CTYPE, NULL));
769 p = strchr (lang, '.');
772 p = strchr (lang, '@');
776 result = pango_language_from_string (lang);
787 GtkInitFunction *init;
790 gtk_main_loop_level++;
792 loop = g_main_new (TRUE);
793 main_loops = g_slist_prepend (main_loops, loop);
795 tmp_list = functions = init_functions;
796 init_functions = NULL;
800 init = tmp_list->data;
801 tmp_list = tmp_list->next;
803 (* init->function) (init->data);
806 g_list_free (functions);
808 if (g_main_is_running (main_loops->data))
810 GDK_THREADS_LEAVE ();
812 GDK_THREADS_ENTER ();
818 GList *reinvoke_list = NULL;
819 GtkQuitFunction *quitf;
821 while (quit_functions)
823 quitf = quit_functions->data;
825 tmp_list = quit_functions;
826 quit_functions = g_list_remove_link (quit_functions, quit_functions);
827 g_list_free_1 (tmp_list);
829 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
830 gtk_quit_invoke_function (quitf))
832 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
836 gtk_quit_destroy (quitf);
843 work = g_list_last (reinvoke_list);
845 quit_functions->prev = work;
846 work->next = quit_functions;
847 quit_functions = work;
853 main_loops = g_slist_remove (main_loops, loop);
855 g_main_destroy (loop);
857 gtk_main_loop_level--;
861 gtk_main_level (void)
863 return gtk_main_loop_level;
869 g_return_if_fail (main_loops != NULL);
871 g_main_quit (main_loops->data);
875 gtk_events_pending (void)
879 GDK_THREADS_LEAVE ();
880 result = g_main_pending ();
881 GDK_THREADS_ENTER ();
887 gtk_main_iteration (void)
889 GDK_THREADS_LEAVE ();
890 g_main_iteration (TRUE);
891 GDK_THREADS_ENTER ();
894 return !g_main_is_running (main_loops->data);
900 gtk_main_iteration_do (gboolean blocking)
902 GDK_THREADS_LEAVE ();
903 g_main_iteration (blocking);
904 GDK_THREADS_ENTER ();
907 return !g_main_is_running (main_loops->data);
913 gtk_main_do_event (GdkEvent *event)
915 GtkWidget *event_widget;
916 GtkWidget *grab_widget;
917 GtkWindowGroup *window_group;
918 GdkEvent *next_event;
921 /* If there are any events pending then get the next one.
923 next_event = gdk_event_peek ();
925 /* Try to compress enter/leave notify events. These event
926 * pairs occur when the mouse is dragged quickly across
927 * a window with many buttons (or through a menu). Instead
928 * of highlighting and de-highlighting each widget that
929 * is crossed it is better to simply de-highlight the widget
930 * which contained the mouse initially and highlight the
931 * widget which ends up containing the mouse.
934 if (((event->type == GDK_ENTER_NOTIFY) ||
935 (event->type == GDK_LEAVE_NOTIFY)) &&
936 ((next_event->type == GDK_ENTER_NOTIFY) ||
937 (next_event->type == GDK_LEAVE_NOTIFY)) &&
938 (next_event->type != event->type) &&
939 (next_event->any.window == event->any.window))
941 /* Throw both the peeked copy and the queued copy away
943 gdk_event_free (next_event);
944 next_event = gdk_event_get ();
945 gdk_event_free (next_event);
951 gdk_event_free (next_event);
953 /* Find the widget which got the event. We store the widget
954 * in the user_data field of GdkWindow's.
955 * Ignore the event if we don't have a widget for it, except
956 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
957 * Though this happens rarely, bogus events can occour
958 * for e.g. destroyed GdkWindows.
960 event_widget = gtk_get_event_widget (event);
963 /* To handle selection INCR transactions, we select
964 * PropertyNotify events on the requestor window and create
965 * a corresponding (fake) GdkWindow so that events get
966 * here. There won't be a widget though, so we have to handle
969 if (event->type == GDK_PROPERTY_NOTIFY)
970 gtk_selection_incr_event (event->any.window,
972 else if (event->type == GDK_SETTING)
973 _gtk_settings_handle_event (&event->setting);
978 /* Push the event onto a stack of current events for
979 * gtk_current_event_get().
981 current_events = g_list_prepend (current_events, event);
983 window_group = gtk_main_get_window_group (event_widget);
985 /* If there is a grab in effect...
987 if (window_group->grabs)
989 grab_widget = window_group->grabs->data;
991 /* If the grab widget is an ancestor of the event widget
992 * then we send the event to the original event widget.
993 * This is the key to implementing modality.
995 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
996 gtk_widget_is_ancestor (event_widget, grab_widget))
997 grab_widget = event_widget;
1001 grab_widget = event_widget;
1004 /* Not all events get sent to the grabbing widget.
1005 * The delete, destroy, expose, focus change and resize
1006 * events still get sent to the event widget because
1007 * 1) these events have no meaning for the grabbing widget
1008 * and 2) redirecting these events to the grabbing widget
1009 * could cause the display to be messed up.
1011 * Drag events are also not redirected, since it isn't
1012 * clear what the semantics of that would be.
1014 switch (event->type)
1020 gtk_widget_ref (event_widget);
1021 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
1022 !gtk_widget_event (event_widget, event))
1023 gtk_widget_destroy (event_widget);
1024 gtk_widget_unref (event_widget);
1028 /* Unexpected GDK_DESTROY from the outside, ignore for
1029 * child windows, handle like a GDK_DELETE for toplevels
1031 if (!event_widget->parent)
1033 gtk_widget_ref (event_widget);
1034 if (!gtk_widget_event (event_widget, event) &&
1035 GTK_WIDGET_REALIZED (event_widget))
1036 gtk_widget_destroy (event_widget);
1037 gtk_widget_unref (event_widget);
1042 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
1044 gdk_window_begin_paint_region (event->any.window, event->expose.region);
1045 gtk_widget_send_expose (event_widget, event);
1046 gdk_window_end_paint (event->any.window);
1049 gtk_widget_send_expose (event_widget, event);
1052 case GDK_PROPERTY_NOTIFY:
1054 case GDK_FOCUS_CHANGE:
1058 case GDK_SELECTION_CLEAR:
1059 case GDK_SELECTION_REQUEST:
1060 case GDK_SELECTION_NOTIFY:
1061 case GDK_CLIENT_EVENT:
1062 case GDK_VISIBILITY_NOTIFY:
1063 case GDK_WINDOW_STATE:
1064 gtk_widget_event (event_widget, event);
1068 case GDK_BUTTON_PRESS:
1069 case GDK_2BUTTON_PRESS:
1070 case GDK_3BUTTON_PRESS:
1071 gtk_propagate_event (grab_widget, event);
1075 case GDK_KEY_RELEASE:
1078 if (gtk_invoke_key_snoopers (grab_widget, event))
1081 /* else fall through */
1082 case GDK_MOTION_NOTIFY:
1083 case GDK_BUTTON_RELEASE:
1084 case GDK_PROXIMITY_IN:
1085 case GDK_PROXIMITY_OUT:
1086 gtk_propagate_event (grab_widget, event);
1089 case GDK_ENTER_NOTIFY:
1090 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1092 g_object_ref (event_widget);
1094 gtk_widget_event (grab_widget, event);
1095 if (event_widget == grab_widget)
1096 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1098 g_object_unref (event_widget);
1102 case GDK_LEAVE_NOTIFY:
1103 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1105 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1106 gtk_widget_event (event_widget, event);
1108 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1109 gtk_widget_event (grab_widget, event);
1112 case GDK_DRAG_STATUS:
1113 case GDK_DROP_FINISHED:
1114 _gtk_drag_source_handle_event (event_widget, event);
1116 case GDK_DRAG_ENTER:
1117 case GDK_DRAG_LEAVE:
1118 case GDK_DRAG_MOTION:
1119 case GDK_DROP_START:
1120 _gtk_drag_dest_handle_event (event_widget, event);
1123 g_assert_not_reached ();
1127 tmp_list = current_events;
1128 current_events = g_list_remove_link (current_events, tmp_list);
1129 g_list_free_1 (tmp_list);
1144 static GtkWindowGroup *
1145 gtk_main_get_window_group (GtkWidget *widget)
1147 GtkWidget *toplevel = NULL;
1150 toplevel = gtk_widget_get_toplevel (widget);
1152 if (toplevel && GTK_IS_WINDOW (toplevel))
1153 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1155 return _gtk_window_get_group (NULL);
1160 gboolean was_grabbed;
1161 GtkWidget *grab_widget;
1165 gtk_grab_notify_foreach (GtkWidget *child,
1169 GrabNotifyInfo *info = data;
1171 if (child != info->grab_widget)
1173 g_object_ref (G_OBJECT (child));
1175 gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", info->was_grabbed);
1177 if (GTK_IS_CONTAINER (child))
1178 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1180 g_object_unref (G_OBJECT (child));
1185 gtk_grab_notify (GtkWindowGroup *group,
1186 GtkWidget *grab_widget,
1187 gboolean was_grabbed)
1190 GrabNotifyInfo info;
1192 info.grab_widget = grab_widget;
1193 info.was_grabbed = was_grabbed;
1195 g_object_ref (group);
1196 g_object_ref (grab_widget);
1198 toplevels = gtk_window_list_toplevels ();
1199 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1203 GtkWindow *toplevel = toplevels->data;
1204 toplevels = g_list_delete_link (toplevels, toplevels);
1206 if (group == toplevel->group)
1207 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1208 g_object_unref (toplevel);
1211 g_object_unref (group);
1212 g_object_unref (grab_widget);
1216 gtk_grab_add (GtkWidget *widget)
1218 GtkWindowGroup *group;
1219 gboolean was_grabbed;
1221 g_return_if_fail (widget != NULL);
1223 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1225 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1227 group = gtk_main_get_window_group (widget);
1229 was_grabbed = (group->grabs != NULL);
1231 gtk_widget_ref (widget);
1232 group->grabs = g_slist_prepend (group->grabs, widget);
1235 gtk_grab_notify (group, widget, FALSE);
1240 gtk_grab_get_current (void)
1242 GtkWindowGroup *group;
1244 group = gtk_main_get_window_group (NULL);
1247 return GTK_WIDGET (group->grabs->data);
1252 gtk_grab_remove (GtkWidget *widget)
1254 GtkWindowGroup *group;
1256 g_return_if_fail (widget != NULL);
1258 if (GTK_WIDGET_HAS_GRAB (widget))
1260 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1262 group = gtk_main_get_window_group (widget);
1263 group->grabs = g_slist_remove (group->grabs, widget);
1265 gtk_widget_unref (widget);
1268 gtk_grab_notify (group, widget, TRUE);
1273 gtk_init_add (GtkFunction function,
1276 GtkInitFunction *init;
1278 init = g_new (GtkInitFunction, 1);
1279 init->function = function;
1282 init_functions = g_list_prepend (init_functions, init);
1286 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1289 GtkKeySnooperData *data;
1290 static guint snooper_id = 1;
1292 g_return_val_if_fail (snooper != NULL, 0);
1294 data = g_new (GtkKeySnooperData, 1);
1295 data->func = snooper;
1296 data->func_data = func_data;
1297 data->id = snooper_id++;
1298 key_snoopers = g_slist_prepend (key_snoopers, data);
1304 gtk_key_snooper_remove (guint snooper_id)
1306 GtkKeySnooperData *data = NULL;
1309 slist = key_snoopers;
1313 if (data->id == snooper_id)
1316 slist = slist->next;
1320 key_snoopers = g_slist_remove (key_snoopers, data);
1324 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1328 gint return_val = FALSE;
1330 slist = key_snoopers;
1331 while (slist && !return_val)
1333 GtkKeySnooperData *data;
1336 slist = slist->next;
1337 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1344 gtk_quit_add_full (guint main_level,
1345 GtkFunction function,
1346 GtkCallbackMarshal marshal,
1348 GtkDestroyNotify destroy)
1350 static guint quit_id = 1;
1351 GtkQuitFunction *quitf;
1353 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1355 if (!quit_mem_chunk)
1356 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1357 512, G_ALLOC_AND_FREE);
1359 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1361 quitf->id = quit_id++;
1362 quitf->main_level = main_level;
1363 quitf->function = function;
1364 quitf->marshal = marshal;
1366 quitf->destroy = destroy;
1368 quit_functions = g_list_prepend (quit_functions, quitf);
1374 gtk_quit_destroy (GtkQuitFunction *quitf)
1377 quitf->destroy (quitf->data);
1378 g_mem_chunk_free (quit_mem_chunk, quitf);
1382 gtk_quit_destructor (GtkObject **object_p)
1385 gtk_object_destroy (*object_p);
1392 gtk_quit_add_destroy (guint main_level,
1395 GtkObject **object_p;
1397 g_return_if_fail (main_level > 0);
1398 g_return_if_fail (GTK_IS_OBJECT (object));
1400 object_p = g_new (GtkObject*, 1);
1402 gtk_signal_connect (object,
1404 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1406 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1410 gtk_quit_add (guint main_level,
1411 GtkFunction function,
1414 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1418 gtk_quit_remove (guint id)
1420 GtkQuitFunction *quitf;
1423 tmp_list = quit_functions;
1426 quitf = tmp_list->data;
1428 if (quitf->id == id)
1430 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1431 g_list_free (tmp_list);
1432 gtk_quit_destroy (quitf);
1437 tmp_list = tmp_list->next;
1442 gtk_quit_remove_by_data (gpointer data)
1444 GtkQuitFunction *quitf;
1447 tmp_list = quit_functions;
1450 quitf = tmp_list->data;
1452 if (quitf->data == data)
1454 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1455 g_list_free (tmp_list);
1456 gtk_quit_destroy (quitf);
1461 tmp_list = tmp_list->next;
1466 gtk_timeout_add_full (guint32 interval,
1467 GtkFunction function,
1468 GtkCallbackMarshal marshal,
1470 GtkDestroyNotify destroy)
1474 GtkClosure *closure;
1476 closure = g_new (GtkClosure, 1);
1477 closure->marshal = marshal;
1478 closure->data = data;
1479 closure->destroy = destroy;
1481 return g_timeout_add_full (0, interval,
1482 gtk_invoke_idle_timeout,
1484 gtk_destroy_closure);
1487 return g_timeout_add_full (0, interval, function, data, destroy);
1491 gtk_timeout_add (guint32 interval,
1492 GtkFunction function,
1495 return g_timeout_add_full (0, interval, function, data, NULL);
1499 gtk_timeout_remove (guint tag)
1501 g_source_remove (tag);
1505 gtk_idle_add_full (gint priority,
1506 GtkFunction function,
1507 GtkCallbackMarshal marshal,
1509 GtkDestroyNotify destroy)
1513 GtkClosure *closure;
1515 closure = g_new (GtkClosure, 1);
1516 closure->marshal = marshal;
1517 closure->data = data;
1518 closure->destroy = destroy;
1520 return g_idle_add_full (priority,
1521 gtk_invoke_idle_timeout,
1523 gtk_destroy_closure);
1526 return g_idle_add_full (priority, function, data, destroy);
1530 gtk_idle_add (GtkFunction function,
1533 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1537 gtk_idle_add_priority (gint priority,
1538 GtkFunction function,
1541 return g_idle_add_full (priority, function, data, NULL);
1545 gtk_idle_remove (guint tag)
1547 g_source_remove (tag);
1551 gtk_idle_remove_by_data (gpointer data)
1553 if (!g_idle_remove_by_data (data))
1554 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1558 gtk_input_add_full (gint source,
1559 GdkInputCondition condition,
1560 GdkInputFunction function,
1561 GtkCallbackMarshal marshal,
1563 GtkDestroyNotify destroy)
1567 GtkClosure *closure;
1569 closure = g_new (GtkClosure, 1);
1570 closure->marshal = marshal;
1571 closure->data = data;
1572 closure->destroy = destroy;
1574 return gdk_input_add_full (source,
1576 (GdkInputFunction) gtk_invoke_input,
1578 (GdkDestroyNotify) gtk_destroy_closure);
1581 return gdk_input_add_full (source, condition, function, data, destroy);
1585 gtk_input_remove (guint tag)
1587 g_source_remove (tag);
1591 gtk_destroy_closure (gpointer data)
1593 GtkClosure *closure = data;
1595 if (closure->destroy)
1596 (closure->destroy) (closure->data);
1601 gtk_invoke_idle_timeout (gpointer data)
1603 GtkClosure *closure = data;
1606 gint ret_val = FALSE;
1607 args[0].name = NULL;
1608 args[0].type = GTK_TYPE_BOOL;
1609 args[0].d.pointer_data = &ret_val;
1610 closure->marshal (NULL, closure->data, 0, args);
1615 gtk_invoke_input (gpointer data,
1617 GdkInputCondition condition)
1619 GtkClosure *closure = data;
1622 args[0].type = GTK_TYPE_INT;
1623 args[0].name = NULL;
1624 GTK_VALUE_INT (args[0]) = source;
1625 args[1].type = GDK_TYPE_INPUT_CONDITION;
1626 args[1].name = NULL;
1627 GTK_VALUE_FLAGS (args[1]) = condition;
1628 args[2].type = GTK_TYPE_NONE;
1629 args[2].name = NULL;
1631 closure->marshal (NULL, closure->data, 2, args);
1635 * gtk_get_current_event:
1637 * Obtains a copy of the event currently being processed by GTK+. For
1638 * example, if you get a "clicked" signal from #GtkButton, the current
1639 * event will be the #GdkEventButton that triggered the "clicked"
1640 * signal. The returned event must be freed with gdk_event_free().
1641 * If there is no current event, the function returns %NULL.
1643 * Return value: a copy of the current event, or %NULL if no current event.
1646 gtk_get_current_event (void)
1649 return gdk_event_copy (current_events->data);
1655 * gtk_get_current_event_time:
1657 * If there is a current event and it has a timestamp, return that
1658 * timestamp, otherwise return %GDK_CURRENT_TIME.
1660 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1663 gtk_get_current_event_time (void)
1666 return gdk_event_get_time (current_events->data);
1668 return GDK_CURRENT_TIME;
1672 * gtk_get_current_event_state:
1673 * @state: a location to store the state of the current event
1675 * If there is a current event and it has a state field, place
1676 * that state field in @state and return %TRUE, otherwise return
1679 * Return value: %TRUE if there was a current event and it had a state field
1682 gtk_get_current_event_state (GdkModifierType *state)
1684 g_return_val_if_fail (state != NULL, FALSE);
1687 return gdk_event_get_state (current_events->data, state);
1696 * gtk_get_event_widget:
1697 * @event: a #GdkEvent
1699 * If @event is %NULL or the event was not associated with any widget,
1700 * returns %NULL, otherwise returns the widget that received the event
1703 * Return value: the widget that originally received @event, or %NULL
1706 gtk_get_event_widget (GdkEvent *event)
1711 if (event && event->any.window)
1712 gdk_window_get_user_data (event->any.window, (void**) &widget);
1718 gtk_exit_func (void)
1720 if (gtk_initialized)
1722 gtk_initialized = FALSE;
1728 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1730 if (!quitf->marshal)
1731 return quitf->function (quitf->data);
1735 gint ret_val = FALSE;
1737 args[0].name = NULL;
1738 args[0].type = GTK_TYPE_BOOL;
1739 args[0].d.pointer_data = &ret_val;
1740 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1748 * gtk_propagate_event:
1749 * @widget: a #GtkWidget
1752 * Sends an event to a widget, propagating the event to parent widgets
1753 * if the event remains unhandled. Events received by GTK+ from GDK
1754 * normally begin in gtk_main_do_event(). Depending on the type of
1755 * event, existence of modal dialogs, grabs, etc., the event may be
1756 * propagated; if so, this function is used. gtk_propagate_event()
1757 * calls gtk_widget_event() on each widget it decides to send the
1758 * event to. So gtk_widget_event() is the lowest-level function; it
1759 * simply emits the "event" and possibly an event-specific signal on a
1760 * widget. gtk_propagate_event() is a bit higher-level, and
1761 * gtk_main_do_event() is the highest level.
1763 * All that said, you most likely don't want to use any of these
1764 * functions; synthesizing events is rarely needed. Consider asking on
1765 * the mailing list for better ways to achieve your goals. For
1766 * example, use gdk_window_invalidate_rect() or
1767 * gtk_widget_queue_draw() instead of making up expose events.
1771 gtk_propagate_event (GtkWidget *widget,
1776 g_return_if_fail (GTK_IS_WIDGET (widget));
1777 g_return_if_fail (event != NULL);
1779 handled_event = FALSE;
1781 gtk_widget_ref (widget);
1783 if ((event->type == GDK_KEY_PRESS) ||
1784 (event->type == GDK_KEY_RELEASE))
1786 /* Only send key events within Window widgets to the Window
1787 * The Window widget will in turn pass the
1788 * key event on to the currently focused widget
1793 window = gtk_widget_get_toplevel (widget);
1794 if (window && GTK_IS_WINDOW (window))
1796 /* If there is a grab within the window, give the grab widget
1797 * a first crack at the key event
1799 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1800 handled_event = gtk_widget_event (widget, event);
1804 window = gtk_widget_get_toplevel (widget);
1805 if (window && GTK_IS_WINDOW (window))
1807 if (GTK_WIDGET_IS_SENSITIVE (window))
1808 gtk_widget_event (window, event);
1812 handled_event = TRUE; /* don't send to widget */
1816 /* Other events get propagated up the widget tree
1817 * so that parents can see the button and motion
1818 * events of the children.
1826 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1827 tmp = widget->parent;
1828 gtk_widget_unref (widget);
1832 if (!handled_event && widget)
1833 gtk_widget_ref (widget);
1839 gtk_widget_unref (widget);
1844 gtk_error (gchar *str)
1850 gtk_warning (gchar *str)
1856 gtk_message (gchar *str)
1862 gtk_print (gchar *str)
1864 static GtkWidget *window = NULL;
1865 static GtkWidget *text;
1866 static int level = 0;
1870 GtkWidget *hscrollbar;
1871 GtkWidget *vscrollbar;
1872 GtkWidget *separator;
1877 fputs (str, stdout);
1884 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1886 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1887 (GtkSignalFunc) gtk_widget_destroyed,
1890 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1892 box1 = gtk_vbox_new (FALSE, 0);
1893 gtk_container_add (GTK_CONTAINER (window), box1);
1894 gtk_widget_show (box1);
1897 box2 = gtk_vbox_new (FALSE, 10);
1898 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1899 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1900 gtk_widget_show (box2);
1903 table = gtk_table_new (2, 2, FALSE);
1904 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1905 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1906 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1907 gtk_widget_show (table);
1909 text = gtk_text_new (NULL, NULL);
1910 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1911 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1912 gtk_widget_show (text);
1913 gtk_widget_realize (text);
1915 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1916 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1917 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1918 gtk_widget_show (hscrollbar);
1920 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1921 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1922 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1923 gtk_widget_show (vscrollbar);
1925 separator = gtk_hseparator_new ();
1926 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1927 gtk_widget_show (separator);
1930 box2 = gtk_vbox_new (FALSE, 10);
1931 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1932 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1933 gtk_widget_show (box2);
1936 button = gtk_button_new_with_label ("close");
1937 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1938 (GtkSignalFunc) gtk_widget_hide,
1939 GTK_OBJECT (window));
1940 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1941 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1942 gtk_widget_grab_default (button);
1943 gtk_widget_show (button);
1947 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1950 if (!GTK_WIDGET_VISIBLE (window))
1951 gtk_widget_show (window);
1956 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1957 GValue *return_accu,
1958 const GValue *handler_return,
1961 gboolean continue_emission;
1962 gboolean signal_handled;
1964 signal_handled = g_value_get_boolean (handler_return);
1965 g_value_set_boolean (return_accu, signal_handled);
1966 continue_emission = !signal_handled;
1968 return continue_emission;