gtkmain.c 77 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * 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.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
17 18

/*
19
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20 21 22 23 24
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
 * SECTION:gtkmain
 * @Short_description: Library initialization, main event loop, and events
 * @Title: Main loop and Events
 * @See_also:See the GLib manual, especially #GMainLoop and signal-related
 *    functions such as g_signal_connect()
 *
 * Before using GTK+, you need to initialize it; initialization connects to the
 * window system display, and parses some standard command line arguments. The
 * gtk_init() macro initializes GTK+. gtk_init() exits the application if errors
 * occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to
 * recover from a failed GTK+ initialization - you might start up your
 * application in text mode instead.
 *
 * 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 44 45
 * event to one or more widgets.
 *
 * 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 52
 *
 * When your callbacks are invoked, you would typically take some action - for
 * 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 57
 * ## Typical main() function for a GTK+ application
 *
58
 * |[<!-- language="C" -->
59 60 61
 * int
 * main (int argc, char **argv)
 * {
62 63
 *   // Initialize i18n support with bindtextdomain(), etc.
 *
64
 *   ...
65
 *
66
 *   // Initialize the widget set
67 68
 *   gtk_init (&argc, &argv);
 *
69
 *   // Create the main window
70 71
 *   mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 *
72 73
 *   // Set up our GUI elements
 *
74 75
 *   ...
 *
76
 *   // Show the application window
77 78
 *   gtk_widget_show_all (mainwin);
 *
79
 *   // Enter the main event loop, and wait for user interaction
80 81
 *   gtk_main ();
 *
82
 *   // The user lost interest
83 84
 *   return 0;
 * }
85
 * ]|
86
 *
87
 * It’s OK to use the GLib main loop directly instead of gtk_main(), though it
88 89 90
 * involves slightly more typing. See #GMainLoop in the GLib documentation.
 */

91
#include "config.h"
92

93
#include "gdk/gdk.h"
94
#include "gdk/gdk-private.h"
95

96
#include <locale.h>
Robert Brady's avatar
Robert Brady committed
97

Elliot Lee's avatar
Elliot Lee committed
98 99
#include <stdio.h>
#include <stdlib.h>
100
#include <string.h>
101
#ifdef HAVE_UNISTD_H
102 103
#include <unistd.h>
#endif
104
#include <sys/types.h>          /* For uid_t, gid_t */
105

Tor Lillqvist's avatar
Tor Lillqvist committed
106 107 108 109 110
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif
111

Tor Lillqvist's avatar
Tor Lillqvist committed
112 113
#include "gtkintl.h"

114
#include "gtkaccelmapprivate.h"
115
#include "gtkbox.h"
116
#include "gtkclipboardprivate.h"
117
#include "gtkdebug.h"
118
#include "gtkdndprivate.h"
119 120
#include "gtkmain.h"
#include "gtkmenu.h"
121
#include "gtkmodules.h"
122
#include "gtkmodulesprivate.h"
123
#include "gtkprivate.h"
124
#include "gtkrecentmanager.h"
125
#include "gtkselectionprivate.h"
126
#include "gtksettingsprivate.h"
127
#include "gtktooltipprivate.h"
128
#include "gtkversion.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
129
#include "gtkwidgetprivate.h"
130
#include "gtkwindowprivate.h"
131
#include "gtkwindowgroup.h"
132

Matthias Clasen's avatar
Matthias Clasen committed
133
#include "a11y/gtkaccessibility.h"
134

Elliot Lee's avatar
Elliot Lee committed
135 136
/* Private type definitions
 */
137
typedef struct _GtkKeySnooperData        GtkKeySnooperData;
Elliot Lee's avatar
Elliot Lee committed
138

139 140 141 142
struct _GtkKeySnooperData
{
  GtkKeySnoopFunc func;
  gpointer func_data;
143
  guint id;
144 145
};

146 147
static gint  gtk_invoke_key_snoopers     (GtkWidget          *grab_widget,
                                          GdkEvent           *event);
148

149 150
static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);

151
static guint gtk_main_loop_level = 0;
Matthias Clasen's avatar
Matthias Clasen committed
152
static gint pre_initialized = FALSE;
153
static gint gtk_initialized = FALSE;
154
static GList *current_events = NULL;
Elliot Lee's avatar
Elliot Lee committed
155

156 157
static GSList *main_loops = NULL;      /* stack of currently executing main loops */

158 159
static GSList *key_snoopers = NULL;

160 161 162 163 164 165 166 167
typedef struct {
  GdkDisplay *display;
  guint flags;
} DisplayDebugFlags;

#define N_DEBUG_DISPLAYS 4

DisplayDebugFlags debug_flags[N_DEBUG_DISPLAYS];
168 169

#ifdef G_ENABLE_DEBUG
170
static const GDebugKey gtk_debug_keys[] = {
Matthias Clasen's avatar
Matthias Clasen committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  { "misc", GTK_DEBUG_MISC },
  { "plugsocket", GTK_DEBUG_PLUGSOCKET },
  { "text", GTK_DEBUG_TEXT },
  { "tree", GTK_DEBUG_TREE },
  { "updates", GTK_DEBUG_UPDATES },
  { "keybindings", GTK_DEBUG_KEYBINDINGS },
  { "multihead", GTK_DEBUG_MULTIHEAD },
  { "modules", GTK_DEBUG_MODULES },
  { "geometry", GTK_DEBUG_GEOMETRY },
  { "icontheme", GTK_DEBUG_ICONTHEME },
  { "printing", GTK_DEBUG_PRINTING} ,
  { "builder", GTK_DEBUG_BUILDER },
  { "size-request", GTK_DEBUG_SIZE_REQUEST },
  { "no-css-cache", GTK_DEBUG_NO_CSS_CACHE },
  { "baselines", GTK_DEBUG_BASELINES },
  { "pixel-cache", GTK_DEBUG_PIXEL_CACHE },
  { "no-pixel-cache", GTK_DEBUG_NO_PIXEL_CACHE },
  { "interactive", GTK_DEBUG_INTERACTIVE },
  { "touchscreen", GTK_DEBUG_TOUCHSCREEN },
  { "actions", GTK_DEBUG_ACTIONS },
  { "resize", GTK_DEBUG_RESIZE },
  { "layout", GTK_DEBUG_LAYOUT }
193 194
};
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
195

196
/**
197
 * gtk_get_major_version:
198
 *
199 200
 * Returns the major version number of the GTK+ library.
 * (e.g. in GTK+ version 3.1.5 this is 3.)
201 202 203 204 205 206
 *
 * This function is in the library, so it represents the GTK+ library
 * your code is running against. Contrast with the #GTK_MAJOR_VERSION
 * macro, which represents the major version of the GTK+ headers you
 * have included when compiling your code.
 *
207
 * Returns: the major version number of the GTK+ library
208 209
 *
 * Since: 3.0
210 211
 */
guint
212
gtk_get_major_version (void)
213 214 215 216 217
{
  return GTK_MAJOR_VERSION;
}

/**
218
 * gtk_get_minor_version:
219
 *
220 221
 * Returns the minor version number of the GTK+ library.
 * (e.g. in GTK+ version 3.1.5 this is 1.)
222 223 224 225 226 227
 *
 * This function is in the library, so it represents the GTK+ library
 * your code is are running against. Contrast with the
 * #GTK_MINOR_VERSION macro, which represents the minor version of the
 * GTK+ headers you have included when compiling your code.
 *
228
 * Returns: the minor version number of the GTK+ library
229 230
 *
 * Since: 3.0
231 232
 */
guint
233
gtk_get_minor_version (void)
234 235 236 237 238
{
  return GTK_MINOR_VERSION;
}

/**
239
 * gtk_get_micro_version:
240
 *
241 242
 * Returns the micro version number of the GTK+ library.
 * (e.g. in GTK+ version 3.1.5 this is 5.)
243 244 245 246 247 248
 *
 * This function is in the library, so it represents the GTK+ library
 * your code is are running against. Contrast with the
 * #GTK_MICRO_VERSION macro, which represents the micro version of the
 * GTK+ headers you have included when compiling your code.
 *
249
 * Returns: the micro version number of the GTK+ library
250 251
 *
 * Since: 3.0
252 253
 */
guint
254
gtk_get_micro_version (void)
255 256 257 258 259
{
  return GTK_MICRO_VERSION;
}

/**
260
 * gtk_get_binary_age:
261
 *
262
 * Returns the binary age as passed to `libtool`
263
 * when building the GTK+ library the process is running against.
264
 * If `libtool` means nothing to you, don't
265 266
 * worry about it.
 *
267
 * Returns: the binary age of the GTK+ library
268 269
 *
 * Since: 3.0
270 271
 */
guint
272
gtk_get_binary_age (void)
273 274 275 276 277
{
  return GTK_BINARY_AGE;
}

/**
278
 * gtk_get_interface_age:
279
 *
280
 * Returns the interface age as passed to `libtool`
281
 * when building the GTK+ library the process is running against.
282
 * If `libtool` means nothing to you, don't
283 284
 * worry about it.
 *
285
 * Returns: the interface age of the GTK+ library
286 287
 *
 * Since: 3.0
288 289
 */
guint
290
gtk_get_interface_age (void)
291 292 293 294
{
  return GTK_INTERFACE_AGE;
}

295 296
/**
 * gtk_check_version:
297 298 299 300
 * @required_major: the required major version
 * @required_minor: the required minor version
 * @required_micro: the required micro version
 *
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
 * Checks that the GTK+ library in use is compatible with the
 * given version. Generally you would pass in the constants
 * #GTK_MAJOR_VERSION, #GTK_MINOR_VERSION, #GTK_MICRO_VERSION
 * as the three arguments to this function; that produces
 * a check that the library in use is compatible with
 * the version of GTK+ the application or module was compiled
 * against.
 *
 * Compatibility is defined by two things: first the version
 * of the running library is newer than the version
 * @required_major.required_minor.@required_micro. Second
 * the running library must be binary compatible with the
 * version @required_major.required_minor.@required_micro
 * (same major version.)
 *
 * This function is primarily for GTK+ modules; the module
317
 * can call this function to check that it wasn’t loaded
318
 * into an incompatible version of GTK+. However, such a
319
 * check isn’t completely reliable, since the module may be
320 321 322 323
 * linked against an old version of GTK+ and calling the
 * old version of gtk_check_version(), but still get loaded
 * into an application using a newer version of GTK+.
 *
324
 * Returns: (nullable): %NULL if the GTK+ library is compatible with the
325 326 327
 *   given version, or a string describing the version mismatch.
 *   The returned string is owned by GTK+ and should not be modified
 *   or freed.
328
 */
329
const gchar*
330
gtk_check_version (guint required_major,
331 332
                   guint required_minor,
                   guint required_micro)
333
{
334 335 336
  gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
  gint required_effective_micro = 100 * required_minor + required_micro;

337
  if (required_major > GTK_MAJOR_VERSION)
338
    return "GTK+ version too old (major mismatch)";
339
  if (required_major < GTK_MAJOR_VERSION)
340
    return "GTK+ version too new (major mismatch)";
341
  if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
342
    return "GTK+ version too new (micro mismatch)";
343
  if (required_effective_micro > gtk_effective_micro)
344
    return "GTK+ version too old (micro mismatch)";
345 346 347
  return NULL;
}

Owen Taylor's avatar
Owen Taylor committed
348
/* This checks to see if the process is running suid or sgid
349
 * at the current time. If so, we don’t allow GTK+ to be initialized.
Owen Taylor's avatar
Owen Taylor committed
350 351 352
 * This is meant to be a mild check - we only error out if we
 * can prove the programmer is doing something wrong, not if
 * they could be doing something wrong. For this reason, we
353
 * don’t use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
Owen Taylor's avatar
Owen Taylor committed
354 355 356 357
 */
static gboolean
check_setugid (void)
{
358
/* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
359
#ifndef G_OS_WIN32
Owen Taylor's avatar
Owen Taylor committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
  gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
  
#ifdef HAVE_GETRESUID
  /* These aren't in the header files, so we prototype them here.
   */
  int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
  int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);

  if (getresuid (&ruid, &euid, &suid) != 0 ||
      getresgid (&rgid, &egid, &sgid) != 0)
#endif /* HAVE_GETRESUID */
    {
      suid = ruid = getuid ();
      sgid = rgid = getgid ();
      euid = geteuid ();
      egid = getegid ();
    }

  if (ruid != euid || ruid != suid ||
      rgid != egid || rgid != sgid)
    {
      g_warning ("This process is currently running setuid or setgid.\n"
383 384 385 386
                 "This is not a supported use of GTK+. You must create a helper\n"
                 "program instead. For further details, see:\n\n"
                 "    http://www.gtk.org/setuid.html\n\n"
                 "Refusing to initialize GTK+.");
Owen Taylor's avatar
Owen Taylor committed
387 388
      exit (1);
    }
389
#endif
Owen Taylor's avatar
Owen Taylor committed
390 391 392
  return TRUE;
}

393 394 395 396 397
static gboolean do_setlocale = TRUE;

/**
 * gtk_disable_setlocale:
 * 
398 399
 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
 * gtk_parse_args() from automatically
400
 * calling `setlocale (LC_ALL, "")`. You would
401
 * want to use this function if you wanted to set the locale for
402
 * your program to something other than the user’s locale, or if
403
 * you wanted to set different values for different locale categories.
404 405 406
 *
 * Most programs should not need to call this function.
 **/
407
void
408 409
gtk_disable_setlocale (void)
{
Matthias Clasen's avatar
Matthias Clasen committed
410
  if (pre_initialized)
411 412 413 414 415
    g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
    
  do_setlocale = FALSE;
}

416
#ifdef G_PLATFORM_WIN32
417
#undef gtk_init_check
418
#endif
419

420
static GString *gtk_modules_string = NULL;
421
static gboolean g_fatal_warnings = FALSE;
422 423 424 425

#ifdef G_ENABLE_DEBUG
static gboolean
gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
Elliot Lee's avatar
Elliot Lee committed
426
{
427 428 429
  debug_flags[0].flags |= g_parse_debug_string (value,
                                                gtk_debug_keys,
                                                G_N_ELEMENTS (gtk_debug_keys));
430

431 432
  return TRUE;
}
Tim Janik's avatar
Tim Janik committed
433

434 435 436
static gboolean
gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
{
437 438 439
  debug_flags[0].flags &= ~g_parse_debug_string (value,
                                                 gtk_debug_keys,
                                                 G_N_ELEMENTS (gtk_debug_keys));
440 441

  return TRUE;
442 443 444 445 446 447 448 449 450
}
#endif /* G_ENABLE_DEBUG */

static gboolean
gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
{
  if (value && *value)
    {
      if (gtk_modules_string)
451
        g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
452
      else
453
        gtk_modules_string = g_string_new (NULL);
454 455 456 457 458 459 460
      
      g_string_append (gtk_modules_string, value);
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
461
static const GOptionEntry gtk_args[] = {
462 463 464
  { "gtk-module",       0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,   
    /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"), 
    /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
465
  { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, 
466
    /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
467
#ifdef G_ENABLE_DEBUG
468 469 470 471 472 473 474
  { "gtk-debug",        0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,    
    /* Description of --gtk-debug=FLAGS in --help output */    N_("GTK+ debugging flags to set"), 
    /* Placeholder in --gtk-debug=FLAGS in --help output */    N_("FLAGS") },
  { "gtk-no-debug",     0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb, 
    /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"), 
    /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
#endif 
475 476 477
  { NULL }
};

Tor Lillqvist's avatar
Tor Lillqvist committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
#ifdef G_OS_WIN32

static char *iso639_to_check = NULL;
static char *iso3166_to_check = NULL;
static char *script_to_check = NULL;
static gboolean setlocale_called = FALSE;

static BOOL CALLBACK
enum_locale_proc (LPTSTR locale)
{
  LCID lcid;
  char iso639[10];
  char iso3166[10];
  char *endptr;


  lcid = strtoul (locale, &endptr, 16);
  if (*endptr == '\0' &&
      GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) &&
      GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
    {
      if (strcmp (iso639, iso639_to_check) == 0 &&
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
          ((iso3166_to_check != NULL &&
            strcmp (iso3166, iso3166_to_check) == 0) ||
           (iso3166_to_check == NULL &&
            SUBLANGID (LANGIDFROMLCID (lcid)) == SUBLANG_DEFAULT)))
        {
          char language[100], country[100];
          char locale[300];

          if (script_to_check != NULL)
            {
              /* If lcid is the "other" script for this language,
               * return TRUE, i.e. continue looking.
               */
              if (strcmp (script_to_check, "Latn") == 0)
                {
                  switch (LANGIDFROMLCID (lcid))
                    {
                    case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_CYRILLIC):
                      return TRUE;
                    case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC):
                      return TRUE;
                    case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC):
                      return TRUE;
                    case MAKELANGID (LANG_SERBIAN, 0x07):
                      /* Serbian in Bosnia and Herzegovina, Cyrillic */
                      return TRUE;
                    }
                }
              else if (strcmp (script_to_check, "Cyrl") == 0)
                {
                  switch (LANGIDFROMLCID (lcid))
                    {
                    case MAKELANGID (LANG_AZERI, SUBLANG_AZERI_LATIN):
                      return TRUE;
                    case MAKELANGID (LANG_UZBEK, SUBLANG_UZBEK_LATIN):
                      return TRUE;
                    case MAKELANGID (LANG_SERBIAN, SUBLANG_SERBIAN_LATIN):
                      return TRUE;
                    case MAKELANGID (LANG_SERBIAN, 0x06):
                      /* Serbian in Bosnia and Herzegovina, Latin */
                      return TRUE;
                    }
                }
            }

          SetThreadLocale (lcid);

          if (GetLocaleInfo (lcid, LOCALE_SENGLANGUAGE, language, sizeof (language)) &&
              GetLocaleInfo (lcid, LOCALE_SENGCOUNTRY, country, sizeof (country)))
            {
              strcpy (locale, language);
              strcat (locale, "_");
              strcat (locale, country);

              if (setlocale (LC_ALL, locale) != NULL)
                setlocale_called = TRUE;
            }

          return FALSE;
        }
Tor Lillqvist's avatar
Tor Lillqvist committed
560 561 562 563 564 565 566
    }

  return TRUE;
}
  
#endif

567
static void
568
setlocale_initialization (void)
569
{
570
  static gboolean initialized = FALSE;
571

572
  if (initialized)
Matthias Clasen's avatar
Matthias Clasen committed
573
    return;
574
  initialized = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
575

576
  if (do_setlocale)
577
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
578 579 580 581 582 583
#ifdef G_OS_WIN32
      /* If some of the POSIXish environment variables are set, set
       * the Win32 thread locale correspondingly.
       */ 
      char *p = getenv ("LC_ALL");
      if (p == NULL)
584
        p = getenv ("LANG");
Tor Lillqvist's avatar
Tor Lillqvist committed
585 586

      if (p != NULL)
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
        {
          p = g_strdup (p);
          if (strcmp (p, "C") == 0)
            SetThreadLocale (LOCALE_SYSTEM_DEFAULT);
          else
            {
              /* Check if one of the supported locales match the
               * environment variable. If so, use that locale.
               */
              iso639_to_check = p;
              iso3166_to_check = strchr (iso639_to_check, '_');
              if (iso3166_to_check != NULL)
                {
                  *iso3166_to_check++ = '\0';

                  script_to_check = strchr (iso3166_to_check, '@');
                  if (script_to_check != NULL)
                    *script_to_check++ = '\0';

                  /* Handle special cases. */

                  /* The standard code for Serbia and Montenegro was
                   * "CS", but MSFT uses for some reason "SP". By now
                   * (October 2006), SP has split into two, "RS" and
                   * "ME", but don't bother trying to handle those
                   * yet. Do handle the even older "YU", though.
                   */
                  if (strcmp (iso3166_to_check, "CS") == 0 ||
                      strcmp (iso3166_to_check, "YU") == 0)
                    iso3166_to_check = "SP";
                }
              else
                {
                  script_to_check = strchr (iso639_to_check, '@');
                  if (script_to_check != NULL)
                    *script_to_check++ = '\0';
                  /* LANG_SERBIAN == LANG_CROATIAN, recognize just "sr" */
                  if (strcmp (iso639_to_check, "sr") == 0)
                    iso3166_to_check = "SP";
                }

              EnumSystemLocales (enum_locale_proc, LCID_SUPPORTED);
            }
          g_free (p);
        }
Tor Lillqvist's avatar
Tor Lillqvist committed
632
      if (!setlocale_called)
633
        setlocale (LC_ALL, "");
Tor Lillqvist's avatar
Tor Lillqvist committed
634
#else
635
      if (!setlocale (LC_ALL, ""))
636
        g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
Tor Lillqvist's avatar
Tor Lillqvist committed
637
#endif
638
    }
639 640 641 642
}

static void
do_pre_parse_initialization (int    *argc,
643
                             char ***argv)
644 645
{
  const gchar *env_string;
646
  double slowdown;
647 648 649 650 651
  
  if (pre_initialized)
    return;

  pre_initialized = TRUE;
652

653 654
  if (_gtk_module_has_mixed_deps (NULL))
    g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
655

656
  GDK_PRIVATE_CALL (gdk_pre_parse) ();
Owen Taylor's avatar
Owen Taylor committed
657
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
658

659
#ifdef G_ENABLE_DEBUG
Matthias Clasen's avatar
Matthias Clasen committed
660
  env_string = g_getenv ("GTK_DEBUG");
661 662
  if (env_string != NULL)
    {
663 664 665
      debug_flags[0].flags = g_parse_debug_string (env_string,
                                                   gtk_debug_keys,
                                                   G_N_ELEMENTS (gtk_debug_keys));
666 667
      env_string = NULL;
    }
668
#endif  /* G_ENABLE_DEBUG */
669

670
  env_string = g_getenv ("GTK3_MODULES");
671
  if (env_string)
672
    gtk_modules_string = g_string_new (env_string);
673 674 675 676 677 678 679 680 681 682 683

  env_string = g_getenv ("GTK_MODULES");
  if (env_string)
    {
      if (gtk_modules_string)
        g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
      else
        gtk_modules_string = g_string_new (NULL);

      g_string_append (gtk_modules_string, env_string);
    }
684 685 686 687 688 689 690

  env_string = g_getenv ("GTK_SLOWDOWN");
  if (env_string)
    {
      slowdown = g_ascii_strtod (env_string, NULL);
      _gtk_set_slowdown (slowdown);
    }
691
}
692

693 694 695
static void
gettext_initialization (void)
{
696 697
  setlocale_initialization ();

698
#ifdef ENABLE_NLS
699 700
  bindtextdomain (GETTEXT_PACKAGE, _gtk_get_localedir ());
  bindtextdomain (GETTEXT_PACKAGE "-properties", _gtk_get_localedir ());
701 702 703 704 705 706 707
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
#    endif
#endif  
}

708 709 710 711
static void
default_display_notify_cb (GdkDisplayManager *dm)
{
  _gtk_accessibility_init ();
712
  debug_flags[0].display = gdk_display_get_default ();
713 714
}

715 716
static void
do_post_parse_initialization (int    *argc,
717
                              char ***argv)
718
{
719 720
  GdkDisplayManager *display_manager;

721 722 723
  if (gtk_initialized)
    return;

724 725
  gettext_initialization ();

726
#ifdef SIGPIPE
727
  signal (SIGPIPE, SIG_IGN);
728
#endif
729

730 731 732 733 734 735 736 737 738
  if (g_fatal_warnings)
    {
      GLogLevelFlags fatal_mask;

      fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
      fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
      g_log_set_always_fatal (fatal_mask);
    }

739
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
740
  if (debug_flags[0].flags & GTK_DEBUG_UPDATES)
741
    gdk_window_set_debug_updates (TRUE);
742
G_GNUC_END_IGNORE_DEPRECATIONS
743

744
  gtk_widget_set_default_direction (gtk_get_locale_direction ());
745

746
  _gtk_ensure_resources ();
747

748
  _gtk_accel_map_init ();
749

750
  gtk_initialized = TRUE;
751

752
  if (gtk_modules_string)
753
    {
754 755
      _gtk_modules_init (argc, argv, gtk_modules_string->str);
      g_string_free (gtk_modules_string, TRUE);
756
    }
757 758 759 760
  else
    {
      _gtk_modules_init (argc, argv, NULL);
    }
761

762 763 764 765 766
  display_manager = gdk_display_manager_get ();
  if (gdk_display_manager_get_default_display (display_manager) != NULL)
    _gtk_accessibility_init ();

  g_signal_connect (display_manager, "notify::default-display",
767 768
                    G_CALLBACK (default_display_notify_cb),
                    NULL);
769 770 771 772 773 774 775 776 777 778
}


typedef struct
{
  gboolean open_default_display;
} OptionGroupInfo;

static gboolean
pre_parse_hook (GOptionContext *context,
779 780 781
                GOptionGroup   *group,
                gpointer        data,
                GError        **error)
782 783 784 785 786 787 788 789
{
  do_pre_parse_initialization (NULL, NULL);
  
  return TRUE;
}

static gboolean
post_parse_hook (GOptionContext *context,
790 791 792
                 GOptionGroup   *group,
                 gpointer       data,
                 GError        **error)
793 794 795 796 797 798 799
{
  OptionGroupInfo *info = data;

  
  do_post_parse_initialization (NULL, NULL);
  
  if (info->open_default_display)
800
    {
801
      if (GDK_PRIVATE_CALL (gdk_display_open_default) () == NULL)
802 803 804 805 806 807 808 809 810 811
        {
          const char *display_name = gdk_get_display_arg_name ();
          g_set_error (error,
                       G_OPTION_ERROR,
                       G_OPTION_ERROR_FAILED,
                       _("Cannot open display: %s"),
                       display_name ? display_name : "" );

          return FALSE;
        }
812 813 814
    }

  return TRUE;
815 816
}

817 818 819
guint
gtk_get_display_debug_flags (GdkDisplay *display)
{
820 821 822 823 824 825 826 827 828
  gint i;

  for (i = 0; i < N_DEBUG_DISPLAYS; i++)
    {
      if (debug_flags[i].display == display)
        return debug_flags[i].flags;
    }

  return 0;
829 830 831 832 833 834
}

void
gtk_set_display_debug_flags (GdkDisplay *display,
                             guint       flags)
{
835 836 837 838 839 840 841 842 843 844 845 846 847
  gint i;

  for (i = 0; i < N_DEBUG_DISPLAYS; i++)
    {
      if (debug_flags[i].display == NULL)
        debug_flags[i].display = display;

      if (debug_flags[i].display == display)
        {
          debug_flags[i].flags = flags;
          return;
        }
    }
848
}
849

850 851 852
/**
 * gtk_get_debug_flags:
 *
853 854 855 856 857 858
 * Returns the GTK+ debug flags.
 *
 * This function is intended for GTK+ modules that want
 * to adjust their debug output based on GTK+ debug flags.
 *
 * Returns: the GTK+ debug flags.
859 860 861 862
 */
guint
gtk_get_debug_flags (void)
{
863
  return gtk_get_display_debug_flags (gdk_display_get_default ());
864 865 866 867 868 869 870 871 872 873
}

/**
 * gtk_set_debug_flags:
 *
 * Sets the GTK+ debug flags.
 */
void
gtk_set_debug_flags (guint flags)
{
874
  gtk_set_display_debug_flags (gdk_display_get_default (), flags);
875 876
}

877 878 879 880 881 882 883 884
gboolean
gtk_simulate_touchscreen (void)
{
  static gint test_touchscreen;

  if (test_touchscreen == 0)
    test_touchscreen = g_getenv ("GTK_TEST_TOUCHSCREEN") != NULL ? 1 : -1;

885
  return test_touchscreen > 0 || (gtk_get_debug_flags () & GTK_DEBUG_TOUCHSCREEN) != 0;
886 887
 }

888
/**
889
 * gtk_get_option_group:
890 891 892
 * @open_default_display: whether to open the default display
 *     when parsing the commandline arguments
 *
893
 * Returns a #GOptionGroup for the commandline arguments recognized
894 895 896 897
 * by GTK+ and GDK.
 *
 * You should add this group to your #GOptionContext
 * with g_option_context_add_group(), if you are using
898 899
 * g_option_context_parse() to parse your commandline arguments.
 *
900 901
 * Returns: (transfer full): a #GOptionGroup for the commandline
 *     arguments recognized by GTK+
902 903 904
 *
 * Since: 2.6
 */
905 906 907 908 909 910
GOptionGroup *
gtk_get_option_group (gboolean open_default_display)
{
  GOptionGroup *group;
  OptionGroupInfo *info;

911 912
  gettext_initialization ();

913 914 915 916 917 918
  info = g_new0 (OptionGroupInfo, 1);
  info->open_default_display = open_default_display;
  
  group = g_option_group_new ("gtk", _("GTK+ Options"), _("Show GTK+ Options"), info, g_free);
  g_option_group_set_parse_hooks (group, pre_parse_hook, post_parse_hook);

919
  GDK_PRIVATE_CALL (gdk_add_option_entries) (group);
920
  g_option_group_add_entries (group, gtk_args);
921
  g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
922 923 924 925
  
  return group;
}

926 927
/**
 * gtk_init_with_args:
928
 * @argc: (inout): Address of the `argc` parameter of
929 930 931
 *     your main() function (or 0 if @argv is %NULL). This will be changed if 
 *     any arguments were handled.
 * @argv: (array length=argc) (inout) (allow-none): Address of the
932
 *     `argv` parameter of main(), or %NULL. Any options
933
 *     understood by GTK+ are stripped before return.
934
 * @parameter_string: (allow-none): a string which is displayed in
935
 *    the first line of `--help` output, after
936
 *    `programname [OPTION...]`
937 938
 * @entries: (array zero-terminated=1): a %NULL-terminated array
 *    of #GOptionEntrys describing the options of your program
939
 * @translation_domain: (nullable): a translation domain to use for translating
940
 *    the `--help` output for the options in @entries
941 942
 *    and the @parameter_string with gettext(), or %NULL
 * @error: a return location for errors
943
 *
944 945 946
 * This function does the same work as gtk_init_check().
 * Additionally, it allows you to add your own commandline options,
 * and it automatically generates nicely formatted
947
 * `--help` output. Note that your program will
948 949
 * be terminated after writing out the help output.
 *
950 951
 * Returns: %TRUE if the windowing system has been successfully
 *     initialized, %FALSE otherwise
952
 *
953 954
 * Since: 2.6
 */
955
gboolean
956 957 958 959 960 961
gtk_init_with_args (gint                 *argc,
                    gchar              ***argv,
                    const gchar          *parameter_string,
                    const GOptionEntry   *entries,
                    const gchar          *translation_domain,
                    GError              **error)
962 963 964 965
{
  GOptionContext *context;
  GOptionGroup *gtk_group;
  gboolean retval;
Matthias Clasen's avatar
Matthias Clasen committed
966

967
  if (gtk_initialized)
968
    goto done;
969

970 971
  gettext_initialization ();

Matthias Clasen's avatar
Matthias Clasen committed
972 973 974
  if (!check_setugid ())
    return FALSE;

975
  gtk_group = gtk_get_option_group (FALSE);
976

977 978
  context = g_option_context_new (parameter_string);
  g_option_context_add_group (context, gtk_group);
979 980
  g_option_context_set_translation_domain (context, translation_domain);

981 982 983
  if (entries)
    g_option_context_add_main_entries (context, entries, translation_domain);
  retval = g_option_context_parse (context, argc, argv, error);
984

985 986
  g_option_context_free (context);

987 988 989 990 991
  if (!retval)
    return FALSE;

done:
  return GDK_PRIVATE_CALL (gdk_display_open_default) () != NULL;
992 993 994 995 996
}


/**
 * gtk_parse_args:
997
 * @argc: (inout): a pointer to the number of command line arguments
998 999
 * @argv: (array length=argc) (inout): a pointer to the array of
 *     command line arguments
1000
 *
1001 1002 1003 1004 1005 1006 1007
 * Parses command line arguments, and initializes global
 * attributes of GTK+, but does not actually open a connection
 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
 *
 * Any arguments used by GTK+ or GDK are removed from the array and
 * @argc and @argv are updated accordingly.
 *
1008
 * There is no need to call this function explicitly if you are using
1009 1010
 * gtk_init(), or gtk_init_check().
 *
1011 1012 1013 1014
 * Note that many aspects of GTK+ require a display connection to
 * function, so this way of initializing GTK+ is really only useful
 * for specialized use cases.
 *
1015
 * Returns: %TRUE if initialization succeeded, otherwise %FALSE
1016
 */
1017 1018
gboolean
gtk_parse_args (int    *argc,
1019
                char ***argv)
1020 1021
{
  GOptionContext *option_context;
1022
  GOptionGroup *gtk_group;
1023
  GError *error = NULL;
1024 1025 1026 1027
  
  if (gtk_initialized)
    return TRUE;

1028 1029
  gettext_initialization ();

1030 1031 1032 1033 1034 1035
  if (!check_setugid ())
    return FALSE;

  option_context = g_option_context_new (NULL);
  g_option_context_set_ignore_unknown_options (option_context, TRUE);
  g_option_context_set_help_enabled (option_context, FALSE);
1036 1037
  gtk_group = gtk_get_option_group (FALSE);
  g_option_context_set_main_group (option_context, gtk_group);
1038 1039 1040 1041 1042 1043
  if (!g_option_context_parse (option_context, argc, argv, &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }

1044 1045
  g_option_context_free (option_context);

1046 1047
  return TRUE;
}
1048

1049
#ifdef G_PLATFORM_WIN32
1050
#undef gtk_init_check
1051
#endif
1052 1053 1054

/**
 * gtk_init_check:
1055
 * @argc: (inout): Address of the `argc` parameter of
1056 1057
 *     your main() function (or 0 if @argv is %NULL). This will be changed if 
 *     any arguments were handled.
1058
 * @argv: (array length=argc) (inout) (allow-none): Address of the
1059
 *     `argv` parameter of main(), or %NULL. Any options
1060
 *     understood by GTK+ are stripped before return.
1061
 *
1062 1063
 * This function does the same work as gtk_init() with only a single
 * change: It does not terminate the program if the windowing system
1064
 * can’t be initialized. Instead it returns %FALSE on failure.
1065
 *
1066 1067 1068 1069
 * This way the application can fall back to some other means of
 * communication with the user - for example a curses or command line
 * interface.
 *
1070
 * Returns: %TRUE if the windowing system has been successfully
1071 1072
 *     initialized, %FALSE otherwise
 */
1073
gboolean
1074 1075
gtk_init_check (int    *argc,
                char ***argv)
1076
{
1077 1078
  gboolean ret;

1079 1080 1081
  if (!gtk_parse_args (argc, argv))
    return FALSE;

1082
  ret = GDK_PRIVATE_CALL (gdk_display_open_default) () != NULL;
1083

1084
  if (gtk_get_debug_flags () & GTK_DEBUG_INTERACTIVE)
1085 1086 1087
    gtk_window_set_interactive_debugging (TRUE);

  return ret;
1088 1089
}

1090
#ifdef G_PLATFORM_WIN32
1091
#undef gtk_init
1092
#endif
1093

1094
/**
Matthias Clasen's avatar
Matthias Clasen committed
1095
 * gtk_init:
1096
 * @argc: (inout): Address of the `argc` parameter of
1097 1098
 *     your main() function (or 0 if @argv is %NULL). This will be changed if 
 *     any arguments were handled.
1099
 * @argv: (array length=argc) (inout) (allow-none): Address of the
1100
 *     `argv` parameter of main(), or %NULL. Any options
1101
 *     understood by GTK+ are stripped before return.
1102
 *
1103
 * Call this function before using any other GTK+ functions in your GUI
1104
 * applications.  It will initialize everything needed to operate the
1105
 * toolkit and parses some standard command line options.
1106
 *
1107 1108 1109 1110
 * Although you are expected to pass the @argc, @argv parameters from main() to 
 * this function, it is possible to pass %NULL if @argv is not available or 
 * commandline handling is not required.
 *
1111 1112 1113 1114 1115 1116 1117
 * @argc and @argv are adjusted accordingly so your own code will
 * never see those standard arguments.
 *
 * Note that there are some alternative ways to initialize GTK+:
 * if you are calling gtk_parse_args(), gtk_init_check(),
 * gtk_init_with_args() or g_option_context_parse() with
 * the option group returned by gtk_get_option_group(),
1118
 * you don’t have to call gtk_init().
1119
 *
1120 1121 1122 1123
 * And if you are using #GtkApplication, you don't have to call any of the
 * initialization functions either; the #GtkApplication::startup handler
 * does it for you.
 *
1124 1125 1126 1127
 * This function will terminate your program if it was unable to
 * initialize the windowing system for some reason. If you want
 * your program to fall back to a textual interface you want to
 * call gtk_init_check() instead.
1128
 *
1129
 * Since 2.18, GTK+ calls `signal (SIGPIPE, SIG_IGN)`
1130 1131 1132 1133 1134
 * during initialization, to ignore SIGPIPE signals, since these are
 * almost never wanted in graphical applications. If you do need to
 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
 * but notice that other libraries (e.g. libdbus or gvfs) might do
 * similar things.
1135
 */
1136 1137 1138 1139 1140
void
gtk_init (int *argc, char ***argv)
{
  if (!gtk_init_check (argc, argv))
    {
1141
      const char *display_name_arg = gdk_get_display_arg_name ();
1142 1143 1144
      if (display_name_arg == NULL)
        display_name_arg = getenv("DISPLAY");
      g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1145
      exit (1);
1146
    }
Elliot Lee's avatar
Elliot Lee committed
1147 1148
}

Fridrich Strba's avatar
Fridrich Strba committed
1149 1150 1151 1152 1153 1154 1155 1156
#ifdef G_OS_WIN32

/* This is relevant when building with gcc for Windows (MinGW),
 * where we want to be struct packing compatible with MSVC,
 * i.e. use the -mms-bitfields switch.
 * For Cygwin there should be no need to be compatible with MSVC,
 * so no need to use G_PLATFORM_WIN32.
 */
1157 1158 1159 1160 1161 1162

static void
check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
{
  if (sizeof_GtkWindow != sizeof (GtkWindow))
    g_error ("Incompatible build!\n"
1163
             "The code using GTK+ thinks GtkWindow is of different\n"
1164
             "size than it actually is in this build of GTK+.\n"
1165 1166 1167
             "On Windows, this probably means that you have compiled\n"
             "your code with gcc without the -mms-bitfields switch,\n"
             "or that you are using an unsupported compiler.");
1168 1169 1170 1171
}

/* In GTK+ 2.0 the GtkWindow struct actually is the same size in
 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
1172
 * not. Unfortunately this wan’t noticed until after GTK+ 2.0.1. So,
1173 1174 1175 1176 1177 1178 1179 1180
 * from GTK+ 2.0.2 on, check some other struct, too, where the use of
 * -fnative-struct still matters. GtkBox is one such.
 */
static void
check_sizeof_GtkBox (size_t sizeof_GtkBox)
{
  if (sizeof_GtkBox != sizeof (GtkBox))
    g_error ("Incompatible build!\n"
1181
             "The code using GTK+ thinks GtkBox is of different\n"
1182
             "size than it actually is in this build of GTK+.\n"
1183 1184 1185
             "On Windows, this probably means that you have compiled\n"
             "your code with gcc without the -mms-bitfields switch,\n"
             "or that you are using an unsupported compiler.");
1186 1187 1188 1189 1190 1191
}

/* These two functions might get more checks added later, thus pass
 * in the number of extra args.
 */
void
1192
gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1193 1194
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
1195 1196
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
1197 1198 1199 1200
  gtk_init (argc, argv);
}

gboolean
1201
gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1202 1203
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
1204 1205
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
1206 1207 1208 1209 1210
  return gtk_init_check (argc, argv);
}

#endif

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
/**
 * gtk_get_locale_direction:
 *
 * Get the direction of the current locale. This is the expected
 * reading direction for text and UI.
 *
 * This function depends on the current locale being set with
 * setlocale() and will default to setting the %GTK_TEXT_DIR_LTR
 * direction otherwise. %GTK_TEXT_DIR_NONE will never be returned.
 *
 * GTK+ sets the default text direction according to the locale
 * during gtk_init(), and you should normally use
 * gtk_widget_get_direction() or gtk_widget_get_default_direction()
 * to obtain the current direcion.
 *
 * This function is only needed rare cases when the locale is
 * changed after GTK+ has already been initialized. In this case,
 * you can use it to update the default text direction as follows:
 *
1230
 * |[<!-- language="C" -->
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
 * setlocale (LC_ALL, new_locale);
 * direction = gtk_get_locale_direction ();
 * gtk_widget_set_default_direction (direction);
 * ]|
 *
 * Returns: the #GtkTextDirection of the current locale
 *
 * Since: 3.12
 */
GtkTextDirection
gtk_get_locale_direction (void)
{
  /* Translate to default:RTL if you want your widgets
   * to be RTL, otherwise translate to default:LTR.
   * Do *not* translate it to "predefinito:LTR", if it
   * it isn't default:LTR or default:RTL it will not work
   */
1248 1249 1250
  gchar            *e   = _("default:LTR");
  GtkTextDirection  dir = GTK_TEXT_DIR_LTR;

1251 1252 1253
  if (g_strcmp0 (e, "default:RTL") == 0)
    dir = GTK_TEXT_DIR_RTL;
  else if (g_strcmp0 (e, "default:LTR") != 0)
1254
    g_warning ("Whoever translated default:LTR did so wrongly. Defaulting to LTR.");
1255 1256 1257 1258

  return dir;
}

1259 1260 1261 1262 1263
/**
 * gtk_get_default_language:
 *
 * Returns the #PangoLanguage for the default language currently in
 * effect. (Note that this can change over the life of an
1264
 * application.) The default language is derived from the current
1265
 * locale. It determines, for example, whether GTK+ uses the
1266 1267
 * right-to-left or left-to-right text direction.
 *
1268 1269 1270
 * This function is equivalent to pango_language_get_default().
 * See that function for details.
 *
1271
 * Returns: (transfer none): the default language as a #PangoLanguage,
1272 1273
 *     must not be freed
 */
1274 1275 1276
PangoLanguage *
gtk_get_default_language (void)
{
1277
  return pango_language_get_default ();
1278 1279
}

1280 1281 1282 1283 1284 1285 1286 1287
/**
 * gtk_main:
 *
 * Runs the main loop until gtk_main_quit() is called.
 *
 * You can nest calls to gtk_main(). In that case gtk_main_quit()
 * will make the innermost invocation of the main loop return.
 */
Elliot Lee's avatar
Elliot Lee committed
1288
void
1289
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
1290
{
1291 1292
  GMainLoop *loop;

1293
  gtk_main_loop_level++;
1294

Manish Singh's avatar
Manish Singh committed
1295
  loop = g_main_loop_new (NULL, TRUE);
1296 1297
  main_loops = g_slist_prepend (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1298
  if (g_main_loop_is_running (main_loops->data))
1299
    {
1300
      gdk_threads_leave ();
Manish Singh's avatar
Manish Singh committed
1301
      g_main_loop_run (loop);
1302
      gdk_threads_enter ();
1303 1304
      gdk_flush ();
    }
1305

1306 1307
  main_loops = g_slist_remove (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1308
  g_main_loop_unref (loop);
1309

1310
  gtk_main_loop_level--;
1311 1312

  if (gtk_main_loop_level == 0)
1313
    {
1314 1315
      /* Keep this section in sync with gtk_application_shutdown() */

1316 1317 1318 1319 1320 1321
      /* Try storing all clipboard data we have */
      _gtk_clipboard_store_all ();

      /* Synchronize the recent manager singleton */
      _gtk_recent_manager_sync ();
    }
1322 1323
}

1324 1325 1326 1327 1328 1329 1330 1331
/**
 * gtk_main_level:
 *
 * Asks for the current nesting level of the main loop.
 *
 * Returns: the nesting level of the current invocation
 *     of the main loop
 */
1332 1333 1334
guint
gtk_main_level (void)
{
1335
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
1336 1337
}

1338 1339 1340 1341 1342 1343
/**
 * gtk_main_quit:
 *
 * Makes the innermost invocation of the main loop return
 * when it regains control.
 */
Elliot Lee's avatar
Elliot Lee committed
1344
void
1345
gtk_main_quit (void)
Elliot Lee's avatar
Elliot Lee committed
1346
{
1347 1348
  g_return_if_fail (main_loops != NULL);

Manish Singh's avatar
Manish Singh committed
1349
  g_main_loop_quit (main_loops->data);
Elliot Lee's avatar
Elliot Lee committed
1350 1351
}

1352 1353 1354 1355 1356 1357 1358 1359
/**
 * gtk_events_pending:
 *
 * Checks if any events are pending.
 *
 * This can be used to update the UI and invoke timeouts etc.
 * while doing some time intensive computation.
 *
1360 1361
 * ## Updating the UI during a long computation
 *
1362
 * |[<!-- language="C" -->
1363
 *  // computation going on...
1364
 *
1365 1366
 *  while (gtk_events_pending ())
 *    gtk_main_iteration ();
1367
 *