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.
20 #include "gtkbutton.h"
21 #include "gtkhscrollbar.h"
22 #include "gtkhseparator.h"
24 #include "gtkpreview.h"
26 #include "gtkselection.h"
27 #include "gtksignal.h"
31 #include "gtkvscrollbar.h"
32 #include "gtkwidget.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
37 /* Private type definitions
39 typedef struct _GtkInitFunction GtkInitFunction;
40 typedef struct _GtkQuitFunction GtkQuitFunction;
41 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
42 typedef struct _GtkIdleFunction GtkIdleFunction;
43 typedef struct _GtkInputFunction GtkInputFunction;
44 typedef struct _GtkKeySnooperData GtkKeySnooperData;
46 struct _GtkInitFunction
52 struct _GtkQuitFunction
56 GtkCallbackMarshal marshal;
59 GtkDestroyNotify destroy;
62 struct _GtkTimeoutFunction
69 GtkCallbackMarshal marshal;
71 GtkDestroyNotify destroy;
74 struct _GtkIdleFunction
78 GtkCallbackMarshal marshal;
81 GtkDestroyNotify destroy;
84 struct _GtkInputFunction
86 GtkCallbackMarshal callback;
88 GtkDestroyNotify destroy;
91 struct _GtkKeySnooperData
98 static void gtk_exit_func (void);
99 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
100 static void gtk_quit_destroy (GtkQuitFunction *quitf);
101 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
102 static void gtk_handle_current_timeouts (guint32 the_time);
103 static void gtk_handle_current_idles (void);
104 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
106 static void gtk_handle_timeouts (void);
107 static void gtk_handle_idle (void);
108 static void gtk_handle_timer (void);
109 static void gtk_propagate_event (GtkWidget *widget,
111 static void gtk_error (gchar *str);
112 static void gtk_warning (gchar *str);
113 static void gtk_message (gchar *str);
114 static void gtk_print (gchar *str);
116 static gint gtk_idle_compare (gpointer a,
119 static gint gtk_timeout_compare (gpointer a,
122 static gboolean iteration_done = FALSE;
123 static guint main_level = 0;
124 static gint initialized = FALSE;
125 static GdkEvent *next_event = NULL;
126 static GList *current_events = NULL;
128 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
129 * widget is the first one on the list.
131 static GList *init_functions = NULL; /* A list of init functions.
133 static GList *quit_functions = NULL; /* A list of quit functions.
135 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
136 * when the length of the time interval
137 * remaining. Therefore, the first timeout
138 * function to expire is at the head of
139 * the list and the last to expire is at
140 * the tail of the list.
142 static GList *idle_functions = NULL; /* A list of idle functions.
145 static GList *current_idles = NULL;
146 static GList *current_timeouts = NULL;
147 static GMemChunk *timeout_mem_chunk = NULL;
148 static GMemChunk *idle_mem_chunk = NULL;
149 static GMemChunk *quit_mem_chunk = NULL;
151 static GSList *key_snoopers = NULL;
153 static GdkVisual *gtk_visual; /* The visual to be used in creating new
156 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
160 guint gtk_debug_flags = 0; /* Global GTK debug flag */
162 #ifdef G_ENABLE_DEBUG
163 static GDebugKey gtk_debug_keys[] = {
164 {"objects", GTK_DEBUG_OBJECTS}
167 static const int gtk_ndebug_keys = sizeof(gtk_debug_keys)/sizeof(GDebugKey);
169 #endif /* G_ENABLE_DEBUG */
180 g_set_error_handler (gtk_error);
181 g_set_warning_handler (gtk_warning);
182 g_set_message_handler (gtk_message);
183 g_set_print_handler (gtk_print);
186 /* Initialize "gdk". We pass along the 'argc' and 'argv'
187 * parameters as they contain information that GDK uses
189 gdk_init (argc, argv);
191 #ifdef G_ENABLE_DEBUG
193 gchar *debug_string = getenv("GTK_DEBUG");
194 if (debug_string != NULL)
195 gtk_debug_flags = g_parse_debug_string (debug_string,
204 for (i = 1; i < *argc;)
206 if ((*argv)[i] == NULL)
212 if (strcmp ("--gtk-debug", (*argv)[i]) == 0)
216 if ((i + 1) < *argc && (*argv)[i + 1])
218 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
221 (*argv)[i + 1] = NULL;
225 else if (strcmp ("--gtk-no-debug", (*argv)[i]) == 0)
229 if ((i + 1) < *argc && (*argv)[i + 1])
231 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
234 (*argv)[i + 1] = NULL;
242 #endif /* G_ENABLE_DEBUG */
244 /* Initialize the default visual and colormap to be
245 * used in creating widgets. (We want to use the system
246 * defaults so as to be nice to the colormap).
248 gtk_visual = gdk_visual_get_system ();
249 gtk_colormap = gdk_colormap_get_system ();
254 /* Register an exit function to make sure we are able to cleanup.
256 if (ATEXIT (gtk_exit_func))
257 g_warning ("unable to register exit function");
259 /* Set the 'initialized' flag.
265 gtk_exit (int errorcode)
267 /* Only if "gtk" has been initialized should we de-initialize.
269 /* de-initialisation is done by the gtk_exit_funct(),
270 * no need to do this here (Alex J.)
278 return gdk_set_locale ();
286 GtkInitFunction *init;
291 tmp_list = functions = init_functions;
292 init_functions = NULL;
296 init = tmp_list->data;
297 tmp_list = tmp_list->next;
299 (* init->function) (init->data);
302 g_list_free (functions);
304 old_done = iteration_done;
305 while (!gtk_main_iteration ())
307 iteration_done = old_done;
311 GList *reinvoke_list = NULL;
312 GtkQuitFunction *quitf;
314 while (quit_functions)
316 quitf = quit_functions->data;
318 quit_functions = g_list_remove_link (quit_functions, quit_functions);
320 if ((quitf->main_level &&
321 quitf->main_level != main_level) ||
322 gtk_quit_invoke_function (quitf) == FALSE)
324 g_list_free (tmp_list);
325 gtk_quit_destroy (quitf);
329 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
336 tmp_list = g_list_last (reinvoke_list);
338 quit_functions->prev = tmp_list;
339 tmp_list->next = quit_functions;
340 quit_functions = tmp_list;
348 gtk_main_level (void)
356 iteration_done = TRUE;
360 gtk_events_pending (void)
362 gint result = gdk_events_pending() + ((next_event != NULL) ? 1 : 0);
364 if (idle_functions &&
365 (((GtkIdleFunction *)idle_functions->data)->priority >=
366 GTK_PRIORITY_INTERNAL))
373 gtk_main_iteration ()
375 return gtk_main_iteration_do (TRUE);
379 gtk_main_iteration_do (gboolean blocking)
381 GtkWidget *event_widget;
382 GtkWidget *grab_widget;
383 GdkEvent *event = NULL;
386 iteration_done = FALSE;
388 /* If this is a recursive call, and there are pending timeouts or
389 * idles, finish them, then return immediately to avoid blocking
392 if (current_timeouts)
394 gtk_handle_current_timeouts( gdk_time_get());
395 return iteration_done;
399 gtk_handle_current_idles();
400 return iteration_done;
403 /* If there is a valid event in 'next_event' then move it to 'event'
411 /* If we don't have an event then get one.
415 /* Handle setting of the "gdk" timer. If there are no
416 * timeout functions, then the timer is turned off.
417 * If there are timeout functions, then the timer is
418 * set to the shortest timeout interval (which is
419 * the first timeout function).
423 if (blocking) event = gdk_event_get ();
426 /* "gdk_event_get" can return FALSE if the timer goes off
427 * and no events are pending. Therefore, we should make
428 * sure that we got an event before continuing.
432 /* If there are any events pending then get the next one.
434 if (gdk_events_pending () > 0)
435 next_event = gdk_event_get ();
437 /* Try to compress enter/leave notify events. These event
438 * pairs occur when the mouse is dragged quickly across
439 * a window with many buttons (or through a menu). Instead
440 * of highlighting and de-highlighting each widget that
441 * is crossed it is better to simply de-highlight the widget
442 * which contained the mouse initially and highlight the
443 * widget which ends up containing the mouse.
446 if (((event->type == GDK_ENTER_NOTIFY) ||
447 (event->type == GDK_LEAVE_NOTIFY)) &&
448 ((next_event->type == GDK_ENTER_NOTIFY) ||
449 (next_event->type == GDK_LEAVE_NOTIFY)) &&
450 (next_event->type != event->type) &&
451 (next_event->any.window == event->any.window))
453 gdk_event_free (event);
454 gdk_event_free (next_event);
457 goto event_handling_done;
460 /* Find the widget which got the event. We store the widget
461 * in the user_data field of GdkWindow's.
462 * Ignore the event if we don't have a widget for it, except
463 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
464 * Though this happens rarely, bogus events can occour
465 * for e.g. destroyed GdkWindows.
467 event_widget = gtk_get_event_widget (event);
470 /* To handle selection INCR transactions, we select
471 * PropertyNotify events on the requestor window and create
472 * a corresponding (fake) GdkWindow so that events get
473 * here. There won't be a widget though, so we have to handle
476 if (event->type == GDK_PROPERTY_NOTIFY)
477 gtk_selection_incr_event (event->any.window,
480 gdk_event_free (event);
482 goto event_handling_done;
485 /* Push the event onto a stack of current events for
486 * gtk_current_event_get().
488 current_events = g_list_prepend (current_events, event);
490 /* If there is a grab in effect...
494 grab_widget = grabs->data;
496 /* If the grab widget is an ancestor of the event widget
497 * then we send the event to the original event widget.
498 * This is the key to implementing modality.
500 if (gtk_widget_is_ancestor (event_widget, grab_widget))
501 grab_widget = event_widget;
505 grab_widget = event_widget;
508 /* Not all events get sent to the grabbing widget.
509 * The delete, destroy, expose, focus change and resize
510 * events still get sent to the event widget because
511 * 1) these events have no meaning for the grabbing widget
512 * and 2) redirecting these events to the grabbing widget
513 * could cause the display to be messed up.
521 gtk_widget_ref (event_widget);
522 if (gtk_widget_event (event_widget, event))
523 gtk_widget_destroy (event_widget);
524 gtk_widget_unref (event_widget);
528 gtk_widget_ref (event_widget);
529 gtk_widget_event (event_widget, event);
530 gtk_widget_destroy (event_widget);
531 gtk_widget_unref (event_widget);
534 case GDK_PROPERTY_NOTIFY:
537 case GDK_FOCUS_CHANGE:
541 case GDK_SELECTION_CLEAR:
542 case GDK_SELECTION_REQUEST:
543 case GDK_SELECTION_NOTIFY:
544 case GDK_CLIENT_EVENT:
546 case GDK_DRAG_REQUEST:
549 case GDK_DROP_DATA_AVAIL:
550 case GDK_VISIBILITY_NOTIFY:
551 gtk_widget_event (event_widget, event);
555 case GDK_KEY_RELEASE:
558 if (gtk_invoke_key_snoopers (grab_widget, event))
561 /* else fall through */
562 case GDK_MOTION_NOTIFY:
563 case GDK_BUTTON_PRESS:
564 case GDK_2BUTTON_PRESS:
565 case GDK_3BUTTON_PRESS:
566 case GDK_BUTTON_RELEASE:
567 case GDK_PROXIMITY_IN:
568 case GDK_PROXIMITY_OUT:
569 case GDK_OTHER_EVENT:
570 gtk_propagate_event (grab_widget, event);
573 case GDK_ENTER_NOTIFY:
574 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
576 gtk_widget_event (grab_widget, event);
577 if (event_widget == grab_widget)
578 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
582 case GDK_LEAVE_NOTIFY:
583 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
585 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
586 gtk_widget_event (event_widget, event);
588 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
589 gtk_widget_event (grab_widget, event);
593 tmp_list = current_events;
594 current_events = g_list_remove_link (current_events, tmp_list);
595 g_list_free_1 (tmp_list);
597 gdk_event_free (event);
601 if (gdk_events_pending() == 0)
607 /* Handle timeout functions that may have expired.
609 gtk_handle_timeouts ();
611 return iteration_done;
627 gtk_grab_add (GtkWidget *widget)
629 g_return_if_fail (widget != NULL);
631 if (!GTK_WIDGET_HAS_GRAB (widget))
633 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
635 grabs = g_slist_prepend (grabs, widget);
636 gtk_widget_ref (widget);
641 gtk_grab_get_current (void)
644 return GTK_WIDGET (grabs->data);
649 gtk_grab_remove (GtkWidget *widget)
651 g_return_if_fail (widget != NULL);
653 if (GTK_WIDGET_HAS_GRAB (widget))
655 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
657 grabs = g_slist_remove (grabs, widget);
658 gtk_widget_unref (widget);
663 gtk_init_add (GtkFunction function,
666 GtkInitFunction *init;
668 init = g_new (GtkInitFunction, 1);
669 init->function = function;
672 init_functions = g_list_prepend (init_functions, init);
676 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
679 GtkKeySnooperData *data;
680 static guint snooper_id = 1;
682 g_return_val_if_fail (snooper != NULL, 0);
684 data = g_new (GtkKeySnooperData, 1);
685 data->func = snooper;
686 data->func_data = func_data;
687 data->id = snooper_id++;
688 key_snoopers = g_slist_prepend (key_snoopers, data);
694 gtk_key_snooper_remove (gint snooper_id)
696 GtkKeySnooperData *data = NULL;
699 slist = key_snoopers;
703 if (data->id == snooper_id)
710 key_snoopers = g_slist_remove (key_snoopers, data);
714 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
718 gint return_val = FALSE;
720 slist = key_snoopers;
721 while (slist && !return_val)
723 GtkKeySnooperData *data;
727 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
734 gtk_timeout_add_full (guint32 interval,
735 GtkFunction function,
736 GtkCallbackMarshal marshal,
738 GtkDestroyNotify destroy)
740 static gint timeout_tag = 1;
741 GtkTimeoutFunction *timeoutf;
743 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
745 /* Create a new timeout function structure.
746 * The start time is the current time.
748 if (!timeout_mem_chunk)
749 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
750 1024, G_ALLOC_AND_FREE);
752 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
754 timeoutf->tag = timeout_tag++;
755 timeoutf->start = gdk_time_get ();
756 timeoutf->interval = interval;
757 timeoutf->originterval = interval;
758 timeoutf->function = function;
759 timeoutf->marshal = marshal;
760 timeoutf->data = data;
761 timeoutf->destroy = destroy;
763 gtk_timeout_insert (timeoutf);
765 return timeoutf->tag;
769 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
771 if (timeoutf->destroy)
772 (timeoutf->destroy) (timeoutf->data);
773 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
777 gtk_timeout_add (guint32 interval,
778 GtkFunction function,
781 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
785 gtk_timeout_add_interp (guint32 interval,
786 GtkCallbackMarshal function,
788 GtkDestroyNotify destroy)
790 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
794 gtk_timeout_remove (gint tag)
796 GtkTimeoutFunction *timeoutf;
799 /* Remove a timeout function.
800 * (Which, basically, involves searching the
803 tmp_list = timeout_functions;
806 timeoutf = tmp_list->data;
808 if (timeoutf->tag == tag)
810 timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
811 g_list_free (tmp_list);
812 gtk_timeout_destroy (timeoutf);
817 tmp_list = tmp_list->next;
820 tmp_list = current_timeouts;
823 timeoutf = tmp_list->data;
825 if (timeoutf->tag == tag)
827 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
828 g_list_free (tmp_list);
829 gtk_timeout_destroy (timeoutf);
834 tmp_list = tmp_list->next;
838 /* We rely on some knowledge of how g_list_insert_sorted works to make
839 * sure that we insert at the _end_ of the idles of this priority
842 gtk_idle_compare (gpointer a, gpointer b)
844 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
849 gtk_quit_add_full (guint main_level,
850 GtkFunction function,
851 GtkCallbackMarshal marshal,
853 GtkDestroyNotify destroy)
855 static gint quit_tag = 1;
856 GtkQuitFunction *quitf;
858 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
861 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
862 512, G_ALLOC_AND_FREE);
864 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
866 quitf->tag = quit_tag++;
867 quitf->main_level = main_level;
868 quitf->function = function;
869 quitf->marshal = marshal;
871 quitf->destroy = destroy;
873 quit_functions = g_list_prepend (quit_functions, quitf);
879 gtk_idle_add_full (gint priority,
880 GtkFunction function,
881 GtkCallbackMarshal marshal,
883 GtkDestroyNotify destroy)
885 static gint idle_tag = 1;
886 GtkIdleFunction *idlef;
888 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
891 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
892 1024, G_ALLOC_AND_FREE);
894 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
896 idlef->tag = idle_tag++;
897 idlef->priority = priority;
898 idlef->function = function;
899 idlef->marshal = marshal;
901 idlef->destroy = destroy;
903 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
909 gtk_idle_add_interp (GtkCallbackMarshal marshal,
911 GtkDestroyNotify destroy)
913 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
917 gtk_idle_destroy (GtkIdleFunction *idlef)
920 idlef->destroy (idlef->data);
921 g_mem_chunk_free (idle_mem_chunk, idlef);
925 gtk_quit_destroy (GtkQuitFunction *quitf)
928 quitf->destroy (quitf->data);
929 g_mem_chunk_free (quit_mem_chunk, quitf);
933 gtk_quit_add (guint main_level,
934 GtkFunction function,
937 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
941 gtk_idle_add (GtkFunction function,
944 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
948 gtk_idle_add_priority (gint priority,
949 GtkFunction function,
952 return gtk_idle_add_full (priority, function, NULL, data, NULL);
956 gtk_quit_remove (gint tag)
958 GtkQuitFunction *quitf;
961 tmp_list = quit_functions;
964 quitf = tmp_list->data;
966 if (quitf->tag == tag)
968 quit_functions = g_list_remove_link (quit_functions, tmp_list);
969 g_list_free (tmp_list);
970 gtk_quit_destroy (quitf);
975 tmp_list = tmp_list->next;
980 gtk_quit_remove_by_data (gpointer data)
982 GtkQuitFunction *quitf;
985 tmp_list = quit_functions;
988 quitf = tmp_list->data;
990 if (quitf->data == data)
992 quit_functions = g_list_remove_link (quit_functions, tmp_list);
993 g_list_free (tmp_list);
994 gtk_quit_destroy (quitf);
999 tmp_list = tmp_list->next;
1004 gtk_idle_remove (gint tag)
1006 GtkIdleFunction *idlef;
1009 tmp_list = idle_functions;
1012 idlef = tmp_list->data;
1014 if (idlef->tag == tag)
1016 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1017 g_list_free (tmp_list);
1018 gtk_idle_destroy (idlef);
1023 tmp_list = tmp_list->next;
1026 tmp_list = current_idles;
1029 idlef = tmp_list->data;
1031 if (idlef->tag == tag)
1033 current_idles = g_list_remove_link (current_idles, tmp_list);
1034 g_list_free (tmp_list);
1035 gtk_idle_destroy (idlef);
1040 tmp_list = tmp_list->next;
1045 gtk_idle_remove_by_data (gpointer data)
1047 GtkIdleFunction *idlef;
1050 tmp_list = idle_functions;
1053 idlef = tmp_list->data;
1055 if (idlef->data == data)
1057 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1058 g_list_free (tmp_list);
1059 gtk_idle_destroy (idlef);
1064 tmp_list = tmp_list->next;
1067 tmp_list = current_idles;
1070 idlef = tmp_list->data;
1072 if (idlef->data == data)
1074 current_idles = g_list_remove_link (current_idles, tmp_list);
1075 g_list_free (tmp_list);
1076 gtk_idle_destroy (idlef);
1081 tmp_list = tmp_list->next;
1086 gtk_invoke_input_function (GtkInputFunction *input,
1088 GdkInputCondition condition)
1091 args[0].type = GTK_TYPE_INT;
1092 args[0].name = NULL;
1093 GTK_VALUE_INT(args[0]) = source;
1094 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1095 args[1].name = NULL;
1096 GTK_VALUE_FLAGS(args[1]) = condition;
1097 args[2].type = GTK_TYPE_NONE;
1098 args[2].name = NULL;
1100 input->callback (NULL, input->data, 2, args);
1104 gtk_destroy_input_function (GtkInputFunction *input)
1107 (input->destroy) (input->data);
1112 gtk_input_add_full (gint source,
1113 GdkInputCondition condition,
1114 GdkInputFunction function,
1115 GtkCallbackMarshal marshal,
1117 GtkDestroyNotify destroy)
1121 GtkInputFunction *input = g_new (GtkInputFunction, 1);
1122 input->callback = marshal;
1124 input->destroy = destroy;
1126 return gdk_input_add_full (source,
1128 (GdkInputFunction) gtk_invoke_input_function,
1130 (GdkDestroyNotify) gtk_destroy_input_function);
1133 return gdk_input_add_full (source, condition, function, data, destroy);
1137 gtk_input_add_interp (gint source,
1138 GdkInputCondition condition,
1139 GtkCallbackMarshal callback,
1141 GtkDestroyNotify destroy)
1143 return gdk_input_add_full (source, condition, NULL, callback, data);
1147 gtk_input_remove (gint tag)
1149 gdk_input_remove (tag);
1153 gtk_get_current_event ()
1156 return gdk_event_copy ((GdkEvent *) current_events->data);
1162 gtk_get_event_widget (GdkEvent *event)
1167 if (event->any.window)
1168 gdk_window_get_user_data (event->any.window, (void**) &widget);
1178 initialized = FALSE;
1179 gtk_preview_uninit ();
1184 /* We rely on some knowledge of how g_list_insert_sorted works to make
1185 * sure that we insert after timeouts of equal interval
1188 gtk_timeout_compare (gpointer a, gpointer b)
1190 return (((GtkTimeoutFunction *)a)->interval <
1191 ((GtkTimeoutFunction *)b)->interval)
1196 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1198 g_assert (timeoutf != NULL);
1200 /* Insert the timeout function appropriately.
1201 * Appropriately meaning sort it into the list
1202 * of timeout functions.
1204 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1205 gtk_timeout_compare);
1209 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1211 if (!timeoutf->marshal)
1212 return timeoutf->function (timeoutf->data);
1216 gint ret_val = FALSE;
1217 args[0].name = NULL;
1218 args[0].type = GTK_TYPE_BOOL;
1219 args[0].d.pointer_data = &ret_val;
1220 ((GtkCallbackMarshal)timeoutf->function) (NULL,
1228 gtk_handle_current_timeouts (guint32 the_time)
1231 GtkTimeoutFunction *timeoutf;
1233 while (current_timeouts)
1235 tmp_list = current_timeouts;
1236 timeoutf = tmp_list->data;
1238 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1239 g_list_free (tmp_list);
1241 if (gtk_invoke_timeout_function (timeoutf) == FALSE)
1243 gtk_timeout_destroy (timeoutf);
1247 timeoutf->interval = timeoutf->originterval;
1248 timeoutf->start = the_time;
1249 gtk_timeout_insert (timeoutf);
1255 gtk_handle_timeouts ()
1260 GtkTimeoutFunction *timeoutf;
1262 /* Caller must already have called gtk_handle_current_timeouts if
1264 g_assert (current_timeouts == NULL);
1266 if (timeout_functions)
1268 the_time = gdk_time_get ();
1270 tmp_list = timeout_functions;
1273 timeoutf = tmp_list->data;
1275 if (timeoutf->interval <= (the_time - timeoutf->start))
1277 tmp_list2 = tmp_list;
1278 tmp_list = tmp_list->next;
1280 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1281 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1285 timeoutf->interval -= (the_time - timeoutf->start);
1286 timeoutf->start = the_time;
1287 tmp_list = tmp_list->next;
1291 if (current_timeouts)
1292 gtk_handle_current_timeouts(the_time);
1297 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1299 if (!quitf->marshal)
1300 return quitf->function (quitf->data);
1304 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 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1317 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1319 if (!idlef->marshal)
1320 return idlef->function (idlef->data);
1324 gint ret_val = FALSE;
1326 args[0].name = NULL;
1327 args[0].type = GTK_TYPE_BOOL;
1328 args[0].d.pointer_data = &ret_val;
1329 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1337 gtk_handle_current_idles ()
1341 GtkIdleFunction *idlef;
1343 while (current_idles)
1345 tmp_list = current_idles;
1346 idlef = tmp_list->data;
1348 current_idles = g_list_remove_link (current_idles, tmp_list);
1350 if (gtk_idle_invoke_function (idlef) == FALSE)
1352 g_list_free (tmp_list);
1353 gtk_idle_destroy (idlef);
1357 /* Insert the idle function back into the list of idle
1358 * functions at the end of the idles of this priority
1360 tmp_list2 = idle_functions;
1362 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1363 tmp_list2 = tmp_list2->next;
1366 idle_functions = g_list_concat (idle_functions, tmp_list);
1367 else if (tmp_list2 == idle_functions)
1369 tmp_list->next = idle_functions;
1371 idle_functions->prev = tmp_list;
1372 idle_functions = tmp_list;
1376 tmp_list->prev = tmp_list2->prev;
1377 tmp_list->next = tmp_list2;
1378 tmp_list2->prev->next = tmp_list;
1379 tmp_list2->prev = tmp_list;
1388 /* Caller must already have called gtk_handle_current_idles if
1391 g_assert (current_idles == NULL);
1393 /* Handle only the idle functions that have the highest priority */
1399 tmp_list = idle_functions;
1400 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1403 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1404 tmp_list = tmp_list->next;
1406 current_idles = idle_functions;
1407 idle_functions = tmp_list;
1411 tmp_list->prev->next = NULL;
1412 tmp_list->prev = NULL;
1415 gtk_handle_current_idles();
1422 GtkTimeoutFunction *timeoutf;
1427 gdk_timer_enable ();
1429 else if (timeout_functions)
1431 timeoutf = timeout_functions->data;
1432 gdk_timer_set (timeoutf->interval);
1433 gdk_timer_enable ();
1438 gdk_timer_disable ();
1443 gtk_propagate_event (GtkWidget *widget,
1450 g_return_if_fail (widget != NULL);
1451 g_return_if_fail (event != NULL);
1453 handled_event = FALSE;
1454 gtk_widget_ref (widget);
1456 if ((event->type == GDK_KEY_PRESS) ||
1457 (event->type == GDK_KEY_RELEASE))
1460 /* Only send key events to window widgets.
1461 * The window widget will in turn pass the
1462 * key event on to the currently focused widget
1465 parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1466 handled_event = (parent &&
1467 GTK_WIDGET_IS_SENSITIVE (parent) &&
1468 gtk_widget_event (parent, event));
1471 /* Other events get propagated up the widget tree
1472 * so that parents can see the button and motion
1473 * events of the children.
1476 while (!handled_event && tmp)
1478 gtk_widget_ref (tmp);
1479 handled_event = !GTK_WIDGET_IS_SENSITIVE (tmp) || gtk_widget_event (tmp, event);
1480 parent = tmp->parent;
1481 gtk_widget_unref (tmp);
1485 gtk_widget_unref (widget);
1490 gtk_error (gchar *str)
1496 gtk_warning (gchar *str)
1502 gtk_message (gchar *str)
1508 gtk_print (gchar *str)
1510 static GtkWidget *window = NULL;
1511 static GtkWidget *text;
1512 static int level = 0;
1516 GtkWidget *hscrollbar;
1517 GtkWidget *vscrollbar;
1518 GtkWidget *separator;
1523 fputs (str, stdout);
1530 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1532 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1533 (GtkSignalFunc) gtk_widget_destroyed,
1536 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1538 box1 = gtk_vbox_new (FALSE, 0);
1539 gtk_container_add (GTK_CONTAINER (window), box1);
1540 gtk_widget_show (box1);
1543 box2 = gtk_vbox_new (FALSE, 10);
1544 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1545 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1546 gtk_widget_show (box2);
1549 table = gtk_table_new (2, 2, FALSE);
1550 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1551 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1552 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1553 gtk_widget_show (table);
1555 text = gtk_text_new (NULL, NULL);
1556 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1557 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1558 gtk_widget_show (text);
1559 gtk_widget_realize (text);
1561 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1562 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1563 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1564 gtk_widget_show (hscrollbar);
1566 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1567 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1568 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1569 gtk_widget_show (vscrollbar);
1571 separator = gtk_hseparator_new ();
1572 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1573 gtk_widget_show (separator);
1576 box2 = gtk_vbox_new (FALSE, 10);
1577 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1578 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1579 gtk_widget_show (box2);
1582 button = gtk_button_new_with_label ("close");
1583 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1584 (GtkSignalFunc) gtk_widget_hide,
1585 GTK_OBJECT (window));
1586 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1587 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1588 gtk_widget_grab_default (button);
1589 gtk_widget_show (button);
1593 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1596 if (!GTK_WIDGET_VISIBLE (window))
1597 gtk_widget_show (window);