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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 /**
26  * SECTION:gtkmain
27  * @Short_description: Library initialization, main event loop, and events
28  * @Title: Main loop and Events
29  * @See_also:See the GLib manual, especially #GMainLoop and signal-related
30  *    functions such as g_signal_connect()
31  *
32  * Before using GTK+, you need to initialize it; initialization connects to the
33  * window system display, and parses some standard command line arguments. The
34  * gtk_init() macro initializes GTK+. gtk_init() exits the application if errors
35  * occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to
36  * recover from a failed GTK+ initialization - you might start up your
37  * application in text mode instead.
38  *
39  * Like all GUI toolkits, GTK+ uses an event-driven programming model. When the
40  * user is doing nothing, GTK+ sits in the “main loop” and
41  * waits for input. If the user performs some action - say, a mouse click - then
42  * the main loop “wakes up” and delivers an event to GTK+. GTK+ forwards the
43  * event to one or more widgets.
44  *
45  * When widgets receive an event, they frequently emit one or more
46  * “signals”. Signals notify your program that "something
47  * interesting happened" by invoking functions you’ve connected to the signal
48  * with g_signal_connect(). Functions connected to a signal are often termed
49  * “callbacks”.
50  *
51  * When your callbacks are invoked, you would typically take some action - for
52  * example, when an Open button is clicked you might display a
53  * #GtkFileChooserDialog. After a callback finishes, GTK+ will return to the
54  * main loop and await more user input.
55  *
56  * ## Typical main() function for a GTK+ application
57  *
58  * |[<!-- language="C" -->
59  * int
60  * main (int argc, char **argv)
61  * {
62  *  GtkWidget *mainwin;
63  *   // Initialize i18n support with bindtextdomain(), etc.
64  *
65  *   // ...
66  *
67  *   // Initialize the widget set
68  *   gtk_init (&argc, &argv);
69  *
70  *   // Create the main window
71  *   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
72  *
73  *   // Set up our GUI elements
74  *
75  *   // ...
76  *
77  *   // Show the application window
78  *   gtk_widget_show_all (mainwin);
79  *
80  *   // Enter the main event loop, and wait for user interaction
81  *   gtk_main ();
82  *
83  *   // The user lost interest
84  *   return 0;
85  * }
86  * ]|
87  *
88  * It’s OK to use the GLib main loop directly instead of gtk_main(), though it
89  * involves slightly more typing. See #GMainLoop in the GLib documentation.
90  */
91 
92 #include "config.h"
93 
94 #include "gdk/gdk.h"
95 #include "gdk/gdk-private.h"
96 
97 #include <locale.h>
98 
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <string.h>
102 #ifdef HAVE_UNISTD_H
103 #include <unistd.h>
104 #endif
105 #include <sys/types.h>          /* For uid_t, gid_t */
106 
107 #ifdef G_OS_WIN32
108 #define STRICT
109 #include <windows.h>
110 #undef STRICT
111 #endif
112 
113 #include "gtkintl.h"
114 
115 #include "gtkaccelmapprivate.h"
116 #include "gtkbox.h"
117 #include "gtkclipboardprivate.h"
118 #include "gtkdebug.h"
119 #include "gtkdndprivate.h"
120 #include "gtkmain.h"
121 #include "gtkmenu.h"
122 #include "gtkmodules.h"
123 #include "gtkmodulesprivate.h"
124 #include "gtkprivate.h"
125 #include "gtkrecentmanager.h"
126 #include "gtkselectionprivate.h"
127 #include "gtksettingsprivate.h"
128 #include "gtktooltipprivate.h"
129 #include "gtkversion.h"
130 #include "gtkwidgetprivate.h"
131 #include "gtkwindowprivate.h"
132 #include "gtkwindowgroup.h"
133 
134 #include "a11y/gtkaccessibility.h"
135 
136 /* Private type definitions
137  */
138 typedef struct _GtkKeySnooperData        GtkKeySnooperData;
139 
140 struct _GtkKeySnooperData
141 {
142   GtkKeySnoopFunc func;
143   gpointer func_data;
144   guint id;
145 };
146 
147 static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
148                                           GdkEvent           *event);
149 
150 static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);
151 
152 static guint gtk_main_loop_level = 0;
153 static gint pre_initialized = FALSE;
154 static gint gtk_initialized = FALSE;
155 static GList *current_events = NULL;
156 
157 static GSList *main_loops = NULL;      /* stack of currently executing main loops */
158 
159 static GSList *key_snoopers = NULL;
160 
161 typedef struct {
162   GdkDisplay *display;
163   guint flags;
164 } DisplayDebugFlags;
165 
166 #define N_DEBUG_DISPLAYS 4
167 
168 DisplayDebugFlags debug_flags[N_DEBUG_DISPLAYS];
169 
170 static const GDebugKey gtk_debug_keys[] = {
171   { "misc", GTK_DEBUG_MISC },
172   { "plugsocket", GTK_DEBUG_PLUGSOCKET },
173   { "text", GTK_DEBUG_TEXT },
174   { "tree", GTK_DEBUG_TREE },
175   { "updates", GTK_DEBUG_UPDATES },
176   { "keybindings", GTK_DEBUG_KEYBINDINGS },
177   { "multihead", GTK_DEBUG_MULTIHEAD },
178   { "modules", GTK_DEBUG_MODULES },
179   { "geometry", GTK_DEBUG_GEOMETRY },
180   { "icontheme", GTK_DEBUG_ICONTHEME },
181   { "printing", GTK_DEBUG_PRINTING} ,
182   { "builder", GTK_DEBUG_BUILDER },
183   { "size-request", GTK_DEBUG_SIZE_REQUEST },
184   { "no-css-cache", GTK_DEBUG_NO_CSS_CACHE },
185   { "baselines", GTK_DEBUG_BASELINES },
186   { "pixel-cache", GTK_DEBUG_PIXEL_CACHE },
187   { "no-pixel-cache", GTK_DEBUG_NO_PIXEL_CACHE },
188   { "interactive", GTK_DEBUG_INTERACTIVE },
189   { "touchscreen", GTK_DEBUG_TOUCHSCREEN },
190   { "actions", GTK_DEBUG_ACTIONS },
191   { "resize", GTK_DEBUG_RESIZE },
192   { "layout", GTK_DEBUG_LAYOUT }
193 };
194 
195 /**
196  * gtk_get_major_version:
197  *
198  * Returns the major version number of the GTK+ library.
199  * (e.g. in GTK+ version 3.1.5 this is 3.)
200  *
201  * This function is in the library, so it represents the GTK+ library
202  * your code is running against. Contrast with the #GTK_MAJOR_VERSION
203  * macro, which represents the major version of the GTK+ headers you
204  * have included when compiling your code.
205  *
206  * Returns: the major version number of the GTK+ library
207  *
208  * Since: 3.0
209  */
210 guint
gtk_get_major_version(void)211 gtk_get_major_version (void)
212 {
213   return GTK_MAJOR_VERSION;
214 }
215 
216 /**
217  * gtk_get_minor_version:
218  *
219  * Returns the minor version number of the GTK+ library.
220  * (e.g. in GTK+ version 3.1.5 this is 1.)
221  *
222  * This function is in the library, so it represents the GTK+ library
223  * your code is are running against. Contrast with the
224  * #GTK_MINOR_VERSION macro, which represents the minor version of the
225  * GTK+ headers you have included when compiling your code.
226  *
227  * Returns: the minor version number of the GTK+ library
228  *
229  * Since: 3.0
230  */
231 guint
gtk_get_minor_version(void)232 gtk_get_minor_version (void)
233 {
234   return GTK_MINOR_VERSION;
235 }
236 
237 /**
238  * gtk_get_micro_version:
239  *
240  * Returns the micro version number of the GTK+ library.
241  * (e.g. in GTK+ version 3.1.5 this is 5.)
242  *
243  * This function is in the library, so it represents the GTK+ library
244  * your code is are running against. Contrast with the
245  * #GTK_MICRO_VERSION macro, which represents the micro version of the
246  * GTK+ headers you have included when compiling your code.
247  *
248  * Returns: the micro version number of the GTK+ library
249  *
250  * Since: 3.0
251  */
252 guint
gtk_get_micro_version(void)253 gtk_get_micro_version (void)
254 {
255   return GTK_MICRO_VERSION;
256 }
257 
258 /**
259  * gtk_get_binary_age:
260  *
261  * Returns the binary age as passed to `libtool`
262  * when building the GTK+ library the process is running against.
263  * If `libtool` means nothing to you, don't
264  * worry about it.
265  *
266  * Returns: the binary age of the GTK+ library
267  *
268  * Since: 3.0
269  */
270 guint
gtk_get_binary_age(void)271 gtk_get_binary_age (void)
272 {
273   return GTK_BINARY_AGE;
274 }
275 
276 /**
277  * gtk_get_interface_age:
278  *
279  * Returns the interface age as passed to `libtool`
280  * when building the GTK+ library the process is running against.
281  * If `libtool` means nothing to you, don't
282  * worry about it.
283  *
284  * Returns: the interface age of the GTK+ library
285  *
286  * Since: 3.0
287  */
288 guint
gtk_get_interface_age(void)289 gtk_get_interface_age (void)
290 {
291   return GTK_INTERFACE_AGE;
292 }
293 
294 /**
295  * gtk_check_version:
296  * @required_major: the required major version
297  * @required_minor: the required minor version
298  * @required_micro: the required micro version
299  *
300  * Checks that the GTK+ library in use is compatible with the
301  * given version. Generally you would pass in the constants
302  * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
303  * as the three arguments to this function; that produces
304  * a check that the library in use is compatible with
305  * the version of GTK+ the application or module was compiled
306  * against.
307  *
308  * Compatibility is defined by two things: first the version
309  * of the running library is newer than the version
310  * @required_major.required_minor.@required_micro. Second
311  * the running library must be binary compatible with the
312  * version @required_major.required_minor.@required_micro
313  * (same major version.)
314  *
315  * This function is primarily for GTK+ modules; the module
316  * can call this function to check that it wasn’t loaded
317  * into an incompatible version of GTK+. However, such a
318  * check isn’t completely reliable, since the module may be
319  * linked against an old version of GTK+ and calling the
320  * old version of gtk_check_version(), but still get loaded
321  * into an application using a newer version of GTK+.
322  *
323  * Returns: (nullable): %NULL if the GTK+ library is compatible with the
324  *   given version, or a string describing the version mismatch.
325  *   The returned string is owned by GTK+ and should not be modified
326  *   or freed.
327  */
328 const gchar*
gtk_check_version(guint required_major,guint required_minor,guint required_micro)329 gtk_check_version (guint required_major,
330                    guint required_minor,
331                    guint required_micro)
332 {
333   gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
334   gint required_effective_micro = 100 * required_minor + required_micro;
335 
336   if (required_major > GTK_MAJOR_VERSION)
337     return "GTK+ version too old (major mismatch)";
338   if (required_major < GTK_MAJOR_VERSION)
339     return "GTK+ version too new (major mismatch)";
340   if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
341     return "GTK+ version too new (micro mismatch)";
342   if (required_effective_micro > gtk_effective_micro)
343     return "GTK+ version too old (micro mismatch)";
344   return NULL;
345 }
346 
347 /* This checks to see if the process is running suid or sgid
348  * at the current time. If so, we don’t allow GTK+ to be initialized.
349  * This is meant to be a mild check - we only error out if we
350  * can prove the programmer is doing something wrong, not if
351  * they could be doing something wrong. For this reason, we
352  * don’t use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
353  */
354 static gboolean
check_setugid(void)355 check_setugid (void)
356 {
357 /* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
358 #ifndef G_OS_WIN32
359   uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
360   gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
361 
362 #ifdef HAVE_GETRESUID
363   /* These aren't in the header files, so we prototype them here.
364    */
365   int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
366   int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
367 
368   if (getresuid (&ruid, &euid, &suid) != 0 ||
369       getresgid (&rgid, &egid, &sgid) != 0)
370 #endif /* HAVE_GETRESUID */
371     {
372       suid = ruid = getuid ();
373       sgid = rgid = getgid ();
374       euid = geteuid ();
375       egid = getegid ();
376     }
377 
378   if (ruid != euid || ruid != suid ||
379       rgid != egid || rgid != sgid)
380     {
381       g_warning ("This process is currently running setuid or setgid.\n"
382                  "This is not a supported use of GTK+. You must create a helper\n"
383                  "program instead. For further details, see:\n\n"
384                  "    http://www.gtk.org/setuid.html\n\n"
385                  "Refusing to initialize GTK+.");
386       exit (1);
387     }
388 #endif
389   return TRUE;
390 }
391 
392 static gboolean do_setlocale = TRUE;
393 
394 /**
395  * gtk_disable_setlocale:
396  *
397  * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
398  * gtk_parse_args() from automatically
399  * calling `setlocale (LC_ALL, "")`. You would
400  * want to use this function if you wanted to set the locale for
401  * your program to something other than the user’s locale, or if
402  * you wanted to set different values for different locale categories.
403  *
404  * Most programs should not need to call this function.
405  **/
406 void
gtk_disable_setlocale(void)407 gtk_disable_setlocale (void)
408 {
409   if (pre_initialized)
410     g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
411 
412   do_setlocale = FALSE;
413 }
414 
415 #ifdef G_PLATFORM_WIN32
416 #undef gtk_init_check
417 #endif
418 
419 static GString *gtk_modules_string = NULL;
420 static gboolean g_fatal_warnings = FALSE;
421 
422 #ifdef G_ENABLE_DEBUG
423 static gboolean
gtk_arg_debug_cb(const char * key,const char * value,gpointer user_data)424 gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
425 {
426   debug_flags[0].flags |= g_parse_debug_string (value,
427                                                 gtk_debug_keys,
428                                                 G_N_ELEMENTS (gtk_debug_keys));
429 
430   return TRUE;
431 }
432 
433 static gboolean
gtk_arg_no_debug_cb(const char * key,const char * value,gpointer user_data)434 gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
435 {
436   debug_flags[0].flags &= ~g_parse_debug_string (value,
437                                                  gtk_debug_keys,
438                                                  G_N_ELEMENTS (gtk_debug_keys));
439 
440   return TRUE;
441 }
442 #endif /* G_ENABLE_DEBUG */
443 
444 static gboolean
gtk_arg_module_cb(const char * key,const char * value,gpointer user_data)445 gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
446 {
447   if (value && *value)
448     {
449       if (gtk_modules_string)
450         g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
451       else
452         gtk_modules_string = g_string_new (NULL);
453 
454       g_string_append (gtk_modules_string, value);
455     }
456 
457   return TRUE;
458 }
459 
460 static const GOptionEntry gtk_args[] = {
461   { "gtk-module",       0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,
462     /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"),
463     /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
464   { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings,
465     /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
466 #ifdef G_ENABLE_DEBUG
467   { "gtk-debug",        0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,
468     /* Description of --gtk-debug=FLAGS in --help output */    N_("GTK+ debugging flags to set"),
469     /* Placeholder in --gtk-debug=FLAGS in --help output */    N_("FLAGS") },
470   { "gtk-no-debug",     0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb,
471     /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"),
472     /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
473 #endif
474   { NULL }
475 };
476 
477 #ifdef G_OS_WIN32
478 
479 static char *iso639_to_check = NULL;
480 static char *iso3166_to_check = NULL;
481 static char *script_to_check = NULL;
482 static gboolean setlocale_called = FALSE;
483 
484 static BOOL CALLBACK
enum_locale_proc(LPTSTR locale)485 enum_locale_proc (LPTSTR locale)
486 {
487   LCID lcid;
488   char iso639[10];
489   char iso3166[10];
490   char *endptr;
491 
492 
493   lcid = strtoul (locale, &endptr, 16);
494   if (*endptr == '\0' &&
495       GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
496       GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
497     {
498       if (strcmp (iso639, iso639_to_check) == 0 &&
499           ((iso3166_to_check != NULL &&
500             strcmp (iso3166, iso3166_to_check) == 0) ||
501            (iso3166_to_check == NULL &&
502             SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
503         {
504           char language[100], country[100];
505           char locale[300];
506 
507           if (script_to_check != NULL)
508             {
509               /* If lcid is the "other" script for this language,
510                * return TRUE, i.e. continue looking.
511                */
512               if (strcmp (script_to_check, "Latn") == 0)
513                 {
514                   switch (LANGIDFROMLCID (lcid))
515                     {
516                     case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
517                       return TRUE;
518                     case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
519                       return TRUE;
520                     case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
521                       return TRUE;
522                     case MAKELANGID (LANG_SERBIAN, 0x07):
523                       /* Serbian in Bosnia and Herzegovina, Cyrillic */
524                       return TRUE;
525                     }
526                 }
527               else if (strcmp (script_to_check, "Cyrl") == 0)
528                 {
529                   switch (LANGIDFROMLCID (lcid))
530                     {
531                     case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
532                       return TRUE;
533                     case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
534                       return TRUE;
535                     case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
536                       return TRUE;
537                     case MAKELANGID (LANG_SERBIAN, 0x06):
538                       /* Serbian in Bosnia and Herzegovina, Latin */
539                       return TRUE;
540                     }
541                 }
542             }
543 
544           SetThreadLocale (lcid);
545 
546           if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
547               GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
548             {
549               strcpy (locale, language);
550               strcat (locale, "_");
551               strcat (locale, country);
552 
553               if (setlocale (LC_ALL, locale) != NULL)
554                 setlocale_called = TRUE;
555             }
556 
557           return FALSE;
558         }
559     }
560 
561   return TRUE;
562 }
563 
564 #endif
565 
566 static void
setlocale_initialization(void)567 setlocale_initialization (void)
568 {
569   static gboolean initialized = FALSE;
570 
571   if (initialized)
572     return;
573   initialized = TRUE;
574 
575   if (do_setlocale)
576     {
577 #ifdef G_OS_WIN32
578       /* If some of the POSIXish environment variables are set, set
579        * the Win32 thread locale correspondingly.
580        */
581       char *p = getenv ("LC_ALL");
582       if (p == NULL)
583         p = getenv ("LANG");
584 
585       if (p != NULL)
586         {
587           p = g_strdup (p);
588           if (strcmp (p, "C") == 0)
589             SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
590           else
591             {
592               /* Check if one of the supported locales match the
593                * environment variable. If so, use that locale.
594                */
595               iso639_to_check = p;
596               iso3166_to_check = strchr (iso639_to_check, '_');
597               if (iso3166_to_check != NULL)
598                 {
599                   *iso3166_to_check++ = '\0';
600 
601                   script_to_check = strchr (iso3166_to_check, '@');
602                   if (script_to_check != NULL)
603                     *script_to_check++ = '\0';
604 
605                   /* Handle special cases. */
606 
607                   /* The standard code for Serbia and Montenegro was
608                    * "CS", but MSFT uses for some reason "SP". By now
609                    * (October 2006), SP has split into two, "RS" and
610                    * "ME", but don't bother trying to handle those
611                    * yet. Do handle the even older "YU", though.
612                    */
613                   if (strcmp (iso3166_to_check, "CS") == 0 ||
614                       strcmp (iso3166_to_check, "YU") == 0)
615                     iso3166_to_check = "SP";
616                 }
617               else
618                 {
619                   script_to_check = strchr (iso639_to_check, '@');
620                   if (script_to_check != NULL)
621                     *script_to_check++ = '\0';
622                   /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
623                   if (strcmp (iso639_to_check, "sr") == 0)
624                     iso3166_to_check = "SP";
625                 }
626 
627               EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
628             }
629           g_free (p);
630         }
631       if (!setlocale_called)
632         setlocale (LC_ALL, "");
633 #else
634       if (!setlocale (LC_ALL, ""))
635         g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
636 #endif
637     }
638 }
639 
640 static void
do_pre_parse_initialization(int * argc,char *** argv)641 do_pre_parse_initialization (int    *argc,
642                              char ***argv)
643 {
644   const gchar *env_string;
645   double slowdown;
646 
647   if (pre_initialized)
648     return;
649 
650   pre_initialized = TRUE;
651 
652   if (_gtk_module_has_mixed_deps (NULL))
653     g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
654 
655   GDK_PRIVATE_CALL (gdk_pre_parse) ();
656   gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
657 
658   env_string = g_getenv ("GTK_DEBUG");
659   if (env_string != NULL)
660     {
661       debug_flags[0].flags = g_parse_debug_string (env_string,
662                                                    gtk_debug_keys,
663                                                    G_N_ELEMENTS (gtk_debug_keys));
664 #ifndef G_ENABLE_DEBUG
665       /* No need to print the warning for "interactive" since it's kept anyway. */
666       if (debug_flags[0].flags != GTK_DEBUG_INTERACTIVE)
667         g_warning ("GTK_DEBUG set but ignored because gtk isn't built with G_ENABLE_DEBUG");
668       /* Only keep "interactive" if not with G_ENABLE_DEBUG. */
669       debug_flags[0].flags &= GTK_DEBUG_INTERACTIVE;
670 #endif  /* G_ENABLE_DEBUG */
671       env_string = NULL;
672     }
673 
674   env_string = g_getenv ("GTK3_MODULES");
675   if (env_string)
676     gtk_modules_string = g_string_new (env_string);
677 
678   env_string = g_getenv ("GTK_MODULES");
679   if (env_string)
680     {
681       if (gtk_modules_string)
682         g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
683       else
684         gtk_modules_string = g_string_new (NULL);
685 
686       g_string_append (gtk_modules_string, env_string);
687     }
688 
689   env_string = g_getenv ("GTK_SLOWDOWN");
690   if (env_string)
691     {
692       slowdown = g_ascii_strtod (env_string, NULL);
693       _gtk_set_slowdown (slowdown);
694     }
695 }
696 
697 static void
gettext_initialization(void)698 gettext_initialization (void)
699 {
700   setlocale_initialization ();
701 
702 #ifdef ENABLE_NLS
703   bindtextdomain (GETTEXT_PACKAGE, _gtk_get_localedir ());
704   bindtextdomain (GETTEXT_PACKAGE "-properties", _gtk_get_localedir ());
705 #    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
706   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
707   bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
708 #    endif
709 #endif
710 }
711 
712 static void
default_display_notify_cb(GdkDisplayManager * dm)713 default_display_notify_cb (GdkDisplayManager *dm)
714 {
715   _gtk_accessibility_init ();
716   debug_flags[0].display = gdk_display_get_default ();
717 }
718 
719 static void
do_post_parse_initialization(int * argc,char *** argv)720 do_post_parse_initialization (int    *argc,
721                               char ***argv)
722 {
723   GdkDisplayManager *display_manager;
724 
725   if (gtk_initialized)
726     return;
727 
728   gettext_initialization ();
729 
730 #ifdef SIGPIPE
731   signal (SIGPIPE, SIG_IGN);
732 #endif
733 
734   if (g_fatal_warnings)
735     {
736       GLogLevelFlags fatal_mask;
737 
738       fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
739       fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
740       g_log_set_always_fatal (fatal_mask);
741     }
742 
743 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
744   if (debug_flags[0].flags & GTK_DEBUG_UPDATES)
745     gdk_window_set_debug_updates (TRUE);
746 G_GNUC_END_IGNORE_DEPRECATIONS
747 
748   gtk_widget_set_default_direction (gtk_get_locale_direction ());
749 
750   _gtk_ensure_resources ();
751 
752   _gtk_accel_map_init ();
753 
754   gtk_initialized = TRUE;
755 
756   if (gtk_modules_string)
757     {
758       _gtk_modules_init (argc, argv, gtk_modules_string->str);
759       g_string_free (gtk_modules_string, TRUE);
760     }
761   else
762     {
763       _gtk_modules_init (argc, argv, NULL);
764     }
765 
766   display_manager = gdk_display_manager_get ();
767   if (gdk_display_manager_get_default_display (display_manager) != NULL)
768     default_display_notify_cb (display_manager);
769 
770   g_signal_connect (display_manager, "notify::default-display",
771                     G_CALLBACK (default_display_notify_cb),
772                     NULL);
773 }
774 
775 
776 typedef struct
777 {
778   gboolean open_default_display;
779 } OptionGroupInfo;
780 
781 static gboolean
pre_parse_hook(GOptionContext * context,GOptionGroup * group,gpointer data,GError ** error)782 pre_parse_hook (GOptionContext *context,
783                 GOptionGroup   *group,
784                 gpointer        data,
785                 GError        **error)
786 {
787   do_pre_parse_initialization (NULL, NULL);
788 
789   return TRUE;
790 }
791 
792 static gboolean
post_parse_hook(GOptionContext * context,GOptionGroup * group,gpointer data,GError ** error)793 post_parse_hook (GOptionContext *context,
794                  GOptionGroup   *group,
795                  gpointer       data,
796                  GError        **error)
797 {
798   OptionGroupInfo *info = data;
799 
800 
801   do_post_parse_initialization (NULL, NULL);
802 
803   if (info->open_default_display)
804     {
805       if (GDK_PRIVATE_CALL (gdk_display_open_default) () == NULL)
806         {
807           const char *display_name = gdk_get_display_arg_name ();
808           g_set_error (error,
809                        G_OPTION_ERROR,
810                        G_OPTION_ERROR_FAILED,
811                        _("Cannot open display: %s"),
812                        display_name ? display_name : "" );
813 
814           return FALSE;
815         }
816 
817       if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
818         gtk_window_set_interactive_debugging (TRUE);
819     }
820 
821   return TRUE;
822 }
823 
824 guint
gtk_get_display_debug_flags(GdkDisplay * display)825 gtk_get_display_debug_flags (GdkDisplay *display)
826 {
827   gint i;
828 
829   for (i = 0; i < N_DEBUG_DISPLAYS; i++)
830     {
831       if (debug_flags[i].display == display)
832         return debug_flags[i].flags;
833     }
834 
835   return 0;
836 }
837 
838 void
gtk_set_display_debug_flags(GdkDisplay * display,guint flags)839 gtk_set_display_debug_flags (GdkDisplay *display,
840                              guint       flags)
841 {
842   gint i;
843 
844   for (i = 0; i < N_DEBUG_DISPLAYS; i++)
845     {
846       if (debug_flags[i].display == NULL)
847         debug_flags[i].display = display;
848 
849       if (debug_flags[i].display == display)
850         {
851           debug_flags[i].flags = flags;
852           return;
853         }
854     }
855 }
856 
857 /**
858  * gtk_get_debug_flags:
859  *
860  * Returns the GTK+ debug flags.
861  *
862  * This function is intended for GTK+ modules that want
863  * to adjust their debug output based on GTK+ debug flags.
864  *
865  * Returns: the GTK+ debug flags.
866  */
867 guint
gtk_get_debug_flags(void)868 gtk_get_debug_flags (void)
869 {
870   return gtk_get_display_debug_flags (gdk_display_get_default ());
871 }
872 
873 /**
874  * gtk_set_debug_flags:
875  *
876  * Sets the GTK+ debug flags.
877  */
878 void
gtk_set_debug_flags(guint flags)879 gtk_set_debug_flags (guint flags)
880 {
881   gtk_set_display_debug_flags (gdk_display_get_default (), flags);
882 }
883 
884 gboolean
gtk_simulate_touchscreen(void)885 gtk_simulate_touchscreen (void)
886 {
887   static gint test_touchscreen;
888 
889   if (test_touchscreen == 0)
890     test_touchscreen = g_getenv ("GTK_TEST_TOUCHSCREEN") != NULL ? 1 : -1;
891 
892   return test_touchscreen > 0 || (gtk_get_debug_flags () & GTK_DEBUG_TOUCHSCREEN) != 0;
893  }
894 
895 /**
896  * gtk_get_option_group:
897  * @open_default_display: whether to open the default display
898  *     when parsing the commandline arguments
899  *
900  * Returns a #GOptionGroup for the commandline arguments recognized
901  * by GTK+ and GDK.
902  *
903  * You should add this group to your #GOptionContext
904  * with g_option_context_add_group(), if you are using
905  * g_option_context_parse() to parse your commandline arguments.
906  *
907  * Returns: (transfer full): a #GOptionGroup for the commandline
908  *     arguments recognized by GTK+
909  *
910  * Since: 2.6
911  */
912 GOptionGroup *
gtk_get_option_group(gboolean open_default_display)913 gtk_get_option_group (gboolean open_default_display)
914 {
915   GOptionGroup *group;
916   OptionGroupInfo *info;
917 
918   gettext_initialization ();
919 
920   info = g_new0 (OptionGroupInfo, 1);
921   info->open_default_display = open_default_display;
922 
923   group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
924   g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
925 
926   GDK_PRIVATE_CALL (gdk_add_option_entries) (group);
927   g_option_group_add_entries (group, gtk_args);
928   g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
929 
930   return group;
931 }
932 
933 /**
934  * gtk_init_with_args:
935  * @argc: (inout): Address of the `argc` parameter of
936  *     your main() function (or 0 if @argv is %NULL). This will be changed if
937  *     any arguments were handled.
938  * @argv: (array length=argc) (inout) (allow-none): Address of the
939  *     `argv` parameter of main(), or %NULL. Any options
940  *     understood by GTK+ are stripped before return.
941  * @parameter_string: (allow-none): a string which is displayed in
942  *    the first line of `--help` output, after
943  *    `programname [OPTION...]`
944  * @entries: (array zero-terminated=1): a %NULL-terminated array
945  *    of #GOptionEntrys describing the options of your program
946  * @translation_domain: (nullable): a translation domain to use for translating
947  *    the `--help` output for the options in @entries
948  *    and the @parameter_string with gettext(), or %NULL
949  * @error: a return location for errors
950  *
951  * This function does the same work as gtk_init_check().
952  * Additionally, it allows you to add your own commandline options,
953  * and it automatically generates nicely formatted
954  * `--help` output. Note that your program will
955  * be terminated after writing out the help output.
956  *
957  * Returns: %TRUE if the commandline arguments (if any) were valid and
958  *     if the windowing system has been successfully initialized,
959  *     %FALSE otherwise
960  *
961  * Since: 2.6
962  */
963 gboolean
gtk_init_with_args(gint * argc,gchar *** argv,const gchar * parameter_string,const GOptionEntry * entries,const gchar * translation_domain,GError ** error)964 gtk_init_with_args (gint                 *argc,
965                     gchar              ***argv,
966                     const gchar          *parameter_string,
967                     const GOptionEntry   *entries,
968                     const gchar          *translation_domain,
969                     GError              **error)
970 {
971   GOptionContext *context;
972   GOptionGroup *gtk_group;
973   gboolean retval;
974 
975   if (gtk_initialized)
976     goto done;
977 
978   gettext_initialization ();
979 
980   if (!check_setugid ())
981     return FALSE;
982 
983   gtk_group = gtk_get_option_group (FALSE);
984 
985   context = g_option_context_new (parameter_string);
986   g_option_context_add_group (context, gtk_group);
987   g_option_context_set_translation_domain (context, translation_domain);
988 
989   if (entries)
990     g_option_context_add_main_entries (context, entries, translation_domain);
991   retval = g_option_context_parse (context, argc, argv, error);
992 
993   g_option_context_free (context);
994 
995   if (!retval)
996     return FALSE;
997 
998 done:
999   if (GDK_PRIVATE_CALL (gdk_display_open_default) () == NULL)
1000     {
1001       const char *display_name = gdk_get_display_arg_name ();
1002       g_set_error (error,
1003                    G_OPTION_ERROR,
1004                    G_OPTION_ERROR_FAILED,
1005                    _("Cannot open display: %s"),
1006                    display_name ? display_name : "" );
1007 
1008       return FALSE;
1009     }
1010 
1011   if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
1012     gtk_window_set_interactive_debugging (TRUE);
1013 
1014   return TRUE;
1015 }
1016 
1017 
1018 /**
1019  * gtk_parse_args:
1020  * @argc: (inout): a pointer to the number of command line arguments
1021  * @argv: (array length=argc) (inout): a pointer to the array of
1022  *     command line arguments
1023  *
1024  * Parses command line arguments, and initializes global
1025  * attributes of GTK+, but does not actually open a connection
1026  * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
1027  *
1028  * Any arguments used by GTK+ or GDK are removed from the array and
1029  * @argc and @argv are updated accordingly.
1030  *
1031  * There is no need to call this function explicitly if you are using
1032  * gtk_init(), or gtk_init_check().
1033  *
1034  * Note that many aspects of GTK+ require a display connection to
1035  * function, so this way of initializing GTK+ is really only useful
1036  * for specialized use cases.
1037  *
1038  * Returns: %TRUE if initialization succeeded, otherwise %FALSE
1039  */
1040 gboolean
gtk_parse_args(int * argc,char *** argv)1041 gtk_parse_args (int    *argc,
1042                 char ***argv)
1043 {
1044   GOptionContext *option_context;
1045   GOptionGroup *gtk_group;
1046   GError *error = NULL;
1047 
1048   if (gtk_initialized)
1049     return TRUE;
1050 
1051   gettext_initialization ();
1052 
1053   if (!check_setugid ())
1054     return FALSE;
1055 
1056   option_context = g_option_context_new (NULL);
1057   g_option_context_set_ignore_unknown_options (option_context, TRUE);
1058   g_option_context_set_help_enabled (option_context, FALSE);
1059   gtk_group = gtk_get_option_group (FALSE);
1060   g_option_context_set_main_group (option_context, gtk_group);
1061   if (!g_option_context_parse (option_context, argc, argv, &error))
1062     {
1063       g_warning ("%s", error->message);
1064       g_error_free (error);
1065     }
1066 
1067   g_option_context_free (option_context);
1068 
1069   return TRUE;
1070 }
1071 
1072 #ifdef G_PLATFORM_WIN32
1073 #undef gtk_init_check
1074 #endif
1075 
1076 /**
1077  * gtk_init_check:
1078  * @argc: (inout): Address of the `argc` parameter of
1079  *     your main() function (or 0 if @argv is %NULL). This will be changed if
1080  *     any arguments were handled.
1081  * @argv: (array length=argc) (inout) (allow-none): Address of the
1082  *     `argv` parameter of main(), or %NULL. Any options
1083  *     understood by GTK+ are stripped before return.
1084  *
1085  * This function does the same work as gtk_init() with only a single
1086  * change: It does not terminate the program if the commandline
1087  * arguments couldn’t be parsed or the windowing system can’t be
1088  * initialized. Instead it returns %FALSE on failure.
1089  *
1090  * This way the application can fall back to some other means of
1091  * communication with the user - for example a curses or command line
1092  * interface.
1093  *
1094  * Note that calling any GTK function or instantiating any GTK type after
1095  * this function returns %FALSE results in undefined behavior.
1096  *
1097  * Returns: %TRUE if the commandline arguments (if any) were valid and
1098  *     the windowing system has been successfully initialized, %FALSE
1099  *     otherwise
1100  */
1101 gboolean
gtk_init_check(int * argc,char *** argv)1102 gtk_init_check (int    *argc,
1103                 char ***argv)
1104 {
1105   gboolean ret;
1106 
1107   if (!gtk_parse_args (argc, argv))
1108     return FALSE;
1109 
1110   ret = GDK_PRIVATE_CALL (gdk_display_open_default) () != NULL;
1111 
1112   if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
1113     gtk_window_set_interactive_debugging (TRUE);
1114 
1115   return ret;
1116 }
1117 
1118 #ifdef G_PLATFORM_WIN32
1119 #undef gtk_init
1120 #endif
1121 
1122 /**
1123  * gtk_init:
1124  * @argc: (inout): Address of the `argc` parameter of
1125  *     your main() function (or 0 if @argv is %NULL). This will be changed if
1126  *     any arguments were handled.
1127  * @argv: (array length=argc) (inout) (allow-none): Address of the
1128  *     `argv` parameter of main(), or %NULL. Any options
1129  *     understood by GTK+ are stripped before return.
1130  *
1131  * Call this function before using any other GTK+ functions in your GUI
1132  * applications.  It will initialize everything needed to operate the
1133  * toolkit and parses some standard command line options.
1134  *
1135  * Although you are expected to pass the @argc, @argv parameters from main() to
1136  * this function, it is possible to pass %NULL if @argv is not available or
1137  * commandline handling is not required.
1138  *
1139  * @argc and @argv are adjusted accordingly so your own code will
1140  * never see those standard arguments.
1141  *
1142  * Note that there are some alternative ways to initialize GTK+:
1143  * if you are calling gtk_parse_args(), gtk_init_check(),
1144  * gtk_init_with_args() or g_option_context_parse() with
1145  * the option group returned by gtk_get_option_group(),
1146  * you don’t have to call gtk_init().
1147  *
1148  * And if you are using #GtkApplication, you don't have to call any of the
1149  * initialization functions either; the #GtkApplication::startup handler
1150  * does it for you.
1151  *
1152  * This function will terminate your program if it was unable to
1153  * initialize the windowing system for some reason. If you want
1154  * your program to fall back to a textual interface you want to
1155  * call gtk_init_check() instead.
1156  *
1157  * Since 2.18, GTK+ calls `signal (SIGPIPE, SIG_IGN)`
1158  * during initialization, to ignore SIGPIPE signals, since these are
1159  * almost never wanted in graphical applications. If you do need to
1160  * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1161  * but notice that other libraries (e.g. libdbus or gvfs) might do
1162  * similar things.
1163  */
1164 void
gtk_init(int * argc,char *** argv)1165 gtk_init (int *argc, char ***argv)
1166 {
1167   if (!gtk_init_check (argc, argv))
1168     {
1169       const char *display_name_arg = gdk_get_display_arg_name ();
1170       if (display_name_arg == NULL)
1171         display_name_arg = getenv("DISPLAY");
1172       g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1173       exit (1);
1174     }
1175 }
1176 
1177 #ifdef G_OS_WIN32
1178 
1179 /* This is relevant when building with gcc for Windows (MinGW),
1180  * where we want to be struct packing compatible with MSVC,
1181  * i.e. use the -mms-bitfields switch.
1182  * For Cygwin there should be no need to be compatible with MSVC,
1183  * so no need to use G_PLATFORM_WIN32.
1184  */
1185 
1186 static void
check_sizeof_GtkWindow(size_t sizeof_GtkWindow)1187 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1188 {
1189   if (sizeof_GtkWindow != sizeof (GtkWindow))
1190     g_error ("Incompatible build!\n"
1191              "The code using GTK+ thinks GtkWindow is of different\n"
1192              "size than it actually is in this build of GTK+.\n"
1193              "On Windows, this probably means that you have compiled\n"
1194              "your code with gcc without the -mms-bitfields switch,\n"
1195              "or that you are using an unsupported compiler.");
1196 }
1197 
1198 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1199  * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1200  * not. Unfortunately this wan’t noticed until after GTK+ 2.0.1. So,
1201  * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1202  * -fnative-struct still matters. GtkBox is one such.
1203  */
1204 static void
check_sizeof_GtkBox(size_t sizeof_GtkBox)1205 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1206 {
1207   if (sizeof_GtkBox != sizeof (GtkBox))
1208     g_error ("Incompatible build!\n"
1209              "The code using GTK+ thinks GtkBox is of different\n"
1210              "size than it actually is in this build of GTK+.\n"
1211              "On Windows, this probably means that you have compiled\n"
1212              "your code with gcc without the -mms-bitfields switch,\n"
1213              "or that you are using an unsupported compiler.");
1214 }
1215 
1216 /* These two functions might get more checks added later, thus pass
1217  * in the number of extra args.
1218  */
1219 void
gtk_init_abi_check(int * argc,char *** argv,int num_checks,size_t sizeof_GtkWindow,size_t sizeof_GtkBox)1220 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1221 {
1222   check_sizeof_GtkWindow (sizeof_GtkWindow);
1223   if (num_checks >= 2)
1224     check_sizeof_GtkBox (sizeof_GtkBox);
1225   gtk_init (argc, argv);
1226 }
1227 
1228 gboolean
gtk_init_check_abi_check(int * argc,char *** argv,int num_checks,size_t sizeof_GtkWindow,size_t sizeof_GtkBox)1229 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1230 {
1231   check_sizeof_GtkWindow (sizeof_GtkWindow);
1232   if (num_checks >= 2)
1233     check_sizeof_GtkBox (sizeof_GtkBox);
1234   return gtk_init_check (argc, argv);
1235 }
1236 
1237 #endif
1238 
1239 /**
1240  * gtk_get_locale_direction:
1241  *
1242  * Get the direction of the current locale. This is the expected
1243  * reading direction for text and UI.
1244  *
1245  * This function depends on the current locale being set with
1246  * setlocale() and will default to setting the %GTK_TEXT_DIR_LTR
1247  * direction otherwise. %GTK_TEXT_DIR_NONE will never be returned.
1248  *
1249  * GTK+ sets the default text direction according to the locale
1250  * during gtk_init(), and you should normally use
1251  * gtk_widget_get_direction() or gtk_widget_get_default_direction()
1252  * to obtain the current direcion.
1253  *
1254  * This function is only needed rare cases when the locale is
1255  * changed after GTK+ has already been initialized. In this case,
1256  * you can use it to update the default text direction as follows:
1257  *
1258  * |[<!-- language="C" -->
1259  * setlocale (LC_ALL, new_locale);
1260  * direction = gtk_get_locale_direction ();
1261  * gtk_widget_set_default_direction (direction);
1262  * ]|
1263  *
1264  * Returns: the #GtkTextDirection of the current locale
1265  *
1266  * Since: 3.12
1267  */
1268 GtkTextDirection
gtk_get_locale_direction(void)1269 gtk_get_locale_direction (void)
1270 {
1271   /* Translate to default:RTL if you want your widgets
1272    * to be RTL, otherwise translate to default:LTR.
1273    * Do *not* translate it to "predefinito:LTR", if it
1274    * it isn't default:LTR or default:RTL it will not work
1275    */
1276   gchar            *e   = _("default:LTR");
1277   GtkTextDirection  dir = GTK_TEXT_DIR_LTR;
1278 
1279   if (g_strcmp0 (e, "default:RTL") == 0)
1280     dir = GTK_TEXT_DIR_RTL;
1281   else if (g_strcmp0 (e, "default:LTR") != 0)
1282     g_warning ("Whoever translated default:LTR did so wrongly. Defaulting to LTR.");
1283 
1284   return dir;
1285 }
1286 
1287 /**
1288  * gtk_get_default_language:
1289  *
1290  * Returns the #PangoLanguage for the default language currently in
1291  * effect. (Note that this can change over the life of an
1292  * application.) The default language is derived from the current
1293  * locale. It determines, for example, whether GTK+ uses the
1294  * right-to-left or left-to-right text direction.
1295  *
1296  * This function is equivalent to pango_language_get_default().
1297  * See that function for details.
1298  *
1299  * Returns: (transfer none): the default language as a #PangoLanguage,
1300  *     must not be freed
1301  */
1302 PangoLanguage *
gtk_get_default_language(void)1303 gtk_get_default_language (void)
1304 {
1305   return pango_language_get_default ();
1306 }
1307 
1308 /**
1309  * gtk_main:
1310  *
1311  * Runs the main loop until gtk_main_quit() is called.
1312  *
1313  * You can nest calls to gtk_main(). In that case gtk_main_quit()
1314  * will make the innermost invocation of the main loop return.
1315  */
1316 void
gtk_main(void)1317 gtk_main (void)
1318 {
1319   GMainLoop *loop;
1320 
1321   gtk_main_loop_level++;
1322 
1323   loop = g_main_loop_new (NULL, TRUE);
1324   main_loops = g_slist_prepend (main_loops, loop);
1325 
1326   if (g_main_loop_is_running (main_loops->data))
1327     {
1328       gdk_threads_leave ();
1329       g_main_loop_run (loop);
1330       gdk_threads_enter ();
1331 
1332       G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1333       gdk_flush ();
1334       G_GNUC_END_IGNORE_DEPRECATIONS;
1335     }
1336 
1337   main_loops = g_slist_remove (main_loops, loop);
1338 
1339   g_main_loop_unref (loop);
1340 
1341   gtk_main_loop_level--;
1342 
1343   if (gtk_main_loop_level == 0)
1344     {
1345       /* Keep this section in sync with gtk_application_shutdown() */
1346 
1347       /* Try storing all clipboard data we have */
1348       _gtk_clipboard_store_all ();
1349 
1350       /* Synchronize the recent manager singleton */
1351       _gtk_recent_manager_sync ();
1352     }
1353 }
1354 
1355 /**
1356  * gtk_main_level:
1357  *
1358  * Asks for the current nesting level of the main loop.
1359  *
1360  * Returns: the nesting level of the current invocation
1361  *     of the main loop
1362  */
1363 guint
gtk_main_level(void)1364 gtk_main_level (void)
1365 {
1366   return gtk_main_loop_level;
1367 }
1368 
1369 /**
1370  * gtk_main_quit:
1371  *
1372  * Makes the innermost invocation of the main loop return
1373  * when it regains control.
1374  */
1375 void
gtk_main_quit(void)1376 gtk_main_quit (void)
1377 {
1378   g_return_if_fail (main_loops != NULL);
1379 
1380   g_main_loop_quit (main_loops->data);
1381 }
1382 
1383 /**
1384  * gtk_events_pending:
1385  *
1386  * Checks if any events are pending.
1387  *
1388  * This can be used to update the UI and invoke timeouts etc.
1389  * while doing some time intensive computation.
1390  *
1391  * ## Updating the UI during a long computation
1392  *
1393  * |[<!-- language="C" -->
1394  *  // computation going on...
1395  *
1396  *  while (gtk_events_pending ())
1397  *    gtk_main_iteration ();
1398  *
1399  *  // ...computation continued
1400  * ]|
1401  *
1402  * Returns: %TRUE if any events are pending, %FALSE otherwise
1403  */
1404 gboolean
gtk_events_pending(void)1405 gtk_events_pending (void)
1406 {
1407   gboolean result;
1408 
1409   gdk_threads_leave ();
1410   result = g_main_context_pending (NULL);
1411   gdk_threads_enter ();
1412 
1413   return result;
1414 }
1415 
1416 /**
1417  * gtk_main_iteration:
1418  *
1419  * Runs a single iteration of the mainloop.
1420  *
1421  * If no events are waiting to be processed GTK+ will block
1422  * until the next event is noticed. If you don’t want to block
1423  * look at gtk_main_iteration_do() or check if any events are
1424  * pending with gtk_events_pending() first.
1425  *
1426  * Returns: %TRUE if gtk_main_quit() has been called for the
1427  *     innermost mainloop
1428  */
1429 gboolean
gtk_main_iteration(void)1430 gtk_main_iteration (void)
1431 {
1432   gdk_threads_leave ();
1433   g_main_context_iteration (NULL, TRUE);
1434   gdk_threads_enter ();
1435 
1436   if (main_loops)
1437     return !g_main_loop_is_running (main_loops->data);
1438   else
1439     return TRUE;
1440 }
1441 
1442 /**
1443  * gtk_main_iteration_do:
1444  * @blocking: %TRUE if you want GTK+ to block if no events are pending
1445  *
1446  * Runs a single iteration of the mainloop.
1447  * If no events are available either return or block depending on
1448  * the value of @blocking.
1449  *
1450  * Returns: %TRUE if gtk_main_quit() has been called for the
1451  *     innermost mainloop
1452  */
1453 gboolean
gtk_main_iteration_do(gboolean blocking)1454 gtk_main_iteration_do (gboolean blocking)
1455 {
1456   gdk_threads_leave ();
1457   g_main_context_iteration (NULL, blocking);
1458   gdk_threads_enter ();
1459 
1460   if (main_loops)
1461     return !g_main_loop_is_running (main_loops->data);
1462   else
1463     return TRUE;
1464 }
1465 
1466 static void
rewrite_events_translate(GdkWindow * old_window,GdkWindow * new_window,gdouble * x,gdouble * y)1467 rewrite_events_translate (GdkWindow *old_window,
1468                           GdkWindow *new_window,
1469                           gdouble   *x,
1470                           gdouble   *y)
1471 {
1472   gint old_origin_x, old_origin_y;
1473   gint new_origin_x, new_origin_y;
1474 
1475   gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1476   gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1477 
1478   *x += old_origin_x - new_origin_x;
1479   *y += old_origin_y - new_origin_y;
1480 }
1481 
1482 static GdkEvent *
rewrite_event_for_window(GdkEvent * event,GdkWindow * new_window)1483 rewrite_event_for_window (GdkEvent  *event,
1484                           GdkWindow *new_window)
1485 {
1486   event = gdk_event_copy (event);
1487 
1488   switch (event->type)
1489     {
1490     case GDK_SCROLL:
1491       rewrite_events_translate (event->any.window,
1492                                 new_window,
1493                                 &event->scroll.x, &event->scroll.y);
1494       break;
1495     case GDK_BUTTON_PRESS:
1496     case GDK_2BUTTON_PRESS:
1497     case GDK_3BUTTON_PRESS:
1498     case GDK_BUTTON_RELEASE:
1499       rewrite_events_translate (event->any.window,
1500                                 new_window,
1501                                 &event->button.x, &event->button.y);
1502       break;
1503     case GDK_MOTION_NOTIFY:
1504       rewrite_events_translate (event->any.window,
1505                                 new_window,
1506                                 &event->motion.x, &event->motion.y);
1507       break;
1508     case GDK_TOUCH_BEGIN:
1509     case GDK_TOUCH_UPDATE:
1510     case GDK_TOUCH_END:
1511     case GDK_TOUCH_CANCEL:
1512       rewrite_events_translate (event->any.window,
1513                                 new_window,
1514                                 &event->touch.x, &event->touch.y);
1515       break;
1516     case GDK_TOUCHPAD_SWIPE:
1517       rewrite_events_translate (event->any.window,
1518                                 new_window,
1519                                 &event->touchpad_swipe.x,
1520                                 &event->touchpad_swipe.y);
1521       break;
1522     case GDK_TOUCHPAD_PINCH:
1523       rewrite_events_translate (event->any.window,
1524                                 new_window,
1525                                 &event->touchpad_pinch.x,
1526                                 &event->touchpad_pinch.y);
1527       break;
1528     case GDK_KEY_PRESS:
1529     case GDK_KEY_RELEASE:
1530     case GDK_PROXIMITY_IN:
1531     case GDK_PROXIMITY_OUT:
1532       break;
1533 
1534     default:
1535       return event;
1536     }
1537 
1538   g_object_unref (event->any.window);
1539   event->any.window = g_object_ref (new_window);
1540 
1541   return event;
1542 }
1543 
1544 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1545  * then what X11 does is deliver the event normally if it was going to this
1546  * client, otherwise, delivers it in terms of the grab window. This function
1547  * rewrites events to the effect that events going to the same window group
1548  * are delivered normally, otherwise, the event is delivered in terms of the
1549  * grab window.
1550  */
1551 static GdkEvent *
rewrite_event_for_grabs(GdkEvent * event)1552 rewrite_event_for_grabs (GdkEvent *event)
1553 {
1554   GdkWindow *grab_window;
1555   GtkWidget *event_widget, *grab_widget;
1556   gpointer grab_widget_ptr;
1557   gboolean owner_events;
1558   GdkDisplay *display;
1559   GdkDevice *device;
1560 
1561   switch (event->type)
1562     {
1563     case GDK_SCROLL:
1564     case GDK_BUTTON_PRESS:
1565     case GDK_2BUTTON_PRESS:
1566     case GDK_3BUTTON_PRESS:
1567     case GDK_BUTTON_RELEASE:
1568     case GDK_MOTION_NOTIFY:
1569     case GDK_PROXIMITY_IN:
1570     case GDK_PROXIMITY_OUT:
1571     case GDK_KEY_PRESS:
1572     case GDK_KEY_RELEASE:
1573     case GDK_TOUCH_BEGIN:
1574     case GDK_TOUCH_UPDATE:
1575     case GDK_TOUCH_END:
1576     case GDK_TOUCH_CANCEL:
1577     case GDK_TOUCHPAD_SWIPE:
1578     case GDK_TOUCHPAD_PINCH:
1579       display = gdk_window_get_display (event->any.window);
1580       device = gdk_event_get_device (event);
1581 
1582       if (!GDK_PRIVATE_CALL (gdk_device_grab_info) (display, device, &grab_window, &owner_events) ||
1583           !owner_events)
1584         return NULL;
1585       break;
1586     default:
1587       return NULL;
1588     }
1589 
1590   event_widget = gtk_get_event_widget (event);
1591   gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1592   grab_widget = grab_widget_ptr;
1593 
1594   if (grab_widget &&
1595       gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1596     return rewrite_event_for_window (event, grab_window);
1597   else
1598     return NULL;
1599 }
1600 
1601 static GtkWidget *
widget_get_popover_ancestor(GtkWidget * widget,GtkWindow * window)1602 widget_get_popover_ancestor (GtkWidget *widget,
1603                              GtkWindow *window)
1604 {
1605   GtkWidget *parent = gtk_widget_get_parent (widget);
1606 
1607   while (parent && parent != GTK_WIDGET (window))
1608     {
1609       widget = parent;
1610       parent = gtk_widget_get_parent (widget);
1611     }
1612 
1613   if (!parent || parent != GTK_WIDGET (window))
1614     return NULL;
1615 
1616   if (_gtk_window_is_popover_widget (GTK_WINDOW (window), widget))
1617     return widget;
1618 
1619   return NULL;
1620 }
1621 
1622 static gboolean
check_event_in_child_popover(GtkWidget * event_widget,GtkWidget * grab_widget)1623 check_event_in_child_popover (GtkWidget *event_widget,
1624                               GtkWidget *grab_widget)
1625 {
1626   GtkWidget *window, *popover = NULL, *popover_parent = NULL;
1627 
1628   if (grab_widget == event_widget)
1629     return FALSE;
1630 
1631   window = gtk_widget_get_ancestor (event_widget, GTK_TYPE_WINDOW);
1632 
1633   if (!window)
1634     return FALSE;
1635 
1636   popover = widget_get_popover_ancestor (event_widget, GTK_WINDOW (window));
1637 
1638   if (!popover)
1639     return FALSE;
1640 
1641   popover_parent = _gtk_window_get_popover_parent (GTK_WINDOW (window), popover);
1642 
1643   if (!popover_parent)
1644     return FALSE;
1645 
1646   return (popover_parent == grab_widget || gtk_widget_is_ancestor (popover_parent, grab_widget));
1647 }
1648 
1649 /**
1650  * gtk_main_do_event:
1651  * @event: An event to process (normally passed by GDK)
1652  *
1653  * Processes a single GDK event.
1654  *
1655  * This is public only to allow filtering of events between GDK and GTK+.
1656  * You will not usually need to call this function directly.
1657  *
1658  * While you should not call this function directly, you might want to
1659  * know how exactly events are handled. So here is what this function
1660  * does with the event:
1661  *
1662  * 1. Compress enter/leave notify events. If the event passed build an
1663  *    enter/leave pair together with the next event (peeked from GDK), both
1664  *    events are thrown away. This is to avoid a backlog of (de-)highlighting
1665  *    widgets crossed by the pointer.
1666  *
1667  * 2. Find the widget which got the event. If the widget can’t be determined
1668  *    the event is thrown away unless it belongs to a INCR transaction.
1669  *
1670  * 3. Then the event is pushed onto a stack so you can query the currently
1671  *    handled event with gtk_get_current_event().
1672  *
1673  * 4. The event is sent to a widget. If a grab is active all events for widgets
1674  *    that are not in the contained in the grab widget are sent to the latter
1675  *    with a few exceptions:
1676  *    - Deletion and destruction events are still sent to the event widget for
1677  *      obvious reasons.
1678  *    - Events which directly relate to the visual representation of the event
1679  *      widget.
1680  *    - Leave events are delivered to the event widget if there was an enter
1681  *      event delivered to it before without the paired leave event.
1682  *    - Drag events are not redirected because it is unclear what the semantics
1683  *      of that would be.
1684  *    Another point of interest might be that all key events are first passed
1685  *    through the key snooper functions if there are any. Read the description
1686  *    of gtk_key_snooper_install() if you need this feature.
1687  *
1688  * 5. After finishing the delivery the event is popped from the event stack.
1689  */
1690 void
gtk_main_do_event(GdkEvent * event)1691 gtk_main_do_event (GdkEvent *event)
1692 {
1693   GtkWidget *event_widget;
1694   GtkWidget *grab_widget = NULL;
1695   GtkWidget *topmost_widget = NULL;
1696   GtkWindowGroup *window_group;
1697   GdkEvent *rewritten_event = NULL;
1698   GdkDevice *device;
1699   GList *tmp_list;
1700 
1701   if (event->type == GDK_SETTING)
1702     {
1703       _gtk_settings_handle_event (&event->setting);
1704       return;
1705     }
1706 
1707   if (event->type == GDK_OWNER_CHANGE)
1708     {
1709       _gtk_clipboard_handle_event (&event->owner_change);
1710       return;
1711     }
1712 
1713   /* Find the widget which got the event. We store the widget
1714    * in the user_data field of GdkWindow's. Ignore the event
1715    * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1716    * events which are handled specially. Though this happens rarely,
1717    * bogus events can occur for e.g. destroyed GdkWindows.
1718    */
1719   event_widget = gtk_get_event_widget (event);
1720   if (!event_widget)
1721     {
1722       /* To handle selection INCR transactions, we select
1723        * PropertyNotify events on the requestor window and create
1724        * a corresponding (fake) GdkWindow so that events get here.
1725        * There won't be a widget though, so we have to handle
1726        * them specially
1727        */
1728       if (event->type == GDK_PROPERTY_NOTIFY)
1729         _gtk_selection_incr_event (event->any.window,
1730                                    &event->property);
1731 
1732       return;
1733     }
1734 
1735   /* If pointer or keyboard grabs are in effect, munge the events
1736    * so that each window group looks like a separate app.
1737    */
1738   rewritten_event = rewrite_event_for_grabs (event);
1739   if (rewritten_event)
1740     {
1741       event = rewritten_event;
1742       event_widget = gtk_get_event_widget (event);
1743     }
1744 
1745   /* Push the event onto a stack of current events for
1746    * gtk_current_event_get().
1747    */
1748   current_events = g_list_prepend (current_events, event);
1749 
1750   window_group = gtk_main_get_window_group (event_widget);
1751   device = gdk_event_get_device (event);
1752 
1753   /* check whether there is a (device) grab in effect... */
1754   if (device)
1755     grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1756 
1757   if (!grab_widget)
1758     grab_widget = gtk_window_group_get_current_grab (window_group);
1759 
1760   if (GTK_IS_WINDOW (event_widget) ||
1761       (grab_widget && grab_widget != event_widget &&
1762        !gtk_widget_is_ancestor (event_widget, grab_widget)))
1763     {
1764       /* Ignore event if we got a grab on another toplevel */
1765       if (!grab_widget ||
1766           gtk_widget_get_toplevel (event_widget) == gtk_widget_get_toplevel (grab_widget))
1767         {
1768           if (_gtk_window_check_handle_wm_event (event))
1769             goto cleanup;
1770         }
1771     }
1772 
1773   /* Find out the topmost widget where captured event propagation
1774    * should start, which is the widget holding the GTK+ grab
1775    * if any, otherwise it's left NULL and events are emitted
1776    * from the toplevel (or topmost parentless parent).
1777    */
1778   if (grab_widget)
1779     topmost_widget = grab_widget;
1780 
1781   /* If the grab widget is an ancestor of the event widget
1782    * then we send the event to the original event widget.
1783    * This is the key to implementing modality.
1784    */
1785   if (!grab_widget ||
1786       ((gtk_widget_is_sensitive (event_widget) || event->type == GDK_SCROLL) &&
1787        gtk_widget_is_ancestor (event_widget, grab_widget)))
1788     grab_widget = event_widget;
1789 
1790   /* popovers are not really a "child" of their "parent" in the widget/window
1791    * hierarchy sense, we however want to interact with popovers spawn by widgets
1792    * within grab_widget. If this is the case, we let the event go through
1793    * unaffected by the grab.
1794    */
1795   if (check_event_in_child_popover (event_widget, grab_widget))
1796     grab_widget = event_widget;
1797 
1798   /* If the widget receiving events is actually blocked by another
1799    * device GTK+ grab
1800    */
1801   if (device &&
1802       _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1803     goto cleanup;
1804 
1805   /* Not all events get sent to the grabbing widget.
1806    * The delete, destroy, expose, focus change and resize
1807    * events still get sent to the event widget because
1808    * 1) these events have no meaning for the grabbing widget
1809    * and 2) redirecting these events to the grabbing widget
1810    * could cause the display to be messed up.
1811    *
1812    * Drag events are also not redirected, since it isn't
1813    * clear what the semantics of that would be.
1814    */
1815   switch (event->type)
1816     {
1817     case GDK_NOTHING:
1818       break;
1819 
1820     case GDK_DELETE:
1821       g_object_ref (event_widget);
1822       if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1823           !gtk_widget_event (event_widget, event))
1824         gtk_widget_destroy (event_widget);
1825       g_object_unref (event_widget);
1826       break;
1827 
1828     case GDK_DESTROY:
1829       /* Unexpected GDK_DESTROY from the outside, ignore for
1830        * child windows, handle like a GDK_DELETE for toplevels
1831        */
1832       if (!gtk_widget_get_parent (event_widget))
1833         {
1834           g_object_ref (event_widget);
1835           if (!gtk_widget_event (event_widget, event) &&
1836               gtk_widget_get_realized (event_widget))
1837             gtk_widget_destroy (event_widget);
1838           g_object_unref (event_widget);
1839         }
1840       break;
1841 
1842     case GDK_EXPOSE:
1843       if (event->any.window)
1844         gtk_widget_render (event_widget, event->any.window, event->expose.region);
1845       break;
1846 
1847     case GDK_PROPERTY_NOTIFY:
1848     case GDK_FOCUS_CHANGE:
1849     case GDK_CONFIGURE:
1850     case GDK_MAP:
1851     case GDK_UNMAP:
1852     case GDK_SELECTION_CLEAR:
1853     case GDK_SELECTION_REQUEST:
1854     case GDK_SELECTION_NOTIFY:
1855     case GDK_CLIENT_EVENT:
1856     case GDK_VISIBILITY_NOTIFY:
1857     case GDK_WINDOW_STATE:
1858     case GDK_GRAB_BROKEN:
1859     case GDK_DAMAGE:
1860       if (!_gtk_widget_captured_event (event_widget, event))
1861         gtk_widget_event (event_widget, event);
1862       break;
1863 
1864     case GDK_KEY_PRESS:
1865     case GDK_KEY_RELEASE:
1866       if (gtk_invoke_key_snoopers (grab_widget, event))
1867         break;
1868 
1869       /* make focus visible in a window that receives a key event */
1870       {
1871         GtkWidget *window;
1872 
1873         window = gtk_widget_get_toplevel (grab_widget);
1874         if (GTK_IS_WINDOW (window))
1875           gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
1876       }
1877 
1878       /* Catch alt press to enable auto-mnemonics;
1879        * menus are handled elsewhere
1880        * FIXME: this does not work with mnemonic modifiers other than Alt
1881        */
1882       if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1883           ((event->key.state & (gtk_accelerator_get_default_mod_mask ()) & ~(GDK_RELEASE_MASK|GDK_MOD1_MASK)) == 0) &&
1884           !GTK_IS_MENU_SHELL (grab_widget))
1885         {
1886           gboolean mnemonics_visible;
1887           GtkWidget *window;
1888 
1889           mnemonics_visible = (event->type == GDK_KEY_PRESS);
1890 
1891           window = gtk_widget_get_toplevel (grab_widget);
1892           if (GTK_IS_WINDOW (window))
1893             {
1894               if (mnemonics_visible)
1895                 _gtk_window_schedule_mnemonics_visible (GTK_WINDOW (window));
1896               else
1897                 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), FALSE);
1898             }
1899         }
1900       /* else fall through */
1901     case GDK_SCROLL:
1902     case GDK_BUTTON_PRESS:
1903     case GDK_2BUTTON_PRESS:
1904     case GDK_3BUTTON_PRESS:
1905     case GDK_TOUCH_BEGIN:
1906     case GDK_MOTION_NOTIFY:
1907     case GDK_BUTTON_RELEASE:
1908     case GDK_PROXIMITY_IN:
1909     case GDK_PROXIMITY_OUT:
1910     case GDK_TOUCH_UPDATE:
1911     case GDK_TOUCH_END:
1912     case GDK_TOUCH_CANCEL:
1913     case GDK_TOUCHPAD_SWIPE:
1914     case GDK_TOUCHPAD_PINCH:
1915     case GDK_PAD_BUTTON_PRESS:
1916     case GDK_PAD_BUTTON_RELEASE:
1917     case GDK_PAD_RING:
1918     case GDK_PAD_STRIP:
1919     case GDK_PAD_GROUP_MODE:
1920       if (!_gtk_propagate_captured_event (grab_widget, event, topmost_widget))
1921         gtk_propagate_event (grab_widget, event);
1922       break;
1923 
1924     case GDK_ENTER_NOTIFY:
1925     case GDK_LEAVE_NOTIFY:
1926       if (gtk_widget_is_sensitive (grab_widget) &&
1927           !_gtk_propagate_captured_event (grab_widget, event, topmost_widget))
1928         gtk_widget_event (grab_widget, event);
1929       break;
1930 
1931     case GDK_DRAG_STATUS:
1932     case GDK_DROP_FINISHED:
1933       _gtk_drag_source_handle_event (event_widget, event);
1934       break;
1935     case GDK_DRAG_ENTER:
1936     case GDK_DRAG_LEAVE:
1937     case GDK_DRAG_MOTION:
1938     case GDK_DROP_START:
1939       _gtk_drag_dest_handle_event (event_widget, event);
1940       break;
1941     default:
1942       g_assert_not_reached ();
1943       break;
1944     }
1945 
1946   if (event->type == GDK_ENTER_NOTIFY
1947       || event->type == GDK_LEAVE_NOTIFY
1948       || event->type == GDK_BUTTON_PRESS
1949       || event->type == GDK_2BUTTON_PRESS
1950       || event->type == GDK_3BUTTON_PRESS
1951       || event->type == GDK_KEY_PRESS
1952       || event->type == GDK_DRAG_ENTER
1953       || event->type == GDK_GRAB_BROKEN
1954       || event->type == GDK_MOTION_NOTIFY
1955       || event->type == GDK_TOUCH_UPDATE
1956       || event->type == GDK_SCROLL)
1957     {
1958       _gtk_tooltip_handle_event (event);
1959     }
1960 
1961  cleanup:
1962   tmp_list = current_events;
1963   current_events = g_list_remove_link (current_events, tmp_list);
1964   g_list_free_1 (tmp_list);
1965 
1966   if (rewritten_event)
1967     gdk_event_free (rewritten_event);
1968 }
1969 
1970 /**
1971  * gtk_true:
1972  *
1973  * All this function does it to return %TRUE.
1974  *
1975  * This can be useful for example if you want to inhibit the deletion
1976  * of a window. Of course you should not do this as the user expects
1977  * a reaction from clicking the close icon of the window...
1978  *
1979  * ## A persistent window
1980  *
1981  * |[<!-- language="C" -->
1982  * #include <gtk/gtk.h>
1983  *
1984  * int
1985  * main (int argc, char **argv)
1986  * {
1987  *   GtkWidget *win, *but;
1988  *   const char *text = "Close yourself. I mean it!";
1989  *
1990  *   gtk_init (&argc, &argv);
1991  *
1992  *   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1993  *   g_signal_connect (win,
1994  *                     "delete-event",
1995  *                     G_CALLBACK (gtk_true),
1996  *                     NULL);
1997  *   g_signal_connect (win, "destroy",
1998  *                     G_CALLBACK (gtk_main_quit),
1999  *                     NULL);
2000  *
2001  *   but = gtk_button_new_with_label (text);
2002  *   g_signal_connect_swapped (but, "clicked",
2003  *                             G_CALLBACK (gtk_object_destroy),
2004  *                             win);
2005  *   gtk_container_add (GTK_CONTAINER (win), but);
2006  *
2007  *   gtk_widget_show_all (win);
2008  *
2009  *   gtk_main ();
2010  *
2011  *   return 0;
2012  * }
2013  * ]|
2014  *
2015  * Returns: %TRUE
2016  */
2017 gboolean
gtk_true(void)2018 gtk_true (void)
2019 {
2020   return TRUE;
2021 }
2022 
2023 /**
2024  * gtk_false:
2025  *
2026  * Analogical to gtk_true(), this function does nothing
2027  * but always returns %FALSE.
2028  *
2029  * Returns: %FALSE
2030  */
2031 gboolean
gtk_false(void)2032 gtk_false (void)
2033 {
2034   return FALSE;
2035 }
2036 
2037 static GtkWindowGroup *
gtk_main_get_window_group(GtkWidget * widget)2038 gtk_main_get_window_group (GtkWidget *widget)
2039 {
2040   GtkWidget *toplevel = NULL;
2041 
2042   if (widget)
2043     toplevel = gtk_widget_get_toplevel (widget);
2044 
2045   if (GTK_IS_WINDOW (toplevel))
2046     return gtk_window_get_group (GTK_WINDOW (toplevel));
2047   else
2048     return gtk_window_get_group (NULL);
2049 }
2050 
2051 typedef struct
2052 {
2053   GtkWidget *old_grab_widget;
2054   GtkWidget *new_grab_widget;
2055   gboolean   was_grabbed;
2056   gboolean   is_grabbed;
2057   gboolean   from_grab;
2058   GList     *notified_windows;
2059   GdkDevice *device;
2060 } GrabNotifyInfo;
2061 
2062 static void
synth_crossing_for_grab_notify(GtkWidget * from,GtkWidget * to,GrabNotifyInfo * info,GList * devices,GdkCrossingMode mode)2063 synth_crossing_for_grab_notify (GtkWidget       *from,
2064                                 GtkWidget       *to,
2065                                 GrabNotifyInfo  *info,
2066                                 GList           *devices,
2067                                 GdkCrossingMode  mode)
2068 {
2069   while (devices)
2070     {
2071       GdkDevice *device = devices->data;
2072       GdkWindow *from_window, *to_window;
2073 
2074       /* Do not propagate events more than once to
2075        * the same windows if non-multidevice aware.
2076        */
2077       if (!from)
2078         from_window = NULL;
2079       else
2080         {
2081           from_window = _gtk_widget_get_device_window (from, device);
2082 
2083           if (from_window &&
2084               !gdk_window_get_support_multidevice (from_window) &&
2085               g_list_find (info->notified_windows, from_window))
2086             from_window = NULL;
2087         }
2088 
2089       if (!to)
2090         to_window = NULL;
2091       else
2092         {
2093           to_window = _gtk_widget_get_device_window (to, device);
2094 
2095           if (to_window &&
2096               !gdk_window_get_support_multidevice (to_window) &&
2097               g_list_find (info->notified_windows, to_window))
2098             to_window = NULL;
2099         }
2100 
2101       if (from_window || to_window)
2102         {
2103           _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
2104                                            (to_window) ? to : NULL,
2105                                            device, mode);
2106 
2107           if (from_window)
2108             info->notified_windows = g_list_prepend (info->notified_windows, from_window);
2109 
2110           if (to_window)
2111             info->notified_windows = g_list_prepend (info->notified_windows, to_window);
2112         }
2113 
2114       devices = devices->next;
2115     }
2116 }
2117 
2118 static void
gtk_grab_notify_foreach(GtkWidget * child,gpointer data)2119 gtk_grab_notify_foreach (GtkWidget *child,
2120                          gpointer   data)
2121 {
2122   GrabNotifyInfo *info = data;
2123   gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
2124   GList *devices;
2125 
2126   was_grabbed = info->was_grabbed;
2127   is_grabbed = info->is_grabbed;
2128 
2129   info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
2130   info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
2131 
2132   was_shadowed = info->old_grab_widget && !info->was_grabbed;
2133   is_shadowed = info->new_grab_widget && !info->is_grabbed;
2134 
2135   g_object_ref (child);
2136 
2137   if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
2138     gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
2139 
2140   if (info->device &&
2141       _gtk_widget_get_device_window (child, info->device))
2142     {
2143       /* Device specified and is on widget */
2144       devices = g_list_prepend (NULL, info->device);
2145     }
2146   else
2147     devices = _gtk_widget_list_devices (child);
2148 
2149   if (is_shadowed)
2150     {
2151       _gtk_widget_set_shadowed (child, TRUE);
2152       if (!was_shadowed && devices &&
2153           gtk_widget_is_sensitive (child))
2154         synth_crossing_for_grab_notify (child, info->new_grab_widget,
2155                                         info, devices,
2156                                         GDK_CROSSING_GTK_GRAB);
2157     }
2158   else
2159     {
2160       _gtk_widget_set_shadowed (child, FALSE);
2161       if (was_shadowed && devices &&
2162           gtk_widget_is_sensitive (child))
2163         synth_crossing_for_grab_notify (info->old_grab_widget, child,
2164                                         info, devices,
2165                                         info->from_grab ? GDK_CROSSING_GTK_GRAB :
2166                                         GDK_CROSSING_GTK_UNGRAB);
2167     }
2168 
2169   if (was_shadowed != is_shadowed)
2170     _gtk_widget_grab_notify (child, was_shadowed);
2171 
2172   g_object_unref (child);
2173   g_list_free (devices);
2174 
2175   info->was_grabbed = was_grabbed;
2176   info->is_grabbed = is_grabbed;
2177 }
2178 
2179 static void
gtk_grab_notify(GtkWindowGroup * group,GdkDevice * device,GtkWidget * old_grab_widget,GtkWidget * new_grab_widget,gboolean from_grab)2180 gtk_grab_notify (GtkWindowGroup *group,
2181                  GdkDevice      *device,
2182                  GtkWidget      *old_grab_widget,
2183                  GtkWidget      *new_grab_widget,
2184                  gboolean        from_grab)
2185 {
2186   GList *toplevels;
2187   GrabNotifyInfo info = { 0 };
2188 
2189   if (old_grab_widget == new_grab_widget)
2190     return;
2191 
2192   info.old_grab_widget = old_grab_widget;
2193   info.new_grab_widget = new_grab_widget;
2194   info.from_grab = from_grab;
2195   info.device = device;
2196 
2197   g_object_ref (group);
2198 
2199   toplevels = gtk_window_list_toplevels ();
2200   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
2201 
2202   while (toplevels)
2203     {
2204       GtkWindow *toplevel = toplevels->data;
2205       toplevels = g_list_delete_link (toplevels, toplevels);
2206 
2207       info.was_grabbed = FALSE;
2208       info.is_grabbed = FALSE;
2209 
2210       if (group == gtk_window_get_group (toplevel))
2211         gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
2212       g_object_unref (toplevel);
2213     }
2214 
2215   g_list_free (info.notified_windows);
2216   g_object_unref (group);
2217 }
2218 
2219 /**
2220  * gtk_grab_add: (method)
2221  * @widget: The widget that grabs keyboard and pointer events
2222  *
2223  * Makes @widget the current grabbed widget.
2224  *
2225  * This means that interaction with other widgets in the same
2226  * application is blocked and mouse as well as keyboard events
2227  * are delivered to this widget.
2228  *
2229  * If @widget is not sensitive, it is not set as the current
2230  * grabbed widget and this function does nothing.
2231  */
2232 void
gtk_grab_add(GtkWidget * widget)2233 gtk_grab_add (GtkWidget *widget)
2234 {
2235   GtkWindowGroup *group;
2236   GtkWidget *old_grab_widget;
2237 
2238   g_return_if_fail (widget != NULL);
2239 
2240   if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2241     {
2242       _gtk_widget_set_has_grab (widget, TRUE);
2243 
2244       group = gtk_main_get_window_group (widget);
2245 
2246       old_grab_widget = gtk_window_group_get_current_grab (group);
2247 
2248       g_object_ref (widget);
2249       _gtk_window_group_add_grab (group, widget);
2250 
2251       gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2252     }
2253 }
2254 
2255 /**
2256  * gtk_grab_get_current:
2257  *
2258  * Queries the current grab of the default window group.
2259  *
2260  * Returns: (transfer none) (nullable): The widget which currently
2261  *     has the grab or %NULL if no grab is active
2262  */
2263 GtkWidget*
gtk_grab_get_current(void)2264 gtk_grab_get_current (void)
2265 {
2266   GtkWindowGroup *group;
2267 
2268   group = gtk_main_get_window_group (NULL);
2269 
2270   return gtk_window_group_get_current_grab (group);
2271 }
2272 
2273 /**
2274  * gtk_grab_remove: (method)
2275  * @widget: The widget which gives up the grab
2276  *
2277  * Removes the grab from the given widget.
2278  *
2279  * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2280  *
2281  * If @widget does not have the grab, this function does nothing.
2282  */
2283 void
gtk_grab_remove(GtkWidget * widget)2284 gtk_grab_remove (GtkWidget *widget)
2285 {
2286   GtkWindowGroup *group;
2287   GtkWidget *new_grab_widget;
2288 
2289   g_return_if_fail (widget != NULL);
2290 
2291   if (gtk_widget_has_grab (widget))
2292     {
2293       _gtk_widget_set_has_grab (widget, FALSE);
2294 
2295       group = gtk_main_get_window_group (widget);
2296       _gtk_window_group_remove_grab (group, widget);
2297       new_grab_widget = gtk_window_group_get_current_grab (group);
2298 
2299       gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2300 
2301       g_object_unref (widget);
2302     }
2303 }
2304 
2305 /**
2306  * gtk_device_grab_add:
2307  * @widget: a #GtkWidget
2308  * @device: a #GdkDevice to grab on.
2309  * @block_others: %TRUE to prevent other devices to interact with @widget.
2310  *
2311  * Adds a GTK+ grab on @device, so all the events on @device and its
2312  * associated pointer or keyboard (if any) are delivered to @widget.
2313  * If the @block_others parameter is %TRUE, any other devices will be
2314  * unable to interact with @widget during the grab.
2315  *
2316  * Since: 3.0
2317  */
2318 void
gtk_device_grab_add(GtkWidget * widget,GdkDevice * device,gboolean block_others)2319 gtk_device_grab_add (GtkWidget *widget,
2320                      GdkDevice *device,
2321                      gboolean   block_others)
2322 {
2323   GtkWindowGroup *group;
2324   GtkWidget *old_grab_widget;
2325 
2326   g_return_if_fail (GTK_IS_WIDGET (widget));
2327   g_return_if_fail (GDK_IS_DEVICE (device));
2328 
2329   group = gtk_main_get_window_group (widget);
2330   old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2331 
2332   if (old_grab_widget != widget)
2333     _gtk_window_group_add_device_grab (group, widget, device, block_others);
2334 
2335   gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2336 }
2337 
2338 /**
2339  * gtk_device_grab_remove:
2340  * @widget: a #GtkWidget
2341  * @device: a #GdkDevice
2342  *
2343  * Removes a device grab from the given widget.
2344  *
2345  * You have to pair calls to gtk_device_grab_add() and
2346  * gtk_device_grab_remove().
2347  *
2348  * Since: 3.0
2349  */
2350 void
gtk_device_grab_remove(GtkWidget * widget,GdkDevice * device)2351 gtk_device_grab_remove (GtkWidget *widget,
2352                         GdkDevice *device)
2353 {
2354   GtkWindowGroup *group;
2355   GtkWidget *new_grab_widget;
2356 
2357   g_return_if_fail (GTK_IS_WIDGET (widget));
2358   g_return_if_fail (GDK_IS_DEVICE (device));
2359 
2360   group = gtk_main_get_window_group (widget);
2361   _gtk_window_group_remove_device_grab (group, widget, device);
2362   new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2363 
2364   gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2365 }
2366 
2367 /**
2368  * gtk_key_snooper_install: (skip)
2369  * @snooper: a #GtkKeySnoopFunc
2370  * @func_data: (closure): data to pass to @snooper
2371  *
2372  * Installs a key snooper function, which will get called on all
2373  * key events before delivering them normally.
2374  *
2375  * Returns: a unique id for this key snooper for use with
2376  *    gtk_key_snooper_remove().
2377  *
2378  * Deprecated: 3.4: Key snooping should not be done. Events should
2379  *     be handled by widgets.
2380  */
2381 guint
gtk_key_snooper_install(GtkKeySnoopFunc snooper,gpointer func_data)2382 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2383                          gpointer        func_data)
2384 {
2385   GtkKeySnooperData *data;
2386   static guint snooper_id = 1;
2387 
2388   g_return_val_if_fail (snooper != NULL, 0);
2389 
2390   data = g_new (GtkKeySnooperData, 1);
2391   data->func = snooper;
2392   data->func_data = func_data;
2393   data->id = snooper_id++;
2394   key_snoopers = g_slist_prepend (key_snoopers, data);
2395 
2396   return data->id;
2397 }
2398 
2399 /**
2400  * gtk_key_snooper_remove:
2401  * @snooper_handler_id: Identifies the key snooper to remove
2402  *
2403  * Removes the key snooper function with the given id.
2404  *
2405  * Deprecated: 3.4: Key snooping should not be done. Events should
2406  *     be handled by widgets.
2407  */
2408 void
gtk_key_snooper_remove(guint snooper_id)2409 gtk_key_snooper_remove (guint snooper_id)
2410 {
2411   GtkKeySnooperData *data = NULL;
2412   GSList *slist;
2413 
2414   slist = key_snoopers;
2415   while (slist)
2416     {
2417       data = slist->data;
2418       if (data->id == snooper_id)
2419         break;
2420 
2421       slist = slist->next;
2422       data = NULL;
2423     }
2424   if (data)
2425     {
2426       key_snoopers = g_slist_remove (key_snoopers, data);
2427       g_free (data);
2428     }
2429 }
2430 
2431 static gint
gtk_invoke_key_snoopers(GtkWidget * grab_widget,GdkEvent * event)2432 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2433                          GdkEvent  *event)
2434 {
2435   GSList *slist;
2436   gint return_val = FALSE;
2437 
2438   return_val = _gtk_accessibility_key_snooper (grab_widget, (GdkEventKey *) event);
2439 
2440   slist = key_snoopers;
2441   while (slist && !return_val)
2442     {
2443       GtkKeySnooperData *data;
2444 
2445       data = slist->data;
2446       slist = slist->next;
2447       return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2448     }
2449 
2450   return return_val;
2451 }
2452 
2453 /**
2454  * gtk_get_current_event:
2455  *
2456  * Obtains a copy of the event currently being processed by GTK+.
2457  *
2458  * For example, if you are handling a #GtkButton::clicked signal,
2459  * the current event will be the #GdkEventButton that triggered
2460  * the ::clicked signal.
2461  *
2462  * Returns: (transfer full) (nullable): a copy of the current event, or
2463  *     %NULL if there is no current event. The returned event must be
2464  *     freed with gdk_event_free().
2465  */
2466 GdkEvent*
gtk_get_current_event(void)2467 gtk_get_current_event (void)
2468 {
2469   if (current_events)
2470     return gdk_event_copy (current_events->data);
2471   else
2472     return NULL;
2473 }
2474 
2475 /**
2476  * gtk_get_current_event_time:
2477  *
2478  * If there is a current event and it has a timestamp,
2479  * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2480  *
2481  * Returns: the timestamp from the current event,
2482  *     or %GDK_CURRENT_TIME.
2483  */
2484 guint32
gtk_get_current_event_time(void)2485 gtk_get_current_event_time (void)
2486 {
2487   if (current_events)
2488     return gdk_event_get_time (current_events->data);
2489   else
2490     return GDK_CURRENT_TIME;
2491 }
2492 
2493 /**
2494  * gtk_get_current_event_state:
2495  * @state: (out): a location to store the state of the current event
2496  *
2497  * If there is a current event and it has a state field, place
2498  * that state field in @state and return %TRUE, otherwise return
2499  * %FALSE.
2500  *
2501  * Returns: %TRUE if there was a current event and it
2502  *     had a state field
2503  */
2504 gboolean
gtk_get_current_event_state(GdkModifierType * state)2505 gtk_get_current_event_state (GdkModifierType *state)
2506 {
2507   g_return_val_if_fail (state != NULL, FALSE);
2508 
2509   if (current_events)
2510     return gdk_event_get_state (current_events->data, state);
2511   else
2512     {
2513       *state = 0;
2514       return FALSE;
2515     }
2516 }
2517 
2518 /**
2519  * gtk_get_current_event_device:
2520  *
2521  * If there is a current event and it has a device, return that
2522  * device, otherwise return %NULL.
2523  *
2524  * Returns: (transfer none) (nullable): a #GdkDevice, or %NULL
2525  */
2526 GdkDevice *
gtk_get_current_event_device(void)2527 gtk_get_current_event_device (void)
2528 {
2529   if (current_events)
2530     return gdk_event_get_device (current_events->data);
2531   else
2532     return NULL;
2533 }
2534 
2535 /**
2536  * gtk_get_event_widget:
2537  * @event: a #GdkEvent
2538  *
2539  * If @event is %NULL or the event was not associated with any widget,
2540  * returns %NULL, otherwise returns the widget that received the event
2541  * originally.
2542  *
2543  * Returns: (transfer none) (nullable): the widget that originally
2544  *     received @event, or %NULL
2545  */
2546 GtkWidget*
gtk_get_event_widget(GdkEvent * event)2547 gtk_get_event_widget (GdkEvent *event)
2548 {
2549   GtkWidget *widget;
2550   gpointer widget_ptr;
2551 
2552   widget = NULL;
2553   if (event && event->any.window &&
2554       (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2555     {
2556       gdk_window_get_user_data (event->any.window, &widget_ptr);
2557       widget = widget_ptr;
2558     }
2559 
2560   return widget;
2561 }
2562 
2563 static gboolean
propagate_event_up(GtkWidget * widget,GdkEvent * event,GtkWidget * topmost)2564 propagate_event_up (GtkWidget *widget,
2565                     GdkEvent  *event,
2566                     GtkWidget *topmost)
2567 {
2568   gboolean handled_event = FALSE;
2569 
2570   /* Propagate event up the widget tree so that
2571    * parents can see the button and motion
2572    * events of the children.
2573    */
2574   while (TRUE)
2575     {
2576       GtkWidget *tmp;
2577 
2578       g_object_ref (widget);
2579 
2580       /* Scroll events are special cased here because it
2581        * feels wrong when scrolling a GtkViewport, say,
2582        * to have children of the viewport eat the scroll
2583        * event
2584        */
2585       if (!gtk_widget_is_sensitive (widget))
2586         handled_event = event->type != GDK_SCROLL;
2587       else
2588         handled_event = gtk_widget_event (widget, event);
2589 
2590       tmp = gtk_widget_get_parent (widget);
2591       g_object_unref (widget);
2592 
2593       if (widget == topmost)
2594         break;
2595 
2596       widget = tmp;
2597 
2598       if (handled_event || !widget)
2599         break;
2600     }
2601 
2602   return handled_event;
2603 }
2604 
2605 static gboolean
propagate_event_down(GtkWidget * widget,GdkEvent * event,GtkWidget * topmost)2606 propagate_event_down (GtkWidget *widget,
2607                       GdkEvent  *event,
2608                       GtkWidget *topmost)
2609 {
2610   gint handled_event = FALSE;
2611   GList *widgets = NULL;
2612   GList *l;
2613 
2614   widgets = g_list_prepend (widgets, g_object_ref (widget));
2615   while (widget && widget != topmost)
2616     {
2617       widget = gtk_widget_get_parent (widget);
2618       if (!widget)
2619         break;
2620 
2621       widgets = g_list_prepend (widgets, g_object_ref (widget));
2622 
2623       if (widget == topmost)
2624         break;
2625     }
2626 
2627   for (l = widgets; l && !handled_event; l = l->next)
2628     {
2629       widget = (GtkWidget *)l->data;
2630 
2631       if (!gtk_widget_is_sensitive (widget))
2632         {
2633           /* stop propagating on SCROLL, but don't handle the event, so it
2634            * can propagate up again and reach its handling widget
2635            */
2636           if (event->type == GDK_SCROLL)
2637             break;
2638           else
2639             handled_event = TRUE;
2640         }
2641       else
2642         handled_event = _gtk_widget_captured_event (widget, event);
2643     }
2644   g_list_free_full (widgets, (GDestroyNotify)g_object_unref);
2645 
2646   return handled_event;
2647 }
2648 
2649 static gboolean
propagate_event(GtkWidget * widget,GdkEvent * event,gboolean captured,GtkWidget * topmost)2650 propagate_event (GtkWidget *widget,
2651                  GdkEvent  *event,
2652                  gboolean   captured,
2653                  GtkWidget *topmost)
2654 {
2655   gboolean handled_event = FALSE;
2656   gboolean (* propagate_func) (GtkWidget *widget, GdkEvent  *event);
2657 
2658   propagate_func = captured ? _gtk_widget_captured_event : gtk_widget_event;
2659 
2660   if (event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE)
2661     {
2662       /* Only send key events within Window widgets to the Window
2663        * The Window widget will in turn pass the
2664        * key event on to the currently focused widget
2665        * for that window.
2666        */
2667       GtkWidget *window;
2668 
2669       window = gtk_widget_get_toplevel (widget);
2670       if (GTK_IS_WINDOW (window))
2671         {
2672           g_object_ref (widget);
2673           /* If there is a grab within the window, give the grab widget
2674            * a first crack at the key event
2675            */
2676           if (widget != window && gtk_widget_has_grab (widget))
2677             handled_event = propagate_func (widget, event);
2678 
2679           if (!handled_event &&
2680               gtk_widget_is_sensitive (window))
2681             handled_event = propagate_func (window, event);
2682 
2683           g_object_unref (widget);
2684           return handled_event;
2685         }
2686     }
2687 
2688   /* Other events get propagated up/down the widget tree */
2689   return captured ?
2690     propagate_event_down (widget, event, topmost) :
2691     propagate_event_up (widget, event, topmost);
2692 }
2693 
2694 /**
2695  * gtk_propagate_event:
2696  * @widget: a #GtkWidget
2697  * @event: an event
2698  *
2699  * Sends an event to a widget, propagating the event to parent widgets
2700  * if the event remains unhandled.
2701  *
2702  * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2703  * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2704  * the event may be propagated; if so, this function is used.
2705  *
2706  * gtk_propagate_event() calls gtk_widget_event() on each widget it
2707  * decides to send the event to. So gtk_widget_event() is the lowest-level
2708  * function; it simply emits the #GtkWidget::event and possibly an
2709  * event-specific signal on a widget. gtk_propagate_event() is a bit
2710  * higher-level, and gtk_main_do_event() is the highest level.
2711  *
2712  * All that said, you most likely don’t want to use any of these
2713  * functions; synthesizing events is rarely needed. There are almost
2714  * certainly better ways to achieve your goals. For example, use
2715  * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2716  * of making up expose events.
2717  */
2718 void
gtk_propagate_event(GtkWidget * widget,GdkEvent * event)2719 gtk_propagate_event (GtkWidget *widget,
2720                      GdkEvent  *event)
2721 {
2722   g_return_if_fail (GTK_IS_WIDGET (widget));
2723   g_return_if_fail (event != NULL);
2724 
2725   propagate_event (widget, event, FALSE, NULL);
2726 }
2727 
2728 gboolean
_gtk_propagate_captured_event(GtkWidget * widget,GdkEvent * event,GtkWidget * topmost)2729 _gtk_propagate_captured_event (GtkWidget *widget,
2730                                GdkEvent  *event,
2731                                GtkWidget *topmost)
2732 {
2733   return propagate_event (widget, event, TRUE, topmost);
2734 }
2735