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
43 #include <pango/pango-utils.h> /* For pango_split_file_list */
46 #include "gtkversion.h"
49 #include "gtkselection.h"
50 #include "gtksettings.h"
51 #include "gtksignal.h"
52 #include "gtkwidget.h"
53 #include "gtkwindow.h"
54 #include "gtkprivate.h"
55 #include "gdk/gdki18n.h"
60 /* Private type definitions
62 typedef struct _GtkInitFunction GtkInitFunction;
63 typedef struct _GtkQuitFunction GtkQuitFunction;
64 typedef struct _GtkClosure GtkClosure;
65 typedef struct _GtkKeySnooperData GtkKeySnooperData;
67 struct _GtkInitFunction
73 struct _GtkQuitFunction
77 GtkCallbackMarshal marshal;
80 GtkDestroyNotify destroy;
85 GtkCallbackMarshal marshal;
87 GtkDestroyNotify destroy;
90 struct _GtkKeySnooperData
97 static void gtk_exit_func (void);
98 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
99 static void gtk_quit_destroy (GtkQuitFunction *quitf);
100 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
103 static void gtk_destroy_closure (gpointer data);
104 static gboolean gtk_invoke_idle_timeout (gpointer data);
105 static void gtk_invoke_input (gpointer data,
107 GdkInputCondition condition);
110 static void gtk_error (gchar *str);
111 static void gtk_warning (gchar *str);
112 static void gtk_message (gchar *str);
113 static void gtk_print (gchar *str);
116 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
118 const guint gtk_major_version = GTK_MAJOR_VERSION;
119 const guint gtk_minor_version = GTK_MINOR_VERSION;
120 const guint gtk_micro_version = GTK_MICRO_VERSION;
121 const guint gtk_binary_age = GTK_BINARY_AGE;
122 const guint gtk_interface_age = GTK_INTERFACE_AGE;
124 static guint gtk_main_loop_level = 0;
125 static gint gtk_initialized = FALSE;
126 static GList *current_events = NULL;
128 static GSList *main_loops = NULL; /* stack of currently executing main loops */
130 static GList *init_functions = NULL; /* A list of init functions.
132 static GList *quit_functions = NULL; /* A list of quit functions.
134 static GMemChunk *quit_mem_chunk = NULL;
136 static GSList *key_snoopers = NULL;
138 static GdkVisual *gtk_visual; /* The visual to be used in creating new
141 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
145 guint gtk_debug_flags = 0; /* Global GTK debug flag */
147 #ifdef G_ENABLE_DEBUG
148 static const GDebugKey gtk_debug_keys[] = {
149 {"misc", GTK_DEBUG_MISC},
150 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
151 {"text", GTK_DEBUG_TEXT},
152 {"tree", GTK_DEBUG_TREE},
153 {"updates", GTK_DEBUG_UPDATES}
156 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
158 #endif /* G_ENABLE_DEBUG */
161 gtk_check_version (guint required_major,
162 guint required_minor,
163 guint required_micro)
165 if (required_major > GTK_MAJOR_VERSION)
166 return "Gtk+ version too old (major mismatch)";
167 if (required_major < GTK_MAJOR_VERSION)
168 return "Gtk+ version too new (major mismatch)";
169 if (required_minor > GTK_MINOR_VERSION)
170 return "Gtk+ version too old (minor mismatch)";
171 if (required_minor < GTK_MINOR_VERSION)
172 return "Gtk+ version too new (minor mismatch)";
173 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
174 return "Gtk+ version too new (micro mismatch)";
175 if (required_micro > GTK_MICRO_VERSION)
176 return "Gtk+ version too old (micro mismatch)";
180 #undef gtk_init_check
182 /* This checks to see if the process is running suid or sgid
183 * at the current time. If so, we don't allow GTK+ to be initialized.
184 * This is meant to be a mild check - we only error out if we
185 * can prove the programmer is doing something wrong, not if
186 * they could be doing something wrong. For this reason, we
187 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
192 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
194 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
195 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
197 #ifdef HAVE_GETRESUID
198 /* These aren't in the header files, so we prototype them here.
200 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
201 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
203 if (getresuid (&ruid, &euid, &suid) != 0 ||
204 getresgid (&rgid, &egid, &sgid) != 0)
205 #endif /* HAVE_GETRESUID */
207 suid = ruid = getuid ();
208 sgid = rgid = getgid ();
213 if (ruid != euid || ruid != suid ||
214 rgid != egid || rgid != sgid)
216 g_warning ("This process is currently running setuid or setgid.\n"
217 "This is not a supported use of GTK+. You must create a helper\n"
218 "program instead. For further details, see:\n\n"
219 " http://www.gtk.org/setuid.html\n\n"
220 "Refusing to initialize GTK+.");
228 get_module_path (void)
230 gchar *module_path = g_getenv ("GTK_MODULE_PATH");
231 gchar *exe_prefix = g_getenv("GTK_EXE_PREFIX");
236 default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", "modules", NULL);
238 default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", "modules", NULL);
240 module_path = g_strconcat (module_path ? module_path : "",
241 module_path ? G_SEARCHPATH_SEPARATOR_S : "",
244 result = pango_split_file_list (module_path);
246 g_free (default_dir);
247 g_free (module_path);
253 find_module (gchar **module_path,
260 if (g_path_is_absolute (name))
261 return g_module_open (name, G_MODULE_BIND_LAZY);
263 for (i = 0; module_path[i]; i++)
265 gchar *version_directory;
267 version_directory = g_build_filename (module_path[i], GTK_BINARY_VERSION, NULL);
268 module_name = g_module_build_path (version_directory, name);
269 g_free (version_directory);
271 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
273 g_free (module_name);
274 return g_module_open (module_name, G_MODULE_BIND_LAZY);
277 g_free (module_name);
279 module_name = g_module_build_path (module_path[i], name);
281 if (g_file_test (module_name, G_FILE_TEST_EXISTS))
283 g_free (module_name);
284 return g_module_open (module_name, G_MODULE_BIND_LAZY);
287 g_free (module_name);
290 /* As last resort, try loading without an absolute path (using system
293 module_name = g_module_build_path (NULL, name);
294 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
301 load_module (GSList *gtk_modules,
305 GtkModuleInitFunc modinit_func = NULL;
306 GModule *module = NULL;
308 if (g_module_supported ())
310 module = find_module (module_path, name);
312 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
315 if (!g_slist_find (gtk_modules, modinit_func))
317 g_module_make_resident (module);
318 gtk_modules = g_slist_prepend (gtk_modules, modinit_func);
322 g_module_close (module);
329 g_message ("Failed to load module \"%s\": %s",
330 module ? g_module_name (module) : name,
333 g_module_close (module);
340 load_modules (const char *module_str)
342 gchar **module_path = get_module_path ();
343 gchar **module_names = pango_split_file_list (module_str);
344 GSList *gtk_modules = NULL;
347 for (i = 0; module_names[i]; i++)
348 gtk_modules = load_module (gtk_modules, module_path, module_names[i]);
350 gtk_modules = g_slist_reverse (gtk_modules);
352 g_strfreev (module_names);
353 g_strfreev (module_path);
359 gtk_init_check (int *argc,
362 GString *gtk_modules_string = NULL;
363 GSList *gtk_modules = NULL;
370 if (!check_setugid ())
374 g_set_error_handler (gtk_error);
375 g_set_warning_handler (gtk_warning);
376 g_set_message_handler (gtk_message);
377 g_set_print_handler (gtk_print);
380 /* Initialize "gdk". We pass along the 'argc' and 'argv'
381 * parameters as they contain information that GDK uses
383 if (!gdk_init_check (argc, argv))
386 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
388 #ifdef G_ENABLE_DEBUG
389 env_string = getenv ("GTK_DEBUG");
390 if (env_string != NULL)
392 gtk_debug_flags = g_parse_debug_string (env_string,
397 #endif /* G_ENABLE_DEBUG */
399 env_string = getenv ("GTK_MODULES");
401 gtk_modules_string = g_string_new (env_string);
407 for (i = 1; i < *argc;)
409 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
410 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
412 gchar *module_name = (*argv)[i] + 12;
414 if (*module_name == '=')
416 else if (i + 1 < *argc)
420 module_name = (*argv)[i];
424 if (module_name && *module_name)
426 if (gtk_modules_string)
427 g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
429 gtk_modules_string = g_string_new (NULL);
431 g_string_append (gtk_modules_string, module_name);
434 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
436 GLogLevelFlags fatal_mask;
438 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
439 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
440 g_log_set_always_fatal (fatal_mask);
443 #ifdef G_ENABLE_DEBUG
444 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
445 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
447 gchar *equal_pos = strchr ((*argv)[i], '=');
449 if (equal_pos != NULL)
451 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
455 else if ((i + 1) < *argc && (*argv)[i + 1])
457 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
465 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
466 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
468 gchar *equal_pos = strchr ((*argv)[i], '=');
470 if (equal_pos != NULL)
472 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
476 else if ((i + 1) < *argc && (*argv)[i + 1])
478 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
486 #endif /* G_ENABLE_DEBUG */
490 for (i = 1; i < *argc; i++)
492 for (k = i; k < *argc; k++)
493 if ((*argv)[k] != NULL)
499 for (j = i + k; j < *argc; j++)
500 (*argv)[j-k] = (*argv)[j];
506 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
507 gdk_window_set_debug_updates (TRUE);
509 /* load gtk modules */
510 if (gtk_modules_string)
512 gtk_modules = load_modules (gtk_modules_string->str);
513 g_string_free (gtk_modules_string, TRUE);
518 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
519 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
520 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
522 # else /* !G_OS_WIN32 */
524 bindtextdomain (GETTEXT_PACKAGE,
525 g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
526 g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
533 /* Translate to default:RTL if you want your widgets
534 * to be RTL, otherwise translate to default:LTR.
535 * Do *not* translate it to "predefinito:LTR", if it
536 * it isn't default:LTR or default:RTL it will not work
538 char *e = _("default:LTR");
539 if (strcmp (e, "default:RTL")==0) {
540 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
541 } else if (strcmp (e, "default:LTR")) {
542 g_warning ("Whoever translated default:LTR did so wrongly.\n");
546 /* Initialize the default visual and colormap to be
547 * used in creating widgets. (We want to use the system
548 * defaults so as to be nice to the colormap).
550 gtk_visual = gdk_visual_get_system ();
551 gtk_colormap = gdk_colormap_get_system ();
557 /* Register an exit function to make sure we are able to cleanup.
559 g_atexit (gtk_exit_func);
561 /* Set the 'initialized' flag.
563 gtk_initialized = TRUE;
565 /* initialize gtk modules
567 for (slist = gtk_modules; slist; slist = slist->next)
571 GtkModuleInitFunc modinit;
573 modinit = slist->data;
574 modinit (argc, argv);
577 g_slist_free (gtk_modules);
580 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
581 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
582 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
583 " if you don't know why you are getting this, you probably want to\n"
584 " use the stable branch which can be retrived from\n"
585 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
586 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
595 gtk_init (int *argc, char ***argv)
597 if (!gtk_init_check (argc, argv))
599 g_warning ("cannot open display: %s", gdk_get_display ());
607 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
609 if (sizeof_GtkWindow != sizeof (GtkWindow))
610 g_error ("Incompatible build!\n"
611 "The code using GTK+ thinks GtkWindow is of different\n"
612 "size than it actually is in this build of GTK+.\n"
613 "On Windows, this probably means that you have compiled\n"
614 "your code with gcc without the -fnative-struct switch.");
617 /* These two functions might get more checks added later, thus pass
618 * in the number of extra args.
621 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
623 check_sizeof_GtkWindow (sizeof_GtkWindow);
624 gtk_init (argc, argv);
628 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
630 check_sizeof_GtkWindow (sizeof_GtkWindow);
631 return gtk_init_check (argc, argv);
637 gtk_exit (gint errorcode)
639 /* Only if "gtk" has been initialized should we de-initialize.
641 /* de-initialisation is done by the gtk_exit_funct(),
642 * no need to do this here (Alex J.)
644 gdk_exit (errorcode);
652 * Initializes internationalization support for GTK+. You
653 * should call this function before gtk_init() if your application
654 * supports internationalization.
656 * (In gory detail - sets the current locale according to the
657 * program environment. This is the same as calling the libc function
658 * setlocale (LC_ALL, "") but also takes care of the locale specific
659 * setup of the windowing system used by GDK.)
661 * Return value: a string corresponding to the locale set, as with the C library function setlocale()
664 gtk_set_locale (void)
666 return gdk_set_locale ();
670 * gtk_get_default_language:
672 * Returns the ISO language code for the default language currently in
673 * effect. (Note that this can change over the life of an
674 * application.) The default language is derived from the current
675 * locale. It determines, for example, whether GTK+ uses the
676 * right-to-left or left-to-right text direction.
678 * Return value: the default language as an allocated string, must be freed
681 gtk_get_default_language (void)
684 PangoLanguage *result;
687 lang = g_strdup (setlocale (LC_CTYPE, NULL));
688 p = strchr (lang, '.');
691 p = strchr (lang, '@');
695 result = pango_language_from_string (lang);
706 GtkInitFunction *init;
709 gtk_main_loop_level++;
711 loop = g_main_new (TRUE);
712 main_loops = g_slist_prepend (main_loops, loop);
714 tmp_list = functions = init_functions;
715 init_functions = NULL;
719 init = tmp_list->data;
720 tmp_list = tmp_list->next;
722 (* init->function) (init->data);
725 g_list_free (functions);
727 if (g_main_is_running (main_loops->data))
729 GDK_THREADS_LEAVE ();
731 GDK_THREADS_ENTER ();
737 GList *reinvoke_list = NULL;
738 GtkQuitFunction *quitf;
740 while (quit_functions)
742 quitf = quit_functions->data;
744 tmp_list = quit_functions;
745 quit_functions = g_list_remove_link (quit_functions, quit_functions);
746 g_list_free_1 (tmp_list);
748 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
749 gtk_quit_invoke_function (quitf))
751 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
755 gtk_quit_destroy (quitf);
762 work = g_list_last (reinvoke_list);
764 quit_functions->prev = work;
765 work->next = quit_functions;
766 quit_functions = work;
772 main_loops = g_slist_remove (main_loops, loop);
774 g_main_destroy (loop);
776 gtk_main_loop_level--;
780 gtk_main_level (void)
782 return gtk_main_loop_level;
788 g_return_if_fail (main_loops != NULL);
790 g_main_quit (main_loops->data);
794 gtk_events_pending (void)
798 GDK_THREADS_LEAVE ();
799 result = g_main_pending ();
800 GDK_THREADS_ENTER ();
806 gtk_main_iteration (void)
808 GDK_THREADS_LEAVE ();
809 g_main_iteration (TRUE);
810 GDK_THREADS_ENTER ();
813 return !g_main_is_running (main_loops->data);
819 gtk_main_iteration_do (gboolean blocking)
821 GDK_THREADS_LEAVE ();
822 g_main_iteration (blocking);
823 GDK_THREADS_ENTER ();
826 return !g_main_is_running (main_loops->data);
832 gtk_main_do_event (GdkEvent *event)
834 GtkWidget *event_widget;
835 GtkWidget *grab_widget;
836 GtkWindowGroup *window_group;
837 GdkEvent *next_event;
840 /* If there are any events pending then get the next one.
842 next_event = gdk_event_peek ();
844 /* Try to compress enter/leave notify events. These event
845 * pairs occur when the mouse is dragged quickly across
846 * a window with many buttons (or through a menu). Instead
847 * of highlighting and de-highlighting each widget that
848 * is crossed it is better to simply de-highlight the widget
849 * which contained the mouse initially and highlight the
850 * widget which ends up containing the mouse.
853 if (((event->type == GDK_ENTER_NOTIFY) ||
854 (event->type == GDK_LEAVE_NOTIFY)) &&
855 ((next_event->type == GDK_ENTER_NOTIFY) ||
856 (next_event->type == GDK_LEAVE_NOTIFY)) &&
857 (next_event->type != event->type) &&
858 (next_event->any.window == event->any.window))
860 /* Throw both the peeked copy and the queued copy away
862 gdk_event_free (next_event);
863 next_event = gdk_event_get ();
864 gdk_event_free (next_event);
870 gdk_event_free (next_event);
872 /* Find the widget which got the event. We store the widget
873 * in the user_data field of GdkWindow's.
874 * Ignore the event if we don't have a widget for it, except
875 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
876 * Though this happens rarely, bogus events can occour
877 * for e.g. destroyed GdkWindows.
879 event_widget = gtk_get_event_widget (event);
882 /* To handle selection INCR transactions, we select
883 * PropertyNotify events on the requestor window and create
884 * a corresponding (fake) GdkWindow so that events get
885 * here. There won't be a widget though, so we have to handle
888 if (event->type == GDK_PROPERTY_NOTIFY)
889 gtk_selection_incr_event (event->any.window,
891 else if (event->type == GDK_SETTING)
892 _gtk_settings_handle_event (&event->setting);
897 /* Push the event onto a stack of current events for
898 * gtk_current_event_get().
900 current_events = g_list_prepend (current_events, event);
902 window_group = gtk_main_get_window_group (event_widget);
904 /* If there is a grab in effect...
906 if (window_group->grabs)
908 grab_widget = window_group->grabs->data;
910 /* If the grab widget is an ancestor of the event widget
911 * then we send the event to the original event widget.
912 * This is the key to implementing modality.
914 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
915 gtk_widget_is_ancestor (event_widget, grab_widget))
916 grab_widget = event_widget;
920 grab_widget = event_widget;
923 /* Not all events get sent to the grabbing widget.
924 * The delete, destroy, expose, focus change and resize
925 * events still get sent to the event widget because
926 * 1) these events have no meaning for the grabbing widget
927 * and 2) redirecting these events to the grabbing widget
928 * could cause the display to be messed up.
930 * Drag events are also not redirected, since it isn't
931 * clear what the semantics of that would be.
939 gtk_widget_ref (event_widget);
940 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
941 !gtk_widget_event (event_widget, event))
942 gtk_widget_destroy (event_widget);
943 gtk_widget_unref (event_widget);
947 /* Unexpected GDK_DESTROY from the outside, ignore for
948 * child windows, handle like a GDK_DELETE for toplevels
950 if (!event_widget->parent)
952 gtk_widget_ref (event_widget);
953 if (!gtk_widget_event (event_widget, event) &&
954 GTK_WIDGET_REALIZED (event_widget))
955 gtk_widget_destroy (event_widget);
956 gtk_widget_unref (event_widget);
961 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
963 gdk_window_begin_paint_region (event->any.window, event->expose.region);
964 gtk_widget_send_expose (event_widget, event);
965 gdk_window_end_paint (event->any.window);
968 gtk_widget_send_expose (event_widget, event);
971 case GDK_PROPERTY_NOTIFY:
973 case GDK_FOCUS_CHANGE:
977 case GDK_SELECTION_CLEAR:
978 case GDK_SELECTION_REQUEST:
979 case GDK_SELECTION_NOTIFY:
980 case GDK_CLIENT_EVENT:
981 case GDK_VISIBILITY_NOTIFY:
982 case GDK_WINDOW_STATE:
983 gtk_widget_event (event_widget, event);
987 case GDK_BUTTON_PRESS:
988 case GDK_2BUTTON_PRESS:
989 case GDK_3BUTTON_PRESS:
990 gtk_propagate_event (grab_widget, event);
994 case GDK_KEY_RELEASE:
997 if (gtk_invoke_key_snoopers (grab_widget, event))
1000 /* else fall through */
1001 case GDK_MOTION_NOTIFY:
1002 case GDK_BUTTON_RELEASE:
1003 case GDK_PROXIMITY_IN:
1004 case GDK_PROXIMITY_OUT:
1005 gtk_propagate_event (grab_widget, event);
1008 case GDK_ENTER_NOTIFY:
1009 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1011 gtk_widget_event (grab_widget, event);
1012 if (event_widget == grab_widget)
1013 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
1017 case GDK_LEAVE_NOTIFY:
1018 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
1020 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
1021 gtk_widget_event (event_widget, event);
1023 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
1024 gtk_widget_event (grab_widget, event);
1027 case GDK_DRAG_STATUS:
1028 case GDK_DROP_FINISHED:
1029 _gtk_drag_source_handle_event (event_widget, event);
1031 case GDK_DRAG_ENTER:
1032 case GDK_DRAG_LEAVE:
1033 case GDK_DRAG_MOTION:
1034 case GDK_DROP_START:
1035 _gtk_drag_dest_handle_event (event_widget, event);
1038 g_assert_not_reached ();
1042 tmp_list = current_events;
1043 current_events = g_list_remove_link (current_events, tmp_list);
1044 g_list_free_1 (tmp_list);
1059 static GtkWindowGroup *
1060 gtk_main_get_window_group (GtkWidget *widget)
1062 GtkWidget *toplevel = NULL;
1065 toplevel = gtk_widget_get_toplevel (widget);
1067 if (toplevel && GTK_IS_WINDOW (toplevel))
1068 return _gtk_window_get_group (GTK_WINDOW (toplevel));
1070 return _gtk_window_get_group (NULL);
1075 gboolean was_grabbed;
1076 GtkWidget *grab_widget;
1080 gtk_grab_notify_foreach (GtkWidget *child,
1084 GrabNotifyInfo *info = data;
1086 if (child != info->grab_widget)
1088 g_object_ref (G_OBJECT (child));
1090 gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", info->was_grabbed);
1092 if (GTK_IS_CONTAINER (child))
1093 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1095 g_object_unref (G_OBJECT (child));
1100 gtk_grab_notify (GtkWindowGroup *group,
1101 GtkWidget *grab_widget,
1102 gboolean was_grabbed)
1105 GrabNotifyInfo info;
1107 info.grab_widget = grab_widget;
1108 info.was_grabbed = was_grabbed;
1110 g_object_ref (group);
1111 g_object_ref (grab_widget);
1113 toplevels = gtk_window_list_toplevels ();
1114 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1118 GtkWindow *toplevel = toplevels->data;
1119 toplevels = g_list_delete_link (toplevels, toplevels);
1121 if (group == toplevel->group)
1122 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1123 g_object_unref (toplevel);
1126 g_object_unref (group);
1127 g_object_unref (grab_widget);
1131 gtk_grab_add (GtkWidget *widget)
1133 GtkWindowGroup *group;
1134 gboolean was_grabbed;
1136 g_return_if_fail (widget != NULL);
1138 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1140 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1142 group = gtk_main_get_window_group (widget);
1144 was_grabbed = (group->grabs != NULL);
1146 gtk_widget_ref (widget);
1147 group->grabs = g_slist_prepend (group->grabs, widget);
1150 gtk_grab_notify (group, widget, FALSE);
1155 gtk_grab_get_current (void)
1157 GtkWindowGroup *group;
1159 group = gtk_main_get_window_group (NULL);
1162 return GTK_WIDGET (group->grabs->data);
1167 gtk_grab_remove (GtkWidget *widget)
1169 GtkWindowGroup *group;
1171 g_return_if_fail (widget != NULL);
1173 if (GTK_WIDGET_HAS_GRAB (widget))
1175 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1177 group = gtk_main_get_window_group (widget);
1178 group->grabs = g_slist_remove (group->grabs, widget);
1180 gtk_widget_unref (widget);
1183 gtk_grab_notify (group, widget, TRUE);
1188 gtk_init_add (GtkFunction function,
1191 GtkInitFunction *init;
1193 init = g_new (GtkInitFunction, 1);
1194 init->function = function;
1197 init_functions = g_list_prepend (init_functions, init);
1201 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1204 GtkKeySnooperData *data;
1205 static guint snooper_id = 1;
1207 g_return_val_if_fail (snooper != NULL, 0);
1209 data = g_new (GtkKeySnooperData, 1);
1210 data->func = snooper;
1211 data->func_data = func_data;
1212 data->id = snooper_id++;
1213 key_snoopers = g_slist_prepend (key_snoopers, data);
1219 gtk_key_snooper_remove (guint snooper_id)
1221 GtkKeySnooperData *data = NULL;
1224 slist = key_snoopers;
1228 if (data->id == snooper_id)
1231 slist = slist->next;
1235 key_snoopers = g_slist_remove (key_snoopers, data);
1239 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1243 gint return_val = FALSE;
1245 slist = key_snoopers;
1246 while (slist && !return_val)
1248 GtkKeySnooperData *data;
1251 slist = slist->next;
1252 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1259 gtk_quit_add_full (guint main_level,
1260 GtkFunction function,
1261 GtkCallbackMarshal marshal,
1263 GtkDestroyNotify destroy)
1265 static guint quit_id = 1;
1266 GtkQuitFunction *quitf;
1268 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1270 if (!quit_mem_chunk)
1271 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1272 512, G_ALLOC_AND_FREE);
1274 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1276 quitf->id = quit_id++;
1277 quitf->main_level = main_level;
1278 quitf->function = function;
1279 quitf->marshal = marshal;
1281 quitf->destroy = destroy;
1283 quit_functions = g_list_prepend (quit_functions, quitf);
1289 gtk_quit_destroy (GtkQuitFunction *quitf)
1292 quitf->destroy (quitf->data);
1293 g_mem_chunk_free (quit_mem_chunk, quitf);
1297 gtk_quit_destructor (GtkObject **object_p)
1300 gtk_object_destroy (*object_p);
1307 gtk_quit_add_destroy (guint main_level,
1310 GtkObject **object_p;
1312 g_return_if_fail (main_level > 0);
1313 g_return_if_fail (GTK_IS_OBJECT (object));
1315 object_p = g_new (GtkObject*, 1);
1317 gtk_signal_connect (object,
1319 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1321 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1325 gtk_quit_add (guint main_level,
1326 GtkFunction function,
1329 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1333 gtk_quit_remove (guint id)
1335 GtkQuitFunction *quitf;
1338 tmp_list = quit_functions;
1341 quitf = tmp_list->data;
1343 if (quitf->id == id)
1345 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1346 g_list_free (tmp_list);
1347 gtk_quit_destroy (quitf);
1352 tmp_list = tmp_list->next;
1357 gtk_quit_remove_by_data (gpointer data)
1359 GtkQuitFunction *quitf;
1362 tmp_list = quit_functions;
1365 quitf = tmp_list->data;
1367 if (quitf->data == data)
1369 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1370 g_list_free (tmp_list);
1371 gtk_quit_destroy (quitf);
1376 tmp_list = tmp_list->next;
1381 gtk_timeout_add_full (guint32 interval,
1382 GtkFunction function,
1383 GtkCallbackMarshal marshal,
1385 GtkDestroyNotify destroy)
1389 GtkClosure *closure;
1391 closure = g_new (GtkClosure, 1);
1392 closure->marshal = marshal;
1393 closure->data = data;
1394 closure->destroy = destroy;
1396 return g_timeout_add_full (0, interval,
1397 gtk_invoke_idle_timeout,
1399 gtk_destroy_closure);
1402 return g_timeout_add_full (0, interval, function, data, destroy);
1406 gtk_timeout_add (guint32 interval,
1407 GtkFunction function,
1410 return g_timeout_add_full (0, interval, function, data, NULL);
1414 gtk_timeout_remove (guint tag)
1416 g_source_remove (tag);
1420 gtk_idle_add_full (gint priority,
1421 GtkFunction function,
1422 GtkCallbackMarshal marshal,
1424 GtkDestroyNotify destroy)
1428 GtkClosure *closure;
1430 closure = g_new (GtkClosure, 1);
1431 closure->marshal = marshal;
1432 closure->data = data;
1433 closure->destroy = destroy;
1435 return g_idle_add_full (priority,
1436 gtk_invoke_idle_timeout,
1438 gtk_destroy_closure);
1441 return g_idle_add_full (priority, function, data, destroy);
1445 gtk_idle_add (GtkFunction function,
1448 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1452 gtk_idle_add_priority (gint priority,
1453 GtkFunction function,
1456 return g_idle_add_full (priority, function, data, NULL);
1460 gtk_idle_remove (guint tag)
1462 g_source_remove (tag);
1466 gtk_idle_remove_by_data (gpointer data)
1468 if (!g_idle_remove_by_data (data))
1469 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1473 gtk_input_add_full (gint source,
1474 GdkInputCondition condition,
1475 GdkInputFunction function,
1476 GtkCallbackMarshal marshal,
1478 GtkDestroyNotify destroy)
1482 GtkClosure *closure;
1484 closure = g_new (GtkClosure, 1);
1485 closure->marshal = marshal;
1486 closure->data = data;
1487 closure->destroy = destroy;
1489 return gdk_input_add_full (source,
1491 (GdkInputFunction) gtk_invoke_input,
1493 (GdkDestroyNotify) gtk_destroy_closure);
1496 return gdk_input_add_full (source, condition, function, data, destroy);
1500 gtk_input_remove (guint tag)
1502 g_source_remove (tag);
1506 gtk_destroy_closure (gpointer data)
1508 GtkClosure *closure = data;
1510 if (closure->destroy)
1511 (closure->destroy) (closure->data);
1516 gtk_invoke_idle_timeout (gpointer data)
1518 GtkClosure *closure = data;
1521 gint ret_val = FALSE;
1522 args[0].name = NULL;
1523 args[0].type = GTK_TYPE_BOOL;
1524 args[0].d.pointer_data = &ret_val;
1525 closure->marshal (NULL, closure->data, 0, args);
1530 gtk_invoke_input (gpointer data,
1532 GdkInputCondition condition)
1534 GtkClosure *closure = data;
1537 args[0].type = GTK_TYPE_INT;
1538 args[0].name = NULL;
1539 GTK_VALUE_INT (args[0]) = source;
1540 args[1].type = GDK_TYPE_INPUT_CONDITION;
1541 args[1].name = NULL;
1542 GTK_VALUE_FLAGS (args[1]) = condition;
1543 args[2].type = GTK_TYPE_NONE;
1544 args[2].name = NULL;
1546 closure->marshal (NULL, closure->data, 2, args);
1550 * gtk_get_current_event:
1552 * Obtains a copy of the event currently being processed by GTK+. For
1553 * example, if you get a "clicked" signal from #GtkButton, the current
1554 * event will be the #GdkEventButton that triggered the "clicked"
1555 * signal. The returned event must be freed with gdk_event_free().
1556 * If there is no current event, the function returns %NULL.
1558 * Return value: a copy of the current event, or %NULL if no current event.
1561 gtk_get_current_event (void)
1564 return gdk_event_copy (current_events->data);
1570 * gtk_get_current_event_time:
1572 * If there is a current event and it has a timestamp, return that
1573 * timestamp, otherwise return %GDK_CURRENT_TIME.
1575 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1578 gtk_get_current_event_time (void)
1581 return gdk_event_get_time (current_events->data);
1583 return GDK_CURRENT_TIME;
1587 * gtk_get_current_event_state:
1588 * @state: a location to store the state of the current event
1590 * If there is a current event and it has a state field, place
1591 * that state field in @state and return %TRUE, otherwise return
1594 * Return value: %TRUE if there was a current event and it had a state field
1597 gtk_get_current_event_state (GdkModifierType *state)
1599 g_return_val_if_fail (state != NULL, FALSE);
1602 return gdk_event_get_state (current_events->data, state);
1611 * gtk_get_event_widget:
1612 * @event: a #GdkEvent
1614 * If @event is %NULL or the event was not associated with any widget,
1615 * returns %NULL, otherwise returns the widget that received the event
1618 * Return value: the widget that originally received @event, or %NULL
1621 gtk_get_event_widget (GdkEvent *event)
1626 if (event && event->any.window)
1627 gdk_window_get_user_data (event->any.window, (void**) &widget);
1633 gtk_exit_func (void)
1635 if (gtk_initialized)
1637 gtk_initialized = FALSE;
1643 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1645 if (!quitf->marshal)
1646 return quitf->function (quitf->data);
1650 gint ret_val = FALSE;
1652 args[0].name = NULL;
1653 args[0].type = GTK_TYPE_BOOL;
1654 args[0].d.pointer_data = &ret_val;
1655 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1663 * gtk_propagate_event:
1664 * @widget: a #GtkWidget
1667 * Sends an event to a widget, propagating the event to parent widgets
1668 * if the event remains unhandled. Events received by GTK+ from GDK
1669 * normally begin in gtk_main_do_event(). Depending on the type of
1670 * event, existence of modal dialogs, grabs, etc., the event may be
1671 * propagated; if so, this function is used. gtk_propagate_event()
1672 * calls gtk_widget_event() on each widget it decides to send the
1673 * event to. So gtk_widget_event() is the lowest-level function; it
1674 * simply emits the "event" and possibly an event-specific signal on a
1675 * widget. gtk_propagate_event() is a bit higher-level, and
1676 * gtk_main_do_event() is the highest level.
1678 * All that said, you most likely don't want to use any of these
1679 * functions; synthesizing events is rarely needed. Consider asking on
1680 * the mailing list for better ways to achieve your goals. For
1681 * example, use gdk_window_invalidate_rect() or
1682 * gtk_widget_queue_draw() instead of making up expose events.
1686 gtk_propagate_event (GtkWidget *widget,
1691 g_return_if_fail (GTK_IS_WIDGET (widget));
1692 g_return_if_fail (event != NULL);
1694 handled_event = FALSE;
1696 gtk_widget_ref (widget);
1698 if ((event->type == GDK_KEY_PRESS) ||
1699 (event->type == GDK_KEY_RELEASE))
1701 /* Only send key events within Window widgets to the Window
1702 * The Window widget will in turn pass the
1703 * key event on to the currently focused widget
1708 window = gtk_widget_get_toplevel (widget);
1709 if (window && GTK_IS_WINDOW (window))
1711 /* If there is a grab within the window, give the grab widget
1712 * a first crack at the key event
1714 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1715 handled_event = gtk_widget_event (widget, event);
1719 window = gtk_widget_get_toplevel (widget);
1720 if (window && GTK_IS_WINDOW (window))
1722 if (GTK_WIDGET_IS_SENSITIVE (window))
1723 gtk_widget_event (window, event);
1727 handled_event = TRUE; /* don't send to widget */
1731 /* Other events get propagated up the widget tree
1732 * so that parents can see the button and motion
1733 * events of the children.
1741 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1742 tmp = widget->parent;
1743 gtk_widget_unref (widget);
1747 if (!handled_event && widget)
1748 gtk_widget_ref (widget);
1754 gtk_widget_unref (widget);
1759 gtk_error (gchar *str)
1765 gtk_warning (gchar *str)
1771 gtk_message (gchar *str)
1777 gtk_print (gchar *str)
1779 static GtkWidget *window = NULL;
1780 static GtkWidget *text;
1781 static int level = 0;
1785 GtkWidget *hscrollbar;
1786 GtkWidget *vscrollbar;
1787 GtkWidget *separator;
1792 fputs (str, stdout);
1799 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1801 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1802 (GtkSignalFunc) gtk_widget_destroyed,
1805 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1807 box1 = gtk_vbox_new (FALSE, 0);
1808 gtk_container_add (GTK_CONTAINER (window), box1);
1809 gtk_widget_show (box1);
1812 box2 = gtk_vbox_new (FALSE, 10);
1813 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1814 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1815 gtk_widget_show (box2);
1818 table = gtk_table_new (2, 2, FALSE);
1819 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1820 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1821 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1822 gtk_widget_show (table);
1824 text = gtk_text_new (NULL, NULL);
1825 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1826 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1827 gtk_widget_show (text);
1828 gtk_widget_realize (text);
1830 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1831 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1832 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1833 gtk_widget_show (hscrollbar);
1835 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1836 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1837 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1838 gtk_widget_show (vscrollbar);
1840 separator = gtk_hseparator_new ();
1841 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1842 gtk_widget_show (separator);
1845 box2 = gtk_vbox_new (FALSE, 10);
1846 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1847 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1848 gtk_widget_show (box2);
1851 button = gtk_button_new_with_label ("close");
1852 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1853 (GtkSignalFunc) gtk_widget_hide,
1854 GTK_OBJECT (window));
1855 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1856 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1857 gtk_widget_grab_default (button);
1858 gtk_widget_show (button);
1862 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1865 if (!GTK_WIDGET_VISIBLE (window))
1866 gtk_widget_show (window);
1871 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1872 GValue *return_accu,
1873 const GValue *handler_return,
1876 gboolean continue_emission;
1877 gboolean signal_handled;
1879 signal_handled = g_value_get_boolean (handler_return);
1880 g_value_set_boolean (return_accu, signal_handled);
1881 continue_emission = !signal_handled;
1883 return continue_emission;