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"
35 #include "../config.h"
38 /* Private type definitions
40 typedef struct _GtkInitFunction GtkInitFunction;
41 typedef struct _GtkQuitFunction GtkQuitFunction;
42 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
43 typedef struct _GtkIdleFunction GtkIdleFunction;
44 typedef struct _GtkInputFunction GtkInputFunction;
45 typedef struct _GtkKeySnooperData GtkKeySnooperData;
47 struct _GtkInitFunction
53 struct _GtkQuitFunction
57 GtkCallbackMarshal marshal;
60 GtkDestroyNotify destroy;
63 struct _GtkTimeoutFunction
70 GtkCallbackMarshal marshal;
72 GtkDestroyNotify destroy;
75 struct _GtkIdleFunction
79 GtkCallbackMarshal marshal;
82 GtkDestroyNotify destroy;
85 struct _GtkInputFunction
87 GtkCallbackMarshal callback;
89 GtkDestroyNotify destroy;
92 struct _GtkKeySnooperData
99 static void gtk_exit_func (void);
100 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
101 static void gtk_quit_destroy (GtkQuitFunction *quitf);
102 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
103 static void gtk_handle_current_timeouts (guint32 the_time);
104 static void gtk_handle_current_idles (void);
105 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
107 static void gtk_handle_timeouts (void);
108 static void gtk_handle_idle (void);
109 static void gtk_handle_timer (void);
110 static void gtk_propagate_event (GtkWidget *widget,
112 static void gtk_error (gchar *str);
113 static void gtk_warning (gchar *str);
114 static void gtk_message (gchar *str);
115 static void gtk_print (gchar *str);
117 static gint gtk_idle_compare (gpointer a,
120 static gint gtk_timeout_compare (gpointer a,
123 const guint gtk_major_version = GTK_MAJOR_VERSION;
124 const guint gtk_minor_version = GTK_MINOR_VERSION;
125 const guint gtk_micro_version = GTK_MICRO_VERSION;
127 static gboolean iteration_done = FALSE;
128 static guint main_level = 0;
129 static gint initialized = FALSE;
130 static GdkEvent *next_event = NULL;
131 static GList *current_events = NULL;
133 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
134 * widget is the first one on the list.
136 static GList *init_functions = NULL; /* A list of init functions.
138 static GList *quit_functions = NULL; /* A list of quit functions.
140 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
141 * when the length of the time interval
142 * remaining. Therefore, the first timeout
143 * function to expire is at the head of
144 * the list and the last to expire is at
145 * the tail of the list.
147 static GList *idle_functions = NULL; /* A list of idle functions.
150 static GList *current_idles = NULL;
151 static GList *current_timeouts = NULL;
152 static GMemChunk *timeout_mem_chunk = NULL;
153 static GMemChunk *idle_mem_chunk = NULL;
154 static GMemChunk *quit_mem_chunk = NULL;
156 static GSList *key_snoopers = NULL;
158 static GdkVisual *gtk_visual; /* The visual to be used in creating new
161 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
165 guint gtk_debug_flags = 0; /* Global GTK debug flag */
167 #ifdef G_ENABLE_DEBUG
168 static GDebugKey gtk_debug_keys[] = {
169 {"objects", GTK_DEBUG_OBJECTS}
172 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
174 #endif /* G_ENABLE_DEBUG */
185 g_set_error_handler (gtk_error);
186 g_set_warning_handler (gtk_warning);
187 g_set_message_handler (gtk_message);
188 g_set_print_handler (gtk_print);
191 /* Initialize "gdk". We pass along the 'argc' and 'argv'
192 * parameters as they contain information that GDK uses
194 gdk_init (argc, argv);
196 #ifdef G_ENABLE_DEBUG
198 gchar *debug_string = getenv("GTK_DEBUG");
199 if (debug_string != NULL)
200 gtk_debug_flags = g_parse_debug_string (debug_string,
209 for (i = 1; i < *argc;)
211 if ((*argv)[i] == NULL)
217 if (strcmp ("--gtk-debug", (*argv)[i]) == 0)
221 if ((i + 1) < *argc && (*argv)[i + 1])
223 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
226 (*argv)[i + 1] = NULL;
230 else if (strcmp ("--gtk-no-debug", (*argv)[i]) == 0)
234 if ((i + 1) < *argc && (*argv)[i + 1])
236 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
239 (*argv)[i + 1] = NULL;
247 #endif /* G_ENABLE_DEBUG */
249 /* Initialize the default visual and colormap to be
250 * used in creating widgets. (We want to use the system
251 * defaults so as to be nice to the colormap).
253 gtk_visual = gdk_visual_get_system ();
254 gtk_colormap = gdk_colormap_get_system ();
259 /* Register an exit function to make sure we are able to cleanup.
261 if (ATEXIT (gtk_exit_func))
262 g_warning ("unable to register exit function");
264 /* Set the 'initialized' flag.
270 gtk_exit (int errorcode)
272 /* Only if "gtk" has been initialized should we de-initialize.
274 /* de-initialisation is done by the gtk_exit_funct(),
275 * no need to do this here (Alex J.)
283 return gdk_set_locale ();
291 GtkInitFunction *init;
296 tmp_list = functions = init_functions;
297 init_functions = NULL;
301 init = tmp_list->data;
302 tmp_list = tmp_list->next;
304 (* init->function) (init->data);
307 g_list_free (functions);
309 old_done = iteration_done;
310 while (!gtk_main_iteration ())
312 iteration_done = old_done;
316 GList *reinvoke_list = NULL;
317 GtkQuitFunction *quitf;
319 while (quit_functions)
321 quitf = quit_functions->data;
323 quit_functions = g_list_remove_link (quit_functions, quit_functions);
325 if ((quitf->main_level &&
326 quitf->main_level != main_level) ||
327 gtk_quit_invoke_function (quitf) == FALSE)
329 g_list_free (tmp_list);
330 gtk_quit_destroy (quitf);
334 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
341 tmp_list = g_list_last (reinvoke_list);
343 quit_functions->prev = tmp_list;
344 tmp_list->next = quit_functions;
345 quit_functions = tmp_list;
353 gtk_main_level (void)
361 iteration_done = TRUE;
365 gtk_events_pending (void)
367 gint result = gdk_events_pending() + ((next_event != NULL) ? 1 : 0);
369 if (idle_functions &&
370 (((GtkIdleFunction *)idle_functions->data)->priority >=
371 GTK_PRIORITY_INTERNAL))
378 gtk_main_iteration ()
380 return gtk_main_iteration_do (TRUE);
384 gtk_main_iteration_do (gboolean blocking)
386 GtkWidget *event_widget;
387 GtkWidget *grab_widget;
388 GdkEvent *event = NULL;
391 iteration_done = FALSE;
393 /* If this is a recursive call, and there are pending timeouts or
394 * idles, finish them, then return immediately to avoid blocking
397 if (current_timeouts)
399 gtk_handle_current_timeouts( gdk_time_get());
400 return iteration_done;
404 gtk_handle_current_idles ();
405 return iteration_done;
408 /* If there is a valid event in 'next_event' then move it to 'event'
416 /* If we don't have an event then get one.
420 /* Handle setting of the "gdk" timer. If there are no
421 * timeout functions, then the timer is turned off.
422 * If there are timeout functions, then the timer is
423 * set to the shortest timeout interval (which is
424 * the first timeout function).
428 if (blocking) event = gdk_event_get ();
431 /* "gdk_event_get" can return FALSE if the timer goes off
432 * and no events are pending. Therefore, we should make
433 * sure that we got an event before continuing.
437 /* If there are any events pending then get the next one.
439 if (gdk_events_pending () > 0)
440 next_event = gdk_event_get ();
442 /* Try to compress enter/leave notify events. These event
443 * pairs occur when the mouse is dragged quickly across
444 * a window with many buttons (or through a menu). Instead
445 * of highlighting and de-highlighting each widget that
446 * is crossed it is better to simply de-highlight the widget
447 * which contained the mouse initially and highlight the
448 * widget which ends up containing the mouse.
451 if (((event->type == GDK_ENTER_NOTIFY) ||
452 (event->type == GDK_LEAVE_NOTIFY)) &&
453 ((next_event->type == GDK_ENTER_NOTIFY) ||
454 (next_event->type == GDK_LEAVE_NOTIFY)) &&
455 (next_event->type != event->type) &&
456 (next_event->any.window == event->any.window))
458 gdk_event_free (event);
459 gdk_event_free (next_event);
462 goto event_handling_done;
465 /* Find the widget which got the event. We store the widget
466 * in the user_data field of GdkWindow's.
467 * Ignore the event if we don't have a widget for it, except
468 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
469 * Though this happens rarely, bogus events can occour
470 * for e.g. destroyed GdkWindows.
472 event_widget = gtk_get_event_widget (event);
475 /* To handle selection INCR transactions, we select
476 * PropertyNotify events on the requestor window and create
477 * a corresponding (fake) GdkWindow so that events get
478 * here. There won't be a widget though, so we have to handle
481 if (event->type == GDK_PROPERTY_NOTIFY)
482 gtk_selection_incr_event (event->any.window,
485 gdk_event_free (event);
487 goto event_handling_done;
490 /* Push the event onto a stack of current events for
491 * gtk_current_event_get().
493 current_events = g_list_prepend (current_events, event);
495 /* If there is a grab in effect...
499 grab_widget = grabs->data;
501 /* If the grab widget is an ancestor of the event widget
502 * then we send the event to the original event widget.
503 * This is the key to implementing modality.
505 if (gtk_widget_is_ancestor (event_widget, grab_widget))
506 grab_widget = event_widget;
510 grab_widget = event_widget;
513 /* Not all events get sent to the grabbing widget.
514 * The delete, destroy, expose, focus change and resize
515 * events still get sent to the event widget because
516 * 1) these events have no meaning for the grabbing widget
517 * and 2) redirecting these events to the grabbing widget
518 * could cause the display to be messed up.
526 gtk_widget_ref (event_widget);
527 if (!gtk_widget_event (event_widget, event) &&
528 !GTK_OBJECT_DESTROYED (event_widget))
529 gtk_widget_destroy (event_widget);
530 gtk_widget_unref (event_widget);
534 gtk_widget_ref (event_widget);
535 gtk_widget_event (event_widget, event);
536 if (!GTK_OBJECT_DESTROYED (event_widget))
537 gtk_widget_destroy (event_widget);
538 gtk_widget_unref (event_widget);
541 case GDK_PROPERTY_NOTIFY:
544 case GDK_FOCUS_CHANGE:
548 case GDK_SELECTION_CLEAR:
549 case GDK_SELECTION_REQUEST:
550 case GDK_SELECTION_NOTIFY:
551 case GDK_CLIENT_EVENT:
553 case GDK_DRAG_REQUEST:
556 case GDK_DROP_DATA_AVAIL:
557 case GDK_VISIBILITY_NOTIFY:
558 gtk_widget_event (event_widget, event);
562 case GDK_KEY_RELEASE:
565 if (gtk_invoke_key_snoopers (grab_widget, event))
568 /* else fall through */
569 case GDK_MOTION_NOTIFY:
570 case GDK_BUTTON_PRESS:
571 case GDK_2BUTTON_PRESS:
572 case GDK_3BUTTON_PRESS:
573 case GDK_BUTTON_RELEASE:
574 case GDK_PROXIMITY_IN:
575 case GDK_PROXIMITY_OUT:
576 case GDK_OTHER_EVENT:
577 gtk_propagate_event (grab_widget, event);
580 case GDK_ENTER_NOTIFY:
581 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
583 gtk_widget_event (grab_widget, event);
584 if (event_widget == grab_widget)
585 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
589 case GDK_LEAVE_NOTIFY:
590 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
592 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
593 gtk_widget_event (event_widget, event);
595 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
596 gtk_widget_event (grab_widget, event);
600 tmp_list = current_events;
601 current_events = g_list_remove_link (current_events, tmp_list);
602 g_list_free_1 (tmp_list);
604 gdk_event_free (event);
608 if (gdk_events_pending() == 0)
614 /* Handle timeout functions that may have expired.
616 gtk_handle_timeouts ();
618 return iteration_done;
634 gtk_grab_add (GtkWidget *widget)
636 g_return_if_fail (widget != NULL);
638 if (!GTK_WIDGET_HAS_GRAB (widget))
640 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
642 grabs = g_slist_prepend (grabs, widget);
643 gtk_widget_ref (widget);
648 gtk_grab_get_current (void)
651 return GTK_WIDGET (grabs->data);
656 gtk_grab_remove (GtkWidget *widget)
658 g_return_if_fail (widget != NULL);
660 if (GTK_WIDGET_HAS_GRAB (widget))
662 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
664 grabs = g_slist_remove (grabs, widget);
665 gtk_widget_unref (widget);
670 gtk_init_add (GtkFunction function,
673 GtkInitFunction *init;
675 init = g_new (GtkInitFunction, 1);
676 init->function = function;
679 init_functions = g_list_prepend (init_functions, init);
683 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
686 GtkKeySnooperData *data;
687 static guint snooper_id = 1;
689 g_return_val_if_fail (snooper != NULL, 0);
691 data = g_new (GtkKeySnooperData, 1);
692 data->func = snooper;
693 data->func_data = func_data;
694 data->id = snooper_id++;
695 key_snoopers = g_slist_prepend (key_snoopers, data);
701 gtk_key_snooper_remove (guint snooper_id)
703 GtkKeySnooperData *data = NULL;
706 slist = key_snoopers;
710 if (data->id == snooper_id)
717 key_snoopers = g_slist_remove (key_snoopers, data);
721 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
725 gint return_val = FALSE;
727 slist = key_snoopers;
728 while (slist && !return_val)
730 GtkKeySnooperData *data;
734 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
741 gtk_timeout_add_full (guint32 interval,
742 GtkFunction function,
743 GtkCallbackMarshal marshal,
745 GtkDestroyNotify destroy)
747 static guint timeout_tag = 1;
748 GtkTimeoutFunction *timeoutf;
750 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
752 /* Create a new timeout function structure.
753 * The start time is the current time.
755 if (!timeout_mem_chunk)
756 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
757 1024, G_ALLOC_AND_FREE);
759 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
761 timeoutf->tag = timeout_tag++;
762 timeoutf->start = gdk_time_get ();
763 timeoutf->interval = interval;
764 timeoutf->originterval = interval;
765 timeoutf->function = function;
766 timeoutf->marshal = marshal;
767 timeoutf->data = data;
768 timeoutf->destroy = destroy;
770 gtk_timeout_insert (timeoutf);
772 return timeoutf->tag;
776 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
778 if (timeoutf->destroy)
779 (timeoutf->destroy) (timeoutf->data);
780 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
784 gtk_timeout_add (guint32 interval,
785 GtkFunction function,
788 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
792 gtk_timeout_add_interp (guint32 interval,
793 GtkCallbackMarshal function,
795 GtkDestroyNotify destroy)
797 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
801 gtk_timeout_remove (guint tag)
803 GtkTimeoutFunction *timeoutf;
806 /* Remove a timeout function.
807 * (Which, basically, involves searching the
810 tmp_list = timeout_functions;
813 timeoutf = tmp_list->data;
815 if (timeoutf->tag == tag)
817 timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
818 g_list_free (tmp_list);
819 gtk_timeout_destroy (timeoutf);
824 tmp_list = tmp_list->next;
827 tmp_list = current_timeouts;
830 timeoutf = tmp_list->data;
832 if (timeoutf->tag == tag)
834 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
835 g_list_free (tmp_list);
836 gtk_timeout_destroy (timeoutf);
841 tmp_list = tmp_list->next;
845 /* We rely on some knowledge of how g_list_insert_sorted works to make
846 * sure that we insert at the _end_ of the idles of this priority
849 gtk_idle_compare (gpointer a, gpointer b)
851 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
856 gtk_quit_add_full (guint main_level,
857 GtkFunction function,
858 GtkCallbackMarshal marshal,
860 GtkDestroyNotify destroy)
862 static guint quit_id = 1;
863 GtkQuitFunction *quitf;
865 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
868 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
869 512, G_ALLOC_AND_FREE);
871 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
873 quitf->id = quit_id++;
874 quitf->main_level = main_level;
875 quitf->function = function;
876 quitf->marshal = marshal;
878 quitf->destroy = destroy;
880 quit_functions = g_list_prepend (quit_functions, quitf);
886 gtk_idle_add_full (gint priority,
887 GtkFunction function,
888 GtkCallbackMarshal marshal,
890 GtkDestroyNotify destroy)
892 static guint idle_tag = 1;
893 GtkIdleFunction *idlef;
895 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
898 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
899 1024, G_ALLOC_AND_FREE);
901 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
903 idlef->tag = idle_tag++;
904 idlef->priority = priority;
905 idlef->function = function;
906 idlef->marshal = marshal;
908 idlef->destroy = destroy;
910 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
916 gtk_idle_add_interp (GtkCallbackMarshal marshal,
918 GtkDestroyNotify destroy)
920 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
924 gtk_idle_destroy (GtkIdleFunction *idlef)
927 idlef->destroy (idlef->data);
928 g_mem_chunk_free (idle_mem_chunk, idlef);
932 gtk_quit_destroy (GtkQuitFunction *quitf)
935 quitf->destroy (quitf->data);
936 g_mem_chunk_free (quit_mem_chunk, quitf);
940 gtk_quit_add (guint main_level,
941 GtkFunction function,
944 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
948 gtk_idle_add (GtkFunction function,
951 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
955 gtk_idle_add_priority (gint priority,
956 GtkFunction function,
959 return gtk_idle_add_full (priority, function, NULL, data, NULL);
963 gtk_quit_remove (guint id)
965 GtkQuitFunction *quitf;
968 tmp_list = quit_functions;
971 quitf = tmp_list->data;
975 quit_functions = g_list_remove_link (quit_functions, tmp_list);
976 g_list_free (tmp_list);
977 gtk_quit_destroy (quitf);
982 tmp_list = tmp_list->next;
987 gtk_quit_remove_by_data (gpointer data)
989 GtkQuitFunction *quitf;
992 tmp_list = quit_functions;
995 quitf = tmp_list->data;
997 if (quitf->data == data)
999 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1000 g_list_free (tmp_list);
1001 gtk_quit_destroy (quitf);
1006 tmp_list = tmp_list->next;
1011 gtk_idle_remove (guint tag)
1013 GtkIdleFunction *idlef;
1016 tmp_list = idle_functions;
1019 idlef = tmp_list->data;
1021 if (idlef->tag == tag)
1023 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1024 g_list_free (tmp_list);
1025 gtk_idle_destroy (idlef);
1030 tmp_list = tmp_list->next;
1033 tmp_list = current_idles;
1036 idlef = tmp_list->data;
1038 if (idlef->tag == tag)
1040 current_idles = g_list_remove_link (current_idles, tmp_list);
1041 g_list_free (tmp_list);
1042 gtk_idle_destroy (idlef);
1047 tmp_list = tmp_list->next;
1052 gtk_idle_remove_by_data (gpointer data)
1054 GtkIdleFunction *idlef;
1057 tmp_list = idle_functions;
1060 idlef = tmp_list->data;
1062 if (idlef->data == data)
1064 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1065 g_list_free (tmp_list);
1066 gtk_idle_destroy (idlef);
1071 tmp_list = tmp_list->next;
1074 tmp_list = current_idles;
1077 idlef = tmp_list->data;
1079 if (idlef->data == data)
1081 current_idles = g_list_remove_link (current_idles, tmp_list);
1082 g_list_free (tmp_list);
1083 gtk_idle_destroy (idlef);
1088 tmp_list = tmp_list->next;
1093 gtk_invoke_input_function (GtkInputFunction *input,
1095 GdkInputCondition condition)
1098 args[0].type = GTK_TYPE_INT;
1099 args[0].name = NULL;
1100 GTK_VALUE_INT(args[0]) = source;
1101 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1102 args[1].name = NULL;
1103 GTK_VALUE_FLAGS(args[1]) = condition;
1104 args[2].type = GTK_TYPE_NONE;
1105 args[2].name = NULL;
1107 input->callback (NULL, input->data, 2, args);
1111 gtk_destroy_input_function (GtkInputFunction *input)
1114 (input->destroy) (input->data);
1119 gtk_input_add_full (gint source,
1120 GdkInputCondition condition,
1121 GdkInputFunction function,
1122 GtkCallbackMarshal marshal,
1124 GtkDestroyNotify destroy)
1128 GtkInputFunction *input;
1130 input = g_new (GtkInputFunction, 1);
1131 input->callback = marshal;
1133 input->destroy = destroy;
1135 return gdk_input_add_full (source,
1137 (GdkInputFunction) gtk_invoke_input_function,
1139 (GdkDestroyNotify) gtk_destroy_input_function);
1142 return gdk_input_add_full (source, condition, function, data, destroy);
1146 gtk_input_add_interp (gint source,
1147 GdkInputCondition condition,
1148 GtkCallbackMarshal callback,
1150 GtkDestroyNotify destroy)
1152 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1156 gtk_input_remove (guint tag)
1158 gdk_input_remove (tag);
1162 gtk_get_current_event ()
1165 return gdk_event_copy ((GdkEvent *) current_events->data);
1171 gtk_get_event_widget (GdkEvent *event)
1176 if (event->any.window)
1177 gdk_window_get_user_data (event->any.window, (void**) &widget);
1187 initialized = FALSE;
1188 gtk_preview_uninit ();
1193 /* We rely on some knowledge of how g_list_insert_sorted works to make
1194 * sure that we insert after timeouts of equal interval
1197 gtk_timeout_compare (gpointer a, gpointer b)
1199 return (((GtkTimeoutFunction *)a)->interval <
1200 ((GtkTimeoutFunction *)b)->interval)
1205 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1207 g_assert (timeoutf != NULL);
1209 /* Insert the timeout function appropriately.
1210 * Appropriately meaning sort it into the list
1211 * of timeout functions.
1213 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1214 gtk_timeout_compare);
1218 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1220 if (!timeoutf->marshal)
1221 return timeoutf->function (timeoutf->data);
1225 gint ret_val = FALSE;
1226 args[0].name = NULL;
1227 args[0].type = GTK_TYPE_BOOL;
1228 args[0].d.pointer_data = &ret_val;
1229 ((GtkCallbackMarshal)timeoutf->function) (NULL,
1237 gtk_handle_current_timeouts (guint32 the_time)
1240 GtkTimeoutFunction *timeoutf;
1242 while (current_timeouts)
1244 tmp_list = current_timeouts;
1245 timeoutf = tmp_list->data;
1247 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1248 g_list_free (tmp_list);
1250 if (gtk_invoke_timeout_function (timeoutf) == FALSE)
1252 gtk_timeout_destroy (timeoutf);
1256 timeoutf->interval = timeoutf->originterval;
1257 timeoutf->start = the_time;
1258 gtk_timeout_insert (timeoutf);
1264 gtk_handle_timeouts ()
1269 GtkTimeoutFunction *timeoutf;
1271 /* Caller must already have called gtk_handle_current_timeouts if
1273 g_assert (current_timeouts == NULL);
1275 if (timeout_functions)
1277 the_time = gdk_time_get ();
1279 tmp_list = timeout_functions;
1282 timeoutf = tmp_list->data;
1284 if (timeoutf->interval <= (the_time - timeoutf->start))
1286 tmp_list2 = tmp_list;
1287 tmp_list = tmp_list->next;
1289 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1290 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1294 timeoutf->interval -= (the_time - timeoutf->start);
1295 timeoutf->start = the_time;
1296 tmp_list = tmp_list->next;
1300 if (current_timeouts)
1301 gtk_handle_current_timeouts(the_time);
1306 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1308 if (!quitf->marshal)
1309 return quitf->function (quitf->data);
1313 gint ret_val = FALSE;
1315 args[0].name = NULL;
1316 args[0].type = GTK_TYPE_BOOL;
1317 args[0].d.pointer_data = &ret_val;
1318 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1326 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1328 if (!idlef->marshal)
1329 return idlef->function (idlef->data);
1333 gint ret_val = FALSE;
1335 args[0].name = NULL;
1336 args[0].type = GTK_TYPE_BOOL;
1337 args[0].d.pointer_data = &ret_val;
1338 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1346 gtk_handle_current_idles ()
1350 GtkIdleFunction *idlef;
1352 while (current_idles)
1354 tmp_list = current_idles;
1355 idlef = tmp_list->data;
1357 current_idles = g_list_remove_link (current_idles, tmp_list);
1359 if (gtk_idle_invoke_function (idlef) == FALSE)
1361 g_list_free (tmp_list);
1362 gtk_idle_destroy (idlef);
1366 /* Insert the idle function back into the list of idle
1367 * functions at the end of the idles of this priority
1369 tmp_list2 = idle_functions;
1371 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1372 tmp_list2 = tmp_list2->next;
1375 idle_functions = g_list_concat (idle_functions, tmp_list);
1376 else if (tmp_list2 == idle_functions)
1378 tmp_list->next = idle_functions;
1380 idle_functions->prev = tmp_list;
1381 idle_functions = tmp_list;
1385 tmp_list->prev = tmp_list2->prev;
1386 tmp_list->next = tmp_list2;
1387 tmp_list2->prev->next = tmp_list;
1388 tmp_list2->prev = tmp_list;
1397 /* Caller must already have called gtk_handle_current_idles if
1400 g_assert (current_idles == NULL);
1402 /* Handle only the idle functions that have the highest priority */
1408 tmp_list = idle_functions;
1409 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1412 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1413 tmp_list = tmp_list->next;
1415 current_idles = idle_functions;
1416 idle_functions = tmp_list;
1420 tmp_list->prev->next = NULL;
1421 tmp_list->prev = NULL;
1424 gtk_handle_current_idles();
1431 GtkTimeoutFunction *timeoutf;
1436 gdk_timer_enable ();
1438 else if (timeout_functions)
1440 timeoutf = timeout_functions->data;
1441 gdk_timer_set (timeoutf->interval);
1442 gdk_timer_enable ();
1447 gdk_timer_disable ();
1452 gtk_propagate_event (GtkWidget *widget,
1459 g_return_if_fail (widget != NULL);
1460 g_return_if_fail (event != NULL);
1462 handled_event = FALSE;
1463 gtk_widget_ref (widget);
1465 if ((event->type == GDK_KEY_PRESS) ||
1466 (event->type == GDK_KEY_RELEASE))
1469 /* Only send key events to window widgets.
1470 * The window widget will in turn pass the
1471 * key event on to the currently focused widget
1474 parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1475 handled_event = (parent &&
1476 GTK_WIDGET_IS_SENSITIVE (parent) &&
1477 gtk_widget_event (parent, event));
1480 /* Other events get propagated up the widget tree
1481 * so that parents can see the button and motion
1482 * events of the children.
1485 while (!handled_event && tmp)
1487 gtk_widget_ref (tmp);
1488 handled_event = !GTK_WIDGET_IS_SENSITIVE (tmp) || gtk_widget_event (tmp, event);
1489 parent = tmp->parent;
1490 gtk_widget_unref (tmp);
1494 gtk_widget_unref (widget);
1499 gtk_error (gchar *str)
1505 gtk_warning (gchar *str)
1511 gtk_message (gchar *str)
1517 gtk_print (gchar *str)
1519 static GtkWidget *window = NULL;
1520 static GtkWidget *text;
1521 static int level = 0;
1525 GtkWidget *hscrollbar;
1526 GtkWidget *vscrollbar;
1527 GtkWidget *separator;
1532 fputs (str, stdout);
1539 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1541 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1542 (GtkSignalFunc) gtk_widget_destroyed,
1545 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1547 box1 = gtk_vbox_new (FALSE, 0);
1548 gtk_container_add (GTK_CONTAINER (window), box1);
1549 gtk_widget_show (box1);
1552 box2 = gtk_vbox_new (FALSE, 10);
1553 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1554 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1555 gtk_widget_show (box2);
1558 table = gtk_table_new (2, 2, FALSE);
1559 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1560 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1561 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1562 gtk_widget_show (table);
1564 text = gtk_text_new (NULL, NULL);
1565 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1566 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1567 gtk_widget_show (text);
1568 gtk_widget_realize (text);
1570 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1571 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1572 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1573 gtk_widget_show (hscrollbar);
1575 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1576 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1577 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1578 gtk_widget_show (vscrollbar);
1580 separator = gtk_hseparator_new ();
1581 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1582 gtk_widget_show (separator);
1585 box2 = gtk_vbox_new (FALSE, 10);
1586 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1587 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1588 gtk_widget_show (box2);
1591 button = gtk_button_new_with_label ("close");
1592 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1593 (GtkSignalFunc) gtk_widget_hide,
1594 GTK_OBJECT (window));
1595 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1596 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1597 gtk_widget_grab_default (button);
1598 gtk_widget_show (button);
1602 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1605 if (!GTK_WIDGET_VISIBLE (window))
1606 gtk_widget_show (window);