]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
e77b1a26c31c48f75cbd957e3a4a57744d812621
[~andy/gtk] / gtk / gtkmain.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
17  */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "gtkbutton.h"
21 #include "gtkhscrollbar.h"
22 #include "gtkhseparator.h"
23 #include "gtkmain.h"
24 #include "gtkpreview.h"
25 #include "gtkrc.h"
26 #include "gtkselection.h"
27 #include "gtksignal.h"
28 #include "gtktable.h"
29 #include "gtktext.h"
30 #include "gtkvbox.h"
31 #include "gtkvscrollbar.h"
32 #include "gtkwidget.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
35
36
37 /* Private type definitions
38  */
39 typedef struct _GtkInitFunction          GtkInitFunction;
40 typedef struct _GtkTimeoutFunction       GtkTimeoutFunction;
41 typedef struct _GtkIdleFunction          GtkIdleFunction;
42 typedef struct _GtkInputFunction         GtkInputFunction;
43 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
44
45 struct _GtkInitFunction
46 {
47   GtkFunction function;
48   gpointer data;
49 };
50
51 struct _GtkTimeoutFunction
52 {
53   gint tag;
54   guint32 start;
55   guint32 interval;
56   guint32 originterval;
57   gint interp;
58   GtkFunction function;
59   gpointer data;
60   GtkDestroyNotify destroy;
61 };
62
63 struct _GtkIdleFunction
64 {
65   gint tag;
66   gint interp;
67   GtkFunction function;
68   gpointer data;
69   GtkDestroyNotify destroy;
70 };
71
72 struct _GtkInputFunction
73 {
74   GtkCallbackMarshal callback;
75   gpointer data;
76   GtkDestroyNotify destroy;
77 };
78
79 struct _GtkKeySnooperData
80 {
81   GtkKeySnoopFunc func;
82   gpointer func_data;
83   gint id;
84 };
85
86 static void  gtk_exit_func               (void);
87 static void  gtk_timeout_insert          (GtkTimeoutFunction *timeoutf);
88 static void  gtk_handle_current_timeouts (guint32             the_time);
89 static void  gtk_handle_current_idles    (void);
90 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
91                                           GdkEvent           *event);
92 static void  gtk_handle_timeouts         (void);
93 static void  gtk_handle_idle             (void);
94 static void  gtk_handle_timer            (void);
95 static void  gtk_propagate_event         (GtkWidget          *widget,
96                                           GdkEvent           *event);
97 static void  gtk_error                   (gchar              *str);
98 static void  gtk_warning                 (gchar              *str);
99 static void  gtk_message                 (gchar              *str);
100 static void  gtk_print                   (gchar              *str);
101
102
103 static gint done;
104 static guint main_level = 0;
105 static gint initialized = FALSE;
106 static GdkEvent *next_event = NULL;
107 static GList *current_events = NULL;
108
109 static GSList *grabs = NULL;               /* A stack of unique grabs. The grabbing
110                                             *  widget is the first one on the list.
111                                             */
112 static GList *init_functions = NULL;       /* A list of init functions.
113                                             */
114 static GList *timeout_functions = NULL;    /* A list of timeout functions sorted by
115                                             *  when the length of the time interval
116                                             *  remaining. Therefore, the first timeout
117                                             *  function to expire is at the head of
118                                             *  the list and the last to expire is at
119                                             *  the tail of the list.
120                                             */
121 static GList *idle_functions = NULL;       /* A list of idle functions.
122                                             */
123
124 static GList *current_idles = NULL;
125 static GList *current_timeouts = NULL;
126 static GMemChunk *timeout_mem_chunk = NULL;
127 static GMemChunk *idle_mem_chunk = NULL;
128
129 static GSList *key_snoopers = NULL;
130
131 static GdkVisual *gtk_visual;              /* The visual to be used in creating new
132                                             *  widgets.
133                                             */
134 static GdkColormap *gtk_colormap;          /* The colormap to be used in creating new
135                                             *  widgets.
136                                             */
137
138
139 void
140 gtk_init (int    *argc,
141           char ***argv)
142 {
143   if (0)
144     {
145       g_set_error_handler (gtk_error);
146       g_set_warning_handler (gtk_warning);
147       g_set_message_handler (gtk_message);
148       g_set_print_handler (gtk_print);
149     }
150   
151   /* Initialize "gdk". We simply pass along the 'argc' and 'argv'
152    *  parameters as they contain information that
153    */
154   gdk_init (argc, argv);
155   
156   /* Initialize the default visual and colormap to be
157    *  used in creating widgets. (We want to use the system
158    *  defaults so as to be nice to the colormap).
159    */
160   gtk_visual = gdk_visual_get_system ();
161   gtk_colormap = gdk_colormap_get_system ();
162   gtk_rc_init ();
163   
164   gtk_type_init ();
165   
166   /* Register an exit function to make sure we are able to cleanup.
167    */
168   if (ATEXIT (gtk_exit_func))
169     g_warning ("unable to register exit function");
170   
171   /* Set the 'initialized' flag.
172    */
173   initialized = TRUE;
174 }
175
176 void
177 gtk_exit (int errorcode)
178 {
179   /* Only if "gtk" has been initialized should we de-initialize.
180    */
181   /* de-initialisation is done by the gtk_exit_funct(),
182    * no need to do this here (Alex J.)
183    */
184   gdk_exit(errorcode);
185 }
186
187 gchar*
188 gtk_set_locale ()
189 {
190   return gdk_set_locale ();
191 }
192
193 void
194 gtk_main ()
195 {
196   GList *tmp_list;
197   GList *functions;
198   GtkInitFunction *init;
199   int old_done;
200   
201   main_level++;
202   
203   tmp_list = functions = init_functions;
204   init_functions = NULL;
205   
206   while (tmp_list)
207     {
208       init = tmp_list->data;
209       tmp_list = tmp_list->next;
210       
211       (* init->function) (init->data);
212       g_free (init);
213     }
214   
215   g_list_free (functions);
216   
217   old_done = done;
218   while (!gtk_main_iteration ())
219     ;
220   done = old_done;
221   
222   main_level--;
223 }
224
225 guint
226 gtk_main_level (void)
227 {
228   return main_level;
229 }
230
231 void
232 gtk_main_quit ()
233 {
234   done = TRUE;
235 }
236
237 gint
238 gtk_events_pending (void)
239 {
240   return gdk_events_pending() + (next_event != NULL) ? 1 : 0;
241 }
242
243 gint 
244 gtk_main_iteration ()
245 {
246   return gtk_main_iteration_do (TRUE);
247 }
248
249 gint
250 gtk_main_iteration_do (gboolean blocking)
251 {
252   GtkWidget *event_widget;
253   GtkWidget *grab_widget;
254   GdkEvent *event = NULL;
255   GList *tmp_list;
256   
257   done = FALSE;
258   
259   /* If this is a recursive call, and there are pending timeouts or
260    * idles, finish them, then return immediately to avoid blocking
261    * in gdk_event_get()
262    */
263   if (current_timeouts)
264     {
265       gtk_handle_current_timeouts( gdk_time_get());
266       return done;
267     }
268   if (current_idles)
269     {
270       gtk_handle_current_idles();
271       return done;
272     }
273   
274   /* If there is a valid event in 'next_event' then move it to 'event'
275    */
276   if (next_event)
277     {
278       event = next_event;
279       next_event = NULL;
280     }
281   
282   /* If we don't have an event then get one.
283    */
284   if (!event)
285     {
286       /* Handle setting of the "gdk" timer. If there are no
287        *  timeout functions, then the timer is turned off.
288        *  If there are timeout functions, then the timer is
289        *  set to the shortest timeout interval (which is
290        *  the first timeout function).
291        */
292       gtk_handle_timer ();
293       
294       if (blocking) event = gdk_event_get ();
295     }
296   
297   /* "gdk_event_get" can return FALSE if the timer goes off
298    *  and no events are pending. Therefore, we should make
299    *  sure that we got an event before continuing.
300    */
301   if (event)
302     {
303       /* If there are any events pending then get the next one.
304        */
305       if (gdk_events_pending () > 0)
306         next_event = gdk_event_get ();
307       
308       /* Try to compress enter/leave notify events. These event
309        *  pairs occur when the mouse is dragged quickly across
310        *  a window with many buttons (or through a menu). Instead
311        *  of highlighting and de-highlighting each widget that
312        *  is crossed it is better to simply de-highlight the widget
313        *  which contained the mouse initially and highlight the
314        *  widget which ends up containing the mouse.
315        */
316       if (next_event)
317         if (((event->type == GDK_ENTER_NOTIFY) ||
318              (event->type == GDK_LEAVE_NOTIFY)) &&
319             ((next_event->type == GDK_ENTER_NOTIFY) ||
320              (next_event->type == GDK_LEAVE_NOTIFY)) &&
321             (next_event->type != event->type) &&
322             (next_event->any.window == event->any.window))
323           {
324             gdk_event_free (event);
325             gdk_event_free (next_event);
326             next_event = NULL;
327
328             return done;
329           }
330
331       /* Push the event onto a stack of current events for
332        * gtk_current_event_get().
333        */
334       current_events = g_list_prepend (current_events, event);
335       
336       /* Find the widget which got the event. We store the widget
337        *  in the user_data field of GdkWindow's.
338        */
339       event_widget = gtk_get_event_widget (event);
340       
341       /* If there is a grab in effect...
342        */
343       if (grabs)
344         {
345           grab_widget = grabs->data;
346           
347           /* If the grab widget is an ancestor of the event widget
348            *  then we send the event to the original event widget.
349            *  This is the key to implementing modality.
350            */
351           if (gtk_widget_is_ancestor (event_widget, grab_widget))
352             grab_widget = event_widget;
353         }
354       else
355         {
356           grab_widget = event_widget;
357         }
358       
359       /* Not all events get sent to the grabbing widget.
360        * The delete, destroy, expose, focus change and resize
361        *  events still get sent to the event widget because
362        *  1) these events have no meaning for the grabbing widget
363        *  and 2) redirecting these events to the grabbing widget
364        *  could cause the display to be messed up.
365        */
366       switch (event->type)
367         {
368         case GDK_NOTHING:
369           break;
370           
371         case GDK_DELETE:
372           gtk_widget_ref (event_widget);
373           if (gtk_widget_event (event_widget, event))
374             gtk_widget_destroy (event_widget);
375           gtk_widget_unref (event_widget);
376           break;
377           
378         case GDK_DESTROY:
379           gtk_widget_ref (event_widget);
380           gtk_widget_event (event_widget, event);
381           gtk_widget_destroy (event_widget);
382           gtk_widget_unref (event_widget);
383           break;
384           
385         case GDK_PROPERTY_NOTIFY:
386           /* To handle selection INCR transactions, we select
387            * PropertyNotify events on the requestor window and create
388            * a corresponding (fake) GdkWindow so that events get
389            * here. There won't be a widget though, so we have to handle
390            * them specially
391            */
392           
393           if (event_widget == NULL)
394             {
395               gtk_selection_incr_event (event->any.window,
396                                         &event->property);
397               break;
398             }
399           /* otherwise fall through */
400         case GDK_EXPOSE:
401         case GDK_NO_EXPOSE:
402         case GDK_FOCUS_CHANGE:
403         case GDK_CONFIGURE:
404         case GDK_MAP:
405         case GDK_UNMAP:
406         case GDK_SELECTION_CLEAR:
407         case GDK_SELECTION_REQUEST:
408         case GDK_SELECTION_NOTIFY:
409         case GDK_CLIENT_EVENT:
410         case GDK_DRAG_BEGIN:
411         case GDK_DRAG_REQUEST:
412         case GDK_DROP_ENTER:
413         case GDK_DROP_LEAVE:
414         case GDK_DROP_DATA_AVAIL:
415         case GDK_VISIBILITY_NOTIFY:
416           gtk_widget_event (event_widget, event);
417           break;
418           
419         case GDK_KEY_PRESS:
420         case GDK_KEY_RELEASE:
421           if (key_snoopers)
422             {
423               if (gtk_invoke_key_snoopers (grab_widget, event))
424                 break;
425             }
426           /* else fall through */
427         case GDK_MOTION_NOTIFY:
428         case GDK_BUTTON_PRESS:
429         case GDK_2BUTTON_PRESS:
430         case GDK_3BUTTON_PRESS:
431         case GDK_BUTTON_RELEASE:
432         case GDK_PROXIMITY_IN:
433         case GDK_PROXIMITY_OUT:
434         case GDK_OTHER_EVENT:
435           gtk_propagate_event (grab_widget, event);
436           break;
437           
438         case GDK_ENTER_NOTIFY:
439           if (grab_widget && GTK_WIDGET_IS_SENSITIVE (grab_widget))
440             {
441               gtk_widget_event (grab_widget, event);
442               if (event_widget == grab_widget)
443                 GTK_PRIVATE_SET_FLAGS (event_widget, GTK_LEAVE_PENDING);
444             }
445           break;
446
447         case GDK_LEAVE_NOTIFY:
448           if (event_widget && GTK_WIDGET_LEAVE_PENDING (event_widget))
449             {
450               GTK_PRIVATE_UNSET_FLAGS (event_widget, GTK_LEAVE_PENDING);
451               gtk_widget_event (event_widget, event);
452             }
453           else if (grab_widget && GTK_WIDGET_IS_SENSITIVE (grab_widget))
454             gtk_widget_event (grab_widget, event);
455           break;
456         }
457       
458       tmp_list = current_events;
459       current_events = g_list_remove_link (current_events, tmp_list);
460       g_list_free_1 (tmp_list);
461
462       gdk_event_free (event);
463     }
464   else
465     {
466       if (gdk_events_pending() == 0)
467         gtk_handle_idle ();
468     }
469   
470   /* Handle a timeout functions that may have expired.
471    */
472   gtk_handle_timeouts ();
473   
474   return done;
475 }
476
477 gint
478 gtk_true (void)
479 {
480   return TRUE;
481 }
482
483 gint
484 gtk_false (void)
485 {
486   return FALSE;
487 }
488
489 void
490 gtk_grab_add (GtkWidget *widget)
491 {
492   g_return_if_fail (widget != NULL);
493
494   if (!GTK_WIDGET_HAS_GRAB (widget))
495     {
496       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
497       
498       grabs = g_slist_prepend (grabs, widget);
499       gtk_widget_ref (widget);
500     }
501 }
502
503 void
504 gtk_grab_remove (GtkWidget *widget)
505 {
506   g_return_if_fail (widget != NULL);
507
508   if (GTK_WIDGET_HAS_GRAB (widget))
509     {
510       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
511
512       grabs = g_slist_remove (grabs, widget);
513       gtk_widget_unref (widget);
514     }
515 }
516
517 void
518 gtk_init_add (GtkFunction function,
519               gpointer    data)
520 {
521   GtkInitFunction *init;
522   
523   init = g_new (GtkInitFunction, 1);
524   init->function = function;
525   init->data = data;
526   
527   init_functions = g_list_prepend (init_functions, init);
528 }
529
530 gint
531 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
532                          gpointer        func_data)
533 {
534   GtkKeySnooperData *data;
535   static guint snooper_id = 1;
536
537   g_return_val_if_fail (snooper != NULL, 0);
538
539   data = g_new (GtkKeySnooperData, 1);
540   data->func = snooper;
541   data->func_data = func_data;
542   data->id = snooper_id++;
543   key_snoopers = g_slist_prepend (key_snoopers, data);
544
545   return data->id;
546 }
547
548 void
549 gtk_key_snooper_remove (gint            snooper_id)
550 {
551   GtkKeySnooperData *data = NULL;
552   GSList *slist;
553
554   slist = key_snoopers;
555   while (slist)
556     {
557       data = slist->data;
558       if (data->id == snooper_id)
559         break;
560
561       slist = slist->next;
562       data = NULL;
563     }
564   if (data)
565     key_snoopers = g_slist_remove (key_snoopers, data);
566 }
567
568 static gint
569 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
570                          GdkEvent  *event)
571 {
572   GSList *slist;
573   gint return_val = FALSE;
574
575   slist = key_snoopers;
576   while (slist && !return_val)
577     {
578       GtkKeySnooperData *data;
579
580       data = slist->data;
581       slist = slist->next;
582       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
583     }
584
585   return return_val;
586 }
587
588 static gint
589 gtk_timeout_add_internal (guint32     interval,
590                           gint        interp,
591                           GtkFunction function,
592                           gpointer    data,
593                           GtkDestroyNotify destroy)
594 {
595   static gint timeout_tag = 1;
596   GtkTimeoutFunction *timeoutf;
597   
598   /* Create a new timeout function structure.
599    * The start time is the current time.
600    */
601   if (!timeout_mem_chunk)
602     timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
603                                          1024, G_ALLOC_AND_FREE);
604   
605   timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
606   
607   timeoutf->tag = timeout_tag++;
608   timeoutf->start = gdk_time_get ();
609   timeoutf->interval = interval;
610   timeoutf->originterval = interval;
611   timeoutf->interp = interp;
612   timeoutf->function = function;
613   timeoutf->data = data;
614   timeoutf->destroy = destroy;
615   
616   gtk_timeout_insert (timeoutf);
617   
618   return timeoutf->tag;
619 }
620
621 static void
622 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
623 {
624   if (timeoutf->destroy)
625     (timeoutf->destroy) (timeoutf->data);
626   g_mem_chunk_free (timeout_mem_chunk, timeoutf);
627 }
628
629 gint
630 gtk_timeout_add (guint32     interval,
631                  GtkFunction function,
632                  gpointer    data)
633 {
634   return gtk_timeout_add_internal (interval, FALSE, function, data, NULL);
635 }
636
637 gint
638 gtk_timeout_add_interp (guint32            interval,
639                         GtkCallbackMarshal function,
640                         gpointer           data,
641                         GtkDestroyNotify   destroy)
642 {
643   return gtk_timeout_add_internal (interval, TRUE,
644                                    (GtkFunction) function,
645                                    data, destroy);
646 }
647
648 void
649 gtk_timeout_remove (gint tag)
650 {
651   GtkTimeoutFunction *timeoutf;
652   GList *tmp_list;
653   
654   /* Remove a timeout function.
655    * (Which, basically, involves searching the
656    *  list for the tag).
657    */
658   tmp_list = timeout_functions;
659   while (tmp_list)
660     {
661       timeoutf = tmp_list->data;
662       
663       if (timeoutf->tag == tag)
664         {
665           timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
666           g_list_free (tmp_list);
667           gtk_timeout_destroy (timeoutf);
668           
669           return;
670         }
671       
672       tmp_list = tmp_list->next;
673     }
674   
675   tmp_list = current_timeouts;
676   while (tmp_list)
677     {
678       timeoutf = tmp_list->data;
679       
680       if (timeoutf->tag == tag)
681         {
682           current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
683           g_list_free (tmp_list);
684           gtk_timeout_destroy (timeoutf);
685           
686           return;
687         }
688       
689       tmp_list = tmp_list->next;
690     }
691 }
692
693 static gint
694 gtk_idle_add_internal (gint             interp,
695                        GtkFunction      function,
696                        gpointer         data,
697                        GtkDestroyNotify destroy)
698 {
699   static gint idle_tag = 1;
700   GtkIdleFunction *idlef;
701   
702   if (!idle_mem_chunk)
703     idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
704                                       1024, G_ALLOC_AND_FREE);
705   
706   idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
707   
708   idlef->tag = idle_tag++;
709   idlef->interp = interp;
710   idlef->function = function;
711   idlef->data = data;
712   idlef->destroy = destroy;
713   
714   idle_functions = g_list_append (idle_functions, idlef);
715   
716   return idlef->tag;
717 }
718
719 static void
720 gtk_idle_destroy (GtkIdleFunction *idlef)
721 {
722   if (idlef->destroy)
723     idlef->destroy (idlef->data);
724   g_mem_chunk_free (idle_mem_chunk, idlef);
725 }
726
727 gint
728 gtk_idle_add (GtkFunction function,
729               gpointer    data)
730 {
731   return gtk_idle_add_internal (FALSE, function, data, NULL);
732 }
733
734 gint
735 gtk_idle_add_interp (GtkCallbackMarshal function,
736                      gpointer           data,
737                      GtkDestroyNotify   destroy)
738 {
739   return gtk_idle_add_internal (TRUE, (GtkFunction)function, data, destroy);
740 }
741
742 void
743 gtk_idle_remove (gint tag)
744 {
745   GtkIdleFunction *idlef;
746   GList *tmp_list;
747   
748   tmp_list = idle_functions;
749   while (tmp_list)
750     {
751       idlef = tmp_list->data;
752       
753       if (idlef->tag == tag)
754         {
755           idle_functions = g_list_remove_link (idle_functions, tmp_list);
756           g_list_free (tmp_list);
757           gtk_idle_destroy (idlef);
758           
759           return;
760         }
761       
762       tmp_list = tmp_list->next;
763     }
764   
765   tmp_list = current_idles;
766   while (tmp_list)
767     {
768       idlef = tmp_list->data;
769       
770       if (idlef->tag == tag)
771         {
772           current_idles = g_list_remove_link (current_idles, tmp_list);
773           g_list_free (tmp_list);
774           gtk_idle_destroy (idlef);
775           
776           return;
777         }
778       
779       tmp_list = tmp_list->next;
780     }
781 }
782
783 void
784 gtk_idle_remove_by_data (gpointer data)
785 {
786   GtkIdleFunction *idlef;
787   GList *tmp_list;
788   
789   tmp_list = idle_functions;
790   while (tmp_list)
791     {
792       idlef = tmp_list->data;
793       
794       if (idlef->data == data)
795         {
796           idle_functions = g_list_remove_link (idle_functions, tmp_list);
797           g_list_free (tmp_list);
798           g_mem_chunk_free (idle_mem_chunk, idlef);
799           
800           return;
801         }
802       
803       tmp_list = tmp_list->next;
804     }
805   
806   tmp_list = current_idles;
807   while (tmp_list)
808     {
809       idlef = tmp_list->data;
810       
811       if (idlef->data == data)
812         {
813           current_idles = g_list_remove_link (current_idles, tmp_list);
814           g_list_free (tmp_list);
815           g_mem_chunk_free (idle_mem_chunk, idlef);
816           
817           return;
818         }
819       
820       tmp_list = tmp_list->next;
821     }
822 }
823
824 static void
825 gtk_invoke_input_function (GtkInputFunction *input,
826                            gint source,
827                            GdkInputCondition condition)
828 {
829   GtkArg args[3];
830   args[0].type = GTK_TYPE_INT;
831   args[0].name = NULL;
832   GTK_VALUE_INT(args[0]) = source;
833   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
834   args[1].name = NULL;
835   GTK_VALUE_FLAGS(args[1]) = condition;
836   args[2].type = GTK_TYPE_NONE;
837   args[2].name = NULL;
838
839   input->callback (NULL, input->data, 2, args);
840 }
841
842 static void
843 gtk_destroy_input_function (GtkInputFunction *input)
844 {
845   if (input->destroy)
846     (input->destroy) (input->data);
847   g_free (input);
848 }
849
850 gint
851 gtk_input_add_interp (gint source,
852                       GdkInputCondition condition,
853                       GtkCallbackMarshal callback,
854                       gpointer data,
855                       GtkDestroyNotify destroy)
856 {
857   GtkInputFunction *input = g_new (GtkInputFunction, 1);
858   input->callback = callback;
859   input->data = data;
860   input->destroy = destroy;
861   return gdk_input_add_interp (source,
862                                condition,
863                                (GdkInputFunction) gtk_invoke_input_function,
864                                input,
865                                (GdkDestroyNotify) gtk_destroy_input_function);
866 }
867
868 void
869 gtk_input_remove (gint tag)
870 {
871   gdk_input_remove (tag);
872 }
873
874 GdkEvent *
875 gtk_get_current_event ()
876 {
877   if (current_events)
878     return gdk_event_copy ((GdkEvent *) current_events->data);
879   else
880     return NULL;
881 }
882
883 GtkWidget*
884 gtk_get_event_widget (GdkEvent *event)
885 {
886   GtkWidget *widget;
887
888   widget = NULL;
889   if (event->any.window)
890     gdk_window_get_user_data (event->any.window, (void**) &widget);
891   
892   return widget;
893 }
894
895 static void
896 gtk_exit_func ()
897 {
898   if (initialized)
899     {
900       initialized = FALSE;
901       gtk_preview_uninit ();
902     }
903 }
904
905 static void
906 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
907 {
908   GtkTimeoutFunction *temp;
909   GList *temp_list;
910   GList *new_list;
911   
912   g_assert (timeoutf != NULL);
913   
914   /* Insert the timeout function appropriately.
915    * Appropriately meaning sort it into the list
916    *  of timeout functions.
917    */
918   temp_list = timeout_functions;
919   while (temp_list)
920     {
921       temp = temp_list->data;
922       if (timeoutf->interval < temp->interval)
923         {
924           new_list = g_list_alloc ();
925           new_list->data = timeoutf;
926           new_list->next = temp_list;
927           new_list->prev = temp_list->prev;
928           if (temp_list->prev)
929             temp_list->prev->next = new_list;
930           temp_list->prev = new_list;
931           
932           if (temp_list == timeout_functions)
933             timeout_functions = new_list;
934           
935           return;
936         }
937       
938       temp_list = temp_list->next;
939     }
940   
941   timeout_functions = g_list_append (timeout_functions, timeoutf);
942 }
943
944 static gint
945 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
946 {
947   if (!timeoutf->interp)
948     return timeoutf->function (timeoutf->data);
949   else
950     {
951       GtkArg args[1];
952       gint ret_val = FALSE;
953       args[0].name = NULL;
954       args[0].type = GTK_TYPE_BOOL;
955       args[0].d.pointer_data = &ret_val;
956       ((GtkCallbackMarshal)timeoutf->function) (NULL,
957                                                 timeoutf->data,
958                                                 0, args);
959       return ret_val;
960     }
961 }
962
963 static void
964 gtk_handle_current_timeouts (guint32 the_time)
965 {
966   GList *tmp_list;
967   GtkTimeoutFunction *timeoutf;
968   
969   while (current_timeouts)
970     {
971       tmp_list = current_timeouts;
972       timeoutf = tmp_list->data;
973       
974       current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
975       g_list_free (tmp_list);
976       
977       if (gtk_invoke_timeout_function (timeoutf) == FALSE)
978         {
979           gtk_timeout_destroy (timeoutf);
980         }
981       else
982         {
983           timeoutf->interval = timeoutf->originterval;
984           timeoutf->start = the_time;
985           gtk_timeout_insert (timeoutf);
986         }
987     }
988 }
989
990 static void
991 gtk_handle_timeouts ()
992 {
993   guint32 the_time;
994   GList *tmp_list;
995   GList *tmp_list2;
996   GtkTimeoutFunction *timeoutf;
997   
998   /* Caller must already have called gtk_handle_current_timeouts if
999    * necessary */
1000   g_assert (current_timeouts == NULL);
1001   
1002   if (timeout_functions)
1003     {
1004       the_time = gdk_time_get ();
1005       
1006       tmp_list = timeout_functions;
1007       while (tmp_list)
1008         {
1009           timeoutf = tmp_list->data;
1010           
1011           if (timeoutf->interval <= (the_time - timeoutf->start))
1012             {
1013               tmp_list2 = tmp_list;
1014               tmp_list = tmp_list->next;
1015               
1016               timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1017               current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1018             }
1019           else
1020             {
1021               timeoutf->interval -= (the_time - timeoutf->start);
1022               timeoutf->start = the_time;
1023               tmp_list = tmp_list->next;
1024             }
1025         }
1026       
1027       if (current_timeouts)
1028         gtk_handle_current_timeouts(the_time);
1029     }
1030 }
1031
1032 static gint
1033 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1034 {
1035   if (!idlef->interp)
1036     return idlef->function (idlef->data);
1037   else
1038     {
1039       GtkArg args[1];
1040       gint ret_val = FALSE;
1041       args[0].name = NULL;
1042       args[0].type = GTK_TYPE_BOOL;
1043       args[0].d.pointer_data = &ret_val;
1044       ((GtkCallbackMarshal)idlef->function) (NULL,
1045                                              idlef->data,
1046                                              0, args);
1047       return ret_val;
1048     }
1049 }
1050
1051 static void
1052 gtk_handle_current_idles ()
1053 {
1054   GList *tmp_list;
1055   GtkIdleFunction *idlef;
1056   
1057   while (current_idles)
1058     {
1059       tmp_list = current_idles;
1060       idlef = tmp_list->data;
1061       
1062       current_idles = g_list_remove_link (current_idles, tmp_list);
1063       
1064       if (gtk_idle_invoke_function (idlef) == FALSE)
1065         {
1066           g_list_free (tmp_list);
1067           gtk_idle_destroy (idlef);
1068         }
1069       else
1070         {
1071           idle_functions = g_list_concat (idle_functions, tmp_list);
1072         }
1073     }
1074 }
1075
1076 static void
1077 gtk_handle_idle ()
1078 {
1079   /* Caller must already have called gtk_handle_current_idles if
1080    * necessary
1081    */
1082   g_assert (current_idles == NULL);
1083   
1084   if (idle_functions)
1085     {
1086       current_idles = idle_functions;
1087       idle_functions = NULL;
1088       
1089       gtk_handle_current_idles();
1090     }
1091 }
1092
1093 static void
1094 gtk_handle_timer ()
1095 {
1096   GtkTimeoutFunction *timeoutf;
1097   
1098   if (idle_functions)
1099     {
1100       gdk_timer_set (0);
1101       gdk_timer_enable ();
1102     }
1103   else if (timeout_functions)
1104     {
1105       timeoutf = timeout_functions->data;
1106       gdk_timer_set (timeoutf->interval);
1107       gdk_timer_enable ();
1108     }
1109   else
1110     {
1111       gdk_timer_set (0);
1112       gdk_timer_disable ();
1113     }
1114 }
1115
1116 static void
1117 gtk_propagate_event (GtkWidget *widget,
1118                      GdkEvent  *event)
1119 {
1120   GtkWidget *parent;
1121   gint handled_event;
1122   
1123   g_return_if_fail (widget != NULL);
1124   g_return_if_fail (event != NULL);
1125   
1126   handled_event = FALSE;
1127   
1128   if ((event->type == GDK_KEY_PRESS) ||
1129       (event->type == GDK_KEY_RELEASE))
1130     {
1131       /* Only send key events to window widgets.
1132        *  The window widget will in turn pass the
1133        *  key event on to the currently focused widget
1134        *  for that window.
1135        */
1136       parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1137       if (parent && GTK_WIDGET_IS_SENSITIVE (parent)
1138           && gtk_widget_event (parent, event))
1139         return;
1140     }
1141   
1142   /* Other events get propagated up the widget tree
1143    *  so that parents can see the button and motion
1144    *  events of the children.
1145    */
1146   while (!handled_event && widget)
1147     {
1148       parent = widget->parent;
1149       handled_event = (!GTK_WIDGET_IS_SENSITIVE (widget) ||
1150                        gtk_widget_event (widget, event));
1151       widget = parent;
1152     }
1153 }
1154
1155
1156 static void
1157 gtk_error (gchar *str)
1158 {
1159   gtk_print (str);
1160 }
1161
1162 static void
1163 gtk_warning (gchar *str)
1164 {
1165   gtk_print (str);
1166 }
1167
1168 static void
1169 gtk_message (gchar *str)
1170 {
1171   gtk_print (str);
1172 }
1173
1174 static void
1175 gtk_print (gchar *str)
1176 {
1177   static GtkWidget *window = NULL;
1178   static GtkWidget *text;
1179   static int level = 0;
1180   GtkWidget *box1;
1181   GtkWidget *box2;
1182   GtkWidget *table;
1183   GtkWidget *hscrollbar;
1184   GtkWidget *vscrollbar;
1185   GtkWidget *separator;
1186   GtkWidget *button;
1187   
1188   if (level > 0)
1189     {
1190       fputs (str, stdout);
1191       fflush (stdout);
1192       return;
1193     }
1194   
1195   if (!window)
1196     {
1197       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1198       
1199       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1200                           (GtkSignalFunc) gtk_widget_destroyed,
1201                           &window);
1202       
1203       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1204       
1205       box1 = gtk_vbox_new (FALSE, 0);
1206       gtk_container_add (GTK_CONTAINER (window), box1);
1207       gtk_widget_show (box1);
1208       
1209       
1210       box2 = gtk_vbox_new (FALSE, 10);
1211       gtk_container_border_width (GTK_CONTAINER (box2), 10);
1212       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1213       gtk_widget_show (box2);
1214       
1215       
1216       table = gtk_table_new (2, 2, FALSE);
1217       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1218       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1219       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1220       gtk_widget_show (table);
1221       
1222       text = gtk_text_new (NULL, NULL);
1223       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1224       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1225       gtk_widget_show (text);
1226       gtk_widget_realize (text);
1227       
1228       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1229       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1230                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1231       gtk_widget_show (hscrollbar);
1232       
1233       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1234       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1235                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1236       gtk_widget_show (vscrollbar);
1237       
1238       separator = gtk_hseparator_new ();
1239       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1240       gtk_widget_show (separator);
1241       
1242       
1243       box2 = gtk_vbox_new (FALSE, 10);
1244       gtk_container_border_width (GTK_CONTAINER (box2), 10);
1245       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1246       gtk_widget_show (box2);
1247       
1248       
1249       button = gtk_button_new_with_label ("close");
1250       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1251                                  (GtkSignalFunc) gtk_widget_hide,
1252                                  GTK_OBJECT (window));
1253       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1254       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1255       gtk_widget_grab_default (button);
1256       gtk_widget_show (button);
1257     }
1258   
1259   level += 1;
1260   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1261   level -= 1;
1262   
1263   if (!GTK_WIDGET_VISIBLE (window))
1264     gtk_widget_show (window);
1265 }