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 #ifdef GDK_WINDOWING_X11
28 #include <X11/Xlocale.h> /* so we get the right setlocale */
33 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
42 #include "gtkcompat.h"
45 #include "gtkselection.h"
46 #include "gtksignal.h"
47 #include "gtkwidget.h"
48 #include "gtkwindow.h"
49 #include "gtkprivate.h"
50 #include "gdk/gdki18n.h"
55 /* Private type definitions
57 typedef struct _GtkInitFunction GtkInitFunction;
58 typedef struct _GtkQuitFunction GtkQuitFunction;
59 typedef struct _GtkClosure GtkClosure;
60 typedef struct _GtkKeySnooperData GtkKeySnooperData;
62 struct _GtkInitFunction
68 struct _GtkQuitFunction
72 GtkCallbackMarshal marshal;
75 GtkDestroyNotify destroy;
80 GtkCallbackMarshal marshal;
82 GtkDestroyNotify destroy;
85 struct _GtkKeySnooperData
92 static void gtk_exit_func (void);
93 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
94 static void gtk_quit_destroy (GtkQuitFunction *quitf);
95 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
98 static void gtk_destroy_closure (gpointer data);
99 static gboolean gtk_invoke_idle_timeout (gpointer data);
100 static void gtk_invoke_input (gpointer data,
102 GdkInputCondition condition);
105 static void gtk_error (gchar *str);
106 static void gtk_warning (gchar *str);
107 static void gtk_message (gchar *str);
108 static void gtk_print (gchar *str);
111 const guint gtk_major_version = GTK_MAJOR_VERSION;
112 const guint gtk_minor_version = GTK_MINOR_VERSION;
113 const guint gtk_micro_version = GTK_MICRO_VERSION;
114 const guint gtk_binary_age = GTK_BINARY_AGE;
115 const guint gtk_interface_age = GTK_INTERFACE_AGE;
117 static guint gtk_main_loop_level = 0;
118 static gint gtk_initialized = FALSE;
119 static GList *current_events = NULL;
121 static GSList *main_loops = NULL; /* stack of currently executing main loops */
123 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
124 * widget is the first one on the list.
126 static GList *init_functions = NULL; /* A list of init functions.
128 static GList *quit_functions = NULL; /* A list of quit functions.
130 static GMemChunk *quit_mem_chunk = NULL;
132 static GSList *key_snoopers = NULL;
134 static GdkVisual *gtk_visual; /* The visual to be used in creating new
137 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
141 guint gtk_debug_flags = 0; /* Global GTK debug flag */
143 #ifdef G_ENABLE_DEBUG
144 static const GDebugKey gtk_debug_keys[] = {
145 {"misc", GTK_DEBUG_MISC},
146 {"dnd", GTK_DEBUG_DND},
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)";
178 static gchar *add_dll_suffix(gchar *module_name)
180 gchar *suffix = strrchr(module_name, '.');
182 if (!suffix || stricmp(suffix, ".dll"))
184 gchar *old = module_name;
186 module_name = g_strconcat (module_name, ".dll", NULL);
189 return (module_name);
193 #undef gtk_init_check
195 /* This checks to see if the process is running suid or sgid
196 * at the current time. If so, we don't allow GTK+ to be initialized.
197 * This is meant to be a mild check - we only error out if we
198 * can prove the programmer is doing something wrong, not if
199 * they could be doing something wrong. For this reason, we
200 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
205 /* this isn't at all relevant on Windoze and doesn't compile ... --hb */
207 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
208 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
210 #ifdef HAVE_GETRESUID
211 /* These aren't in the header files, so we prototype them here.
213 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
214 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
216 if (getresuid (&ruid, &euid, &suid) != 0 ||
217 getresgid (&rgid, &egid, &sgid) != 0)
218 #endif /* HAVE_GETRESUID */
220 suid = ruid = getuid ();
221 sgid = rgid = getgid ();
226 if (ruid != euid || ruid != suid ||
227 rgid != egid || rgid != sgid)
229 g_warning ("This process is currently running setuid or setgid.\n"
230 "This is not a supported use of GTK+. You must create a helper\n"
231 "program instead. For further details, see:\n\n"
232 " http://www.gtk.org/setuid.html\n\n"
233 "Refusing to initialize GTK+.");
241 gtk_init_check (int *argc,
244 GSList *gtk_modules = NULL;
246 gchar *env_string = NULL;
251 if (!check_setugid ())
255 g_set_error_handler (gtk_error);
256 g_set_warning_handler (gtk_warning);
257 g_set_message_handler (gtk_message);
258 g_set_print_handler (gtk_print);
261 /* Initialize "gdk". We pass along the 'argc' and 'argv'
262 * parameters as they contain information that GDK uses
264 if (!gdk_init_check (argc, argv))
267 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
269 #ifdef G_ENABLE_DEBUG
270 env_string = getenv ("GTK_DEBUG");
271 if (env_string != NULL)
273 gtk_debug_flags = g_parse_debug_string (env_string,
278 #endif /* G_ENABLE_DEBUG */
280 env_string = getenv ("GTK_MODULES");
283 gchar **modules, **as;
286 modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
288 modules = g_strsplit (env_string, ";", -1);
290 for (as = modules; *as; as++)
293 gtk_modules = g_slist_prepend (gtk_modules, *as);
305 for (i = 1; i < *argc;)
307 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
308 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
310 gchar *module_name = (*argv)[i] + 12;
312 if (*module_name == '=')
314 else if (i + 1 < *argc)
318 module_name = (*argv)[i];
322 if (module_name && *module_name)
323 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
325 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
327 GLogLevelFlags fatal_mask;
329 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
330 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
331 g_log_set_always_fatal (fatal_mask);
334 #ifdef G_ENABLE_DEBUG
335 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
336 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
338 gchar *equal_pos = strchr ((*argv)[i], '=');
340 if (equal_pos != NULL)
342 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
346 else if ((i + 1) < *argc && (*argv)[i + 1])
348 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
356 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
357 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
359 gchar *equal_pos = strchr ((*argv)[i], '=');
361 if (equal_pos != NULL)
363 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
367 else if ((i + 1) < *argc && (*argv)[i + 1])
369 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
377 #endif /* G_ENABLE_DEBUG */
381 for (i = 1; i < *argc; i++)
383 for (k = i; k < *argc; k++)
384 if ((*argv)[k] != NULL)
390 for (j = i + k; j < *argc; j++)
391 (*argv)[j-k] = (*argv)[j];
397 if (gtk_debug_flags & GTK_DEBUG_UPDATES)
398 gdk_window_set_debug_updates (TRUE);
400 /* load gtk modules */
401 gtk_modules = g_slist_reverse (gtk_modules);
402 for (slist = gtk_modules; slist; slist = slist->next)
405 GModule *module = NULL;
406 GtkModuleInitFunc modinit_func = NULL;
408 module_name = slist->data;
411 if (!g_path_is_absolute (module_name))
413 gchar *old = module_name;
415 module_name = g_module_build_path (NULL, module_name);
419 module_name = add_dll_suffix(module_name);
421 if (g_module_supported ())
423 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
425 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
428 if (!g_slist_find (gtk_modules, modinit_func))
430 g_module_make_resident (module);
431 slist->data = modinit_func;
435 g_module_close (module);
442 g_message ("Failed to load module \"%s\": %s",
443 module ? g_module_name (module) : module_name,
446 g_module_close (module);
448 g_free (module_name);
453 bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
454 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
455 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
457 # else /* !G_OS_WIN32 */
459 bindtextdomain (GETTEXT_PACKAGE,
460 g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
461 g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
468 /* Translate to default:RTL if you want your widgets
469 * to be RTL, otherwise translate to default:LTR.
470 * Do *not* translate it to "predefinito:LTR", if it
471 * it isn't default:LTR or default:RTL it will not work
473 char *e = _("default:LTR");
474 if (strcmp (e, "default:RTL")==0) {
475 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
476 } else if (strcmp (e, "default:LTR")) {
477 g_warning ("Whoever translated default:LTR did so wrongly.\n");
481 /* Initialize the default visual and colormap to be
482 * used in creating widgets. (We want to use the system
483 * defaults so as to be nice to the colormap).
485 gtk_visual = gdk_visual_get_system ();
486 gtk_colormap = gdk_colormap_get_system ();
492 /* Register an exit function to make sure we are able to cleanup.
494 g_atexit (gtk_exit_func);
496 /* Set the 'initialized' flag.
498 gtk_initialized = TRUE;
500 /* initialize gtk modules
502 for (slist = gtk_modules; slist; slist = slist->next)
506 GtkModuleInitFunc modinit;
508 modinit = slist->data;
509 modinit (argc, argv);
512 g_slist_free (gtk_modules);
515 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
516 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
517 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
518 " if you don't know why you are getting this, you probably want to\n"
519 " use the stable branch which can be retrived from\n"
520 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
521 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
530 gtk_init (int *argc, char ***argv)
532 if (!gtk_init_check (argc, argv))
534 g_warning ("cannot open display: %s", gdk_get_display ());
542 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
544 if (sizeof_GtkWindow != sizeof (GtkWindow))
545 g_error ("Incompatible build!\n"
546 "The code using GTK+ thinks GtkWindow is of different\n"
547 "size than it actually is in this build of GTK+.\n"
548 "On Windows, this probably means that you have compiled\n"
549 "your code with gcc without the -fnative-struct switch.");
552 /* These two functions might get more checks added later, thus pass
553 * in the number of extra args.
556 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
558 check_sizeof_GtkWindow (sizeof_GtkWindow);
559 gtk_init (argc, argv);
563 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
565 check_sizeof_GtkWindow (sizeof_GtkWindow);
566 return gtk_init_check (argc, argv);
572 gtk_exit (gint errorcode)
574 /* Only if "gtk" has been initialized should we de-initialize.
576 /* de-initialisation is done by the gtk_exit_funct(),
577 * no need to do this here (Alex J.)
583 gtk_set_locale (void)
585 return gdk_set_locale ();
589 * gtk_get_default_language:
591 * Returns the ISO language code for the default language currently in
592 * effect. (Note that this can change over the life of an
593 * application.) The default language is derived from the current
594 * locale. It determines, for example, whether GTK+ uses the
595 * right-to-left or left-to-right text direction.
597 * Return value: the default language as an allocated string, must be freed
600 gtk_get_default_language (void)
605 lang = g_strdup (setlocale (LC_CTYPE, NULL));
606 p = strchr (lang, '.');
609 p = strchr (lang, '@');
621 GtkInitFunction *init;
624 gtk_main_loop_level++;
626 loop = g_main_new (TRUE);
627 main_loops = g_slist_prepend (main_loops, loop);
629 tmp_list = functions = init_functions;
630 init_functions = NULL;
634 init = tmp_list->data;
635 tmp_list = tmp_list->next;
637 (* init->function) (init->data);
640 g_list_free (functions);
642 if (g_main_is_running (main_loops->data))
644 GDK_THREADS_LEAVE ();
646 GDK_THREADS_ENTER ();
652 GList *reinvoke_list = NULL;
653 GtkQuitFunction *quitf;
655 while (quit_functions)
657 quitf = quit_functions->data;
659 tmp_list = quit_functions;
660 quit_functions = g_list_remove_link (quit_functions, quit_functions);
661 g_list_free_1 (tmp_list);
663 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
664 gtk_quit_invoke_function (quitf))
666 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
670 gtk_quit_destroy (quitf);
677 work = g_list_last (reinvoke_list);
679 quit_functions->prev = work;
680 work->next = quit_functions;
681 quit_functions = work;
687 main_loops = g_slist_remove (main_loops, loop);
689 g_main_destroy (loop);
691 gtk_main_loop_level--;
695 gtk_main_level (void)
697 return gtk_main_loop_level;
703 g_return_if_fail (main_loops != NULL);
705 g_main_quit (main_loops->data);
709 gtk_events_pending (void)
713 GDK_THREADS_LEAVE ();
714 result = g_main_pending();
715 GDK_THREADS_ENTER ();
721 gtk_main_iteration (void)
723 GDK_THREADS_LEAVE ();
724 g_main_iteration (TRUE);
725 GDK_THREADS_ENTER ();
728 return !g_main_is_running (main_loops->data);
734 gtk_main_iteration_do (gboolean blocking)
736 GDK_THREADS_LEAVE ();
737 g_main_iteration (blocking);
738 GDK_THREADS_ENTER ();
741 return !g_main_is_running (main_loops->data);
747 gtk_main_do_event (GdkEvent *event)
749 GtkWidget *event_widget;
750 GtkWidget *grab_widget;
751 GdkEvent *next_event;
754 /* If there are any events pending then get the next one.
756 next_event = gdk_event_peek ();
758 /* Try to compress enter/leave notify events. These event
759 * pairs occur when the mouse is dragged quickly across
760 * a window with many buttons (or through a menu). Instead
761 * of highlighting and de-highlighting each widget that
762 * is crossed it is better to simply de-highlight the widget
763 * which contained the mouse initially and highlight the
764 * widget which ends up containing the mouse.
767 if (((event->type == GDK_ENTER_NOTIFY) ||
768 (event->type == GDK_LEAVE_NOTIFY)) &&
769 ((next_event->type == GDK_ENTER_NOTIFY) ||
770 (next_event->type == GDK_LEAVE_NOTIFY)) &&
771 (next_event->type != event->type) &&
772 (next_event->any.window == event->any.window))
774 /* Throw both the peeked copy and the queued copy away
776 gdk_event_free (next_event);
777 next_event = gdk_event_get ();
778 gdk_event_free (next_event);
784 gdk_event_free (next_event);
786 /* Find the widget which got the event. We store the widget
787 * in the user_data field of GdkWindow's.
788 * Ignore the event if we don't have a widget for it, except
789 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
790 * Though this happens rarely, bogus events can occour
791 * for e.g. destroyed GdkWindows.
793 event_widget = gtk_get_event_widget (event);
796 /* To handle selection INCR transactions, we select
797 * PropertyNotify events on the requestor window and create
798 * a corresponding (fake) GdkWindow so that events get
799 * here. There won't be a widget though, so we have to handle
802 if (event->type == GDK_PROPERTY_NOTIFY)
803 gtk_selection_incr_event (event->any.window,
809 /* Push the event onto a stack of current events for
810 * gtk_current_event_get().
812 current_events = g_list_prepend (current_events, event);
814 /* If there is a grab in effect...
818 grab_widget = grabs->data;
820 /* If the grab widget is an ancestor of the event widget
821 * then we send the event to the original event widget.
822 * This is the key to implementing modality.
824 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
825 gtk_widget_is_ancestor (event_widget, grab_widget))
826 grab_widget = event_widget;
830 grab_widget = event_widget;
833 /* Not all events get sent to the grabbing widget.
834 * The delete, destroy, expose, focus change and resize
835 * events still get sent to the event widget because
836 * 1) these events have no meaning for the grabbing widget
837 * and 2) redirecting these events to the grabbing widget
838 * could cause the display to be messed up.
840 * Drag events are also not redirected, since it isn't
841 * clear what the semantics of that would be.
849 gtk_widget_ref (event_widget);
850 if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
851 !gtk_widget_event (event_widget, event))
852 gtk_widget_destroy (event_widget);
853 gtk_widget_unref (event_widget);
857 /* Unexpected GDK_DESTROY from the outside, ignore for
858 * child windows, handle like a GDK_DELETE for toplevels
860 if (!event_widget->parent)
862 gtk_widget_ref (event_widget);
863 if (!gtk_widget_event (event_widget, event) &&
864 !GTK_OBJECT_DESTROYED (event_widget))
865 gtk_widget_destroy (event_widget);
866 gtk_widget_unref (event_widget);
871 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
872 gdk_window_begin_paint_rect (event->any.window, &event->expose.area);
874 gtk_widget_event (event_widget, event);
876 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
877 gdk_window_end_paint (event->any.window);
880 case GDK_PROPERTY_NOTIFY:
882 case GDK_FOCUS_CHANGE:
886 case GDK_SELECTION_CLEAR:
887 case GDK_SELECTION_REQUEST:
888 case GDK_SELECTION_NOTIFY:
889 case GDK_CLIENT_EVENT:
890 case GDK_VISIBILITY_NOTIFY:
891 case GDK_WINDOW_STATE:
892 gtk_widget_event (event_widget, event);
896 case GDK_BUTTON_PRESS:
897 case GDK_2BUTTON_PRESS:
898 case GDK_3BUTTON_PRESS:
899 gtk_propagate_event (grab_widget, event);
903 case GDK_KEY_RELEASE:
906 if (gtk_invoke_key_snoopers (grab_widget, event))
909 /* else fall through */
910 case GDK_MOTION_NOTIFY:
911 case GDK_BUTTON_RELEASE:
912 case GDK_PROXIMITY_IN:
913 case GDK_PROXIMITY_OUT:
914 gtk_propagate_event (grab_widget, event);
917 case GDK_ENTER_NOTIFY:
918 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
920 gtk_widget_event (grab_widget, event);
921 if (event_widget == grab_widget)
922 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
926 case GDK_LEAVE_NOTIFY:
927 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
929 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
930 gtk_widget_event (event_widget, event);
932 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
933 gtk_widget_event (grab_widget, event);
936 case GDK_DRAG_STATUS:
937 case GDK_DROP_FINISHED:
938 gtk_drag_source_handle_event (event_widget, event);
942 case GDK_DRAG_MOTION:
944 gtk_drag_dest_handle_event (event_widget, event);
948 tmp_list = current_events;
949 current_events = g_list_remove_link (current_events, tmp_list);
950 g_list_free_1 (tmp_list);
966 gtk_grab_add (GtkWidget *widget)
968 g_return_if_fail (widget != NULL);
970 if (!GTK_WIDGET_HAS_GRAB (widget))
972 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
974 grabs = g_slist_prepend (grabs, widget);
975 gtk_widget_ref (widget);
980 gtk_grab_get_current (void)
983 return GTK_WIDGET (grabs->data);
988 gtk_grab_remove (GtkWidget *widget)
990 g_return_if_fail (widget != NULL);
992 if (GTK_WIDGET_HAS_GRAB (widget))
994 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
996 grabs = g_slist_remove (grabs, widget);
997 gtk_widget_unref (widget);
1002 gtk_init_add (GtkFunction function,
1005 GtkInitFunction *init;
1007 init = g_new (GtkInitFunction, 1);
1008 init->function = function;
1011 init_functions = g_list_prepend (init_functions, init);
1015 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1018 GtkKeySnooperData *data;
1019 static guint snooper_id = 1;
1021 g_return_val_if_fail (snooper != NULL, 0);
1023 data = g_new (GtkKeySnooperData, 1);
1024 data->func = snooper;
1025 data->func_data = func_data;
1026 data->id = snooper_id++;
1027 key_snoopers = g_slist_prepend (key_snoopers, data);
1033 gtk_key_snooper_remove (guint snooper_id)
1035 GtkKeySnooperData *data = NULL;
1038 slist = key_snoopers;
1042 if (data->id == snooper_id)
1045 slist = slist->next;
1049 key_snoopers = g_slist_remove (key_snoopers, data);
1053 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1057 gint return_val = FALSE;
1059 slist = key_snoopers;
1060 while (slist && !return_val)
1062 GtkKeySnooperData *data;
1065 slist = slist->next;
1066 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1073 gtk_quit_add_full (guint main_level,
1074 GtkFunction function,
1075 GtkCallbackMarshal marshal,
1077 GtkDestroyNotify destroy)
1079 static guint quit_id = 1;
1080 GtkQuitFunction *quitf;
1082 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1084 if (!quit_mem_chunk)
1085 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1086 512, G_ALLOC_AND_FREE);
1088 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1090 quitf->id = quit_id++;
1091 quitf->main_level = main_level;
1092 quitf->function = function;
1093 quitf->marshal = marshal;
1095 quitf->destroy = destroy;
1097 quit_functions = g_list_prepend (quit_functions, quitf);
1103 gtk_quit_destroy (GtkQuitFunction *quitf)
1106 quitf->destroy (quitf->data);
1107 g_mem_chunk_free (quit_mem_chunk, quitf);
1111 gtk_quit_destructor (GtkObject **object_p)
1114 gtk_object_destroy (*object_p);
1121 gtk_quit_add_destroy (guint main_level,
1124 GtkObject **object_p;
1126 g_return_if_fail (main_level > 0);
1127 g_return_if_fail (object != NULL);
1128 g_return_if_fail (GTK_IS_OBJECT (object));
1130 object_p = g_new (GtkObject*, 1);
1132 gtk_signal_connect (object,
1134 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1136 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1140 gtk_quit_add (guint main_level,
1141 GtkFunction function,
1144 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1148 gtk_quit_remove (guint id)
1150 GtkQuitFunction *quitf;
1153 tmp_list = quit_functions;
1156 quitf = tmp_list->data;
1158 if (quitf->id == id)
1160 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1161 g_list_free (tmp_list);
1162 gtk_quit_destroy (quitf);
1167 tmp_list = tmp_list->next;
1172 gtk_quit_remove_by_data (gpointer data)
1174 GtkQuitFunction *quitf;
1177 tmp_list = quit_functions;
1180 quitf = tmp_list->data;
1182 if (quitf->data == data)
1184 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1185 g_list_free (tmp_list);
1186 gtk_quit_destroy (quitf);
1191 tmp_list = tmp_list->next;
1196 gtk_timeout_add_full (guint32 interval,
1197 GtkFunction function,
1198 GtkCallbackMarshal marshal,
1200 GtkDestroyNotify destroy)
1204 GtkClosure *closure;
1206 closure = g_new (GtkClosure, 1);
1207 closure->marshal = marshal;
1208 closure->data = data;
1209 closure->destroy = destroy;
1211 return g_timeout_add_full (0, interval,
1212 gtk_invoke_idle_timeout,
1214 gtk_destroy_closure);
1217 return g_timeout_add_full (0, interval, function, data, destroy);
1221 gtk_timeout_add (guint32 interval,
1222 GtkFunction function,
1225 return g_timeout_add_full (0, interval, function, data, NULL);
1229 gtk_timeout_remove (guint tag)
1231 g_source_remove (tag);
1235 gtk_idle_add_full (gint priority,
1236 GtkFunction function,
1237 GtkCallbackMarshal marshal,
1239 GtkDestroyNotify destroy)
1243 GtkClosure *closure;
1245 closure = g_new (GtkClosure, 1);
1246 closure->marshal = marshal;
1247 closure->data = data;
1248 closure->destroy = destroy;
1250 return g_idle_add_full (priority,
1251 gtk_invoke_idle_timeout,
1253 gtk_destroy_closure);
1256 return g_idle_add_full (priority, function, data, destroy);
1260 gtk_idle_add (GtkFunction function,
1263 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1267 gtk_idle_add_priority (gint priority,
1268 GtkFunction function,
1271 return g_idle_add_full (priority, function, data, NULL);
1275 gtk_idle_remove (guint tag)
1277 g_source_remove (tag);
1281 gtk_idle_remove_by_data (gpointer data)
1283 if (!g_idle_remove_by_data (data))
1284 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1288 gtk_input_add_full (gint source,
1289 GdkInputCondition condition,
1290 GdkInputFunction function,
1291 GtkCallbackMarshal marshal,
1293 GtkDestroyNotify destroy)
1297 GtkClosure *closure;
1299 closure = g_new (GtkClosure, 1);
1300 closure->marshal = marshal;
1301 closure->data = data;
1302 closure->destroy = destroy;
1304 return gdk_input_add_full (source,
1306 (GdkInputFunction) gtk_invoke_input,
1308 (GdkDestroyNotify) gtk_destroy_closure);
1311 return gdk_input_add_full (source, condition, function, data, destroy);
1315 gtk_input_remove (guint tag)
1317 g_source_remove (tag);
1321 gtk_destroy_closure (gpointer data)
1323 GtkClosure *closure = data;
1325 if (closure->destroy)
1326 (closure->destroy) (closure->data);
1331 gtk_invoke_idle_timeout (gpointer data)
1333 GtkClosure *closure = data;
1336 gint ret_val = FALSE;
1337 args[0].name = NULL;
1338 args[0].type = GTK_TYPE_BOOL;
1339 args[0].d.pointer_data = &ret_val;
1340 closure->marshal (NULL, closure->data, 0, args);
1345 gtk_invoke_input (gpointer data,
1347 GdkInputCondition condition)
1349 GtkClosure *closure = data;
1352 args[0].type = GTK_TYPE_INT;
1353 args[0].name = NULL;
1354 GTK_VALUE_INT(args[0]) = source;
1355 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1356 args[1].name = NULL;
1357 GTK_VALUE_FLAGS(args[1]) = condition;
1358 args[2].type = GTK_TYPE_NONE;
1359 args[2].name = NULL;
1361 closure->marshal (NULL, closure->data, 2, args);
1365 * gtk_get_current_event:
1367 * Obtains a copy of the event currently being processed by GTK+. For
1368 * example, if you get a "clicked" signal from #GtkButton, the current
1369 * event will be the #GdkEventButton that triggered the "clicked"
1370 * signal. The returned event must be freed with gdk_event_free().
1371 * If there is no current event, the function returns %NULL.
1373 * Return value: a copy of the current event, or %NULL if no current event.
1376 gtk_get_current_event (void)
1379 return gdk_event_copy (current_events->data);
1385 * gtk_get_current_event_time:
1387 * If there is a current event and it has a timestamp, return that
1388 * timestamp, otherwise return %GDK_CURRENT_TIME.
1390 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1393 gtk_get_current_event_time (void)
1396 return gdk_event_get_time (current_events->data);
1398 return GDK_CURRENT_TIME;
1402 * gtk_get_current_event_state:
1403 * @state: a location to store the state of the current event
1405 * If there is a current event and it has a state field, place
1406 * that state field in @state and return %TRUE, otherwise return
1409 * Return value: %TRUE if there was a current event and it had a state field
1412 gtk_get_current_event_state (GdkModifierType *state)
1414 g_return_val_if_fail (state != NULL, FALSE);
1417 return gdk_event_get_state (current_events->data, state);
1426 * gtk_get_event_widget:
1427 * @event: a #GdkEvent
1429 * If @event is %NULL or the event was not associated with any widget,
1430 * returns %NULL, otherwise returns the widget that received the event
1433 * Return value: the widget that originally received @event, or %NULL
1436 gtk_get_event_widget (GdkEvent *event)
1441 if (event && event->any.window)
1442 gdk_window_get_user_data (event->any.window, (void**) &widget);
1448 gtk_exit_func (void)
1450 if (gtk_initialized)
1452 gtk_initialized = FALSE;
1458 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1460 if (!quitf->marshal)
1461 return quitf->function (quitf->data);
1465 gint ret_val = FALSE;
1467 args[0].name = NULL;
1468 args[0].type = GTK_TYPE_BOOL;
1469 args[0].d.pointer_data = &ret_val;
1470 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1478 * gtk_propagate_event:
1479 * @widget: a #GtkWidget
1482 * Sends an event to a widget, propagating the event to parent widgets
1483 * if the event remains unhandled. Events received by GTK+ from GDK
1484 * normally begin in gtk_main_do_event(). Depending on the type of
1485 * event, existence of modal dialogs, grabs, etc., the event may be
1486 * propagated; if so, this function is used. gtk_propagate_event()
1487 * calls gtk_widget_event() on each widget it decides to send the
1488 * event to. So gtk_widget_event() is the lowest-level function; it
1489 * simply emits the "event" and possibly an event-specific signal on a
1490 * widget. gtk_propagate_event() is a bit higher-level, and
1491 * gtk_main_do_event() is the highest level.
1493 * All that said, you most likely don't want to use any of these
1494 * functions; synthesizing events is rarely needed. Consider asking on
1495 * the mailing list for better ways to achieve your goals. For
1496 * example, use gdk_window_invalidate_rect() or
1497 * gtk_widget_queue_draw() instead of making up expose events.
1501 gtk_propagate_event (GtkWidget *widget,
1506 g_return_if_fail (widget != NULL);
1507 g_return_if_fail (GTK_IS_WIDGET (widget));
1508 g_return_if_fail (event != NULL);
1510 handled_event = FALSE;
1512 gtk_widget_ref (widget);
1514 if ((event->type == GDK_KEY_PRESS) ||
1515 (event->type == GDK_KEY_RELEASE))
1517 /* Only send key events within Window widgets to the Window
1518 * The Window widget will in turn pass the
1519 * key event on to the currently focused widget
1524 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1528 /* If there is a grab within the window, give the grab widget
1529 * a first crack at the key event
1531 if (widget != window && GTK_WIDGET_HAS_GRAB (widget))
1532 handled_event = gtk_widget_event (widget, event);
1536 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1539 if (GTK_WIDGET_IS_SENSITIVE (window))
1540 gtk_widget_event (window, event);
1544 handled_event = TRUE; /* don't send to widget */
1548 /* Other events get propagated up the widget tree
1549 * so that parents can see the button and motion
1550 * events of the children.
1558 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1559 tmp = widget->parent;
1560 gtk_widget_unref (widget);
1564 if (!handled_event && widget)
1565 gtk_widget_ref (widget);
1571 gtk_widget_unref (widget);
1576 gtk_error (gchar *str)
1582 gtk_warning (gchar *str)
1588 gtk_message (gchar *str)
1594 gtk_print (gchar *str)
1596 static GtkWidget *window = NULL;
1597 static GtkWidget *text;
1598 static int level = 0;
1602 GtkWidget *hscrollbar;
1603 GtkWidget *vscrollbar;
1604 GtkWidget *separator;
1609 fputs (str, stdout);
1616 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1618 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1619 (GtkSignalFunc) gtk_widget_destroyed,
1622 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1624 box1 = gtk_vbox_new (FALSE, 0);
1625 gtk_container_add (GTK_CONTAINER (window), box1);
1626 gtk_widget_show (box1);
1629 box2 = gtk_vbox_new (FALSE, 10);
1630 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1631 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1632 gtk_widget_show (box2);
1635 table = gtk_table_new (2, 2, FALSE);
1636 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1637 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1638 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1639 gtk_widget_show (table);
1641 text = gtk_text_new (NULL, NULL);
1642 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1643 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1644 gtk_widget_show (text);
1645 gtk_widget_realize (text);
1647 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1648 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1649 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1650 gtk_widget_show (hscrollbar);
1652 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1653 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1654 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1655 gtk_widget_show (vscrollbar);
1657 separator = gtk_hseparator_new ();
1658 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1659 gtk_widget_show (separator);
1662 box2 = gtk_vbox_new (FALSE, 10);
1663 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1664 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1665 gtk_widget_show (box2);
1668 button = gtk_button_new_with_label ("close");
1669 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1670 (GtkSignalFunc) gtk_widget_hide,
1671 GTK_OBJECT (window));
1672 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1673 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1674 gtk_widget_grab_default (button);
1675 gtk_widget_show (button);
1679 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1682 if (!GTK_WIDGET_VISIBLE (window))
1683 gtk_widget_show (window);