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 */
23 #include "gtkbutton.h"
24 #include "gtkfeatures.h"
25 #include "gtkhscrollbar.h"
26 #include "gtkhseparator.h"
28 #include "gtkpreview.h"
30 #include "gtkselection.h"
31 #include "gtksignal.h"
35 #include "gtkvscrollbar.h"
36 #include "gtkwidget.h"
37 #include "gtkwindow.h"
38 #include "gtkprivate.h"
39 #include "gdk/gdki18n.h"
40 #include "../config.h"
44 /* Private type definitions
46 typedef struct _GtkInitFunction GtkInitFunction;
47 typedef struct _GtkQuitFunction GtkQuitFunction;
48 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
49 typedef struct _GtkIdleFunction GtkIdleFunction;
50 typedef struct _GtkInputFunction GtkInputFunction;
51 typedef struct _GtkKeySnooperData GtkKeySnooperData;
53 struct _GtkInitFunction
59 struct _GtkQuitFunction
63 GtkCallbackMarshal marshal;
66 GtkDestroyNotify destroy;
69 struct _GtkTimeoutFunction
76 GtkCallbackMarshal marshal;
78 GtkDestroyNotify destroy;
81 struct _GtkIdleFunction
85 GtkCallbackMarshal marshal;
88 GtkDestroyNotify destroy;
91 struct _GtkInputFunction
93 GtkCallbackMarshal callback;
95 GtkDestroyNotify destroy;
98 struct _GtkKeySnooperData
100 GtkKeySnoopFunc func;
105 static void gtk_exit_func (void);
106 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
107 static void gtk_quit_destroy (GtkQuitFunction *quitf);
108 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
109 static void gtk_handle_current_timeouts (guint32 the_time);
110 static void gtk_handle_current_idles (void);
111 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
113 static void gtk_handle_timeouts (void);
114 static void gtk_handle_idle (void);
115 static void gtk_handle_timer (void);
116 static void gtk_propagate_event (GtkWidget *widget,
118 static void gtk_error (gchar *str);
119 static void gtk_warning (gchar *str);
120 static void gtk_message (gchar *str);
121 static void gtk_print (gchar *str);
123 static gint gtk_idle_remove_from_list (GList **list,
127 static gint gtk_timeout_remove_from_list (GList **list,
131 static gint gtk_idle_compare (gconstpointer a,
134 static gint gtk_timeout_compare (gconstpointer a,
137 const guint gtk_major_version = GTK_MAJOR_VERSION;
138 const guint gtk_minor_version = GTK_MINOR_VERSION;
139 const guint gtk_micro_version = GTK_MICRO_VERSION;
140 const guint gtk_binary_age = GTK_BINARY_AGE;
141 const guint gtk_interface_age = GTK_INTERFACE_AGE;
143 static gboolean iteration_done = FALSE;
144 static guint main_level = 0;
145 static gint gtk_initialized = FALSE;
146 static GdkEvent *next_event = NULL;
147 static GList *current_events = NULL;
149 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
150 * widget is the first one on the list.
152 static GList *init_functions = NULL; /* A list of init functions.
154 static GList *quit_functions = NULL; /* A list of quit functions.
158 /* When handling timeouts, the algorithm is to move all of the expired
159 * timeouts from timeout_functions to current_timeouts then process
160 * them as a batch. If one of the timeouts recursively calls the main
161 * loop, then the remainder of the timeouts in current_timeouts will
162 * be processed before anything else happens.
164 * Each timeout is procesed as follows:
166 * - The timeout is removed from current_timeouts
167 * - The timeout is pushed on the running_timeouts stack
168 * - The timeout is executed
169 * - The timeout stack is popped
170 * - If the timeout function wasn't removed from the stack while executing,
171 * and it returned TRUE, it is added back to timeout_functions, otherwise
172 * it is destroyed if necessary.
174 * gtk_timeout_remove() works by checking for the timeout in
175 * timeout_functions current_timeouts and running_timeouts. If it is
176 * found in one of the first two, it is removed and destroyed. If it
177 * is found in running_timeouts, it is destroyed and ->data is set to
178 * NULL for the stack entry.
180 * Idle functions work pretty much identically.
183 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
184 * when the length of the time interval
185 * remaining. Therefore, the first timeout
186 * function to expire is at the head of
187 * the list and the last to expire is at
188 * the tail of the list. */
189 static GList *idle_functions = NULL; /* A list of idle functions.
192 /* The idle functions / timeouts that are currently being processed
193 * by gtk_handle_current_(timeouts/idles)
195 static GList *current_idles = NULL;
196 static GList *current_timeouts = NULL;
198 /* A stack of idle functions / timeouts that are currently being
201 static GList *running_idles = NULL;
202 static GList *running_timeouts = NULL;
204 static GMemChunk *timeout_mem_chunk = NULL;
205 static GMemChunk *idle_mem_chunk = NULL;
206 static GMemChunk *quit_mem_chunk = NULL;
208 static GSList *key_snoopers = NULL;
210 static GdkVisual *gtk_visual; /* The visual to be used in creating new
213 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
217 guint gtk_debug_flags = 0; /* Global GTK debug flag */
219 #ifdef G_ENABLE_DEBUG
220 static GDebugKey gtk_debug_keys[] = {
221 {"objects", GTK_DEBUG_OBJECTS},
222 {"misc", GTK_DEBUG_MISC},
223 {"signals", GTK_DEBUG_SIGNALS}
226 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
228 #endif /* G_ENABLE_DEBUG */
230 gint gtk_use_mb = -1;
236 gchar *current_locale;
243 g_set_error_handler (gtk_error);
244 g_set_warning_handler (gtk_warning);
245 g_set_message_handler (gtk_message);
246 g_set_print_handler (gtk_print);
249 /* Initialize "gdk". We pass along the 'argc' and 'argv'
250 * parameters as they contain information that GDK uses
252 gdk_init (argc, argv);
254 #ifdef G_ENABLE_DEBUG
256 gchar *debug_string = getenv("GTK_DEBUG");
257 if (debug_string != NULL)
258 gtk_debug_flags = g_parse_debug_string (debug_string,
267 for (i = 1; i < *argc;)
269 if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
270 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
272 gchar *equal_pos = strchr ((*argv)[i], '=');
274 if (equal_pos != NULL)
276 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
280 else if ((i + 1) < *argc && (*argv)[i + 1])
282 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
290 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
291 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
293 gchar *equal_pos = strchr ((*argv)[i], '=');
295 if (equal_pos != NULL)
297 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
301 else if ((i + 1) < *argc && (*argv)[i + 1])
303 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
311 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
313 g_set_warning_handler ((GWarningFunc)g_error);
319 for (i = 1; i < *argc; i++)
321 for (k = i; k < *argc; k++)
322 if ((*argv)[k] != NULL)
328 for (j = i + k; j < *argc; j++)
329 (*argv)[j-k] = (*argv)[j];
335 #endif /* G_ENABLE_DEBUG */
337 /* Check if there is a good chance the mb functions will handle things
338 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
339 * C locale, or we're using X's mb functions. (-DX_LOCALE && locale != C)
342 current_locale = g_strdup (setlocale (LC_CTYPE, NULL));
345 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
350 setlocale (LC_CTYPE, "C");
351 gtk_use_mb = (mblen ("\xc0", MB_CUR_MAX) == 1);
352 setlocale (LC_CTYPE, current_locale);
355 g_free (current_locale);
357 GTK_NOTE (MISC, g_print("%s multi-byte string functions.\n",
358 gtk_use_mb ? "Using" : "Not using"));
360 /* Initialize the default visual and colormap to be
361 * used in creating widgets. (We want to use the system
362 * defaults so as to be nice to the colormap).
364 gtk_visual = gdk_visual_get_system ();
365 gtk_colormap = gdk_colormap_get_system ();
372 /* Register an exit function to make sure we are able to cleanup.
374 if (ATEXIT (gtk_exit_func))
375 g_warning ("unable to register exit function");
377 /* Set the 'initialized' flag.
379 gtk_initialized = TRUE;
383 gtk_exit (int errorcode)
385 /* Only if "gtk" has been initialized should we de-initialize.
387 /* de-initialisation is done by the gtk_exit_funct(),
388 * no need to do this here (Alex J.)
394 gtk_set_locale (void)
396 return gdk_set_locale ();
404 GtkInitFunction *init;
409 tmp_list = functions = init_functions;
410 init_functions = NULL;
414 init = tmp_list->data;
415 tmp_list = tmp_list->next;
417 (* init->function) (init->data);
420 g_list_free (functions);
422 old_done = iteration_done;
423 while (!gtk_main_iteration ())
425 iteration_done = old_done;
429 GList *reinvoke_list = NULL;
430 GtkQuitFunction *quitf;
432 while (quit_functions)
434 quitf = quit_functions->data;
436 quit_functions = g_list_remove_link (quit_functions, quit_functions);
438 if ((quitf->main_level && quitf->main_level != main_level) ||
439 gtk_quit_invoke_function (quitf))
441 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
445 g_list_free (tmp_list);
446 gtk_quit_destroy (quitf);
453 tmp_list = g_list_last (reinvoke_list);
455 quit_functions->prev = tmp_list;
456 tmp_list->next = quit_functions;
457 quit_functions = tmp_list;
465 gtk_main_level (void)
473 iteration_done = TRUE;
477 gtk_events_pending (void)
481 /* if this function is called from a timeout which will only return
482 * if gtk needs processor time, we need to take iteration_done==TRUE
483 * into account as well.
485 result = iteration_done;
486 result += next_event != NULL;
487 result += gdk_events_pending();
489 result += current_idles != NULL;
490 result += current_timeouts != NULL;
494 result += (idle_functions &&
495 (((GtkIdleFunction *)idle_functions->data)->priority <=
496 GTK_PRIORITY_INTERNAL));
499 if (!result && timeout_functions)
502 GtkTimeoutFunction *timeoutf;
504 the_time = gdk_time_get ();
506 timeoutf = timeout_functions->data;
508 result += timeoutf->interval <= (the_time - timeoutf->start);
515 gtk_main_iteration (void)
517 return gtk_main_iteration_do (TRUE);
521 gtk_main_iteration_do (gboolean blocking)
523 GtkWidget *event_widget;
524 GtkWidget *grab_widget;
525 GdkEvent *event = NULL;
528 iteration_done = FALSE;
530 /* If this is a recursive call, and there are pending timeouts or
531 * idles, finish them, then return immediately to avoid blocking
534 if (current_timeouts)
536 gtk_handle_current_timeouts( gdk_time_get());
541 return iteration_done;
545 gtk_handle_current_idles ();
550 return iteration_done;
553 /* If there is a valid event in 'next_event' then move it to 'event'
561 /* If we don't have an event then get one.
565 /* Handle setting of the "gdk" timer. If there are no
566 * timeout functions, then the timer is turned off.
567 * If there are timeout functions, then the timer is
568 * set to the shortest timeout interval (which is
569 * the first timeout function).
573 if (blocking) event = gdk_event_get ();
576 /* "gdk_event_get" can return FALSE if the timer goes off
577 * and no events are pending. Therefore, we should make
578 * sure that we got an event before continuing.
582 /* If there are any events pending then get the next one.
584 if (gdk_events_pending () > 0)
585 next_event = gdk_event_get ();
587 /* Try to compress enter/leave notify events. These event
588 * pairs occur when the mouse is dragged quickly across
589 * a window with many buttons (or through a menu). Instead
590 * of highlighting and de-highlighting each widget that
591 * is crossed it is better to simply de-highlight the widget
592 * which contained the mouse initially and highlight the
593 * widget which ends up containing the mouse.
596 if (((event->type == GDK_ENTER_NOTIFY) ||
597 (event->type == GDK_LEAVE_NOTIFY)) &&
598 ((next_event->type == GDK_ENTER_NOTIFY) ||
599 (next_event->type == GDK_LEAVE_NOTIFY)) &&
600 (next_event->type != event->type) &&
601 (next_event->any.window == event->any.window))
603 gdk_event_free (event);
604 gdk_event_free (next_event);
607 goto event_handling_done;
610 /* Find the widget which got the event. We store the widget
611 * in the user_data field of GdkWindow's.
612 * Ignore the event if we don't have a widget for it, except
613 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
614 * Though this happens rarely, bogus events can occour
615 * for e.g. destroyed GdkWindows.
617 event_widget = gtk_get_event_widget (event);
620 /* To handle selection INCR transactions, we select
621 * PropertyNotify events on the requestor window and create
622 * a corresponding (fake) GdkWindow so that events get
623 * here. There won't be a widget though, so we have to handle
626 if (event->type == GDK_PROPERTY_NOTIFY)
627 gtk_selection_incr_event (event->any.window,
630 gdk_event_free (event);
632 goto event_handling_done;
635 /* Push the event onto a stack of current events for
636 * gtk_current_event_get().
638 current_events = g_list_prepend (current_events, event);
640 /* If there is a grab in effect...
644 grab_widget = grabs->data;
646 /* If the grab widget is an ancestor of the event widget
647 * then we send the event to the original event widget.
648 * This is the key to implementing modality.
650 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
651 gtk_widget_is_ancestor (event_widget, grab_widget))
652 grab_widget = event_widget;
656 grab_widget = event_widget;
659 /* Not all events get sent to the grabbing widget.
660 * The delete, destroy, expose, focus change and resize
661 * events still get sent to the event widget because
662 * 1) these events have no meaning for the grabbing widget
663 * and 2) redirecting these events to the grabbing widget
664 * could cause the display to be messed up.
672 gtk_widget_ref (event_widget);
673 if (!gtk_widget_event (event_widget, event) &&
674 !GTK_OBJECT_DESTROYED (event_widget))
675 gtk_widget_destroy (event_widget);
676 gtk_widget_unref (event_widget);
680 gtk_widget_ref (event_widget);
681 gtk_widget_event (event_widget, event);
682 if (!GTK_OBJECT_DESTROYED (event_widget))
683 gtk_widget_destroy (event_widget);
684 gtk_widget_unref (event_widget);
687 case GDK_PROPERTY_NOTIFY:
690 case GDK_FOCUS_CHANGE:
694 case GDK_SELECTION_CLEAR:
695 case GDK_SELECTION_REQUEST:
696 case GDK_SELECTION_NOTIFY:
697 case GDK_CLIENT_EVENT:
699 case GDK_DRAG_REQUEST:
702 case GDK_DROP_DATA_AVAIL:
703 case GDK_VISIBILITY_NOTIFY:
704 gtk_widget_event (event_widget, event);
708 case GDK_KEY_RELEASE:
711 if (gtk_invoke_key_snoopers (grab_widget, event))
714 /* else fall through */
715 case GDK_MOTION_NOTIFY:
716 case GDK_BUTTON_PRESS:
717 case GDK_2BUTTON_PRESS:
718 case GDK_3BUTTON_PRESS:
719 case GDK_BUTTON_RELEASE:
720 case GDK_PROXIMITY_IN:
721 case GDK_PROXIMITY_OUT:
722 case GDK_OTHER_EVENT:
723 gtk_propagate_event (grab_widget, event);
726 case GDK_ENTER_NOTIFY:
727 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
729 gtk_widget_event (grab_widget, event);
730 if (event_widget == grab_widget)
731 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
735 case GDK_LEAVE_NOTIFY:
736 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
738 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
739 gtk_widget_event (event_widget, event);
741 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
742 gtk_widget_event (grab_widget, event);
746 tmp_list = current_events;
747 current_events = g_list_remove_link (current_events, tmp_list);
748 g_list_free_1 (tmp_list);
750 gdk_event_free (event);
754 if (gdk_events_pending() == 0)
760 /* Handle timeout functions that may have expired.
762 gtk_handle_timeouts ();
767 return iteration_done;
783 gtk_grab_add (GtkWidget *widget)
785 g_return_if_fail (widget != NULL);
787 if (!GTK_WIDGET_HAS_GRAB (widget))
789 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
791 grabs = g_slist_prepend (grabs, widget);
792 gtk_widget_ref (widget);
797 gtk_grab_get_current (void)
800 return GTK_WIDGET (grabs->data);
805 gtk_grab_remove (GtkWidget *widget)
807 g_return_if_fail (widget != NULL);
809 if (GTK_WIDGET_HAS_GRAB (widget))
811 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
813 grabs = g_slist_remove (grabs, widget);
814 gtk_widget_unref (widget);
819 gtk_init_add (GtkFunction function,
822 GtkInitFunction *init;
824 init = g_new (GtkInitFunction, 1);
825 init->function = function;
828 init_functions = g_list_prepend (init_functions, init);
832 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
835 GtkKeySnooperData *data;
836 static guint snooper_id = 1;
838 g_return_val_if_fail (snooper != NULL, 0);
840 data = g_new (GtkKeySnooperData, 1);
841 data->func = snooper;
842 data->func_data = func_data;
843 data->id = snooper_id++;
844 key_snoopers = g_slist_prepend (key_snoopers, data);
850 gtk_key_snooper_remove (guint snooper_id)
852 GtkKeySnooperData *data = NULL;
855 slist = key_snoopers;
859 if (data->id == snooper_id)
866 key_snoopers = g_slist_remove (key_snoopers, data);
870 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
874 gint return_val = FALSE;
876 slist = key_snoopers;
877 while (slist && !return_val)
879 GtkKeySnooperData *data;
883 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
890 gtk_timeout_add_full (guint32 interval,
891 GtkFunction function,
892 GtkCallbackMarshal marshal,
894 GtkDestroyNotify destroy)
896 static guint timeout_tag = 1;
897 GtkTimeoutFunction *timeoutf;
899 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
901 /* Create a new timeout function structure.
902 * The start time is the current time.
904 if (!timeout_mem_chunk)
905 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
906 1024, G_ALLOC_AND_FREE);
908 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
910 timeoutf->tag = timeout_tag++;
911 timeoutf->start = gdk_time_get ();
912 timeoutf->interval = interval;
913 timeoutf->originterval = interval;
914 timeoutf->function = function;
915 timeoutf->marshal = marshal;
916 timeoutf->data = data;
917 timeoutf->destroy = destroy;
919 gtk_timeout_insert (timeoutf);
921 return timeoutf->tag;
925 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
927 if (timeoutf->destroy)
928 (timeoutf->destroy) (timeoutf->data);
929 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
933 gtk_timeout_add (guint32 interval,
934 GtkFunction function,
937 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
941 gtk_timeout_add_interp (guint32 interval,
942 GtkCallbackMarshal function,
944 GtkDestroyNotify destroy)
946 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
949 /* Search for the specified tag in a list of timeouts. If it
950 * is found, destroy the timeout, and either remove the link
951 * or set link->data to NULL depending on remove_link
954 gtk_timeout_remove_from_list (GList **list, guint tag, gint remove_link)
957 GtkTimeoutFunction *timeoutf;
962 timeoutf = tmp_list->data;
964 if (timeoutf->tag == tag)
968 *list = g_list_remove_link (*list, tmp_list);
969 g_list_free (tmp_list);
972 tmp_list->data = NULL;
974 gtk_timeout_destroy (timeoutf);
979 tmp_list = tmp_list->next;
985 gtk_timeout_remove (guint tag)
988 /* Remove a timeout function.
989 * (Which, basically, involves searching the
993 if (gtk_timeout_remove_from_list (&timeout_functions, tag, TRUE))
995 if (gtk_timeout_remove_from_list (¤t_timeouts, tag, TRUE))
997 if (gtk_timeout_remove_from_list (&running_timeouts, tag, FALSE))
1001 /* We rely on some knowledge of how g_list_insert_sorted works to make
1002 * sure that we insert at the _end_ of the idles of this priority
1005 gtk_idle_compare (gconstpointer a, gconstpointer b)
1007 return (((const GtkIdleFunction *)a)->priority <
1008 ((const GtkIdleFunction *)b)->priority)
1013 gtk_quit_add_full (guint main_level,
1014 GtkFunction function,
1015 GtkCallbackMarshal marshal,
1017 GtkDestroyNotify destroy)
1019 static guint quit_id = 1;
1020 GtkQuitFunction *quitf;
1022 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1024 if (!quit_mem_chunk)
1025 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1026 512, G_ALLOC_AND_FREE);
1028 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1030 quitf->id = quit_id++;
1031 quitf->main_level = main_level;
1032 quitf->function = function;
1033 quitf->marshal = marshal;
1035 quitf->destroy = destroy;
1037 quit_functions = g_list_prepend (quit_functions, quitf);
1043 gtk_idle_add_full (gint priority,
1044 GtkFunction function,
1045 GtkCallbackMarshal marshal,
1047 GtkDestroyNotify destroy)
1049 static guint idle_tag = 1;
1050 GtkIdleFunction *idlef;
1052 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1054 if (!idle_mem_chunk)
1055 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
1056 1024, G_ALLOC_AND_FREE);
1058 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
1060 idlef->tag = idle_tag++;
1061 idlef->priority = priority;
1062 idlef->function = function;
1063 idlef->marshal = marshal;
1065 idlef->destroy = destroy;
1067 /* If we are adding the first idle function, possibly wake up
1068 * the main thread out of its select().
1070 if (!idle_functions)
1071 gdk_threads_wake ();
1073 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
1080 gtk_idle_add_interp (GtkCallbackMarshal marshal,
1082 GtkDestroyNotify destroy)
1084 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
1088 gtk_idle_destroy (GtkIdleFunction *idlef)
1091 idlef->destroy (idlef->data);
1092 g_mem_chunk_free (idle_mem_chunk, idlef);
1096 gtk_quit_destroy (GtkQuitFunction *quitf)
1099 quitf->destroy (quitf->data);
1100 g_mem_chunk_free (quit_mem_chunk, quitf);
1104 gtk_quit_destructor (GtkObject **object_p)
1107 gtk_object_destroy (*object_p);
1114 gtk_quit_add_destroy (guint main_level,
1117 GtkObject **object_p;
1119 g_return_if_fail (main_level > 0);
1120 g_return_if_fail (object != NULL);
1121 g_return_if_fail (GTK_IS_OBJECT (object));
1123 object_p = g_new (GtkObject*, 1);
1125 gtk_signal_connect (object,
1127 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1129 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1133 gtk_quit_add (guint main_level,
1134 GtkFunction function,
1137 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1141 gtk_idle_add (GtkFunction function,
1144 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1148 gtk_idle_add_priority (gint priority,
1149 GtkFunction function,
1152 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1156 gtk_quit_remove (guint id)
1158 GtkQuitFunction *quitf;
1161 tmp_list = quit_functions;
1164 quitf = tmp_list->data;
1166 if (quitf->id == id)
1168 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1169 g_list_free (tmp_list);
1170 gtk_quit_destroy (quitf);
1175 tmp_list = tmp_list->next;
1180 gtk_quit_remove_by_data (gpointer data)
1182 GtkQuitFunction *quitf;
1185 tmp_list = quit_functions;
1188 quitf = tmp_list->data;
1190 if (quitf->data == data)
1192 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1193 g_list_free (tmp_list);
1194 gtk_quit_destroy (quitf);
1199 tmp_list = tmp_list->next;
1203 /* Search for the specified tag in a list of idles. If it
1204 * is found, destroy the idle, and either remove the link
1205 * or set link->data to NULL depending on remove_link
1207 * If tag != 0, match tag against idlef->tag, otherwise, match
1208 * data against idlef->data
1211 gtk_idle_remove_from_list (GList **list,
1216 GtkIdleFunction *idlef;
1222 idlef = tmp_list->data;
1224 if (((tag != 0) && (idlef->tag == tag)) ||
1225 ((tag == 0) && (idlef->data == data)))
1229 *list = g_list_remove_link (*list, tmp_list);
1230 g_list_free (tmp_list);
1233 tmp_list->data = NULL;
1235 gtk_idle_destroy (idlef);
1240 tmp_list = tmp_list->next;
1246 gtk_idle_remove (guint tag)
1248 g_return_if_fail (tag != 0);
1250 if (gtk_idle_remove_from_list (&idle_functions, tag, NULL, TRUE))
1252 if (gtk_idle_remove_from_list (¤t_idles, tag, NULL, TRUE))
1254 if (gtk_idle_remove_from_list (&running_idles, tag, NULL, FALSE))
1259 gtk_idle_remove_by_data (gpointer data)
1261 if (gtk_idle_remove_from_list (&idle_functions, 0, data, TRUE))
1263 if (gtk_idle_remove_from_list (¤t_idles, 0, data, TRUE))
1265 if (gtk_idle_remove_from_list (&running_idles, 0, data, FALSE))
1270 gtk_invoke_input_function (GtkInputFunction *input,
1272 GdkInputCondition condition)
1275 args[0].type = GTK_TYPE_INT;
1276 args[0].name = NULL;
1277 GTK_VALUE_INT(args[0]) = source;
1278 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1279 args[1].name = NULL;
1280 GTK_VALUE_FLAGS(args[1]) = condition;
1281 args[2].type = GTK_TYPE_NONE;
1282 args[2].name = NULL;
1284 input->callback (NULL, input->data, 2, args);
1288 gtk_destroy_input_function (GtkInputFunction *input)
1291 (input->destroy) (input->data);
1296 gtk_input_add_full (gint source,
1297 GdkInputCondition condition,
1298 GdkInputFunction function,
1299 GtkCallbackMarshal marshal,
1301 GtkDestroyNotify destroy)
1305 GtkInputFunction *input;
1307 input = g_new (GtkInputFunction, 1);
1308 input->callback = marshal;
1310 input->destroy = destroy;
1312 return gdk_input_add_full (source,
1314 (GdkInputFunction) gtk_invoke_input_function,
1316 (GdkDestroyNotify) gtk_destroy_input_function);
1319 return gdk_input_add_full (source, condition, function, data, destroy);
1323 gtk_input_add_interp (gint source,
1324 GdkInputCondition condition,
1325 GtkCallbackMarshal callback,
1327 GtkDestroyNotify destroy)
1329 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1333 gtk_input_remove (guint tag)
1335 gdk_input_remove (tag);
1339 gtk_get_current_event (void)
1342 return gdk_event_copy ((GdkEvent *) current_events->data);
1348 gtk_get_event_widget (GdkEvent *event)
1353 if (event && event->any.window)
1354 gdk_window_get_user_data (event->any.window, (void**) &widget);
1360 gtk_exit_func (void)
1362 if (gtk_initialized)
1364 gtk_initialized = FALSE;
1365 gtk_preview_uninit ();
1370 /* We rely on some knowledge of how g_list_insert_sorted works to make
1371 * sure that we insert after timeouts of equal interval
1374 gtk_timeout_compare (gconstpointer a, gconstpointer b)
1376 return (((const GtkTimeoutFunction *)a)->interval <
1377 ((const GtkTimeoutFunction *)b)->interval)
1382 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1384 g_assert (timeoutf != NULL);
1386 /* Insert the timeout function appropriately.
1387 * Appropriately meaning sort it into the list
1388 * of timeout functions.
1390 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1391 gtk_timeout_compare);
1395 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1397 if (!timeoutf->marshal)
1398 return timeoutf->function (timeoutf->data);
1402 gint ret_val = FALSE;
1403 args[0].name = NULL;
1404 args[0].type = GTK_TYPE_BOOL;
1405 args[0].d.pointer_data = &ret_val;
1406 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1412 gtk_handle_current_timeouts (guint32 the_time)
1416 GtkTimeoutFunction *timeoutf;
1418 while (current_timeouts)
1420 tmp_list = current_timeouts;
1421 timeoutf = tmp_list->data;
1423 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1424 if (running_timeouts)
1426 running_timeouts->prev = tmp_list;
1427 tmp_list->next = running_timeouts;
1429 running_timeouts = tmp_list;
1431 result = gtk_invoke_timeout_function (timeoutf);
1433 running_timeouts = g_list_remove_link (running_timeouts, tmp_list);
1434 timeoutf = tmp_list->data;
1436 g_list_free_1 (tmp_list);
1442 gtk_timeout_destroy (timeoutf);
1446 timeoutf->interval = timeoutf->originterval;
1447 timeoutf->start = the_time;
1448 gtk_timeout_insert (timeoutf);
1455 gtk_handle_timeouts (void)
1460 GtkTimeoutFunction *timeoutf;
1462 /* Caller must already have called gtk_handle_current_timeouts if
1464 g_assert (current_timeouts == NULL);
1466 if (timeout_functions)
1468 the_time = gdk_time_get ();
1470 tmp_list = timeout_functions;
1473 timeoutf = tmp_list->data;
1475 if (timeoutf->interval <= (the_time - timeoutf->start))
1477 tmp_list2 = tmp_list;
1478 tmp_list = tmp_list->next;
1480 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1481 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1485 timeoutf->interval -= (the_time - timeoutf->start);
1486 timeoutf->start = the_time;
1487 tmp_list = tmp_list->next;
1491 if (current_timeouts)
1492 gtk_handle_current_timeouts (the_time);
1497 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1499 if (!quitf->marshal)
1500 return quitf->function (quitf->data);
1504 gint ret_val = FALSE;
1506 args[0].name = NULL;
1507 args[0].type = GTK_TYPE_BOOL;
1508 args[0].d.pointer_data = &ret_val;
1509 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1517 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1519 if (!idlef->marshal)
1520 return idlef->function (idlef->data);
1524 gint ret_val = FALSE;
1526 args[0].name = NULL;
1527 args[0].type = GTK_TYPE_BOOL;
1528 args[0].d.pointer_data = &ret_val;
1529 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1537 gtk_handle_current_idles (void)
1541 GtkIdleFunction *idlef;
1544 while (current_idles)
1546 tmp_list = current_idles;
1547 idlef = tmp_list->data;
1549 current_idles = g_list_remove_link (current_idles, tmp_list);
1552 running_idles->prev = tmp_list;
1553 tmp_list->next = running_idles;
1555 running_idles = tmp_list;
1557 result = gtk_idle_invoke_function (idlef);
1559 running_idles = g_list_remove_link (running_idles, tmp_list);
1560 idlef = tmp_list->data;
1562 if (!idlef || !result)
1564 g_list_free (tmp_list);
1566 gtk_idle_destroy (idlef);
1570 /* Insert the idle function back into the list of idle
1571 * functions at the end of the idles of this priority
1573 tmp_list2 = idle_functions;
1575 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1576 tmp_list2 = tmp_list2->next;
1579 idle_functions = g_list_concat (idle_functions, tmp_list);
1580 else if (tmp_list2 == idle_functions)
1582 tmp_list->next = idle_functions;
1584 idle_functions->prev = tmp_list;
1585 idle_functions = tmp_list;
1589 tmp_list->prev = tmp_list2->prev;
1590 tmp_list->next = tmp_list2;
1591 tmp_list2->prev->next = tmp_list;
1592 tmp_list2->prev = tmp_list;
1599 gtk_handle_idle (void)
1601 /* Caller must already have called gtk_handle_current_idles if
1604 g_assert (current_idles == NULL);
1606 /* Handle only the idle functions that have the highest priority */
1612 tmp_list = idle_functions;
1613 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1616 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1617 tmp_list = tmp_list->next;
1619 current_idles = idle_functions;
1620 idle_functions = tmp_list;
1624 tmp_list->prev->next = NULL;
1625 tmp_list->prev = NULL;
1628 gtk_handle_current_idles();
1633 gtk_handle_timer (void)
1635 GtkTimeoutFunction *timeoutf;
1640 gdk_timer_enable ();
1642 else if (timeout_functions)
1644 timeoutf = timeout_functions->data;
1645 gdk_timer_set (timeoutf->interval);
1646 gdk_timer_enable ();
1651 gdk_timer_disable ();
1656 gtk_propagate_event (GtkWidget *widget,
1661 g_return_if_fail (widget != NULL);
1662 g_return_if_fail (event != NULL);
1664 handled_event = FALSE;
1666 if ((event->type == GDK_KEY_PRESS) ||
1667 (event->type == GDK_KEY_RELEASE))
1669 /* Only send key events within Window widgets to the Window
1670 * The Window widget will in turn pass the
1671 * key event on to the currently focused widget
1676 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1679 if (GTK_WIDGET_IS_SENSITIVE (window))
1680 gtk_widget_event (window, event);
1682 handled_event = TRUE; /* don't send to widget */
1686 /* Other events get propagated up the widget tree
1687 * so that parents can see the button and motion
1688 * events of the children.
1690 while (!handled_event && widget)
1694 gtk_widget_ref (widget);
1695 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1696 tmp = widget->parent;
1697 gtk_widget_unref (widget);
1704 gtk_error (gchar *str)
1710 gtk_warning (gchar *str)
1716 gtk_message (gchar *str)
1722 gtk_print (gchar *str)
1724 static GtkWidget *window = NULL;
1725 static GtkWidget *text;
1726 static int level = 0;
1730 GtkWidget *hscrollbar;
1731 GtkWidget *vscrollbar;
1732 GtkWidget *separator;
1737 fputs (str, stdout);
1744 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1746 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1747 (GtkSignalFunc) gtk_widget_destroyed,
1750 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1752 box1 = gtk_vbox_new (FALSE, 0);
1753 gtk_container_add (GTK_CONTAINER (window), box1);
1754 gtk_widget_show (box1);
1757 box2 = gtk_vbox_new (FALSE, 10);
1758 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1759 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1760 gtk_widget_show (box2);
1763 table = gtk_table_new (2, 2, FALSE);
1764 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1765 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1766 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1767 gtk_widget_show (table);
1769 text = gtk_text_new (NULL, NULL);
1770 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1771 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1772 gtk_widget_show (text);
1773 gtk_widget_realize (text);
1775 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1776 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1777 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1778 gtk_widget_show (hscrollbar);
1780 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1781 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1782 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1783 gtk_widget_show (vscrollbar);
1785 separator = gtk_hseparator_new ();
1786 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1787 gtk_widget_show (separator);
1790 box2 = gtk_vbox_new (FALSE, 10);
1791 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1792 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1793 gtk_widget_show (box2);
1796 button = gtk_button_new_with_label ("close");
1797 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1798 (GtkSignalFunc) gtk_widget_hide,
1799 GTK_OBJECT (window));
1800 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1801 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1802 gtk_widget_grab_default (button);
1803 gtk_widget_show (button);
1807 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1810 if (!GTK_WIDGET_VISIBLE (window))
1811 gtk_widget_show (window);