]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
return TRUE for GtkWidget::delete_event to avoid destruction of our float
[~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 #include "../config.h"
36
37
38 /* Private type definitions
39  */
40 typedef struct _GtkInitFunction          GtkInitFunction;
41 typedef struct _GtkQuitFunction          GtkQuitFunction;
42 typedef struct _GtkTimeoutFunction       GtkTimeoutFunction;
43 typedef struct _GtkIdleFunction          GtkIdleFunction;
44 typedef struct _GtkInputFunction         GtkInputFunction;
45 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
46
47 struct _GtkInitFunction
48 {
49   GtkFunction function;
50   gpointer data;
51 };
52
53 struct _GtkQuitFunction
54 {
55   guint id;
56   guint main_level;
57   GtkCallbackMarshal marshal;
58   GtkFunction function;
59   gpointer data;
60   GtkDestroyNotify destroy;
61 };
62
63 struct _GtkTimeoutFunction
64 {
65   guint tag;
66   guint32 start;
67   guint32 interval;
68   guint32 originterval;
69   GtkFunction function;
70   GtkCallbackMarshal marshal;
71   gpointer data;
72   GtkDestroyNotify destroy;
73 };
74
75 struct _GtkIdleFunction
76 {
77   guint tag;
78   gint priority;
79   GtkCallbackMarshal marshal;
80   GtkFunction function;
81   gpointer data;
82   GtkDestroyNotify destroy;
83 };
84
85 struct _GtkInputFunction
86 {
87   GtkCallbackMarshal callback;
88   gpointer data;
89   GtkDestroyNotify destroy;
90 };
91
92 struct _GtkKeySnooperData
93 {
94   GtkKeySnoopFunc func;
95   gpointer func_data;
96   guint id;
97 };
98
99 static void  gtk_exit_func               (void);
100 static gint  gtk_quit_invoke_function    (GtkQuitFunction    *quitf);
101 static void  gtk_quit_destroy            (GtkQuitFunction    *quitf);
102 static void  gtk_timeout_insert          (GtkTimeoutFunction *timeoutf);
103 static void  gtk_handle_current_timeouts (guint32             the_time);
104 static void  gtk_handle_current_idles    (void);
105 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
106                                           GdkEvent           *event);
107 static void  gtk_handle_timeouts         (void);
108 static void  gtk_handle_idle             (void);
109 static void  gtk_handle_timer            (void);
110 static void  gtk_propagate_event         (GtkWidget          *widget,
111                                           GdkEvent           *event);
112 static void  gtk_error                   (gchar              *str);
113 static void  gtk_warning                 (gchar              *str);
114 static void  gtk_message                 (gchar              *str);
115 static void  gtk_print                   (gchar              *str);
116
117 static gint  gtk_idle_compare            (gpointer            a, 
118                                           gpointer            b);
119
120 static gint  gtk_timeout_compare         (gpointer            a, 
121                                           gpointer            b);
122
123 const guint gtk_major_version = GTK_MAJOR_VERSION;
124 const guint gtk_minor_version = GTK_MINOR_VERSION;
125 const guint gtk_micro_version = GTK_MICRO_VERSION;
126
127 static gboolean iteration_done = FALSE;
128 static guint main_level = 0;
129 static gint initialized = FALSE;
130 static GdkEvent *next_event = NULL;
131 static GList *current_events = NULL;
132
133 static GSList *grabs = NULL;               /* A stack of unique grabs. The grabbing
134                                             *  widget is the first one on the list.
135                                             */
136 static GList *init_functions = NULL;       /* A list of init functions.
137                                             */
138 static GList *quit_functions = NULL;       /* A list of quit functions.
139                                             */
140 static GList *timeout_functions = NULL;    /* A list of timeout functions sorted by
141                                             *  when the length of the time interval
142                                             *  remaining. Therefore, the first timeout
143                                             *  function to expire is at the head of
144                                             *  the list and the last to expire is at
145                                             *  the tail of the list.
146                                             */
147 static GList *idle_functions = NULL;       /* A list of idle functions.
148                                             */
149
150 static GList *current_idles = NULL;
151 static GList *current_timeouts = NULL;
152 static GMemChunk *timeout_mem_chunk = NULL;
153 static GMemChunk *idle_mem_chunk = NULL;
154 static GMemChunk *quit_mem_chunk = NULL;
155
156 static GSList *key_snoopers = NULL;
157
158 static GdkVisual *gtk_visual;              /* The visual to be used in creating new
159                                             *  widgets.
160                                             */
161 static GdkColormap *gtk_colormap;          /* The colormap to be used in creating new
162                                             *  widgets.
163                                             */
164
165 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
166
167 #ifdef G_ENABLE_DEBUG
168 static GDebugKey gtk_debug_keys[] = {
169   {"objects", GTK_DEBUG_OBJECTS}
170 };
171
172 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
173
174 #endif /* G_ENABLE_DEBUG */
175
176
177
178
179 void
180 gtk_init (int    *argc,
181           char ***argv)
182 {
183   if (0)
184     {
185       g_set_error_handler (gtk_error);
186       g_set_warning_handler (gtk_warning);
187       g_set_message_handler (gtk_message);
188       g_set_print_handler (gtk_print);
189     }
190   
191   /* Initialize "gdk". We pass along the 'argc' and 'argv'
192    *  parameters as they contain information that GDK uses
193    */
194   gdk_init (argc, argv);
195   
196 #ifdef G_ENABLE_DEBUG
197   {
198     gchar *debug_string = getenv("GTK_DEBUG");
199     if (debug_string != NULL)
200       gtk_debug_flags = g_parse_debug_string (debug_string,
201                                               gtk_debug_keys,
202                                               gtk_ndebug_keys);
203   }
204
205   if (argc && argv)
206     {
207       gint i;
208       
209       for (i = 1; i < *argc;)
210         {
211           if ((*argv)[i] == NULL)
212             {
213               i += 1;
214               continue;
215             }
216
217           if (strcmp ("--gtk-debug", (*argv)[i]) == 0)
218             {
219               (*argv)[i] = NULL;
220
221               if ((i + 1) < *argc && (*argv)[i + 1])
222                 {
223                   gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
224                                                            gtk_debug_keys,
225                                                            gtk_ndebug_keys);
226                   (*argv)[i + 1] = NULL;
227                   i += 1;
228                 }
229             }
230           else if (strcmp ("--gtk-no-debug", (*argv)[i]) == 0)
231             {
232               (*argv)[i] = NULL;
233
234               if ((i + 1) < *argc && (*argv)[i + 1])
235                 {
236                   gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
237                                                             gtk_debug_keys,
238                                                             gtk_ndebug_keys);
239                   (*argv)[i + 1] = NULL;
240                   i += 1;
241                 }
242             }
243           i += 1;
244         }
245     }
246
247 #endif /* G_ENABLE_DEBUG */
248
249   /* Initialize the default visual and colormap to be
250    *  used in creating widgets. (We want to use the system
251    *  defaults so as to be nice to the colormap).
252    */
253   gtk_visual = gdk_visual_get_system ();
254   gtk_colormap = gdk_colormap_get_system ();
255   gtk_rc_init ();
256   
257   gtk_type_init ();
258   
259   /* Register an exit function to make sure we are able to cleanup.
260    */
261   if (ATEXIT (gtk_exit_func))
262     g_warning ("unable to register exit function");
263   
264   /* Set the 'initialized' flag.
265    */
266   initialized = TRUE;
267 }
268
269 void
270 gtk_exit (int errorcode)
271 {
272   /* Only if "gtk" has been initialized should we de-initialize.
273    */
274   /* de-initialisation is done by the gtk_exit_funct(),
275    * no need to do this here (Alex J.)
276    */
277   gdk_exit(errorcode);
278 }
279
280 gchar*
281 gtk_set_locale ()
282 {
283   return gdk_set_locale ();
284 }
285
286 void
287 gtk_main ()
288 {
289   GList *tmp_list;
290   GList *functions;
291   GtkInitFunction *init;
292   int old_done;
293   
294   main_level++;
295   
296   tmp_list = functions = init_functions;
297   init_functions = NULL;
298   
299   while (tmp_list)
300     {
301       init = tmp_list->data;
302       tmp_list = tmp_list->next;
303       
304       (* init->function) (init->data);
305       g_free (init);
306     }
307   g_list_free (functions);
308   
309   old_done = iteration_done;
310   while (!gtk_main_iteration ())
311     ;
312   iteration_done = old_done;
313
314   if (quit_functions)
315     {
316       GList *reinvoke_list = NULL;
317       GtkQuitFunction *quitf;
318
319       while (quit_functions)
320         {
321           quitf = quit_functions->data;
322
323           quit_functions = g_list_remove_link (quit_functions, quit_functions);
324
325           if ((quitf->main_level &&
326                quitf->main_level != main_level) ||
327               gtk_quit_invoke_function (quitf) == FALSE)
328             {
329               g_list_free (tmp_list);
330               gtk_quit_destroy (quitf);
331             }
332           else
333             {
334               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
335             }
336         }
337       if (reinvoke_list)
338         {
339           GList *tmp_list;
340           
341           tmp_list = g_list_last (reinvoke_list);
342           if (quit_functions)
343             quit_functions->prev = tmp_list;
344           tmp_list->next = quit_functions;
345           quit_functions = tmp_list;
346         }
347     }
348               
349   main_level--;
350 }
351
352 guint
353 gtk_main_level (void)
354 {
355   return main_level;
356 }
357
358 void
359 gtk_main_quit ()
360 {
361   iteration_done = TRUE;
362 }
363
364 gint
365 gtk_events_pending (void)
366 {
367   gint result = gdk_events_pending() + ((next_event != NULL) ? 1 : 0);
368
369   if (idle_functions &&
370       (((GtkIdleFunction *)idle_functions->data)->priority >=
371        GTK_PRIORITY_INTERNAL))
372     result += 1;
373
374   return result;
375 }
376
377 gint 
378 gtk_main_iteration ()
379 {
380   return gtk_main_iteration_do (TRUE);
381 }
382
383 gint
384 gtk_main_iteration_do (gboolean blocking)
385 {
386   GtkWidget *event_widget;
387   GtkWidget *grab_widget;
388   GdkEvent *event = NULL;
389   GList *tmp_list;
390   
391   iteration_done = FALSE;
392   
393   /* If this is a recursive call, and there are pending timeouts or
394    * idles, finish them, then return immediately to avoid blocking
395    * in gdk_event_get()
396    */
397   if (current_timeouts)
398     {
399       gtk_handle_current_timeouts( gdk_time_get());
400       return iteration_done;
401     }
402   if (current_idles)
403     {
404       gtk_handle_current_idles ();
405       return iteration_done;
406     }
407   
408   /* If there is a valid event in 'next_event' then move it to 'event'
409    */
410   if (next_event)
411     {
412       event = next_event;
413       next_event = NULL;
414     }
415   
416   /* If we don't have an event then get one.
417    */
418   if (!event)
419     {
420       /* Handle setting of the "gdk" timer. If there are no
421        *  timeout functions, then the timer is turned off.
422        *  If there are timeout functions, then the timer is
423        *  set to the shortest timeout interval (which is
424        *  the first timeout function).
425        */
426       gtk_handle_timer ();
427       
428       if (blocking) event = gdk_event_get ();
429     }
430   
431   /* "gdk_event_get" can return FALSE if the timer goes off
432    *  and no events are pending. Therefore, we should make
433    *  sure that we got an event before continuing.
434    */
435   if (event)
436     {
437       /* If there are any events pending then get the next one.
438        */
439       if (gdk_events_pending () > 0)
440         next_event = gdk_event_get ();
441       
442       /* Try to compress enter/leave notify events. These event
443        *  pairs occur when the mouse is dragged quickly across
444        *  a window with many buttons (or through a menu). Instead
445        *  of highlighting and de-highlighting each widget that
446        *  is crossed it is better to simply de-highlight the widget
447        *  which contained the mouse initially and highlight the
448        *  widget which ends up containing the mouse.
449        */
450       if (next_event)
451         if (((event->type == GDK_ENTER_NOTIFY) ||
452              (event->type == GDK_LEAVE_NOTIFY)) &&
453             ((next_event->type == GDK_ENTER_NOTIFY) ||
454              (next_event->type == GDK_LEAVE_NOTIFY)) &&
455             (next_event->type != event->type) &&
456             (next_event->any.window == event->any.window))
457           {
458             gdk_event_free (event);
459             gdk_event_free (next_event);
460             next_event = NULL;
461             
462             goto event_handling_done;
463           }
464       
465       /* Find the widget which got the event. We store the widget
466        *  in the user_data field of GdkWindow's.
467        *  Ignore the event if we don't have a widget for it, except
468        *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
469        *  Though this happens rarely, bogus events can occour
470        *  for e.g. destroyed GdkWindows. 
471        */
472       event_widget = gtk_get_event_widget (event);
473       if (!event_widget)
474         {
475           /* To handle selection INCR transactions, we select
476            * PropertyNotify events on the requestor window and create
477            * a corresponding (fake) GdkWindow so that events get
478            * here. There won't be a widget though, so we have to handle
479            * them specially
480            */
481           if (event->type == GDK_PROPERTY_NOTIFY)
482             gtk_selection_incr_event (event->any.window,
483                                       &event->property);
484           
485           gdk_event_free (event);
486           
487           goto event_handling_done;
488         }
489       
490       /* Push the event onto a stack of current events for
491        * gtk_current_event_get().
492        */
493       current_events = g_list_prepend (current_events, event);
494       
495       /* If there is a grab in effect...
496        */
497       if (grabs)
498         {
499           grab_widget = grabs->data;
500           
501           /* If the grab widget is an ancestor of the event widget
502            *  then we send the event to the original event widget.
503            *  This is the key to implementing modality.
504            */
505           if (gtk_widget_is_ancestor (event_widget, grab_widget))
506             grab_widget = event_widget;
507         }
508       else
509         {
510           grab_widget = event_widget;
511         }
512
513       /* Not all events get sent to the grabbing widget.
514        * The delete, destroy, expose, focus change and resize
515        *  events still get sent to the event widget because
516        *  1) these events have no meaning for the grabbing widget
517        *  and 2) redirecting these events to the grabbing widget
518        *  could cause the display to be messed up.
519        */
520       switch (event->type)
521         {
522         case GDK_NOTHING:
523           break;
524           
525         case GDK_DELETE:
526           gtk_widget_ref (event_widget);
527           if (!gtk_widget_event (event_widget, event) &&
528               !GTK_OBJECT_DESTROYED (event_widget))
529             gtk_widget_destroy (event_widget);
530           gtk_widget_unref (event_widget);
531           break;
532           
533         case GDK_DESTROY:
534           gtk_widget_ref (event_widget);
535           gtk_widget_event (event_widget, event);
536           if (!GTK_OBJECT_DESTROYED (event_widget))
537             gtk_widget_destroy (event_widget);
538           gtk_widget_unref (event_widget);
539           break;
540           
541         case GDK_PROPERTY_NOTIFY:
542         case GDK_EXPOSE:
543         case GDK_NO_EXPOSE:
544         case GDK_FOCUS_CHANGE:
545         case GDK_CONFIGURE:
546         case GDK_MAP:
547         case GDK_UNMAP:
548         case GDK_SELECTION_CLEAR:
549         case GDK_SELECTION_REQUEST:
550         case GDK_SELECTION_NOTIFY:
551         case GDK_CLIENT_EVENT:
552         case GDK_DRAG_BEGIN:
553         case GDK_DRAG_REQUEST:
554         case GDK_DROP_ENTER:
555         case GDK_DROP_LEAVE:
556         case GDK_DROP_DATA_AVAIL:
557         case GDK_VISIBILITY_NOTIFY:
558           gtk_widget_event (event_widget, event);
559           break;
560           
561         case GDK_KEY_PRESS:
562         case GDK_KEY_RELEASE:
563           if (key_snoopers)
564             {
565               if (gtk_invoke_key_snoopers (grab_widget, event))
566                 break;
567             }
568           /* else fall through */
569         case GDK_MOTION_NOTIFY:
570         case GDK_BUTTON_PRESS:
571         case GDK_2BUTTON_PRESS:
572         case GDK_3BUTTON_PRESS:
573         case GDK_BUTTON_RELEASE:
574         case GDK_PROXIMITY_IN:
575         case GDK_PROXIMITY_OUT:
576         case GDK_OTHER_EVENT:
577           gtk_propagate_event (grab_widget, event);
578           break;
579           
580         case GDK_ENTER_NOTIFY:
581           if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
582             {
583               gtk_widget_event (grab_widget, event);
584               if (event_widget == grab_widget)
585                 GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
586             }
587           break;
588           
589         case GDK_LEAVE_NOTIFY:
590           if (GTK_WIDGET_LEAVE_PENDING (event_widget))
591             {
592               GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
593               gtk_widget_event (event_widget, event);
594             }
595           else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
596             gtk_widget_event (grab_widget, event);
597           break;
598         }
599       
600       tmp_list = current_events;
601       current_events = g_list_remove_link (current_events, tmp_list);
602       g_list_free_1 (tmp_list);
603       
604       gdk_event_free (event);
605     }
606   else
607     {
608       if (gdk_events_pending() == 0)
609         gtk_handle_idle ();
610     }
611   
612 event_handling_done:
613   
614   /* Handle timeout functions that may have expired.
615    */
616   gtk_handle_timeouts ();
617   
618   return iteration_done;
619 }
620
621 gint
622 gtk_true (void)
623 {
624   return TRUE;
625 }
626
627 gint
628 gtk_false (void)
629 {
630   return FALSE;
631 }
632
633 void
634 gtk_grab_add (GtkWidget *widget)
635 {
636   g_return_if_fail (widget != NULL);
637   
638   if (!GTK_WIDGET_HAS_GRAB (widget))
639     {
640       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
641       
642       grabs = g_slist_prepend (grabs, widget);
643       gtk_widget_ref (widget);
644     }
645 }
646
647 GtkWidget*
648 gtk_grab_get_current (void)
649 {
650   if (grabs)
651     return GTK_WIDGET (grabs->data);
652   return NULL;
653 }
654
655 void
656 gtk_grab_remove (GtkWidget *widget)
657 {
658   g_return_if_fail (widget != NULL);
659   
660   if (GTK_WIDGET_HAS_GRAB (widget))
661     {
662       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
663       
664       grabs = g_slist_remove (grabs, widget);
665       gtk_widget_unref (widget);
666     }
667 }
668
669 void
670 gtk_init_add (GtkFunction function,
671               gpointer    data)
672 {
673   GtkInitFunction *init;
674   
675   init = g_new (GtkInitFunction, 1);
676   init->function = function;
677   init->data = data;
678   
679   init_functions = g_list_prepend (init_functions, init);
680 }
681
682 guint
683 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
684                          gpointer        func_data)
685 {
686   GtkKeySnooperData *data;
687   static guint snooper_id = 1;
688
689   g_return_val_if_fail (snooper != NULL, 0);
690
691   data = g_new (GtkKeySnooperData, 1);
692   data->func = snooper;
693   data->func_data = func_data;
694   data->id = snooper_id++;
695   key_snoopers = g_slist_prepend (key_snoopers, data);
696
697   return data->id;
698 }
699
700 void
701 gtk_key_snooper_remove (guint            snooper_id)
702 {
703   GtkKeySnooperData *data = NULL;
704   GSList *slist;
705
706   slist = key_snoopers;
707   while (slist)
708     {
709       data = slist->data;
710       if (data->id == snooper_id)
711         break;
712
713       slist = slist->next;
714       data = NULL;
715     }
716   if (data)
717     key_snoopers = g_slist_remove (key_snoopers, data);
718 }
719
720 static gint
721 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
722                          GdkEvent  *event)
723 {
724   GSList *slist;
725   gint return_val = FALSE;
726
727   slist = key_snoopers;
728   while (slist && !return_val)
729     {
730       GtkKeySnooperData *data;
731
732       data = slist->data;
733       slist = slist->next;
734       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
735     }
736
737   return return_val;
738 }
739
740 guint
741 gtk_timeout_add_full (guint32            interval,
742                       GtkFunction        function,
743                       GtkCallbackMarshal marshal,
744                       gpointer           data,
745                       GtkDestroyNotify   destroy)
746 {
747   static guint timeout_tag = 1;
748   GtkTimeoutFunction *timeoutf;
749   
750   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
751
752   /* Create a new timeout function structure.
753    * The start time is the current time.
754    */
755   if (!timeout_mem_chunk)
756     timeout_mem_chunk = g_mem_chunk_new ("timeout mem chunk", sizeof (GtkTimeoutFunction),
757                                          1024, G_ALLOC_AND_FREE);
758   
759   timeoutf = g_chunk_new (GtkTimeoutFunction, timeout_mem_chunk);
760   
761   timeoutf->tag = timeout_tag++;
762   timeoutf->start = gdk_time_get ();
763   timeoutf->interval = interval;
764   timeoutf->originterval = interval;
765   timeoutf->function = function;
766   timeoutf->marshal = marshal;
767   timeoutf->data = data;
768   timeoutf->destroy = destroy;
769   
770   gtk_timeout_insert (timeoutf);
771   
772   return timeoutf->tag;
773 }
774
775 static void
776 gtk_timeout_destroy (GtkTimeoutFunction *timeoutf)
777 {
778   if (timeoutf->destroy)
779     (timeoutf->destroy) (timeoutf->data);
780   g_mem_chunk_free (timeout_mem_chunk, timeoutf);
781 }
782
783 guint
784 gtk_timeout_add (guint32     interval,
785                  GtkFunction function,
786                  gpointer    data)
787 {
788   return gtk_timeout_add_full (interval, function, FALSE, data, NULL);
789 }
790
791 guint
792 gtk_timeout_add_interp (guint32            interval,
793                         GtkCallbackMarshal function,
794                         gpointer           data,
795                         GtkDestroyNotify   destroy)
796 {
797   return gtk_timeout_add_full (interval, NULL, function, data, destroy);
798 }
799
800 void
801 gtk_timeout_remove (guint tag)
802 {
803   GtkTimeoutFunction *timeoutf;
804   GList *tmp_list;
805   
806   /* Remove a timeout function.
807    * (Which, basically, involves searching the
808    *  list for the tag).
809    */
810   tmp_list = timeout_functions;
811   while (tmp_list)
812     {
813       timeoutf = tmp_list->data;
814       
815       if (timeoutf->tag == tag)
816         {
817           timeout_functions = g_list_remove_link (timeout_functions, tmp_list);
818           g_list_free (tmp_list);
819           gtk_timeout_destroy (timeoutf);
820           
821           return;
822         }
823       
824       tmp_list = tmp_list->next;
825     }
826   
827   tmp_list = current_timeouts;
828   while (tmp_list)
829     {
830       timeoutf = tmp_list->data;
831       
832       if (timeoutf->tag == tag)
833         {
834           current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
835           g_list_free (tmp_list);
836           gtk_timeout_destroy (timeoutf);
837           
838           return;
839         }
840       
841       tmp_list = tmp_list->next;
842     }
843 }
844
845 /* We rely on some knowledge of how g_list_insert_sorted works to make
846  * sure that we insert at the _end_ of the idles of this priority
847  */
848 static gint
849 gtk_idle_compare (gpointer a, gpointer b)
850 {
851   return (((GtkIdleFunction *)a)->priority < ((GtkIdleFunction *)b)->priority)
852     ? -1 : 1;
853 }
854
855 guint
856 gtk_quit_add_full (guint                main_level,
857                    GtkFunction          function,
858                    GtkCallbackMarshal   marshal,
859                    gpointer             data,
860                    GtkDestroyNotify     destroy)
861 {
862   static guint quit_id = 1;
863   GtkQuitFunction *quitf;
864   
865   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
866
867   if (!quit_mem_chunk)
868     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
869                                       512, G_ALLOC_AND_FREE);
870   
871   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
872   
873   quitf->id = quit_id++;
874   quitf->main_level = main_level;
875   quitf->function = function;
876   quitf->marshal = marshal;
877   quitf->data = data;
878   quitf->destroy = destroy;
879
880   quit_functions = g_list_prepend (quit_functions, quitf);
881   
882   return quitf->id;
883 }
884
885 guint
886 gtk_idle_add_full (gint                 priority,
887                    GtkFunction          function,
888                    GtkCallbackMarshal   marshal,
889                    gpointer             data,
890                    GtkDestroyNotify     destroy)
891 {
892   static guint idle_tag = 1;
893   GtkIdleFunction *idlef;
894   
895   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
896
897   if (!idle_mem_chunk)
898     idle_mem_chunk = g_mem_chunk_new ("idle mem chunk", sizeof (GtkIdleFunction),
899                                       1024, G_ALLOC_AND_FREE);
900   
901   idlef = g_chunk_new (GtkIdleFunction, idle_mem_chunk);
902   
903   idlef->tag = idle_tag++;
904   idlef->priority = priority;
905   idlef->function = function;
906   idlef->marshal = marshal;
907   idlef->data = data;
908   idlef->destroy = destroy;
909
910   idle_functions = g_list_insert_sorted (idle_functions, idlef, gtk_idle_compare);
911   
912   return idlef->tag;
913 }
914
915 guint
916 gtk_idle_add_interp  (GtkCallbackMarshal   marshal,
917                       gpointer             data,
918                       GtkDestroyNotify     destroy)
919 {
920   return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, NULL, marshal, data, destroy);
921 }
922
923 static void
924 gtk_idle_destroy (GtkIdleFunction *idlef)
925 {
926   if (idlef->destroy)
927     idlef->destroy (idlef->data);
928   g_mem_chunk_free (idle_mem_chunk, idlef);
929 }
930
931 static void
932 gtk_quit_destroy (GtkQuitFunction *quitf)
933 {
934   if (quitf->destroy)
935     quitf->destroy (quitf->data);
936   g_mem_chunk_free (quit_mem_chunk, quitf);
937 }
938
939 guint
940 gtk_quit_add (guint       main_level,
941               GtkFunction function,
942               gpointer    data)
943 {
944   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
945 }
946
947 guint
948 gtk_idle_add (GtkFunction function,
949               gpointer    data)
950 {
951   return gtk_idle_add_full (GTK_PRIORITY_DEFAULT, function, NULL, data, NULL);
952 }
953
954 guint       
955 gtk_idle_add_priority   (gint               priority,
956                          GtkFunction        function,
957                          gpointer           data)
958 {
959   return gtk_idle_add_full (priority, function, NULL, data, NULL);
960 }
961
962 void
963 gtk_quit_remove (guint id)
964 {
965   GtkQuitFunction *quitf;
966   GList *tmp_list;
967   
968   tmp_list = quit_functions;
969   while (tmp_list)
970     {
971       quitf = tmp_list->data;
972       
973       if (quitf->id == id)
974         {
975           quit_functions = g_list_remove_link (quit_functions, tmp_list);
976           g_list_free (tmp_list);
977           gtk_quit_destroy (quitf);
978           
979           return;
980         }
981       
982       tmp_list = tmp_list->next;
983     }
984 }
985
986 void
987 gtk_quit_remove_by_data (gpointer data)
988 {
989   GtkQuitFunction *quitf;
990   GList *tmp_list;
991   
992   tmp_list = quit_functions;
993   while (tmp_list)
994     {
995       quitf = tmp_list->data;
996       
997       if (quitf->data == data)
998         {
999           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1000           g_list_free (tmp_list);
1001           gtk_quit_destroy (quitf);
1002
1003           return;
1004         }
1005       
1006       tmp_list = tmp_list->next;
1007     }
1008 }
1009
1010 void
1011 gtk_idle_remove (guint tag)
1012 {
1013   GtkIdleFunction *idlef;
1014   GList *tmp_list;
1015   
1016   tmp_list = idle_functions;
1017   while (tmp_list)
1018     {
1019       idlef = tmp_list->data;
1020       
1021       if (idlef->tag == tag)
1022         {
1023           idle_functions = g_list_remove_link (idle_functions, tmp_list);
1024           g_list_free (tmp_list);
1025           gtk_idle_destroy (idlef);
1026           
1027           return;
1028         }
1029       
1030       tmp_list = tmp_list->next;
1031     }
1032   
1033   tmp_list = current_idles;
1034   while (tmp_list)
1035     {
1036       idlef = tmp_list->data;
1037       
1038       if (idlef->tag == tag)
1039         {
1040           current_idles = g_list_remove_link (current_idles, tmp_list);
1041           g_list_free (tmp_list);
1042           gtk_idle_destroy (idlef);
1043           
1044           return;
1045         }
1046       
1047       tmp_list = tmp_list->next;
1048     }
1049 }
1050
1051 void
1052 gtk_idle_remove_by_data (gpointer data)
1053 {
1054   GtkIdleFunction *idlef;
1055   GList *tmp_list;
1056   
1057   tmp_list = idle_functions;
1058   while (tmp_list)
1059     {
1060       idlef = tmp_list->data;
1061       
1062       if (idlef->data == data)
1063         {
1064           idle_functions = g_list_remove_link (idle_functions, tmp_list);
1065           g_list_free (tmp_list);
1066           gtk_idle_destroy (idlef);
1067           
1068           return;
1069         }
1070       
1071       tmp_list = tmp_list->next;
1072     }
1073   
1074   tmp_list = current_idles;
1075   while (tmp_list)
1076     {
1077       idlef = tmp_list->data;
1078       
1079       if (idlef->data == data)
1080         {
1081           current_idles = g_list_remove_link (current_idles, tmp_list);
1082           g_list_free (tmp_list);
1083           gtk_idle_destroy (idlef);
1084           
1085           return;
1086         }
1087       
1088       tmp_list = tmp_list->next;
1089     }
1090 }
1091
1092 static void
1093 gtk_invoke_input_function (GtkInputFunction *input,
1094                            gint source,
1095                            GdkInputCondition condition)
1096 {
1097   GtkArg args[3];
1098   args[0].type = GTK_TYPE_INT;
1099   args[0].name = NULL;
1100   GTK_VALUE_INT(args[0]) = source;
1101   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1102   args[1].name = NULL;
1103   GTK_VALUE_FLAGS(args[1]) = condition;
1104   args[2].type = GTK_TYPE_NONE;
1105   args[2].name = NULL;
1106
1107   input->callback (NULL, input->data, 2, args);
1108 }
1109
1110 static void
1111 gtk_destroy_input_function (GtkInputFunction *input)
1112 {
1113   if (input->destroy)
1114     (input->destroy) (input->data);
1115   g_free (input);
1116 }
1117
1118 guint
1119 gtk_input_add_full (gint source,
1120                     GdkInputCondition condition,
1121                     GdkInputFunction function,
1122                     GtkCallbackMarshal marshal,
1123                     gpointer data,
1124                     GtkDestroyNotify destroy)
1125 {
1126   if (marshal)
1127     {
1128       GtkInputFunction *input;
1129
1130       input = g_new (GtkInputFunction, 1);
1131       input->callback = marshal;
1132       input->data = data;
1133       input->destroy = destroy;
1134
1135       return gdk_input_add_full (source,
1136                                  condition,
1137                                  (GdkInputFunction) gtk_invoke_input_function,
1138                                  input,
1139                                  (GdkDestroyNotify) gtk_destroy_input_function);
1140     }
1141   else
1142     return gdk_input_add_full (source, condition, function, data, destroy);
1143 }
1144
1145 guint
1146 gtk_input_add_interp (gint source,
1147                       GdkInputCondition condition,
1148                       GtkCallbackMarshal callback,
1149                       gpointer data,
1150                       GtkDestroyNotify destroy)
1151 {
1152   return gtk_input_add_full (source, condition, NULL, callback, data, destroy);
1153 }
1154
1155 void
1156 gtk_input_remove (guint tag)
1157 {
1158   gdk_input_remove (tag);
1159 }
1160
1161 GdkEvent *
1162 gtk_get_current_event ()
1163 {
1164   if (current_events)
1165     return gdk_event_copy ((GdkEvent *) current_events->data);
1166   else
1167     return NULL;
1168 }
1169
1170 GtkWidget*
1171 gtk_get_event_widget (GdkEvent *event)
1172 {
1173   GtkWidget *widget;
1174
1175   widget = NULL;
1176   if (event->any.window)
1177     gdk_window_get_user_data (event->any.window, (void**) &widget);
1178   
1179   return widget;
1180 }
1181
1182 static void
1183 gtk_exit_func ()
1184 {
1185   if (initialized)
1186     {
1187       initialized = FALSE;
1188       gtk_preview_uninit ();
1189     }
1190 }
1191
1192
1193 /* We rely on some knowledge of how g_list_insert_sorted works to make
1194  * sure that we insert after timeouts of equal interval
1195  */
1196 static gint
1197 gtk_timeout_compare (gpointer a, gpointer b)
1198 {
1199   return (((GtkTimeoutFunction *)a)->interval < 
1200           ((GtkTimeoutFunction *)b)->interval)
1201     ? -1 : 1;
1202 }
1203
1204 static void
1205 gtk_timeout_insert (GtkTimeoutFunction *timeoutf)
1206 {
1207   g_assert (timeoutf != NULL);
1208   
1209   /* Insert the timeout function appropriately.
1210    * Appropriately meaning sort it into the list
1211    *  of timeout functions.
1212    */
1213   timeout_functions = g_list_insert_sorted (timeout_functions, timeoutf,
1214                                             gtk_timeout_compare);
1215 }
1216
1217 static gint
1218 gtk_invoke_timeout_function (GtkTimeoutFunction *timeoutf)
1219 {
1220   if (!timeoutf->marshal)
1221     return timeoutf->function (timeoutf->data);
1222   else
1223     {
1224       GtkArg args[1];
1225       gint ret_val = FALSE;
1226       args[0].name = NULL;
1227       args[0].type = GTK_TYPE_BOOL;
1228       args[0].d.pointer_data = &ret_val;
1229       ((GtkCallbackMarshal)timeoutf->function) (NULL,
1230                                                 timeoutf->data,
1231                                                 0, args);
1232       return ret_val;
1233     }
1234 }
1235
1236 static void
1237 gtk_handle_current_timeouts (guint32 the_time)
1238 {
1239   GList *tmp_list;
1240   GtkTimeoutFunction *timeoutf;
1241   
1242   while (current_timeouts)
1243     {
1244       tmp_list = current_timeouts;
1245       timeoutf = tmp_list->data;
1246       
1247       current_timeouts = g_list_remove_link (current_timeouts, tmp_list);
1248       g_list_free (tmp_list);
1249       
1250       if (gtk_invoke_timeout_function (timeoutf) == FALSE)
1251         {
1252           gtk_timeout_destroy (timeoutf);
1253         }
1254       else
1255         {
1256           timeoutf->interval = timeoutf->originterval;
1257           timeoutf->start = the_time;
1258           gtk_timeout_insert (timeoutf);
1259         }
1260     }
1261 }
1262
1263 static void
1264 gtk_handle_timeouts ()
1265 {
1266   guint32 the_time;
1267   GList *tmp_list;
1268   GList *tmp_list2;
1269   GtkTimeoutFunction *timeoutf;
1270   
1271   /* Caller must already have called gtk_handle_current_timeouts if
1272    * necessary */
1273   g_assert (current_timeouts == NULL);
1274   
1275   if (timeout_functions)
1276     {
1277       the_time = gdk_time_get ();
1278       
1279       tmp_list = timeout_functions;
1280       while (tmp_list)
1281         {
1282           timeoutf = tmp_list->data;
1283           
1284           if (timeoutf->interval <= (the_time - timeoutf->start))
1285             {
1286               tmp_list2 = tmp_list;
1287               tmp_list = tmp_list->next;
1288               
1289               timeout_functions = g_list_remove_link (timeout_functions, tmp_list2);
1290               current_timeouts = g_list_concat (current_timeouts, tmp_list2);
1291             }
1292           else
1293             {
1294               timeoutf->interval -= (the_time - timeoutf->start);
1295               timeoutf->start = the_time;
1296               tmp_list = tmp_list->next;
1297             }
1298         }
1299       
1300       if (current_timeouts)
1301         gtk_handle_current_timeouts(the_time);
1302     }
1303 }
1304
1305 static gint
1306 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1307 {
1308   if (!quitf->marshal)
1309     return quitf->function (quitf->data);
1310   else
1311     {
1312       GtkArg args[1];
1313       gint ret_val = FALSE;
1314
1315       args[0].name = NULL;
1316       args[0].type = GTK_TYPE_BOOL;
1317       args[0].d.pointer_data = &ret_val;
1318       ((GtkCallbackMarshal) quitf->marshal) (NULL,
1319                                              quitf->data,
1320                                              0, args);
1321       return ret_val;
1322     }
1323 }
1324
1325 static gint
1326 gtk_idle_invoke_function (GtkIdleFunction *idlef)
1327 {
1328   if (!idlef->marshal)
1329     return idlef->function (idlef->data);
1330   else
1331     {
1332       GtkArg args[1];
1333       gint ret_val = FALSE;
1334
1335       args[0].name = NULL;
1336       args[0].type = GTK_TYPE_BOOL;
1337       args[0].d.pointer_data = &ret_val;
1338       ((GtkCallbackMarshal) idlef->marshal) (NULL,
1339                                              idlef->data,
1340                                              0, args);
1341       return ret_val;
1342     }
1343 }
1344
1345 static void
1346 gtk_handle_current_idles ()
1347 {
1348   GList *tmp_list;
1349   GList *tmp_list2;
1350   GtkIdleFunction *idlef;
1351   
1352   while (current_idles)
1353     {
1354       tmp_list = current_idles;
1355       idlef = tmp_list->data;
1356       
1357       current_idles = g_list_remove_link (current_idles, tmp_list);
1358       
1359       if (gtk_idle_invoke_function (idlef) == FALSE)
1360         {
1361           g_list_free (tmp_list);
1362           gtk_idle_destroy (idlef);
1363         }
1364       else
1365         {
1366           /* Insert the idle function back into the list of idle
1367            * functions at the end of the idles of this priority
1368            */
1369           tmp_list2 = idle_functions;
1370           while (tmp_list2 &&
1371                  (((GtkIdleFunction *)tmp_list2->data)->priority <= idlef->priority))
1372             tmp_list2 = tmp_list2->next;
1373
1374           if (!tmp_list2)
1375             idle_functions = g_list_concat (idle_functions, tmp_list);
1376           else if (tmp_list2 == idle_functions)
1377             {
1378               tmp_list->next = idle_functions;
1379               if (idle_functions)
1380                 idle_functions->prev = tmp_list;
1381               idle_functions = tmp_list;
1382             }
1383           else
1384             {
1385               tmp_list->prev = tmp_list2->prev;
1386               tmp_list->next = tmp_list2;
1387               tmp_list2->prev->next = tmp_list;
1388               tmp_list2->prev = tmp_list;
1389             }
1390         }
1391     }
1392 }
1393
1394 static void
1395 gtk_handle_idle ()
1396 {
1397   /* Caller must already have called gtk_handle_current_idles if
1398    * necessary
1399    */
1400   g_assert (current_idles == NULL);
1401   
1402   /* Handle only the idle functions that have the highest priority */
1403   if (idle_functions)
1404     {
1405       GList *tmp_list;
1406       gint top_priority;
1407
1408       tmp_list = idle_functions;
1409       top_priority = ((GtkIdleFunction *)tmp_list->data)->priority;
1410  
1411       while (tmp_list &&
1412              (((GtkIdleFunction *)tmp_list->data)->priority == top_priority))
1413         tmp_list = tmp_list->next;
1414
1415       current_idles = idle_functions;
1416       idle_functions = tmp_list;
1417
1418       if (tmp_list) 
1419         {
1420           tmp_list->prev->next = NULL;
1421           tmp_list->prev = NULL;
1422         }
1423       
1424       gtk_handle_current_idles();
1425     }
1426 }
1427
1428 static void
1429 gtk_handle_timer ()
1430 {
1431   GtkTimeoutFunction *timeoutf;
1432   
1433   if (idle_functions)
1434     {
1435       gdk_timer_set (0);
1436       gdk_timer_enable ();
1437     }
1438   else if (timeout_functions)
1439     {
1440       timeoutf = timeout_functions->data;
1441       gdk_timer_set (timeoutf->interval);
1442       gdk_timer_enable ();
1443     }
1444   else
1445     {
1446       gdk_timer_set (0);
1447       gdk_timer_disable ();
1448     }
1449 }
1450
1451 static void
1452 gtk_propagate_event (GtkWidget *widget,
1453                      GdkEvent  *event)
1454 {
1455   GtkWidget *parent;
1456   GtkWidget *tmp;
1457   gint handled_event;
1458   
1459   g_return_if_fail (widget != NULL);
1460   g_return_if_fail (event != NULL);
1461   
1462   handled_event = FALSE;
1463   gtk_widget_ref (widget);
1464
1465   if ((event->type == GDK_KEY_PRESS) ||
1466       (event->type == GDK_KEY_RELEASE))
1467     {
1468
1469       /* Only send key events to window widgets.
1470        *  The window widget will in turn pass the
1471        *  key event on to the currently focused widget
1472        *  for that window.
1473        */
1474       parent = gtk_widget_get_ancestor (widget, gtk_window_get_type ());
1475       handled_event = (parent &&
1476                        GTK_WIDGET_IS_SENSITIVE (parent) &&
1477                        gtk_widget_event (parent, event));
1478     }
1479   
1480   /* Other events get propagated up the widget tree
1481    *  so that parents can see the button and motion
1482    *  events of the children.
1483    */
1484   tmp = widget;
1485   while (!handled_event && tmp)
1486     {
1487       gtk_widget_ref (tmp);
1488       handled_event = !GTK_WIDGET_IS_SENSITIVE (tmp) || gtk_widget_event (tmp, event);
1489       parent = tmp->parent;
1490       gtk_widget_unref (tmp);
1491       tmp = parent;
1492     }
1493
1494   gtk_widget_unref (widget);
1495 }
1496
1497
1498 static void
1499 gtk_error (gchar *str)
1500 {
1501   gtk_print (str);
1502 }
1503
1504 static void
1505 gtk_warning (gchar *str)
1506 {
1507   gtk_print (str);
1508 }
1509
1510 static void
1511 gtk_message (gchar *str)
1512 {
1513   gtk_print (str);
1514 }
1515
1516 static void
1517 gtk_print (gchar *str)
1518 {
1519   static GtkWidget *window = NULL;
1520   static GtkWidget *text;
1521   static int level = 0;
1522   GtkWidget *box1;
1523   GtkWidget *box2;
1524   GtkWidget *table;
1525   GtkWidget *hscrollbar;
1526   GtkWidget *vscrollbar;
1527   GtkWidget *separator;
1528   GtkWidget *button;
1529   
1530   if (level > 0)
1531     {
1532       fputs (str, stdout);
1533       fflush (stdout);
1534       return;
1535     }
1536   
1537   if (!window)
1538     {
1539       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1540       
1541       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1542                           (GtkSignalFunc) gtk_widget_destroyed,
1543                           &window);
1544       
1545       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1546       
1547       box1 = gtk_vbox_new (FALSE, 0);
1548       gtk_container_add (GTK_CONTAINER (window), box1);
1549       gtk_widget_show (box1);
1550       
1551       
1552       box2 = gtk_vbox_new (FALSE, 10);
1553       gtk_container_border_width (GTK_CONTAINER (box2), 10);
1554       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1555       gtk_widget_show (box2);
1556       
1557       
1558       table = gtk_table_new (2, 2, FALSE);
1559       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1560       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1561       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1562       gtk_widget_show (table);
1563       
1564       text = gtk_text_new (NULL, NULL);
1565       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1566       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1567       gtk_widget_show (text);
1568       gtk_widget_realize (text);
1569       
1570       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1571       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1572                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1573       gtk_widget_show (hscrollbar);
1574       
1575       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1576       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1577                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1578       gtk_widget_show (vscrollbar);
1579       
1580       separator = gtk_hseparator_new ();
1581       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1582       gtk_widget_show (separator);
1583       
1584       
1585       box2 = gtk_vbox_new (FALSE, 10);
1586       gtk_container_border_width (GTK_CONTAINER (box2), 10);
1587       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1588       gtk_widget_show (box2);
1589       
1590       
1591       button = gtk_button_new_with_label ("close");
1592       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1593                                  (GtkSignalFunc) gtk_widget_hide,
1594                                  GTK_OBJECT (window));
1595       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1596       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1597       gtk_widget_grab_default (button);
1598       gtk_widget_show (button);
1599     }
1600   
1601   level += 1;
1602   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1603   level -= 1;
1604   
1605   if (!GTK_WIDGET_VISIBLE (window))
1606     gtk_widget_show (window);
1607 }