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 "gtkversion.h"
46 #include "gtkselection.h"
47 #include "gtksettings.h"
48 #include "gtksignal.h"
49 #include "gtkwidget.h"
50 #include "gtkwindow.h"
51 #include "gtkprivate.h"
52 #include "gdk/gdki18n.h"
57 /* Private type definitions
59 typedef struct _GtkInitFunction GtkInitFunction;
60 typedef struct _GtkQuitFunction GtkQuitFunction;
61 typedef struct _GtkClosure GtkClosure;
62 typedef struct _GtkKeySnooperData GtkKeySnooperData;
64 struct _GtkInitFunction
70 struct _GtkQuitFunction
74 GtkCallbackMarshal marshal;
77 GtkDestroyNotify destroy;
82 GtkCallbackMarshal marshal;
84 GtkDestroyNotify destroy;
87 struct _GtkKeySnooperData
94 static void gtk_exit_func (void);
95 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
96 static void gtk_quit_destroy (GtkQuitFunction *quitf);
97 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
100 static void gtk_destroy_closure (gpointer data);
101 static gboolean gtk_invoke_idle_timeout (gpointer data);
102 static void gtk_invoke_input (gpointer data,
104 GdkInputCondition condition);
107 static void gtk_error (gchar *str);
108 static void gtk_warning (gchar *str);
109 static void gtk_message (gchar *str);
110 static void gtk_print (gchar *str);
113 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget *widget);
115 const guint gtk_major_version = GTK_MAJOR_VERSION;
116 const guint gtk_minor_version = GTK_MINOR_VERSION;
117 const guint gtk_micro_version = GTK_MICRO_VERSION;
118 const guint gtk_binary_age = GTK_BINARY_AGE;
119 const guint gtk_interface_age = GTK_INTERFACE_AGE;
121 static guint gtk_main_loop_level = 0;
122 static gint gtk_initialized = FALSE;
123 static GList *current_events = NULL;
125 static GSList *main_loops = NULL; /* stack of currently executing main loops */
127 static GList *init_functions = NULL; /* A list of init functions.
129 static GList *quit_functions = NULL; /* A list of quit functions.
131 static GMemChunk *quit_mem_chunk = NULL;
133 static GSList *key_snoopers = NULL;
135 static GdkVisual *gtk_visual; /* The visual to be used in creating new
138 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
142 guint gtk_debug_flags = 0; /* Global GTK debug flag */
144 #ifdef G_ENABLE_DEBUG
145 static const GDebugKey gtk_debug_keys[] = {
146 {"misc", GTK_DEBUG_MISC},
147 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
148 {"text", GTK_DEBUG_TEXT},
149 {"tree", GTK_DEBUG_TREE},
150 {"updates", GTK_DEBUG_UPDATES}
153 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
155 #endif /* G_ENABLE_DEBUG */
158 gtk_check_version (guint required_major,
159 guint required_minor,
160 guint required_micro)
162 if (required_major > GTK_MAJOR_VERSION)
163 return "Gtk+ version too old (major mismatch)";
164 if (required_major < GTK_MAJOR_VERSION)
165 return "Gtk+ version too new (major mismatch)";
166 if (required_minor > GTK_MINOR_VERSION)
167 return "Gtk+ version too old (minor mismatch)";
168 if (required_minor < GTK_MINOR_VERSION)
169 return "Gtk+ version too new (minor mismatch)";
170 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
171 return "Gtk+ version too new (micro mismatch)";
172 if (required_micro > GTK_MICRO_VERSION)
173 return "Gtk+ version too old (micro mismatch)";
179 add_dll_suffix (gchar *module_name)
181 gchar *suffix = strrchr (module_name, '.');
183 if (!suffix || stricmp(suffix, ".dll"))
185 gchar *old = module_name;
187 module_name = g_strconcat (module_name, ".dll", NULL);
190 return (module_name);
194 #undef gtk_init_check
196 /* This checks to see if the process is running suid or sgid
197 * at the current time. If so, we don't allow GTK+ to be initialized.
198 * This is meant to be a mild check - we only error out if we
199 * can prove the programmer is doing something wrong, not if
200 * they could be doing something wrong. For this reason, we
201 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
206 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
208 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
209 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
211 #ifdef HAVE_GETRESUID
212 /* These aren't in the header files, so we prototype them here.
214 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
215 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
217 if (getresuid (&ruid, &euid, &suid) != 0 ||
218 getresgid (&rgid, &egid, &sgid) != 0)
219 #endif /* HAVE_GETRESUID */
221 suid = ruid = getuid ();
222 sgid = rgid = getgid ();
227 if (ruid != euid || ruid != suid ||
228 rgid != egid || rgid != sgid)
230 g_warning ("This process is currently running setuid or setgid.\n"
231 "This is not a supported use of GTK+. You must create a helper\n"
232 "program instead. For further details, see:\n\n"
233 " http://www.gtk.org/setuid.html\n\n"
234 "Refusing to initialize GTK+.");
242 gtk_init_check (int *argc,
245 GSList *gtk_modules = NULL;
247 gchar *env_string = NULL;
252 if (!check_setugid ())
256 g_set_error_handler (gtk_error);
257 g_set_warning_handler (gtk_warning);
258 g_set_message_handler (gtk_message);
259 g_set_print_handler (gtk_print);
262 /* Initialize "gdk". We pass along the 'argc' and 'argv'
263 * parameters as they contain information that GDK uses
265 if (!gdk_init_check (argc, argv))
268 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
270 #ifdef G_ENABLE_DEBUG
271 env_string = getenv ("GTK_DEBUG");
272 if (env_string != NULL)
274 gtk_debug_flags = g_parse_debug_string (env_string,
279 #endif /* G_ENABLE_DEBUG */
281 env_string = getenv ("GTK_MODULES");
284 gchar **modules, **as;
287 modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
289 modules = g_strsplit (env_string, ";", -1);
291 for (as = modules; *as; as++)
294 gtk_modules = g_slist_prepend (gtk_modules, *as);
306 for (i = 1; i < *argc;)
308 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
309 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
311 gchar *module_name = (*argv)[i] + 12;
313 if (*module_name == '=')
315 else if (i + 1 < *argc)
319 module_name = (*argv)[i];
323 if (module_name && *module_name)
324 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
326 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
328 GLogLevelFlags fatal_mask;
330 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
331 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
332 g_log_set_always_fatal (fatal_mask);
335 #ifdef G_ENABLE_DEBUG
336 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
337 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
339 gchar *equal_pos = strchr ((*argv)[i], '=');
341 if (equal_pos != NULL)
343 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
347 else if ((i + 1) < *argc && (*argv)[i + 1])
349 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
357 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
358 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
360 gchar *equal_pos = strchr ((*argv)[i], '=');
362 if (equal_pos != NULL)
364 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
368 else if ((i + 1) < *argc && (*argv)[i + 1])
370 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
378 #endif /* G_ENABLE_DEBUG */
382 for (i = 1; i < *argc; i++)
384 for (k = i; k < *argc; k++)
385 if ((*argv)[k] != NULL)
391 for (j = i + k; j < *argc; j++)
392 (*argv)[j-k] = (*argv)[j];
398 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
399 gdk_window_set_debug_updates (TRUE);
401 /* load gtk modules */
402 gtk_modules = g_slist_reverse (gtk_modules);
403 for (slist = gtk_modules; slist; slist = slist->next)
406 GModule *module = NULL;
407 GtkModuleInitFunc modinit_func = NULL;
409 module_name = slist->data;
412 if (!g_path_is_absolute (module_name))
414 gchar *old = module_name;
416 module_name = g_module_build_path (NULL, module_name);
420 module_name = add_dll_suffix (module_name);
422 if (g_module_supported ())
424 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
426 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
429 if (!g_slist_find (gtk_modules, modinit_func))
431 g_module_make_resident (module);
432 slist->data = modinit_func;
436 g_module_close (module);
443 g_message ("Failed to load module \"%s\": %s",
444 module ? g_module_name (module) : module_name,
447 g_module_close (module);
449 g_free (module_name);
454 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
455 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
456 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
458 # else /* !G_OS_WIN32 */
460 bindtextdomain (GETTEXT_PACKAGE,
461 g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
462 g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
469 /* Translate to default:RTL if you want your widgets
470 * to be RTL, otherwise translate to default:LTR.
471 * Do *not* translate it to "predefinito:LTR", if it
472 * it isn't default:LTR or default:RTL it will not work
474 char *e = _("default:LTR");
475 if (strcmp (e, "default:RTL")==0) {
476 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
477 } else if (strcmp (e, "default:LTR")) {
478 g_warning ("Whoever translated default:LTR did so wrongly.\n");
482 /* Initialize the default visual and colormap to be
483 * used in creating widgets. (We want to use the system
484 * defaults so as to be nice to the colormap).
486 gtk_visual = gdk_visual_get_system ();
487 gtk_colormap = gdk_colormap_get_system ();
493 /* Register an exit function to make sure we are able to cleanup.
495 g_atexit (gtk_exit_func);
497 /* Set the 'initialized' flag.
499 gtk_initialized = TRUE;
501 /* initialize gtk modules
503 for (slist = gtk_modules; slist; slist = slist->next)
507 GtkModuleInitFunc modinit;
509 modinit = slist->data;
510 modinit (argc, argv);
513 g_slist_free (gtk_modules);
516 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
517 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
518 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
519 " if you don't know why you are getting this, you probably want to\n"
520 " use the stable branch which can be retrived from\n"
521 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
522 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
531 gtk_init (int *argc, char ***argv)
533 if (!gtk_init_check (argc, argv))
535 g_warning ("cannot open display: %s", gdk_get_display ());
543 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
545 if (sizeof_GtkWindow != sizeof (GtkWindow))
546 g_error ("Incompatible build!\n"
547 "The code using GTK+ thinks GtkWindow is of different\n"
548 "size than it actually is in this build of GTK+.\n"
549 "On Windows, this probably means that you have compiled\n"
550 "your code with gcc without the -fnative-struct switch.");
553 /* These two functions might get more checks added later, thus pass
554 * in the number of extra args.
557 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
559 check_sizeof_GtkWindow (sizeof_GtkWindow);
560 gtk_init (argc, argv);
564 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
566 check_sizeof_GtkWindow (sizeof_GtkWindow);
567 return gtk_init_check (argc, argv);
573 gtk_exit (gint errorcode)
575 /* Only if "gtk" has been initialized should we de-initialize.
577 /* de-initialisation is done by the gtk_exit_funct(),
578 * no need to do this here (Alex J.)
580 gdk_exit (errorcode);
584 gtk_set_locale (void)
586 return gdk_set_locale ();
590 * gtk_get_default_language:
592 * Returns the ISO language code for the default language currently in
593 * effect. (Note that this can change over the life of an
594 * application.) The default language is derived from the current
595 * locale. It determines, for example, whether GTK+ uses the
596 * right-to-left or left-to-right text direction.
598 * Return value: the default language as an allocated string, must be freed
601 gtk_get_default_language (void)
604 PangoLanguage *result;
607 lang = g_strdup (setlocale (LC_CTYPE, NULL));
608 p = strchr (lang, '.');
611 p = strchr (lang, '@');
615 result = pango_language_from_string (lang);
626 GtkInitFunction *init;
629 gtk_main_loop_level++;
631 loop = g_main_new (TRUE);
632 main_loops = g_slist_prepend (main_loops, loop);
634 tmp_list = functions = init_functions;
635 init_functions = NULL;
639 init = tmp_list->data;
640 tmp_list = tmp_list->next;
642 (* init->function) (init->data);
645 g_list_free (functions);
647 if (g_main_is_running (main_loops->data))
649 GDK_THREADS_LEAVE ();
651 GDK_THREADS_ENTER ();
657 GList *reinvoke_list = NULL;
658 GtkQuitFunction *quitf;
660 while (quit_functions)
662 quitf = quit_functions->data;
664 tmp_list = quit_functions;
665 quit_functions = g_list_remove_link (quit_functions, quit_functions);
666 g_list_free_1 (tmp_list);
668 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
669 gtk_quit_invoke_function (quitf))
671 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
675 gtk_quit_destroy (quitf);
682 work = g_list_last (reinvoke_list);
684 quit_functions->prev = work;
685 work->next = quit_functions;
686 quit_functions = work;
692 main_loops = g_slist_remove (main_loops, loop);
694 g_main_destroy (loop);
696 gtk_main_loop_level--;
700 gtk_main_level (void)
702 return gtk_main_loop_level;
708 g_return_if_fail (main_loops != NULL);
710 g_main_quit (main_loops->data);
714 gtk_events_pending (void)
718 GDK_THREADS_LEAVE ();
719 result = g_main_pending ();
720 GDK_THREADS_ENTER ();
726 gtk_main_iteration (void)
728 GDK_THREADS_LEAVE ();
729 g_main_iteration (TRUE);
730 GDK_THREADS_ENTER ();
733 return !g_main_is_running (main_loops->data);
739 gtk_main_iteration_do (gboolean blocking)
741 GDK_THREADS_LEAVE ();
742 g_main_iteration (blocking);
743 GDK_THREADS_ENTER ();
746 return !g_main_is_running (main_loops->data);
752 gtk_main_do_event (GdkEvent *event)
754 GtkWidget *event_widget;
755 GtkWidget *grab_widget;
756 GtkWindowGroup *window_group;
757 GdkEvent *next_event;
760 /* If there are any events pending then get the next one.
762 next_event = gdk_event_peek ();
764 /* Try to compress enter/leave notify events. These event
765 * pairs occur when the mouse is dragged quickly across
766 * a window with many buttons (or through a menu). Instead
767 * of highlighting and de-highlighting each widget that
768 * is crossed it is better to simply de-highlight the widget
769 * which contained the mouse initially and highlight the
770 * widget which ends up containing the mouse.
773 if (((event->type == GDK_ENTER_NOTIFY) ||
774 (event->type == GDK_LEAVE_NOTIFY)) &&
775 ((next_event->type == GDK_ENTER_NOTIFY) ||
776 (next_event->type == GDK_LEAVE_NOTIFY)) &&
777 (next_event->type != event->type) &&
778 (next_event->any.window == event->any.window))
780 /* Throw both the peeked copy and the queued copy away
782 gdk_event_free (next_event);
783 next_event = gdk_event_get ();
784 gdk_event_free (next_event);
790 gdk_event_free (next_event);
792 /* Find the widget which got the event. We store the widget
793 * in the user_data field of GdkWindow's.
794 * Ignore the event if we don't have a widget for it, except
795 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
796 * Though this happens rarely, bogus events can occour
797 * for e.g. destroyed GdkWindows.
799 event_widget = gtk_get_event_widget (event);
802 /* To handle selection INCR transactions, we select
803 * PropertyNotify events on the requestor window and create
804 * a corresponding (fake) GdkWindow so that events get
805 * here. There won't be a widget though, so we have to handle
808 if (event->type == GDK_PROPERTY_NOTIFY)
809 gtk_selection_incr_event (event->any.window,
811 else if (event->type == GDK_SETTING)
812 _gtk_settings_handle_event (&event->setting);
817 /* Push the event onto a stack of current events for
818 * gtk_current_event_get().
820 current_events = g_list_prepend (current_events, event);
822 window_group = gtk_main_get_window_group (event_widget);
824 /* If there is a grab in effect...
826 if (window_group->grabs)
828 grab_widget = window_group->grabs->data;
830 /* If the grab widget is an ancestor of the event widget
831 * then we send the event to the original event widget.
832 * This is the key to implementing modality.
834 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
835 gtk_widget_is_ancestor (event_widget, grab_widget))
836 grab_widget = event_widget;
840 grab_widget = event_widget;
843 /* Not all events get sent to the grabbing widget.
844 * The delete, destroy, expose, focus change and resize
845 * events still get sent to the event widget because
846 * 1) these events have no meaning for the grabbing widget
847 * and 2) redirecting these events to the grabbing widget
848 * could cause the display to be messed up.
850 * Drag events are also not redirected, since it isn't
851 * clear what the semantics of that would be.
859 gtk_widget_ref (event_widget);
860 if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
861 !gtk_widget_event (event_widget, event))
862 gtk_widget_destroy (event_widget);
863 gtk_widget_unref (event_widget);
867 /* Unexpected GDK_DESTROY from the outside, ignore for
868 * child windows, handle like a GDK_DELETE for toplevels
870 if (!event_widget->parent)
872 gtk_widget_ref (event_widget);
873 if (!gtk_widget_event (event_widget, event) &&
874 GTK_WIDGET_REALIZED (event_widget))
875 gtk_widget_destroy (event_widget);
876 gtk_widget_unref (event_widget);
881 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
883 gdk_window_begin_paint_region (event->any.window, event->expose.region);
884 gtk_widget_send_expose (event_widget, event);
885 gdk_window_end_paint (event->any.window);
888 gtk_widget_send_expose (event_widget, event);
891 case GDK_PROPERTY_NOTIFY:
893 case GDK_FOCUS_CHANGE:
897 case GDK_SELECTION_CLEAR:
898 case GDK_SELECTION_REQUEST:
899 case GDK_SELECTION_NOTIFY:
900 case GDK_CLIENT_EVENT:
901 case GDK_VISIBILITY_NOTIFY:
902 case GDK_WINDOW_STATE:
903 gtk_widget_event (event_widget, event);
907 case GDK_BUTTON_PRESS:
908 case GDK_2BUTTON_PRESS:
909 case GDK_3BUTTON_PRESS:
910 gtk_propagate_event (grab_widget, event);
914 case GDK_KEY_RELEASE:
917 if (gtk_invoke_key_snoopers (grab_widget, event))
920 /* else fall through */
921 case GDK_MOTION_NOTIFY:
922 case GDK_BUTTON_RELEASE:
923 case GDK_PROXIMITY_IN:
924 case GDK_PROXIMITY_OUT:
925 gtk_propagate_event (grab_widget, event);
928 case GDK_ENTER_NOTIFY:
929 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
931 gtk_widget_event (grab_widget, event);
932 if (event_widget == grab_widget)
933 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
937 case GDK_LEAVE_NOTIFY:
938 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
940 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
941 gtk_widget_event (event_widget, event);
943 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
944 gtk_widget_event (grab_widget, event);
947 case GDK_DRAG_STATUS:
948 case GDK_DROP_FINISHED:
949 _gtk_drag_source_handle_event (event_widget, event);
953 case GDK_DRAG_MOTION:
955 _gtk_drag_dest_handle_event (event_widget, event);
958 g_assert_not_reached ();
962 tmp_list = current_events;
963 current_events = g_list_remove_link (current_events, tmp_list);
964 g_list_free_1 (tmp_list);
979 static GtkWindowGroup *
980 gtk_main_get_window_group (GtkWidget *widget)
982 GtkWidget *toplevel = NULL;
985 toplevel = gtk_widget_get_toplevel (widget);
987 if (toplevel && GTK_IS_WINDOW (toplevel))
988 return _gtk_window_get_group (GTK_WINDOW (toplevel));
990 return _gtk_window_get_group (NULL);
995 gboolean was_grabbed;
996 GtkWidget *grab_widget;
1000 gtk_grab_notify_foreach (GtkWidget *child,
1004 GrabNotifyInfo *info = data;
1006 if (child != info->grab_widget)
1008 g_object_ref (G_OBJECT (child));
1010 gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", info->was_grabbed);
1012 if (GTK_IS_CONTAINER (child))
1013 gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
1015 g_object_unref (G_OBJECT (child));
1020 gtk_grab_notify (GtkWindowGroup *group,
1021 GtkWidget *grab_widget,
1022 gboolean was_grabbed)
1025 GrabNotifyInfo info;
1027 info.grab_widget = grab_widget;
1028 info.was_grabbed = was_grabbed;
1030 g_object_ref (group);
1031 g_object_ref (grab_widget);
1033 toplevels = gtk_window_list_toplevels ();
1034 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1038 GtkWindow *toplevel = toplevels->data;
1039 toplevels = g_list_delete_link (toplevels, toplevels);
1041 if (group == toplevel->group)
1042 gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
1043 g_object_unref (toplevel);
1046 g_object_unref (group);
1047 g_object_unref (grab_widget);
1051 gtk_grab_add (GtkWidget *widget)
1053 GtkWindowGroup *group;
1054 gboolean was_grabbed;
1056 g_return_if_fail (widget != NULL);
1058 if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1060 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1062 group = gtk_main_get_window_group (widget);
1064 was_grabbed = (group->grabs != NULL);
1066 gtk_widget_ref (widget);
1067 group->grabs = g_slist_prepend (group->grabs, widget);
1070 gtk_grab_notify (group, widget, FALSE);
1075 gtk_grab_get_current (void)
1077 GtkWindowGroup *group;
1079 group = gtk_main_get_window_group (NULL);
1082 return GTK_WIDGET (group->grabs->data);
1087 gtk_grab_remove (GtkWidget *widget)
1089 GtkWindowGroup *group;
1091 g_return_if_fail (widget != NULL);
1093 if (GTK_WIDGET_HAS_GRAB (widget))
1095 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1097 group = gtk_main_get_window_group (widget);
1098 group->grabs = g_slist_remove (group->grabs, widget);
1100 gtk_widget_unref (widget);
1103 gtk_grab_notify (group, widget, TRUE);
1108 gtk_init_add (GtkFunction function,
1111 GtkInitFunction *init;
1113 init = g_new (GtkInitFunction, 1);
1114 init->function = function;
1117 init_functions = g_list_prepend (init_functions, init);
1121 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1124 GtkKeySnooperData *data;
1125 static guint snooper_id = 1;
1127 g_return_val_if_fail (snooper != NULL, 0);
1129 data = g_new (GtkKeySnooperData, 1);
1130 data->func = snooper;
1131 data->func_data = func_data;
1132 data->id = snooper_id++;
1133 key_snoopers = g_slist_prepend (key_snoopers, data);
1139 gtk_key_snooper_remove (guint snooper_id)
1141 GtkKeySnooperData *data = NULL;
1144 slist = key_snoopers;
1148 if (data->id == snooper_id)
1151 slist = slist->next;
1155 key_snoopers = g_slist_remove (key_snoopers, data);
1159 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1163 gint return_val = FALSE;
1165 slist = key_snoopers;
1166 while (slist && !return_val)
1168 GtkKeySnooperData *data;
1171 slist = slist->next;
1172 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1179 gtk_quit_add_full (guint main_level,
1180 GtkFunction function,
1181 GtkCallbackMarshal marshal,
1183 GtkDestroyNotify destroy)
1185 static guint quit_id = 1;
1186 GtkQuitFunction *quitf;
1188 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1190 if (!quit_mem_chunk)
1191 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1192 512, G_ALLOC_AND_FREE);
1194 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1196 quitf->id = quit_id++;
1197 quitf->main_level = main_level;
1198 quitf->function = function;
1199 quitf->marshal = marshal;
1201 quitf->destroy = destroy;
1203 quit_functions = g_list_prepend (quit_functions, quitf);
1209 gtk_quit_destroy (GtkQuitFunction *quitf)
1212 quitf->destroy (quitf->data);
1213 g_mem_chunk_free (quit_mem_chunk, quitf);
1217 gtk_quit_destructor (GtkObject **object_p)
1220 gtk_object_destroy (*object_p);
1227 gtk_quit_add_destroy (guint main_level,
1230 GtkObject **object_p;
1232 g_return_if_fail (main_level > 0);
1233 g_return_if_fail (GTK_IS_OBJECT (object));
1235 object_p = g_new (GtkObject*, 1);
1237 gtk_signal_connect (object,
1239 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1241 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1245 gtk_quit_add (guint main_level,
1246 GtkFunction function,
1249 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1253 gtk_quit_remove (guint id)
1255 GtkQuitFunction *quitf;
1258 tmp_list = quit_functions;
1261 quitf = tmp_list->data;
1263 if (quitf->id == id)
1265 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1266 g_list_free (tmp_list);
1267 gtk_quit_destroy (quitf);
1272 tmp_list = tmp_list->next;
1277 gtk_quit_remove_by_data (gpointer data)
1279 GtkQuitFunction *quitf;
1282 tmp_list = quit_functions;
1285 quitf = tmp_list->data;
1287 if (quitf->data == data)
1289 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1290 g_list_free (tmp_list);
1291 gtk_quit_destroy (quitf);
1296 tmp_list = tmp_list->next;
1301 gtk_timeout_add_full (guint32 interval,
1302 GtkFunction function,
1303 GtkCallbackMarshal marshal,
1305 GtkDestroyNotify destroy)
1309 GtkClosure *closure;
1311 closure = g_new (GtkClosure, 1);
1312 closure->marshal = marshal;
1313 closure->data = data;
1314 closure->destroy = destroy;
1316 return g_timeout_add_full (0, interval,
1317 gtk_invoke_idle_timeout,
1319 gtk_destroy_closure);
1322 return g_timeout_add_full (0, interval, function, data, destroy);
1326 gtk_timeout_add (guint32 interval,
1327 GtkFunction function,
1330 return g_timeout_add_full (0, interval, function, data, NULL);
1334 gtk_timeout_remove (guint tag)
1336 g_source_remove (tag);
1340 gtk_idle_add_full (gint priority,
1341 GtkFunction function,
1342 GtkCallbackMarshal marshal,
1344 GtkDestroyNotify destroy)
1348 GtkClosure *closure;
1350 closure = g_new (GtkClosure, 1);
1351 closure->marshal = marshal;
1352 closure->data = data;
1353 closure->destroy = destroy;
1355 return g_idle_add_full (priority,
1356 gtk_invoke_idle_timeout,
1358 gtk_destroy_closure);
1361 return g_idle_add_full (priority, function, data, destroy);
1365 gtk_idle_add (GtkFunction function,
1368 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1372 gtk_idle_add_priority (gint priority,
1373 GtkFunction function,
1376 return g_idle_add_full (priority, function, data, NULL);
1380 gtk_idle_remove (guint tag)
1382 g_source_remove (tag);
1386 gtk_idle_remove_by_data (gpointer data)
1388 if (!g_idle_remove_by_data (data))
1389 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1393 gtk_input_add_full (gint source,
1394 GdkInputCondition condition,
1395 GdkInputFunction function,
1396 GtkCallbackMarshal marshal,
1398 GtkDestroyNotify destroy)
1402 GtkClosure *closure;
1404 closure = g_new (GtkClosure, 1);
1405 closure->marshal = marshal;
1406 closure->data = data;
1407 closure->destroy = destroy;
1409 return gdk_input_add_full (source,
1411 (GdkInputFunction) gtk_invoke_input,
1413 (GdkDestroyNotify) gtk_destroy_closure);
1416 return gdk_input_add_full (source, condition, function, data, destroy);
1420 gtk_input_remove (guint tag)
1422 g_source_remove (tag);
1426 gtk_destroy_closure (gpointer data)
1428 GtkClosure *closure = data;
1430 if (closure->destroy)
1431 (closure->destroy) (closure->data);
1436 gtk_invoke_idle_timeout (gpointer data)
1438 GtkClosure *closure = data;
1441 gint ret_val = FALSE;
1442 args[0].name = NULL;
1443 args[0].type = GTK_TYPE_BOOL;
1444 args[0].d.pointer_data = &ret_val;
1445 closure->marshal (NULL, closure->data, 0, args);
1450 gtk_invoke_input (gpointer data,
1452 GdkInputCondition condition)
1454 GtkClosure *closure = data;
1457 args[0].type = GTK_TYPE_INT;
1458 args[0].name = NULL;
1459 GTK_VALUE_INT (args[0]) = source;
1460 args[1].type = GDK_TYPE_INPUT_CONDITION;
1461 args[1].name = NULL;
1462 GTK_VALUE_FLAGS (args[1]) = condition;
1463 args[2].type = GTK_TYPE_NONE;
1464 args[2].name = NULL;
1466 closure->marshal (NULL, closure->data, 2, args);
1470 * gtk_get_current_event:
1472 * Obtains a copy of the event currently being processed by GTK+. For
1473 * example, if you get a "clicked" signal from #GtkButton, the current
1474 * event will be the #GdkEventButton that triggered the "clicked"
1475 * signal. The returned event must be freed with gdk_event_free().
1476 * If there is no current event, the function returns %NULL.
1478 * Return value: a copy of the current event, or %NULL if no current event.
1481 gtk_get_current_event (void)
1484 return gdk_event_copy (current_events->data);
1490 * gtk_get_current_event_time:
1492 * If there is a current event and it has a timestamp, return that
1493 * timestamp, otherwise return %GDK_CURRENT_TIME.
1495 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1498 gtk_get_current_event_time (void)
1501 return gdk_event_get_time (current_events->data);
1503 return GDK_CURRENT_TIME;
1507 * gtk_get_current_event_state:
1508 * @state: a location to store the state of the current event
1510 * If there is a current event and it has a state field, place
1511 * that state field in @state and return %TRUE, otherwise return
1514 * Return value: %TRUE if there was a current event and it had a state field
1517 gtk_get_current_event_state (GdkModifierType *state)
1519 g_return_val_if_fail (state != NULL, FALSE);
1522 return gdk_event_get_state (current_events->data, state);
1531 * gtk_get_event_widget:
1532 * @event: a #GdkEvent
1534 * If @event is %NULL or the event was not associated with any widget,
1535 * returns %NULL, otherwise returns the widget that received the event
1538 * Return value: the widget that originally received @event, or %NULL
1541 gtk_get_event_widget (GdkEvent *event)
1546 if (event && event->any.window)
1547 gdk_window_get_user_data (event->any.window, (void**) &widget);
1553 gtk_exit_func (void)
1555 if (gtk_initialized)
1557 gtk_initialized = FALSE;
1563 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1565 if (!quitf->marshal)
1566 return quitf->function (quitf->data);
1570 gint ret_val = FALSE;
1572 args[0].name = NULL;
1573 args[0].type = GTK_TYPE_BOOL;
1574 args[0].d.pointer_data = &ret_val;
1575 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1583 * gtk_propagate_event:
1584 * @widget: a #GtkWidget
1587 * Sends an event to a widget, propagating the event to parent widgets
1588 * if the event remains unhandled. Events received by GTK+ from GDK
1589 * normally begin in gtk_main_do_event(). Depending on the type of
1590 * event, existence of modal dialogs, grabs, etc., the event may be
1591 * propagated; if so, this function is used. gtk_propagate_event()
1592 * calls gtk_widget_event() on each widget it decides to send the
1593 * event to. So gtk_widget_event() is the lowest-level function; it
1594 * simply emits the "event" and possibly an event-specific signal on a
1595 * widget. gtk_propagate_event() is a bit higher-level, and
1596 * gtk_main_do_event() is the highest level.
1598 * All that said, you most likely don't want to use any of these
1599 * functions; synthesizing events is rarely needed. Consider asking on
1600 * the mailing list for better ways to achieve your goals. For
1601 * example, use gdk_window_invalidate_rect() or
1602 * gtk_widget_queue_draw() instead of making up expose events.
1606 gtk_propagate_event (GtkWidget *widget,
1611 g_return_if_fail (GTK_IS_WIDGET (widget));
1612 g_return_if_fail (event != NULL);
1614 handled_event = FALSE;
1616 gtk_widget_ref (widget);
1618 if ((event->type == GDK_KEY_PRESS) ||
1619 (event->type == GDK_KEY_RELEASE))
1621 /* Only send key events within Window widgets to the Window
1622 * The Window widget will in turn pass the
1623 * key event on to the currently focused widget
1628 window = gtk_widget_get_toplevel (widget);
1629 if (window && GTK_IS_WINDOW (window))
1631 /* If there is a grab within the window, give the grab widget
1632 * a first crack at the key event
1634 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1635 handled_event = gtk_widget_event (widget, event);
1639 window = gtk_widget_get_toplevel (widget);
1640 if (window && GTK_IS_WINDOW (window))
1642 if (GTK_WIDGET_IS_SENSITIVE (window))
1643 gtk_widget_event (window, event);
1647 handled_event = TRUE; /* don't send to widget */
1651 /* Other events get propagated up the widget tree
1652 * so that parents can see the button and motion
1653 * events of the children.
1661 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1662 tmp = widget->parent;
1663 gtk_widget_unref (widget);
1667 if (!handled_event && widget)
1668 gtk_widget_ref (widget);
1674 gtk_widget_unref (widget);
1679 gtk_error (gchar *str)
1685 gtk_warning (gchar *str)
1691 gtk_message (gchar *str)
1697 gtk_print (gchar *str)
1699 static GtkWidget *window = NULL;
1700 static GtkWidget *text;
1701 static int level = 0;
1705 GtkWidget *hscrollbar;
1706 GtkWidget *vscrollbar;
1707 GtkWidget *separator;
1712 fputs (str, stdout);
1719 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1721 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1722 (GtkSignalFunc) gtk_widget_destroyed,
1725 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1727 box1 = gtk_vbox_new (FALSE, 0);
1728 gtk_container_add (GTK_CONTAINER (window), box1);
1729 gtk_widget_show (box1);
1732 box2 = gtk_vbox_new (FALSE, 10);
1733 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1734 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1735 gtk_widget_show (box2);
1738 table = gtk_table_new (2, 2, FALSE);
1739 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1740 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1741 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1742 gtk_widget_show (table);
1744 text = gtk_text_new (NULL, NULL);
1745 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1746 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1747 gtk_widget_show (text);
1748 gtk_widget_realize (text);
1750 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1751 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1752 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1753 gtk_widget_show (hscrollbar);
1755 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1756 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1757 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1758 gtk_widget_show (vscrollbar);
1760 separator = gtk_hseparator_new ();
1761 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1762 gtk_widget_show (separator);
1765 box2 = gtk_vbox_new (FALSE, 10);
1766 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1767 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1768 gtk_widget_show (box2);
1771 button = gtk_button_new_with_label ("close");
1772 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1773 (GtkSignalFunc) gtk_widget_hide,
1774 GTK_OBJECT (window));
1775 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1776 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1777 gtk_widget_grab_default (button);
1778 gtk_widget_show (button);
1782 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1785 if (!GTK_WIDGET_VISIBLE (window))
1786 gtk_widget_show (window);
1791 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1792 GValue *return_accu,
1793 const GValue *handler_return,
1796 gboolean continue_emission;
1797 gboolean signal_handled;
1799 signal_handled = g_value_get_boolean (handler_return);
1800 g_value_set_boolean (return_accu, signal_handled);
1801 continue_emission = !signal_handled;
1803 return continue_emission;