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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
19 #include <X11/Xlocale.h> /* so we get the right setlocale */
24 #include "gtkbutton.h"
26 #include "gtkfeatures.h"
27 #include "gtkhscrollbar.h"
28 #include "gtkhseparator.h"
30 #include "gtkpreview.h"
32 #include "gtkselection.h"
33 #include "gtksignal.h"
37 #include "gtkvscrollbar.h"
38 #include "gtkwidget.h"
39 #include "gtkwindow.h"
40 #include "gtkprivate.h"
41 #include "gdk/gdki18n.h"
46 /* Private type definitions
48 typedef struct _GtkInitFunction GtkInitFunction;
49 typedef struct _GtkQuitFunction GtkQuitFunction;
50 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
51 typedef struct _GtkIdleHook GtkIdleHook;
52 typedef struct _GtkInputFunction GtkInputFunction;
53 typedef struct _GtkKeySnooperData GtkKeySnooperData;
55 struct _GtkInitFunction
61 struct _GtkQuitFunction
65 GtkCallbackMarshal marshal;
68 GtkDestroyNotify destroy;
71 struct _GtkTimeoutFunction
78 GtkCallbackMarshal marshal;
80 GtkDestroyNotify destroy;
85 GTK_HOOK_MARSHAL = 1 << (G_HOOK_FLAG_USER_SHIFT)
93 #define GTK_IDLE_HOOK(hook) ((GtkIdleHook*) hook)
95 struct _GtkInputFunction
97 GtkCallbackMarshal callback;
99 GtkDestroyNotify destroy;
102 struct _GtkKeySnooperData
104 GtkKeySnoopFunc func;
109 static void gtk_exit_func (void);
110 static void gtk_invoke_hook (GHookList *hook_list,
112 static void gtk_handle_idles (void);
113 static gboolean gtk_finish_idles (void);
114 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
115 static void gtk_quit_destroy (GtkQuitFunction *quitf);
116 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
117 static void gtk_handle_current_timeouts (guint32 the_time);
118 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
120 static void gtk_handle_timeouts (void);
121 static void gtk_handle_timer (void);
122 static void gtk_propagate_event (GtkWidget *widget,
125 static void gtk_error (gchar *str);
126 static void gtk_warning (gchar *str);
127 static void gtk_message (gchar *str);
128 static void gtk_print (gchar *str);
131 static gint gtk_timeout_remove_from_list (GList **list,
135 static gint gtk_timeout_compare (gconstpointer a,
139 const guint gtk_major_version = GTK_MAJOR_VERSION;
140 const guint gtk_minor_version = GTK_MINOR_VERSION;
141 const guint gtk_micro_version = GTK_MICRO_VERSION;
142 const guint gtk_binary_age = GTK_BINARY_AGE;
143 const guint gtk_interface_age = GTK_INTERFACE_AGE;
145 static gboolean iteration_done = FALSE;
146 static guint gtk_main_loop_level = 0;
147 static gint gtk_initialized = FALSE;
148 static GdkEvent *next_event = NULL;
149 static GList *current_events = NULL;
151 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
152 * widget is the first one on the list.
154 static GList *init_functions = NULL; /* A list of init functions.
156 static GList *quit_functions = NULL; /* A list of quit functions.
160 /* When handling timeouts, the algorithm is to move all of the expired
161 * timeouts from timeout_functions to current_timeouts then process
162 * them as a batch. If one of the timeouts recursively calls the main
163 * loop, then the remainder of the timeouts in current_timeouts will
164 * be processed before anything else happens.
166 * Each timeout is procesed as follows:
168 * - The timeout is removed from current_timeouts
169 * - The timeout is pushed on the running_timeouts stack
170 * - The timeout is executed
171 * - The timeout stack is popped
172 * - If the timeout function wasn't removed from the stack while executing,
173 * and it returned TRUE, it is added back to timeout_functions, otherwise
174 * it is destroyed if necessary.
176 * gtk_timeout_remove() works by checking for the timeout in
177 * timeout_functions current_timeouts and running_timeouts. If it is
178 * found in one of the first two, it is removed and destroyed. If it
179 * is found in running_timeouts, it is destroyed and ->data is set to
180 * NULL for the stack entry.
182 * Idle functions work pretty much identically.
185 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
186 * when the length of the time interval
187 * remaining. Therefore, the first timeout
188 * function to expire is at the head of
189 * the list and the last to expire is at
190 * the tail of the list. */
191 /* Prioritized idle callbacks */
192 static GHookList idle_hooks = { 0 };
193 static GHook *last_idle = NULL;
195 /* The timeouts that are currently being processed
196 * by gtk_handle_current_timeouts
198 static GList *current_timeouts = NULL;
200 /* A stack of timeouts that is currently being executed
202 static GList *running_timeouts = NULL;
204 static GMemChunk *timeout_mem_chunk = NULL;
205 static GMemChunk *quit_mem_chunk = NULL;
207 static GSList *key_snoopers = NULL;
209 static GdkVisual *gtk_visual; /* The visual to be used in creating new
212 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
216 guint gtk_debug_flags = 0; /* Global GTK debug flag */
218 #ifdef G_ENABLE_DEBUG
219 static const GDebugKey gtk_debug_keys[] = {
220 {"objects", GTK_DEBUG_OBJECTS},
221 {"misc", GTK_DEBUG_MISC},
222 {"signals", GTK_DEBUG_SIGNALS},
223 {"dnd", GTK_DEBUG_DND}
226 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
228 #endif /* G_ENABLE_DEBUG */
231 gtk_check_version (guint required_major,
232 guint required_minor,
233 guint required_micro)
235 if (required_major > GTK_MAJOR_VERSION)
236 return "Gtk+ version to old (major mismatch)";
237 if (required_major < GTK_MAJOR_VERSION)
238 return "Gtk+ version to new (major mismatch)";
239 if (required_minor > GTK_MINOR_VERSION)
240 return "Gtk+ version to old (minor mismatch)";
241 if (required_minor < GTK_MINOR_VERSION)
242 return "Gtk+ version to new (minor mismatch)";
243 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
244 return "Gtk+ version to new (micro mismatch)";
245 if (required_micro > GTK_MICRO_VERSION)
246 return "Gtk+ version to old (micro mismatch)";
251 gint gtk_use_mb = -1;
257 GSList *gtk_modules = NULL;
259 gchar *current_locale;
260 gchar *env_string = NULL;
266 g_set_error_handler (gtk_error);
267 g_set_warning_handler (gtk_warning);
268 g_set_message_handler (gtk_message);
269 g_set_print_handler (gtk_print);
272 /* Initialize "gdk". We pass along the 'argc' and 'argv'
273 * parameters as they contain information that GDK uses
275 gdk_init (argc, argv);
277 #ifdef G_ENABLE_DEBUG
278 env_string = getenv ("GTK_DEBUG");
279 if (env_string != NULL)
281 gtk_debug_flags = g_parse_debug_string (env_string,
286 #endif /* G_ENABLE_DEBUG */
288 env_string = getenv ("GTK_MODULES");
291 gchar **modules, **as;
293 modules = g_strsplit (env_string, ":", -1);
294 for (as = modules; *as; as++)
297 gtk_modules = g_slist_prepend (gtk_modules, *as);
309 for (i = 1; i < *argc;)
311 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
312 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
314 gchar *module_name = (*argv)[i] + 12;
316 if (*module_name == '=')
322 module_name = (*argv)[i];
326 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
328 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
330 GLogLevelFlags fatal_mask;
332 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
333 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
334 g_log_set_always_fatal (fatal_mask);
337 #ifdef G_ENABLE_DEBUG
338 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
339 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
341 gchar *equal_pos = strchr ((*argv)[i], '=');
343 if (equal_pos != NULL)
345 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
349 else if ((i + 1) < *argc && (*argv)[i + 1])
351 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
359 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
360 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
362 gchar *equal_pos = strchr ((*argv)[i], '=');
364 if (equal_pos != NULL)
366 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
370 else if ((i + 1) < *argc && (*argv)[i + 1])
372 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
380 #endif /* G_ENABLE_DEBUG */
384 for (i = 1; i < *argc; i++)
386 for (k = i; k < *argc; k++)
387 if ((*argv)[k] != NULL)
393 for (j = i + k; j < *argc; j++)
394 (*argv)[j-k] = (*argv)[j];
400 /* Check if there is a good chance the mb functions will handle things
401 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
402 * C locale, or we're using X's mb functions. (-DX_LOCALE && locale != C)
405 current_locale = setlocale (LC_CTYPE, NULL);
408 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
411 #endif /* X_LOCALE */
413 /* Detect GNU libc, where mb == UTF8. Not useful unless it's
414 * really a UTF8 locale. The below still probably will
415 * screw up on Greek, Cyrillic, etc, encoded as UTF8.
421 if ((MB_CUR_MAX == 2) &&
422 (mbstowcs (&result, "\xdd\xa5", 1) > 0) &&
425 if ((strlen (current_locale) < 4) ||
426 g_strcasecmp (current_locale + strlen(current_locale) - 4, "utf8"))
432 g_message ("%s multi-byte string functions.",
433 gtk_use_mb ? "Using" : "Not using"));
436 /* load gtk modules */
437 gtk_modules = g_slist_reverse (gtk_modules);
438 for (slist = gtk_modules; slist; slist = slist->next)
441 GModule *module = NULL;
442 GtkModuleInitFunc modinit_func = NULL;
444 module_name = slist->data;
446 if (!(module_name[0] == '/' ||
447 (module_name[0] == 'l' &&
448 module_name[1] == 'i' &&
449 module_name[2] == 'b')))
451 gchar *old = module_name;
453 module_name = g_strconcat ("lib", module_name, ".so", NULL);
456 if (g_module_supported ())
458 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
460 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
463 if (!g_slist_find (gtk_modules, modinit_func))
465 g_module_make_resident (module);
466 slist->data = modinit_func;
470 g_module_close (module);
477 g_warning ("Failed to load module \"%s\": %s",
478 module ? g_module_name (module) : module_name,
481 g_module_close (module);
483 g_free (module_name);
486 /* Initialize the default visual and colormap to be
487 * used in creating widgets. (We want to use the system
488 * defaults so as to be nice to the colormap).
490 gtk_visual = gdk_visual_get_system ();
491 gtk_colormap = gdk_colormap_get_system ();
498 /* Register an exit function to make sure we are able to cleanup.
500 g_atexit (gtk_exit_func);
502 /* Set the 'initialized' flag.
504 gtk_initialized = TRUE;
506 /* initialize gtk modules
508 for (slist = gtk_modules; slist; slist = slist->next)
512 GtkModuleInitFunc modinit;
514 modinit = slist->data;
515 modinit (argc, argv);
518 g_slist_free (gtk_modules);
522 gtk_exit (int errorcode)
524 /* Only if "gtk" has been initialized should we de-initialize.
526 /* de-initialisation is done by the gtk_exit_funct(),
527 * no need to do this here (Alex J.)
533 gtk_set_locale (void)
535 return gdk_set_locale ();
543 GtkInitFunction *init;
546 gtk_main_loop_level++;
548 tmp_list = functions = init_functions;
549 init_functions = NULL;
553 init = tmp_list->data;
554 tmp_list = tmp_list->next;
556 (* init->function) (init->data);
559 g_list_free (functions);
561 old_done = iteration_done;
562 while (!gtk_main_iteration ())
564 iteration_done = old_done;
568 GList *reinvoke_list = NULL;
569 GtkQuitFunction *quitf;
571 while (quit_functions)
573 quitf = quit_functions->data;
575 quit_functions = g_list_remove_link (quit_functions, quit_functions);
577 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
578 gtk_quit_invoke_function (quitf))
580 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
584 g_list_free (tmp_list);
585 gtk_quit_destroy (quitf);
592 work = g_list_last (reinvoke_list);
594 quit_functions->prev = work;
595 work->next = quit_functions;
596 quit_functions = work;
600 gtk_main_loop_level--;
604 gtk_main_level (void)
606 return gtk_main_loop_level;
612 iteration_done = TRUE;
616 gtk_events_pending (void)
620 /* if this function is called from a timeout which will only return
621 * if gtk needs processor time, we need to take iteration_done==TRUE
622 * into account as well.
624 result = iteration_done;
625 result += next_event != NULL;
626 result += gdk_events_pending();
628 result += last_idle != NULL;
629 result += current_timeouts != NULL;
635 hook = g_hook_first_valid (&idle_hooks, FALSE);
637 result += hook && GTK_IDLE_HOOK (hook)->priority <= GTK_PRIORITY_INTERNAL;
640 if (!result && timeout_functions)
643 GtkTimeoutFunction *timeoutf;
645 the_time = gdk_time_get ();
647 timeoutf = timeout_functions->data;
649 result += timeoutf->interval <= (the_time - timeoutf->start);
656 gtk_main_iteration (void)
658 return gtk_main_iteration_do (TRUE);
662 gtk_main_iteration_do (gboolean blocking)
664 GtkWidget *event_widget;
665 GtkWidget *grab_widget;
666 GdkEvent *event = NULL;
669 iteration_done = FALSE;
671 /* If this is a recursive call, and there are pending timeouts or
672 * idles, finish them, then return immediately to avoid blocking
675 if (current_timeouts)
677 gtk_handle_current_timeouts( gdk_time_get());
682 return iteration_done;
684 if (last_idle && gtk_finish_idles ())
689 return iteration_done;
692 /* If there is a valid event in 'next_event' then move it to 'event'
700 /* If we don't have an event then get one.
704 /* Handle setting of the "gdk" timer. If there are no
705 * timeout functions, then the timer is turned off.
706 * If there are timeout functions, then the timer is
707 * set to the shortest timeout interval (which is
708 * the first timeout function).
713 event = gdk_event_get ();
716 /* "gdk_event_get" can return FALSE if the timer goes off
717 * and no events are pending. Therefore, we should make
718 * sure that we got an event before continuing.
722 /* If there are any events pending then get the next one.
724 if (gdk_events_pending () > 0)
725 next_event = gdk_event_get ();
727 /* Try to compress enter/leave notify events. These event
728 * pairs occur when the mouse is dragged quickly across
729 * a window with many buttons (or through a menu). Instead
730 * of highlighting and de-highlighting each widget that
731 * is crossed it is better to simply de-highlight the widget
732 * which contained the mouse initially and highlight the
733 * widget which ends up containing the mouse.
736 if (((event->type == GDK_ENTER_NOTIFY) ||
737 (event->type == GDK_LEAVE_NOTIFY)) &&
738 ((next_event->type == GDK_ENTER_NOTIFY) ||
739 (next_event->type == GDK_LEAVE_NOTIFY)) &&
740 (next_event->type != event->type) &&
741 (next_event->any.window == event->any.window))
743 gdk_event_free (event);
744 gdk_event_free (next_event);
747 goto event_handling_done;
750 /* Find the widget which got the event. We store the widget
751 * in the user_data field of GdkWindow's.
752 * Ignore the event if we don't have a widget for it, except
753 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
754 * Though this happens rarely, bogus events can occour
755 * for e.g. destroyed GdkWindows.
757 event_widget = gtk_get_event_widget (event);
760 /* To handle selection INCR transactions, we select
761 * PropertyNotify events on the requestor window and create
762 * a corresponding (fake) GdkWindow so that events get
763 * here. There won't be a widget though, so we have to handle
766 if (event->type == GDK_PROPERTY_NOTIFY)
767 gtk_selection_incr_event (event->any.window,
770 gdk_event_free (event);
772 goto event_handling_done;
775 /* Push the event onto a stack of current events for
776 * gtk_current_event_get().
778 current_events = g_list_prepend (current_events, event);
780 /* If there is a grab in effect...
784 grab_widget = grabs->data;
786 /* If the grab widget is an ancestor of the event widget
787 * then we send the event to the original event widget.
788 * This is the key to implementing modality.
790 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
791 gtk_widget_is_ancestor (event_widget, grab_widget))
792 grab_widget = event_widget;
796 grab_widget = event_widget;
799 /* Not all events get sent to the grabbing widget.
800 * The delete, destroy, expose, focus change and resize
801 * events still get sent to the event widget because
802 * 1) these events have no meaning for the grabbing widget
803 * and 2) redirecting these events to the grabbing widget
804 * could cause the display to be messed up.
806 * Drag events are also not redirected, since it isn't
807 * clear what the semantics of that would be.
815 gtk_widget_ref (event_widget);
816 if (!gtk_widget_event (event_widget, event) &&
817 !GTK_OBJECT_DESTROYED (event_widget))
818 gtk_widget_destroy (event_widget);
819 gtk_widget_unref (event_widget);
823 gtk_widget_ref (event_widget);
824 gtk_widget_event (event_widget, event);
825 if (!GTK_OBJECT_DESTROYED (event_widget))
826 gtk_widget_destroy (event_widget);
827 gtk_widget_unref (event_widget);
830 case GDK_PROPERTY_NOTIFY:
833 case GDK_FOCUS_CHANGE:
837 case GDK_SELECTION_CLEAR:
838 case GDK_SELECTION_REQUEST:
839 case GDK_SELECTION_NOTIFY:
840 case GDK_CLIENT_EVENT:
841 case GDK_VISIBILITY_NOTIFY:
842 gtk_widget_event (event_widget, event);
846 case GDK_KEY_RELEASE:
849 if (gtk_invoke_key_snoopers (grab_widget, event))
852 /* else fall through */
853 case GDK_MOTION_NOTIFY:
854 case GDK_BUTTON_PRESS:
855 case GDK_2BUTTON_PRESS:
856 case GDK_3BUTTON_PRESS:
857 case GDK_BUTTON_RELEASE:
858 case GDK_PROXIMITY_IN:
859 case GDK_PROXIMITY_OUT:
860 gtk_propagate_event (grab_widget, event);
863 case GDK_ENTER_NOTIFY:
864 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
866 gtk_widget_event (grab_widget, event);
867 if (event_widget == grab_widget)
868 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
872 case GDK_LEAVE_NOTIFY:
873 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
875 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
876 gtk_widget_event (event_widget, event);
878 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
879 gtk_widget_event (grab_widget, event);
882 case GDK_DRAG_STATUS:
883 case GDK_DROP_FINISHED:
884 gtk_drag_source_handle_event (event_widget, event);
888 case GDK_DRAG_MOTION:
890 gtk_drag_dest_handle_event (event_widget, event);
894 tmp_list = current_events;
895 current_events = g_list_remove_link (current_events, tmp_list);
896 g_list_free_1 (tmp_list);
898 gdk_event_free (event);
902 if (gdk_events_pending() == 0)
908 /* Handle timeout functions that may have expired.
910 gtk_handle_timeouts ();
915 return iteration_done;
931 gtk_grab_add (GtkWidget *widget)
933 g_return_if_fail (widget != NULL);
935 if (!GTK_WIDGET_HAS_GRAB (widget))
937 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
939 grabs = g_slist_prepend (grabs, widget);
940 gtk_widget_ref (widget);
945 gtk_grab_get_current (void)
948 return GTK_WIDGET (grabs->data);
953 gtk_grab_remove (GtkWidget *widget)
955 g_return_if_fail (widget != NULL);
957 if (GTK_WIDGET_HAS_GRAB (widget))
959 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
961 grabs = g_slist_remove (grabs, widget);
962 gtk_widget_unref (widget);
967 gtk_init_add (GtkFunction function,
970 GtkInitFunction *init;
972 init = g_new (GtkInitFunction, 1);
973 init->function = function;
976 init_functions = g_list_prepend (init_functions, init);
980 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
983 GtkKeySnooperData *data;
984 static guint snooper_id = 1;
986 g_return_val_if_fail (snooper != NULL, 0);
988 data = g_new (GtkKeySnooperData, 1);
989 data->func = snooper;
990 data->func_data = func_data;
991 data->id = snooper_id++;
992 key_snoopers = g_slist_prepend (key_snoopers, data);
998 gtk_key_snooper_remove (guint snooper_id)
1000 GtkKeySnooperData *data = NULL;
1003 slist = key_snoopers;
1007 if (data->id == snooper_id)
1010 slist = slist->next;
1014 key_snoopers = g_slist_remove (key_snoopers, data);
1018 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
1022 gint return_val = FALSE;
1024 slist = key_snoopers;
1025 while (slist && !return_val)
1027 GtkKeySnooperData *data;
1030 slist = slist->next;
1031 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
1038 gtk_timeout_add_full (guint32 interval,
1039 GtkFunction function,
1040 GtkCallbackMarshal marshal,
1042 GtkDestroyNotify destroy)
1044 static guint timeout_tag = 1;
1045 GtkTimeoutFunction *timeoutf;
1047 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1049 /* Create a new timeout function structure.
1050 * The start time is the current time.
1052 if (!timeout_mem_chunk)
1053 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
1054 1024, G_ALLOC_AND_FREE);
1056 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
1058 timeoutf->tag = timeout_tag++;
1059 timeoutf->start = gdk_time_get ();
1060 timeoutf->interval = interval;
1061 timeoutf->originterval = interval;
1062 timeoutf->function = function;
1063 timeoutf->marshal = marshal;
1064 timeoutf->data = data;
1065 timeoutf->destroy = destroy;
1067 gtk_timeout_insert (timeoutf);
1069 return timeoutf->tag;
1073 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
1075 if (timeoutf->destroy)
1076 (timeoutf->destroy) (timeoutf->data);
1077 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
1081 gtk_timeout_add (guint32 interval,
1082 GtkFunction function,
1085 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
1089 gtk_timeout_add_interp (guint32 interval,
1090 GtkCallbackMarshal function,
1092 GtkDestroyNotify destroy)
1094 g_message ("gtk_timeout_add_interp() is deprecated");
1095 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
1098 /* Search for the specified tag in a list of timeouts. If it
1099 * is found, destroy the timeout, and either remove the link
1100 * or set link->data to NULL depending on remove_link
1103 gtk_timeout_remove_from_list (GList **list, guint tag, gint remove_link)
1106 GtkTimeoutFunction *timeoutf;
1111 timeoutf = tmp_list->data;
1113 if (timeoutf->tag == tag)
1117 *list = g_list_remove_link (*list, tmp_list);
1118 g_list_free (tmp_list);
1121 tmp_list->data = NULL;
1123 gtk_timeout_destroy (timeoutf);
1128 tmp_list = tmp_list->next;
1134 gtk_timeout_remove (guint tag)
1137 /* Remove a timeout function.
1138 * (Which, basically, involves searching the
1139 * list for the tag).
1142 if (gtk_timeout_remove_from_list (&timeout_functions, tag, TRUE))
1144 if (gtk_timeout_remove_from_list (¤t_timeouts, tag, TRUE))
1146 if (gtk_timeout_remove_from_list (&running_timeouts, tag, FALSE))
1151 gtk_quit_add_full (guint main_level,
1152 GtkFunction function,
1153 GtkCallbackMarshal marshal,
1155 GtkDestroyNotify destroy)
1157 static guint quit_id = 1;
1158 GtkQuitFunction *quitf;
1160 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1162 if (!quit_mem_chunk)
1163 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1164 512, G_ALLOC_AND_FREE);
1166 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1168 quitf->id = quit_id++;
1169 quitf->main_level = main_level;
1170 quitf->function = function;
1171 quitf->marshal = marshal;
1173 quitf->destroy = destroy;
1175 quit_functions = g_list_prepend (quit_functions, quitf);
1181 gtk_idle_compare (GHook *new_g_hook,
1184 GtkIdleHook *new_hook = GTK_IDLE_HOOK (new_g_hook);
1185 GtkIdleHook *sibling = GTK_IDLE_HOOK (g_sibling);
1187 /* We add an extra +1 to the comparision result to make sure
1188 * that we get inserted at the end of the list of hooks with
1189 * the same priority.
1191 return new_hook->priority - sibling->priority + 1;
1195 gtk_idle_add_full (gint priority,
1196 GtkFunction function,
1197 GtkCallbackMarshal marshal,
1199 GtkDestroyNotify destroy)
1205 g_return_val_if_fail (marshal == NULL, 0);
1207 g_return_val_if_fail (marshal != NULL, 0);
1209 if (!idle_hooks.seq_id)
1210 g_hook_list_init (&idle_hooks, sizeof (GtkIdleHook));
1212 hook = g_hook_alloc (&idle_hooks);
1213 ihook = GTK_IDLE_HOOK (hook);
1217 hook->flags |= GTK_HOOK_MARSHAL;
1218 hook->func = marshal;
1221 hook->func = function;
1222 hook->destroy = destroy;
1223 ihook->priority = priority;
1225 /* If we are adding the first idle function, possibly wake up
1226 * the main thread out of its select().
1228 if (!g_hook_first_valid (&idle_hooks, TRUE))
1229 gdk_threads_wake ();
1231 g_hook_insert_sorted (&idle_hooks, hook, gtk_idle_compare);
1233 return hook->hook_id;
1237 gtk_idle_add (GtkFunction function,
1240 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1244 gtk_idle_add_priority (gint priority,
1245 GtkFunction function,
1248 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1252 gtk_idle_add_interp (GtkCallbackMarshal marshal,
1254 GtkDestroyNotify destroy)
1256 g_message ("gtk_idle_add_interp() is deprecated");
1257 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
1261 gtk_idle_remove (guint tag)
1263 g_return_if_fail (tag > 0);
1265 if (!g_hook_destroy (&idle_hooks, tag))
1266 g_warning ("gtk_idle_remove(%d): no such idle function", tag);
1270 gtk_idle_remove_by_data (gpointer data)
1274 hook = g_hook_find_data (&idle_hooks, TRUE, data);
1276 g_hook_destroy_link (&idle_hooks, hook);
1278 g_warning ("gtk_idle_remove_by_data(%p): no such idle function", data);
1282 gtk_finish_idles (void)
1284 gboolean idles_called;
1286 idles_called = FALSE;
1291 hook = g_hook_next_valid (last_idle, FALSE);
1293 if (!hook || GTK_IDLE_HOOK (hook)->priority != GTK_IDLE_HOOK (last_idle)->priority)
1295 g_hook_unref (&idle_hooks, last_idle);
1300 g_hook_unref (&idle_hooks, last_idle);
1302 g_hook_ref (&idle_hooks, last_idle);
1304 idles_called = TRUE;
1305 gtk_invoke_hook (&idle_hooks, last_idle);
1309 return idles_called;
1313 gtk_handle_idles (void)
1317 /* Caller must already have called gtk_finish_idles() if necessary
1319 g_assert (last_idle == NULL);
1321 hook = g_hook_first_valid (&idle_hooks, FALSE);
1326 g_hook_ref (&idle_hooks, last_idle);
1328 gtk_invoke_hook (&idle_hooks, last_idle);
1330 gtk_finish_idles ();
1335 gtk_invoke_hook (GHookList *hook_list,
1338 gboolean keep_alive;
1340 if (hook->flags & GTK_HOOK_MARSHAL)
1343 register GtkCallbackMarshal marshal;
1346 args[0].name = NULL;
1347 args[0].type = GTK_TYPE_BOOL;
1348 GTK_VALUE_POINTER (args[0]) = &keep_alive;
1349 marshal = hook->func;
1350 marshal (NULL, hook->data, 0, args);
1354 register GtkFunction func;
1357 keep_alive = func (hook->data);
1360 g_hook_destroy_link (hook_list, hook);
1364 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1366 if (!timeoutf->marshal)
1367 return timeoutf->function (timeoutf->data);
1371 gint ret_val = FALSE;
1372 args[0].name = NULL;
1373 args[0].type = GTK_TYPE_BOOL;
1374 args[0].d.pointer_data = &ret_val;
1375 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1381 gtk_handle_current_timeouts (guint32 the_time)
1385 GtkTimeoutFunction *timeoutf;
1387 while (current_timeouts)
1389 tmp_list = current_timeouts;
1390 timeoutf = tmp_list->data;
1392 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1393 if (running_timeouts)
1395 running_timeouts->prev = tmp_list;
1396 tmp_list->next = running_timeouts;
1398 running_timeouts = tmp_list;
1400 result = gtk_invoke_timeout_function (timeoutf);
1402 running_timeouts = g_list_remove_link (running_timeouts, tmp_list);
1403 timeoutf = tmp_list->data;
1405 g_list_free_1 (tmp_list);
1411 gtk_timeout_destroy (timeoutf);
1415 timeoutf->interval = timeoutf->originterval;
1416 timeoutf->start = the_time;
1417 gtk_timeout_insert (timeoutf);
1424 gtk_handle_timeouts (void)
1429 GtkTimeoutFunction *timeoutf;
1431 /* Caller must already have called gtk_handle_current_timeouts if
1433 g_assert (current_timeouts == NULL);
1435 if (timeout_functions)
1437 the_time = gdk_time_get ();
1439 tmp_list = timeout_functions;
1442 timeoutf = tmp_list->data;
1444 if (timeoutf->interval <= (the_time - timeoutf->start))
1446 tmp_list2 = tmp_list;
1447 tmp_list = tmp_list->next;
1449 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1450 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1454 timeoutf->interval -= (the_time - timeoutf->start);
1455 timeoutf->start = the_time;
1456 tmp_list = tmp_list->next;
1460 if (current_timeouts)
1461 gtk_handle_current_timeouts (the_time);
1466 gtk_quit_destroy (GtkQuitFunction *quitf)
1469 quitf->destroy (quitf->data);
1470 g_mem_chunk_free (quit_mem_chunk, quitf);
1474 gtk_quit_destructor (GtkObject **object_p)
1477 gtk_object_destroy (*object_p);
1484 gtk_quit_add_destroy (guint main_level,
1487 GtkObject **object_p;
1489 g_return_if_fail (main_level > 0);
1490 g_return_if_fail (object != NULL);
1491 g_return_if_fail (GTK_IS_OBJECT (object));
1493 object_p = g_new (GtkObject*, 1);
1495 gtk_signal_connect (object,
1497 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1499 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1503 gtk_quit_add (guint main_level,
1504 GtkFunction function,
1507 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1511 gtk_quit_remove (guint id)
1513 GtkQuitFunction *quitf;
1516 tmp_list = quit_functions;
1519 quitf = tmp_list->data;
1521 if (quitf->id == id)
1523 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1524 g_list_free (tmp_list);
1525 gtk_quit_destroy (quitf);
1530 tmp_list = tmp_list->next;
1535 gtk_quit_remove_by_data (gpointer data)
1537 GtkQuitFunction *quitf;
1540 tmp_list = quit_functions;
1543 quitf = tmp_list->data;
1545 if (quitf->data == data)
1547 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1548 g_list_free (tmp_list);
1549 gtk_quit_destroy (quitf);
1554 tmp_list = tmp_list->next;
1559 gtk_invoke_input_function (GtkInputFunction *input,
1561 GdkInputCondition condition)
1564 args[0].type = GTK_TYPE_INT;
1565 args[0].name = NULL;
1566 GTK_VALUE_INT(args[0]) = source;
1567 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1568 args[1].name = NULL;
1569 GTK_VALUE_FLAGS(args[1]) = condition;
1570 args[2].type = GTK_TYPE_NONE;
1571 args[2].name = NULL;
1573 input->callback (NULL, input->data, 2, args);
1577 gtk_destroy_input_function (GtkInputFunction *input)
1580 (input->destroy) (input->data);
1585 gtk_input_add_full (gint source,
1586 GdkInputCondition condition,
1587 GdkInputFunction function,
1588 GtkCallbackMarshal marshal,
1590 GtkDestroyNotify destroy)
1594 GtkInputFunction *input;
1596 input = g_new (GtkInputFunction, 1);
1597 input->callback = marshal;
1599 input->destroy = destroy;
1601 return gdk_input_add_full (source,
1603 (GdkInputFunction) gtk_invoke_input_function,
1605 (GdkDestroyNotify) gtk_destroy_input_function);
1608 return gdk_input_add_full (source, condition, function, data, destroy);
1612 gtk_input_remove (guint tag)
1614 gdk_input_remove (tag);
1618 gtk_get_current_event (void)
1621 return gdk_event_copy ((GdkEvent *) current_events->data);
1627 gtk_get_event_widget (GdkEvent *event)
1632 if (event && event->any.window)
1633 gdk_window_get_user_data (event->any.window, (void**) &widget);
1639 gtk_exit_func (void)
1641 if (gtk_initialized)
1643 gtk_initialized = FALSE;
1644 gtk_preview_uninit ();
1649 /* We rely on some knowledge of how g_list_insert_sorted works to make
1650 * sure that we insert after timeouts of equal interval
1653 gtk_timeout_compare (gconstpointer a, gconstpointer b)
1655 return (((const GtkTimeoutFunction *)a)->interval <
1656 ((const GtkTimeoutFunction *)b)->interval)
1661 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1663 g_assert (timeoutf != NULL);
1665 /* Insert the timeout function appropriately.
1666 * Appropriately meaning sort it into the list
1667 * of timeout functions.
1669 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1670 gtk_timeout_compare);
1674 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1676 if (!quitf->marshal)
1677 return quitf->function (quitf->data);
1681 gint ret_val = FALSE;
1683 args[0].name = NULL;
1684 args[0].type = GTK_TYPE_BOOL;
1685 args[0].d.pointer_data = &ret_val;
1686 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1694 gtk_handle_timer (void)
1696 GtkTimeoutFunction *timeoutf;
1698 if (g_hook_first_valid (&idle_hooks, FALSE))
1701 gdk_timer_enable ();
1703 else if (timeout_functions)
1705 timeoutf = timeout_functions->data;
1706 gdk_timer_set (timeoutf->interval);
1707 gdk_timer_enable ();
1712 gdk_timer_disable ();
1717 gtk_propagate_event (GtkWidget *widget,
1722 g_return_if_fail (widget != NULL);
1723 g_return_if_fail (event != NULL);
1725 handled_event = FALSE;
1727 if ((event->type == GDK_KEY_PRESS) ||
1728 (event->type == GDK_KEY_RELEASE))
1730 /* Only send key events within Window widgets to the Window
1731 * The Window widget will in turn pass the
1732 * key event on to the currently focused widget
1737 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1740 if (GTK_WIDGET_IS_SENSITIVE (window))
1741 gtk_widget_event (window, event);
1743 handled_event = TRUE; /* don't send to widget */
1747 /* Other events get propagated up the widget tree
1748 * so that parents can see the button and motion
1749 * events of the children.
1751 while (!handled_event && widget)
1755 gtk_widget_ref (widget);
1756 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1757 tmp = widget->parent;
1758 gtk_widget_unref (widget);
1766 gtk_error (gchar *str)
1772 gtk_warning (gchar *str)
1778 gtk_message (gchar *str)
1784 gtk_print (gchar *str)
1786 static GtkWidget *window = NULL;
1787 static GtkWidget *text;
1788 static int level = 0;
1792 GtkWidget *hscrollbar;
1793 GtkWidget *vscrollbar;
1794 GtkWidget *separator;
1799 fputs (str, stdout);
1806 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1808 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1809 (GtkSignalFunc) gtk_widget_destroyed,
1812 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1814 box1 = gtk_vbox_new (FALSE, 0);
1815 gtk_container_add (GTK_CONTAINER (window), box1);
1816 gtk_widget_show (box1);
1819 box2 = gtk_vbox_new (FALSE, 10);
1820 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1821 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1822 gtk_widget_show (box2);
1825 table = gtk_table_new (2, 2, FALSE);
1826 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1827 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1828 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1829 gtk_widget_show (table);
1831 text = gtk_text_new (NULL, NULL);
1832 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1833 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1834 gtk_widget_show (text);
1835 gtk_widget_realize (text);
1837 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1838 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1839 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1840 gtk_widget_show (hscrollbar);
1842 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1843 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1844 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1845 gtk_widget_show (vscrollbar);
1847 separator = gtk_hseparator_new ();
1848 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1849 gtk_widget_show (separator);
1852 box2 = gtk_vbox_new (FALSE, 10);
1853 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1854 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1855 gtk_widget_show (box2);
1858 button = gtk_button_new_with_label ("close");
1859 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1860 (GtkSignalFunc) gtk_widget_hide,
1861 GTK_OBJECT (window));
1862 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1863 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1864 gtk_widget_grab_default (button);
1865 gtk_widget_show (button);
1869 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1872 if (!GTK_WIDGET_VISIBLE (window))
1873 gtk_widget_show (window);