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 "gtkhscrollbar.h"
25 #include "gtkhseparator.h"
27 #include "gtkpreview.h"
29 #include "gtkselection.h"
30 #include "gtksignal.h"
34 #include "gtkvscrollbar.h"
35 #include "gtkwidget.h"
36 #include "gtkwindow.h"
37 #include "gtkprivate.h"
38 #include "gdk/gdki18n.h"
39 #include "../config.h"
43 /* Private type definitions
45 typedef struct _GtkInitFunction GtkInitFunction;
46 typedef struct _GtkQuitFunction GtkQuitFunction;
47 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
48 typedef struct _GtkIdleFunction GtkIdleFunction;
49 typedef struct _GtkInputFunction GtkInputFunction;
50 typedef struct _GtkKeySnooperData GtkKeySnooperData;
52 struct _GtkInitFunction
58 struct _GtkQuitFunction
62 GtkCallbackMarshal marshal;
65 GtkDestroyNotify destroy;
68 struct _GtkTimeoutFunction
75 GtkCallbackMarshal marshal;
77 GtkDestroyNotify destroy;
80 struct _GtkIdleFunction
84 GtkCallbackMarshal marshal;
87 GtkDestroyNotify destroy;
90 struct _GtkInputFunction
92 GtkCallbackMarshal callback;
94 GtkDestroyNotify destroy;
97 struct _GtkKeySnooperData
104 static void gtk_exit_func (void);
105 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
106 static void gtk_quit_destroy (GtkQuitFunction *quitf);
107 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
108 static void gtk_handle_current_timeouts (guint32 the_time);
109 static void gtk_handle_current_idles (void);
110 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
112 static void gtk_handle_timeouts (void);
113 static void gtk_handle_idle (void);
114 static void gtk_handle_timer (void);
115 static void gtk_propagate_event (GtkWidget *widget,
117 static void gtk_error (gchar *str);
118 static void gtk_warning (gchar *str);
119 static void gtk_message (gchar *str);
120 static void gtk_print (gchar *str);
122 static gint gtk_idle_remove_from_list (GList **list,
126 static gint gtk_timeout_remove_from_list (GList **list,
130 static gint gtk_idle_compare (gpointer a,
133 static gint gtk_timeout_compare (gpointer a,
136 const guint gtk_major_version = GTK_MAJOR_VERSION;
137 const guint gtk_minor_version = GTK_MINOR_VERSION;
138 const guint gtk_micro_version = GTK_MICRO_VERSION;
140 static gboolean iteration_done = FALSE;
141 static guint main_level = 0;
142 static gint initialized = FALSE;
143 static GdkEvent *next_event = NULL;
144 static GList *current_events = NULL;
146 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
147 * widget is the first one on the list.
149 static GList *init_functions = NULL; /* A list of init functions.
151 static GList *quit_functions = NULL; /* A list of quit functions.
155 /* When handling timeouts, the algorithm is to move all of the expired
156 * timeouts from timeout_functions to current_timeouts then process
157 * them as a batch. If one of the timeouts recursively calls the main
158 * loop, then the remainder of the timeouts in current_timeouts will
159 * be processed before anything else happens.
161 * Each timeout is procesed as follows:
163 * - The timeout is removed from current_timeouts
164 * - The timeout is pushed on the running_timeouts stack
165 * - The timeout is executed
166 * - The timeout stack is popped
167 * - If the timeout function wasn't removed from the stack while executing,
168 * and it returned TRUE, it is added back to timeout_functions, otherwise
169 * it is destroyed if necessary.
171 * gtk_timeout_remove() works by checking for the timeout in
172 * timeout_functions current_timeouts and running_timeouts. If it is
173 * found in one of the first two, it is removed and destroyed. If it
174 * is found in running_timeouts, it is destroyed and ->data is set to
175 * NULL for the stack entry.
177 * Idle functions work pretty much identically.
180 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
181 * when the length of the time interval
182 * remaining. Therefore, the first timeout
183 * function to expire is at the head of
184 * the list and the last to expire is at
185 * the tail of the list. */
186 static GList *idle_functions = NULL; /* A list of idle functions.
189 /* The idle functions / timeouts that are currently being processed
190 * by gtk_handle_current_(timeouts/idles)
192 static GList *current_idles = NULL;
193 static GList *current_timeouts = NULL;
195 /* A stack of idle functions / timeouts that are currently being
198 static GList *running_idles = NULL;
199 static GList *running_timeouts = NULL;
201 static GMemChunk *timeout_mem_chunk = NULL;
202 static GMemChunk *idle_mem_chunk = NULL;
203 static GMemChunk *quit_mem_chunk = NULL;
205 static GSList *key_snoopers = NULL;
207 static GdkVisual *gtk_visual; /* The visual to be used in creating new
210 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
214 guint gtk_debug_flags = 0; /* Global GTK debug flag */
216 #ifdef G_ENABLE_DEBUG
217 static GDebugKey gtk_debug_keys[] = {
218 {"objects", GTK_DEBUG_OBJECTS},
219 {"misc", GTK_DEBUG_MISC}
222 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
224 #endif /* G_ENABLE_DEBUG */
226 gint gtk_use_mb = -1;
232 gchar *current_locale;
236 g_set_error_handler (gtk_error);
237 g_set_warning_handler (gtk_warning);
238 g_set_message_handler (gtk_message);
239 g_set_print_handler (gtk_print);
242 /* Initialize "gdk". We pass along the 'argc' and 'argv'
243 * parameters as they contain information that GDK uses
245 gdk_init (argc, argv);
247 #ifdef G_ENABLE_DEBUG
249 gchar *debug_string = getenv("GTK_DEBUG");
250 if (debug_string != NULL)
251 gtk_debug_flags = g_parse_debug_string (debug_string,
260 for (i = 1; i < *argc;)
262 if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
263 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
265 gchar *equal_pos = strchr ((*argv)[i], '=');
267 if (equal_pos != NULL)
269 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
273 else if ((i + 1) < *argc && (*argv)[i + 1])
275 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
283 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
284 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
286 gchar *equal_pos = strchr ((*argv)[i], '=');
288 if (equal_pos != NULL)
290 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
294 else if ((i + 1) < *argc && (*argv)[i + 1])
296 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
307 for (i = 1; i < *argc; i++)
309 for (k = i; k < *argc; k++)
310 if ((*argv)[k] != NULL)
316 for (j = i + k; j < *argc; j++)
317 (*argv)[j-k] = (*argv)[j];
323 #endif /* G_ENABLE_DEBUG */
325 /* Check if there is a good chance the mb functions will handle things
326 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
327 * C locale, or were using X's mb functions. (-DX_LOCALE && locale != C)
330 current_locale = g_strdup(setlocale (LC_CTYPE, NULL));
333 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
338 setlocale (LC_CTYPE, "C");
339 gtk_use_mb = (mblen ("\xc0", MB_CUR_MAX) == 1);
340 setlocale (LC_CTYPE, current_locale);
343 g_free (current_locale);
345 GTK_NOTE(MISC, g_print("%s multi-byte string functions.\n",
346 gtk_use_mb ? "Using" : "Not using"));
348 /* Initialize the default visual and colormap to be
349 * used in creating widgets. (We want to use the system
350 * defaults so as to be nice to the colormap).
352 gtk_visual = gdk_visual_get_system ();
353 gtk_colormap = gdk_colormap_get_system ();
358 /* Register an exit function to make sure we are able to cleanup.
360 if (ATEXIT (gtk_exit_func))
361 g_warning ("unable to register exit function");
363 /* Set the 'initialized' flag.
369 gtk_exit (int errorcode)
371 /* Only if "gtk" has been initialized should we de-initialize.
373 /* de-initialisation is done by the gtk_exit_funct(),
374 * no need to do this here (Alex J.)
382 return gdk_set_locale ();
390 GtkInitFunction *init;
395 tmp_list = functions = init_functions;
396 init_functions = NULL;
400 init = tmp_list->data;
401 tmp_list = tmp_list->next;
403 (* init->function) (init->data);
406 g_list_free (functions);
408 old_done = iteration_done;
409 while (!gtk_main_iteration ())
411 iteration_done = old_done;
415 GList *reinvoke_list = NULL;
416 GtkQuitFunction *quitf;
418 while (quit_functions)
420 quitf = quit_functions->data;
422 quit_functions = g_list_remove_link (quit_functions, quit_functions);
424 if ((quitf->main_level && quitf->main_level != main_level) ||
425 gtk_quit_invoke_function (quitf))
427 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
431 g_list_free (tmp_list);
432 gtk_quit_destroy (quitf);
439 tmp_list = g_list_last (reinvoke_list);
441 quit_functions->prev = tmp_list;
442 tmp_list->next = quit_functions;
443 quit_functions = tmp_list;
451 gtk_main_level (void)
459 iteration_done = TRUE;
463 gtk_events_pending (void)
467 /* if this function is called from a timeout which will only return
468 * if gtk needs processor time, we need to take iteration_done==TRUE
469 * into account as well.
471 result = iteration_done;
472 result += next_event != NULL;
473 result += gdk_events_pending();
475 result += current_idles != NULL;
476 result += current_timeouts != NULL;
480 result += (idle_functions &&
481 (((GtkIdleFunction *)idle_functions->data)->priority <=
482 GTK_PRIORITY_INTERNAL));
485 if (!result && timeout_functions)
488 GtkTimeoutFunction *timeoutf;
490 the_time = gdk_time_get ();
492 timeoutf = timeout_functions->data;
494 result += timeoutf->interval <= (the_time - timeoutf->start);
501 gtk_main_iteration ()
503 return gtk_main_iteration_do (TRUE);
507 gtk_main_iteration_do (gboolean blocking)
509 GtkWidget *event_widget;
510 GtkWidget *grab_widget;
511 GdkEvent *event = NULL;
514 iteration_done = FALSE;
516 /* If this is a recursive call, and there are pending timeouts or
517 * idles, finish them, then return immediately to avoid blocking
520 if (current_timeouts)
522 gtk_handle_current_timeouts( gdk_time_get());
527 return iteration_done;
531 gtk_handle_current_idles ();
536 return iteration_done;
539 /* If there is a valid event in 'next_event' then move it to 'event'
547 /* If we don't have an event then get one.
551 /* Handle setting of the "gdk" timer. If there are no
552 * timeout functions, then the timer is turned off.
553 * If there are timeout functions, then the timer is
554 * set to the shortest timeout interval (which is
555 * the first timeout function).
559 if (blocking) event = gdk_event_get ();
562 /* "gdk_event_get" can return FALSE if the timer goes off
563 * and no events are pending. Therefore, we should make
564 * sure that we got an event before continuing.
568 /* If there are any events pending then get the next one.
570 if (gdk_events_pending () > 0)
571 next_event = gdk_event_get ();
573 /* Try to compress enter/leave notify events. These event
574 * pairs occur when the mouse is dragged quickly across
575 * a window with many buttons (or through a menu). Instead
576 * of highlighting and de-highlighting each widget that
577 * is crossed it is better to simply de-highlight the widget
578 * which contained the mouse initially and highlight the
579 * widget which ends up containing the mouse.
582 if (((event->type == GDK_ENTER_NOTIFY) ||
583 (event->type == GDK_LEAVE_NOTIFY)) &&
584 ((next_event->type == GDK_ENTER_NOTIFY) ||
585 (next_event->type == GDK_LEAVE_NOTIFY)) &&
586 (next_event->type != event->type) &&
587 (next_event->any.window == event->any.window))
589 gdk_event_free (event);
590 gdk_event_free (next_event);
593 goto event_handling_done;
596 /* Find the widget which got the event. We store the widget
597 * in the user_data field of GdkWindow's.
598 * Ignore the event if we don't have a widget for it, except
599 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
600 * Though this happens rarely, bogus events can occour
601 * for e.g. destroyed GdkWindows.
603 event_widget = gtk_get_event_widget (event);
606 /* To handle selection INCR transactions, we select
607 * PropertyNotify events on the requestor window and create
608 * a corresponding (fake) GdkWindow so that events get
609 * here. There won't be a widget though, so we have to handle
612 if (event->type == GDK_PROPERTY_NOTIFY)
613 gtk_selection_incr_event (event->any.window,
616 gdk_event_free (event);
618 goto event_handling_done;
621 /* Push the event onto a stack of current events for
622 * gtk_current_event_get().
624 current_events = g_list_prepend (current_events, event);
626 /* If there is a grab in effect...
630 grab_widget = grabs->data;
632 /* If the grab widget is an ancestor of the event widget
633 * then we send the event to the original event widget.
634 * This is the key to implementing modality.
636 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
637 gtk_widget_is_ancestor (event_widget, grab_widget))
638 grab_widget = event_widget;
642 grab_widget = event_widget;
645 /* Not all events get sent to the grabbing widget.
646 * The delete, destroy, expose, focus change and resize
647 * events still get sent to the event widget because
648 * 1) these events have no meaning for the grabbing widget
649 * and 2) redirecting these events to the grabbing widget
650 * could cause the display to be messed up.
658 gtk_widget_ref (event_widget);
659 if (!gtk_widget_event (event_widget, event) &&
660 !GTK_OBJECT_DESTROYED (event_widget))
661 gtk_widget_destroy (event_widget);
662 gtk_widget_unref (event_widget);
666 gtk_widget_ref (event_widget);
667 gtk_widget_event (event_widget, event);
668 if (!GTK_OBJECT_DESTROYED (event_widget))
669 gtk_widget_destroy (event_widget);
670 gtk_widget_unref (event_widget);
673 case GDK_PROPERTY_NOTIFY:
676 case GDK_FOCUS_CHANGE:
680 case GDK_SELECTION_CLEAR:
681 case GDK_SELECTION_REQUEST:
682 case GDK_SELECTION_NOTIFY:
683 case GDK_CLIENT_EVENT:
685 case GDK_DRAG_REQUEST:
688 case GDK_DROP_DATA_AVAIL:
689 case GDK_VISIBILITY_NOTIFY:
690 gtk_widget_event (event_widget, event);
694 case GDK_KEY_RELEASE:
697 if (gtk_invoke_key_snoopers (grab_widget, event))
700 /* else fall through */
701 case GDK_MOTION_NOTIFY:
702 case GDK_BUTTON_PRESS:
703 case GDK_2BUTTON_PRESS:
704 case GDK_3BUTTON_PRESS:
705 case GDK_BUTTON_RELEASE:
706 case GDK_PROXIMITY_IN:
707 case GDK_PROXIMITY_OUT:
708 case GDK_OTHER_EVENT:
709 gtk_propagate_event (grab_widget, event);
712 case GDK_ENTER_NOTIFY:
713 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
715 gtk_widget_event (grab_widget, event);
716 if (event_widget == grab_widget)
717 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
721 case GDK_LEAVE_NOTIFY:
722 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
724 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
725 gtk_widget_event (event_widget, event);
727 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
728 gtk_widget_event (grab_widget, event);
732 tmp_list = current_events;
733 current_events = g_list_remove_link (current_events, tmp_list);
734 g_list_free_1 (tmp_list);
736 gdk_event_free (event);
740 if (gdk_events_pending() == 0)
746 /* Handle timeout functions that may have expired.
748 gtk_handle_timeouts ();
753 return iteration_done;
769 gtk_grab_add (GtkWidget *widget)
771 g_return_if_fail (widget != NULL);
773 if (!GTK_WIDGET_HAS_GRAB (widget))
775 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
777 grabs = g_slist_prepend (grabs, widget);
778 gtk_widget_ref (widget);
783 gtk_grab_get_current (void)
786 return GTK_WIDGET (grabs->data);
791 gtk_grab_remove (GtkWidget *widget)
793 g_return_if_fail (widget != NULL);
795 if (GTK_WIDGET_HAS_GRAB (widget))
797 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
799 grabs = g_slist_remove (grabs, widget);
800 gtk_widget_unref (widget);
805 gtk_init_add (GtkFunction function,
808 GtkInitFunction *init;
810 init = g_new (GtkInitFunction, 1);
811 init->function = function;
814 init_functions = g_list_prepend (init_functions, init);
818 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
821 GtkKeySnooperData *data;
822 static guint snooper_id = 1;
824 g_return_val_if_fail (snooper != NULL, 0);
826 data = g_new (GtkKeySnooperData, 1);
827 data->func = snooper;
828 data->func_data = func_data;
829 data->id = snooper_id++;
830 key_snoopers = g_slist_prepend (key_snoopers, data);
836 gtk_key_snooper_remove (guint snooper_id)
838 GtkKeySnooperData *data = NULL;
841 slist = key_snoopers;
845 if (data->id == snooper_id)
852 key_snoopers = g_slist_remove (key_snoopers, data);
856 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
860 gint return_val = FALSE;
862 slist = key_snoopers;
863 while (slist && !return_val)
865 GtkKeySnooperData *data;
869 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
876 gtk_timeout_add_full (guint32 interval,
877 GtkFunction function,
878 GtkCallbackMarshal marshal,
880 GtkDestroyNotify destroy)
882 static guint timeout_tag = 1;
883 GtkTimeoutFunction *timeoutf;
885 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
887 /* Create a new timeout function structure.
888 * The start time is the current time.
890 if (!timeout_mem_chunk)
891 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
892 1024, G_ALLOC_AND_FREE);
894 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
896 timeoutf->tag = timeout_tag++;
897 timeoutf->start = gdk_time_get ();
898 timeoutf->interval = interval;
899 timeoutf->originterval = interval;
900 timeoutf->function = function;
901 timeoutf->marshal = marshal;
902 timeoutf->data = data;
903 timeoutf->destroy = destroy;
905 gtk_timeout_insert (timeoutf);
907 return timeoutf->tag;
911 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
913 if (timeoutf->destroy)
914 (timeoutf->destroy) (timeoutf->data);
915 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
919 gtk_timeout_add (guint32 interval,
920 GtkFunction function,
923 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
927 gtk_timeout_add_interp (guint32 interval,
928 GtkCallbackMarshal function,
930 GtkDestroyNotify destroy)
932 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
935 /* Search for the specified tag in a list of timeouts. If it
936 * is found, destroy the timeout, and either remove the link
937 * or set link->data to NULL depending on remove_link
940 gtk_timeout_remove_from_list (GList **list, guint tag, gint remove_link)
943 GtkTimeoutFunction *timeoutf;
948 timeoutf = tmp_list->data;
950 if (timeoutf->tag == tag)
954 *list = g_list_remove_link (*list, tmp_list);
955 g_list_free (tmp_list);
958 tmp_list->data = NULL;
960 gtk_timeout_destroy (timeoutf);
965 tmp_list = tmp_list->next;
971 gtk_timeout_remove (guint tag)
974 /* Remove a timeout function.
975 * (Which, basically, involves searching the
979 if (gtk_timeout_remove_from_list (&timeout_functions, tag, TRUE))
981 if (gtk_timeout_remove_from_list (¤t_timeouts, tag, TRUE))
983 if (gtk_timeout_remove_from_list (&running_timeouts, tag, FALSE))
987 /* We rely on some knowledge of how g_list_insert_sorted works to make
988 * sure that we insert at the _end_ of the idles of this priority
991 gtk_idle_compare (gpointer a, gpointer b)
993 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
998 gtk_quit_add_full (guint main_level,
999 GtkFunction function,
1000 GtkCallbackMarshal marshal,
1002 GtkDestroyNotify destroy)
1004 static guint quit_id = 1;
1005 GtkQuitFunction *quitf;
1007 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1009 if (!quit_mem_chunk)
1010 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1011 512, G_ALLOC_AND_FREE);
1013 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1015 quitf->id = quit_id++;
1016 quitf->main_level = main_level;
1017 quitf->function = function;
1018 quitf->marshal = marshal;
1020 quitf->destroy = destroy;
1022 quit_functions = g_list_prepend (quit_functions, quitf);
1028 gtk_idle_add_full (gint priority,
1029 GtkFunction function,
1030 GtkCallbackMarshal marshal,
1032 GtkDestroyNotify destroy)
1034 static guint idle_tag = 1;
1035 GtkIdleFunction *idlef;
1037 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1039 if (!idle_mem_chunk)
1040 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
1041 1024, G_ALLOC_AND_FREE);
1043 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
1045 idlef->tag = idle_tag++;
1046 idlef->priority = priority;
1047 idlef->function = function;
1048 idlef->marshal = marshal;
1050 idlef->destroy = destroy;
1052 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
1058 gtk_idle_add_interp (GtkCallbackMarshal marshal,
1060 GtkDestroyNotify destroy)
1062 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
1066 gtk_idle_destroy (GtkIdleFunction *idlef)
1069 idlef->destroy (idlef->data);
1070 g_mem_chunk_free (idle_mem_chunk, idlef);
1074 gtk_quit_destroy (GtkQuitFunction *quitf)
1077 quitf->destroy (quitf->data);
1078 g_mem_chunk_free (quit_mem_chunk, quitf);
1082 gtk_quit_destructor (GtkObject **object_p)
1085 gtk_object_destroy (*object_p);
1092 gtk_quit_add_destroy (guint main_level,
1095 GtkObject **object_p;
1097 g_return_if_fail (main_level > 0);
1098 g_return_if_fail (object != NULL);
1099 g_return_if_fail (GTK_IS_OBJECT (object));
1101 object_p = g_new (GtkObject*, 1);
1103 gtk_signal_connect (object,
1105 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1107 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1111 gtk_quit_add (guint main_level,
1112 GtkFunction function,
1115 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1119 gtk_idle_add (GtkFunction function,
1122 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1126 gtk_idle_add_priority (gint priority,
1127 GtkFunction function,
1130 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1134 gtk_quit_remove (guint id)
1136 GtkQuitFunction *quitf;
1139 tmp_list = quit_functions;
1142 quitf = tmp_list->data;
1144 if (quitf->id == id)
1146 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1147 g_list_free (tmp_list);
1148 gtk_quit_destroy (quitf);
1153 tmp_list = tmp_list->next;
1158 gtk_quit_remove_by_data (gpointer data)
1160 GtkQuitFunction *quitf;
1163 tmp_list = quit_functions;
1166 quitf = tmp_list->data;
1168 if (quitf->data == data)
1170 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1171 g_list_free (tmp_list);
1172 gtk_quit_destroy (quitf);
1177 tmp_list = tmp_list->next;
1181 /* Search for the specified tag in a list of idles. If it
1182 * is found, destroy the idle, and either remove the link
1183 * or set link->data to NULL depending on remove_link
1185 * If tag != 0, match tag against idlef->tag, otherwise, match
1186 * data against idlef->data
1189 gtk_idle_remove_from_list (GList **list,
1194 GtkIdleFunction *idlef;
1200 idlef = tmp_list->data;
1202 if (((tag != 0) && (idlef->tag == tag)) ||
1203 ((tag == 0) && (idlef->data == data)))
1207 *list = g_list_remove_link (*list, tmp_list);
1208 g_list_free (tmp_list);
1211 tmp_list->data = NULL;
1213 gtk_idle_destroy (idlef);
1218 tmp_list = tmp_list->next;
1224 gtk_idle_remove (guint tag)
1226 g_return_if_fail (tag != 0);
1228 if (gtk_idle_remove_from_list (&idle_functions, tag, NULL, TRUE))
1230 if (gtk_idle_remove_from_list (¤t_idles, tag, NULL, TRUE))
1232 if (gtk_idle_remove_from_list (&running_idles, tag, NULL, FALSE))
1237 gtk_idle_remove_by_data (gpointer data)
1239 if (gtk_idle_remove_from_list (&idle_functions, 0, data, TRUE))
1241 if (gtk_idle_remove_from_list (¤t_idles, 0, data, TRUE))
1243 if (gtk_idle_remove_from_list (&running_idles, 0, data, FALSE))
1248 gtk_invoke_input_function (GtkInputFunction *input,
1250 GdkInputCondition condition)
1253 args[0].type = GTK_TYPE_INT;
1254 args[0].name = NULL;
1255 GTK_VALUE_INT(args[0]) = source;
1256 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1257 args[1].name = NULL;
1258 GTK_VALUE_FLAGS(args[1]) = condition;
1259 args[2].type = GTK_TYPE_NONE;
1260 args[2].name = NULL;
1262 input->callback (NULL, input->data, 2, args);
1266 gtk_destroy_input_function (GtkInputFunction *input)
1269 (input->destroy) (input->data);
1274 gtk_input_add_full (gint source,
1275 GdkInputCondition condition,
1276 GdkInputFunction function,
1277 GtkCallbackMarshal marshal,
1279 GtkDestroyNotify destroy)
1283 GtkInputFunction *input;
1285 input = g_new (GtkInputFunction, 1);
1286 input->callback = marshal;
1288 input->destroy = destroy;
1290 return gdk_input_add_full (source,
1292 (GdkInputFunction) gtk_invoke_input_function,
1294 (GdkDestroyNotify) gtk_destroy_input_function);
1297 return gdk_input_add_full (source, condition, function, data, destroy);
1301 gtk_input_add_interp (gint source,
1302 GdkInputCondition condition,
1303 GtkCallbackMarshal callback,
1305 GtkDestroyNotify destroy)
1307 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1311 gtk_input_remove (guint tag)
1313 gdk_input_remove (tag);
1317 gtk_get_current_event ()
1320 return gdk_event_copy ((GdkEvent *) current_events->data);
1326 gtk_get_event_widget (GdkEvent *event)
1331 if (event && event->any.window)
1332 gdk_window_get_user_data (event->any.window, (void**) &widget);
1342 initialized = FALSE;
1343 gtk_preview_uninit ();
1348 /* We rely on some knowledge of how g_list_insert_sorted works to make
1349 * sure that we insert after timeouts of equal interval
1352 gtk_timeout_compare (gpointer a, gpointer b)
1354 return (((GtkTimeoutFunction *)a)->interval <
1355 ((GtkTimeoutFunction *)b)->interval)
1360 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1362 g_assert (timeoutf != NULL);
1364 /* Insert the timeout function appropriately.
1365 * Appropriately meaning sort it into the list
1366 * of timeout functions.
1368 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1369 gtk_timeout_compare);
1373 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1375 if (!timeoutf->marshal)
1376 return timeoutf->function (timeoutf->data);
1380 gint ret_val = FALSE;
1381 args[0].name = NULL;
1382 args[0].type = GTK_TYPE_BOOL;
1383 args[0].d.pointer_data = &ret_val;
1384 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1390 gtk_handle_current_timeouts (guint32 the_time)
1394 GtkTimeoutFunction *timeoutf;
1396 while (current_timeouts)
1398 tmp_list = current_timeouts;
1399 timeoutf = tmp_list->data;
1401 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1402 if (running_timeouts)
1404 running_timeouts->prev = tmp_list;
1405 tmp_list->next = running_timeouts;
1407 running_timeouts = tmp_list;
1409 result = gtk_invoke_timeout_function (timeoutf);
1411 running_timeouts = g_list_remove_link (running_timeouts, tmp_list);
1412 timeoutf = tmp_list->data;
1414 g_list_free_1 (tmp_list);
1420 gtk_timeout_destroy (timeoutf);
1424 timeoutf->interval = timeoutf->originterval;
1425 timeoutf->start = the_time;
1426 gtk_timeout_insert (timeoutf);
1433 gtk_handle_timeouts ()
1438 GtkTimeoutFunction *timeoutf;
1440 /* Caller must already have called gtk_handle_current_timeouts if
1442 g_assert (current_timeouts == NULL);
1444 if (timeout_functions)
1446 the_time = gdk_time_get ();
1448 tmp_list = timeout_functions;
1451 timeoutf = tmp_list->data;
1453 if (timeoutf->interval <= (the_time - timeoutf->start))
1455 tmp_list2 = tmp_list;
1456 tmp_list = tmp_list->next;
1458 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1459 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1463 timeoutf->interval -= (the_time - timeoutf->start);
1464 timeoutf->start = the_time;
1465 tmp_list = tmp_list->next;
1469 if (current_timeouts)
1470 gtk_handle_current_timeouts (the_time);
1475 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1477 if (!quitf->marshal)
1478 return quitf->function (quitf->data);
1482 gint ret_val = FALSE;
1484 args[0].name = NULL;
1485 args[0].type = GTK_TYPE_BOOL;
1486 args[0].d.pointer_data = &ret_val;
1487 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1495 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1497 if (!idlef->marshal)
1498 return idlef->function (idlef->data);
1502 gint ret_val = FALSE;
1504 args[0].name = NULL;
1505 args[0].type = GTK_TYPE_BOOL;
1506 args[0].d.pointer_data = &ret_val;
1507 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1515 gtk_handle_current_idles ()
1519 GtkIdleFunction *idlef;
1522 while (current_idles)
1524 tmp_list = current_idles;
1525 idlef = tmp_list->data;
1527 current_idles = g_list_remove_link (current_idles, tmp_list);
1530 running_idles->prev = tmp_list;
1531 tmp_list->next = running_idles;
1533 running_idles = tmp_list;
1535 result = gtk_idle_invoke_function (idlef);
1537 running_idles = g_list_remove_link (running_idles, tmp_list);
1538 idlef = tmp_list->data;
1540 if (!idlef || !result)
1542 g_list_free (tmp_list);
1544 gtk_idle_destroy (idlef);
1548 /* Insert the idle function back into the list of idle
1549 * functions at the end of the idles of this priority
1551 tmp_list2 = idle_functions;
1553 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1554 tmp_list2 = tmp_list2->next;
1557 idle_functions = g_list_concat (idle_functions, tmp_list);
1558 else if (tmp_list2 == idle_functions)
1560 tmp_list->next = idle_functions;
1562 idle_functions->prev = tmp_list;
1563 idle_functions = tmp_list;
1567 tmp_list->prev = tmp_list2->prev;
1568 tmp_list->next = tmp_list2;
1569 tmp_list2->prev->next = tmp_list;
1570 tmp_list2->prev = tmp_list;
1579 /* Caller must already have called gtk_handle_current_idles if
1582 g_assert (current_idles == NULL);
1584 /* Handle only the idle functions that have the highest priority */
1590 tmp_list = idle_functions;
1591 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1594 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1595 tmp_list = tmp_list->next;
1597 current_idles = idle_functions;
1598 idle_functions = tmp_list;
1602 tmp_list->prev->next = NULL;
1603 tmp_list->prev = NULL;
1606 gtk_handle_current_idles();
1613 GtkTimeoutFunction *timeoutf;
1618 gdk_timer_enable ();
1620 else if (timeout_functions)
1622 timeoutf = timeout_functions->data;
1623 gdk_timer_set (timeoutf->interval);
1624 gdk_timer_enable ();
1629 gdk_timer_disable ();
1634 gtk_propagate_event (GtkWidget *widget,
1639 g_return_if_fail (widget != NULL);
1640 g_return_if_fail (event != NULL);
1642 handled_event = FALSE;
1644 if ((event->type == GDK_KEY_PRESS) ||
1645 (event->type == GDK_KEY_RELEASE))
1647 /* Only send key events within Window widgets to the Window
1648 * The Window widget will in turn pass the
1649 * key event on to the currently focused widget
1654 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1657 if (GTK_WIDGET_IS_SENSITIVE (window))
1658 gtk_widget_event (window, event);
1660 handled_event = TRUE; /* don't send to widget */
1664 /* Other events get propagated up the widget tree
1665 * so that parents can see the button and motion
1666 * events of the children.
1668 while (!handled_event && widget)
1672 gtk_widget_ref (widget);
1673 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1674 tmp = widget->parent;
1675 gtk_widget_unref (widget);
1682 gtk_error (gchar *str)
1688 gtk_warning (gchar *str)
1694 gtk_message (gchar *str)
1700 gtk_print (gchar *str)
1702 static GtkWidget *window = NULL;
1703 static GtkWidget *text;
1704 static int level = 0;
1708 GtkWidget *hscrollbar;
1709 GtkWidget *vscrollbar;
1710 GtkWidget *separator;
1715 fputs (str, stdout);
1722 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1724 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1725 (GtkSignalFunc) gtk_widget_destroyed,
1728 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1730 box1 = gtk_vbox_new (FALSE, 0);
1731 gtk_container_add (GTK_CONTAINER (window), box1);
1732 gtk_widget_show (box1);
1735 box2 = gtk_vbox_new (FALSE, 10);
1736 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1737 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1738 gtk_widget_show (box2);
1741 table = gtk_table_new (2, 2, FALSE);
1742 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1743 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1744 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1745 gtk_widget_show (table);
1747 text = gtk_text_new (NULL, NULL);
1748 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1749 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1750 gtk_widget_show (text);
1751 gtk_widget_realize (text);
1753 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1754 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1755 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1756 gtk_widget_show (hscrollbar);
1758 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1759 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1760 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1761 gtk_widget_show (vscrollbar);
1763 separator = gtk_hseparator_new ();
1764 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1765 gtk_widget_show (separator);
1768 box2 = gtk_vbox_new (FALSE, 10);
1769 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1770 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1771 gtk_widget_show (box2);
1774 button = gtk_button_new_with_label ("close");
1775 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1776 (GtkSignalFunc) gtk_widget_hide,
1777 GTK_OBJECT (window));
1778 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1779 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1780 gtk_widget_grab_default (button);
1781 gtk_widget_show (button);
1785 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1788 if (!GTK_WIDGET_VISIBLE (window))
1789 gtk_widget_show (window);