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 gtk_widget_event (event_widget, event);
895 case GDK_BUTTON_PRESS:
896 case GDK_2BUTTON_PRESS:
897 case GDK_3BUTTON_PRESS:
898 gtk_propagate_event (grab_widget, event);
902 case GDK_KEY_RELEASE:
905 if (gtk_invoke_key_snoopers (grab_widget, event))
908 /* else fall through */
909 case GDK_MOTION_NOTIFY:
910 case GDK_BUTTON_RELEASE:
911 case GDK_PROXIMITY_IN:
912 case GDK_PROXIMITY_OUT:
913 gtk_propagate_event (grab_widget, event);
916 case GDK_ENTER_NOTIFY:
917 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
919 gtk_widget_event (grab_widget, event);
920 if (event_widget == grab_widget)
921 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
925 case GDK_LEAVE_NOTIFY:
926 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
928 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
929 gtk_widget_event (event_widget, event);
931 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
932 gtk_widget_event (grab_widget, event);
935 case GDK_DRAG_STATUS:
936 case GDK_DROP_FINISHED:
937 gtk_drag_source_handle_event (event_widget, event);
941 case GDK_DRAG_MOTION:
943 gtk_drag_dest_handle_event (event_widget, event);
947 tmp_list = current_events;
948 current_events = g_list_remove_link (current_events, tmp_list);
949 g_list_free_1 (tmp_list);
965 gtk_grab_add (GtkWidget *widget)
967 g_return_if_fail (widget != NULL);
969 if (!GTK_WIDGET_HAS_GRAB (widget))
971 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
973 grabs = g_slist_prepend (grabs, widget);
974 gtk_widget_ref (widget);
979 gtk_grab_get_current (void)
982 return GTK_WIDGET (grabs->data);
987 gtk_grab_remove (GtkWidget *widget)
989 g_return_if_fail (widget != NULL);
991 if (GTK_WIDGET_HAS_GRAB (widget))
993 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
995 grabs = g_slist_remove (grabs, widget);
996 gtk_widget_unref (widget);
1001 gtk_init_add (GtkFunction function,
1004 GtkInitFunction *init;
1006 init = g_new (GtkInitFunction, 1);
1007 init->function = function;
1010 init_functions = g_list_prepend (init_functions, init);
1014 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
1017 GtkKeySnooperData *data;
1018 static guint snooper_id = 1;
1020 g_return_val_if_fail (snooper != NULL, 0);
1022 data = g_new (GtkKeySnooperData, 1);
1023 data->func = snooper;
1024 data->func_data = func_data;
1025 data->id = snooper_id++;
1026 key_snoopers = g_slist_prepend (key_snoopers, data);
1032 gtk_key_snooper_remove (guint snooper_id)
1034 GtkKeySnooperData *data = NULL;
1037 slist = key_snoopers;
1041 if (data->id == snooper_id)
1044 slist = slist->next;
1048 key_snoopers = g_slist_remove (key_snoopers, data);
1052 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1056 gint return_val = FALSE;
1058 slist = key_snoopers;
1059 while (slist && !return_val)
1061 GtkKeySnooperData *data;
1064 slist = slist->next;
1065 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1072 gtk_quit_add_full (guint main_level,
1073 GtkFunction function,
1074 GtkCallbackMarshal marshal,
1076 GtkDestroyNotify destroy)
1078 static guint quit_id = 1;
1079 GtkQuitFunction *quitf;
1081 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1083 if (!quit_mem_chunk)
1084 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1085 512, G_ALLOC_AND_FREE);
1087 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1089 quitf->id = quit_id++;
1090 quitf->main_level = main_level;
1091 quitf->function = function;
1092 quitf->marshal = marshal;
1094 quitf->destroy = destroy;
1096 quit_functions = g_list_prepend (quit_functions, quitf);
1102 gtk_quit_destroy (GtkQuitFunction *quitf)
1105 quitf->destroy (quitf->data);
1106 g_mem_chunk_free (quit_mem_chunk, quitf);
1110 gtk_quit_destructor (GtkObject **object_p)
1113 gtk_object_destroy (*object_p);
1120 gtk_quit_add_destroy (guint main_level,
1123 GtkObject **object_p;
1125 g_return_if_fail (main_level > 0);
1126 g_return_if_fail (object != NULL);
1127 g_return_if_fail (GTK_IS_OBJECT (object));
1129 object_p = g_new (GtkObject*, 1);
1131 gtk_signal_connect (object,
1133 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1135 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1139 gtk_quit_add (guint main_level,
1140 GtkFunction function,
1143 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1147 gtk_quit_remove (guint id)
1149 GtkQuitFunction *quitf;
1152 tmp_list = quit_functions;
1155 quitf = tmp_list->data;
1157 if (quitf->id == id)
1159 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1160 g_list_free (tmp_list);
1161 gtk_quit_destroy (quitf);
1166 tmp_list = tmp_list->next;
1171 gtk_quit_remove_by_data (gpointer data)
1173 GtkQuitFunction *quitf;
1176 tmp_list = quit_functions;
1179 quitf = tmp_list->data;
1181 if (quitf->data == data)
1183 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1184 g_list_free (tmp_list);
1185 gtk_quit_destroy (quitf);
1190 tmp_list = tmp_list->next;
1195 gtk_timeout_add_full (guint32 interval,
1196 GtkFunction function,
1197 GtkCallbackMarshal marshal,
1199 GtkDestroyNotify destroy)
1203 GtkClosure *closure;
1205 closure = g_new (GtkClosure, 1);
1206 closure->marshal = marshal;
1207 closure->data = data;
1208 closure->destroy = destroy;
1210 return g_timeout_add_full (0, interval,
1211 gtk_invoke_idle_timeout,
1213 gtk_destroy_closure);
1216 return g_timeout_add_full (0, interval, function, data, destroy);
1220 gtk_timeout_add (guint32 interval,
1221 GtkFunction function,
1224 return g_timeout_add_full (0, interval, function, data, NULL);
1228 gtk_timeout_remove (guint tag)
1230 g_source_remove (tag);
1234 gtk_idle_add_full (gint priority,
1235 GtkFunction function,
1236 GtkCallbackMarshal marshal,
1238 GtkDestroyNotify destroy)
1242 GtkClosure *closure;
1244 closure = g_new (GtkClosure, 1);
1245 closure->marshal = marshal;
1246 closure->data = data;
1247 closure->destroy = destroy;
1249 return g_idle_add_full (priority,
1250 gtk_invoke_idle_timeout,
1252 gtk_destroy_closure);
1255 return g_idle_add_full (priority, function, data, destroy);
1259 gtk_idle_add (GtkFunction function,
1262 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1266 gtk_idle_add_priority (gint priority,
1267 GtkFunction function,
1270 return g_idle_add_full (priority, function, data, NULL);
1274 gtk_idle_remove (guint tag)
1276 g_source_remove (tag);
1280 gtk_idle_remove_by_data (gpointer data)
1282 if (!g_idle_remove_by_data (data))
1283 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1287 gtk_input_add_full (gint source,
1288 GdkInputCondition condition,
1289 GdkInputFunction function,
1290 GtkCallbackMarshal marshal,
1292 GtkDestroyNotify destroy)
1296 GtkClosure *closure;
1298 closure = g_new (GtkClosure, 1);
1299 closure->marshal = marshal;
1300 closure->data = data;
1301 closure->destroy = destroy;
1303 return gdk_input_add_full (source,
1305 (GdkInputFunction) gtk_invoke_input,
1307 (GdkDestroyNotify) gtk_destroy_closure);
1310 return gdk_input_add_full (source, condition, function, data, destroy);
1314 gtk_input_remove (guint tag)
1316 g_source_remove (tag);
1320 gtk_destroy_closure (gpointer data)
1322 GtkClosure *closure = data;
1324 if (closure->destroy)
1325 (closure->destroy) (closure->data);
1330 gtk_invoke_idle_timeout (gpointer data)
1332 GtkClosure *closure = data;
1335 gint ret_val = FALSE;
1336 args[0].name = NULL;
1337 args[0].type = GTK_TYPE_BOOL;
1338 args[0].d.pointer_data = &ret_val;
1339 closure->marshal (NULL, closure->data, 0, args);
1344 gtk_invoke_input (gpointer data,
1346 GdkInputCondition condition)
1348 GtkClosure *closure = data;
1351 args[0].type = GTK_TYPE_INT;
1352 args[0].name = NULL;
1353 GTK_VALUE_INT(args[0]) = source;
1354 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1355 args[1].name = NULL;
1356 GTK_VALUE_FLAGS(args[1]) = condition;
1357 args[2].type = GTK_TYPE_NONE;
1358 args[2].name = NULL;
1360 closure->marshal (NULL, closure->data, 2, args);
1364 * gtk_get_current_event:
1366 * Obtains a copy of the event currently being processed by GTK+. For
1367 * example, if you get a "clicked" signal from #GtkButton, the current
1368 * event will be the #GdkEventButton that triggered the "clicked"
1369 * signal. The returned event must be freed with gdk_event_free().
1370 * If there is no current event, the function returns %NULL.
1372 * Return value: a copy of the current event, or %NULL if no current event.
1375 gtk_get_current_event (void)
1378 return gdk_event_copy (current_events->data);
1384 * gtk_get_current_event_time:
1386 * If there is a current event and it has a timestamp, return that
1387 * timestamp, otherwise return %GDK_CURRENT_TIME.
1389 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1392 gtk_get_current_event_time (void)
1395 return gdk_event_get_time (current_events->data);
1397 return GDK_CURRENT_TIME;
1401 * gtk_get_current_event_state:
1402 * @state: a location to store the state of the current event
1404 * If there is a current event and it has a state field, place
1405 * that state field in @state and return %TRUE, otherwise return
1408 * Return value: %TRUE if there was a current event and it had a state field
1411 gtk_get_current_event_state (GdkModifierType *state)
1413 g_return_val_if_fail (state != NULL, FALSE);
1416 return gdk_event_get_state (current_events->data, state);
1425 * gtk_get_event_widget:
1426 * @event: a #GdkEvent
1428 * If @event is %NULL or the event was not associated with any widget,
1429 * returns %NULL, otherwise returns the widget that received the event
1432 * Return value: the widget that originally received @event, or %NULL
1435 gtk_get_event_widget (GdkEvent *event)
1440 if (event && event->any.window)
1441 gdk_window_get_user_data (event->any.window, (void**) &widget);
1447 gtk_exit_func (void)
1449 if (gtk_initialized)
1451 gtk_initialized = FALSE;
1457 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1459 if (!quitf->marshal)
1460 return quitf->function (quitf->data);
1464 gint ret_val = FALSE;
1466 args[0].name = NULL;
1467 args[0].type = GTK_TYPE_BOOL;
1468 args[0].d.pointer_data = &ret_val;
1469 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1477 * gtk_propagate_event:
1478 * @widget: a #GtkWidget
1481 * Sends an event to a widget, propagating the event to parent widgets
1482 * if the event remains unhandled. Events received by GTK+ from GDK
1483 * normally begin in gtk_main_do_event(). Depending on the type of
1484 * event, existence of modal dialogs, grabs, etc., the event may be
1485 * propagated; if so, this function is used. gtk_propagate_event()
1486 * calls gtk_widget_event() on each widget it decides to send the
1487 * event to. So gtk_widget_event() is the lowest-level function; it
1488 * simply emits the "event" and possibly an event-specific signal on a
1489 * widget. gtk_propagate_event() is a bit higher-level, and
1490 * gtk_main_do_event() is the highest level.
1492 * All that said, you most likely don't want to use any of these
1493 * functions; synthesizing events is rarely needed. Consider asking on
1494 * the mailing list for better ways to achieve your goals. For
1495 * example, use gdk_window_invalidate_rect() or
1496 * gtk_widget_queue_draw() instead of making up expose events.
1500 gtk_propagate_event (GtkWidget *widget,
1505 g_return_if_fail (widget != NULL);
1506 g_return_if_fail (GTK_IS_WIDGET (widget));
1507 g_return_if_fail (event != NULL);
1509 handled_event = FALSE;
1511 if ((event->type == GDK_KEY_PRESS) ||
1512 (event->type == GDK_KEY_RELEASE))
1514 /* Only send key events within Window widgets to the Window
1515 * The Window widget will in turn pass the
1516 * key event on to the currently focused widget
1521 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1524 if (GTK_WIDGET_IS_SENSITIVE (window))
1525 gtk_widget_event (window, event);
1527 handled_event = TRUE; /* don't send to widget */
1531 /* Other events get propagated up the widget tree
1532 * so that parents can see the button and motion
1533 * events of the children.
1535 while (!handled_event && widget)
1539 gtk_widget_ref (widget);
1540 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1541 tmp = widget->parent;
1542 gtk_widget_unref (widget);
1549 gtk_error (gchar *str)
1555 gtk_warning (gchar *str)
1561 gtk_message (gchar *str)
1567 gtk_print (gchar *str)
1569 static GtkWidget *window = NULL;
1570 static GtkWidget *text;
1571 static int level = 0;
1575 GtkWidget *hscrollbar;
1576 GtkWidget *vscrollbar;
1577 GtkWidget *separator;
1582 fputs (str, stdout);
1589 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1591 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1592 (GtkSignalFunc) gtk_widget_destroyed,
1595 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1597 box1 = gtk_vbox_new (FALSE, 0);
1598 gtk_container_add (GTK_CONTAINER (window), box1);
1599 gtk_widget_show (box1);
1602 box2 = gtk_vbox_new (FALSE, 10);
1603 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1604 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1605 gtk_widget_show (box2);
1608 table = gtk_table_new (2, 2, FALSE);
1609 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1610 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1611 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1612 gtk_widget_show (table);
1614 text = gtk_text_new (NULL, NULL);
1615 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1616 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1617 gtk_widget_show (text);
1618 gtk_widget_realize (text);
1620 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1621 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1622 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1623 gtk_widget_show (hscrollbar);
1625 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1626 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1627 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1628 gtk_widget_show (vscrollbar);
1630 separator = gtk_hseparator_new ();
1631 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1632 gtk_widget_show (separator);
1635 box2 = gtk_vbox_new (FALSE, 10);
1636 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1637 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1638 gtk_widget_show (box2);
1641 button = gtk_button_new_with_label ("close");
1642 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1643 (GtkSignalFunc) gtk_widget_hide,
1644 GTK_OBJECT (window));
1645 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1646 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1647 gtk_widget_grab_default (button);
1648 gtk_widget_show (button);
1652 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1655 if (!GTK_WIDGET_VISIBLE (window))
1656 gtk_widget_show (window);