Debian Codegraph @

gtk/gtkmain.c

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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
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
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
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
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
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*
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
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
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
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
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
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
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
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
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 /* Trigger fontconfig initialization early */
697 pango_cairo_font_map_get_default ();
698 }
699
700 static void
701 gettext_initialization (void)
702 {
703 setlocale_initialization ();
704
705 #ifdef ENABLE_NLS
706 bindtextdomain (GETTEXT_PACKAGE, _gtk_get_localedir ());
707 bindtextdomain (GETTEXT_PACKAGE "-properties", _gtk_get_localedir ());
708 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
709 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
710 bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
711 # endif
712 #endif
713 }
714
715 static void
716 default_display_notify_cb (GdkDisplayManager *dm)
717 {
718 _gtk_accessibility_init ();
719 debug_flags[0].display = gdk_display_get_default ();
720 }
721
722 static void
723 do_post_parse_initialization (int *argc,
724 char ***argv)
725 {
726 GdkDisplayManager *display_manager;
727
728 if (gtk_initialized)
729 return;
730
731 gettext_initialization ();
732
733 #ifdef SIGPIPE
734 signal (SIGPIPE, SIG_IGN);
735 #endif
736
737 if (g_fatal_warnings)
738 {
739 GLogLevelFlags fatal_mask;
740
741 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
742 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
743 g_log_set_always_fatal (fatal_mask);
744 }
745
746 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
747 if (debug_flags[0].flags & GTK_DEBUG_UPDATES)
748 gdk_window_set_debug_updates (TRUE);
749 G_GNUC_END_IGNORE_DEPRECATIONS
750
751 gtk_widget_set_default_direction (gtk_get_locale_direction ());
752
753 _gtk_ensure_resources ();
754
755 _gtk_accel_map_init ();
756
757 gtk_initialized = TRUE;
758
759 if (gtk_modules_string)
760 {
761 _gtk_modules_init (argc, argv, gtk_modules_string->str);
762 g_string_free (gtk_modules_string, TRUE);
763 }
764 else
765 {
766 _gtk_modules_init (argc, argv, NULL);
767 }
768
769 display_manager = gdk_display_manager_get ();
770 if (gdk_display_manager_get_default_display (display_manager) != NULL)
771 default_display_notify_cb (display_manager);
772
773 g_signal_connect (display_manager, "notify::default-display",
774 G_CALLBACK (default_display_notify_cb),
775 NULL);
776 }
777
778
779 typedef struct
780 {
781 gboolean open_default_display;
782 } OptionGroupInfo;
783
784 static gboolean
785 pre_parse_hook (GOptionContext *context,
786 GOptionGroup *group,
787 gpointer data,
788 GError **error)
789 {
790 do_pre_parse_initialization (NULL, NULL);
791
792 return TRUE;
793 }
794
795 static gboolean
796 post_parse_hook (GOptionContext *context,
797 GOptionGroup *group,
798 gpointer data,
799 GError **error)
800 {
801 OptionGroupInfo *info = data;
802
803
804 do_post_parse_initialization (NULL, NULL);
805
806 if (info->open_default_display)
807 {
808 if (GDK_PRIVATE_CALL (gdk_display_open_default) () == NULL)
809 {
810 const char *display_name = gdk_get_display_arg_name ();
811 g_set_error (error,
812 G_OPTION_ERROR,
813 G_OPTION_ERROR_FAILED,
814 _("Cannot open display: %s"),
815 display_name ? display_name : "" );
816
817 return FALSE;
818 }
819
820 if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
821 gtk_window_set_interactive_debugging (TRUE);
822 }
823
824 return TRUE;
825 }
826
827 guint
828 gtk_get_display_debug_flags (GdkDisplay *display)
829 {
830 gint i;
831
832 for (i = 0; i < N_DEBUG_DISPLAYS; i++)
833 {
834 if (debug_flags[i].display == display)
835 return debug_flags[i].flags;
836 }
837
838 return 0;
839 }
840
841 void
842 gtk_set_display_debug_flags (GdkDisplay *display,
843 guint flags)
844 {
845 gint i;
846
847 for (i = 0; i < N_DEBUG_DISPLAYS; i++)
848 {
849 if (debug_flags[i].display == NULL)
850 debug_flags[i].display = display;
851
852 if (debug_flags[i].display == display)
853 {
854 debug_flags[i].flags = flags;
855 return;
856 }
857 }
858 }
859
860 /**
861 * gtk_get_debug_flags:
862 *
863 * Returns the GTK+ debug flags.
864 *
865 * This function is intended for GTK+ modules that want
866 * to adjust their debug output based on GTK+ debug flags.
867 *
868 * Returns: the GTK+ debug flags.
869 */
870 guint
871 gtk_get_debug_flags (void)
872 {
873 return gtk_get_display_debug_flags (gdk_display_get_default ());
874 }
875
876 /**
877 * gtk_set_debug_flags:
878 *
879 * Sets the GTK+ debug flags.
880 */
881 void
882 gtk_set_debug_flags (guint flags)
883 {
884 gtk_set_display_debug_flags (gdk_display_get_default (), flags);
885 }
886
887 gboolean
888 gtk_simulate_touchscreen (void)
889 {
890 static gint test_touchscreen;
891
892 if (test_touchscreen == 0)
893 test_touchscreen = g_getenv ("GTK_TEST_TOUCHSCREEN") != NULL ? 1 : -1;
894
895 return test_touchscreen > 0 || (gtk_get_debug_flags () & GTK_DEBUG_TOUCHSCREEN) != 0;
896 }
897
898 /**
899 * gtk_get_option_group:
900 * @open_default_display: whether to open the default display
901 * when parsing the commandline arguments
902 *
903 * Returns a #GOptionGroup for the commandline arguments recognized
904 * by GTK+ and GDK.
905 *
906 * You should add this group to your #GOptionContext
907 * with g_option_context_add_group(), if you are using
908 * g_option_context_parse() to parse your commandline arguments.
909 *
910 * Returns: (transfer full): a #GOptionGroup for the commandline
911 * arguments recognized by GTK+
912 *
913 * Since: 2.6
914 */
915 GOptionGroup *
916 gtk_get_option_group (gboolean open_default_display)
917 {
918 GOptionGroup *group;
919 OptionGroupInfo *info;
920
921 gettext_initialization ();
922
923 info = g_new0 (OptionGroupInfo, 1);
924 info->open_default_display = open_default_display;
925
926 group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
927 g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);
928
929 GDK_PRIVATE_CALL (gdk_add_option_entries) (group);
930 g_option_group_add_entries (group, gtk_args);
931 g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
932
933 return group;
934 }
935
936 /**
937 * gtk_init_with_args:
938 * @argc: (inout): Address of the `argc` parameter of
939 * your main() function (or 0 if @argv is %NULL). This will be changed if
940 * any arguments were handled.
941 * @argv: (array length=argc) (inout) (allow-none): Address of the
942 * `argv` parameter of main(), or %NULL. Any options
943 * understood by GTK+ are stripped before return.
944 * @parameter_string: (allow-none): a string which is displayed in
945 * the first line of `--help` output, after
946 * `programname [OPTION...]`
947 * @entries: (array zero-terminated=1): a %NULL-terminated array
948 * of #GOptionEntrys describing the options of your program
949 * @translation_domain: (nullable): a translation domain to use for translating
950 * the `--help` output for the options in @entries
951 * and the @parameter_string with gettext(), or %NULL
952 * @error: a return location for errors
953 *
954 * This function does the same work as gtk_init_check().
955 * Additionally, it allows you to add your own commandline options,
956 * and it automatically generates nicely formatted
957 * `--help` output. Note that your program will
958 * be terminated after writing out the help output.
959 *
960 * Returns: %TRUE if the commandline arguments (if any) were valid and
961 * if the windowing system has been successfully initialized,
962 * %FALSE otherwise
963 *
964 * Since: 2.6
965 */
966 gboolean
967 gtk_init_with_args (gint *argc,
968 gchar ***argv,
969 const gchar *parameter_string,
970 const GOptionEntry *entries,
971 const gchar *translation_domain,
972 GError **error)
973 {
974 GOptionContext *context;
975 GOptionGroup *gtk_group;
976 gboolean retval;
977
978 if (gtk_initialized)
979 goto done;
980
981 gettext_initialization ();
982
983 if (!check_setugid ())
984 return FALSE;
985
986 gtk_group = gtk_get_option_group (FALSE);
987
988 context = g_option_context_new (parameter_string);
989 g_option_context_add_group (context, gtk_group);
990 g_option_context_set_translation_domain (context, translation_domain);
991
992 if (entries)
993 g_option_context_add_main_entries (context, entries, translation_domain);
994 retval = g_option_context_parse (context, argc, argv, error);
995
996 g_option_context_free (context);
997
998 if (!retval)
999 return FALSE;
1000
1001 done:
1002 if (GDK_PRIVATE_CALL (gdk_display_open_default) () == NULL)
1003 {
1004 const char *display_name = gdk_get_display_arg_name ();
1005 g_set_error (error,
1006 G_OPTION_ERROR,
1007 G_OPTION_ERROR_FAILED,
1008 _("Cannot open display: %s"),
1009 display_name ? display_name : "" );
1010
1011 return FALSE;
1012 }
1013
1014 if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
1015 gtk_window_set_interactive_debugging (TRUE);
1016
1017 return TRUE;
1018 }
1019
1020
1021 /**
1022 * gtk_parse_args:
1023 * @argc: (inout): a pointer to the number of command line arguments
1024 * @argv: (array length=argc) (inout): a pointer to the array of
1025 * command line arguments
1026 *
1027 * Parses command line arguments, and initializes global
1028 * attributes of GTK+, but does not actually open a connection
1029 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
1030 *
1031 * Any arguments used by GTK+ or GDK are removed from the array and
1032 * @argc and @argv are updated accordingly.
1033 *
1034 * There is no need to call this function explicitly if you are using
1035 * gtk_init(), or gtk_init_check().
1036 *
1037 * Note that many aspects of GTK+ require a display connection to
1038 * function, so this way of initializing GTK+ is really only useful
1039 * for specialized use cases.
1040 *
1041 * Returns: %TRUE if initialization succeeded, otherwise %FALSE
1042 */
1043 gboolean
1044 gtk_parse_args (int *argc,
1045 char ***argv)
1046 {
1047 GOptionContext *option_context;
1048 GOptionGroup *gtk_group;
1049 GError *error = NULL;
1050
1051 if (gtk_initialized)
1052 return TRUE;
1053
1054 gettext_initialization ();
1055
1056 if (!check_setugid ())
1057 return FALSE;
1058
1059 option_context = g_option_context_new (NULL);
1060 g_option_context_set_ignore_unknown_options (option_context, TRUE);
1061 g_option_context_set_help_enabled (option_context, FALSE);
1062 gtk_group = gtk_get_option_group (FALSE);
1063 g_option_context_set_main_group (option_context, gtk_group);
1064 if (!g_option_context_parse (option_context, argc, argv, &error))
1065 {
1066 g_warning ("%s", error->message);
1067 g_error_free (error);
1068 }
1069
1070 g_option_context_free (option_context);
1071
1072 return TRUE;
1073 }
1074
1075 #ifdef G_PLATFORM_WIN32
1076 #undef gtk_init_check
1077 #endif
1078
1079 /**
1080 * gtk_init_check:
1081 * @argc: (inout): Address of the `argc` parameter of
1082 * your main() function (or 0 if @argv is %NULL). This will be changed if
1083 * any arguments were handled.
1084 * @argv: (array length=argc) (inout) (allow-none): Address of the
1085 * `argv` parameter of main(), or %NULL. Any options
1086 * understood by GTK+ are stripped before return.
1087 *
1088 * This function does the same work as gtk_init() with only a single
1089 * change: It does not terminate the program if the commandline
1090 * arguments couldn’t be parsed or the windowing system can’t be
1091 * initialized. Instead it returns %FALSE on failure.
1092 *
1093 * This way the application can fall back to some other means of
1094 * communication with the user - for example a curses or command line
1095 * interface.
1096 *
1097 * Note that calling any GTK function or instantiating any GTK type after
1098 * this function returns %FALSE results in undefined behavior.
1099 *
1100 * Returns: %TRUE if the commandline arguments (if any) were valid and
1101 * the windowing system has been successfully initialized, %FALSE
1102 * otherwise
1103 */
1104 gboolean
1105 gtk_init_check (int *argc,
1106 char ***argv)
1107 {
1108 gboolean ret;
1109
1110 if (!gtk_parse_args (argc, argv))
1111 return FALSE;
1112
1113 ret = GDK_PRIVATE_CALL (gdk_display_open_default) () != NULL;
1114
1115 if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
1116 gtk_window_set_interactive_debugging (TRUE);
1117
1118 return ret;
1119 }
1120
1121 #ifdef G_PLATFORM_WIN32
1122 #undef gtk_init
1123 #endif
1124
1125 /**
1126 * gtk_init:
1127 * @argc: (inout): Address of the `argc` parameter of
1128 * your main() function (or 0 if @argv is %NULL). This will be changed if
1129 * any arguments were handled.
1130 * @argv: (array length=argc) (inout) (allow-none): Address of the
1131 * `argv` parameter of main(), or %NULL. Any options
1132 * understood by GTK+ are stripped before return.
1133 *
1134 * Call this function before using any other GTK+ functions in your GUI
1135 * applications. It will initialize everything needed to operate the
1136 * toolkit and parses some standard command line options.
1137 *
1138 * Although you are expected to pass the @argc, @argv parameters from main() to
1139 * this function, it is possible to pass %NULL if @argv is not available or
1140 * commandline handling is not required.
1141 *
1142 * @argc and @argv are adjusted accordingly so your own code will
1143 * never see those standard arguments.
1144 *
1145 * Note that there are some alternative ways to initialize GTK+:
1146 * if you are calling gtk_parse_args(), gtk_init_check(),
1147 * gtk_init_with_args() or g_option_context_parse() with
1148 * the option group returned by gtk_get_option_group(),
1149 * you don’t have to call gtk_init().
1150 *
1151 * And if you are using #GtkApplication, you don't have to call any of the
1152 * initialization functions either; the #GtkApplication::startup handler
1153 * does it for you.
1154 *
1155 * This function will terminate your program if it was unable to
1156 * initialize the windowing system for some reason. If you want
1157 * your program to fall back to a textual interface you want to
1158 * call gtk_init_check() instead.
1159 *
1160 * Since 2.18, GTK+ calls `signal (SIGPIPE, SIG_IGN)`
1161 * during initialization, to ignore SIGPIPE signals, since these are
1162 * almost never wanted in graphical applications. If you do need to
1163 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
1164 * but notice that other libraries (e.g. libdbus or gvfs) might do
1165 * similar things.
1166 */
1167 void
1168 gtk_init (int *argc, char ***argv)
1169 {
1170 if (!gtk_init_check (argc, argv))
1171 {
1172 const char *display_name_arg = gdk_get_display_arg_name ();
1173 if (display_name_arg == NULL)
1174 display_name_arg = getenv("DISPLAY");
1175 g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1176 exit (1);
1177 }
1178 }
1179
1180 #ifdef G_OS_WIN32
1181
1182 /* This is relevant when building with gcc for Windows (MinGW),
1183 * where we want to be struct packing compatible with MSVC,
1184 * i.e. use the -mms-bitfields switch.
1185 * For Cygwin there should be no need to be compatible with MSVC,
1186 * so no need to use G_PLATFORM_WIN32.
1187 */
1188
1189 static void
1190 check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
1191 {
1192 if (sizeof_GtkWindow != sizeof (GtkWindow))
1193 g_error ("Incompatible build!\n"
1194 "The code using GTK+ thinks GtkWindow is of different\n"
1195 "size than it actually is in this build of GTK+.\n"
1196 "On Windows, this probably means that you have compiled\n"
1197 "your code with gcc without the -mms-bitfields switch,\n"
1198 "or that you are using an unsupported compiler.");
1199 }
1200
1201 /* In GTK+ 2.0 the GtkWindow struct actually is the same size in
1202 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1203 * not. Unfortunately this wan’t noticed until after GTK+ 2.0.1. So,
1204 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
1205 * -fnative-struct still matters. GtkBox is one such.
1206 */
1207 static void
1208 check_sizeof_GtkBox (size_t sizeof_GtkBox)
1209 {
1210 if (sizeof_GtkBox != sizeof (GtkBox))
1211 g_error ("Incompatible build!\n"
1212 "The code using GTK+ thinks GtkBox is of different\n"
1213 "size than it actually is in this build of GTK+.\n"
1214 "On Windows, this probably means that you have compiled\n"
1215 "your code with gcc without the -mms-bitfields switch,\n"
1216 "or that you are using an unsupported compiler.");
1217 }
1218
1219 /* These two functions might get more checks added later, thus pass
1220 * in the number of extra args.
1221 */
1222 void
1223 gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1224 {
1225 check_sizeof_GtkWindow (sizeof_GtkWindow);
1226 if (num_checks >= 2)
1227 check_sizeof_GtkBox (sizeof_GtkBox);
1228 gtk_init (argc, argv);
1229 }
1230
1231 gboolean
1232 gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1233 {
1234 check_sizeof_GtkWindow (sizeof_GtkWindow);
1235 if (num_checks >= 2)
1236 check_sizeof_GtkBox (sizeof_GtkBox);
1237 return gtk_init_check (argc, argv);
1238 }
1239
1240 #endif
1241
1242 /**
1243 * gtk_get_locale_direction:
1244 *
1245 * Get the direction of the current locale. This is the expected
1246 * reading direction for text and UI.
1247 *
1248 * This function depends on the current locale being set with
1249 * setlocale() and will default to setting the %GTK_TEXT_DIR_LTR
1250 * direction otherwise. %GTK_TEXT_DIR_NONE will never be returned.
1251 *
1252 * GTK+ sets the default text direction according to the locale
1253 * during gtk_init(), and you should normally use
1254 * gtk_widget_get_direction() or gtk_widget_get_default_direction()
1255 * to obtain the current direcion.
1256 *
1257 * This function is only needed rare cases when the locale is
1258 * changed after GTK+ has already been initialized. In this case,
1259 * you can use it to update the default text direction as follows:
1260 *
1261 * |[<!-- language="C" -->
1262 * setlocale (LC_ALL, new_locale);
1263 * direction = gtk_get_locale_direction ();
1264 * gtk_widget_set_default_direction (direction);
1265 * ]|
1266 *
1267 * Returns: the #GtkTextDirection of the current locale
1268 *
1269 * Since: 3.12
1270 */
1271 GtkTextDirection
1272 gtk_get_locale_direction (void)
1273 {
1274 /* Translate to default:RTL if you want your widgets
1275 * to be RTL, otherwise translate to default:LTR.
1276 * Do *not* translate it to "predefinito:LTR", if it
1277 * it isn't default:LTR or default:RTL it will not work
1278 */
1279 gchar *e = _("default:LTR");
1280 GtkTextDirection dir = GTK_TEXT_DIR_LTR;
1281
1282 if (g_strcmp0 (e, "default:RTL") == 0)
1283 dir = GTK_TEXT_DIR_RTL;
1284 else if (g_strcmp0 (e, "default:LTR") != 0)
1285 g_warning ("Whoever translated default:LTR did so wrongly. Defaulting to LTR.");
1286
1287 return dir;
1288 }
1289
1290 /**
1291 * gtk_get_default_language:
1292 *
1293 * Returns the #PangoLanguage for the default language currently in
1294 * effect. (Note that this can change over the life of an
1295 * application.) The default language is derived from the current
1296 * locale. It determines, for example, whether GTK+ uses the
1297 * right-to-left or left-to-right text direction.
1298 *
1299 * This function is equivalent to pango_language_get_default().
1300 * See that function for details.
1301 *
1302 * Returns: (transfer none): the default language as a #PangoLanguage,
1303 * must not be freed
1304 */
1305 PangoLanguage *
1306 gtk_get_default_language (void)
1307 {
1308 return pango_language_get_default ();
1309 }
1310
1311 /**
1312 * gtk_main:
1313 *
1314 * Runs the main loop until gtk_main_quit() is called.
1315 *
1316 * You can nest calls to gtk_main(). In that case gtk_main_quit()
1317 * will make the innermost invocation of the main loop return.
1318 */
1319 void
1320 gtk_main (void)
1321 {
1322 GMainLoop *loop;
1323
1324 gtk_main_loop_level++;
1325
1326 loop = g_main_loop_new (NULL, TRUE);
1327 main_loops = g_slist_prepend (main_loops, loop);
1328
1329 if (g_main_loop_is_running (main_loops->data))
1330 {
1331 gdk_threads_leave ();
1332 g_main_loop_run (loop);
1333 gdk_threads_enter ();
1334
1335 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
1336 gdk_flush ();
1337 G_GNUC_END_IGNORE_DEPRECATIONS;
1338 }
1339
1340 main_loops = g_slist_remove (main_loops, loop);
1341
1342 g_main_loop_unref (loop);
1343
1344 gtk_main_loop_level--;
1345
1346 if (gtk_main_loop_level == 0)
1347 {
1348 /* Keep this section in sync with gtk_application_shutdown() */
1349
1350 /* Try storing all clipboard data we have */
1351 _gtk_clipboard_store_all ();
1352
1353 /* Synchronize the recent manager singleton */
1354 _gtk_recent_manager_sync ();
1355 }
1356 }
1357
1358 /**
1359 * gtk_main_level:
1360 *
1361 * Asks for the current nesting level of the main loop.
1362 *
1363 * Returns: the nesting level of the current invocation
1364 * of the main loop
1365 */
1366 guint
1367 gtk_main_level (void)
1368 {
1369 return gtk_main_loop_level;
1370 }
1371
1372 /**
1373 * gtk_main_quit:
1374 *
1375 * Makes the innermost invocation of the main loop return
1376 * when it regains control.
1377 */
1378 void
1379 gtk_main_quit (void)
1380 {
1381 g_return_if_fail (main_loops != NULL);
1382
1383 g_main_loop_quit (main_loops->data);
1384 }
1385
1386 /**
1387 * gtk_events_pending:
1388 *
1389 * Checks if any events are pending.
1390 *
1391 * This can be used to update the UI and invoke timeouts etc.
1392 * while doing some time intensive computation.
1393 *
1394 * ## Updating the UI during a long computation
1395 *
1396 * |[<!-- language="C" -->
1397 * // computation going on...
1398 *
1399 * while (gtk_events_pending ())
1400 * gtk_main_iteration ();
1401 *
1402 * // ...computation continued
1403 * ]|
1404 *
1405 * Returns: %TRUE if any events are pending, %FALSE otherwise
1406 */
1407 gboolean
1408 gtk_events_pending (void)
1409 {
1410 gboolean result;
1411
1412 gdk_threads_leave ();
1413 result = g_main_context_pending (NULL);
1414 gdk_threads_enter ();
1415
1416 return result;
1417 }
1418
1419 /**
1420 * gtk_main_iteration:
1421 *
1422 * Runs a single iteration of the mainloop.
1423 *
1424 * If no events are waiting to be processed GTK+ will block
1425 * until the next event is noticed. If you don’t want to block
1426 * look at gtk_main_iteration_do() or check if any events are
1427 * pending with gtk_events_pending() first.
1428 *
1429 * Returns: %TRUE if gtk_main_quit() has been called for the
1430 * innermost mainloop
1431 */
1432 gboolean
1433 gtk_main_iteration (void)
1434 {
1435 gdk_threads_leave ();
1436 g_main_context_iteration (NULL, TRUE);
1437 gdk_threads_enter ();
1438
1439 if (main_loops)
1440 return !g_main_loop_is_running (main_loops->data);
1441 else
1442 return TRUE;
1443 }
1444
1445 /**
1446 * gtk_main_iteration_do:
1447 * @blocking: %TRUE if you want GTK+ to block if no events are pending
1448 *
1449 * Runs a single iteration of the mainloop.
1450 * If no events are available either return or block depending on
1451 * the value of @blocking.
1452 *
1453 * Returns: %TRUE if gtk_main_quit() has been called for the
1454 * innermost mainloop
1455 */
1456 gboolean
1457 gtk_main_iteration_do (gboolean blocking)
1458 {
1459 gdk_threads_leave ();
1460 g_main_context_iteration (NULL, blocking);
1461 gdk_threads_enter ();
1462
1463 if (main_loops)
1464 return !g_main_loop_is_running (main_loops->data);
1465 else
1466 return TRUE;
1467 }
1468
1469 static void
1470 rewrite_events_translate (GdkWindow *old_window,
1471 GdkWindow *new_window,
1472 gdouble *x,
1473 gdouble *y)
1474 {
1475 gint old_origin_x, old_origin_y;
1476 gint new_origin_x, new_origin_y;
1477
1478 gdk_window_get_origin (old_window, &old_origin_x, &old_origin_y);
1479 gdk_window_get_origin (new_window, &new_origin_x, &new_origin_y);
1480
1481 *x += old_origin_x - new_origin_x;
1482 *y += old_origin_y - new_origin_y;
1483 }
1484
1485 static GdkEvent *
1486 rewrite_event_for_window (GdkEvent *event,
1487 GdkWindow *new_window)
1488 {
1489 event = gdk_event_copy (event);
1490
1491 switch (event->type)
1492 {
1493 case GDK_SCROLL:
1494 rewrite_events_translate (event->any.window,
1495 new_window,
1496 &event->scroll.x, &event->scroll.y);
1497 break;
1498 case GDK_BUTTON_PRESS:
1499 case GDK_2BUTTON_PRESS:
1500 case GDK_3BUTTON_PRESS:
1501 case GDK_BUTTON_RELEASE:
1502 rewrite_events_translate (event->any.window,
1503 new_window,
1504 &event->button.x, &event->button.y);
1505 break;
1506 case GDK_MOTION_NOTIFY:
1507 rewrite_events_translate (event->any.window,
1508 new_window,
1509 &event->motion.x, &event->motion.y);
1510 break;
1511 case GDK_TOUCH_BEGIN:
1512 case GDK_TOUCH_UPDATE:
1513 case GDK_TOUCH_END:
1514 case GDK_TOUCH_CANCEL:
1515 rewrite_events_translate (event->any.window,
1516 new_window,
1517 &event->touch.x, &event->touch.y);
1518 break;
1519 case GDK_TOUCHPAD_SWIPE:
1520 rewrite_events_translate (event->any.window,
1521 new_window,
1522 &event->touchpad_swipe.x,
1523 &event->touchpad_swipe.y);
1524 break;
1525 case GDK_TOUCHPAD_PINCH:
1526 rewrite_events_translate (event->any.window,
1527 new_window,
1528 &event->touchpad_pinch.x,
1529 &event->touchpad_pinch.y);
1530 break;
1531 case GDK_KEY_PRESS:
1532 case GDK_KEY_RELEASE:
1533 case GDK_PROXIMITY_IN:
1534 case GDK_PROXIMITY_OUT:
1535 break;
1536
1537 default:
1538 return event;
1539 }
1540
1541 g_object_unref (event->any.window);
1542 event->any.window = g_object_ref (new_window);
1543
1544 return event;
1545 }
1546
1547 /* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
1548 * then what X11 does is deliver the event normally if it was going to this
1549 * client, otherwise, delivers it in terms of the grab window. This function
1550 * rewrites events to the effect that events going to the same window group
1551 * are delivered normally, otherwise, the event is delivered in terms of the
1552 * grab window.
1553 */
1554 static GdkEvent *
1555 rewrite_event_for_grabs (GdkEvent *event)
1556 {
1557 GdkWindow *grab_window;
1558 GtkWidget *event_widget, *grab_widget;
1559 gpointer grab_widget_ptr;
1560 gboolean owner_events;
1561 GdkDisplay *display;
1562 GdkDevice *device;
1563
1564 switch (event->type)
1565 {
1566 case GDK_SCROLL:
1567 case GDK_BUTTON_PRESS:
1568 case GDK_2BUTTON_PRESS:
1569 case GDK_3BUTTON_PRESS:
1570 case GDK_BUTTON_RELEASE:
1571 case GDK_MOTION_NOTIFY:
1572 case GDK_PROXIMITY_IN:
1573 case GDK_PROXIMITY_OUT:
1574 case GDK_KEY_PRESS:
1575 case GDK_KEY_RELEASE:
1576 case GDK_TOUCH_BEGIN:
1577 case GDK_TOUCH_UPDATE:
1578 case GDK_TOUCH_END:
1579 case GDK_TOUCH_CANCEL:
1580 case GDK_TOUCHPAD_SWIPE:
1581 case GDK_TOUCHPAD_PINCH:
1582 display = gdk_window_get_display (event->any.window);
1583 device = gdk_event_get_device (event);
1584
1585 if (!GDK_PRIVATE_CALL (gdk_device_grab_info) (display, device, &grab_window, &owner_events) ||
1586 !owner_events)
1587 return NULL;
1588 break;
1589 default:
1590 return NULL;
1591 }
1592
1593 event_widget = gtk_get_event_widget (event);
1594 gdk_window_get_user_data (grab_window, &grab_widget_ptr);
1595 grab_widget = grab_widget_ptr;
1596
1597 if (grab_widget &&
1598 gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
1599 return rewrite_event_for_window (event, grab_window);
1600 else
1601 return NULL;
1602 }
1603
1604 static GtkWidget *
1605 widget_get_popover_ancestor (GtkWidget *widget,
1606 GtkWindow *window)
1607 {
1608 GtkWidget *parent = gtk_widget_get_parent (widget);
1609
1610 while (parent && parent != GTK_WIDGET (window))
1611 {
1612 widget = parent;
1613 parent = gtk_widget_get_parent (widget);
1614 }
1615
1616 if (!parent || parent != GTK_WIDGET (window))
1617 return NULL;
1618
1619 if (_gtk_window_is_popover_widget (GTK_WINDOW (window), widget))
1620 return widget;
1621
1622 return NULL;
1623 }
1624
1625 static gboolean
1626 check_event_in_child_popover (GtkWidget *event_widget,
1627 GtkWidget *grab_widget)
1628 {
1629 GtkWidget *window, *popover = NULL, *popover_parent = NULL;
1630
1631 if (grab_widget == event_widget)
1632 return FALSE;
1633
1634 window = gtk_widget_get_ancestor (event_widget, GTK_TYPE_WINDOW);
1635
1636 if (!window)
1637 return FALSE;
1638
1639 popover = widget_get_popover_ancestor (event_widget, GTK_WINDOW (window));
1640
1641 if (!popover)
1642 return FALSE;
1643
1644 popover_parent = _gtk_window_get_popover_parent (GTK_WINDOW (window), popover);
1645
1646 if (!popover_parent)
1647 return FALSE;
1648
1649 return (popover_parent == grab_widget || gtk_widget_is_ancestor (popover_parent, grab_widget));
1650 }
1651
1652 /**
1653 * gtk_main_do_event:
1654 * @event: An event to process (normally passed by GDK)
1655 *
1656 * Processes a single GDK event.
1657 *
1658 * This is public only to allow filtering of events between GDK and GTK+.
1659 * You will not usually need to call this function directly.
1660 *
1661 * While you should not call this function directly, you might want to
1662 * know how exactly events are handled. So here is what this function
1663 * does with the event:
1664 *
1665 * 1. Compress enter/leave notify events. If the event passed build an
1666 * enter/leave pair together with the next event (peeked from GDK), both
1667 * events are thrown away. This is to avoid a backlog of (de-)highlighting
1668 * widgets crossed by the pointer.
1669 *
1670 * 2. Find the widget which got the event. If the widget can’t be determined
1671 * the event is thrown away unless it belongs to a INCR transaction.
1672 *
1673 * 3. Then the event is pushed onto a stack so you can query the currently
1674 * handled event with gtk_get_current_event().
1675 *
1676 * 4. The event is sent to a widget. If a grab is active all events for widgets
1677 * that are not in the contained in the grab widget are sent to the latter
1678 * with a few exceptions:
1679 * - Deletion and destruction events are still sent to the event widget for
1680 * obvious reasons.
1681 * - Events which directly relate to the visual representation of the event
1682 * widget.
1683 * - Leave events are delivered to the event widget if there was an enter
1684 * event delivered to it before without the paired leave event.
1685 * - Drag events are not redirected because it is unclear what the semantics
1686 * of that would be.
1687 * Another point of interest might be that all key events are first passed
1688 * through the key snooper functions if there are any. Read the description
1689 * of gtk_key_snooper_install() if you need this feature.
1690 *
1691 * 5. After finishing the delivery the event is popped from the event stack.
1692 */
1693 void
1694 gtk_main_do_event (GdkEvent *event)
1695 {
1696 GtkWidget *event_widget;
1697 GtkWidget *grab_widget = NULL;
1698 GtkWidget *topmost_widget = NULL;
1699 GtkWindowGroup *window_group;
1700 GdkEvent *rewritten_event = NULL;
1701 GdkDevice *device;
1702 GList *tmp_list;
1703
1704 if (event->type == GDK_SETTING)
1705 {
1706 _gtk_settings_handle_event (&event->setting);
1707 return;
1708 }
1709
1710 if (event->type == GDK_OWNER_CHANGE)
1711 {
1712 _gtk_clipboard_handle_event (&event->owner_change);
1713 return;
1714 }
1715
1716 /* Find the widget which got the event. We store the widget
1717 * in the user_data field of GdkWindow's. Ignore the event
1718 * if we don't have a widget for it, except for GDK_PROPERTY_NOTIFY
1719 * events which are handled specially. Though this happens rarely,
1720 * bogus events can occur for e.g. destroyed GdkWindows.
1721 */
1722 event_widget = gtk_get_event_widget (event);
1723 if (!event_widget)
1724 {
1725 /* To handle selection INCR transactions, we select
1726 * PropertyNotify events on the requestor window and create
1727 * a corresponding (fake) GdkWindow so that events get here.
1728 * There won't be a widget though, so we have to handle
1729 * them specially
1730 */
1731 if (event->type == GDK_PROPERTY_NOTIFY)
1732 _gtk_selection_incr_event (event->any.window,
1733 &event->property);
1734
1735 return;
1736 }
1737
1738 /* If pointer or keyboard grabs are in effect, munge the events
1739 * so that each window group looks like a separate app.
1740 */
1741 rewritten_event = rewrite_event_for_grabs (event);
1742 if (rewritten_event)
1743 {
1744 event = rewritten_event;
1745 event_widget = gtk_get_event_widget (event);
1746 }
1747
1748 /* Push the event onto a stack of current events for
1749 * gtk_current_event_get().
1750 */
1751 current_events = g_list_prepend (current_events, event);
1752
1753 window_group = gtk_main_get_window_group (event_widget);
1754 device = gdk_event_get_device (event);
1755
1756 /* check whether there is a (device) grab in effect... */
1757 if (device)
1758 grab_widget = gtk_window_group_get_current_device_grab (window_group, device);
1759
1760 if (!grab_widget)
1761 grab_widget = gtk_window_group_get_current_grab (window_group);
1762
1763 if (GTK_IS_WINDOW (event_widget) ||
1764 (grab_widget && grab_widget != event_widget &&
1765 !gtk_widget_is_ancestor (event_widget, grab_widget)))
1766 {
1767 /* Ignore event if we got a grab on another toplevel */
1768 if (!grab_widget ||
1769 gtk_widget_get_toplevel (event_widget) == gtk_widget_get_toplevel (grab_widget))
1770 {
1771 if (_gtk_window_check_handle_wm_event (event))
1772 goto cleanup;
1773 }
1774 }
1775
1776 /* Find out the topmost widget where captured event propagation
1777 * should start, which is the widget holding the GTK+ grab
1778 * if any, otherwise it's left NULL and events are emitted
1779 * from the toplevel (or topmost parentless parent).
1780 */
1781 if (grab_widget)
1782 topmost_widget = grab_widget;
1783
1784 /* If the grab widget is an ancestor of the event widget
1785 * then we send the event to the original event widget.
1786 * This is the key to implementing modality.
1787 */
1788 if (!grab_widget ||
1789 ((gtk_widget_is_sensitive (event_widget) || event->type == GDK_SCROLL) &&
1790 gtk_widget_is_ancestor (event_widget, grab_widget)))
1791 grab_widget = event_widget;
1792
1793 /* popovers are not really a "child" of their "parent" in the widget/window
1794 * hierarchy sense, we however want to interact with popovers spawn by widgets
1795 * within grab_widget. If this is the case, we let the event go through
1796 * unaffected by the grab.
1797 */
1798 if (check_event_in_child_popover (event_widget, grab_widget))
1799 grab_widget = event_widget;
1800
1801 /* If the widget receiving events is actually blocked by another
1802 * device GTK+ grab
1803 */
1804 if (device &&
1805 _gtk_window_group_widget_is_blocked_for_device (window_group, grab_widget, device))
1806 goto cleanup;
1807
1808 /* Not all events get sent to the grabbing widget.
1809 * The delete, destroy, expose, focus change and resize
1810 * events still get sent to the event widget because
1811 * 1) these events have no meaning for the grabbing widget
1812 * and 2) redirecting these events to the grabbing widget
1813 * could cause the display to be messed up.
1814 *
1815 * Drag events are also not redirected, since it isn't
1816 * clear what the semantics of that would be.
1817 */
1818 switch (event->type)
1819 {
1820 case GDK_NOTHING:
1821 break;
1822
1823 case GDK_DELETE:
1824 g_object_ref (event_widget);
1825 if ((!gtk_window_group_get_current_grab (window_group) || gtk_widget_get_toplevel (gtk_window_group_get_current_grab (window_group)) == event_widget) &&
1826 !gtk_widget_event (event_widget, event))
1827 gtk_widget_destroy (event_widget);
1828 g_object_unref (event_widget);
1829 break;
1830
1831 case GDK_DESTROY:
1832 /* Unexpected GDK_DESTROY from the outside, ignore for
1833 * child windows, handle like a GDK_DELETE for toplevels
1834 */
1835 if (!gtk_widget_get_parent (event_widget))
1836 {
1837 g_object_ref (event_widget);
1838 if (!gtk_widget_event (event_widget, event) &&
1839 gtk_widget_get_realized (event_widget))
1840 gtk_widget_destroy (event_widget);
1841 g_object_unref (event_widget);
1842 }
1843 break;
1844
1845 case GDK_EXPOSE:
1846 if (event->any.window)
1847 gtk_widget_render (event_widget, event->any.window, event->expose.region);
1848 break;
1849
1850 case GDK_PROPERTY_NOTIFY:
1851 case GDK_FOCUS_CHANGE:
1852 case GDK_CONFIGURE:
1853 case GDK_MAP:
1854 case GDK_UNMAP:
1855 case GDK_SELECTION_CLEAR:
1856 case GDK_SELECTION_REQUEST:
1857 case GDK_SELECTION_NOTIFY:
1858 case GDK_CLIENT_EVENT:
1859 case GDK_VISIBILITY_NOTIFY:
1860 case GDK_WINDOW_STATE:
1861 case GDK_GRAB_BROKEN:
1862 case GDK_DAMAGE:
1863 if (!_gtk_widget_captured_event (event_widget, event))
1864 gtk_widget_event (event_widget, event);
1865 break;
1866
1867 case GDK_KEY_PRESS:
1868 case GDK_KEY_RELEASE:
1869 if (gtk_invoke_key_snoopers (grab_widget, event))
1870 break;
1871
1872 /* make focus visible in a window that receives a key event */
1873 {
1874 GtkWidget *window;
1875
1876 window = gtk_widget_get_toplevel (grab_widget);
1877 if (GTK_IS_WINDOW (window))
1878 gtk_window_set_focus_visible (GTK_WINDOW (window), TRUE);
1879 }
1880
1881 /* Catch alt press to enable auto-mnemonics;
1882 * menus are handled elsewhere
1883 * FIXME: this does not work with mnemonic modifiers other than Alt
1884 */
1885 if ((event->key.keyval == GDK_KEY_Alt_L || event->key.keyval == GDK_KEY_Alt_R) &&
1886 ((event->key.state & (gtk_accelerator_get_default_mod_mask ()) & ~(GDK_RELEASE_MASK|GDK_MOD1_MASK)) == 0) &&
1887 !GTK_IS_MENU_SHELL (grab_widget))
1888 {
1889 gboolean mnemonics_visible;
1890 GtkWidget *window;
1891
1892 mnemonics_visible = (event->type == GDK_KEY_PRESS);
1893
1894 window = gtk_widget_get_toplevel (grab_widget);
1895 if (GTK_IS_WINDOW (window))
1896 {
1897 if (mnemonics_visible)
1898 _gtk_window_schedule_mnemonics_visible (GTK_WINDOW (window));
1899 else
1900 gtk_window_set_mnemonics_visible (GTK_WINDOW (window), FALSE);
1901 }
1902 }
1903 /* else fall through */
1904 case GDK_SCROLL:
1905 case GDK_BUTTON_PRESS:
1906 case GDK_2BUTTON_PRESS:
1907 case GDK_3BUTTON_PRESS:
1908 case GDK_TOUCH_BEGIN:
1909 case GDK_MOTION_NOTIFY:
1910 case GDK_BUTTON_RELEASE:
1911 case GDK_PROXIMITY_IN:
1912 case GDK_PROXIMITY_OUT:
1913 case GDK_TOUCH_UPDATE:
1914 case GDK_TOUCH_END:
1915 case GDK_TOUCH_CANCEL:
1916 case GDK_TOUCHPAD_SWIPE:
1917 case GDK_TOUCHPAD_PINCH:
1918 case GDK_PAD_BUTTON_PRESS:
1919 case GDK_PAD_BUTTON_RELEASE:
1920 case GDK_PAD_RING:
1921 case GDK_PAD_STRIP:
1922 case GDK_PAD_GROUP_MODE:
1923 if (!_gtk_propagate_captured_event (grab_widget, event, topmost_widget))
1924 gtk_propagate_event (grab_widget, event);
1925 break;
1926
1927 case GDK_ENTER_NOTIFY:
1928 case GDK_LEAVE_NOTIFY:
1929 if (gtk_widget_is_sensitive (grab_widget) &&
1930 !_gtk_propagate_captured_event (grab_widget, event, topmost_widget))
1931 gtk_widget_event (grab_widget, event);
1932 break;
1933
1934 case GDK_DRAG_STATUS:
1935 case GDK_DROP_FINISHED:
1936 _gtk_drag_source_handle_event (event_widget, event);
1937 break;
1938 case GDK_DRAG_ENTER:
1939 case GDK_DRAG_LEAVE:
1940 case GDK_DRAG_MOTION:
1941 case GDK_DROP_START:
1942 _gtk_drag_dest_handle_event (event_widget, event);
1943 break;
1944 default:
1945 g_assert_not_reached ();
1946 break;
1947 }
1948
1949 if (event->type == GDK_ENTER_NOTIFY
1950 || event->type == GDK_LEAVE_NOTIFY
1951 || event->type == GDK_BUTTON_PRESS
1952 || event->type == GDK_2BUTTON_PRESS
1953 || event->type == GDK_3BUTTON_PRESS
1954 || event->type == GDK_KEY_PRESS
1955 || event->type == GDK_DRAG_ENTER
1956 || event->type == GDK_GRAB_BROKEN
1957 || event->type == GDK_MOTION_NOTIFY
1958 || event->type == GDK_TOUCH_UPDATE
1959 || event->type == GDK_SCROLL)
1960 {
1961 _gtk_tooltip_handle_event (event);
1962 }
1963
1964 cleanup:
1965 tmp_list = current_events;
1966 current_events = g_list_remove_link (current_events, tmp_list);
1967 g_list_free_1 (tmp_list);
1968
1969 if (rewritten_event)
1970 gdk_event_free (rewritten_event);
1971 }
1972
1973 /**
1974 * gtk_true:
1975 *
1976 * All this function does it to return %TRUE.
1977 *
1978 * This can be useful for example if you want to inhibit the deletion
1979 * of a window. Of course you should not do this as the user expects
1980 * a reaction from clicking the close icon of the window...
1981 *
1982 * ## A persistent window
1983 *
1984 * |[<!-- language="C" -->
1985 * #include <gtk/gtk.h>
1986 *
1987 * int
1988 * main (int argc, char **argv)
1989 * {
1990 * GtkWidget *win, *but;
1991 * const char *text = "Close yourself. I mean it!";
1992 *
1993 * gtk_init (&argc, &argv);
1994 *
1995 * win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1996 * g_signal_connect (win,
1997 * "delete-event",
1998 * G_CALLBACK (gtk_true),
1999 * NULL);
2000 * g_signal_connect (win, "destroy",
2001 * G_CALLBACK (gtk_main_quit),
2002 * NULL);
2003 *
2004 * but = gtk_button_new_with_label (text);
2005 * g_signal_connect_swapped (but, "clicked",
2006 * G_CALLBACK (gtk_object_destroy),
2007 * win);
2008 * gtk_container_add (GTK_CONTAINER (win), but);
2009 *
2010 * gtk_widget_show_all (win);
2011 *
2012 * gtk_main ();
2013 *
2014 * return 0;
2015 * }
2016 * ]|
2017 *
2018 * Returns: %TRUE
2019 */
2020 gboolean
2021 gtk_true (void)
2022 {
2023 return TRUE;
2024 }
2025
2026 /**
2027 * gtk_false:
2028 *
2029 * Analogical to gtk_true(), this function does nothing
2030 * but always returns %FALSE.
2031 *
2032 * Returns: %FALSE
2033 */
2034 gboolean
2035 gtk_false (void)
2036 {
2037 return FALSE;
2038 }
2039
2040 static GtkWindowGroup *
2041 gtk_main_get_window_group (GtkWidget *widget)
2042 {
2043 GtkWidget *toplevel = NULL;
2044
2045 if (widget)
2046 toplevel = gtk_widget_get_toplevel (widget);
2047
2048 if (GTK_IS_WINDOW (toplevel))
2049 return gtk_window_get_group (GTK_WINDOW (toplevel));
2050 else
2051 return gtk_window_get_group (NULL);
2052 }
2053
2054 typedef struct
2055 {
2056 GtkWidget *old_grab_widget;
2057 GtkWidget *new_grab_widget;
2058 gboolean was_grabbed;
2059 gboolean is_grabbed;
2060 gboolean from_grab;
2061 GList *notified_windows;
2062 GdkDevice *device;
2063 } GrabNotifyInfo;
2064
2065 static void
2066 synth_crossing_for_grab_notify (GtkWidget *from,
2067 GtkWidget *to,
2068 GrabNotifyInfo *info,
2069 GList *devices,
2070 GdkCrossingMode mode)
2071 {
2072 while (devices)
2073 {
2074 GdkDevice *device = devices->data;
2075 GdkWindow *from_window, *to_window;
2076
2077 /* Do not propagate events more than once to
2078 * the same windows if non-multidevice aware.
2079 */
2080 if (!from)
2081 from_window = NULL;
2082 else
2083 {
2084 from_window = _gtk_widget_get_device_window (from, device);
2085
2086 if (from_window &&
2087 !gdk_window_get_support_multidevice (from_window) &&
2088 g_list_find (info->notified_windows, from_window))
2089 from_window = NULL;
2090 }
2091
2092 if (!to)
2093 to_window = NULL;
2094 else
2095 {
2096 to_window = _gtk_widget_get_device_window (to, device);
2097
2098 if (to_window &&
2099 !gdk_window_get_support_multidevice (to_window) &&
2100 g_list_find (info->notified_windows, to_window))
2101 to_window = NULL;
2102 }
2103
2104 if (from_window || to_window)
2105 {
2106 _gtk_widget_synthesize_crossing ((from_window) ? from : NULL,
2107 (to_window) ? to : NULL,
2108 device, mode);
2109
2110 if (from_window)
2111 info->notified_windows = g_list_prepend (info->notified_windows, from_window);
2112
2113 if (to_window)
2114 info->notified_windows = g_list_prepend (info->notified_windows, to_window);
2115 }
2116
2117 devices = devices->next;
2118 }
2119 }
2120
2121 static void
2122 gtk_grab_notify_foreach (GtkWidget *child,
2123 gpointer data)
2124 {
2125 GrabNotifyInfo *info = data;
2126 gboolean was_grabbed, is_grabbed, was_shadowed, is_shadowed;
2127 GList *devices;
2128
2129 was_grabbed = info->was_grabbed;
2130 is_grabbed = info->is_grabbed;
2131
2132 info->was_grabbed = info->was_grabbed || (child == info->old_grab_widget);
2133 info->is_grabbed = info->is_grabbed || (child == info->new_grab_widget);
2134
2135 was_shadowed = info->old_grab_widget && !info->was_grabbed;
2136 is_shadowed = info->new_grab_widget && !info->is_grabbed;
2137
2138 g_object_ref (child);
2139
2140 if ((was_shadowed || is_shadowed) && GTK_IS_CONTAINER (child))
2141 gtk_container_forall (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
2142
2143 if (info->device &&
2144 _gtk_widget_get_device_window (child, info->device))
2145 {
2146 /* Device specified and is on widget */
2147 devices = g_list_prepend (NULL, info->device);
2148 }
2149 else
2150 devices = _gtk_widget_list_devices (child);
2151
2152 if (is_shadowed)
2153 {
2154 _gtk_widget_set_shadowed (child, TRUE);
2155 if (!was_shadowed && devices &&
2156 gtk_widget_is_sensitive (child))
2157 synth_crossing_for_grab_notify (child, info->new_grab_widget,
2158 info, devices,
2159 GDK_CROSSING_GTK_GRAB);
2160 }
2161 else
2162 {
2163 _gtk_widget_set_shadowed (child, FALSE);
2164 if (was_shadowed && devices &&
2165 gtk_widget_is_sensitive (child))
2166 synth_crossing_for_grab_notify (info->old_grab_widget, child,
2167 info, devices,
2168 info->from_grab ? GDK_CROSSING_GTK_GRAB :
2169 GDK_CROSSING_GTK_UNGRAB);
2170 }
2171
2172 if (was_shadowed != is_shadowed)
2173 _gtk_widget_grab_notify (child, was_shadowed);
2174
2175 g_object_unref (child);
2176 g_list_free (devices);
2177
2178 info->was_grabbed = was_grabbed;
2179 info->is_grabbed = is_grabbed;
2180 }
2181
2182 static void
2183 gtk_grab_notify (GtkWindowGroup *group,
2184 GdkDevice *device,
2185 GtkWidget *old_grab_widget,
2186 GtkWidget *new_grab_widget,
2187 gboolean from_grab)
2188 {
2189 GList *toplevels;
2190 GrabNotifyInfo info = { 0 };
2191
2192 if (old_grab_widget == new_grab_widget)
2193 return;
2194
2195 info.old_grab_widget = old_grab_widget;
2196 info.new_grab_widget = new_grab_widget;
2197 info.from_grab = from_grab;
2198 info.device = device;
2199
2200 g_object_ref (group);
2201
2202 toplevels = gtk_window_list_toplevels ();
2203 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
2204
2205 while (toplevels)
2206 {
2207 GtkWindow *toplevel = toplevels->data;
2208 toplevels = g_list_delete_link (toplevels, toplevels);
2209
2210 info.was_grabbed = FALSE;
2211 info.is_grabbed = FALSE;
2212
2213 if (group == gtk_window_get_group (toplevel))
2214 gtk_grab_notify_foreach (GTK_WIDGET (toplevel), &info);
2215 g_object_unref (toplevel);
2216 }
2217
2218 g_list_free (info.notified_windows);
2219 g_object_unref (group);
2220 }
2221
2222 /**
2223 * gtk_grab_add: (method)
2224 * @widget: The widget that grabs keyboard and pointer events
2225 *
2226 * Makes @widget the current grabbed widget.
2227 *
2228 * This means that interaction with other widgets in the same
2229 * application is blocked and mouse as well as keyboard events
2230 * are delivered to this widget.
2231 *
2232 * If @widget is not sensitive, it is not set as the current
2233 * grabbed widget and this function does nothing.
2234 */
2235 void
2236 gtk_grab_add (GtkWidget *widget)
2237 {
2238 GtkWindowGroup *group;
2239 GtkWidget *old_grab_widget;
2240
2241 g_return_if_fail (widget != NULL);
2242
2243 if (!gtk_widget_has_grab (widget) && gtk_widget_is_sensitive (widget))
2244 {
2245 _gtk_widget_set_has_grab (widget, TRUE);
2246
2247 group = gtk_main_get_window_group (widget);
2248
2249 old_grab_widget = gtk_window_group_get_current_grab (group);
2250
2251 g_object_ref (widget);
2252 _gtk_window_group_add_grab (group, widget);
2253
2254 gtk_grab_notify (group, NULL, old_grab_widget, widget, TRUE);
2255 }
2256 }
2257
2258 /**
2259 * gtk_grab_get_current:
2260 *
2261 * Queries the current grab of the default window group.
2262 *
2263 * Returns: (transfer none) (nullable): The widget which currently
2264 * has the grab or %NULL if no grab is active
2265 */
2266 GtkWidget*
2267 gtk_grab_get_current (void)
2268 {
2269 GtkWindowGroup *group;
2270
2271 group = gtk_main_get_window_group (NULL);
2272
2273 return gtk_window_group_get_current_grab (group);
2274 }
2275
2276 /**
2277 * gtk_grab_remove: (method)
2278 * @widget: The widget which gives up the grab
2279 *
2280 * Removes the grab from the given widget.
2281 *
2282 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
2283 *
2284 * If @widget does not have the grab, this function does nothing.
2285 */
2286 void
2287 gtk_grab_remove (GtkWidget *widget)
2288 {
2289 GtkWindowGroup *group;
2290 GtkWidget *new_grab_widget;
2291
2292 g_return_if_fail (widget != NULL);
2293
2294 if (gtk_widget_has_grab (widget))
2295 {
2296 _gtk_widget_set_has_grab (widget, FALSE);
2297
2298 group = gtk_main_get_window_group (widget);
2299 _gtk_window_group_remove_grab (group, widget);
2300 new_grab_widget = gtk_window_group_get_current_grab (group);
2301
2302 gtk_grab_notify (group, NULL, widget, new_grab_widget, FALSE);
2303
2304 g_object_unref (widget);
2305 }
2306 }
2307
2308 /**
2309 * gtk_device_grab_add:
2310 * @widget: a #GtkWidget
2311 * @device: a #GdkDevice to grab on.
2312 * @block_others: %TRUE to prevent other devices to interact with @widget.
2313 *
2314 * Adds a GTK+ grab on @device, so all the events on @device and its
2315 * associated pointer or keyboard (if any) are delivered to @widget.
2316 * If the @block_others parameter is %TRUE, any other devices will be
2317 * unable to interact with @widget during the grab.
2318 *
2319 * Since: 3.0
2320 */
2321 void
2322 gtk_device_grab_add (GtkWidget *widget,
2323 GdkDevice *device,
2324 gboolean block_others)
2325 {
2326 GtkWindowGroup *group;
2327 GtkWidget *old_grab_widget;
2328
2329 g_return_if_fail (GTK_IS_WIDGET (widget));
2330 g_return_if_fail (GDK_IS_DEVICE (device));
2331
2332 group = gtk_main_get_window_group (widget);
2333 old_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2334
2335 if (old_grab_widget != widget)
2336 _gtk_window_group_add_device_grab (group, widget, device, block_others);
2337
2338 gtk_grab_notify (group, device, old_grab_widget, widget, TRUE);
2339 }
2340
2341 /**
2342 * gtk_device_grab_remove:
2343 * @widget: a #GtkWidget
2344 * @device: a #GdkDevice
2345 *
2346 * Removes a device grab from the given widget.
2347 *
2348 * You have to pair calls to gtk_device_grab_add() and
2349 * gtk_device_grab_remove().
2350 *
2351 * Since: 3.0
2352 */
2353 void
2354 gtk_device_grab_remove (GtkWidget *widget,
2355 GdkDevice *device)
2356 {
2357 GtkWindowGroup *group;
2358 GtkWidget *new_grab_widget;
2359
2360 g_return_if_fail (GTK_IS_WIDGET (widget));
2361 g_return_if_fail (GDK_IS_DEVICE (device));
2362
2363 group = gtk_main_get_window_group (widget);
2364 _gtk_window_group_remove_device_grab (group, widget, device);
2365 new_grab_widget = gtk_window_group_get_current_device_grab (group, device);
2366
2367 gtk_grab_notify (group, device, widget, new_grab_widget, FALSE);
2368 }
2369
2370 /**
2371 * gtk_key_snooper_install: (skip)
2372 * @snooper: a #GtkKeySnoopFunc
2373 * @func_data: (closure): data to pass to @snooper
2374 *
2375 * Installs a key snooper function, which will get called on all
2376 * key events before delivering them normally.
2377 *
2378 * Returns: a unique id for this key snooper for use with
2379 * gtk_key_snooper_remove().
2380 *
2381 * Deprecated: 3.4: Key snooping should not be done. Events should
2382 * be handled by widgets.
2383 */
2384 guint
2385 gtk_key_snooper_install (GtkKeySnoopFunc snooper,
2386 gpointer func_data)
2387 {
2388 GtkKeySnooperData *data;
2389 static guint snooper_id = 1;
2390
2391 g_return_val_if_fail (snooper != NULL, 0);
2392
2393 data = g_new (GtkKeySnooperData, 1);
2394 data->func = snooper;
2395 data->func_data = func_data;
2396 data->id = snooper_id++;
2397 key_snoopers = g_slist_prepend (key_snoopers, data);
2398
2399 return data->id;
2400 }
2401
2402 /**
2403 * gtk_key_snooper_remove:
2404 * @snooper_handler_id: Identifies the key snooper to remove
2405 *
2406 * Removes the key snooper function with the given id.
2407 *
2408 * Deprecated: 3.4: Key snooping should not be done. Events should
2409 * be handled by widgets.
2410 */
2411 void
2412 gtk_key_snooper_remove (guint snooper_id)
2413 {
2414 GtkKeySnooperData *data = NULL;
2415 GSList *slist;
2416
2417 slist = key_snoopers;
2418 while (slist)
2419 {
2420 data = slist->data;
2421 if (data->id == snooper_id)
2422 break;
2423
2424 slist = slist->next;
2425 data = NULL;
2426 }
2427 if (data)
2428 {
2429 key_snoopers = g_slist_remove (key_snoopers, data);
2430 g_free (data);
2431 }
2432 }
2433
2434 static gint
2435 gtk_invoke_key_snoopers (GtkWidget *grab_widget,
2436 GdkEvent *event)
2437 {
2438 GSList *slist;
2439 gint return_val = FALSE;
2440
2441 return_val = _gtk_accessibility_key_snooper (grab_widget, (GdkEventKey *) event);
2442
2443 slist = key_snoopers;
2444 while (slist && !return_val)
2445 {
2446 GtkKeySnooperData *data;
2447
2448 data = slist->data;
2449 slist = slist->next;
2450 return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
2451 }
2452
2453 return return_val;
2454 }
2455
2456 /**
2457 * gtk_get_current_event:
2458 *
2459 * Obtains a copy of the event currently being processed by GTK+.
2460 *
2461 * For example, if you are handling a #GtkButton::clicked signal,
2462 * the current event will be the #GdkEventButton that triggered
2463 * the ::clicked signal.
2464 *
2465 * Returns: (transfer full) (nullable): a copy of the current event, or
2466 * %NULL if there is no current event. The returned event must be
2467 * freed with gdk_event_free().
2468 */
2469 GdkEvent*
2470 gtk_get_current_event (void)
2471 {
2472 if (current_events)
2473 return gdk_event_copy (current_events->data);
2474 else
2475 return NULL;
2476 }
2477
2478 /**
2479 * gtk_get_current_event_time:
2480 *
2481 * If there is a current event and it has a timestamp,
2482 * return that timestamp, otherwise return %GDK_CURRENT_TIME.
2483 *
2484 * Returns: the timestamp from the current event,
2485 * or %GDK_CURRENT_TIME.
2486 */
2487 guint32
2488 gtk_get_current_event_time (void)
2489 {
2490 if (current_events)
2491 return gdk_event_get_time (current_events->data);
2492 else
2493 return GDK_CURRENT_TIME;
2494 }
2495
2496 /**
2497 * gtk_get_current_event_state:
2498 * @state: (out): a location to store the state of the current event
2499 *
2500 * If there is a current event and it has a state field, place
2501 * that state field in @state and return %TRUE, otherwise return
2502 * %FALSE.
2503 *
2504 * Returns: %TRUE if there was a current event and it
2505 * had a state field
2506 */
2507 gboolean
2508 gtk_get_current_event_state (GdkModifierType *state)
2509 {
2510 g_return_val_if_fail (state != NULL, FALSE);
2511
2512 if (current_events)
2513 return gdk_event_get_state (current_events->data, state);
2514 else
2515 {
2516 *state = 0;
2517 return FALSE;
2518 }
2519 }
2520
2521 /**
2522 * gtk_get_current_event_device:
2523 *
2524 * If there is a current event and it has a device, return that
2525 * device, otherwise return %NULL.
2526 *
2527 * Returns: (transfer none) (nullable): a #GdkDevice, or %NULL
2528 */
2529 GdkDevice *
2530 gtk_get_current_event_device (void)
2531 {
2532 if (current_events)
2533 return gdk_event_get_device (current_events->data);
2534 else
2535 return NULL;
2536 }
2537
2538 /**
2539 * gtk_get_event_widget:
2540 * @event: a #GdkEvent
2541 *
2542 * If @event is %NULL or the event was not associated with any widget,
2543 * returns %NULL, otherwise returns the widget that received the event
2544 * originally.
2545 *
2546 * Returns: (transfer none) (nullable): the widget that originally
2547 * received @event, or %NULL
2548 */
2549 GtkWidget*
2550 gtk_get_event_widget (GdkEvent *event)
2551 {
2552 GtkWidget *widget;
2553 gpointer widget_ptr;
2554
2555 widget = NULL;
2556 if (event && event->any.window &&
2557 (event->type == GDK_DESTROY || !gdk_window_is_destroyed (event->any.window)))
2558 {
2559 gdk_window_get_user_data (event->any.window, &widget_ptr);
2560 widget = widget_ptr;
2561 }
2562
2563 return widget;
2564 }
2565
2566 static gboolean
2567 propagate_event_up (GtkWidget *widget,
2568 GdkEvent *event,
2569 GtkWidget *topmost)
2570 {
2571 gboolean handled_event = FALSE;
2572
2573 /* Propagate event up the widget tree so that
2574 * parents can see the button and motion
2575 * events of the children.
2576 */
2577 while (TRUE)
2578 {
2579 GtkWidget *tmp;
2580
2581 g_object_ref (widget);
2582
2583 /* Scroll events are special cased here because it
2584 * feels wrong when scrolling a GtkViewport, say,
2585 * to have children of the viewport eat the scroll
2586 * event
2587 */
2588 if (!gtk_widget_is_sensitive (widget))
2589 handled_event = event->type != GDK_SCROLL;
2590 else
2591 handled_event = gtk_widget_event (widget, event);
2592
2593 tmp = gtk_widget_get_parent (widget);
2594 g_object_unref (widget);
2595
2596 if (widget == topmost)
2597 break;
2598
2599 widget = tmp;
2600
2601 if (handled_event || !widget)
2602 break;
2603 }
2604
2605 return handled_event;
2606 }
2607
2608 static gboolean
2609 propagate_event_down (GtkWidget *widget,
2610 GdkEvent *event,
2611 GtkWidget *topmost)
2612 {
2613 gint handled_event = FALSE;
2614 GList *widgets = NULL;
2615 GList *l;
2616
2617 widgets = g_list_prepend (widgets, g_object_ref (widget));
2618 while (widget && widget != topmost)
2619 {
2620 widget = gtk_widget_get_parent (widget);
2621 if (!widget)
2622 break;
2623
2624 widgets = g_list_prepend (widgets, g_object_ref (widget));
2625
2626 if (widget == topmost)
2627 break;
2628 }
2629
2630 for (l = widgets; l && !handled_event; l = l->next)
2631 {
2632 widget = (GtkWidget *)l->data;
2633
2634 if (!gtk_widget_is_sensitive (widget))
2635 {
2636 /* stop propagating on SCROLL, but don't handle the event, so it
2637 * can propagate up again and reach its handling widget
2638 */
2639 if (event->type == GDK_SCROLL)
2640 break;
2641 else
2642 handled_event = TRUE;
2643 }
2644 else
2645 handled_event = _gtk_widget_captured_event (widget, event);
2646 }
2647 g_list_free_full (widgets, (GDestroyNotify)g_object_unref);
2648
2649 return handled_event;
2650 }
2651
2652 static gboolean
2653 propagate_event (GtkWidget *widget,
2654 GdkEvent *event,
2655 gboolean captured,
2656 GtkWidget *topmost)
2657 {
2658 gboolean handled_event = FALSE;
2659 gboolean (* propagate_func) (GtkWidget *widget, GdkEvent *event);
2660
2661 propagate_func = captured ? _gtk_widget_captured_event : gtk_widget_event;
2662
2663 if (event->type == GDK_KEY_PRESS || event->type == GDK_KEY_RELEASE)
2664 {
2665 /* Only send key events within Window widgets to the Window
2666 * The Window widget will in turn pass the
2667 * key event on to the currently focused widget
2668 * for that window.
2669 */
2670 GtkWidget *window;
2671
2672 window = gtk_widget_get_toplevel (widget);
2673 if (GTK_IS_WINDOW (window))
2674 {
2675 g_object_ref (widget);
2676 /* If there is a grab within the window, give the grab widget
2677 * a first crack at the key event
2678 */
2679 if (widget != window && gtk_widget_has_grab (widget))
2680 handled_event = propagate_func (widget, event);
2681
2682 if (!handled_event &&
2683 gtk_widget_is_sensitive (window))
2684 handled_event = propagate_func (window, event);
2685
2686 g_object_unref (widget);
2687 return handled_event;
2688 }
2689 }
2690
2691 /* Other events get propagated up/down the widget tree */
2692 return captured ?
2693 propagate_event_down (widget, event, topmost) :
2694 propagate_event_up (widget, event, topmost);
2695 }
2696
2697 /**
2698 * gtk_propagate_event:
2699 * @widget: a #GtkWidget
2700 * @event: an event
2701 *
2702 * Sends an event to a widget, propagating the event to parent widgets
2703 * if the event remains unhandled.
2704 *
2705 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
2706 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
2707 * the event may be propagated; if so, this function is used.
2708 *
2709 * gtk_propagate_event() calls gtk_widget_event() on each widget it
2710 * decides to send the event to. So gtk_widget_event() is the lowest-level
2711 * function; it simply emits the #GtkWidget::event and possibly an
2712 * event-specific signal on a widget. gtk_propagate_event() is a bit
2713 * higher-level, and gtk_main_do_event() is the highest level.
2714 *
2715 * All that said, you most likely don’t want to use any of these
2716 * functions; synthesizing events is rarely needed. There are almost
2717 * certainly better ways to achieve your goals. For example, use
2718 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
2719 * of making up expose events.
2720 */
2721 void
2722 gtk_propagate_event (GtkWidget *widget,
2723 GdkEvent *event)
2724 {
2725 g_return_if_fail (GTK_IS_WIDGET (widget));
2726 g_return_if_fail (event != NULL);
2727
2728 propagate_event (widget, event, FALSE, NULL);
2729 }
2730
2731 gboolean
2732 _gtk_propagate_captured_event (GtkWidget *widget,
2733 GdkEvent *event,
2734 GtkWidget *topmost)
2735 {
2736 return propagate_event (widget, event, TRUE, topmost);
2737 }