]> Pileus Git - ~andy/gtk/blob - gtk/gtkmain.c
gdk/gdkimage.h No need any longer on Win32 for the shared memory
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #ifdef GDK_WINDOWING_X11
28 #include <X11/Xlocale.h>        /* so we get the right setlocale */
29 #else
30 #include <locale.h>
31 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <gmodule.h>
36 #include "gtkbutton.h"
37 #include "gtkdnd.h"
38 #include "gtkcompat.h"
39 #include "gtkhscrollbar.h"
40 #include "gtkhseparator.h"
41 #include "gtkmain.h"
42 #include "gtkpreview.h"
43 #include "gtkrc.h"
44 #include "gtkscrolledwindow.h"
45 #include "gtkselection.h"
46 #include "gtksignal.h"
47 #include "gtktable.h"
48 #include "gtktext.h"
49 #include "gtkvbox.h"
50 #include "gtkvscrollbar.h"
51 #include "gtkwidget.h"
52 #include "gtkwindow.h"
53 #include "gtkprivate.h"
54 #include "gdk/gdki18n.h"
55 #include "config.h"
56 #include "gtkdebug.h"
57 #include "gtkintl.h"
58
59 /* Private type definitions
60  */
61 typedef struct _GtkInitFunction          GtkInitFunction;
62 typedef struct _GtkQuitFunction          GtkQuitFunction;
63 typedef struct _GtkClosure               GtkClosure;
64 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
65
66 struct _GtkInitFunction
67 {
68   GtkFunction function;
69   gpointer data;
70 };
71
72 struct _GtkQuitFunction
73 {
74   guint id;
75   guint main_level;
76   GtkCallbackMarshal marshal;
77   GtkFunction function;
78   gpointer data;
79   GtkDestroyNotify destroy;
80 };
81
82 struct _GtkClosure
83 {
84   GtkCallbackMarshal marshal;
85   gpointer data;
86   GtkDestroyNotify destroy;
87 };
88
89 struct _GtkKeySnooperData
90 {
91   GtkKeySnoopFunc func;
92   gpointer func_data;
93   guint id;
94 };
95
96 static void  gtk_exit_func               (void);
97 static gint  gtk_quit_invoke_function    (GtkQuitFunction    *quitf);
98 static void  gtk_quit_destroy            (GtkQuitFunction    *quitf);
99 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
100                                           GdkEvent           *event);
101
102 static void     gtk_destroy_closure      (gpointer            data);
103 static gboolean gtk_invoke_idle_timeout  (gpointer            data);
104 static void     gtk_invoke_input         (gpointer            data,
105                                           gint                source,
106                                           GdkInputCondition   condition);
107
108 #if 0
109 static void  gtk_error                   (gchar              *str);
110 static void  gtk_warning                 (gchar              *str);
111 static void  gtk_message                 (gchar              *str);
112 static void  gtk_print                   (gchar              *str);
113 #endif
114
115 const guint gtk_major_version = GTK_MAJOR_VERSION;
116 const guint gtk_minor_version = GTK_MINOR_VERSION;
117 const guint gtk_micro_version = GTK_MICRO_VERSION;
118 const guint gtk_binary_age = GTK_BINARY_AGE;
119 const guint gtk_interface_age = GTK_INTERFACE_AGE;
120
121 static guint gtk_main_loop_level = 0;
122 static gint gtk_initialized = FALSE;
123 static GList *current_events = NULL;
124
125 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
126
127 static GSList *grabs = NULL;               /* A stack of unique grabs. The grabbing
128                                             *  widget is the first one on the list.
129                                             */
130 static GList *init_functions = NULL;       /* A list of init functions.
131                                             */
132 static GList *quit_functions = NULL;       /* A list of quit functions.
133                                             */
134 static GMemChunk *quit_mem_chunk = NULL;
135
136 static GSList *key_snoopers = NULL;
137
138 static GdkVisual *gtk_visual;              /* The visual to be used in creating new
139                                             *  widgets.
140                                             */
141 static GdkColormap *gtk_colormap;          /* The colormap to be used in creating new
142                                             *  widgets.
143                                             */
144
145 guint gtk_debug_flags = 0;                 /* Global GTK debug flag */
146
147 #ifdef G_ENABLE_DEBUG
148 static const GDebugKey gtk_debug_keys[] = {
149   {"objects", GTK_DEBUG_OBJECTS},
150   {"misc", GTK_DEBUG_MISC},
151   {"signals", GTK_DEBUG_SIGNALS},
152   {"dnd", GTK_DEBUG_DND},
153   {"plugsocket", GTK_DEBUG_PLUGSOCKET},
154   {"text", GTK_DEBUG_TEXT}
155 };
156
157 static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
158
159 #endif /* G_ENABLE_DEBUG */
160
161 gchar*
162 gtk_check_version (guint required_major,
163                    guint required_minor,
164                    guint required_micro)
165 {
166   if (required_major > GTK_MAJOR_VERSION)
167     return "Gtk+ version too old (major mismatch)";
168   if (required_major < GTK_MAJOR_VERSION)
169     return "Gtk+ version too new (major mismatch)";
170   if (required_minor > GTK_MINOR_VERSION)
171     return "Gtk+ version too old (minor mismatch)";
172   if (required_minor < GTK_MINOR_VERSION)
173     return "Gtk+ version too new (minor mismatch)";
174   if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
175     return "Gtk+ version too new (micro mismatch)";
176   if (required_micro > GTK_MICRO_VERSION)
177     return "Gtk+ version too old (micro mismatch)";
178   return NULL;
179 }
180
181 #ifdef __EMX__
182 static gchar *add_dll_suffix(gchar *module_name)
183 {
184     gchar *suffix = strrchr(module_name, '.');
185     
186     if (!suffix || stricmp(suffix, ".dll"))
187     {
188         gchar *old = module_name;
189           
190         module_name = g_strconcat (module_name, ".dll", NULL);
191         g_free (old);
192     }
193     return (module_name);
194 }
195 #endif
196
197 #undef gtk_init_check
198
199 gboolean
200 gtk_init_check (int      *argc,
201                 char   ***argv)
202 {
203   GSList *gtk_modules = NULL;
204   GSList *slist;
205   gchar *env_string = NULL;
206
207   if (gtk_initialized)
208     return TRUE;
209
210 #if     0
211   g_set_error_handler (gtk_error);
212   g_set_warning_handler (gtk_warning);
213   g_set_message_handler (gtk_message);
214   g_set_print_handler (gtk_print);
215 #endif
216   
217   /* Initialize "gdk". We pass along the 'argc' and 'argv'
218    *  parameters as they contain information that GDK uses
219    */
220   if (!gdk_init_check (argc, argv))
221     return FALSE;
222
223   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
224   
225 #ifdef G_ENABLE_DEBUG
226   env_string = getenv ("GTK_DEBUG");
227   if (env_string != NULL)
228     {
229       gtk_debug_flags = g_parse_debug_string (env_string,
230                                               gtk_debug_keys,
231                                               gtk_ndebug_keys);
232       env_string = NULL;
233     }
234 #endif  /* G_ENABLE_DEBUG */
235
236   env_string = getenv ("GTK_MODULES");
237   if (env_string)
238     {
239       gchar **modules, **as;
240
241 #ifndef __EMX__
242       modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
243 #else
244       modules = g_strsplit (env_string, ";", -1);
245 #endif
246       for (as = modules; *as; as++)
247         {
248           if (**as)
249             gtk_modules = g_slist_prepend (gtk_modules, *as);
250           else
251             g_free (*as);
252         }
253       g_free (modules);
254       env_string = NULL;
255     }
256
257   if (argc && argv)
258     {
259       gint i, j, k;
260       
261       for (i = 1; i < *argc;)
262         {
263           if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
264               strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
265             {
266               gchar *module_name = (*argv)[i] + 12;
267               
268               if (*module_name == '=')
269                 module_name++;
270               else if (i + 1 < *argc)
271                 {
272                   (*argv)[i] = NULL;
273                   i += 1;
274                   module_name = (*argv)[i];
275                 }
276               (*argv)[i] = NULL;
277
278               if (module_name && *module_name)
279                 gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
280             }
281           else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
282             {
283               GLogLevelFlags fatal_mask;
284               
285               fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
286               fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
287               g_log_set_always_fatal (fatal_mask);
288               (*argv)[i] = NULL;
289             }
290 #ifdef G_ENABLE_DEBUG
291           else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
292                    (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
293             {
294               gchar *equal_pos = strchr ((*argv)[i], '=');
295               
296               if (equal_pos != NULL)
297                 {
298                   gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
299                                                            gtk_debug_keys,
300                                                            gtk_ndebug_keys);
301                 }
302               else if ((i + 1) < *argc && (*argv)[i + 1])
303                 {
304                   gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
305                                                            gtk_debug_keys,
306                                                            gtk_ndebug_keys);
307                   (*argv)[i] = NULL;
308                   i += 1;
309                 }
310               (*argv)[i] = NULL;
311             }
312           else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
313                    (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
314             {
315               gchar *equal_pos = strchr ((*argv)[i], '=');
316               
317               if (equal_pos != NULL)
318                 {
319                   gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
320                                                             gtk_debug_keys,
321                                                             gtk_ndebug_keys);
322                 }
323               else if ((i + 1) < *argc && (*argv)[i + 1])
324                 {
325                   gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
326                                                             gtk_debug_keys,
327                                                             gtk_ndebug_keys);
328                   (*argv)[i] = NULL;
329                   i += 1;
330                 }
331               (*argv)[i] = NULL;
332             }
333 #endif /* G_ENABLE_DEBUG */
334           i += 1;
335         }
336       
337       for (i = 1; i < *argc; i++)
338         {
339           for (k = i; k < *argc; k++)
340             if ((*argv)[k] != NULL)
341               break;
342           
343           if (k > i)
344             {
345               k -= i;
346               for (j = i + k; j < *argc; j++)
347                 (*argv)[j-k] = (*argv)[j];
348               *argc -= k;
349             }
350         }
351     }
352   
353   /* load gtk modules */
354   gtk_modules = g_slist_reverse (gtk_modules);
355   for (slist = gtk_modules; slist; slist = slist->next)
356     {
357       gchar *module_name;
358       GModule *module = NULL;
359       GtkModuleInitFunc modinit_func = NULL;
360       
361       module_name = slist->data;
362       slist->data = NULL;
363 #ifndef __EMX__
364       if (!g_path_is_absolute (module_name))
365         {
366           gchar *old = module_name;
367           
368           module_name = g_module_build_path (NULL, module_name);
369           g_free (old);
370         }
371 #else
372       module_name = add_dll_suffix(module_name);
373 #endif
374       if (g_module_supported ())
375         {
376           module = g_module_open (module_name, G_MODULE_BIND_LAZY);
377           if (module &&
378               g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
379               modinit_func)
380             {
381               if (!g_slist_find (gtk_modules, modinit_func))
382                 {
383                   g_module_make_resident (module);
384                   slist->data = modinit_func;
385                 }
386               else
387                 {
388                   g_module_close (module);
389                   module = NULL;
390                 }
391             }
392         }
393       if (!modinit_func)
394         {
395           g_warning ("Failed to load module \"%s\": %s",
396                      module ? g_module_name (module) : module_name,
397                      g_module_error ());
398           if (module)
399             g_module_close (module);
400         }
401       g_free (module_name);
402     }
403
404 #ifdef ENABLE_NLS
405 #ifndef G_OS_WIN32
406   bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
407 #else
408   {
409     /* GTk+ locale dir is %WinDir%\gtk+\locale */
410     bindtextdomain (GETTEXT_PACKAGE,
411                     g_strconcat (gtk_win32_get_installation_directory (),
412                                  G_DIR_SEPARATOR_S,
413                                  "locale",
414                                  NULL));
415   }
416 #endif
417 #endif  
418
419   /* Initialize the default visual and colormap to be
420    *  used in creating widgets. (We want to use the system
421    *  defaults so as to be nice to the colormap).
422    */
423   gtk_visual = gdk_visual_get_system ();
424   gtk_colormap = gdk_colormap_get_system ();
425
426   gtk_type_init ();
427   gtk_signal_init ();
428   gtk_rc_init ();
429   
430   
431   /* Register an exit function to make sure we are able to cleanup.
432    */
433   g_atexit (gtk_exit_func);
434   
435   /* Set the 'initialized' flag.
436    */
437   gtk_initialized = TRUE;
438
439   /* initialize gtk modules
440    */
441   for (slist = gtk_modules; slist; slist = slist->next)
442     {
443       if (slist->data)
444         {
445           GtkModuleInitFunc modinit;
446           
447           modinit = slist->data;
448           modinit (argc, argv);
449         }
450     }
451   g_slist_free (gtk_modules);
452   
453 #ifndef G_OS_WIN32
454   /* No use warning on Win32, there aren't any non-devel versions anyhow... */
455   g_message (""              "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
456              "                UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
457              "                if you don't know why you are getting this, you probably want to\n"
458              "                use the stable branch which can be retrived from\n"
459              "                ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
460              "                cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
461 #endif
462
463   return TRUE;
464 }
465
466 #undef gtk_init
467
468 void
469 gtk_init (int *argc, char ***argv)
470 {
471   if (!gtk_init_check (argc, argv))
472     {
473       g_warning ("cannot open display: %s", gdk_get_display ());
474       exit(1);
475     }
476 }
477
478 #ifdef G_OS_WIN32
479
480 static void
481 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
482 {
483   if (sizeof_GtkWindow != sizeof (GtkWindow))
484     g_error ("Incompatible build!\n"
485              "The code using GTK+ thinks GtkWindow is of different\n"
486              "size than it actually is in this build of GTK+.\n"
487              "On Windows, this probably means that you have compiled\n"
488              "your code with gcc without the -fnative-struct switch.");
489 }
490
491 /* These two functions might get more checks added later, thus pass
492  * in the number of extra args.
493  */
494 void
495 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
496 {
497   check_sizeof_GtkWindow (sizeof_GtkWindow);
498   gtk_init (argc, argv);
499 }
500
501 gboolean
502 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
503 {
504   check_sizeof_GtkWindow (sizeof_GtkWindow);
505   return gtk_init_check (argc, argv);
506 }
507
508 #endif
509
510 void
511 gtk_exit (gint errorcode)
512 {
513   /* Only if "gtk" has been initialized should we de-initialize.
514    */
515   /* de-initialisation is done by the gtk_exit_funct(),
516    * no need to do this here (Alex J.)
517    */
518   gdk_exit(errorcode);
519 }
520
521 gchar*
522 gtk_set_locale (void)
523 {
524   return gdk_set_locale ();
525 }
526
527 gchar*
528 gtk_get_default_language (void)
529 {
530   gchar *lang;
531   gchar *p;
532   
533   lang = g_strdup (setlocale (LC_CTYPE, NULL));
534   p = strchr (lang, '.');
535   if (p)
536     *p = '\0';
537   p = strchr (lang, '@');
538   if (p)
539     *p = '\0';
540
541   return lang;
542 }
543
544 void
545 gtk_main (void)
546 {
547   GList *tmp_list;
548   GList *functions;
549   GtkInitFunction *init;
550   GMainLoop *loop;
551
552   gtk_main_loop_level++;
553   
554   loop = g_main_new (TRUE);
555   main_loops = g_slist_prepend (main_loops, loop);
556
557   tmp_list = functions = init_functions;
558   init_functions = NULL;
559   
560   while (tmp_list)
561     {
562       init = tmp_list->data;
563       tmp_list = tmp_list->next;
564       
565       (* init->function) (init->data);
566       g_free (init);
567     }
568   g_list_free (functions);
569
570   if (g_main_is_running (main_loops->data))
571     {
572       GDK_THREADS_LEAVE ();
573       g_main_run (loop);
574       GDK_THREADS_ENTER ();
575       gdk_flush ();
576     }
577
578   if (quit_functions)
579     {
580       GList *reinvoke_list = NULL;
581       GtkQuitFunction *quitf;
582
583       while (quit_functions)
584         {
585           quitf = quit_functions->data;
586
587           tmp_list = quit_functions;
588           quit_functions = g_list_remove_link (quit_functions, quit_functions);
589           g_list_free_1 (tmp_list);
590
591           if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
592               gtk_quit_invoke_function (quitf))
593             {
594               reinvoke_list = g_list_prepend (reinvoke_list, quitf);
595             }
596           else
597             {
598               gtk_quit_destroy (quitf);
599             }
600         }
601       if (reinvoke_list)
602         {
603           GList *work;
604           
605           work = g_list_last (reinvoke_list);
606           if (quit_functions)
607             quit_functions->prev = work;
608           work->next = quit_functions;
609           quit_functions = work;
610         }
611
612       gdk_flush ();
613     }
614               
615   main_loops = g_slist_remove (main_loops, loop);
616
617   g_main_destroy (loop);
618
619   gtk_main_loop_level--;
620 }
621
622 guint
623 gtk_main_level (void)
624 {
625   return gtk_main_loop_level;
626 }
627
628 void
629 gtk_main_quit (void)
630 {
631   g_return_if_fail (main_loops != NULL);
632
633   g_main_quit (main_loops->data);
634 }
635
636 gint
637 gtk_events_pending (void)
638 {
639   gboolean result;
640   
641   GDK_THREADS_LEAVE ();  
642   result = g_main_pending();
643   GDK_THREADS_ENTER ();
644
645   return result;
646 }
647
648 gint 
649 gtk_main_iteration (void)
650 {
651   GDK_THREADS_LEAVE ();
652   g_main_iteration (TRUE);
653   GDK_THREADS_ENTER ();
654
655   if (main_loops)
656     return !g_main_is_running (main_loops->data);
657   else
658     return TRUE;
659 }
660
661 gint 
662 gtk_main_iteration_do (gboolean blocking)
663 {
664   GDK_THREADS_LEAVE ();
665   g_main_iteration (blocking);
666   GDK_THREADS_ENTER ();
667
668   if (main_loops)
669     return !g_main_is_running (main_loops->data);
670   else
671     return TRUE;
672 }
673
674 void 
675 gtk_main_do_event (GdkEvent *event)
676 {
677   GtkWidget *event_widget;
678   GtkWidget *grab_widget;
679   GdkEvent *next_event;
680   GList *tmp_list;
681
682   /* If there are any events pending then get the next one.
683    */
684   next_event = gdk_event_peek ();
685   
686   /* Try to compress enter/leave notify events. These event
687    *  pairs occur when the mouse is dragged quickly across
688    *  a window with many buttons (or through a menu). Instead
689    *  of highlighting and de-highlighting each widget that
690    *  is crossed it is better to simply de-highlight the widget
691    *  which contained the mouse initially and highlight the
692    *  widget which ends up containing the mouse.
693    */
694   if (next_event)
695     if (((event->type == GDK_ENTER_NOTIFY) ||
696          (event->type == GDK_LEAVE_NOTIFY)) &&
697         ((next_event->type == GDK_ENTER_NOTIFY) ||
698          (next_event->type == GDK_LEAVE_NOTIFY)) &&
699         (next_event->type != event->type) &&
700         (next_event->any.window == event->any.window))
701       {
702         /* Throw both the peeked copy and the queued copy away 
703          */
704         gdk_event_free (next_event);
705         next_event = gdk_event_get ();
706         gdk_event_free (next_event);
707         
708         return;
709       }
710
711   if (next_event)
712     gdk_event_free (next_event);
713
714   /* Find the widget which got the event. We store the widget
715    *  in the user_data field of GdkWindow's.
716    *  Ignore the event if we don't have a widget for it, except
717    *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
718    *  Though this happens rarely, bogus events can occour
719    *  for e.g. destroyed GdkWindows. 
720    */
721   event_widget = gtk_get_event_widget (event);
722   if (!event_widget)
723     {
724       /* To handle selection INCR transactions, we select
725        * PropertyNotify events on the requestor window and create
726        * a corresponding (fake) GdkWindow so that events get
727        * here. There won't be a widget though, so we have to handle
728            * them specially
729            */
730       if (event->type == GDK_PROPERTY_NOTIFY)
731         gtk_selection_incr_event (event->any.window,
732                                   &event->property);
733       
734       return;
735     }
736   
737   /* Push the event onto a stack of current events for
738    * gtk_current_event_get().
739    */
740   current_events = g_list_prepend (current_events, event);
741   
742   /* If there is a grab in effect...
743    */
744   if (grabs)
745     {
746       grab_widget = grabs->data;
747       
748       /* If the grab widget is an ancestor of the event widget
749        *  then we send the event to the original event widget.
750        *  This is the key to implementing modality.
751        */
752       if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
753           gtk_widget_is_ancestor (event_widget, grab_widget))
754         grab_widget = event_widget;
755     }
756   else
757     {
758       grab_widget = event_widget;
759     }
760
761   /* Not all events get sent to the grabbing widget.
762    * The delete, destroy, expose, focus change and resize
763    *  events still get sent to the event widget because
764    *  1) these events have no meaning for the grabbing widget
765    *  and 2) redirecting these events to the grabbing widget
766    *  could cause the display to be messed up.
767    * 
768    * Drag events are also not redirected, since it isn't
769    *  clear what the semantics of that would be.
770    */
771   switch (event->type)
772     {
773     case GDK_NOTHING:
774       break;
775       
776     case GDK_DELETE:
777       gtk_widget_ref (event_widget);
778       if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
779           !gtk_widget_event (event_widget, event))
780         gtk_widget_destroy (event_widget);
781       gtk_widget_unref (event_widget);
782       break;
783       
784     case GDK_DESTROY:
785       /* Unexpected GDK_DESTROY from the outside, ignore for
786        * child windows, handle like a GDK_DELETE for toplevels
787        */
788       if (!event_widget->parent)
789         {
790           gtk_widget_ref (event_widget);
791           if (!gtk_widget_event (event_widget, event) &&
792               !GTK_OBJECT_DESTROYED (event_widget))
793             gtk_widget_destroy (event_widget);
794           gtk_widget_unref (event_widget);
795         }
796       break;
797       
798     case GDK_EXPOSE:
799       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
800         gdk_window_begin_paint_rect (event->any.window, &event->expose.area);
801
802       gtk_widget_event (event_widget, event);
803
804       if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
805         gdk_window_end_paint (event->any.window);
806       break;
807
808     case GDK_PROPERTY_NOTIFY:
809     case GDK_NO_EXPOSE:
810     case GDK_FOCUS_CHANGE:
811     case GDK_CONFIGURE:
812     case GDK_MAP:
813     case GDK_UNMAP:
814     case GDK_SELECTION_CLEAR:
815     case GDK_SELECTION_REQUEST:
816     case GDK_SELECTION_NOTIFY:
817     case GDK_CLIENT_EVENT:
818     case GDK_VISIBILITY_NOTIFY:
819       gtk_widget_event (event_widget, event);
820       break;
821
822     case GDK_SCROLL:
823     case GDK_BUTTON_PRESS:
824     case GDK_2BUTTON_PRESS:
825     case GDK_3BUTTON_PRESS:
826       gtk_propagate_event (grab_widget, event);
827       break;
828
829     case GDK_KEY_PRESS:
830     case GDK_KEY_RELEASE:
831       if (key_snoopers)
832         {
833           if (gtk_invoke_key_snoopers (grab_widget, event))
834             break;
835         }
836       /* else fall through */
837     case GDK_MOTION_NOTIFY:
838     case GDK_BUTTON_RELEASE:
839     case GDK_PROXIMITY_IN:
840     case GDK_PROXIMITY_OUT:
841       gtk_propagate_event (grab_widget, event);
842       break;
843       
844     case GDK_ENTER_NOTIFY:
845       if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
846         {
847           gtk_widget_event (grab_widget, event);
848           if (event_widget == grab_widget)
849             GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
850         }
851       break;
852       
853     case GDK_LEAVE_NOTIFY:
854       if (GTK_WIDGET_LEAVE_PENDING (event_widget))
855         {
856           GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
857           gtk_widget_event (event_widget, event);
858         }
859       else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
860         gtk_widget_event (grab_widget, event);
861       break;
862       
863     case GDK_DRAG_STATUS:
864     case GDK_DROP_FINISHED:
865       gtk_drag_source_handle_event (event_widget, event);
866       break;
867     case GDK_DRAG_ENTER:
868     case GDK_DRAG_LEAVE:
869     case GDK_DRAG_MOTION:
870     case GDK_DROP_START:
871       gtk_drag_dest_handle_event (event_widget, event);
872       break;
873     }
874   
875   tmp_list = current_events;
876   current_events = g_list_remove_link (current_events, tmp_list);
877   g_list_free_1 (tmp_list);
878 }
879
880 gint
881 gtk_true (void)
882 {
883   return TRUE;
884 }
885
886 gint
887 gtk_false (void)
888 {
889   return FALSE;
890 }
891
892 void
893 gtk_grab_add (GtkWidget *widget)
894 {
895   g_return_if_fail (widget != NULL);
896   
897   if (!GTK_WIDGET_HAS_GRAB (widget))
898     {
899       GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
900       
901       grabs = g_slist_prepend (grabs, widget);
902       gtk_widget_ref (widget);
903     }
904 }
905
906 GtkWidget*
907 gtk_grab_get_current (void)
908 {
909   if (grabs)
910     return GTK_WIDGET (grabs->data);
911   return NULL;
912 }
913
914 void
915 gtk_grab_remove (GtkWidget *widget)
916 {
917   g_return_if_fail (widget != NULL);
918   
919   if (GTK_WIDGET_HAS_GRAB (widget))
920     {
921       GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
922       
923       grabs = g_slist_remove (grabs, widget);
924       gtk_widget_unref (widget);
925     }
926 }
927
928 void
929 gtk_init_add (GtkFunction function,
930               gpointer    data)
931 {
932   GtkInitFunction *init;
933   
934   init = g_new (GtkInitFunction, 1);
935   init->function = function;
936   init->data = data;
937   
938   init_functions = g_list_prepend (init_functions, init);
939 }
940
941 guint
942 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
943                          gpointer        func_data)
944 {
945   GtkKeySnooperData *data;
946   static guint snooper_id = 1;
947
948   g_return_val_if_fail (snooper != NULL, 0);
949
950   data = g_new (GtkKeySnooperData, 1);
951   data->func = snooper;
952   data->func_data = func_data;
953   data->id = snooper_id++;
954   key_snoopers = g_slist_prepend (key_snoopers, data);
955
956   return data->id;
957 }
958
959 void
960 gtk_key_snooper_remove (guint            snooper_id)
961 {
962   GtkKeySnooperData *data = NULL;
963   GSList *slist;
964
965   slist = key_snoopers;
966   while (slist)
967     {
968       data = slist->data;
969       if (data->id == snooper_id)
970         break;
971
972       slist = slist->next;
973       data = NULL;
974     }
975   if (data)
976     key_snoopers = g_slist_remove (key_snoopers, data);
977 }
978
979 static gint
980 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
981                          GdkEvent  *event)
982 {
983   GSList *slist;
984   gint return_val = FALSE;
985
986   slist = key_snoopers;
987   while (slist && !return_val)
988     {
989       GtkKeySnooperData *data;
990
991       data = slist->data;
992       slist = slist->next;
993       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
994     }
995
996   return return_val;
997 }
998
999 guint
1000 gtk_quit_add_full (guint                main_level,
1001                    GtkFunction          function,
1002                    GtkCallbackMarshal   marshal,
1003                    gpointer             data,
1004                    GtkDestroyNotify     destroy)
1005 {
1006   static guint quit_id = 1;
1007   GtkQuitFunction *quitf;
1008   
1009   g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);
1010
1011   if (!quit_mem_chunk)
1012     quit_mem_chunk = g_mem_chunk_new ("quit mem chunk", sizeof (GtkQuitFunction),
1013                                       512, G_ALLOC_AND_FREE);
1014   
1015   quitf = g_chunk_new (GtkQuitFunction, quit_mem_chunk);
1016   
1017   quitf->id = quit_id++;
1018   quitf->main_level = main_level;
1019   quitf->function = function;
1020   quitf->marshal = marshal;
1021   quitf->data = data;
1022   quitf->destroy = destroy;
1023
1024   quit_functions = g_list_prepend (quit_functions, quitf);
1025   
1026   return quitf->id;
1027 }
1028
1029 static void
1030 gtk_quit_destroy (GtkQuitFunction *quitf)
1031 {
1032   if (quitf->destroy)
1033     quitf->destroy (quitf->data);
1034   g_mem_chunk_free (quit_mem_chunk, quitf);
1035 }
1036
1037 static gint
1038 gtk_quit_destructor (GtkObject **object_p)
1039 {
1040   if (*object_p)
1041     gtk_object_destroy (*object_p);
1042   g_free (object_p);
1043
1044   return FALSE;
1045 }
1046
1047 void
1048 gtk_quit_add_destroy (guint              main_level,
1049                       GtkObject         *object)
1050 {
1051   GtkObject **object_p;
1052
1053   g_return_if_fail (main_level > 0);
1054   g_return_if_fail (object != NULL);
1055   g_return_if_fail (GTK_IS_OBJECT (object));
1056
1057   object_p = g_new (GtkObject*, 1);
1058   *object_p = object;
1059   gtk_signal_connect (object,
1060                       "destroy",
1061                       GTK_SIGNAL_FUNC (gtk_widget_destroyed),
1062                       object_p);
1063   gtk_quit_add (main_level, (GtkFunction) gtk_quit_destructor, object_p);
1064 }
1065
1066 guint
1067 gtk_quit_add (guint       main_level,
1068               GtkFunction function,
1069               gpointer    data)
1070 {
1071   return gtk_quit_add_full (main_level, function, NULL, data, NULL);
1072 }
1073
1074 void
1075 gtk_quit_remove (guint id)
1076 {
1077   GtkQuitFunction *quitf;
1078   GList *tmp_list;
1079   
1080   tmp_list = quit_functions;
1081   while (tmp_list)
1082     {
1083       quitf = tmp_list->data;
1084       
1085       if (quitf->id == id)
1086         {
1087           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1088           g_list_free (tmp_list);
1089           gtk_quit_destroy (quitf);
1090           
1091           return;
1092         }
1093       
1094       tmp_list = tmp_list->next;
1095     }
1096 }
1097
1098 void
1099 gtk_quit_remove_by_data (gpointer data)
1100 {
1101   GtkQuitFunction *quitf;
1102   GList *tmp_list;
1103   
1104   tmp_list = quit_functions;
1105   while (tmp_list)
1106     {
1107       quitf = tmp_list->data;
1108       
1109       if (quitf->data == data)
1110         {
1111           quit_functions = g_list_remove_link (quit_functions, tmp_list);
1112           g_list_free (tmp_list);
1113           gtk_quit_destroy (quitf);
1114
1115           return;
1116         }
1117       
1118       tmp_list = tmp_list->next;
1119     }
1120 }
1121
1122 guint
1123 gtk_timeout_add_full (guint32            interval,
1124                       GtkFunction        function,
1125                       GtkCallbackMarshal marshal,
1126                       gpointer           data,
1127                       GtkDestroyNotify   destroy)
1128 {
1129   if (marshal)
1130     {
1131       GtkClosure *closure;
1132
1133       closure = g_new (GtkClosure, 1);
1134       closure->marshal = marshal;
1135       closure->data = data;
1136       closure->destroy = destroy;
1137
1138       return g_timeout_add_full (0, interval, 
1139                                  gtk_invoke_idle_timeout,
1140                                  closure,
1141                                  gtk_destroy_closure);
1142     }
1143   else
1144     return g_timeout_add_full (0, interval, function, data, destroy);
1145 }
1146
1147 guint
1148 gtk_timeout_add (guint32     interval,
1149                  GtkFunction function,
1150                  gpointer    data)
1151 {
1152   return g_timeout_add_full (0, interval, function, data, NULL);
1153 }
1154
1155 void
1156 gtk_timeout_remove (guint tag)
1157 {
1158   g_source_remove (tag);
1159 }
1160
1161 guint
1162 gtk_idle_add_full (gint                 priority,
1163                    GtkFunction          function,
1164                    GtkCallbackMarshal   marshal,
1165                    gpointer             data,
1166                    GtkDestroyNotify     destroy)
1167 {
1168   if (marshal)
1169     {
1170       GtkClosure *closure;
1171
1172       closure = g_new (GtkClosure, 1);
1173       closure->marshal = marshal;
1174       closure->data = data;
1175       closure->destroy = destroy;
1176
1177       return g_idle_add_full (priority,
1178                               gtk_invoke_idle_timeout,
1179                               closure,
1180                               gtk_destroy_closure);
1181     }
1182   else
1183     return g_idle_add_full (priority, function, data, destroy);
1184 }
1185
1186 guint
1187 gtk_idle_add (GtkFunction function,
1188               gpointer    data)
1189 {
1190   return g_idle_add_full (GTK_PRIORITY_DEFAULT, function, data, NULL);
1191 }
1192
1193 guint       
1194 gtk_idle_add_priority   (gint               priority,
1195                          GtkFunction        function,
1196                          gpointer           data)
1197 {
1198   return g_idle_add_full (priority, function, data, NULL);
1199 }
1200
1201 void
1202 gtk_idle_remove (guint tag)
1203 {
1204   g_source_remove (tag);
1205 }
1206
1207 void
1208 gtk_idle_remove_by_data (gpointer data)
1209 {
1210   if (!g_idle_remove_by_data (data))
1211     g_warning ("gtk_idle_remove_by_data(%p): no such idle", data);
1212 }
1213
1214 guint
1215 gtk_input_add_full (gint                source,
1216                     GdkInputCondition   condition,
1217                     GdkInputFunction    function,
1218                     GtkCallbackMarshal  marshal,
1219                     gpointer            data,
1220                     GtkDestroyNotify    destroy)
1221 {
1222   if (marshal)
1223     {
1224       GtkClosure *closure;
1225
1226       closure = g_new (GtkClosure, 1);
1227       closure->marshal = marshal;
1228       closure->data = data;
1229       closure->destroy = destroy;
1230
1231       return gdk_input_add_full (source,
1232                                  condition,
1233                                  (GdkInputFunction) gtk_invoke_input,
1234                                  closure,
1235                                  (GdkDestroyNotify) gtk_destroy_closure);
1236     }
1237   else
1238     return gdk_input_add_full (source, condition, function, data, destroy);
1239 }
1240
1241 void
1242 gtk_input_remove (guint tag)
1243 {
1244   g_source_remove (tag);
1245 }
1246
1247 static void
1248 gtk_destroy_closure (gpointer data)
1249 {
1250   GtkClosure *closure = data;
1251
1252   if (closure->destroy)
1253     (closure->destroy) (closure->data);
1254   g_free (closure);
1255 }
1256
1257 static gboolean
1258 gtk_invoke_idle_timeout (gpointer data)
1259 {
1260   GtkClosure *closure = data;
1261
1262   GtkArg args[1];
1263   gint ret_val = FALSE;
1264   args[0].name = NULL;
1265   args[0].type = GTK_TYPE_BOOL;
1266   args[0].d.pointer_data = &ret_val;
1267   closure->marshal (NULL, closure->data,  0, args);
1268   return ret_val;
1269 }
1270
1271 static void
1272 gtk_invoke_input (gpointer          data,
1273                   gint              source,
1274                   GdkInputCondition condition)
1275 {
1276   GtkClosure *closure = data;
1277
1278   GtkArg args[3];
1279   args[0].type = GTK_TYPE_INT;
1280   args[0].name = NULL;
1281   GTK_VALUE_INT(args[0]) = source;
1282   args[1].type = GTK_TYPE_GDK_INPUT_CONDITION;
1283   args[1].name = NULL;
1284   GTK_VALUE_FLAGS(args[1]) = condition;
1285   args[2].type = GTK_TYPE_NONE;
1286   args[2].name = NULL;
1287
1288   closure->marshal (NULL, closure->data, 2, args);
1289 }
1290
1291 GdkEvent*
1292 gtk_get_current_event (void)
1293 {
1294   if (current_events)
1295     return gdk_event_copy ((GdkEvent *) current_events->data);
1296   else
1297     return NULL;
1298 }
1299
1300 GtkWidget*
1301 gtk_get_event_widget (GdkEvent *event)
1302 {
1303   GtkWidget *widget;
1304
1305   widget = NULL;
1306   if (event && event->any.window)
1307     gdk_window_get_user_data (event->any.window, (void**) &widget);
1308   
1309   return widget;
1310 }
1311
1312 static void
1313 gtk_exit_func (void)
1314 {
1315   if (gtk_initialized)
1316     {
1317       gtk_initialized = FALSE;
1318       gtk_preview_uninit ();
1319     }
1320 }
1321
1322
1323 static gint
1324 gtk_quit_invoke_function (GtkQuitFunction *quitf)
1325 {
1326   if (!quitf->marshal)
1327     return quitf->function (quitf->data);
1328   else
1329     {
1330       GtkArg args[1];
1331       gint ret_val = FALSE;
1332
1333       args[0].name = NULL;
1334       args[0].type = GTK_TYPE_BOOL;
1335       args[0].d.pointer_data = &ret_val;
1336       ((GtkCallbackMarshal) quitf->marshal) (NULL,
1337                                              quitf->data,
1338                                              0, args);
1339       return ret_val;
1340     }
1341 }
1342
1343 void
1344 gtk_propagate_event (GtkWidget *widget,
1345                      GdkEvent  *event)
1346 {
1347   gint handled_event;
1348   
1349   g_return_if_fail (widget != NULL);
1350   g_return_if_fail (GTK_IS_WIDGET (widget));
1351   g_return_if_fail (event != NULL);
1352   
1353   handled_event = FALSE;
1354
1355   if ((event->type == GDK_KEY_PRESS) ||
1356       (event->type == GDK_KEY_RELEASE))
1357     {
1358       /* Only send key events within Window widgets to the Window
1359        *  The Window widget will in turn pass the
1360        *  key event on to the currently focused widget
1361        *  for that window.
1362        */
1363       GtkWidget *window;
1364
1365       window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1366       if (window)
1367         {
1368           if (GTK_WIDGET_IS_SENSITIVE (window))
1369             gtk_widget_event (window, event);
1370
1371           handled_event = TRUE; /* don't send to widget */
1372         }
1373     }
1374   
1375   /* Other events get propagated up the widget tree
1376    *  so that parents can see the button and motion
1377    *  events of the children.
1378    */
1379   while (!handled_event && widget)
1380     {
1381       GtkWidget *tmp;
1382
1383       gtk_widget_ref (widget);
1384       handled_event = !GTK_WIDGET_IS_SENSITIVE (widget) || gtk_widget_event (widget, event);
1385       tmp = widget->parent;
1386       gtk_widget_unref (widget);
1387       widget  = tmp;
1388     }
1389 }
1390
1391 #if 0
1392 static void
1393 gtk_error (gchar *str)
1394 {
1395   gtk_print (str);
1396 }
1397
1398 static void
1399 gtk_warning (gchar *str)
1400 {
1401   gtk_print (str);
1402 }
1403
1404 static void
1405 gtk_message (gchar *str)
1406 {
1407   gtk_print (str);
1408 }
1409
1410 static void
1411 gtk_print (gchar *str)
1412 {
1413   static GtkWidget *window = NULL;
1414   static GtkWidget *text;
1415   static int level = 0;
1416   GtkWidget *box1;
1417   GtkWidget *box2;
1418   GtkWidget *table;
1419   GtkWidget *hscrollbar;
1420   GtkWidget *vscrollbar;
1421   GtkWidget *separator;
1422   GtkWidget *button;
1423   
1424   if (level > 0)
1425     {
1426       fputs (str, stdout);
1427       fflush (stdout);
1428       return;
1429     }
1430   
1431   if (!window)
1432     {
1433       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1434       
1435       gtk_signal_connect (GTK_OBJECT (window), "destroy",
1436                           (GtkSignalFunc) gtk_widget_destroyed,
1437                           &window);
1438       
1439       gtk_window_set_title (GTK_WINDOW (window), "Messages");
1440       
1441       box1 = gtk_vbox_new (FALSE, 0);
1442       gtk_container_add (GTK_CONTAINER (window), box1);
1443       gtk_widget_show (box1);
1444       
1445       
1446       box2 = gtk_vbox_new (FALSE, 10);
1447       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1448       gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1449       gtk_widget_show (box2);
1450       
1451       
1452       table = gtk_table_new (2, 2, FALSE);
1453       gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
1454       gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
1455       gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
1456       gtk_widget_show (table);
1457       
1458       text = gtk_text_new (NULL, NULL);
1459       gtk_text_set_editable (GTK_TEXT (text), FALSE);
1460       gtk_table_attach_defaults (GTK_TABLE (table), text, 0, 1, 0, 1);
1461       gtk_widget_show (text);
1462       gtk_widget_realize (text);
1463       
1464       hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
1465       gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
1466                         GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
1467       gtk_widget_show (hscrollbar);
1468       
1469       vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
1470       gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
1471                         GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
1472       gtk_widget_show (vscrollbar);
1473       
1474       separator = gtk_hseparator_new ();
1475       gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1476       gtk_widget_show (separator);
1477       
1478       
1479       box2 = gtk_vbox_new (FALSE, 10);
1480       gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
1481       gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1482       gtk_widget_show (box2);
1483       
1484       
1485       button = gtk_button_new_with_label ("close");
1486       gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1487                                  (GtkSignalFunc) gtk_widget_hide,
1488                                  GTK_OBJECT (window));
1489       gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1490       GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1491       gtk_widget_grab_default (button);
1492       gtk_widget_show (button);
1493     }
1494   
1495   level += 1;
1496   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL, str, -1);
1497   level -= 1;
1498   
1499   if (!GTK_WIDGET_VISIBLE (window))
1500     gtk_widget_show (window);
1501 }
1502 #endif