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_widget_destroy (event_widget);
529 gtk_widget_unref (event_widget);
533 gtk_widget_ref (event_widget);
534 gtk_widget_event (event_widget, event);
535 gtk_widget_destroy (event_widget);
536 gtk_widget_unref (event_widget);
539 case GDK_PROPERTY_NOTIFY:
542 case GDK_FOCUS_CHANGE:
546 case GDK_SELECTION_CLEAR:
547 case GDK_SELECTION_REQUEST:
548 case GDK_SELECTION_NOTIFY:
549 case GDK_CLIENT_EVENT:
551 case GDK_DRAG_REQUEST:
554 case GDK_DROP_DATA_AVAIL:
555 case GDK_VISIBILITY_NOTIFY:
556 gtk_widget_event (event_widget, event);
560 case GDK_KEY_RELEASE:
563 if (gtk_invoke_key_snoopers (grab_widget, event))
566 /* else fall through */
567 case GDK_MOTION_NOTIFY:
568 case GDK_BUTTON_PRESS:
569 case GDK_2BUTTON_PRESS:
570 case GDK_3BUTTON_PRESS:
571 case GDK_BUTTON_RELEASE:
572 case GDK_PROXIMITY_IN:
573 case GDK_PROXIMITY_OUT:
574 case GDK_OTHER_EVENT:
575 gtk_propagate_event (grab_widget, event);
578 case GDK_ENTER_NOTIFY:
579 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
581 gtk_widget_event (grab_widget, event);
582 if (event_widget == grab_widget)
583 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
587 case GDK_LEAVE_NOTIFY:
588 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
590 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
591 gtk_widget_event (event_widget, event);
593 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
594 gtk_widget_event (grab_widget, event);
598 tmp_list = current_events;
599 current_events = g_list_remove_link (current_events, tmp_list);
600 g_list_free_1 (tmp_list);
602 gdk_event_free (event);
606 if (gdk_events_pending() == 0)
612 /* Handle timeout functions that may have expired.
614 gtk_handle_timeouts ();
616 return iteration_done;
632 gtk_grab_add (GtkWidget *widget)
634 g_return_if_fail (widget != NULL);
636 if (!GTK_WIDGET_HAS_GRAB (widget))
638 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
640 grabs = g_slist_prepend (grabs, widget);
641 gtk_widget_ref (widget);
646 gtk_grab_get_current (void)
649 return GTK_WIDGET (grabs->data);
654 gtk_grab_remove (GtkWidget *widget)
656 g_return_if_fail (widget != NULL);
658 if (GTK_WIDGET_HAS_GRAB (widget))
660 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
662 grabs = g_slist_remove (grabs, widget);
663 gtk_widget_unref (widget);
668 gtk_init_add (GtkFunction function,
671 GtkInitFunction *init;
673 init = g_new (GtkInitFunction, 1);
674 init->function = function;
677 init_functions = g_list_prepend (init_functions, init);
681 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
684 GtkKeySnooperData *data;
685 static guint snooper_id = 1;
687 g_return_val_if_fail (snooper != NULL, 0);
689 data = g_new (GtkKeySnooperData, 1);
690 data->func = snooper;
691 data->func_data = func_data;
692 data->id = snooper_id++;
693 key_snoopers = g_slist_prepend (key_snoopers, data);
699 gtk_key_snooper_remove (guint snooper_id)
701 GtkKeySnooperData *data = NULL;
704 slist = key_snoopers;
708 if (data->id == snooper_id)
715 key_snoopers = g_slist_remove (key_snoopers, data);
719 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
723 gint return_val = FALSE;
725 slist = key_snoopers;
726 while (slist && !return_val)
728 GtkKeySnooperData *data;
732 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
739 gtk_timeout_add_full (guint32 interval,
740 GtkFunction function,
741 GtkCallbackMarshal marshal,
743 GtkDestroyNotify destroy)
745 static guint timeout_tag = 1;
746 GtkTimeoutFunction *timeoutf;
748 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
750 /* Create a new timeout function structure.
751 * The start time is the current time.
753 if (!timeout_mem_chunk)
754 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
755 1024, G_ALLOC_AND_FREE);
757 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
759 timeoutf->tag = timeout_tag++;
760 timeoutf->start = gdk_time_get ();
761 timeoutf->interval = interval;
762 timeoutf->originterval = interval;
763 timeoutf->function = function;
764 timeoutf->marshal = marshal;
765 timeoutf->data = data;
766 timeoutf->destroy = destroy;
768 gtk_timeout_insert (timeoutf);
770 return timeoutf->tag;
774 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
776 if (timeoutf->destroy)
777 (timeoutf->destroy) (timeoutf->data);
778 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
782 gtk_timeout_add (guint32 interval,
783 GtkFunction function,
786 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
790 gtk_timeout_add_interp (guint32 interval,
791 GtkCallbackMarshal function,
793 GtkDestroyNotify destroy)
795 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
799 gtk_timeout_remove (guint tag)
801 GtkTimeoutFunction *timeoutf;
804 /* Remove a timeout function.
805 * (Which, basically, involves searching the
808 tmp_list = timeout_functions;
811 timeoutf = tmp_list->data;
813 if (timeoutf->tag == tag)
815 timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
816 g_list_free (tmp_list);
817 gtk_timeout_destroy (timeoutf);
822 tmp_list = tmp_list->next;
825 tmp_list = current_timeouts;
828 timeoutf = tmp_list->data;
830 if (timeoutf->tag == tag)
832 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
833 g_list_free (tmp_list);
834 gtk_timeout_destroy (timeoutf);
839 tmp_list = tmp_list->next;
843 /* We rely on some knowledge of how g_list_insert_sorted works to make
844 * sure that we insert at the _end_ of the idles of this priority
847 gtk_idle_compare (gpointer a, gpointer b)
849 return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
854 gtk_quit_add_full (guint main_level,
855 GtkFunction function,
856 GtkCallbackMarshal marshal,
858 GtkDestroyNotify destroy)
860 static guint quit_id = 1;
861 GtkQuitFunction *quitf;
863 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
866 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
867 512, G_ALLOC_AND_FREE);
869 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
871 quitf->id = quit_id++;
872 quitf->main_level = main_level;
873 quitf->function = function;
874 quitf->marshal = marshal;
876 quitf->destroy = destroy;
878 quit_functions = g_list_prepend (quit_functions, quitf);
884 gtk_idle_add_full (gint priority,
885 GtkFunction function,
886 GtkCallbackMarshal marshal,
888 GtkDestroyNotify destroy)
890 static guint idle_tag = 1;
891 GtkIdleFunction *idlef;
893 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
896 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
897 1024, G_ALLOC_AND_FREE);
899 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
901 idlef->tag = idle_tag++;
902 idlef->priority = priority;
903 idlef->function = function;
904 idlef->marshal = marshal;
906 idlef->destroy = destroy;
908 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
914 gtk_idle_add_interp (GtkCallbackMarshal marshal,
916 GtkDestroyNotify destroy)
918 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
922 gtk_idle_destroy (GtkIdleFunction *idlef)
925 idlef->destroy (idlef->data);
926 g_mem_chunk_free (idle_mem_chunk, idlef);
930 gtk_quit_destroy (GtkQuitFunction *quitf)
933 quitf->destroy (quitf->data);
934 g_mem_chunk_free (quit_mem_chunk, quitf);
938 gtk_quit_add (guint main_level,
939 GtkFunction function,
942 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
946 gtk_idle_add (GtkFunction function,
949 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
953 gtk_idle_add_priority (gint priority,
954 GtkFunction function,
957 return gtk_idle_add_full (priority, function, NULL, data, NULL);
961 gtk_quit_remove (guint id)
963 GtkQuitFunction *quitf;
966 tmp_list = quit_functions;
969 quitf = tmp_list->data;
973 quit_functions = g_list_remove_link (quit_functions, tmp_list);
974 g_list_free (tmp_list);
975 gtk_quit_destroy (quitf);
980 tmp_list = tmp_list->next;
985 gtk_quit_remove_by_data (gpointer data)
987 GtkQuitFunction *quitf;
990 tmp_list = quit_functions;
993 quitf = tmp_list->data;
995 if (quitf->data == data)
997 quit_functions = g_list_remove_link (quit_functions, tmp_list);
998 g_list_free (tmp_list);
999 gtk_quit_destroy (quitf);
1004 tmp_list = tmp_list->next;
1009 gtk_idle_remove (guint tag)
1011 GtkIdleFunction *idlef;
1014 tmp_list = idle_functions;
1017 idlef = tmp_list->data;
1019 if (idlef->tag == tag)
1021 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1022 g_list_free (tmp_list);
1023 gtk_idle_destroy (idlef);
1028 tmp_list = tmp_list->next;
1031 tmp_list = current_idles;
1034 idlef = tmp_list->data;
1036 if (idlef->tag == tag)
1038 current_idles = g_list_remove_link (current_idles, tmp_list);
1039 g_list_free (tmp_list);
1040 gtk_idle_destroy (idlef);
1045 tmp_list = tmp_list->next;
1050 gtk_idle_remove_by_data (gpointer data)
1052 GtkIdleFunction *idlef;
1055 tmp_list = idle_functions;
1058 idlef = tmp_list->data;
1060 if (idlef->data == data)
1062 idle_functions = g_list_remove_link (idle_functions, tmp_list);
1063 g_list_free (tmp_list);
1064 gtk_idle_destroy (idlef);
1069 tmp_list = tmp_list->next;
1072 tmp_list = current_idles;
1075 idlef = tmp_list->data;
1077 if (idlef->data == data)
1079 current_idles = g_list_remove_link (current_idles, tmp_list);
1080 g_list_free (tmp_list);
1081 gtk_idle_destroy (idlef);
1086 tmp_list = tmp_list->next;
1091 gtk_invoke_input_function (GtkInputFunction *input,
1093 GdkInputCondition condition)
1096 args[0].type = GTK_TYPE_INT;
1097 args[0].name = NULL;
1098 GTK_VALUE_INT(args[0]) = source;
1099 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1100 args[1].name = NULL;
1101 GTK_VALUE_FLAGS(args[1]) = condition;
1102 args[2].type = GTK_TYPE_NONE;
1103 args[2].name = NULL;
1105 input->callback (NULL, input->data, 2, args);
1109 gtk_destroy_input_function (GtkInputFunction *input)
1112 (input->destroy) (input->data);
1117 gtk_input_add_full (gint source,
1118 GdkInputCondition condition,
1119 GdkInputFunction function,
1120 GtkCallbackMarshal marshal,
1122 GtkDestroyNotify destroy)
1126 GtkInputFunction *input;
1128 input = g_new (GtkInputFunction, 1);
1129 input->callback = marshal;
1131 input->destroy = destroy;
1133 return gdk_input_add_full (source,
1135 (GdkInputFunction) gtk_invoke_input_function,
1137 (GdkDestroyNotify) gtk_destroy_input_function);
1140 return gdk_input_add_full (source, condition, function, data, destroy);
1144 gtk_input_add_interp (gint source,
1145 GdkInputCondition condition,
1146 GtkCallbackMarshal callback,
1148 GtkDestroyNotify destroy)
1150 return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1154 gtk_input_remove (guint tag)
1156 gdk_input_remove (tag);
1160 gtk_get_current_event ()
1163 return gdk_event_copy ((GdkEvent *) current_events->data);
1169 gtk_get_event_widget (GdkEvent *event)
1174 if (event->any.window)
1175 gdk_window_get_user_data (event->any.window, (void**) &widget);
1185 initialized = FALSE;
1186 gtk_preview_uninit ();
1191 /* We rely on some knowledge of how g_list_insert_sorted works to make
1192 * sure that we insert after timeouts of equal interval
1195 gtk_timeout_compare (gpointer a, gpointer b)
1197 return (((GtkTimeoutFunction *)a)->interval <
1198 ((GtkTimeoutFunction *)b)->interval)
1203 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1205 g_assert (timeoutf != NULL);
1207 /* Insert the timeout function appropriately.
1208 * Appropriately meaning sort it into the list
1209 * of timeout functions.
1211 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1212 gtk_timeout_compare);
1216 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1218 if (!timeoutf->marshal)
1219 return timeoutf->function (timeoutf->data);
1223 gint ret_val = FALSE;
1224 args[0].name = NULL;
1225 args[0].type = GTK_TYPE_BOOL;
1226 args[0].d.pointer_data = &ret_val;
1227 ((GtkCallbackMarshal)timeoutf->function) (NULL,
1235 gtk_handle_current_timeouts (guint32 the_time)
1238 GtkTimeoutFunction *timeoutf;
1240 while (current_timeouts)
1242 tmp_list = current_timeouts;
1243 timeoutf = tmp_list->data;
1245 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1246 g_list_free (tmp_list);
1248 if (gtk_invoke_timeout_function (timeoutf) == FALSE)
1250 gtk_timeout_destroy (timeoutf);
1254 timeoutf->interval = timeoutf->originterval;
1255 timeoutf->start = the_time;
1256 gtk_timeout_insert (timeoutf);
1262 gtk_handle_timeouts ()
1267 GtkTimeoutFunction *timeoutf;
1269 /* Caller must already have called gtk_handle_current_timeouts if
1271 g_assert (current_timeouts == NULL);
1273 if (timeout_functions)
1275 the_time = gdk_time_get ();
1277 tmp_list = timeout_functions;
1280 timeoutf = tmp_list->data;
1282 if (timeoutf->interval <= (the_time - timeoutf->start))
1284 tmp_list2 = tmp_list;
1285 tmp_list = tmp_list->next;
1287 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1288 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1292 timeoutf->interval -= (the_time - timeoutf->start);
1293 timeoutf->start = the_time;
1294 tmp_list = tmp_list->next;
1298 if (current_timeouts)
1299 gtk_handle_current_timeouts(the_time);
1304 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1306 if (!quitf->marshal)
1307 return quitf->function (quitf->data);
1311 gint ret_val = FALSE;
1313 args[0].name = NULL;
1314 args[0].type = GTK_TYPE_BOOL;
1315 args[0].d.pointer_data = &ret_val;
1316 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1324 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1326 if (!idlef->marshal)
1327 return idlef->function (idlef->data);
1331 gint ret_val = FALSE;
1333 args[0].name = NULL;
1334 args[0].type = GTK_TYPE_BOOL;
1335 args[0].d.pointer_data = &ret_val;
1336 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1344 gtk_handle_current_idles ()
1348 GtkIdleFunction *idlef;
1350 while (current_idles)
1352 tmp_list = current_idles;
1353 idlef = tmp_list->data;
1355 current_idles = g_list_remove_link (current_idles, tmp_list);
1357 if (gtk_idle_invoke_function (idlef) == FALSE)
1359 g_list_free (tmp_list);
1360 gtk_idle_destroy (idlef);
1364 /* Insert the idle function back into the list of idle
1365 * functions at the end of the idles of this priority
1367 tmp_list2 = idle_functions;
1369 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1370 tmp_list2 = tmp_list2->next;
1373 idle_functions = g_list_concat (idle_functions, tmp_list);
1374 else if (tmp_list2 == idle_functions)
1376 tmp_list->next = idle_functions;
1378 idle_functions->prev = tmp_list;
1379 idle_functions = tmp_list;
1383 tmp_list->prev = tmp_list2->prev;
1384 tmp_list->next = tmp_list2;
1385 tmp_list2->prev->next = tmp_list;
1386 tmp_list2->prev = tmp_list;
1395 /* Caller must already have called gtk_handle_current_idles if
1398 g_assert (current_idles == NULL);
1400 /* Handle only the idle functions that have the highest priority */
1406 tmp_list = idle_functions;
1407 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1410 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1411 tmp_list = tmp_list->next;
1413 current_idles = idle_functions;
1414 idle_functions = tmp_list;
1418 tmp_list->prev->next = NULL;
1419 tmp_list->prev = NULL;
1422 gtk_handle_current_idles();
1429 GtkTimeoutFunction *timeoutf;
1434 gdk_timer_enable ();
1436 else if (timeout_functions)
1438 timeoutf = timeout_functions->data;
1439 gdk_timer_set (timeoutf->interval);
1440 gdk_timer_enable ();
1445 gdk_timer_disable ();
1450 gtk_propagate_event (GtkWidget *widget,
1457 g_return_if_fail (widget != NULL);
1458 g_return_if_fail (event != NULL);
1460 handled_event = FALSE;
1461 gtk_widget_ref (widget);
1463 if ((event->type == GDK_KEY_PRESS) ||
1464 (event->type == GDK_KEY_RELEASE))
1467 /* Only send key events to window widgets.
1468 * The window widget will in turn pass the
1469 * key event on to the currently focused widget
1472 parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1473 handled_event = (parent &&
1474 GTK_WIDGET_IS_SENSITIVE (parent) &&
1475 gtk_widget_event (parent, event));
1478 /* Other events get propagated up the widget tree
1479 * so that parents can see the button and motion
1480 * events of the children.
1483 while (!handled_event && tmp)
1485 gtk_widget_ref (tmp);
1486 handled_event = !GTK_WIDGET_IS_SENSITIVE (tmp) || gtk_widget_event (tmp, event);
1487 parent = tmp->parent;
1488 gtk_widget_unref (tmp);
1492 gtk_widget_unref (widget);
1497 gtk_error (gchar *str)
1503 gtk_warning (gchar *str)
1509 gtk_message (gchar *str)
1515 gtk_print (gchar *str)
1517 static GtkWidget *window = NULL;
1518 static GtkWidget *text;
1519 static int level = 0;
1523 GtkWidget *hscrollbar;
1524 GtkWidget *vscrollbar;
1525 GtkWidget *separator;
1530 fputs (str, stdout);
1537 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1539 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1540 (GtkSignalFunc) gtk_widget_destroyed,
1543 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1545 box1 = gtk_vbox_new (FALSE, 0);
1546 gtk_container_add (GTK_CONTAINER (window), box1);
1547 gtk_widget_show (box1);
1550 box2 = gtk_vbox_new (FALSE, 10);
1551 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1552 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1553 gtk_widget_show (box2);
1556 table = gtk_table_new (2, 2, FALSE);
1557 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1558 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1559 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1560 gtk_widget_show (table);
1562 text = gtk_text_new (NULL, NULL);
1563 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1564 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1565 gtk_widget_show (text);
1566 gtk_widget_realize (text);
1568 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1569 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1570 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1571 gtk_widget_show (hscrollbar);
1573 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1574 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1575 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1576 gtk_widget_show (vscrollbar);
1578 separator = gtk_hseparator_new ();
1579 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1580 gtk_widget_show (separator);
1583 box2 = gtk_vbox_new (FALSE, 10);
1584 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1585 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1586 gtk_widget_show (box2);
1589 button = gtk_button_new_with_label ("close");
1590 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1591 (GtkSignalFunc) gtk_widget_hide,
1592 GTK_OBJECT (window));
1593 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1594 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1595 gtk_widget_grab_default (button);
1596 gtk_widget_show (button);
1600 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1603 if (!GTK_WIDGET_VISIBLE (window))
1604 gtk_widget_show (window);