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"
36 /* Private type definitions
38 typedef struct _GtkInitFunction GtkInitFunction;
39 typedef struct _GtkTimeoutFunction GtkTimeoutFunction;
40 typedef struct _GtkIdleFunction GtkIdleFunction;
42 struct _GtkInitFunction
48 struct _GtkTimeoutFunction
57 GtkDestroyNotify destroy;
60 struct _GtkIdleFunction
66 GtkDestroyNotify destroy;
70 static void gtk_exit_func (void);
71 static void gtk_timeout_insert (GtkTimeoutFunction *timeoutf);
72 static void gtk_handle_current_timeouts (guint32 the_time);
73 static void gtk_handle_current_idles (void);
74 static void gtk_handle_timeouts (void);
75 static void gtk_handle_idle (void);
76 static void gtk_handle_timer (void);
77 static void gtk_propagate_event (GtkWidget *widget,
79 static void gtk_error (gchar *str);
80 static void gtk_warning (gchar *str);
81 static void gtk_message (gchar *str);
82 static void gtk_print (gchar *str);
86 static guint main_level = 0;
87 static gint initialized = FALSE;
88 static GdkEvent next_event;
89 static GdkEvent current_event;
90 static gint have_event = FALSE;
91 static gint have_next_event = FALSE;
93 static GList *grabs = NULL; /* A list of grabs. The grabbing widget
94 * is the first one on the list.
96 static GList *init_functions = NULL; /* A list of init functions.
98 static GList *timeout_functions = NULL; /* A list of timeout functions sorted by
99 * when the length of the time interval
100 * remaining. Therefore, the first timeout
101 * function to expire is at the head of
102 * the list and the last to expire is at
103 * the tail of the list.
105 static GList *idle_functions = NULL; /* A list of idle functions.
108 static GList *current_idles = NULL;
109 static GList *current_timeouts = NULL;
110 static GMemChunk *timeout_mem_chunk = NULL;
111 static GMemChunk *idle_mem_chunk = NULL;
113 static GdkVisual *gtk_visual; /* The visual to be used in creating new
116 static GdkColormap *gtk_colormap; /* The colormap to be used in creating new
127 g_set_error_handler (gtk_error);
128 g_set_warning_handler (gtk_warning);
129 g_set_message_handler (gtk_message);
130 g_set_print_handler (gtk_print);
133 /* Initialize "gdk". We simply pass along the 'argc' and 'argv'
134 * parameters as they contain information that
136 gdk_init (argc, argv);
138 /* Initialize the default visual and colormap to be
139 * used in creating widgets. (We want to use the system
140 * defaults so as to be nice to the colormap).
142 gtk_visual = gdk_visual_get_system ();
143 gtk_colormap = gdk_colormap_get_system ();
148 /* Register an exit function to make sure we are able to cleanup.
150 if (ATEXIT (gtk_exit_func))
151 g_warning ("unable to register exit function");
153 /* Set the 'initialized' flag.
159 gtk_exit (int errorcode)
161 /* Only if "gtk" has been initialized should we de-initialize.
163 /* de-initialisation is done by the gtk_exit_funct(),
164 * no need to do this here (Alex J.)
172 return gdk_set_locale ();
180 GtkInitFunction *init;
185 tmp_list = functions = init_functions;
186 init_functions = NULL;
190 init = tmp_list->data;
191 tmp_list = tmp_list->next;
193 (* init->function) (init->data);
197 g_list_free (functions);
200 while (!gtk_main_iteration ())
208 gtk_main_level (void)
220 gtk_main_iteration ()
223 GtkWidget *event_widget;
224 GtkWidget *grab_widget;
228 /* If this is a recursive call, and there are pending timeouts or
229 * idles, finish them, then return immediately to avoid blocking
232 if (current_timeouts)
234 gtk_handle_current_timeouts( gdk_time_get());
239 gtk_handle_current_idles();
243 /* If there is a valid event in 'next_event' then copy
244 * it to 'event' and unset the flag.
248 have_next_event = FALSE;
250 current_event = next_event;
253 /* If we don't have an event then get one.
257 /* Handle setting of the "gdk" timer. If there are no
258 * timeout functions, then the timer is turned off.
259 * If there are timeout functions, then the timer is
260 * set to the shortest timeout interval (which is
261 * the first timeout function).
265 have_event = gdk_event_get (¤t_event, NULL, NULL);
268 /* "gdk_event_get" can return FALSE if the timer goes off
269 * and no events are pending. Therefore, we should make
270 * sure that we got an event before continuing.
276 /* If there are any events pending then get the next one.
278 if (gdk_events_pending () > 0)
279 have_next_event = gdk_event_get (&next_event, NULL, NULL);
281 /* Try to compress enter/leave notify events. These event
282 * pairs occur when the mouse is dragged quickly across
283 * a window with many buttons (or through a menu). Instead
284 * of highlighting and de-highlighting each widget that
285 * is crossed it is better to simply de-highlight the widget
286 * which contained the mouse initially and highlight the
287 * widget which ends up containing the mouse.
290 if (((current_event.type == GDK_ENTER_NOTIFY) ||
291 (current_event.type == GDK_LEAVE_NOTIFY)) &&
292 ((next_event.type == GDK_ENTER_NOTIFY) ||
293 (next_event.type == GDK_LEAVE_NOTIFY)) &&
294 (next_event.type != current_event.type) &&
295 (next_event.any.window == current_event.any.window))
298 /* Find the widget which got the event. We store the widget
299 * in the user_data field of GdkWindow's.
301 event_widget = gtk_get_event_widget (¤t_event);
303 /* If there is a grab in effect...
307 grab_widget = grabs->data;
309 /* If the grab widget is an ancestor of the event widget
310 * then we send the event to the original event widget.
311 * This is the key to implementing modality.
313 if (gtk_widget_is_ancestor (event_widget, grab_widget))
314 grab_widget = event_widget;
318 grab_widget = event_widget;
321 /* Not all events get sent to the grabbing widget.
322 * The delete, destroy, expose, focus change and resize
323 * events still get sent to the event widget because
324 * 1) these events have no meaning for the grabbing widget
325 * and 2) redirecting these events to the grabbing widget
326 * could cause the display to be messed up.
328 event_copy = current_event;
329 switch (event_copy.type)
335 if (gtk_widget_event (event_widget, &event_copy))
336 gtk_widget_destroy (event_widget);
340 gtk_widget_event (event_widget, &event_copy);
341 gtk_widget_destroy (event_widget);
344 case GDK_PROPERTY_NOTIFY:
345 /* To handle selection INCR transactions, we select
346 * PropertyNotify events on the requestor window and create
347 * a corresponding (fake) GdkWindow so that events get
348 * here. There won't be a widget though, so we have to handle
352 if (event_widget == NULL)
354 gtk_selection_incr_event (event_copy.any.window,
355 &event_copy.property);
358 /* otherwise fall through */
361 case GDK_FOCUS_CHANGE:
365 case GDK_SELECTION_CLEAR:
366 case GDK_SELECTION_REQUEST:
367 case GDK_SELECTION_NOTIFY:
368 case GDK_CLIENT_EVENT:
369 gtk_widget_event (event_widget, &event_copy);
372 case GDK_MOTION_NOTIFY:
373 case GDK_BUTTON_PRESS:
374 case GDK_2BUTTON_PRESS:
375 case GDK_3BUTTON_PRESS:
376 case GDK_BUTTON_RELEASE:
378 case GDK_KEY_RELEASE:
379 case GDK_PROXIMITY_IN:
380 case GDK_PROXIMITY_OUT:
381 case GDK_OTHER_EVENT:
383 case GDK_DRAG_REQUEST:
386 case GDK_DROP_DATA_AVAIL:
387 gtk_propagate_event (grab_widget, &event_copy);
390 case GDK_ENTER_NOTIFY:
391 case GDK_LEAVE_NOTIFY:
392 if (grab_widget && GTK_WIDGET_IS_SENSITIVE (grab_widget))
393 gtk_widget_event (grab_widget, &event_copy);
399 if (gdk_events_pending() == 0)
403 /* Handle a timeout functions that may have expired.
405 gtk_handle_timeouts ();
423 gtk_grab_add (GtkWidget *widget)
425 /* Place the grab on the front of the list of grabs.
427 grabs = g_list_prepend (grabs, widget);
431 gtk_grab_remove (GtkWidget *widget)
433 /* Remove the grab from the list of grabs.
434 * Note: the grab being removed may be in
435 * the middle of the list.
437 grabs = g_list_remove (grabs, widget);
441 gtk_init_add (GtkFunction function,
444 GtkInitFunction *init;
446 init = g_new (GtkInitFunction, 1);
447 init->function = function;
450 init_functions = g_list_prepend (init_functions, init);
454 gtk_timeout_add_internal (guint32 interval,
456 GtkFunction function,
458 GtkDestroyNotify destroy)
460 static gint timeout_tag = 1;
461 GtkTimeoutFunction *timeoutf;
463 /* Create a new timeout function structure.
464 * The start time is the current time.
466 if (!timeout_mem_chunk)
467 timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
468 1024, G_ALLOC_AND_FREE);
470 timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
472 timeoutf->tag = timeout_tag++;
473 timeoutf->start = gdk_time_get ();
474 timeoutf->interval = interval;
475 timeoutf->originterval = interval;
476 timeoutf->interp = interp;
477 timeoutf->function = function;
478 timeoutf->data = data;
479 timeoutf->destroy = destroy;
481 gtk_timeout_insert (timeoutf);
483 return timeoutf->tag;
487 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
489 if (timeoutf->destroy)
490 (timeoutf->destroy) (timeoutf->data);
491 g_mem_chunk_free (timeout_mem_chunk, timeoutf);
495 gtk_timeout_add (guint32 interval,
496 GtkFunction function,
499 return gtk_timeout_add_internal (interval, FALSE, function, data, NULL);
503 gtk_timeout_add_interp (guint32 interval,
504 GtkCallbackMarshal function,
506 GtkDestroyNotify destroy)
508 return gtk_timeout_add_internal (interval, TRUE,
509 (GtkFunction) function,
514 gtk_timeout_remove (gint tag)
516 GtkTimeoutFunction *timeoutf;
519 /* Remove a timeout function.
520 * (Which, basically, involves searching the
523 tmp_list = timeout_functions;
526 timeoutf = tmp_list->data;
528 if (timeoutf->tag == tag)
530 timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
531 g_list_free (tmp_list);
532 gtk_timeout_destroy (timeoutf);
537 tmp_list = tmp_list->next;
540 tmp_list = current_timeouts;
543 timeoutf = tmp_list->data;
545 if (timeoutf->tag == tag)
547 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
548 g_list_free (tmp_list);
549 gtk_timeout_destroy (timeoutf);
554 tmp_list = tmp_list->next;
559 gtk_idle_add_internal (gint interp,
560 GtkFunction function,
562 GtkDestroyNotify destroy)
564 static gint idle_tag = 1;
565 GtkIdleFunction *idlef;
568 idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
569 1024, G_ALLOC_AND_FREE);
571 idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
573 idlef->tag = idle_tag++;
574 idlef->interp = interp;
575 idlef->function = function;
577 idlef->destroy = destroy;
579 idle_functions = g_list_append (idle_functions, idlef);
585 gtk_idle_destroy (GtkIdleFunction *idlef)
588 idlef->destroy (idlef->data);
589 g_mem_chunk_free (idle_mem_chunk, idlef);
593 gtk_idle_add (GtkFunction function,
596 return gtk_idle_add_internal (FALSE, function, data, NULL);
600 gtk_idle_add_interp (GtkCallbackMarshal function,
602 GtkDestroyNotify destroy)
604 return gtk_idle_add_internal (TRUE, (GtkFunction)function, data, destroy);
608 gtk_idle_remove (gint tag)
610 GtkIdleFunction *idlef;
613 tmp_list = idle_functions;
616 idlef = tmp_list->data;
618 if (idlef->tag == tag)
620 idle_functions = g_list_remove_link (idle_functions, tmp_list);
621 g_list_free (tmp_list);
622 gtk_idle_destroy (idlef);
627 tmp_list = tmp_list->next;
630 tmp_list = current_idles;
633 idlef = tmp_list->data;
635 if (idlef->tag == tag)
637 current_idles = g_list_remove_link (current_idles, tmp_list);
638 g_list_free (tmp_list);
639 gtk_idle_destroy (idlef);
644 tmp_list = tmp_list->next;
649 gtk_idle_remove_by_data (gpointer data)
651 GtkIdleFunction *idlef;
654 tmp_list = idle_functions;
657 idlef = tmp_list->data;
659 if (idlef->data == data)
661 idle_functions = g_list_remove_link (idle_functions, tmp_list);
662 g_list_free (tmp_list);
663 g_mem_chunk_free (idle_mem_chunk, idlef);
668 tmp_list = tmp_list->next;
671 tmp_list = current_idles;
674 idlef = tmp_list->data;
676 if (idlef->data == data)
678 current_idles = g_list_remove_link (current_idles, tmp_list);
679 g_list_free (tmp_list);
680 g_mem_chunk_free (idle_mem_chunk, idlef);
685 tmp_list = tmp_list->next;
690 gtk_get_current_event (GdkEvent *event)
692 g_assert (event != NULL);
694 *event = current_event;
698 gtk_get_event_widget (GdkEvent *event)
701 gdk_window_get_user_data (event->any.window, (void**) &widget);
712 gtk_preview_uninit ();
717 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
719 GtkTimeoutFunction *temp;
723 g_assert (timeoutf != NULL);
725 /* Insert the timeout function appropriately.
726 * Appropriately meaning sort it into the list
727 * of timeout functions.
729 temp_list = timeout_functions;
732 temp = temp_list->data;
733 if (timeoutf->interval < temp->interval)
735 new_list = g_list_alloc ();
736 new_list->data = timeoutf;
737 new_list->next = temp_list;
738 new_list->prev = temp_list->prev;
740 temp_list->prev->next = new_list;
741 temp_list->prev = new_list;
743 if (temp_list == timeout_functions)
744 timeout_functions = new_list;
749 temp_list = temp_list->next;
752 timeout_functions = g_list_append (timeout_functions, timeoutf);
756 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
758 if (!timeoutf->interp)
759 return timeoutf->function (timeoutf->data);
763 gint ret_val = FALSE;
765 args[0].type = GTK_TYPE_BOOL;
766 args[0].d.pointer_data = &ret_val;
767 ((GtkCallbackMarshal)timeoutf->function) (NULL,
775 gtk_handle_current_timeouts (guint32 the_time)
778 GtkTimeoutFunction *timeoutf;
780 while (current_timeouts)
782 tmp_list = current_timeouts;
783 timeoutf = tmp_list->data;
785 current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
786 g_list_free (tmp_list);
788 if (gtk_invoke_timeout_function (timeoutf) == FALSE)
790 gtk_timeout_destroy (timeoutf);
794 timeoutf->interval = timeoutf->originterval;
795 timeoutf->start = the_time;
796 gtk_timeout_insert (timeoutf);
802 gtk_handle_timeouts ()
808 GtkTimeoutFunction *timeoutf;
810 /* Caller must already have called gtk_handle_current_timeouts if
812 g_assert (current_timeouts == NULL);
814 if (timeout_functions)
816 the_time = gdk_time_get ();
818 tmp_list = timeout_functions;
821 timeoutf = tmp_list->data;
823 if (timeoutf->interval <= (the_time - timeoutf->start))
825 tmp_list2 = tmp_list;
826 tmp_list = tmp_list->next;
828 timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
829 current_timeouts = g_list_concat (current_timeouts, tmp_list2);
833 timeoutf->interval -= (the_time - timeoutf->start);
834 timeoutf->start = the_time;
835 tmp_list = tmp_list->next;
839 if (current_timeouts)
840 gtk_handle_current_timeouts(the_time);
845 gtk_idle_invoke_function (GtkIdleFunction *idlef)
848 return idlef->function (idlef->data);
852 gint ret_val = FALSE;
854 args[0].type = GTK_TYPE_BOOL;
855 args[0].d.pointer_data = &ret_val;
856 ((GtkCallbackMarshal)idlef->function) (NULL,
864 gtk_handle_current_idles ()
867 GtkIdleFunction *idlef;
869 while (current_idles)
871 tmp_list = current_idles;
872 idlef = tmp_list->data;
874 current_idles = g_list_remove_link (current_idles, tmp_list);
876 if (gtk_idle_invoke_function (idlef) == FALSE)
878 g_list_free (tmp_list);
879 gtk_idle_destroy (idlef);
883 idle_functions = g_list_concat (idle_functions, tmp_list);
891 GtkIdleFunction *idlef;
895 /* Caller must already have called gtk_handle_current_idles if
897 g_assert (current_idles == NULL);
901 current_idles = idle_functions;
902 idle_functions = NULL;
904 gtk_handle_current_idles();
911 GtkTimeoutFunction *timeoutf;
918 else if (timeout_functions)
920 timeoutf = timeout_functions->data;
921 gdk_timer_set (timeoutf->interval);
927 gdk_timer_disable ();
932 gtk_propagate_event (GtkWidget *widget,
937 gint parent_old_value;
940 g_return_if_fail (widget != NULL);
941 g_return_if_fail (event != NULL);
943 handled_event = FALSE;
945 if ((event->type == GDK_KEY_PRESS) ||
946 (event->type == GDK_KEY_RELEASE))
948 /* Only send key events to window widgets.
949 * The window widget will in turn pass the
950 * key event on to the currently focused widget
953 parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
954 if (parent && GTK_WIDGET_IS_SENSITIVE (parent))
956 parent_old_value = GTK_OBJECT_IN_CALL (parent);
957 GTK_OBJECT_SET_FLAGS (parent, GTK_IN_CALL);
959 handled_event = gtk_widget_event (parent, event);
961 if (!parent_old_value)
962 GTK_OBJECT_UNSET_FLAGS (parent, GTK_IN_CALL);
964 if (GTK_OBJECT_NEED_DESTROY (parent) && !GTK_OBJECT_IN_CALL (parent))
966 gtk_object_destroy (GTK_OBJECT (parent));
974 old_value = GTK_OBJECT_IN_CALL (widget);
975 GTK_OBJECT_SET_FLAGS (widget, GTK_IN_CALL);
977 /* Other events get propagated up the widget tree
978 * so that parents can see the button and motion
979 * events of the children.
984 parent_old_value = GTK_OBJECT_IN_CALL (parent);
985 GTK_OBJECT_SET_FLAGS (parent, GTK_IN_CALL);
987 handled_event = (!GTK_WIDGET_IS_SENSITIVE (parent) ||
988 gtk_widget_event (parent, event));
990 if (!parent_old_value)
991 GTK_OBJECT_UNSET_FLAGS (parent, GTK_IN_CALL);
996 if (GTK_OBJECT_NEED_DESTROY (parent) && !GTK_OBJECT_IN_CALL (parent))
998 gtk_object_destroy (GTK_OBJECT (parent));
1002 parent = parent->parent;
1006 GTK_OBJECT_UNSET_FLAGS (widget, GTK_IN_CALL);
1008 if (GTK_OBJECT_NEED_DESTROY (widget) && !GTK_OBJECT_IN_CALL (widget))
1009 gtk_object_destroy (GTK_OBJECT (widget));
1015 gtk_error (gchar *str)
1021 gtk_warning (gchar *str)
1027 gtk_message (gchar *str)
1033 gtk_print (gchar *str)
1035 static GtkWidget *window = NULL;
1036 static GtkWidget *text;
1037 static int level = 0;
1041 GtkWidget *hscrollbar;
1042 GtkWidget *vscrollbar;
1043 GtkWidget *separator;
1048 fputs (str, stdout);
1055 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1057 * gtk_signal_connect (GTK_OBJECT (window), "destroy",
1058 * (GtkSignalFunc) gtk_widget_destroyed,
1061 gtk_window_set_title (GTK_WINDOW (window), "Messages");
1063 box1 = gtk_vbox_new (FALSE, 0);
1064 gtk_container_add (GTK_CONTAINER (window), box1);
1065 gtk_widget_show (box1);
1068 box2 = gtk_vbox_new (FALSE, 10);
1069 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1070 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1071 gtk_widget_show (box2);
1074 table = gtk_table_new (2, 2, FALSE);
1075 gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1076 gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1077 gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1078 gtk_widget_show (table);
1080 text = gtk_text_new (NULL, NULL);
1081 gtk_text_set_editable (GTK_TEXT (text), FALSE);
1082 gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1083 gtk_widget_show (text);
1084 gtk_widget_realize (text);
1086 hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1087 gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1088 GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1089 gtk_widget_show (hscrollbar);
1091 vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1092 gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1093 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1094 gtk_widget_show (vscrollbar);
1096 separator = gtk_hseparator_new ();
1097 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1098 gtk_widget_show (separator);
1101 box2 = gtk_vbox_new (FALSE, 10);
1102 gtk_container_border_width (GTK_CONTAINER (box2), 10);
1103 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1104 gtk_widget_show (box2);
1107 button = gtk_button_new_with_label ("close");
1108 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1109 (GtkSignalFunc) gtk_widget_hide,
1110 GTK_OBJECT (window));
1111 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1112 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1113 gtk_widget_grab_default (button);
1114 gtk_widget_show (button);
1118 gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1121 if (!GTK_WIDGET_VISIBLE (window))
1122 gtk_widget_show (window);