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"
29 #ifdef GDK_WINDOWING_X11
30 #include <X11/Xlocale.h> /* so we get the right setlocale */
35 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
44 #include "gtkcompat.h"
47 #include "gtkselection.h"
48 #include "gtksettings.h"
49 #include "gtksignal.h"
50 #include "gtkwidget.h"
51 #include "gtkwindow.h"
52 #include "gtkprivate.h"
53 #include "gdk/gdki18n.h"
58 /* Private type definitions
60 typedef struct _GtkInitFunction GtkInitFunction;
61 typedef struct _GtkQuitFunction GtkQuitFunction;
62 typedef struct _GtkClosure GtkClosure;
63 typedef struct _GtkKeySnooperData GtkKeySnooperData;
65 struct _GtkInitFunction
71 struct _GtkQuitFunction
75 GtkCallbackMarshal marshal;
78 GtkDestroyNotify destroy;
83 GtkCallbackMarshal marshal;
85 GtkDestroyNotify destroy;
88 struct _GtkKeySnooperData
95 static void gtk_exit_func (void);
96 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
97 static void gtk_quit_destroy (GtkQuitFunction *quitf);
98 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
101 static void gtk_destroy_closure (gpointer data);
102 static gboolean gtk_invoke_idle_timeout (gpointer data);
103 static void gtk_invoke_input (gpointer data,
105 GdkInputCondition condition);
108 static void gtk_error (gchar *str);
109 static void gtk_warning (gchar *str);
110 static void gtk_message (gchar *str);
111 static void gtk_print (gchar *str);
114 const guint gtk_major_version = GTK_MAJOR_VERSION;
115 const guint gtk_minor_version = GTK_MINOR_VERSION;
116 const guint gtk_micro_version = GTK_MICRO_VERSION;
117 const guint gtk_binary_age = GTK_BINARY_AGE;
118 const guint gtk_interface_age = GTK_INTERFACE_AGE;
120 static guint gtk_main_loop_level = 0;
121 static gint gtk_initialized = FALSE;
122 static GList *current_events = NULL;
124 static GSList *main_loops = NULL; /* stack of currently executing main loops */
126 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
127 * widget is the first one on the list.
129 static GList *init_functions = NULL; /* A list of init functions.
131 static GList *quit_functions = NULL; /* A list of quit functions.
133 static GMemChunk *quit_mem_chunk = NULL;
135 static GSList *key_snoopers = NULL;
137 static GdkVisual *gtk_visual; /* The visual to be used in creating new
140 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
144 guint gtk_debug_flags = 0; /* Global GTK debug flag */
146 #ifdef G_ENABLE_DEBUG
147 static const GDebugKey gtk_debug_keys[] = {
148 {"misc", GTK_DEBUG_MISC},
149 {"dnd", GTK_DEBUG_DND},
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)";
181 static gchar *add_dll_suffix(gchar *module_name)
183 gchar *suffix = strrchr(module_name, '.');
185 if (!suffix || stricmp(suffix, ".dll"))
187 gchar *old = module_name;
189 module_name = g_strconcat (module_name, ".dll", NULL);
192 return (module_name);
196 #undef gtk_init_check
198 /* This checks to see if the process is running suid or sgid
199 * at the current time. If so, we don't allow GTK+ to be initialized.
200 * This is meant to be a mild check - we only error out if we
201 * can prove the programmer is doing something wrong, not if
202 * they could be doing something wrong. For this reason, we
203 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
208 /* this isn't at all relevant on Windoze and doesn't compile ... --hb */
210 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
211 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
213 #ifdef HAVE_GETRESUID
214 /* These aren't in the header files, so we prototype them here.
216 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
217 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
219 if (getresuid (&ruid, &euid, &suid) != 0 ||
220 getresgid (&rgid, &egid, &sgid) != 0)
221 #endif /* HAVE_GETRESUID */
223 suid = ruid = getuid ();
224 sgid = rgid = getgid ();
229 if (ruid != euid || ruid != suid ||
230 rgid != egid || rgid != sgid)
232 g_warning ("This process is currently running setuid or setgid.\n"
233 "This is not a supported use of GTK+. You must create a helper\n"
234 "program instead. For further details, see:\n\n"
235 " http://www.gtk.org/setuid.html\n\n"
236 "Refusing to initialize GTK+.");
244 gtk_init_check (int *argc,
247 GSList *gtk_modules = NULL;
249 gchar *env_string = NULL;
254 if (!check_setugid ())
258 g_set_error_handler (gtk_error);
259 g_set_warning_handler (gtk_warning);
260 g_set_message_handler (gtk_message);
261 g_set_print_handler (gtk_print);
264 /* Initialize "gdk". We pass along the 'argc' and 'argv'
265 * parameters as they contain information that GDK uses
267 if (!gdk_init_check (argc, argv))
270 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
272 #ifdef G_ENABLE_DEBUG
273 env_string = getenv ("GTK_DEBUG");
274 if (env_string != NULL)
276 gtk_debug_flags = g_parse_debug_string (env_string,
281 #endif /* G_ENABLE_DEBUG */
283 env_string = getenv ("GTK_MODULES");
286 gchar **modules, **as;
289 modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
291 modules = g_strsplit (env_string, ";", -1);
293 for (as = modules; *as; as++)
296 gtk_modules = g_slist_prepend (gtk_modules, *as);
308 for (i = 1; i < *argc;)
310 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
311 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
313 gchar *module_name = (*argv)[i] + 12;
315 if (*module_name == '=')
317 else if (i + 1 < *argc)
321 module_name = (*argv)[i];
325 if (module_name && *module_name)
326 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
328 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
330 GLogLevelFlags fatal_mask;
332 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
333 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
334 g_log_set_always_fatal (fatal_mask);
337 #ifdef G_ENABLE_DEBUG
338 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
339 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
341 gchar *equal_pos = strchr ((*argv)[i], '=');
343 if (equal_pos != NULL)
345 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
349 else if ((i + 1) < *argc && (*argv)[i + 1])
351 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
359 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
360 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
362 gchar *equal_pos = strchr ((*argv)[i], '=');
364 if (equal_pos != NULL)
366 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
370 else if ((i + 1) < *argc && (*argv)[i + 1])
372 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
380 #endif /* G_ENABLE_DEBUG */
384 for (i = 1; i < *argc; i++)
386 for (k = i; k < *argc; k++)
387 if ((*argv)[k] != NULL)
393 for (j = i + k; j < *argc; j++)
394 (*argv)[j-k] = (*argv)[j];
400 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
401 gdk_window_set_debug_updates (TRUE);
403 /* load gtk modules */
404 gtk_modules = g_slist_reverse (gtk_modules);
405 for (slist = gtk_modules; slist; slist = slist->next)
408 GModule *module = NULL;
409 GtkModuleInitFunc modinit_func = NULL;
411 module_name = slist->data;
414 if (!g_path_is_absolute (module_name))
416 gchar *old = module_name;
418 module_name = g_module_build_path (NULL, module_name);
422 module_name = add_dll_suffix(module_name);
424 if (g_module_supported ())
426 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
428 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
431 if (!g_slist_find (gtk_modules, modinit_func))
433 g_module_make_resident (module);
434 slist->data = modinit_func;
438 g_module_close (module);
445 g_message ("Failed to load module \"%s\": %s",
446 module ? g_module_name (module) : module_name,
449 g_module_close (module);
451 g_free (module_name);
456 bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
457 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
458 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
460 # else /* !G_OS_WIN32 */
462 bindtextdomain (GETTEXT_PACKAGE,
463 g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
464 g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
471 /* Translate to default:RTL if you want your widgets
472 * to be RTL, otherwise translate to default:LTR.
473 * Do *not* translate it to "predefinito:LTR", if it
474 * it isn't default:LTR or default:RTL it will not work
476 char *e = _("default:LTR");
477 if (strcmp (e, "default:RTL")==0) {
478 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
479 } else if (strcmp (e, "default:LTR")) {
480 g_warning ("Whoever translated default:LTR did so wrongly.\n");
484 /* Initialize the default visual and colormap to be
485 * used in creating widgets. (We want to use the system
486 * defaults so as to be nice to the colormap).
488 gtk_visual = gdk_visual_get_system ();
489 gtk_colormap = gdk_colormap_get_system ();
495 /* Register an exit function to make sure we are able to cleanup.
497 g_atexit (gtk_exit_func);
499 /* Set the 'initialized' flag.
501 gtk_initialized = TRUE;
503 /* initialize gtk modules
505 for (slist = gtk_modules; slist; slist = slist->next)
509 GtkModuleInitFunc modinit;
511 modinit = slist->data;
512 modinit (argc, argv);
515 g_slist_free (gtk_modules);
518 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
519 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
520 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
521 " if you don't know why you are getting this, you probably want to\n"
522 " use the stable branch which can be retrived from\n"
523 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
524 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
533 gtk_init (int *argc, char ***argv)
535 if (!gtk_init_check (argc, argv))
537 g_warning ("cannot open display: %s", gdk_get_display ());
545 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
547 if (sizeof_GtkWindow != sizeof (GtkWindow))
548 g_error ("Incompatible build!\n"
549 "The code using GTK+ thinks GtkWindow is of different\n"
550 "size than it actually is in this build of GTK+.\n"
551 "On Windows, this probably means that you have compiled\n"
552 "your code with gcc without the -fnative-struct switch.");
555 /* These two functions might get more checks added later, thus pass
556 * in the number of extra args.
559 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
561 check_sizeof_GtkWindow (sizeof_GtkWindow);
562 gtk_init (argc, argv);
566 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
568 check_sizeof_GtkWindow (sizeof_GtkWindow);
569 return gtk_init_check (argc, argv);
575 gtk_exit (gint errorcode)
577 /* Only if "gtk" has been initialized should we de-initialize.
579 /* de-initialisation is done by the gtk_exit_funct(),
580 * no need to do this here (Alex J.)
586 gtk_set_locale (void)
588 return gdk_set_locale ();
592 * gtk_get_default_language:
594 * Returns the ISO language code for the default language currently in
595 * effect. (Note that this can change over the life of an
596 * application.) The default language is derived from the current
597 * locale. It determines, for example, whether GTK+ uses the
598 * right-to-left or left-to-right text direction.
600 * Return value: the default language as an allocated string, must be freed
603 gtk_get_default_language (void)
608 lang = g_strdup (setlocale (LC_CTYPE, NULL));
609 p = strchr (lang, '.');
612 p = strchr (lang, '@');
624 GtkInitFunction *init;
627 gtk_main_loop_level++;
629 loop = g_main_new (TRUE);
630 main_loops = g_slist_prepend (main_loops, loop);
632 tmp_list = functions = init_functions;
633 init_functions = NULL;
637 init = tmp_list->data;
638 tmp_list = tmp_list->next;
640 (* init->function) (init->data);
643 g_list_free (functions);
645 if (g_main_is_running (main_loops->data))
647 GDK_THREADS_LEAVE ();
649 GDK_THREADS_ENTER ();
655 GList *reinvoke_list = NULL;
656 GtkQuitFunction *quitf;
658 while (quit_functions)
660 quitf = quit_functions->data;
662 tmp_list = quit_functions;
663 quit_functions = g_list_remove_link (quit_functions, quit_functions);
664 g_list_free_1 (tmp_list);
666 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
667 gtk_quit_invoke_function (quitf))
669 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
673 gtk_quit_destroy (quitf);
680 work = g_list_last (reinvoke_list);
682 quit_functions->prev = work;
683 work->next = quit_functions;
684 quit_functions = work;
690 main_loops = g_slist_remove (main_loops, loop);
692 g_main_destroy (loop);
694 gtk_main_loop_level--;
698 gtk_main_level (void)
700 return gtk_main_loop_level;
706 g_return_if_fail (main_loops != NULL);
708 g_main_quit (main_loops->data);
712 gtk_events_pending (void)
716 GDK_THREADS_LEAVE ();
717 result = g_main_pending();
718 GDK_THREADS_ENTER ();
724 gtk_main_iteration (void)
726 GDK_THREADS_LEAVE ();
727 g_main_iteration (TRUE);
728 GDK_THREADS_ENTER ();
731 return !g_main_is_running (main_loops->data);
737 gtk_main_iteration_do (gboolean blocking)
739 GDK_THREADS_LEAVE ();
740 g_main_iteration (blocking);
741 GDK_THREADS_ENTER ();
744 return !g_main_is_running (main_loops->data);
750 gtk_main_do_event (GdkEvent *event)
752 GtkWidget *event_widget;
753 GtkWidget *grab_widget;
754 GdkEvent *next_event;
757 /* If there are any events pending then get the next one.
759 next_event = gdk_event_peek ();
761 /* Try to compress enter/leave notify events. These event
762 * pairs occur when the mouse is dragged quickly across
763 * a window with many buttons (or through a menu). Instead
764 * of highlighting and de-highlighting each widget that
765 * is crossed it is better to simply de-highlight the widget
766 * which contained the mouse initially and highlight the
767 * widget which ends up containing the mouse.
770 if (((event->type == GDK_ENTER_NOTIFY) ||
771 (event->type == GDK_LEAVE_NOTIFY)) &&
772 ((next_event->type == GDK_ENTER_NOTIFY) ||
773 (next_event->type == GDK_LEAVE_NOTIFY)) &&
774 (next_event->type != event->type) &&
775 (next_event->any.window == event->any.window))
777 /* Throw both the peeked copy and the queued copy away
779 gdk_event_free (next_event);
780 next_event = gdk_event_get ();
781 gdk_event_free (next_event);
787 gdk_event_free (next_event);
789 /* Find the widget which got the event. We store the widget
790 * in the user_data field of GdkWindow's.
791 * Ignore the event if we don't have a widget for it, except
792 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
793 * Though this happens rarely, bogus events can occour
794 * for e.g. destroyed GdkWindows.
796 event_widget = gtk_get_event_widget (event);
799 /* To handle selection INCR transactions, we select
800 * PropertyNotify events on the requestor window and create
801 * a corresponding (fake) GdkWindow so that events get
802 * here. There won't be a widget though, so we have to handle
805 if (event->type == GDK_PROPERTY_NOTIFY)
806 gtk_selection_incr_event (event->any.window,
808 else if (event->type == GDK_SETTING)
809 _gtk_settings_handle_event (&event->setting);
814 /* Push the event onto a stack of current events for
815 * gtk_current_event_get().
817 current_events = g_list_prepend (current_events, event);
819 /* If there is a grab in effect...
823 grab_widget = grabs->data;
825 /* If the grab widget is an ancestor of the event widget
826 * then we send the event to the original event widget.
827 * This is the key to implementing modality.
829 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
830 gtk_widget_is_ancestor (event_widget, grab_widget))
831 grab_widget = event_widget;
835 grab_widget = event_widget;
838 /* Not all events get sent to the grabbing widget.
839 * The delete, destroy, expose, focus change and resize
840 * events still get sent to the event widget because
841 * 1) these events have no meaning for the grabbing widget
842 * and 2) redirecting these events to the grabbing widget
843 * could cause the display to be messed up.
845 * Drag events are also not redirected, since it isn't
846 * clear what the semantics of that would be.
854 gtk_widget_ref (event_widget);
855 if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
856 !gtk_widget_event (event_widget, event))
857 gtk_widget_destroy (event_widget);
858 gtk_widget_unref (event_widget);
862 /* Unexpected GDK_DESTROY from the outside, ignore for
863 * child windows, handle like a GDK_DELETE for toplevels
865 if (!event_widget->parent)
867 gtk_widget_ref (event_widget);
868 if (!gtk_widget_event (event_widget, event) &&
869 !GTK_OBJECT_DESTROYED (event_widget))
870 gtk_widget_destroy (event_widget);
871 gtk_widget_unref (event_widget);
876 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
877 gdk_window_begin_paint_region (event->any.window, event->expose.region);
879 gtk_widget_send_expose (event_widget, event);
881 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
882 gdk_window_end_paint (event->any.window);
885 case GDK_PROPERTY_NOTIFY:
887 case GDK_FOCUS_CHANGE:
891 case GDK_SELECTION_CLEAR:
892 case GDK_SELECTION_REQUEST:
893 case GDK_SELECTION_NOTIFY:
894 case GDK_CLIENT_EVENT:
895 case GDK_VISIBILITY_NOTIFY:
896 case GDK_WINDOW_STATE:
897 gtk_widget_event (event_widget, event);
901 case GDK_BUTTON_PRESS:
902 case GDK_2BUTTON_PRESS:
903 case GDK_3BUTTON_PRESS:
904 gtk_propagate_event (grab_widget, event);
908 case GDK_KEY_RELEASE:
911 if (gtk_invoke_key_snoopers (grab_widget, event))
914 /* else fall through */
915 case GDK_MOTION_NOTIFY:
916 case GDK_BUTTON_RELEASE:
917 case GDK_PROXIMITY_IN:
918 case GDK_PROXIMITY_OUT:
919 gtk_propagate_event (grab_widget, event);
922 case GDK_ENTER_NOTIFY:
923 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
925 gtk_widget_event (grab_widget, event);
926 if (event_widget == grab_widget)
927 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
931 case GDK_LEAVE_NOTIFY:
932 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
934 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
935 gtk_widget_event (event_widget, event);
937 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
938 gtk_widget_event (grab_widget, event);
941 case GDK_DRAG_STATUS:
942 case GDK_DROP_FINISHED:
943 gtk_drag_source_handle_event (event_widget, event);
947 case GDK_DRAG_MOTION:
949 gtk_drag_dest_handle_event (event_widget, event);
953 tmp_list = current_events;
954 current_events = g_list_remove_link (current_events, tmp_list);
955 g_list_free_1 (tmp_list);
971 gtk_grab_add (GtkWidget *widget)
973 g_return_if_fail (widget != NULL);
975 if (!GTK_WIDGET_HAS_GRAB (widget))
977 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
979 grabs = g_slist_prepend (grabs, widget);
980 gtk_widget_ref (widget);
985 gtk_grab_get_current (void)
988 return GTK_WIDGET (grabs->data);
993 gtk_grab_remove (GtkWidget *widget)
995 g_return_if_fail (widget != NULL);
997 if (GTK_WIDGET_HAS_GRAB (widget))
999 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1001 grabs = g_slist_remove (grabs, widget);
1002 gtk_widget_unref (widget);
1007 gtk_init_add (GtkFunction function,
1010 GtkInitFunction *init;
1012 init = g_new (GtkInitFunction, 1);
1013 init->function = function;
1016 init_functions = g_list_prepend (init_functions, init);
1020 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1023 GtkKeySnooperData *data;
1024 static guint snooper_id = 1;
1026 g_return_val_if_fail (snooper != NULL, 0);
1028 data = g_new (GtkKeySnooperData, 1);
1029 data->func = snooper;
1030 data->func_data = func_data;
1031 data->id = snooper_id++;
1032 key_snoopers = g_slist_prepend (key_snoopers, data);
1038 gtk_key_snooper_remove (guint snooper_id)
1040 GtkKeySnooperData *data = NULL;
1043 slist = key_snoopers;
1047 if (data->id == snooper_id)
1050 slist = slist->next;
1054 key_snoopers = g_slist_remove (key_snoopers, data);
1058 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1062 gint return_val = FALSE;
1064 slist = key_snoopers;
1065 while (slist && !return_val)
1067 GtkKeySnooperData *data;
1070 slist = slist->next;
1071 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1078 gtk_quit_add_full (guint main_level,
1079 GtkFunction function,
1080 GtkCallbackMarshal marshal,
1082 GtkDestroyNotify destroy)
1084 static guint quit_id = 1;
1085 GtkQuitFunction *quitf;
1087 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1089 if (!quit_mem_chunk)
1090 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1091 512, G_ALLOC_AND_FREE);
1093 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1095 quitf->id = quit_id++;
1096 quitf->main_level = main_level;
1097 quitf->function = function;
1098 quitf->marshal = marshal;
1100 quitf->destroy = destroy;
1102 quit_functions = g_list_prepend (quit_functions, quitf);
1108 gtk_quit_destroy (GtkQuitFunction *quitf)
1111 quitf->destroy (quitf->data);
1112 g_mem_chunk_free (quit_mem_chunk, quitf);
1116 gtk_quit_destructor (GtkObject **object_p)
1119 gtk_object_destroy (*object_p);
1126 gtk_quit_add_destroy (guint main_level,
1129 GtkObject **object_p;
1131 g_return_if_fail (main_level > 0);
1132 g_return_if_fail (object != NULL);
1133 g_return_if_fail (GTK_IS_OBJECT (object));
1135 object_p = g_new (GtkObject*, 1);
1137 gtk_signal_connect (object,
1139 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1141 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1145 gtk_quit_add (guint main_level,
1146 GtkFunction function,
1149 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1153 gtk_quit_remove (guint id)
1155 GtkQuitFunction *quitf;
1158 tmp_list = quit_functions;
1161 quitf = tmp_list->data;
1163 if (quitf->id == id)
1165 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1166 g_list_free (tmp_list);
1167 gtk_quit_destroy (quitf);
1172 tmp_list = tmp_list->next;
1177 gtk_quit_remove_by_data (gpointer data)
1179 GtkQuitFunction *quitf;
1182 tmp_list = quit_functions;
1185 quitf = tmp_list->data;
1187 if (quitf->data == data)
1189 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1190 g_list_free (tmp_list);
1191 gtk_quit_destroy (quitf);
1196 tmp_list = tmp_list->next;
1201 gtk_timeout_add_full (guint32 interval,
1202 GtkFunction function,
1203 GtkCallbackMarshal marshal,
1205 GtkDestroyNotify destroy)
1209 GtkClosure *closure;
1211 closure = g_new (GtkClosure, 1);
1212 closure->marshal = marshal;
1213 closure->data = data;
1214 closure->destroy = destroy;
1216 return g_timeout_add_full (0, interval,
1217 gtk_invoke_idle_timeout,
1219 gtk_destroy_closure);
1222 return g_timeout_add_full (0, interval, function, data, destroy);
1226 gtk_timeout_add (guint32 interval,
1227 GtkFunction function,
1230 return g_timeout_add_full (0, interval, function, data, NULL);
1234 gtk_timeout_remove (guint tag)
1236 g_source_remove (tag);
1240 gtk_idle_add_full (gint priority,
1241 GtkFunction function,
1242 GtkCallbackMarshal marshal,
1244 GtkDestroyNotify destroy)
1248 GtkClosure *closure;
1250 closure = g_new (GtkClosure, 1);
1251 closure->marshal = marshal;
1252 closure->data = data;
1253 closure->destroy = destroy;
1255 return g_idle_add_full (priority,
1256 gtk_invoke_idle_timeout,
1258 gtk_destroy_closure);
1261 return g_idle_add_full (priority, function, data, destroy);
1265 gtk_idle_add (GtkFunction function,
1268 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1272 gtk_idle_add_priority (gint priority,
1273 GtkFunction function,
1276 return g_idle_add_full (priority, function, data, NULL);
1280 gtk_idle_remove (guint tag)
1282 g_source_remove (tag);
1286 gtk_idle_remove_by_data (gpointer data)
1288 if (!g_idle_remove_by_data (data))
1289 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1293 gtk_input_add_full (gint source,
1294 GdkInputCondition condition,
1295 GdkInputFunction function,
1296 GtkCallbackMarshal marshal,
1298 GtkDestroyNotify destroy)
1302 GtkClosure *closure;
1304 closure = g_new (GtkClosure, 1);
1305 closure->marshal = marshal;
1306 closure->data = data;
1307 closure->destroy = destroy;
1309 return gdk_input_add_full (source,
1311 (GdkInputFunction) gtk_invoke_input,
1313 (GdkDestroyNotify) gtk_destroy_closure);
1316 return gdk_input_add_full (source, condition, function, data, destroy);
1320 gtk_input_remove (guint tag)
1322 g_source_remove (tag);
1326 gtk_destroy_closure (gpointer data)
1328 GtkClosure *closure = data;
1330 if (closure->destroy)
1331 (closure->destroy) (closure->data);
1336 gtk_invoke_idle_timeout (gpointer data)
1338 GtkClosure *closure = data;
1341 gint ret_val = FALSE;
1342 args[0].name = NULL;
1343 args[0].type = GTK_TYPE_BOOL;
1344 args[0].d.pointer_data = &ret_val;
1345 closure->marshal (NULL, closure->data, 0, args);
1350 gtk_invoke_input (gpointer data,
1352 GdkInputCondition condition)
1354 GtkClosure *closure = data;
1357 args[0].type = GTK_TYPE_INT;
1358 args[0].name = NULL;
1359 GTK_VALUE_INT(args[0]) = source;
1360 args[1].type = GDK_TYPE_INPUT_CONDITION;
1361 args[1].name = NULL;
1362 GTK_VALUE_FLAGS(args[1]) = condition;
1363 args[2].type = GTK_TYPE_NONE;
1364 args[2].name = NULL;
1366 closure->marshal (NULL, closure->data, 2, args);
1370 * gtk_get_current_event:
1372 * Obtains a copy of the event currently being processed by GTK+. For
1373 * example, if you get a "clicked" signal from #GtkButton, the current
1374 * event will be the #GdkEventButton that triggered the "clicked"
1375 * signal. The returned event must be freed with gdk_event_free().
1376 * If there is no current event, the function returns %NULL.
1378 * Return value: a copy of the current event, or %NULL if no current event.
1381 gtk_get_current_event (void)
1384 return gdk_event_copy (current_events->data);
1390 * gtk_get_current_event_time:
1392 * If there is a current event and it has a timestamp, return that
1393 * timestamp, otherwise return %GDK_CURRENT_TIME.
1395 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1398 gtk_get_current_event_time (void)
1401 return gdk_event_get_time (current_events->data);
1403 return GDK_CURRENT_TIME;
1407 * gtk_get_current_event_state:
1408 * @state: a location to store the state of the current event
1410 * If there is a current event and it has a state field, place
1411 * that state field in @state and return %TRUE, otherwise return
1414 * Return value: %TRUE if there was a current event and it had a state field
1417 gtk_get_current_event_state (GdkModifierType *state)
1419 g_return_val_if_fail (state != NULL, FALSE);
1422 return gdk_event_get_state (current_events->data, state);
1431 * gtk_get_event_widget:
1432 * @event: a #GdkEvent
1434 * If @event is %NULL or the event was not associated with any widget,
1435 * returns %NULL, otherwise returns the widget that received the event
1438 * Return value: the widget that originally received @event, or %NULL
1441 gtk_get_event_widget (GdkEvent *event)
1446 if (event && event->any.window)
1447 gdk_window_get_user_data (event->any.window, (void**) &widget);
1453 gtk_exit_func (void)
1455 if (gtk_initialized)
1457 gtk_initialized = FALSE;
1463 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1465 if (!quitf->marshal)
1466 return quitf->function (quitf->data);
1470 gint ret_val = FALSE;
1472 args[0].name = NULL;
1473 args[0].type = GTK_TYPE_BOOL;
1474 args[0].d.pointer_data = &ret_val;
1475 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1483 * gtk_propagate_event:
1484 * @widget: a #GtkWidget
1487 * Sends an event to a widget, propagating the event to parent widgets
1488 * if the event remains unhandled. Events received by GTK+ from GDK
1489 * normally begin in gtk_main_do_event(). Depending on the type of
1490 * event, existence of modal dialogs, grabs, etc., the event may be
1491 * propagated; if so, this function is used. gtk_propagate_event()
1492 * calls gtk_widget_event() on each widget it decides to send the
1493 * event to. So gtk_widget_event() is the lowest-level function; it
1494 * simply emits the "event" and possibly an event-specific signal on a
1495 * widget. gtk_propagate_event() is a bit higher-level, and
1496 * gtk_main_do_event() is the highest level.
1498 * All that said, you most likely don't want to use any of these
1499 * functions; synthesizing events is rarely needed. Consider asking on
1500 * the mailing list for better ways to achieve your goals. For
1501 * example, use gdk_window_invalidate_rect() or
1502 * gtk_widget_queue_draw() instead of making up expose events.
1506 gtk_propagate_event (GtkWidget *widget,
1511 g_return_if_fail (widget != NULL);
1512 g_return_if_fail (GTK_IS_WIDGET (widget));
1513 g_return_if_fail (event != NULL);
1515 handled_event = FALSE;
1517 gtk_widget_ref (widget);
1519 if ((event->type == GDK_KEY_PRESS) ||
1520 (event->type == GDK_KEY_RELEASE))
1522 /* Only send key events within Window widgets to the Window
1523 * The Window widget will in turn pass the
1524 * key event on to the currently focused widget
1529 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1533 /* If there is a grab within the window, give the grab widget
1534 * a first crack at the key event
1536 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1537 handled_event = gtk_widget_event (widget, event);
1541 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1544 if (GTK_WIDGET_IS_SENSITIVE (window))
1545 gtk_widget_event (window, event);
1549 handled_event = TRUE; /* don't send to widget */
1553 /* Other events get propagated up the widget tree
1554 * so that parents can see the button and motion
1555 * events of the children.
1563 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1564 tmp = widget->parent;
1565 gtk_widget_unref (widget);
1569 if (!handled_event && widget)
1570 gtk_widget_ref (widget);
1576 gtk_widget_unref (widget);
1581 gtk_error (gchar *str)
1587 gtk_warning (gchar *str)
1593 gtk_message (gchar *str)
1599 gtk_print (gchar *str)
1601 static GtkWidget *window = NULL;
1602 static GtkWidget *text;
1603 static int level = 0;
1607 GtkWidget *hscrollbar;
1608 GtkWidget *vscrollbar;
1609 GtkWidget *separator;
1614 fputs (str, stdout);
1621 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1623 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1624 (GtkSignalFunc) gtk_widget_destroyed,
1627 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1629 box1 = gtk_vbox_new (FALSE, 0);
1630 gtk_container_add (GTK_CONTAINER (window), box1);
1631 gtk_widget_show (box1);
1634 box2 = gtk_vbox_new (FALSE, 10);
1635 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1636 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1637 gtk_widget_show (box2);
1640 table = gtk_table_new (2, 2, FALSE);
1641 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1642 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1643 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1644 gtk_widget_show (table);
1646 text = gtk_text_new (NULL, NULL);
1647 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1648 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1649 gtk_widget_show (text);
1650 gtk_widget_realize (text);
1652 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1653 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1654 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1655 gtk_widget_show (hscrollbar);
1657 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1658 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1659 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1660 gtk_widget_show (vscrollbar);
1662 separator = gtk_hseparator_new ();
1663 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1664 gtk_widget_show (separator);
1667 box2 = gtk_vbox_new (FALSE, 10);
1668 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1669 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1670 gtk_widget_show (box2);
1673 button = gtk_button_new_with_label ("close");
1674 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1675 (GtkSignalFunc) gtk_widget_hide,
1676 GTK_OBJECT (window));
1677 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1678 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1679 gtk_widget_grab_default (button);
1680 gtk_widget_show (button);
1684 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1687 if (!GTK_WIDGET_VISIBLE (window))
1688 gtk_widget_show (window);
1693 _gtk_boolean_handled_accumulator (GSignalInvocationHint *ihint,
1694 GValue *return_accu,
1695 const GValue *handler_return,
1698 gboolean continue_emission;
1699 gboolean signal_handled;
1701 signal_handled = g_value_get_boolean (handler_return);
1702 g_value_set_boolean (return_accu, signal_handled);
1703 continue_emission = !signal_handled;
1705 return continue_emission;