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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <X11/Xlocale.h> /* so we get the right setlocale */
21 #include "gtkbutton.h"
22 #include "gtkhscrollbar.h"
23 #include "gtkhseparator.h"
25 #include "gtkpreview.h"
27 #include "gtkselection.h"
28 #include "gtksignal.h"
32 #include "gtkvscrollbar.h"
33 #include "gtkwidget.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
36 #include "gdk/gdki18n.h"
37 #include "../config.h"
41 /* Private type definitions
43 typedef struct _GtkInitFunction GtkInitFunction;
44 typedef struct _GtkQuitFunction GtkQuitFunction;
45 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
46 typedef struct _GtkIdleFunction GtkIdleFunction;
47 typedef struct _GtkInputFunction GtkInputFunction;
48 typedef struct _GtkKeySnooperData GtkKeySnooperData;
50 struct _GtkInitFunction
56 struct _GtkQuitFunction
60 GtkCallbackMarshal marshal;
63 GtkDestroyNotify destroy;
66 struct _GtkTimeoutFunction
73 GtkCallbackMarshal marshal;
75 GtkDestroyNotify destroy;
78 struct _GtkIdleFunction
82 GtkCallbackMarshal marshal;
85 GtkDestroyNotify destroy;
88 struct _GtkInputFunction
90 GtkCallbackMarshal callback;
92 GtkDestroyNotify destroy;
95 struct _GtkKeySnooperData
102 static void gtk_exit_func (void);
103 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
104 static void gtk_quit_destroy (GtkQuitFunction *quitf);
105 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
106 static void gtk_handle_current_timeouts (guint32 the_time);
107 static void gtk_handle_current_idles (void);
108 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
110 static void gtk_handle_timeouts (void);
111 static void gtk_handle_idle (void);
112 static void gtk_handle_timer (void);
113 static void gtk_propagate_event (GtkWidget *widget,
115 static void gtk_error (gchar *str);
116 static void gtk_warning (gchar *str);
117 static void gtk_message (gchar *str);
118 static void gtk_print (gchar *str);
120 static gint gtk_idle_compare (gpointer a,
123 static gint gtk_timeout_compare (gpointer a,
126 const guint gtk_major_version = GTK_MAJOR_VERSION;
127 const guint gtk_minor_version = GTK_MINOR_VERSION;
128 const guint gtk_micro_version = GTK_MICRO_VERSION;
130 static gboolean iteration_done = FALSE;
131 static guint main_level = 0;
132 static gint initialized = FALSE;
133 static GdkEvent *next_event = NULL;
134 static GList *current_events = NULL;
136 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
137 * widget is the first one on the list.
139 static GList *init_functions = NULL; /* A list of init functions.
141 static GList *quit_functions = NULL; /* A list of quit functions.
143 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
144 * when the length of the time interval
145 * remaining. Therefore, the first timeout
146 * function to expire is at the head of
147 * the list and the last to expire is at
148 * the tail of the list.
150 static GList *idle_functions = NULL; /* A list of idle functions.
153 static GList *current_idles = NULL;
154 static GList *current_timeouts = NULL;
155 static GtkIdleFunction *running_idle = NULL;
156 static GtkTimeoutFunction *running_timeout = NULL;
157 static GMemChunk *timeout_mem_chunk = NULL;
158 static GMemChunk *idle_mem_chunk = NULL;
159 static GMemChunk *quit_mem_chunk = NULL;
161 static GSList *key_snoopers = NULL;
163 static GdkVisual *gtk_visual; /* The visual to be used in creating new
166 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
170 guint gtk_debug_flags = 0; /* Global GTK debug flag */
172 #ifdef G_ENABLE_DEBUG
173 static GDebugKey gtk_debug_keys[] = {
174 {"objects", GTK_DEBUG_OBJECTS},
175 {"misc", GTK_DEBUG_MISC}
178 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
180 #endif /* G_ENABLE_DEBUG */
182 gint gtk_use_mb = -1;
188 gchar *current_locale;
192 g_set_error_handler (gtk_error);
193 g_set_warning_handler (gtk_warning);
194 g_set_message_handler (gtk_message);
195 g_set_print_handler (gtk_print);
198 /* Initialize "gdk". We pass along the 'argc' and 'argv'
199 * parameters as they contain information that GDK uses
201 gdk_init (argc, argv);
203 #ifdef G_ENABLE_DEBUG
205 gchar *debug_string = getenv("GTK_DEBUG");
206 if (debug_string != NULL)
207 gtk_debug_flags = g_parse_debug_string (debug_string,
216 for (i = 1; i < *argc;)
218 if (strcmp ("--gtk-debug", (*argv)[i]) == 0)
222 if ((i + 1) < *argc && (*argv)[i + 1])
224 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
227 (*argv)[i + 1] = NULL;
231 else if (strcmp ("--gtk-no-debug", (*argv)[i]) == 0)
235 if ((i + 1) < *argc && (*argv)[i + 1])
237 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
240 (*argv)[i + 1] = NULL;
247 for (i = 1; i < *argc; i++)
249 for (k = i; k < *argc; k++)
250 if ((*argv)[k] != NULL)
256 for (j = i + k; j < *argc; j++)
257 (*argv)[j-k] = (*argv)[j];
263 #endif /* G_ENABLE_DEBUG */
265 /* Check if there is a good chance the mb functions will handle things
266 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
267 * C locale, or were using X's mb functions. (-DX_LOCALE && locale != C)
270 current_locale = g_strdup(setlocale (LC_CTYPE, NULL));
273 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
278 setlocale (LC_CTYPE, "C");
279 gtk_use_mb = (mblen ("\xc0", MB_CUR_MAX) == 1);
280 setlocale (LC_CTYPE, current_locale);
283 g_free (current_locale);
285 GTK_NOTE(MISC, g_print("%s multi-byte string functions.\n",
286 gtk_use_mb ? "Using" : "Not using"));
288 /* Initialize the default visual and colormap to be
289 * used in creating widgets. (We want to use the system
290 * defaults so as to be nice to the colormap).
292 gtk_visual = gdk_visual_get_system ();
293 gtk_colormap = gdk_colormap_get_system ();
298 /* Register an exit function to make sure we are able to cleanup.
300 if (ATEXIT (gtk_exit_func))
301 g_warning ("unable to register exit function");
303 /* Set the 'initialized' flag.
309 gtk_exit (int errorcode)
311 /* Only if "gtk" has been initialized should we de-initialize.
313 /* de-initialisation is done by the gtk_exit_funct(),
314 * no need to do this here (Alex J.)
322 return gdk_set_locale ();
330 GtkInitFunction *init;
335 tmp_list = functions = init_functions;
336 init_functions = NULL;
340 init = tmp_list->data;
341 tmp_list = tmp_list->next;
343 (* init->function) (init->data);
346 g_list_free (functions);
348 old_done = iteration_done;
349 while (!gtk_main_iteration ())
351 iteration_done = old_done;
355 GList *reinvoke_list = NULL;
356 GtkQuitFunction *quitf;
358 while (quit_functions)
360 quitf = quit_functions->data;
362 quit_functions = g_list_remove_link (quit_functions, quit_functions);
364 if ((quitf->main_level && quitf->main_level != main_level) ||
365 gtk_quit_invoke_function (quitf))
367 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
371 g_list_free (tmp_list);
372 gtk_quit_destroy (quitf);
379 tmp_list = g_list_last (reinvoke_list);
381 quit_functions->prev = tmp_list;
382 tmp_list->next = quit_functions;
383 quit_functions = tmp_list;
391 gtk_main_level (void)
399 iteration_done = TRUE;
403 gtk_events_pending (void)
405 gint result = gdk_events_pending() + ((next_event != NULL) ? 1 : 0);
407 if (idle_functions &&
408 (((GtkIdleFunction *)idle_functions->data)->priority <=
409 GTK_PRIORITY_INTERNAL))
416 gtk_main_iteration ()
418 return gtk_main_iteration_do (TRUE);
422 gtk_main_iteration_do (gboolean blocking)
424 GtkWidget *event_widget;
425 GtkWidget *grab_widget;
426 GdkEvent *event = NULL;
429 iteration_done = FALSE;
431 /* If this is a recursive call, and there are pending timeouts or
432 * idles, finish them, then return immediately to avoid blocking
435 if (current_timeouts)
437 gtk_handle_current_timeouts( gdk_time_get());
438 return iteration_done;
442 gtk_handle_current_idles ();
443 return iteration_done;
446 /* If there is a valid event in 'next_event' then move it to 'event'
454 /* If we don't have an event then get one.
458 /* Handle setting of the "gdk" timer. If there are no
459 * timeout functions, then the timer is turned off.
460 * If there are timeout functions, then the timer is
461 * set to the shortest timeout interval (which is
462 * the first timeout function).
466 if (blocking) event = gdk_event_get ();
469 /* "gdk_event_get" can return FALSE if the timer goes off
470 * and no events are pending. Therefore, we should make
471 * sure that we got an event before continuing.
475 /* If there are any events pending then get the next one.
477 if (gdk_events_pending () > 0)
478 next_event = gdk_event_get ();
480 /* Try to compress enter/leave notify events. These event
481 * pairs occur when the mouse is dragged quickly across
482 * a window with many buttons (or through a menu). Instead
483 * of highlighting and de-highlighting each widget that
484 * is crossed it is better to simply de-highlight the widget
485 * which contained the mouse initially and highlight the
486 * widget which ends up containing the mouse.
489 if (((event->type == GDK_ENTER_NOTIFY) ||
490 (event->type == GDK_LEAVE_NOTIFY)) &&
491 ((next_event->type == GDK_ENTER_NOTIFY) ||
492 (next_event->type == GDK_LEAVE_NOTIFY)) &&
493 (next_event->type != event->type) &&
494 (next_event->any.window == event->any.window))
496 gdk_event_free (event);
497 gdk_event_free (next_event);
500 goto event_handling_done;
503 /* Find the widget which got the event. We store the widget
504 * in the user_data field of GdkWindow's.
505 * Ignore the event if we don't have a widget for it, except
506 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
507 * Though this happens rarely, bogus events can occour
508 * for e.g. destroyed GdkWindows.
510 event_widget = gtk_get_event_widget (event);
513 /* To handle selection INCR transactions, we select
514 * PropertyNotify events on the requestor window and create
515 * a corresponding (fake) GdkWindow so that events get
516 * here. There won't be a widget though, so we have to handle
519 if (event->type == GDK_PROPERTY_NOTIFY)
520 gtk_selection_incr_event (event->any.window,
523 gdk_event_free (event);
525 goto event_handling_done;
528 /* Push the event onto a stack of current events for
529 * gtk_current_event_get().
531 current_events = g_list_prepend (current_events, event);
533 /* If there is a grab in effect...
537 grab_widget = grabs->data;
539 /* If the grab widget is an ancestor of the event widget
540 * then we send the event to the original event widget.
541 * This is the key to implementing modality.
543 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
544 gtk_widget_is_ancestor (event_widget, grab_widget))
545 grab_widget = event_widget;
549 grab_widget = event_widget;
552 /* Not all events get sent to the grabbing widget.
553 * The delete, destroy, expose, focus change and resize
554 * events still get sent to the event widget because
555 * 1) these events have no meaning for the grabbing widget
556 * and 2) redirecting these events to the grabbing widget
557 * could cause the display to be messed up.
565 gtk_widget_ref (event_widget);
566 if (!gtk_widget_event (event_widget, event) &&
567 !GTK_OBJECT_DESTROYED (event_widget))
568 gtk_widget_destroy (event_widget);
569 gtk_widget_unref (event_widget);
573 gtk_widget_ref (event_widget);
574 gtk_widget_event (event_widget, event);
575 if (!GTK_OBJECT_DESTROYED (event_widget))
576 gtk_widget_destroy (event_widget);
577 gtk_widget_unref (event_widget);
580 case GDK_PROPERTY_NOTIFY:
583 case GDK_FOCUS_CHANGE:
587 case GDK_SELECTION_CLEAR:
588 case GDK_SELECTION_REQUEST:
589 case GDK_SELECTION_NOTIFY:
590 case GDK_CLIENT_EVENT:
592 case GDK_DRAG_REQUEST:
595 case GDK_DROP_DATA_AVAIL:
596 case GDK_VISIBILITY_NOTIFY:
597 gtk_widget_event (event_widget, event);
601 case GDK_KEY_RELEASE:
604 if (gtk_invoke_key_snoopers (grab_widget, event))
607 /* else fall through */
608 case GDK_MOTION_NOTIFY:
609 case GDK_BUTTON_PRESS:
610 case GDK_2BUTTON_PRESS:
611 case GDK_3BUTTON_PRESS:
612 case GDK_BUTTON_RELEASE:
613 case GDK_PROXIMITY_IN:
614 case GDK_PROXIMITY_OUT:
615 case GDK_OTHER_EVENT:
616 gtk_propagate_event (grab_widget, event);
619 case GDK_ENTER_NOTIFY:
620 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
622 gtk_widget_event (grab_widget, event);
623 if (event_widget == grab_widget)
624 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
628 case GDK_LEAVE_NOTIFY:
629 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
631 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
632 gtk_widget_event (event_widget, event);
634 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
635 gtk_widget_event (grab_widget, event);
639 tmp_list = current_events;
640 current_events = g_list_remove_link (current_events, tmp_list);
641 g_list_free_1 (tmp_list);
643 gdk_event_free (event);
647 if (gdk_events_pending() == 0)
653 /* Handle timeout functions that may have expired.
655 gtk_handle_timeouts ();
657 return iteration_done;
673 gtk_grab_add (GtkWidget *widget)
675 g_return_if_fail (widget != NULL);
677 if (!GTK_WIDGET_HAS_GRAB (widget))
679 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
681 grabs = g_slist_prepend (grabs, widget);
682 gtk_widget_ref (widget);
687 gtk_grab_get_current (void)
690 return GTK_WIDGET (grabs->data);
695 gtk_grab_remove (GtkWidget *widget)
697 g_return_if_fail (widget != NULL);
699 if (GTK_WIDGET_HAS_GRAB (widget))
701 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
703 grabs = g_slist_remove (grabs, widget);
704 gtk_widget_unref (widget);
709 gtk_init_add (GtkFunction function,
712 GtkInitFunction *init;
714 init = g_new (GtkInitFunction, 1);
715 init->function = function;
718 init_functions = g_list_prepend (init_functions, init);
722 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
725 GtkKeySnooperData *data;
726 static guint snooper_id = 1;
728 g_return_val_if_fail (snooper != NULL, 0);
730 data = g_new (GtkKeySnooperData, 1);
731 data->func = snooper;
732 data->func_data = func_data;
733 data->id = snooper_id++;
734 key_snoopers = g_slist_prepend (key_snoopers, data);
740 gtk_key_snooper_remove (guint snooper_id)
742 GtkKeySnooperData *data = NULL;
745 slist = key_snoopers;
749 if (data->id == snooper_id)
756 key_snoopers = g_slist_remove (key_snoopers, data);
760 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
764 gint return_val = FALSE;
766 slist = key_snoopers;
767 while (slist && !return_val)
769 GtkKeySnooperData *data;
773 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
780 gtk_timeout_add_full (guint32 interval,
781 GtkFunction function,
782 GtkCallbackMarshal marshal,
784 GtkDestroyNotify destroy)
786 static guint timeout_tag = 1;
787 GtkTimeoutFunction *timeoutf;
789 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
791 /* Create a new timeout function structure.
792 * The start time is the current time.
794 if (!timeout_mem_chunk)
795 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
796 1024, G_ALLOC_AND_FREE);
798 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
800 timeoutf->tag = timeout_tag++;
801 timeoutf->start = gdk_time_get ();
802 timeoutf->interval = interval;
803 timeoutf->originterval = interval;
804 timeoutf->function = function;
805 timeoutf->marshal = marshal;
806 timeoutf->data = data;
807 timeoutf->destroy = destroy;
809 gtk_timeout_insert (timeoutf);
811 return timeoutf->tag;
815 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
817 if (timeoutf->destroy)
818 (timeoutf->destroy) (timeoutf->data);
819 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
823 gtk_timeout_add (guint32 interval,
824 GtkFunction function,
827 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
831 gtk_timeout_add_interp (guint32 interval,
832 GtkCallbackMarshal function,
834 GtkDestroyNotify destroy)
836 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
840 gtk_timeout_remove (guint tag)
842 GtkTimeoutFunction *timeoutf;
845 /* Remove a timeout function.
846 * (Which, basically, involves searching the
849 if ((running_timeout) && (running_timeout->tag == tag))
851 gtk_timeout_destroy (running_timeout);
852 running_timeout = NULL;
855 tmp_list = timeout_functions;
858 timeoutf = tmp_list->data;
860 if (timeoutf->tag == tag)
862 timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
863 g_list_free (tmp_list);
864 gtk_timeout_destroy (timeoutf);
869 tmp_list = tmp_list->next;
872 tmp_list = current_timeouts;
875 timeoutf = tmp_list->data;
877 if (timeoutf->tag == tag)
879 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
880 g_list_free (tmp_list);
881 gtk_timeout_destroy (timeoutf);
886 tmp_list = tmp_list->next;
890 /* We rely on some knowledge of how g_list_insert_sorted works to make
891 * sure that we insert at the _end_ of the idles of this priority
894 gtk_idle_compare (gpointer a, gpointer b)
896 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
901 gtk_quit_add_full (guint main_level,
902 GtkFunction function,
903 GtkCallbackMarshal marshal,
905 GtkDestroyNotify destroy)
907 static guint quit_id = 1;
908 GtkQuitFunction *quitf;
910 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
913 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
914 512, G_ALLOC_AND_FREE);
916 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
918 quitf->id = quit_id++;
919 quitf->main_level = main_level;
920 quitf->function = function;
921 quitf->marshal = marshal;
923 quitf->destroy = destroy;
925 quit_functions = g_list_prepend (quit_functions, quitf);
931 gtk_idle_add_full (gint priority,
932 GtkFunction function,
933 GtkCallbackMarshal marshal,
935 GtkDestroyNotify destroy)
937 static guint idle_tag = 1;
938 GtkIdleFunction *idlef;
940 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
943 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
944 1024, G_ALLOC_AND_FREE);
946 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
948 idlef->tag = idle_tag++;
949 idlef->priority = priority;
950 idlef->function = function;
951 idlef->marshal = marshal;
953 idlef->destroy = destroy;
955 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
961 gtk_idle_add_interp (GtkCallbackMarshal marshal,
963 GtkDestroyNotify destroy)
965 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
969 gtk_idle_destroy (GtkIdleFunction *idlef)
972 idlef->destroy (idlef->data);
973 g_mem_chunk_free (idle_mem_chunk, idlef);
977 gtk_quit_destroy (GtkQuitFunction *quitf)
980 quitf->destroy (quitf->data);
981 g_mem_chunk_free (quit_mem_chunk, quitf);
985 gtk_quit_destructor (GtkObject **object_p)
988 gtk_object_destroy (*object_p);
995 gtk_quit_add_destroy (guint main_level,
998 GtkObject **object_p;
1000 g_return_if_fail (main_level > 0);
1001 g_return_if_fail (object != NULL);
1002 g_return_if_fail (GTK_IS_OBJECT (object));
1004 object_p = g_new (GtkObject*, 1);
1006 gtk_signal_connect (object,
1008 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1010 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1014 gtk_quit_add (guint main_level,
1015 GtkFunction function,
1018 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1022 gtk_idle_add (GtkFunction function,
1025 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1029 gtk_idle_add_priority (gint priority,
1030 GtkFunction function,
1033 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1037 gtk_quit_remove (guint id)
1039 GtkQuitFunction *quitf;
1042 tmp_list = quit_functions;
1045 quitf = tmp_list->data;
1047 if (quitf->id == id)
1049 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1050 g_list_free (tmp_list);
1051 gtk_quit_destroy (quitf);
1056 tmp_list = tmp_list->next;
1061 gtk_quit_remove_by_data (gpointer data)
1063 GtkQuitFunction *quitf;
1066 tmp_list = quit_functions;
1069 quitf = tmp_list->data;
1071 if (quitf->data == data)
1073 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1074 g_list_free (tmp_list);
1075 gtk_quit_destroy (quitf);
1080 tmp_list = tmp_list->next;
1085 gtk_idle_remove (guint tag)
1087 GtkIdleFunction *idlef;
1090 if ((running_idle) && (running_idle->tag == tag))
1092 gtk_idle_destroy (running_idle);
1093 running_idle = NULL;
1096 tmp_list = idle_functions;
1099 idlef = tmp_list->data;
1101 if (idlef->tag == tag)
1103 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1104 g_list_free (tmp_list);
1105 gtk_idle_destroy (idlef);
1110 tmp_list = tmp_list->next;
1113 tmp_list = current_idles;
1116 idlef = tmp_list->data;
1118 if (idlef->tag == tag)
1120 current_idles = g_list_remove_link (current_idles, tmp_list);
1121 g_list_free (tmp_list);
1122 gtk_idle_destroy (idlef);
1127 tmp_list = tmp_list->next;
1132 gtk_idle_remove_by_data (gpointer data)
1134 GtkIdleFunction *idlef;
1137 tmp_list = idle_functions;
1140 idlef = tmp_list->data;
1142 if (idlef->data == data)
1144 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1145 g_list_free (tmp_list);
1146 gtk_idle_destroy (idlef);
1151 tmp_list = tmp_list->next;
1154 tmp_list = current_idles;
1157 idlef = tmp_list->data;
1159 if (idlef->data == data)
1161 current_idles = g_list_remove_link (current_idles, tmp_list);
1162 g_list_free (tmp_list);
1163 gtk_idle_destroy (idlef);
1168 tmp_list = tmp_list->next;
1173 gtk_invoke_input_function (GtkInputFunction *input,
1175 GdkInputCondition condition)
1178 args[0].type = GTK_TYPE_INT;
1179 args[0].name = NULL;
1180 GTK_VALUE_INT(args[0]) = source;
1181 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1182 args[1].name = NULL;
1183 GTK_VALUE_FLAGS(args[1]) = condition;
1184 args[2].type = GTK_TYPE_NONE;
1185 args[2].name = NULL;
1187 input->callback (NULL, input->data, 2, args);
1191 gtk_destroy_input_function (GtkInputFunction *input)
1194 (input->destroy) (input->data);
1199 gtk_input_add_full (gint source,
1200 GdkInputCondition condition,
1201 GdkInputFunction function,
1202 GtkCallbackMarshal marshal,
1204 GtkDestroyNotify destroy)
1208 GtkInputFunction *input;
1210 input = g_new (GtkInputFunction, 1);
1211 input->callback = marshal;
1213 input->destroy = destroy;
1215 return gdk_input_add_full (source,
1217 (GdkInputFunction) gtk_invoke_input_function,
1219 (GdkDestroyNotify) gtk_destroy_input_function);
1222 return gdk_input_add_full (source, condition, function, data, destroy);
1226 gtk_input_add_interp (gint source,
1227 GdkInputCondition condition,
1228 GtkCallbackMarshal callback,
1230 GtkDestroyNotify destroy)
1232 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1236 gtk_input_remove (guint tag)
1238 gdk_input_remove (tag);
1242 gtk_get_current_event ()
1245 return gdk_event_copy ((GdkEvent *) current_events->data);
1251 gtk_get_event_widget (GdkEvent *event)
1256 if (event->any.window)
1257 gdk_window_get_user_data (event->any.window, (void**) &widget);
1267 initialized = FALSE;
1268 gtk_preview_uninit ();
1273 /* We rely on some knowledge of how g_list_insert_sorted works to make
1274 * sure that we insert after timeouts of equal interval
1277 gtk_timeout_compare (gpointer a, gpointer b)
1279 return (((GtkTimeoutFunction *)a)->interval <
1280 ((GtkTimeoutFunction *)b)->interval)
1285 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1287 g_assert (timeoutf != NULL);
1289 /* Insert the timeout function appropriately.
1290 * Appropriately meaning sort it into the list
1291 * of timeout functions.
1293 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1294 gtk_timeout_compare);
1298 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1300 if (!timeoutf->marshal)
1301 return timeoutf->function (timeoutf->data);
1305 gint ret_val = FALSE;
1306 args[0].name = NULL;
1307 args[0].type = GTK_TYPE_BOOL;
1308 args[0].d.pointer_data = &ret_val;
1309 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1315 gtk_handle_current_timeouts (guint32 the_time)
1319 while (current_timeouts)
1321 tmp_list = current_timeouts;
1322 running_timeout = tmp_list->data;
1324 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1325 g_list_free (tmp_list);
1327 if ((gtk_invoke_timeout_function (running_timeout) == FALSE) ||
1328 (running_timeout == NULL))
1330 if (running_timeout)
1331 gtk_timeout_destroy (running_timeout);
1335 running_timeout->interval = running_timeout->originterval;
1336 running_timeout->start = the_time;
1337 gtk_timeout_insert (running_timeout);
1340 running_timeout = NULL;
1344 gtk_handle_timeouts ()
1349 GtkTimeoutFunction *timeoutf;
1351 /* Caller must already have called gtk_handle_current_timeouts if
1353 g_assert (current_timeouts == NULL);
1355 if (timeout_functions)
1357 the_time = gdk_time_get ();
1359 tmp_list = timeout_functions;
1362 timeoutf = tmp_list->data;
1364 if (timeoutf->interval <= (the_time - timeoutf->start))
1366 tmp_list2 = tmp_list;
1367 tmp_list = tmp_list->next;
1369 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1370 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1374 timeoutf->interval -= (the_time - timeoutf->start);
1375 timeoutf->start = the_time;
1376 tmp_list = tmp_list->next;
1380 if (current_timeouts)
1381 gtk_handle_current_timeouts(the_time);
1386 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1388 if (!quitf->marshal)
1389 return quitf->function (quitf->data);
1393 gint ret_val = FALSE;
1395 args[0].name = NULL;
1396 args[0].type = GTK_TYPE_BOOL;
1397 args[0].d.pointer_data = &ret_val;
1398 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1406 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1408 if (!idlef->marshal)
1409 return idlef->function (idlef->data);
1413 gint ret_val = FALSE;
1415 args[0].name = NULL;
1416 args[0].type = GTK_TYPE_BOOL;
1417 args[0].d.pointer_data = &ret_val;
1418 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1426 gtk_handle_current_idles ()
1431 while (current_idles)
1433 tmp_list = current_idles;
1434 running_idle = tmp_list->data;
1436 current_idles = g_list_remove_link (current_idles, tmp_list);
1438 if ((gtk_idle_invoke_function (running_idle) == FALSE) ||
1439 (running_idle == NULL))
1441 g_list_free (tmp_list);
1443 gtk_idle_destroy (running_idle);
1447 /* Insert the idle function back into the list of idle
1448 * functions at the end of the idles of this priority
1450 tmp_list2 = idle_functions;
1452 (((GtkIdleFunction *)tmp_list2->data)->priority <= running_idle->priority))
1453 tmp_list2 = tmp_list2->next;
1456 idle_functions = g_list_concat (idle_functions, tmp_list);
1457 else if (tmp_list2 == idle_functions)
1459 tmp_list->next = idle_functions;
1461 idle_functions->prev = tmp_list;
1462 idle_functions = tmp_list;
1466 tmp_list->prev = tmp_list2->prev;
1467 tmp_list->next = tmp_list2;
1468 tmp_list2->prev->next = tmp_list;
1469 tmp_list2->prev = tmp_list;
1473 running_idle = NULL;
1479 /* Caller must already have called gtk_handle_current_idles if
1482 g_assert (current_idles == NULL);
1484 /* Handle only the idle functions that have the highest priority */
1490 tmp_list = idle_functions;
1491 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1494 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1495 tmp_list = tmp_list->next;
1497 current_idles = idle_functions;
1498 idle_functions = tmp_list;
1502 tmp_list->prev->next = NULL;
1503 tmp_list->prev = NULL;
1506 gtk_handle_current_idles();
1513 GtkTimeoutFunction *timeoutf;
1518 gdk_timer_enable ();
1520 else if (timeout_functions)
1522 timeoutf = timeout_functions->data;
1523 gdk_timer_set (timeoutf->interval);
1524 gdk_timer_enable ();
1529 gdk_timer_disable ();
1534 gtk_propagate_event (GtkWidget *widget,
1539 g_return_if_fail (widget != NULL);
1540 g_return_if_fail (event != NULL);
1542 handled_event = FALSE;
1544 if ((event->type == GDK_KEY_PRESS) ||
1545 (event->type == GDK_KEY_RELEASE))
1547 /* Only send key events within Window widgets to the Window
1548 * The Window widget will in turn pass the
1549 * key event on to the currently focused widget
1554 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1557 if (GTK_WIDGET_IS_SENSITIVE (window))
1558 gtk_widget_event (window, event);
1560 handled_event = TRUE; /* don't send to widget */
1564 /* Other events get propagated up the widget tree
1565 * so that parents can see the button and motion
1566 * events of the children.
1568 while (!handled_event && widget)
1572 gtk_widget_ref (widget);
1573 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1574 tmp = widget->parent;
1575 gtk_widget_unref (widget);
1582 gtk_error (gchar *str)
1588 gtk_warning (gchar *str)
1594 gtk_message (gchar *str)
1600 gtk_print (gchar *str)
1602 static GtkWidget *window = NULL;
1603 static GtkWidget *text;
1604 static int level = 0;
1608 GtkWidget *hscrollbar;
1609 GtkWidget *vscrollbar;
1610 GtkWidget *separator;
1615 fputs (str, stdout);
1622 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1624 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1625 (GtkSignalFunc) gtk_widget_destroyed,
1628 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1630 box1 = gtk_vbox_new (FALSE, 0);
1631 gtk_container_add (GTK_CONTAINER (window), box1);
1632 gtk_widget_show (box1);
1635 box2 = gtk_vbox_new (FALSE, 10);
1636 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1637 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1638 gtk_widget_show (box2);
1641 table = gtk_table_new (2, 2, FALSE);
1642 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1643 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1644 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1645 gtk_widget_show (table);
1647 text = gtk_text_new (NULL, NULL);
1648 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1649 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1650 gtk_widget_show (text);
1651 gtk_widget_realize (text);
1653 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1654 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1655 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1656 gtk_widget_show (hscrollbar);
1658 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1659 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1660 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1661 gtk_widget_show (vscrollbar);
1663 separator = gtk_hseparator_new ();
1664 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1665 gtk_widget_show (separator);
1668 box2 = gtk_vbox_new (FALSE, 10);
1669 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1670 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1671 gtk_widget_show (box2);
1674 button = gtk_button_new_with_label ("close");
1675 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1676 (GtkSignalFunc) gtk_widget_hide,
1677 GTK_OBJECT (window));
1678 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1679 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1680 gtk_widget_grab_default (button);
1681 gtk_widget_show (button);
1685 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1688 if (!GTK_WIDGET_VISIBLE (window))
1689 gtk_widget_show (window);