1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #include <X11/Xlocale.h> /* so we get the right setlocale */
24 #include "gtkbutton.h"
25 #include "gtkfeatures.h"
26 #include "gtkhscrollbar.h"
27 #include "gtkhseparator.h"
29 #include "gtkpreview.h"
31 #include "gtkselection.h"
32 #include "gtksignal.h"
36 #include "gtkvscrollbar.h"
37 #include "gtkwidget.h"
38 #include "gtkwindow.h"
39 #include "gtkprivate.h"
40 #include "gdk/gdki18n.h"
41 #include "../config.h"
45 /* Private type definitions
47 typedef struct _GtkInitFunction GtkInitFunction;
48 typedef struct _GtkQuitFunction GtkQuitFunction;
49 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
50 typedef struct _GtkIdleFunction GtkIdleFunction;
51 typedef struct _GtkInputFunction GtkInputFunction;
52 typedef struct _GtkKeySnooperData GtkKeySnooperData;
54 struct _GtkInitFunction
60 struct _GtkQuitFunction
64 GtkCallbackMarshal marshal;
67 GtkDestroyNotify destroy;
70 struct _GtkTimeoutFunction
77 GtkCallbackMarshal marshal;
79 GtkDestroyNotify destroy;
82 struct _GtkIdleFunction
86 GtkCallbackMarshal marshal;
89 GtkDestroyNotify destroy;
92 struct _GtkInputFunction
94 GtkCallbackMarshal callback;
96 GtkDestroyNotify destroy;
99 struct _GtkKeySnooperData
101 GtkKeySnoopFunc func;
106 static void gtk_exit_func (void);
107 static gint gtk_quit_invoke_function (GtkQuitFunction *quitf);
108 static void gtk_quit_destroy (GtkQuitFunction *quitf);
109 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
110 static void gtk_handle_current_timeouts (guint32 the_time);
111 static void gtk_handle_current_idles (void);
112 static gint gtk_invoke_key_snoopers (GtkWidget *grab_widget,
114 static void gtk_handle_timeouts (void);
115 static void gtk_handle_idle (void);
116 static void gtk_handle_timer (void);
117 static void gtk_propagate_event (GtkWidget *widget,
120 static void gtk_error (gchar *str);
121 static void gtk_warning (gchar *str);
122 static void gtk_message (gchar *str);
123 static void gtk_print (gchar *str);
126 static gint gtk_idle_remove_from_list (GList **list,
130 static gint gtk_timeout_remove_from_list (GList **list,
134 static gint gtk_idle_compare (gconstpointer a,
137 static gint gtk_timeout_compare (gconstpointer a,
141 const guint gtk_major_version = GTK_MAJOR_VERSION;
142 const guint gtk_minor_version = GTK_MINOR_VERSION;
143 const guint gtk_micro_version = GTK_MICRO_VERSION;
144 const guint gtk_binary_age = GTK_BINARY_AGE;
145 const guint gtk_interface_age = GTK_INTERFACE_AGE;
147 static gboolean iteration_done = FALSE;
148 static guint gtk_main_loop_level = 0;
149 static gint gtk_initialized = FALSE;
150 static GdkEvent *next_event = NULL;
151 static GList *current_events = NULL;
153 static GSList *grabs = NULL; /* A stack of unique grabs. The grabbing
154 * widget is the first one on the list.
156 static GList *init_functions = NULL; /* A list of init functions.
158 static GList *quit_functions = NULL; /* A list of quit functions.
162 /* When handling timeouts, the algorithm is to move all of the expired
163 * timeouts from timeout_functions to current_timeouts then process
164 * them as a batch. If one of the timeouts recursively calls the main
165 * loop, then the remainder of the timeouts in current_timeouts will
166 * be processed before anything else happens.
168 * Each timeout is procesed as follows:
170 * - The timeout is removed from current_timeouts
171 * - The timeout is pushed on the running_timeouts stack
172 * - The timeout is executed
173 * - The timeout stack is popped
174 * - If the timeout function wasn't removed from the stack while executing,
175 * and it returned TRUE, it is added back to timeout_functions, otherwise
176 * it is destroyed if necessary.
178 * gtk_timeout_remove() works by checking for the timeout in
179 * timeout_functions current_timeouts and running_timeouts. If it is
180 * found in one of the first two, it is removed and destroyed. If it
181 * is found in running_timeouts, it is destroyed and ->data is set to
182 * NULL for the stack entry.
184 * Idle functions work pretty much identically.
187 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
188 * when the length of the time interval
189 * remaining. Therefore, the first timeout
190 * function to expire is at the head of
191 * the list and the last to expire is at
192 * the tail of the list. */
193 static GList *idle_functions = NULL; /* A list of idle functions.
196 /* The idle functions / timeouts that are currently being processed
197 * by gtk_handle_current_(timeouts/idles)
199 static GList *current_idles = NULL;
200 static GList *current_timeouts = NULL;
202 /* A stack of idle functions / timeouts that are currently being
205 static GList *running_idles = NULL;
206 static GList *running_timeouts = NULL;
208 static GMemChunk *timeout_mem_chunk = NULL;
209 static GMemChunk *idle_mem_chunk = NULL;
210 static GMemChunk *quit_mem_chunk = NULL;
212 static GSList *key_snoopers = NULL;
214 static GdkVisual *gtk_visual; /* The visual to be used in creating new
217 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
221 guint gtk_debug_flags = 0; /* Global GTK debug flag */
223 #ifdef G_ENABLE_DEBUG
224 static GDebugKey gtk_debug_keys[] = {
225 {"objects", GTK_DEBUG_OBJECTS},
226 {"misc", GTK_DEBUG_MISC},
227 {"signals", GTK_DEBUG_SIGNALS}
230 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
232 #endif /* G_ENABLE_DEBUG */
235 gtk_check_version (guint required_major,
236 guint required_minor,
237 guint required_micro)
239 if (required_major > GTK_MAJOR_VERSION)
240 return "Gtk+ version to old (major mismatch)";
241 if (required_major < GTK_MAJOR_VERSION)
242 return "Gtk+ version to new (major mismatch)";
243 if (required_minor > GTK_MINOR_VERSION)
244 return "Gtk+ version to old (minor mismatch)";
245 if (required_minor < GTK_MINOR_VERSION)
246 return "Gtk+ version to new (minor mismatch)";
247 if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
248 return "Gtk+ version to new (micro mismatch)";
249 if (required_micro > GTK_MICRO_VERSION)
250 return "Gtk+ version to old (micro mismatch)";
255 gint gtk_use_mb = -1;
261 GSList *gtk_modinit_funcs = NULL;
262 gchar *current_locale;
268 g_set_error_handler (gtk_error);
269 g_set_warning_handler (gtk_warning);
270 g_set_message_handler (gtk_message);
271 g_set_print_handler (gtk_print);
274 /* Initialize "gdk". We pass along the 'argc' and 'argv'
275 * parameters as they contain information that GDK uses
277 gdk_init (argc, argv);
279 #ifdef G_ENABLE_DEBUG
281 gchar *debug_string = getenv("GTK_DEBUG");
282 if (debug_string != NULL)
283 gtk_debug_flags = g_parse_debug_string (debug_string,
287 #endif /* G_ENABLE_DEBUG */
293 for (i = 1; i < *argc;)
295 if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
296 strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
298 GModule *module = NULL;
299 GtkModuleInitFunc modinit_func = NULL;
300 gchar *module_name = (*argv)[i] + 12;
302 if (*module_name == '=')
308 module_name = (*argv)[i];
310 if (module_name[0] == '/' ||
311 (module_name[0] == 'l' &&
312 module_name[1] == 'i' &&
313 module_name[2] == 'b'))
314 module_name = g_strdup (module_name);
316 module_name = g_strconcat ("lib", module_name, ".so", NULL);
319 if (g_module_supported ())
321 module = g_module_open (module_name, G_MODULE_BIND_LAZY);
323 g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
326 if (!g_slist_find (gtk_modinit_funcs, modinit_func))
328 g_module_make_resident (module);
329 gtk_modinit_funcs = g_slist_prepend (gtk_modinit_funcs, modinit_func);
333 g_module_close (module);
340 g_warning ("Failed to load module \"%s\": %s",
341 module ? g_module_name (module) : module_name,
344 g_module_close (module);
346 g_free (module_name);
348 else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
350 GLogLevelFlags fatal_mask;
352 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
353 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
354 g_log_set_always_fatal (fatal_mask);
357 #ifdef G_ENABLE_DEBUG
358 else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
359 (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
361 gchar *equal_pos = strchr ((*argv)[i], '=');
363 if (equal_pos != NULL)
365 gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
369 else if ((i + 1) < *argc && (*argv)[i + 1])
371 gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
379 else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
380 (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
382 gchar *equal_pos = strchr ((*argv)[i], '=');
384 if (equal_pos != NULL)
386 gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
390 else if ((i + 1) < *argc && (*argv)[i + 1])
392 gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
400 #endif /* G_ENABLE_DEBUG */
404 for (i = 1; i < *argc; i++)
406 for (k = i; k < *argc; k++)
407 if ((*argv)[k] != NULL)
413 for (j = i + k; j < *argc; j++)
414 (*argv)[j-k] = (*argv)[j];
420 /* Check if there is a good chance the mb functions will handle things
421 * correctly - set if either mblen("\xc0", MB_CUR_MAX) == 1 in the
422 * C locale, or we're using X's mb functions. (-DX_LOCALE && locale != C)
425 current_locale = g_strdup (setlocale (LC_CTYPE, NULL));
428 if ((strcmp (current_locale, "C")) && (strcmp (current_locale, "POSIX")))
433 setlocale (LC_CTYPE, "C");
434 gtk_use_mb = (mblen ("\xc0", MB_CUR_MAX) == 1);
435 setlocale (LC_CTYPE, current_locale);
438 g_free (current_locale);
441 g_message ("%s multi-byte string functions.",
442 gtk_use_mb ? "Using" : "Not using"));
444 /* Initialize the default visual and colormap to be
445 * used in creating widgets. (We want to use the system
446 * defaults so as to be nice to the colormap).
448 gtk_visual = gdk_visual_get_system ();
449 gtk_colormap = gdk_colormap_get_system ();
456 /* Register an exit function to make sure we are able to cleanup.
458 g_atexit (gtk_exit_func);
460 /* Set the 'initialized' flag.
462 gtk_initialized = TRUE;
464 /* initialize modules
466 if (gtk_modinit_funcs)
470 slist = gtk_modinit_funcs;
473 GtkModuleInitFunc modinit;
475 modinit = slist->data;
476 modinit (argc, argv);
480 g_slist_free (gtk_modinit_funcs);
485 gtk_exit (int errorcode)
487 /* Only if "gtk" has been initialized should we de-initialize.
489 /* de-initialisation is done by the gtk_exit_funct(),
490 * no need to do this here (Alex J.)
496 gtk_set_locale (void)
498 return gdk_set_locale ();
506 GtkInitFunction *init;
509 gtk_main_loop_level++;
511 tmp_list = functions = init_functions;
512 init_functions = NULL;
516 init = tmp_list->data;
517 tmp_list = tmp_list->next;
519 (* init->function) (init->data);
522 g_list_free (functions);
524 old_done = iteration_done;
525 while (!gtk_main_iteration ())
527 iteration_done = old_done;
531 GList *reinvoke_list = NULL;
532 GtkQuitFunction *quitf;
534 while (quit_functions)
536 quitf = quit_functions->data;
538 quit_functions = g_list_remove_link (quit_functions, quit_functions);
540 if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
541 gtk_quit_invoke_function (quitf))
543 reinvoke_list = g_list_prepend (reinvoke_list, quitf);
547 g_list_free (tmp_list);
548 gtk_quit_destroy (quitf);
555 work = g_list_last (reinvoke_list);
557 quit_functions->prev = work;
558 work->next = quit_functions;
559 quit_functions = work;
563 gtk_main_loop_level--;
567 gtk_main_level (void)
569 return gtk_main_loop_level;
575 iteration_done = TRUE;
579 gtk_events_pending (void)
583 /* if this function is called from a timeout which will only return
584 * if gtk needs processor time, we need to take iteration_done==TRUE
585 * into account as well.
587 result = iteration_done;
588 result += next_event != NULL;
589 result += gdk_events_pending();
591 result += current_idles != NULL;
592 result += current_timeouts != NULL;
596 result += (idle_functions &&
597 (((GtkIdleFunction *)idle_functions->data)->priority <=
598 GTK_PRIORITY_INTERNAL));
601 if (!result && timeout_functions)
604 GtkTimeoutFunction *timeoutf;
606 the_time = gdk_time_get ();
608 timeoutf = timeout_functions->data;
610 result += timeoutf->interval <= (the_time - timeoutf->start);
617 gtk_main_iteration (void)
619 return gtk_main_iteration_do (TRUE);
623 gtk_main_iteration_do (gboolean blocking)
625 GtkWidget *event_widget;
626 GtkWidget *grab_widget;
627 GdkEvent *event = NULL;
630 iteration_done = FALSE;
632 /* If this is a recursive call, and there are pending timeouts or
633 * idles, finish them, then return immediately to avoid blocking
636 if (current_timeouts)
638 gtk_handle_current_timeouts( gdk_time_get());
643 return iteration_done;
647 gtk_handle_current_idles ();
652 return iteration_done;
655 /* If there is a valid event in 'next_event' then move it to 'event'
663 /* If we don't have an event then get one.
667 /* Handle setting of the "gdk" timer. If there are no
668 * timeout functions, then the timer is turned off.
669 * If there are timeout functions, then the timer is
670 * set to the shortest timeout interval (which is
671 * the first timeout function).
675 if (blocking) event = gdk_event_get ();
678 /* "gdk_event_get" can return FALSE if the timer goes off
679 * and no events are pending. Therefore, we should make
680 * sure that we got an event before continuing.
684 /* If there are any events pending then get the next one.
686 if (gdk_events_pending () > 0)
687 next_event = gdk_event_get ();
689 /* Try to compress enter/leave notify events. These event
690 * pairs occur when the mouse is dragged quickly across
691 * a window with many buttons (or through a menu). Instead
692 * of highlighting and de-highlighting each widget that
693 * is crossed it is better to simply de-highlight the widget
694 * which contained the mouse initially and highlight the
695 * widget which ends up containing the mouse.
698 if (((event->type == GDK_ENTER_NOTIFY) ||
699 (event->type == GDK_LEAVE_NOTIFY)) &&
700 ((next_event->type == GDK_ENTER_NOTIFY) ||
701 (next_event->type == GDK_LEAVE_NOTIFY)) &&
702 (next_event->type != event->type) &&
703 (next_event->any.window == event->any.window))
705 gdk_event_free (event);
706 gdk_event_free (next_event);
709 goto event_handling_done;
712 /* Find the widget which got the event. We store the widget
713 * in the user_data field of GdkWindow's.
714 * Ignore the event if we don't have a widget for it, except
715 * for GDK_PROPERTY_NOTIFY events which are handled specialy.
716 * Though this happens rarely, bogus events can occour
717 * for e.g. destroyed GdkWindows.
719 event_widget = gtk_get_event_widget (event);
722 /* To handle selection INCR transactions, we select
723 * PropertyNotify events on the requestor window and create
724 * a corresponding (fake) GdkWindow so that events get
725 * here. There won't be a widget though, so we have to handle
728 if (event->type == GDK_PROPERTY_NOTIFY)
729 gtk_selection_incr_event (event->any.window,
732 gdk_event_free (event);
734 goto event_handling_done;
737 /* Push the event onto a stack of current events for
738 * gtk_current_event_get().
740 current_events = g_list_prepend (current_events, event);
742 /* If there is a grab in effect...
746 grab_widget = grabs->data;
748 /* If the grab widget is an ancestor of the event widget
749 * then we send the event to the original event widget.
750 * This is the key to implementing modality.
752 if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
753 gtk_widget_is_ancestor (event_widget, grab_widget))
754 grab_widget = event_widget;
758 grab_widget = event_widget;
761 /* Not all events get sent to the grabbing widget.
762 * The delete, destroy, expose, focus change and resize
763 * events still get sent to the event widget because
764 * 1) these events have no meaning for the grabbing widget
765 * and 2) redirecting these events to the grabbing widget
766 * could cause the display to be messed up.
774 gtk_widget_ref (event_widget);
775 if (!gtk_widget_event (event_widget, event) &&
776 !GTK_OBJECT_DESTROYED (event_widget))
777 gtk_widget_destroy (event_widget);
778 gtk_widget_unref (event_widget);
782 gtk_widget_ref (event_widget);
783 gtk_widget_event (event_widget, event);
784 if (!GTK_OBJECT_DESTROYED (event_widget))
785 gtk_widget_destroy (event_widget);
786 gtk_widget_unref (event_widget);
789 case GDK_PROPERTY_NOTIFY:
792 case GDK_FOCUS_CHANGE:
796 case GDK_SELECTION_CLEAR:
797 case GDK_SELECTION_REQUEST:
798 case GDK_SELECTION_NOTIFY:
799 case GDK_CLIENT_EVENT:
801 case GDK_DRAG_REQUEST:
804 case GDK_DROP_DATA_AVAIL:
805 case GDK_VISIBILITY_NOTIFY:
806 gtk_widget_event (event_widget, event);
810 case GDK_KEY_RELEASE:
813 if (gtk_invoke_key_snoopers (grab_widget, event))
816 /* else fall through */
817 case GDK_MOTION_NOTIFY:
818 case GDK_BUTTON_PRESS:
819 case GDK_2BUTTON_PRESS:
820 case GDK_3BUTTON_PRESS:
821 case GDK_BUTTON_RELEASE:
822 case GDK_PROXIMITY_IN:
823 case GDK_PROXIMITY_OUT:
824 case GDK_OTHER_EVENT:
825 gtk_propagate_event (grab_widget, event);
828 case GDK_ENTER_NOTIFY:
829 if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
831 gtk_widget_event (grab_widget, event);
832 if (event_widget == grab_widget)
833 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
837 case GDK_LEAVE_NOTIFY:
838 if (GTK_WIDGET_LEAVE_PENDING (event_widget))
840 GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
841 gtk_widget_event (event_widget, event);
843 else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
844 gtk_widget_event (grab_widget, event);
848 tmp_list = current_events;
849 current_events = g_list_remove_link (current_events, tmp_list);
850 g_list_free_1 (tmp_list);
852 gdk_event_free (event);
856 if (gdk_events_pending() == 0)
862 /* Handle timeout functions that may have expired.
864 gtk_handle_timeouts ();
869 return iteration_done;
885 gtk_grab_add (GtkWidget *widget)
887 g_return_if_fail (widget != NULL);
889 if (!GTK_WIDGET_HAS_GRAB (widget))
891 GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
893 grabs = g_slist_prepend (grabs, widget);
894 gtk_widget_ref (widget);
899 gtk_grab_get_current (void)
902 return GTK_WIDGET (grabs->data);
907 gtk_grab_remove (GtkWidget *widget)
909 g_return_if_fail (widget != NULL);
911 if (GTK_WIDGET_HAS_GRAB (widget))
913 GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
915 grabs = g_slist_remove (grabs, widget);
916 gtk_widget_unref (widget);
921 gtk_init_add (GtkFunction function,
924 GtkInitFunction *init;
926 init = g_new (GtkInitFunction, 1);
927 init->function = function;
930 init_functions = g_list_prepend (init_functions, init);
934 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
937 GtkKeySnooperData *data;
938 static guint snooper_id = 1;
940 g_return_val_if_fail (snooper != NULL, 0);
942 data = g_new (GtkKeySnooperData, 1);
943 data->func = snooper;
944 data->func_data = func_data;
945 data->id = snooper_id++;
946 key_snoopers = g_slist_prepend (key_snoopers, data);
952 gtk_key_snooper_remove (guint snooper_id)
954 GtkKeySnooperData *data = NULL;
957 slist = key_snoopers;
961 if (data->id == snooper_id)
968 key_snoopers = g_slist_remove (key_snoopers, data);
972 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
976 gint return_val = FALSE;
978 slist = key_snoopers;
979 while (slist && !return_val)
981 GtkKeySnooperData *data;
985 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
992 gtk_timeout_add_full (guint32 interval,
993 GtkFunction function,
994 GtkCallbackMarshal marshal,
996 GtkDestroyNotify destroy)
998 static guint timeout_tag = 1;
999 GtkTimeoutFunction *timeoutf;
1001 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1003 /* Create a new timeout function structure.
1004 * The start time is the current time.
1006 if (!timeout_mem_chunk)
1007 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
1008 1024, G_ALLOC_AND_FREE);
1010 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
1012 timeoutf->tag = timeout_tag++;
1013 timeoutf->start = gdk_time_get ();
1014 timeoutf->interval = interval;
1015 timeoutf->originterval = interval;
1016 timeoutf->function = function;
1017 timeoutf->marshal = marshal;
1018 timeoutf->data = data;
1019 timeoutf->destroy = destroy;
1021 gtk_timeout_insert (timeoutf);
1023 return timeoutf->tag;
1027 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
1029 if (timeoutf->destroy)
1030 (timeoutf->destroy) (timeoutf->data);
1031 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
1035 gtk_timeout_add (guint32 interval,
1036 GtkFunction function,
1039 return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
1043 gtk_timeout_add_interp (guint32 interval,
1044 GtkCallbackMarshal function,
1046 GtkDestroyNotify destroy)
1048 return gtk_timeout_add_full (interval, NULL, function, data, destroy);
1051 /* Search for the specified tag in a list of timeouts. If it
1052 * is found, destroy the timeout, and either remove the link
1053 * or set link->data to NULL depending on remove_link
1056 gtk_timeout_remove_from_list (GList **list, guint tag, gint remove_link)
1059 GtkTimeoutFunction *timeoutf;
1064 timeoutf = tmp_list->data;
1066 if (timeoutf->tag == tag)
1070 *list = g_list_remove_link (*list, tmp_list);
1071 g_list_free (tmp_list);
1074 tmp_list->data = NULL;
1076 gtk_timeout_destroy (timeoutf);
1081 tmp_list = tmp_list->next;
1087 gtk_timeout_remove (guint tag)
1090 /* Remove a timeout function.
1091 * (Which, basically, involves searching the
1092 * list for the tag).
1095 if (gtk_timeout_remove_from_list (&timeout_functions, tag, TRUE))
1097 if (gtk_timeout_remove_from_list (¤t_timeouts, tag, TRUE))
1099 if (gtk_timeout_remove_from_list (&running_timeouts, tag, FALSE))
1103 /* We rely on some knowledge of how g_list_insert_sorted works to make
1104 * sure that we insert at the _end_ of the idles of this priority
1107 gtk_idle_compare (gconstpointer a, gconstpointer b)
1109 return (((const GtkIdleFunction *)a)->priority <
1110 ((const GtkIdleFunction *)b)->priority)
1115 gtk_quit_add_full (guint main_level,
1116 GtkFunction function,
1117 GtkCallbackMarshal marshal,
1119 GtkDestroyNotify destroy)
1121 static guint quit_id = 1;
1122 GtkQuitFunction *quitf;
1124 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1126 if (!quit_mem_chunk)
1127 quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1128 512, G_ALLOC_AND_FREE);
1130 quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1132 quitf->id = quit_id++;
1133 quitf->main_level = main_level;
1134 quitf->function = function;
1135 quitf->marshal = marshal;
1137 quitf->destroy = destroy;
1139 quit_functions = g_list_prepend (quit_functions, quitf);
1145 gtk_idle_add_full (gint priority,
1146 GtkFunction function,
1147 GtkCallbackMarshal marshal,
1149 GtkDestroyNotify destroy)
1151 static guint idle_tag = 1;
1152 GtkIdleFunction *idlef;
1154 g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1156 if (!idle_mem_chunk)
1157 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
1158 1024, G_ALLOC_AND_FREE);
1160 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
1162 idlef->tag = idle_tag++;
1163 idlef->priority = priority;
1164 idlef->function = function;
1165 idlef->marshal = marshal;
1167 idlef->destroy = destroy;
1169 /* If we are adding the first idle function, possibly wake up
1170 * the main thread out of its select().
1172 if (!idle_functions)
1173 gdk_threads_wake ();
1175 idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
1182 gtk_idle_add_interp (GtkCallbackMarshal marshal,
1184 GtkDestroyNotify destroy)
1186 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
1190 gtk_idle_destroy (GtkIdleFunction *idlef)
1193 idlef->destroy (idlef->data);
1194 g_mem_chunk_free (idle_mem_chunk, idlef);
1198 gtk_quit_destroy (GtkQuitFunction *quitf)
1201 quitf->destroy (quitf->data);
1202 g_mem_chunk_free (quit_mem_chunk, quitf);
1206 gtk_quit_destructor (GtkObject **object_p)
1209 gtk_object_destroy (*object_p);
1216 gtk_quit_add_destroy (guint main_level,
1219 GtkObject **object_p;
1221 g_return_if_fail (main_level > 0);
1222 g_return_if_fail (object != NULL);
1223 g_return_if_fail (GTK_IS_OBJECT (object));
1225 object_p = g_new (GtkObject*, 1);
1227 gtk_signal_connect (object,
1229 GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1231 gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1235 gtk_quit_add (guint main_level,
1236 GtkFunction function,
1239 return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1243 gtk_idle_add (GtkFunction function,
1246 return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
1250 gtk_idle_add_priority (gint priority,
1251 GtkFunction function,
1254 return gtk_idle_add_full (priority, function, NULL, data, NULL);
1258 gtk_quit_remove (guint id)
1260 GtkQuitFunction *quitf;
1263 tmp_list = quit_functions;
1266 quitf = tmp_list->data;
1268 if (quitf->id == id)
1270 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1271 g_list_free (tmp_list);
1272 gtk_quit_destroy (quitf);
1277 tmp_list = tmp_list->next;
1282 gtk_quit_remove_by_data (gpointer data)
1284 GtkQuitFunction *quitf;
1287 tmp_list = quit_functions;
1290 quitf = tmp_list->data;
1292 if (quitf->data == data)
1294 quit_functions = g_list_remove_link (quit_functions, tmp_list);
1295 g_list_free (tmp_list);
1296 gtk_quit_destroy (quitf);
1301 tmp_list = tmp_list->next;
1305 /* Search for the specified tag in a list of idles. If it
1306 * is found, destroy the idle, and either remove the link
1307 * or set link->data to NULL depending on remove_link
1309 * If tag != 0, match tag against idlef->tag, otherwise, match
1310 * data against idlef->data
1313 gtk_idle_remove_from_list (GList **list,
1318 GtkIdleFunction *idlef;
1324 idlef = tmp_list->data;
1326 if (((tag != 0) && (idlef->tag == tag)) ||
1327 ((tag == 0) && (idlef->data == data)))
1331 *list = g_list_remove_link (*list, tmp_list);
1332 g_list_free (tmp_list);
1335 tmp_list->data = NULL;
1337 gtk_idle_destroy (idlef);
1342 tmp_list = tmp_list->next;
1348 gtk_idle_remove (guint tag)
1350 g_return_if_fail (tag != 0);
1352 if (gtk_idle_remove_from_list (&idle_functions, tag, NULL, TRUE))
1354 if (gtk_idle_remove_from_list (¤t_idles, tag, NULL, TRUE))
1356 if (gtk_idle_remove_from_list (&running_idles, tag, NULL, FALSE))
1361 gtk_idle_remove_by_data (gpointer data)
1363 if (gtk_idle_remove_from_list (&idle_functions, 0, data, TRUE))
1365 if (gtk_idle_remove_from_list (¤t_idles, 0, data, TRUE))
1367 if (gtk_idle_remove_from_list (&running_idles, 0, data, FALSE))
1372 gtk_invoke_input_function (GtkInputFunction *input,
1374 GdkInputCondition condition)
1377 args[0].type = GTK_TYPE_INT;
1378 args[0].name = NULL;
1379 GTK_VALUE_INT(args[0]) = source;
1380 args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1381 args[1].name = NULL;
1382 GTK_VALUE_FLAGS(args[1]) = condition;
1383 args[2].type = GTK_TYPE_NONE;
1384 args[2].name = NULL;
1386 input->callback (NULL, input->data, 2, args);
1390 gtk_destroy_input_function (GtkInputFunction *input)
1393 (input->destroy) (input->data);
1398 gtk_input_add_full (gint source,
1399 GdkInputCondition condition,
1400 GdkInputFunction function,
1401 GtkCallbackMarshal marshal,
1403 GtkDestroyNotify destroy)
1407 GtkInputFunction *input;
1409 input = g_new (GtkInputFunction, 1);
1410 input->callback = marshal;
1412 input->destroy = destroy;
1414 return gdk_input_add_full (source,
1416 (GdkInputFunction) gtk_invoke_input_function,
1418 (GdkDestroyNotify) gtk_destroy_input_function);
1421 return gdk_input_add_full (source, condition, function, data, destroy);
1425 gtk_input_remove (guint tag)
1427 gdk_input_remove (tag);
1431 gtk_get_current_event (void)
1434 return gdk_event_copy ((GdkEvent *) current_events->data);
1440 gtk_get_event_widget (GdkEvent *event)
1445 if (event && event->any.window)
1446 gdk_window_get_user_data (event->any.window, (void**) &widget);
1452 gtk_exit_func (void)
1454 if (gtk_initialized)
1456 gtk_initialized = FALSE;
1457 gtk_preview_uninit ();
1462 /* We rely on some knowledge of how g_list_insert_sorted works to make
1463 * sure that we insert after timeouts of equal interval
1466 gtk_timeout_compare (gconstpointer a, gconstpointer b)
1468 return (((const GtkTimeoutFunction *)a)->interval <
1469 ((const GtkTimeoutFunction *)b)->interval)
1474 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1476 g_assert (timeoutf != NULL);
1478 /* Insert the timeout function appropriately.
1479 * Appropriately meaning sort it into the list
1480 * of timeout functions.
1482 timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1483 gtk_timeout_compare);
1487 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1489 if (!timeoutf->marshal)
1490 return timeoutf->function (timeoutf->data);
1494 gint ret_val = FALSE;
1495 args[0].name = NULL;
1496 args[0].type = GTK_TYPE_BOOL;
1497 args[0].d.pointer_data = &ret_val;
1498 timeoutf->marshal (NULL, timeoutf->data, 0, args);
1504 gtk_handle_current_timeouts (guint32 the_time)
1508 GtkTimeoutFunction *timeoutf;
1510 while (current_timeouts)
1512 tmp_list = current_timeouts;
1513 timeoutf = tmp_list->data;
1515 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1516 if (running_timeouts)
1518 running_timeouts->prev = tmp_list;
1519 tmp_list->next = running_timeouts;
1521 running_timeouts = tmp_list;
1523 result = gtk_invoke_timeout_function (timeoutf);
1525 running_timeouts = g_list_remove_link (running_timeouts, tmp_list);
1526 timeoutf = tmp_list->data;
1528 g_list_free_1 (tmp_list);
1534 gtk_timeout_destroy (timeoutf);
1538 timeoutf->interval = timeoutf->originterval;
1539 timeoutf->start = the_time;
1540 gtk_timeout_insert (timeoutf);
1547 gtk_handle_timeouts (void)
1552 GtkTimeoutFunction *timeoutf;
1554 /* Caller must already have called gtk_handle_current_timeouts if
1556 g_assert (current_timeouts == NULL);
1558 if (timeout_functions)
1560 the_time = gdk_time_get ();
1562 tmp_list = timeout_functions;
1565 timeoutf = tmp_list->data;
1567 if (timeoutf->interval <= (the_time - timeoutf->start))
1569 tmp_list2 = tmp_list;
1570 tmp_list = tmp_list->next;
1572 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1573 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1577 timeoutf->interval -= (the_time - timeoutf->start);
1578 timeoutf->start = the_time;
1579 tmp_list = tmp_list->next;
1583 if (current_timeouts)
1584 gtk_handle_current_timeouts (the_time);
1589 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1591 if (!quitf->marshal)
1592 return quitf->function (quitf->data);
1596 gint ret_val = FALSE;
1598 args[0].name = NULL;
1599 args[0].type = GTK_TYPE_BOOL;
1600 args[0].d.pointer_data = &ret_val;
1601 ((GtkCallbackMarshal) quitf->marshal) (NULL,
1609 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1611 if (!idlef->marshal)
1612 return idlef->function (idlef->data);
1616 gint ret_val = FALSE;
1618 args[0].name = NULL;
1619 args[0].type = GTK_TYPE_BOOL;
1620 args[0].d.pointer_data = &ret_val;
1621 ((GtkCallbackMarshal) idlef->marshal) (NULL,
1629 gtk_handle_current_idles (void)
1633 GtkIdleFunction *idlef;
1636 while (current_idles)
1638 tmp_list = current_idles;
1639 idlef = tmp_list->data;
1641 current_idles = g_list_remove_link (current_idles, tmp_list);
1644 running_idles->prev = tmp_list;
1645 tmp_list->next = running_idles;
1647 running_idles = tmp_list;
1649 result = gtk_idle_invoke_function (idlef);
1651 running_idles = g_list_remove_link (running_idles, tmp_list);
1652 idlef = tmp_list->data;
1654 if (!idlef || !result)
1656 g_list_free (tmp_list);
1658 gtk_idle_destroy (idlef);
1662 /* Insert the idle function back into the list of idle
1663 * functions at the end of the idles of this priority
1665 tmp_list2 = idle_functions;
1667 (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1668 tmp_list2 = tmp_list2->next;
1671 idle_functions = g_list_concat (idle_functions, tmp_list);
1672 else if (tmp_list2 == idle_functions)
1674 tmp_list->next = idle_functions;
1676 idle_functions->prev = tmp_list;
1677 idle_functions = tmp_list;
1681 tmp_list->prev = tmp_list2->prev;
1682 tmp_list->next = tmp_list2;
1683 tmp_list2->prev->next = tmp_list;
1684 tmp_list2->prev = tmp_list;
1691 gtk_handle_idle (void)
1693 /* Caller must already have called gtk_handle_current_idles if
1696 g_assert (current_idles == NULL);
1698 /* Handle only the idle functions that have the highest priority */
1704 tmp_list = idle_functions;
1705 top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1708 (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1709 tmp_list = tmp_list->next;
1711 current_idles = idle_functions;
1712 idle_functions = tmp_list;
1716 tmp_list->prev->next = NULL;
1717 tmp_list->prev = NULL;
1720 gtk_handle_current_idles();
1725 gtk_handle_timer (void)
1727 GtkTimeoutFunction *timeoutf;
1732 gdk_timer_enable ();
1734 else if (timeout_functions)
1736 timeoutf = timeout_functions->data;
1737 gdk_timer_set (timeoutf->interval);
1738 gdk_timer_enable ();
1743 gdk_timer_disable ();
1748 gtk_propagate_event (GtkWidget *widget,
1753 g_return_if_fail (widget != NULL);
1754 g_return_if_fail (event != NULL);
1756 handled_event = FALSE;
1758 if ((event->type == GDK_KEY_PRESS) ||
1759 (event->type == GDK_KEY_RELEASE))
1761 /* Only send key events within Window widgets to the Window
1762 * The Window widget will in turn pass the
1763 * key event on to the currently focused widget
1768 window = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1771 if (GTK_WIDGET_IS_SENSITIVE (window))
1772 gtk_widget_event (window, event);
1774 handled_event = TRUE; /* don't send to widget */
1778 /* Other events get propagated up the widget tree
1779 * so that parents can see the button and motion
1780 * events of the children.
1782 while (!handled_event && widget)
1786 gtk_widget_ref (widget);
1787 handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1788 tmp = widget->parent;
1789 gtk_widget_unref (widget);
1797 gtk_error (gchar *str)
1803 gtk_warning (gchar *str)
1809 gtk_message (gchar *str)
1815 gtk_print (gchar *str)
1817 static GtkWidget *window = NULL;
1818 static GtkWidget *text;
1819 static int level = 0;
1823 GtkWidget *hscrollbar;
1824 GtkWidget *vscrollbar;
1825 GtkWidget *separator;
1830 fputs (str, stdout);
1837 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1839 gtk_signal_connect (GTK_OBJECT (window), "destroy",
1840 (GtkSignalFunc) gtk_widget_destroyed,
1843 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1845 box1 = gtk_vbox_new (FALSE, 0);
1846 gtk_container_add (GTK_CONTAINER (window), box1);
1847 gtk_widget_show (box1);
1850 box2 = gtk_vbox_new (FALSE, 10);
1851 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1852 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1853 gtk_widget_show (box2);
1856 table = gtk_table_new (2, 2, FALSE);
1857 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1858 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1859 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1860 gtk_widget_show (table);
1862 text = gtk_text_new (NULL, NULL);
1863 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1864 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1865 gtk_widget_show (text);
1866 gtk_widget_realize (text);
1868 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1869 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1870 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1871 gtk_widget_show (hscrollbar);
1873 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1874 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1875 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1876 gtk_widget_show (vscrollbar);
1878 separator = gtk_hseparator_new ();
1879 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1880 gtk_widget_show (separator);
1883 box2 = gtk_vbox_new (FALSE, 10);
1884 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1885 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1886 gtk_widget_show (box2);
1889 button = gtk_button_new_with_label ("close");
1890 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1891 (GtkSignalFunc) gtk_widget_hide,
1892 GTK_OBJECT (window));
1893 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1894 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1895 gtk_widget_grab_default (button);
1896 gtk_widget_show (button);
1900 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1903 if (!GTK_WIDGET_VISIBLE (window))
1904 gtk_widget_show (window);