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 */
37 #include "gtkcompat.h"
40 #include "gtkselection.h"
41 #include "gtksignal.h"
42 #include "gtkwidget.h"
43 #include "gtkwindow.h"
44 #include "gtkprivate.h"
45 #include "gdk/gdki18n.h"
50 /* Private type definitions
52 typedef struct _GtkInitFunction GtkInitFunction;
53 typedef struct _GtkQuitFunction GtkQuitFunction;
54 typedef struct _GtkClosure GtkClosure;
55 typedef struct _GtkKeySnooperData GtkKeySnooperData;
57 struct _GtkInitFunction
63 struct _GtkQuitFunction
67 GtkCallbackMarshal marshal;
70 GtkDestroyNotify destroy;
75 GtkCallbackMarshal marshal;
77 GtkDestroyNotify destroy;
80 struct _GtkKeySnooperData
87 static void gtk_exit_func (void);
88 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
89 static void gtk_quit_destroy (GtkQuitFunction *quitf);
90 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
93 static void gtk_destroy_closure (gpointer data);
94 static gboolean gtk_invoke_idle_timeout (gpointer data);
95 static void gtk_invoke_input (gpointer data,
97 GdkInputCondition condition);
100 static void gtk_error (gchar *str);
101 static void gtk_warning (gchar *str);
102 static void gtk_message (gchar *str);
103 static void gtk_print (gchar *str);
106 const guint gtk_major_version = GTK_MAJOR_VERSION;
107 const guint gtk_minor_version = GTK_MINOR_VERSION;
108 const guint gtk_micro_version = GTK_MICRO_VERSION;
109 const guint gtk_binary_age = GTK_BINARY_AGE;
110 const guint gtk_interface_age = GTK_INTERFACE_AGE;
112 static guint gtk_main_loop_level = 0;
113 static gint gtk_initialized = FALSE;
114 static GList *current_events = NULL;
116 static GSList *main_loops = NULL; /* stack of currently executing main loops */
118 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
119 * widget is the first one on the list.
121 static GList *init_functions = NULL; /* A list of init functions.
123 static GList *quit_functions = NULL; /* A list of quit functions.
125 static GMemChunk *quit_mem_chunk = NULL;
127 static GSList *key_snoopers = NULL;
129 static GdkVisual *gtk_visual; /* The visual to be used in creating new
132 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
136 guint gtk_debug_flags = 0; /* Global GTK debug flag */
138 #ifdef G_ENABLE_DEBUG
139 static const GDebugKey gtk_debug_keys[] = {
140 {"objects", GTK_DEBUG_OBJECTS},
141 {"misc", GTK_DEBUG_MISC},
142 {"signals", GTK_DEBUG_SIGNALS},
143 {"dnd", GTK_DEBUG_DND},
144 {"plugsocket", GTK_DEBUG_PLUGSOCKET},
145 {"text", GTK_DEBUG_TEXT}
148 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
150 #endif /* G_ENABLE_DEBUG */
153 gtk_check_version (guint required_major,
154 guint required_minor,
155 guint required_micro)
157 if (required_major > GTK_MAJOR_VERSION)
158 return "Gtk+ version too old (major mismatch)";
159 if (required_major < GTK_MAJOR_VERSION)
160 return "Gtk+ version too new (major mismatch)";
161 if (required_minor > GTK_MINOR_VERSION)
162 return "Gtk+ version too old (minor mismatch)";
163 if (required_minor < GTK_MINOR_VERSION)
164 return "Gtk+ version too new (minor mismatch)";
165 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
166 return "Gtk+ version too new (micro mismatch)";
167 if (required_micro > GTK_MICRO_VERSION)
168 return "Gtk+ version too old (micro mismatch)";
173 static gchar *add_dll_suffix(gchar *module_name)
175 gchar *suffix = strrchr(module_name, '.');
177 if (!suffix || stricmp(suffix, ".dll"))
179 gchar *old = module_name;
181 module_name = g_strconcat (module_name, ".dll", NULL);
184 return (module_name);
188 #undef gtk_init_check
191 gtk_init_check (int *argc,
194 GSList *gtk_modules = NULL;
196 gchar *env_string = NULL;
202 g_set_error_handler (gtk_error);
203 g_set_warning_handler (gtk_warning);
204 g_set_message_handler (gtk_message);
205 g_set_print_handler (gtk_print);
208 /* Initialize "gdk". We pass along the 'argc' and 'argv'
209 * parameters as they contain information that GDK uses
211 if (!gdk_init_check (argc, argv))
214 gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
216 #ifdef G_ENABLE_DEBUG
217 env_string = getenv ("GTK_DEBUG");
218 if (env_string != NULL)
220 gtk_debug_flags = g_parse_debug_string (env_string,
225 #endif /* G_ENABLE_DEBUG */
227 env_string = getenv ("GTK_MODULES");
230 gchar **modules, **as;
233 modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
235 modules = g_strsplit (env_string, ";", -1);
237 for (as = modules; *as; as++)
240 gtk_modules = g_slist_prepend (gtk_modules, *as);
252 for (i = 1; i < *argc;)
254 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
255 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
257 gchar *module_name = (*argv)[i] + 12;
259 if (*module_name == '=')
261 else if (i + 1 < *argc)
265 module_name = (*argv)[i];
269 if (module_name && *module_name)
270 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
272 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
274 GLogLevelFlags fatal_mask;
276 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
277 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
278 g_log_set_always_fatal (fatal_mask);
281 #ifdef G_ENABLE_DEBUG
282 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
283 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
285 gchar *equal_pos = strchr ((*argv)[i], '=');
287 if (equal_pos != NULL)
289 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
293 else if ((i + 1) < *argc && (*argv)[i + 1])
295 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
303 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
304 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
306 gchar *equal_pos = strchr ((*argv)[i], '=');
308 if (equal_pos != NULL)
310 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
314 else if ((i + 1) < *argc && (*argv)[i + 1])
316 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
324 #endif /* G_ENABLE_DEBUG */
328 for (i = 1; i < *argc; i++)
330 for (k = i; k < *argc; k++)
331 if ((*argv)[k] != NULL)
337 for (j = i + k; j < *argc; j++)
338 (*argv)[j-k] = (*argv)[j];
344 /* load gtk modules */
345 gtk_modules = g_slist_reverse (gtk_modules);
346 for (slist = gtk_modules; slist; slist = slist->next)
349 GModule *module = NULL;
350 GtkModuleInitFunc modinit_func = NULL;
352 module_name = slist->data;
355 if (!g_path_is_absolute (module_name))
357 gchar *old = module_name;
359 module_name = g_module_build_path (NULL, module_name);
363 module_name = add_dll_suffix(module_name);
365 if (g_module_supported ())
367 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
369 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
372 if (!g_slist_find (gtk_modules, modinit_func))
374 g_module_make_resident (module);
375 slist->data = modinit_func;
379 g_module_close (module);
386 g_warning ("Failed to load module \"%s\": %s",
387 module ? g_module_name (module) : module_name,
390 g_module_close (module);
392 g_free (module_name);
397 bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
398 # ifdef HAVE_BIND_TEXTDOMAIN_CODSET
399 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
401 # else /* !G_OS_WIN32 */
403 /* GTk+ locale dir is %WinDir%\gtk+\locale */
404 bindtextdomain (GETTEXT_PACKAGE,
405 g_strconcat (gtk_win32_get_installation_directory (),
414 /* Translate to default:RTL if you want your widgets
415 * to be RTL, otherwise translate to default:LTR
417 char *e = _("default:LTR");
418 if (strcmp (e, "default:RTL")==0) {
419 gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
420 } else if (strcmp (e, "default:LTR")) {
421 g_warning ("Whoever translated default:LTR did so wrongly.\n");
425 /* Initialize the default visual and colormap to be
426 * used in creating widgets. (We want to use the system
427 * defaults so as to be nice to the colormap).
429 gtk_visual = gdk_visual_get_system ();
430 gtk_colormap = gdk_colormap_get_system ();
437 /* Register an exit function to make sure we are able to cleanup.
439 g_atexit (gtk_exit_func);
441 /* Set the 'initialized' flag.
443 gtk_initialized = TRUE;
445 /* initialize gtk modules
447 for (slist = gtk_modules; slist; slist = slist->next)
451 GtkModuleInitFunc modinit;
453 modinit = slist->data;
454 modinit (argc, argv);
457 g_slist_free (gtk_modules);
460 /* No use warning on Win32, there aren't any non-devel versions anyhow... */
461 g_message ("" "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
462 " UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
463 " if you don't know why you are getting this, you probably want to\n"
464 " use the stable branch which can be retrived from\n"
465 " ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
466 " cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
475 gtk_init (int *argc, char ***argv)
477 if (!gtk_init_check (argc, argv))
479 g_warning ("cannot open display: %s", gdk_get_display ());
487 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
489 if (sizeof_GtkWindow != sizeof (GtkWindow))
490 g_error ("Incompatible build!\n"
491 "The code using GTK+ thinks GtkWindow is of different\n"
492 "size than it actually is in this build of GTK+.\n"
493 "On Windows, this probably means that you have compiled\n"
494 "your code with gcc without the -fnative-struct switch.");
497 /* These two functions might get more checks added later, thus pass
498 * in the number of extra args.
501 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
503 check_sizeof_GtkWindow (sizeof_GtkWindow);
504 gtk_init (argc, argv);
508 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
510 check_sizeof_GtkWindow (sizeof_GtkWindow);
511 return gtk_init_check (argc, argv);
517 gtk_exit (gint errorcode)
519 /* Only if "gtk" has been initialized should we de-initialize.
521 /* de-initialisation is done by the gtk_exit_funct(),
522 * no need to do this here (Alex J.)
528 gtk_set_locale (void)
530 return gdk_set_locale ();
534 gtk_get_default_language (void)
539 lang = g_strdup (setlocale (LC_CTYPE, NULL));
540 p = strchr (lang, '.');
543 p = strchr (lang, '@');
555 GtkInitFunction *init;
558 gtk_main_loop_level++;
560 loop = g_main_new (TRUE);
561 main_loops = g_slist_prepend (main_loops, loop);
563 tmp_list = functions = init_functions;
564 init_functions = NULL;
568 init = tmp_list->data;
569 tmp_list = tmp_list->next;
571 (* init->function) (init->data);
574 g_list_free (functions);
576 if (g_main_is_running (main_loops->data))
578 GDK_THREADS_LEAVE ();
580 GDK_THREADS_ENTER ();
586 GList *reinvoke_list = NULL;
587 GtkQuitFunction *quitf;
589 while (quit_functions)
591 quitf = quit_functions->data;
593 tmp_list = quit_functions;
594 quit_functions = g_list_remove_link (quit_functions, quit_functions);
595 g_list_free_1 (tmp_list);
597 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
598 gtk_quit_invoke_function (quitf))
600 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
604 gtk_quit_destroy (quitf);
611 work = g_list_last (reinvoke_list);
613 quit_functions->prev = work;
614 work->next = quit_functions;
615 quit_functions = work;
621 main_loops = g_slist_remove (main_loops, loop);
623 g_main_destroy (loop);
625 gtk_main_loop_level--;
629 gtk_main_level (void)
631 return gtk_main_loop_level;
637 g_return_if_fail (main_loops != NULL);
639 g_main_quit (main_loops->data);
643 gtk_events_pending (void)
647 GDK_THREADS_LEAVE ();
648 result = g_main_pending();
649 GDK_THREADS_ENTER ();
655 gtk_main_iteration (void)
657 GDK_THREADS_LEAVE ();
658 g_main_iteration (TRUE);
659 GDK_THREADS_ENTER ();
662 return !g_main_is_running (main_loops->data);
668 gtk_main_iteration_do (gboolean blocking)
670 GDK_THREADS_LEAVE ();
671 g_main_iteration (blocking);
672 GDK_THREADS_ENTER ();
675 return !g_main_is_running (main_loops->data);
681 gtk_main_do_event (GdkEvent *event)
683 GtkWidget *event_widget;
684 GtkWidget *grab_widget;
685 GdkEvent *next_event;
688 /* If there are any events pending then get the next one.
690 next_event = gdk_event_peek ();
692 /* Try to compress enter/leave notify events. These event
693 * pairs occur when the mouse is dragged quickly across
694 * a window with many buttons (or through a menu). Instead
695 * of highlighting and de-highlighting each widget that
696 * is crossed it is better to simply de-highlight the widget
697 * which contained the mouse initially and highlight the
698 * widget which ends up containing the mouse.
701 if (((event->type == GDK_ENTER_NOTIFY) ||
702 (event->type == GDK_LEAVE_NOTIFY)) &&
703 ((next_event->type == GDK_ENTER_NOTIFY) ||
704 (next_event->type == GDK_LEAVE_NOTIFY)) &&
705 (next_event->type != event->type) &&
706 (next_event->any.window == event->any.window))
708 /* Throw both the peeked copy and the queued copy away
710 gdk_event_free (next_event);
711 next_event = gdk_event_get ();
712 gdk_event_free (next_event);
718 gdk_event_free (next_event);
720 /* Find the widget which got the event. We store the widget
721 * in the user_data field of GdkWindow's.
722 * Ignore the event if we don't have a widget for it, except
723 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
724 * Though this happens rarely, bogus events can occour
725 * for e.g. destroyed GdkWindows.
727 event_widget = gtk_get_event_widget (event);
730 /* To handle selection INCR transactions, we select
731 * PropertyNotify events on the requestor window and create
732 * a corresponding (fake) GdkWindow so that events get
733 * here. There won't be a widget though, so we have to handle
736 if (event->type == GDK_PROPERTY_NOTIFY)
737 gtk_selection_incr_event (event->any.window,
743 /* Push the event onto a stack of current events for
744 * gtk_current_event_get().
746 current_events = g_list_prepend (current_events, event);
748 /* If there is a grab in effect...
752 grab_widget = grabs->data;
754 /* If the grab widget is an ancestor of the event widget
755 * then we send the event to the original event widget.
756 * This is the key to implementing modality.
758 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
759 gtk_widget_is_ancestor (event_widget, grab_widget))
760 grab_widget = event_widget;
764 grab_widget = event_widget;
767 /* Not all events get sent to the grabbing widget.
768 * The delete, destroy, expose, focus change and resize
769 * events still get sent to the event widget because
770 * 1) these events have no meaning for the grabbing widget
771 * and 2) redirecting these events to the grabbing widget
772 * could cause the display to be messed up.
774 * Drag events are also not redirected, since it isn't
775 * clear what the semantics of that would be.
783 gtk_widget_ref (event_widget);
784 if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
785 !gtk_widget_event (event_widget, event))
786 gtk_widget_destroy (event_widget);
787 gtk_widget_unref (event_widget);
791 /* Unexpected GDK_DESTROY from the outside, ignore for
792 * child windows, handle like a GDK_DELETE for toplevels
794 if (!event_widget->parent)
796 gtk_widget_ref (event_widget);
797 if (!gtk_widget_event (event_widget, event) &&
798 !GTK_OBJECT_DESTROYED (event_widget))
799 gtk_widget_destroy (event_widget);
800 gtk_widget_unref (event_widget);
805 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
806 gdk_window_begin_paint_rect (event->any.window, &event->expose.area);
808 gtk_widget_event (event_widget, event);
810 if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
811 gdk_window_end_paint (event->any.window);
814 case GDK_PROPERTY_NOTIFY:
816 case GDK_FOCUS_CHANGE:
820 case GDK_SELECTION_CLEAR:
821 case GDK_SELECTION_REQUEST:
822 case GDK_SELECTION_NOTIFY:
823 case GDK_CLIENT_EVENT:
824 case GDK_VISIBILITY_NOTIFY:
825 gtk_widget_event (event_widget, event);
829 case GDK_BUTTON_PRESS:
830 case GDK_2BUTTON_PRESS:
831 case GDK_3BUTTON_PRESS:
832 gtk_propagate_event (grab_widget, event);
836 case GDK_KEY_RELEASE:
839 if (gtk_invoke_key_snoopers (grab_widget, event))
842 /* else fall through */
843 case GDK_MOTION_NOTIFY:
844 case GDK_BUTTON_RELEASE:
845 case GDK_PROXIMITY_IN:
846 case GDK_PROXIMITY_OUT:
847 gtk_propagate_event (grab_widget, event);
850 case GDK_ENTER_NOTIFY:
851 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
853 gtk_widget_event (grab_widget, event);
854 if (event_widget == grab_widget)
855 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
859 case GDK_LEAVE_NOTIFY:
860 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
862 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
863 gtk_widget_event (event_widget, event);
865 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
866 gtk_widget_event (grab_widget, event);
869 case GDK_DRAG_STATUS:
870 case GDK_DROP_FINISHED:
871 gtk_drag_source_handle_event (event_widget, event);
875 case GDK_DRAG_MOTION:
877 gtk_drag_dest_handle_event (event_widget, event);
881 tmp_list = current_events;
882 current_events = g_list_remove_link (current_events, tmp_list);
883 g_list_free_1 (tmp_list);
899 gtk_grab_add (GtkWidget *widget)
901 g_return_if_fail (widget != NULL);
903 if (!GTK_WIDGET_HAS_GRAB (widget))
905 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
907 grabs = g_slist_prepend (grabs, widget);
908 gtk_widget_ref (widget);
913 gtk_grab_get_current (void)
916 return GTK_WIDGET (grabs->data);
921 gtk_grab_remove (GtkWidget *widget)
923 g_return_if_fail (widget != NULL);
925 if (GTK_WIDGET_HAS_GRAB (widget))
927 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
929 grabs = g_slist_remove (grabs, widget);
930 gtk_widget_unref (widget);
935 gtk_init_add (GtkFunction function,
938 GtkInitFunction *init;
940 init = g_new (GtkInitFunction, 1);
941 init->function = function;
944 init_functions = g_list_prepend (init_functions, init);
948 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
951 GtkKeySnooperData *data;
952 static guint snooper_id = 1;
954 g_return_val_if_fail (snooper != NULL, 0);
956 data = g_new (GtkKeySnooperData, 1);
957 data->func = snooper;
958 data->func_data = func_data;
959 data->id = snooper_id++;
960 key_snoopers = g_slist_prepend (key_snoopers, data);
966 gtk_key_snooper_remove (guint snooper_id)
968 GtkKeySnooperData *data = NULL;
971 slist = key_snoopers;
975 if (data->id == snooper_id)
982 key_snoopers = g_slist_remove (key_snoopers, data);
986 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
990 gint return_val = FALSE;
992 slist = key_snoopers;
993 while (slist && !return_val)
995 GtkKeySnooperData *data;
999 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1006 gtk_quit_add_full (guint main_level,
1007 GtkFunction function,
1008 GtkCallbackMarshal marshal,
1010 GtkDestroyNotify destroy)
1012 static guint quit_id = 1;
1013 GtkQuitFunction *quitf;
1015 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1017 if (!quit_mem_chunk)
1018 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1019 512, G_ALLOC_AND_FREE);
1021 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1023 quitf->id = quit_id++;
1024 quitf->main_level = main_level;
1025 quitf->function = function;
1026 quitf->marshal = marshal;
1028 quitf->destroy = destroy;
1030 quit_functions = g_list_prepend (quit_functions, quitf);
1036 gtk_quit_destroy (GtkQuitFunction *quitf)
1039 quitf->destroy (quitf->data);
1040 g_mem_chunk_free (quit_mem_chunk, quitf);
1044 gtk_quit_destructor (GtkObject **object_p)
1047 gtk_object_destroy (*object_p);
1054 gtk_quit_add_destroy (guint main_level,
1057 GtkObject **object_p;
1059 g_return_if_fail (main_level > 0);
1060 g_return_if_fail (object != NULL);
1061 g_return_if_fail (GTK_IS_OBJECT (object));
1063 object_p = g_new (GtkObject*, 1);
1065 gtk_signal_connect (object,
1067 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1069 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1073 gtk_quit_add (guint main_level,
1074 GtkFunction function,
1077 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1081 gtk_quit_remove (guint id)
1083 GtkQuitFunction *quitf;
1086 tmp_list = quit_functions;
1089 quitf = tmp_list->data;
1091 if (quitf->id == id)
1093 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1094 g_list_free (tmp_list);
1095 gtk_quit_destroy (quitf);
1100 tmp_list = tmp_list->next;
1105 gtk_quit_remove_by_data (gpointer data)
1107 GtkQuitFunction *quitf;
1110 tmp_list = quit_functions;
1113 quitf = tmp_list->data;
1115 if (quitf->data == data)
1117 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1118 g_list_free (tmp_list);
1119 gtk_quit_destroy (quitf);
1124 tmp_list = tmp_list->next;
1129 gtk_timeout_add_full (guint32 interval,
1130 GtkFunction function,
1131 GtkCallbackMarshal marshal,
1133 GtkDestroyNotify destroy)
1137 GtkClosure *closure;
1139 closure = g_new (GtkClosure, 1);
1140 closure->marshal = marshal;
1141 closure->data = data;
1142 closure->destroy = destroy;
1144 return g_timeout_add_full (0, interval,
1145 gtk_invoke_idle_timeout,
1147 gtk_destroy_closure);
1150 return g_timeout_add_full (0, interval, function, data, destroy);
1154 gtk_timeout_add (guint32 interval,
1155 GtkFunction function,
1158 return g_timeout_add_full (0, interval, function, data, NULL);
1162 gtk_timeout_remove (guint tag)
1164 g_source_remove (tag);
1168 gtk_idle_add_full (gint priority,
1169 GtkFunction function,
1170 GtkCallbackMarshal marshal,
1172 GtkDestroyNotify destroy)
1176 GtkClosure *closure;
1178 closure = g_new (GtkClosure, 1);
1179 closure->marshal = marshal;
1180 closure->data = data;
1181 closure->destroy = destroy;
1183 return g_idle_add_full (priority,
1184 gtk_invoke_idle_timeout,
1186 gtk_destroy_closure);
1189 return g_idle_add_full (priority, function, data, destroy);
1193 gtk_idle_add (GtkFunction function,
1196 return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1200 gtk_idle_add_priority (gint priority,
1201 GtkFunction function,
1204 return g_idle_add_full (priority, function, data, NULL);
1208 gtk_idle_remove (guint tag)
1210 g_source_remove (tag);
1214 gtk_idle_remove_by_data (gpointer data)
1216 if (!g_idle_remove_by_data (data))
1217 g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1221 gtk_input_add_full (gint source,
1222 GdkInputCondition condition,
1223 GdkInputFunction function,
1224 GtkCallbackMarshal marshal,
1226 GtkDestroyNotify destroy)
1230 GtkClosure *closure;
1232 closure = g_new (GtkClosure, 1);
1233 closure->marshal = marshal;
1234 closure->data = data;
1235 closure->destroy = destroy;
1237 return gdk_input_add_full (source,
1239 (GdkInputFunction) gtk_invoke_input,
1241 (GdkDestroyNotify) gtk_destroy_closure);
1244 return gdk_input_add_full (source, condition, function, data, destroy);
1248 gtk_input_remove (guint tag)
1250 g_source_remove (tag);
1254 gtk_destroy_closure (gpointer data)
1256 GtkClosure *closure = data;
1258 if (closure->destroy)
1259 (closure->destroy) (closure->data);
1264 gtk_invoke_idle_timeout (gpointer data)
1266 GtkClosure *closure = data;
1269 gint ret_val = FALSE;
1270 args[0].name = NULL;
1271 args[0].type = GTK_TYPE_BOOL;
1272 args[0].d.pointer_data = &ret_val;
1273 closure->marshal (NULL, closure->data, 0, args);
1278 gtk_invoke_input (gpointer data,
1280 GdkInputCondition condition)
1282 GtkClosure *closure = data;
1285 args[0].type = GTK_TYPE_INT;
1286 args[0].name = NULL;
1287 GTK_VALUE_INT(args[0]) = source;
1288 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1289 args[1].name = NULL;
1290 GTK_VALUE_FLAGS(args[1]) = condition;
1291 args[2].type = GTK_TYPE_NONE;
1292 args[2].name = NULL;
1294 closure->marshal (NULL, closure->data, 2, args);
1298 gtk_get_current_event (void)
1301 return gdk_event_copy (current_events->data);
1307 * gtk_get_current_event_time:
1309 * If there is a current event and it has a timestamp, return that
1310 * timestamp, otherwise return %GDK_CURRENT_TIME.
1312 * Return value: the timestamp from the current event, or %GDK_CURRENT_TIME.
1315 gtk_get_current_event_time (void)
1318 return gdk_event_get_time (current_events->data);
1320 return GDK_CURRENT_TIME;
1324 gtk_get_event_widget (GdkEvent *event)
1329 if (event && event->any.window)
1330 gdk_window_get_user_data (event->any.window, (void**) &widget);
1336 gtk_exit_func (void)
1338 if (gtk_initialized)
1340 gtk_initialized = FALSE;
1346 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1348 if (!quitf->marshal)
1349 return quitf->function (quitf->data);
1353 gint ret_val = FALSE;
1355 args[0].name = NULL;
1356 args[0].type = GTK_TYPE_BOOL;
1357 args[0].d.pointer_data = &ret_val;
1358 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1366 gtk_propagate_event (GtkWidget *widget,
1371 g_return_if_fail (widget != NULL);
1372 g_return_if_fail (GTK_IS_WIDGET (widget));
1373 g_return_if_fail (event != NULL);
1375 handled_event = FALSE;
1377 if ((event->type == GDK_KEY_PRESS) ||
1378 (event->type == GDK_KEY_RELEASE))
1380 /* Only send key events within Window widgets to the Window
1381 * The Window widget will in turn pass the
1382 * key event on to the currently focused widget
1387 window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1390 if (GTK_WIDGET_IS_SENSITIVE (window))
1391 gtk_widget_event (window, event);
1393 handled_event = TRUE; /* don't send to widget */
1397 /* Other events get propagated up the widget tree
1398 * so that parents can see the button and motion
1399 * events of the children.
1401 while (!handled_event && widget)
1405 gtk_widget_ref (widget);
1406 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1407 tmp = widget->parent;
1408 gtk_widget_unref (widget);
1415 gtk_error (gchar *str)
1421 gtk_warning (gchar *str)
1427 gtk_message (gchar *str)
1433 gtk_print (gchar *str)
1435 static GtkWidget *window = NULL;
1436 static GtkWidget *text;
1437 static int level = 0;
1441 GtkWidget *hscrollbar;
1442 GtkWidget *vscrollbar;
1443 GtkWidget *separator;
1448 fputs (str, stdout);
1455 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1457 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1458 (GtkSignalFunc) gtk_widget_destroyed,
1461 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1463 box1 = gtk_vbox_new (FALSE, 0);
1464 gtk_container_add (GTK_CONTAINER (window), box1);
1465 gtk_widget_show (box1);
1468 box2 = gtk_vbox_new (FALSE, 10);
1469 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1470 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1471 gtk_widget_show (box2);
1474 table = gtk_table_new (2, 2, FALSE);
1475 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1476 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1477 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1478 gtk_widget_show (table);
1480 text = gtk_text_new (NULL, NULL);
1481 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1482 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1483 gtk_widget_show (text);
1484 gtk_widget_realize (text);
1486 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1487 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1488 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1489 gtk_widget_show (hscrollbar);
1491 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1492 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1493 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1494 gtk_widget_show (vscrollbar);
1496 separator = gtk_hseparator_new ();
1497 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1498 gtk_widget_show (separator);
1501 box2 = gtk_vbox_new (FALSE, 10);
1502 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1503 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1504 gtk_widget_show (box2);
1507 button = gtk_button_new_with_label ("close");
1508 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1509 (GtkSignalFunc) gtk_widget_hide,
1510 GTK_OBJECT (window));
1511 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1512 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1513 gtk_widget_grab_default (button);
1514 gtk_widget_show (button);
1518 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1521 if (!GTK_WIDGET_VISIBLE (window))
1522 gtk_widget_show (window);