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 (gpointer a,
134 static gint gtk_timeout_compare (gpointer 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;
141 static gboolean iteration_done = FALSE;
142 static guint main_level = 0;
143 static gint initialized = FALSE;
144 static GdkEvent *next_event = NULL;
145 static GList *current_events = NULL;
147 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
148 * widget is the first one on the list.
150 static GList *init_functions = NULL; /* A list of init functions.
152 static GList *quit_functions = NULL; /* A list of quit functions.
156 /* When handling timeouts, the algorithm is to move all of the expired
157 * timeouts from timeout_functions to current_timeouts then process
158 * them as a batch. If one of the timeouts recursively calls the main
159 * loop, then the remainder of the timeouts in current_timeouts will
160 * be processed before anything else happens.
162 * Each timeout is procesed as follows:
164 * - The timeout is removed from current_timeouts
165 * - The timeout is pushed on the running_timeouts stack
166 * - The timeout is executed
167 * - The timeout stack is popped
168 * - If the timeout function wasn't removed from the stack while executing,
169 * and it returned TRUE, it is added back to timeout_functions, otherwise
170 * it is destroyed if necessary.
172 * gtk_timeout_remove() works by checking for the timeout in
173 * timeout_functions current_timeouts and running_timeouts. If it is
174 * found in one of the first two, it is removed and destroyed. If it
175 * is found in running_timeouts, it is destroyed and ->data is set to
176 * NULL for the stack entry.
178 * Idle functions work pretty much identically.
181 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
182 * when the length of the time interval
183 * remaining. Therefore, the first timeout
184 * function to expire is at the head of
185 * the list and the last to expire is at
186 * the tail of the list. */
187 static GList *idle_functions = NULL; /* A list of idle functions.
190 /* The idle functions / timeouts that are currently being processed
191 * by gtk_handle_current_(timeouts/idles)
193 static GList *current_idles = NULL;
194 static GList *current_timeouts = NULL;
196 /* A stack of idle functions / timeouts that are currently being
199 static GList *running_idles = NULL;
200 static GList *running_timeouts = NULL;
202 static GMemChunk *timeout_mem_chunk = NULL;
203 static GMemChunk *idle_mem_chunk = NULL;
204 static GMemChunk *quit_mem_chunk = NULL;
206 static GSList *key_snoopers = NULL;
208 static GdkVisual *gtk_visual; /* The visual to be used in creating new
211 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
215 guint gtk_debug_flags = 0; /* Global GTK debug flag */
217 #ifdef G_ENABLE_DEBUG
218 static GDebugKey gtk_debug_keys[] = {
219 {"objects", GTK_DEBUG_OBJECTS},
220 {"misc", GTK_DEBUG_MISC}
223 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
225 #endif /* G_ENABLE_DEBUG */
227 gint gtk_use_mb = -1;
233 gchar *current_locale;
237 g_set_error_handler (gtk_error);
238 g_set_warning_handler (gtk_warning);
239 g_set_message_handler (gtk_message);
240 g_set_print_handler (gtk_print);
243 /* Initialize "gdk". We pass along the 'argc' and 'argv'
244 * parameters as they contain information that GDK uses
246 gdk_init (argc, argv);
248 #ifdef G_ENABLE_DEBUG
250 gchar *debug_string = getenv("GTK_DEBUG");
251 if (debug_string != NULL)
252 gtk_debug_flags = g_parse_debug_string (debug_string,
261 for (i = 1; i < *argc;)
263 if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
264 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
266 gchar *equal_pos = strchr ((*argv)[i], '=');
268 if (equal_pos != NULL)
270 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
274 else if ((i + 1) < *argc && (*argv)[i + 1])
276 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
284 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
285 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
287 gchar *equal_pos = strchr ((*argv)[i], '=');
289 if (equal_pos != NULL)
291 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
295 else if ((i + 1) < *argc && (*argv)[i + 1])
297 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
308 for (i = 1; i < *argc; i++)
310 for (k = i; k < *argc; k++)
311 if ((*argv)[k] != NULL)
317 for (j = i + k; j < *argc; j++)
318 (*argv)[j-k] = (*argv)[j];
324 #endif /* G_ENABLE_DEBUG */
326 /* Check if there is a good chance the mb functions will handle things
327 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
328 * C locale, or were using X's mb functions. (-DX_LOCALE && locale != C)
331 current_locale = g_strdup (setlocale (LC_CTYPE, NULL));
334 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
339 setlocale (LC_CTYPE, "C");
340 gtk_use_mb = (mblen ("\xc0", MB_CUR_MAX) == 1);
341 setlocale (LC_CTYPE, current_locale);
344 g_free (current_locale);
346 GTK_NOTE (MISC, g_print("%s multi-byte string functions.\n",
347 gtk_use_mb ? "Using" : "Not using"));
349 /* Initialize the default visual and colormap to be
350 * used in creating widgets. (We want to use the system
351 * defaults so as to be nice to the colormap).
353 gtk_visual = gdk_visual_get_system ();
354 gtk_colormap = gdk_colormap_get_system ();
361 /* Register an exit function to make sure we are able to cleanup.
363 if (ATEXIT (gtk_exit_func))
364 g_warning ("unable to register exit function");
366 /* Set the 'initialized' flag.
372 gtk_exit (int errorcode)
374 /* Only if "gtk" has been initialized should we de-initialize.
376 /* de-initialisation is done by the gtk_exit_funct(),
377 * no need to do this here (Alex J.)
383 gtk_set_locale (void)
385 return gdk_set_locale ();
393 GtkInitFunction *init;
398 tmp_list = functions = init_functions;
399 init_functions = NULL;
403 init = tmp_list->data;
404 tmp_list = tmp_list->next;
406 (* init->function) (init->data);
409 g_list_free (functions);
411 old_done = iteration_done;
412 while (!gtk_main_iteration ())
414 iteration_done = old_done;
418 GList *reinvoke_list = NULL;
419 GtkQuitFunction *quitf;
421 while (quit_functions)
423 quitf = quit_functions->data;
425 quit_functions = g_list_remove_link (quit_functions, quit_functions);
427 if ((quitf->main_level && quitf->main_level != main_level) ||
428 gtk_quit_invoke_function (quitf))
430 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
434 g_list_free (tmp_list);
435 gtk_quit_destroy (quitf);
442 tmp_list = g_list_last (reinvoke_list);
444 quit_functions->prev = tmp_list;
445 tmp_list->next = quit_functions;
446 quit_functions = tmp_list;
454 gtk_main_level (void)
462 iteration_done = TRUE;
466 gtk_events_pending (void)
470 /* if this function is called from a timeout which will only return
471 * if gtk needs processor time, we need to take iteration_done==TRUE
472 * into account as well.
474 result = iteration_done;
475 result += next_event != NULL;
476 result += gdk_events_pending();
478 result += current_idles != NULL;
479 result += current_timeouts != NULL;
483 result += (idle_functions &&
484 (((GtkIdleFunction *)idle_functions->data)->priority <=
485 GTK_PRIORITY_INTERNAL));
488 if (!result && timeout_functions)
491 GtkTimeoutFunction *timeoutf;
493 the_time = gdk_time_get ();
495 timeoutf = timeout_functions->data;
497 result += timeoutf->interval <= (the_time - timeoutf->start);
504 gtk_main_iteration (void)
506 return gtk_main_iteration_do (TRUE);
510 gtk_main_iteration_do (gboolean blocking)
512 GtkWidget *event_widget;
513 GtkWidget *grab_widget;
514 GdkEvent *event = NULL;
517 iteration_done = FALSE;
519 /* If this is a recursive call, and there are pending timeouts or
520 * idles, finish them, then return immediately to avoid blocking
523 if (current_timeouts)
525 gtk_handle_current_timeouts( gdk_time_get());
530 return iteration_done;
534 gtk_handle_current_idles ();
539 return iteration_done;
542 /* If there is a valid event in 'next_event' then move it to 'event'
550 /* If we don't have an event then get one.
554 /* Handle setting of the "gdk" timer. If there are no
555 * timeout functions, then the timer is turned off.
556 * If there are timeout functions, then the timer is
557 * set to the shortest timeout interval (which is
558 * the first timeout function).
562 if (blocking) event = gdk_event_get ();
565 /* "gdk_event_get" can return FALSE if the timer goes off
566 * and no events are pending. Therefore, we should make
567 * sure that we got an event before continuing.
571 /* If there are any events pending then get the next one.
573 if (gdk_events_pending () > 0)
574 next_event = gdk_event_get ();
576 /* Try to compress enter/leave notify events. These event
577 * pairs occur when the mouse is dragged quickly across
578 * a window with many buttons (or through a menu). Instead
579 * of highlighting and de-highlighting each widget that
580 * is crossed it is better to simply de-highlight the widget
581 * which contained the mouse initially and highlight the
582 * widget which ends up containing the mouse.
585 if (((event->type == GDK_ENTER_NOTIFY) ||
586 (event->type == GDK_LEAVE_NOTIFY)) &&
587 ((next_event->type == GDK_ENTER_NOTIFY) ||
588 (next_event->type == GDK_LEAVE_NOTIFY)) &&
589 (next_event->type != event->type) &&
590 (next_event->any.window == event->any.window))
592 gdk_event_free (event);
593 gdk_event_free (next_event);
596 goto event_handling_done;
599 /* Find the widget which got the event. We store the widget
600 * in the user_data field of GdkWindow's.
601 * Ignore the event if we don't have a widget for it, except
602 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
603 * Though this happens rarely, bogus events can occour
604 * for e.g. destroyed GdkWindows.
606 event_widget = gtk_get_event_widget (event);
609 /* To handle selection INCR transactions, we select
610 * PropertyNotify events on the requestor window and create
611 * a corresponding (fake) GdkWindow so that events get
612 * here. There won't be a widget though, so we have to handle
615 if (event->type == GDK_PROPERTY_NOTIFY)
616 gtk_selection_incr_event (event->any.window,
619 gdk_event_free (event);
621 goto event_handling_done;
624 /* Push the event onto a stack of current events for
625 * gtk_current_event_get().
627 current_events = g_list_prepend (current_events, event);
629 /* If there is a grab in effect...
633 grab_widget = grabs->data;
635 /* If the grab widget is an ancestor of the event widget
636 * then we send the event to the original event widget.
637 * This is the key to implementing modality.
639 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
640 gtk_widget_is_ancestor (event_widget, grab_widget))
641 grab_widget = event_widget;
645 grab_widget = event_widget;
648 /* Not all events get sent to the grabbing widget.
649 * The delete, destroy, expose, focus change and resize
650 * events still get sent to the event widget because
651 * 1) these events have no meaning for the grabbing widget
652 * and 2) redirecting these events to the grabbing widget
653 * could cause the display to be messed up.
661 gtk_widget_ref (event_widget);
662 if (!gtk_widget_event (event_widget, event) &&
663 !GTK_OBJECT_DESTROYED (event_widget))
664 gtk_widget_destroy (event_widget);
665 gtk_widget_unref (event_widget);
669 gtk_widget_ref (event_widget);
670 gtk_widget_event (event_widget, event);
671 if (!GTK_OBJECT_DESTROYED (event_widget))
672 gtk_widget_destroy (event_widget);
673 gtk_widget_unref (event_widget);
676 case GDK_PROPERTY_NOTIFY:
679 case GDK_FOCUS_CHANGE:
683 case GDK_SELECTION_CLEAR:
684 case GDK_SELECTION_REQUEST:
685 case GDK_SELECTION_NOTIFY:
686 case GDK_CLIENT_EVENT:
688 case GDK_DRAG_REQUEST:
691 case GDK_DROP_DATA_AVAIL:
692 case GDK_VISIBILITY_NOTIFY:
693 gtk_widget_event (event_widget, event);
697 case GDK_KEY_RELEASE:
700 if (gtk_invoke_key_snoopers (grab_widget, event))
703 /* else fall through */
704 case GDK_MOTION_NOTIFY:
705 case GDK_BUTTON_PRESS:
706 case GDK_2BUTTON_PRESS:
707 case GDK_3BUTTON_PRESS:
708 case GDK_BUTTON_RELEASE:
709 case GDK_PROXIMITY_IN:
710 case GDK_PROXIMITY_OUT:
711 case GDK_OTHER_EVENT:
712 gtk_propagate_event (grab_widget, event);
715 case GDK_ENTER_NOTIFY:
716 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
718 gtk_widget_event (grab_widget, event);
719 if (event_widget == grab_widget)
720 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
724 case GDK_LEAVE_NOTIFY:
725 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
727 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
728 gtk_widget_event (event_widget, event);
730 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
731 gtk_widget_event (grab_widget, event);
735 tmp_list = current_events;
736 current_events = g_list_remove_link (current_events, tmp_list);
737 g_list_free_1 (tmp_list);
739 gdk_event_free (event);
743 if (gdk_events_pending() == 0)
749 /* Handle timeout functions that may have expired.
751 gtk_handle_timeouts ();
756 return iteration_done;
772 gtk_grab_add (GtkWidget *widget)
774 g_return_if_fail (widget != NULL);
776 if (!GTK_WIDGET_HAS_GRAB (widget))
778 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
780 grabs = g_slist_prepend (grabs, widget);
781 gtk_widget_ref (widget);
786 gtk_grab_get_current (void)
789 return GTK_WIDGET (grabs->data);
794 gtk_grab_remove (GtkWidget *widget)
796 g_return_if_fail (widget != NULL);
798 if (GTK_WIDGET_HAS_GRAB (widget))
800 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
802 grabs = g_slist_remove (grabs, widget);
803 gtk_widget_unref (widget);
808 gtk_init_add (GtkFunction function,
811 GtkInitFunction *init;
813 init = g_new (GtkInitFunction, 1);
814 init->function = function;
817 init_functions = g_list_prepend (init_functions, init);
821 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
824 GtkKeySnooperData *data;
825 static guint snooper_id = 1;
827 g_return_val_if_fail (snooper != NULL, 0);
829 data = g_new (GtkKeySnooperData, 1);
830 data->func = snooper;
831 data->func_data = func_data;
832 data->id = snooper_id++;
833 key_snoopers = g_slist_prepend (key_snoopers, data);
839 gtk_key_snooper_remove (guint snooper_id)
841 GtkKeySnooperData *data = NULL;
844 slist = key_snoopers;
848 if (data->id == snooper_id)
855 key_snoopers = g_slist_remove (key_snoopers, data);
859 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
863 gint return_val = FALSE;
865 slist = key_snoopers;
866 while (slist && !return_val)
868 GtkKeySnooperData *data;
872 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
879 gtk_timeout_add_full (guint32 interval,
880 GtkFunction function,
881 GtkCallbackMarshal marshal,
883 GtkDestroyNotify destroy)
885 static guint timeout_tag = 1;
886 GtkTimeoutFunction *timeoutf;
888 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
890 /* Create a new timeout function structure.
891 * The start time is the current time.
893 if (!timeout_mem_chunk)
894 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
895 1024, G_ALLOC_AND_FREE);
897 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
899 timeoutf->tag = timeout_tag++;
900 timeoutf->start = gdk_time_get ();
901 timeoutf->interval = interval;
902 timeoutf->originterval = interval;
903 timeoutf->function = function;
904 timeoutf->marshal = marshal;
905 timeoutf->data = data;
906 timeoutf->destroy = destroy;
908 gtk_timeout_insert (timeoutf);
910 return timeoutf->tag;
914 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
916 if (timeoutf->destroy)
917 (timeoutf->destroy) (timeoutf->data);
918 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
922 gtk_timeout_add (guint32 interval,
923 GtkFunction function,
926 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
930 gtk_timeout_add_interp (guint32 interval,
931 GtkCallbackMarshal function,
933 GtkDestroyNotify destroy)
935 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
938 /* Search for the specified tag in a list of timeouts. If it
939 * is found, destroy the timeout, and either remove the link
940 * or set link->data to NULL depending on remove_link
943 gtk_timeout_remove_from_list (GList **list, guint tag, gint remove_link)
946 GtkTimeoutFunction *timeoutf;
951 timeoutf = tmp_list->data;
953 if (timeoutf->tag == tag)
957 *list = g_list_remove_link (*list, tmp_list);
958 g_list_free (tmp_list);
961 tmp_list->data = NULL;
963 gtk_timeout_destroy (timeoutf);
968 tmp_list = tmp_list->next;
974 gtk_timeout_remove (guint tag)
977 /* Remove a timeout function.
978 * (Which, basically, involves searching the
982 if (gtk_timeout_remove_from_list (&timeout_functions, tag, TRUE))
984 if (gtk_timeout_remove_from_list (¤t_timeouts, tag, TRUE))
986 if (gtk_timeout_remove_from_list (&running_timeouts, tag, FALSE))
990 /* We rely on some knowledge of how g_list_insert_sorted works to make
991 * sure that we insert at the _end_ of the idles of this priority
994 gtk_idle_compare (gpointer a, gpointer b)
996 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
1001 gtk_quit_add_full (guint main_level,
1002 GtkFunction function,
1003 GtkCallbackMarshal marshal,
1005 GtkDestroyNotify destroy)
1007 static guint quit_id = 1;
1008 GtkQuitFunction *quitf;
1010 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1012 if (!quit_mem_chunk)
1013 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1014 512, G_ALLOC_AND_FREE);
1016 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1018 quitf->id = quit_id++;
1019 quitf->main_level = main_level;
1020 quitf->function = function;
1021 quitf->marshal = marshal;
1023 quitf->destroy = destroy;
1025 quit_functions = g_list_prepend (quit_functions, quitf);
1031 gtk_idle_add_full (gint priority,
1032 GtkFunction function,
1033 GtkCallbackMarshal marshal,
1035 GtkDestroyNotify destroy)
1037 static guint idle_tag = 1;
1038 GtkIdleFunction *idlef;
1040 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1042 if (!idle_mem_chunk)
1043 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
1044 1024, G_ALLOC_AND_FREE);
1046 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
1048 idlef->tag = idle_tag++;
1049 idlef->priority = priority;
1050 idlef->function = function;
1051 idlef->marshal = marshal;
1053 idlef->destroy = destroy;
1055 /* If we are adding the first idle function, possibly wake up
1056 * the main thread out of its select().
1058 if (!idle_functions)
1059 gdk_threads_wake ();
1061 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
1068 gtk_idle_add_interp (GtkCallbackMarshal marshal,
1070 GtkDestroyNotify destroy)
1072 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
1076 gtk_idle_destroy (GtkIdleFunction *idlef)
1079 idlef->destroy (idlef->data);
1080 g_mem_chunk_free (idle_mem_chunk, idlef);
1084 gtk_quit_destroy (GtkQuitFunction *quitf)
1087 quitf->destroy (quitf->data);
1088 g_mem_chunk_free (quit_mem_chunk, quitf);
1092 gtk_quit_destructor (GtkObject **object_p)
1095 gtk_object_destroy (*object_p);
1102 gtk_quit_add_destroy (guint main_level,
1105 GtkObject **object_p;
1107 g_return_if_fail (main_level > 0);
1108 g_return_if_fail (object != NULL);
1109 g_return_if_fail (GTK_IS_OBJECT (object));
1111 object_p = g_new (GtkObject*, 1);
1113 gtk_signal_connect (object,
1115 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1117 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1121 gtk_quit_add (guint main_level,
1122 GtkFunction function,
1125 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1129 gtk_idle_add (GtkFunction function,
1132 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1136 gtk_idle_add_priority (gint priority,
1137 GtkFunction function,
1140 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1144 gtk_quit_remove (guint id)
1146 GtkQuitFunction *quitf;
1149 tmp_list = quit_functions;
1152 quitf = tmp_list->data;
1154 if (quitf->id == id)
1156 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1157 g_list_free (tmp_list);
1158 gtk_quit_destroy (quitf);
1163 tmp_list = tmp_list->next;
1168 gtk_quit_remove_by_data (gpointer data)
1170 GtkQuitFunction *quitf;
1173 tmp_list = quit_functions;
1176 quitf = tmp_list->data;
1178 if (quitf->data == data)
1180 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1181 g_list_free (tmp_list);
1182 gtk_quit_destroy (quitf);
1187 tmp_list = tmp_list->next;
1191 /* Search for the specified tag in a list of idles. If it
1192 * is found, destroy the idle, and either remove the link
1193 * or set link->data to NULL depending on remove_link
1195 * If tag != 0, match tag against idlef->tag, otherwise, match
1196 * data against idlef->data
1199 gtk_idle_remove_from_list (GList **list,
1204 GtkIdleFunction *idlef;
1210 idlef = tmp_list->data;
1212 if (((tag != 0) && (idlef->tag == tag)) ||
1213 ((tag == 0) && (idlef->data == data)))
1217 *list = g_list_remove_link (*list, tmp_list);
1218 g_list_free (tmp_list);
1221 tmp_list->data = NULL;
1223 gtk_idle_destroy (idlef);
1228 tmp_list = tmp_list->next;
1234 gtk_idle_remove (guint tag)
1236 g_return_if_fail (tag != 0);
1238 if (gtk_idle_remove_from_list (&idle_functions, tag, NULL, TRUE))
1240 if (gtk_idle_remove_from_list (¤t_idles, tag, NULL, TRUE))
1242 if (gtk_idle_remove_from_list (&running_idles, tag, NULL, FALSE))
1247 gtk_idle_remove_by_data (gpointer data)
1249 if (gtk_idle_remove_from_list (&idle_functions, 0, data, TRUE))
1251 if (gtk_idle_remove_from_list (¤t_idles, 0, data, TRUE))
1253 if (gtk_idle_remove_from_list (&running_idles, 0, data, FALSE))
1258 gtk_invoke_input_function (GtkInputFunction *input,
1260 GdkInputCondition condition)
1263 args[0].type = GTK_TYPE_INT;
1264 args[0].name = NULL;
1265 GTK_VALUE_INT(args[0]) = source;
1266 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1267 args[1].name = NULL;
1268 GTK_VALUE_FLAGS(args[1]) = condition;
1269 args[2].type = GTK_TYPE_NONE;
1270 args[2].name = NULL;
1272 input->callback (NULL, input->data, 2, args);
1276 gtk_destroy_input_function (GtkInputFunction *input)
1279 (input->destroy) (input->data);
1284 gtk_input_add_full (gint source,
1285 GdkInputCondition condition,
1286 GdkInputFunction function,
1287 GtkCallbackMarshal marshal,
1289 GtkDestroyNotify destroy)
1293 GtkInputFunction *input;
1295 input = g_new (GtkInputFunction, 1);
1296 input->callback = marshal;
1298 input->destroy = destroy;
1300 return gdk_input_add_full (source,
1302 (GdkInputFunction) gtk_invoke_input_function,
1304 (GdkDestroyNotify) gtk_destroy_input_function);
1307 return gdk_input_add_full (source, condition, function, data, destroy);
1311 gtk_input_add_interp (gint source,
1312 GdkInputCondition condition,
1313 GtkCallbackMarshal callback,
1315 GtkDestroyNotify destroy)
1317 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1321 gtk_input_remove (guint tag)
1323 gdk_input_remove (tag);
1327 gtk_get_current_event (void)
1330 return gdk_event_copy ((GdkEvent *) current_events->data);
1336 gtk_get_event_widget (GdkEvent *event)
1341 if (event && event->any.window)
1342 gdk_window_get_user_data (event->any.window, (void**) &widget);
1348 gtk_exit_func (void)
1352 initialized = FALSE;
1353 gtk_preview_uninit ();
1358 /* We rely on some knowledge of how g_list_insert_sorted works to make
1359 * sure that we insert after timeouts of equal interval
1362 gtk_timeout_compare (gpointer a, gpointer b)
1364 return (((GtkTimeoutFunction *)a)->interval <
1365 ((GtkTimeoutFunction *)b)->interval)
1370 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1372 g_assert (timeoutf != NULL);
1374 /* Insert the timeout function appropriately.
1375 * Appropriately meaning sort it into the list
1376 * of timeout functions.
1378 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1379 gtk_timeout_compare);
1383 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1385 if (!timeoutf->marshal)
1386 return timeoutf->function (timeoutf->data);
1390 gint ret_val = FALSE;
1391 args[0].name = NULL;
1392 args[0].type = GTK_TYPE_BOOL;
1393 args[0].d.pointer_data = &ret_val;
1394 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1400 gtk_handle_current_timeouts (guint32 the_time)
1404 GtkTimeoutFunction *timeoutf;
1406 while (current_timeouts)
1408 tmp_list = current_timeouts;
1409 timeoutf = tmp_list->data;
1411 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1412 if (running_timeouts)
1414 running_timeouts->prev = tmp_list;
1415 tmp_list->next = running_timeouts;
1417 running_timeouts = tmp_list;
1419 result = gtk_invoke_timeout_function (timeoutf);
1421 running_timeouts = g_list_remove_link (running_timeouts, tmp_list);
1422 timeoutf = tmp_list->data;
1424 g_list_free_1 (tmp_list);
1430 gtk_timeout_destroy (timeoutf);
1434 timeoutf->interval = timeoutf->originterval;
1435 timeoutf->start = the_time;
1436 gtk_timeout_insert (timeoutf);
1443 gtk_handle_timeouts (void)
1448 GtkTimeoutFunction *timeoutf;
1450 /* Caller must already have called gtk_handle_current_timeouts if
1452 g_assert (current_timeouts == NULL);
1454 if (timeout_functions)
1456 the_time = gdk_time_get ();
1458 tmp_list = timeout_functions;
1461 timeoutf = tmp_list->data;
1463 if (timeoutf->interval <= (the_time - timeoutf->start))
1465 tmp_list2 = tmp_list;
1466 tmp_list = tmp_list->next;
1468 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1469 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1473 timeoutf->interval -= (the_time - timeoutf->start);
1474 timeoutf->start = the_time;
1475 tmp_list = tmp_list->next;
1479 if (current_timeouts)
1480 gtk_handle_current_timeouts (the_time);
1485 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1487 if (!quitf->marshal)
1488 return quitf->function (quitf->data);
1492 gint ret_val = FALSE;
1494 args[0].name = NULL;
1495 args[0].type = GTK_TYPE_BOOL;
1496 args[0].d.pointer_data = &ret_val;
1497 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1505 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1507 if (!idlef->marshal)
1508 return idlef->function (idlef->data);
1512 gint ret_val = FALSE;
1514 args[0].name = NULL;
1515 args[0].type = GTK_TYPE_BOOL;
1516 args[0].d.pointer_data = &ret_val;
1517 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1525 gtk_handle_current_idles (void)
1529 GtkIdleFunction *idlef;
1532 while (current_idles)
1534 tmp_list = current_idles;
1535 idlef = tmp_list->data;
1537 current_idles = g_list_remove_link (current_idles, tmp_list);
1540 running_idles->prev = tmp_list;
1541 tmp_list->next = running_idles;
1543 running_idles = tmp_list;
1545 result = gtk_idle_invoke_function (idlef);
1547 running_idles = g_list_remove_link (running_idles, tmp_list);
1548 idlef = tmp_list->data;
1550 if (!idlef || !result)
1552 g_list_free (tmp_list);
1554 gtk_idle_destroy (idlef);
1558 /* Insert the idle function back into the list of idle
1559 * functions at the end of the idles of this priority
1561 tmp_list2 = idle_functions;
1563 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1564 tmp_list2 = tmp_list2->next;
1567 idle_functions = g_list_concat (idle_functions, tmp_list);
1568 else if (tmp_list2 == idle_functions)
1570 tmp_list->next = idle_functions;
1572 idle_functions->prev = tmp_list;
1573 idle_functions = tmp_list;
1577 tmp_list->prev = tmp_list2->prev;
1578 tmp_list->next = tmp_list2;
1579 tmp_list2->prev->next = tmp_list;
1580 tmp_list2->prev = tmp_list;
1587 gtk_handle_idle (void)
1589 /* Caller must already have called gtk_handle_current_idles if
1592 g_assert (current_idles == NULL);
1594 /* Handle only the idle functions that have the highest priority */
1600 tmp_list = idle_functions;
1601 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1604 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1605 tmp_list = tmp_list->next;
1607 current_idles = idle_functions;
1608 idle_functions = tmp_list;
1612 tmp_list->prev->next = NULL;
1613 tmp_list->prev = NULL;
1616 gtk_handle_current_idles();
1621 gtk_handle_timer (void)
1623 GtkTimeoutFunction *timeoutf;
1628 gdk_timer_enable ();
1630 else if (timeout_functions)
1632 timeoutf = timeout_functions->data;
1633 gdk_timer_set (timeoutf->interval);
1634 gdk_timer_enable ();
1639 gdk_timer_disable ();
1644 gtk_propagate_event (GtkWidget *widget,
1649 g_return_if_fail (widget != NULL);
1650 g_return_if_fail (event != NULL);
1652 handled_event = FALSE;
1654 if ((event->type == GDK_KEY_PRESS) ||
1655 (event->type == GDK_KEY_RELEASE))
1657 /* Only send key events within Window widgets to the Window
1658 * The Window widget will in turn pass the
1659 * key event on to the currently focused widget
1664 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1667 if (GTK_WIDGET_IS_SENSITIVE (window))
1668 gtk_widget_event (window, event);
1670 handled_event = TRUE; /* don't send to widget */
1674 /* Other events get propagated up the widget tree
1675 * so that parents can see the button and motion
1676 * events of the children.
1678 while (!handled_event && widget)
1682 gtk_widget_ref (widget);
1683 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1684 tmp = widget->parent;
1685 gtk_widget_unref (widget);
1692 gtk_error (gchar *str)
1698 gtk_warning (gchar *str)
1704 gtk_message (gchar *str)
1710 gtk_print (gchar *str)
1712 static GtkWidget *window = NULL;
1713 static GtkWidget *text;
1714 static int level = 0;
1718 GtkWidget *hscrollbar;
1719 GtkWidget *vscrollbar;
1720 GtkWidget *separator;
1725 fputs (str, stdout);
1732 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1734 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1735 (GtkSignalFunc) gtk_widget_destroyed,
1738 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1740 box1 = gtk_vbox_new (FALSE, 0);
1741 gtk_container_add (GTK_CONTAINER (window), box1);
1742 gtk_widget_show (box1);
1745 box2 = gtk_vbox_new (FALSE, 10);
1746 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1747 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1748 gtk_widget_show (box2);
1751 table = gtk_table_new (2, 2, FALSE);
1752 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1753 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1754 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1755 gtk_widget_show (table);
1757 text = gtk_text_new (NULL, NULL);
1758 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1759 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1760 gtk_widget_show (text);
1761 gtk_widget_realize (text);
1763 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1764 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1765 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1766 gtk_widget_show (hscrollbar);
1768 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1769 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1770 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1771 gtk_widget_show (vscrollbar);
1773 separator = gtk_hseparator_new ();
1774 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1775 gtk_widget_show (separator);
1778 box2 = gtk_vbox_new (FALSE, 10);
1779 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1780 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1781 gtk_widget_show (box2);
1784 button = gtk_button_new_with_label ("close");
1785 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1786 (GtkSignalFunc) gtk_widget_hide,
1787 GTK_OBJECT (window));
1788 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1789 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1790 gtk_widget_grab_default (button);
1791 gtk_widget_show (button);
1795 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1798 if (!GTK_WIDGET_VISIBLE (window))
1799 gtk_widget_show (window);