gtkmain.c 67.1 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
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * 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/. 
 */

27
#include "config.h"
28

29 30
#include "gtkmain.h"

Matthias Clasen's avatar
2.5.3  
Matthias Clasen committed
31
#include <glib.h>
32 33
#include "gdkconfig.h"

34
#include <locale.h>
Robert Brady's avatar
Robert Brady committed
35

Elliot Lee's avatar
Elliot Lee committed
36 37
#include <stdio.h>
#include <stdlib.h>
38
#include <string.h>
39
#ifdef HAVE_UNISTD_H
40 41
#include <unistd.h>
#endif
42 43
#include <sys/types.h>		/* For uid_t, gid_t */

Tor Lillqvist's avatar
Tor Lillqvist committed
44 45 46 47 48
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif
49

Tor Lillqvist's avatar
Tor Lillqvist committed
50 51
#include "gtkintl.h"

52
#include "gtkaccelmap.h"
53
#include "gtkbox.h"
54
#include "gtkclipboard.h"
55
#include "gtkdnd.h"
56
#include "gtkversion.h"
57
#include "gtkmodules.h"
Elliot Lee's avatar
Elliot Lee committed
58
#include "gtkrc.h"
59
#include "gtkrecentmanager.h"
Elliot Lee's avatar
Elliot Lee committed
60
#include "gtkselection.h"
61
#include "gtksettings.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
62
#include "gtkwidgetprivate.h"
Elliot Lee's avatar
Elliot Lee committed
63
#include "gtkwindow.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
64
#include "gtktooltip.h"
65
#include "gtkdebug.h"
66 67
#include "gtkmenu.h"
#include "gdk/gdkkeysyms.h"
Elliot Lee's avatar
Elliot Lee committed
68

69 70
#ifdef G_OS_WIN32

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static HMODULE gtk_dll;

BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
	 DWORD     fdwReason,
	 LPVOID    lpvReserved)
{
  switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      gtk_dll = (HMODULE) hinstDLL;
      break;
    }

  return TRUE;
}
87

88 89 90
/* These here before inclusion of gtkprivate.h so that the original
 * GTK_LIBDIR and GTK_LOCALEDIR definitions are seen. Yeah, this is a
 * bit sucky.
91
 */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
const gchar *
_gtk_get_libdir (void)
{
  static char *gtk_libdir = NULL;
  if (gtk_libdir == NULL)
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gchar *slash = strrchr (root, '\\');
      if (g_ascii_strcasecmp (slash + 1, ".libs") == 0)
	gtk_libdir = GTK_LIBDIR;
      else
	gtk_libdir = g_build_filename (root, "lib", NULL);
      g_free (root);
    }

  return gtk_libdir;
}

110 111 112 113 114 115 116
const gchar *
_gtk_get_localedir (void)
{
  static char *gtk_localedir = NULL;
  if (gtk_localedir == NULL)
    {
      const gchar *p;
117
      gchar *root, *temp;
118 119 120 121 122 123 124 125 126 127
      
      /* GTK_LOCALEDIR ends in either /lib/locale or
       * /share/locale. Scan for that slash.
       */
      p = GTK_LOCALEDIR + strlen (GTK_LOCALEDIR);
      while (*--p != '/')
	;
      while (*--p != '/')
	;

128 129 130
      root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      temp = g_build_filename (root, p, NULL);
      g_free (root);
131 132 133 134 135 136 137 138 139 140 141 142 143 144

      /* gtk_localedir is passed to bindtextdomain() which isn't
       * UTF-8-aware.
       */
      gtk_localedir = g_win32_locale_filename_from_utf8 (temp);
      g_free (temp);
    }
  return gtk_localedir;
}

#endif

#include "gtkprivate.h"

Elliot Lee's avatar
Elliot Lee committed
145 146
/* Private type definitions
 */
147 148
typedef struct _GtkQuitFunction		 GtkQuitFunction;
typedef struct _GtkKeySnooperData	 GtkKeySnooperData;
Elliot Lee's avatar
Elliot Lee committed
149

150 151
struct _GtkQuitFunction
{
152
  guint id;
153 154 155 156
  guint main_level;
  GtkCallbackMarshal marshal;
  GtkFunction function;
  gpointer data;
157
  GDestroyNotify destroy;
158 159
};

160 161 162 163
struct _GtkKeySnooperData
{
  GtkKeySnoopFunc func;
  gpointer func_data;
164
  guint id;
165 166
};

167 168 169 170
static gint  gtk_quit_invoke_function	 (GtkQuitFunction    *quitf);
static void  gtk_quit_destroy		 (GtkQuitFunction    *quitf);
static gint  gtk_invoke_key_snoopers	 (GtkWidget	     *grab_widget,
					  GdkEvent	     *event);
171

172 173
static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);

Tim Janik's avatar
Tim Janik committed
174
static guint gtk_main_loop_level = 0;
Matthias Clasen's avatar
Matthias Clasen committed
175
static gint pre_initialized = FALSE;
176
static gint gtk_initialized = FALSE;
177
static GList *current_events = NULL;
Elliot Lee's avatar
Elliot Lee committed
178

179 180
static GSList *main_loops = NULL;      /* stack of currently executing main loops */

181 182
static GList *quit_functions = NULL;	   /* A list of quit functions.
					    */
183 184
static GSList *key_snoopers = NULL;

185
static guint debug_flags = 0;		   /* Global GTK debug flag */
186 187

#ifdef G_ENABLE_DEBUG
188
static const GDebugKey gtk_debug_keys[] = {
189
  {"misc", GTK_DEBUG_MISC},
190
  {"plugsocket", GTK_DEBUG_PLUGSOCKET},
191
  {"text", GTK_DEBUG_TEXT},
192
  {"tree", GTK_DEBUG_TREE},
193
  {"updates", GTK_DEBUG_UPDATES},
194
  {"keybindings", GTK_DEBUG_KEYBINDINGS},
195
  {"multihead", GTK_DEBUG_MULTIHEAD},
196
  {"modules", GTK_DEBUG_MODULES},
197
  {"geometry", GTK_DEBUG_GEOMETRY},
198
  {"icontheme", GTK_DEBUG_ICONTHEME},
Johan Dahlin's avatar
Johan Dahlin committed
199
  {"printing", GTK_DEBUG_PRINTING},
200
  {"builder", GTK_DEBUG_BUILDER},
201
  {"size-request", GTK_DEBUG_SIZE_REQUEST},
202 203
};
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
204

205
/**
206
 * gtk_get_major_version:
207 208 209 210 211 212 213 214 215
 *
 * Returns the major version number of the GTK+ library.  (e.g. in GTK+ version
 * 3.1.5 this is 3.) 
 *
 * 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.
 *
216 217 218
 * Returns: the major version number of the GTK+ library.
 *
 * Since: 3.0
219 220
 */
guint
221
gtk_get_major_version (void)
222 223 224 225 226
{
  return GTK_MAJOR_VERSION;
}

/**
227
 * gtk_get_minor_version:
228 229 230 231 232 233 234 235 236
 *
 * Returns the minor version number of the GTK+ library.  (e.g. in GTK+ version
 * 3.1.5 this is 1.) 
 *
 * 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.
 *
237 238 239
 * Returns: the minor version number of the GTK+ library.
 *
 * Since: 3.0
240 241
 */
guint
242
gtk_get_minor_version (void)
243 244 245 246 247
{
  return GTK_MINOR_VERSION;
}

/**
248
 * gtk_get_micro_version:
249 250 251 252 253 254 255 256 257
 *
 * Returns the micro version number of the GTK+ library.  (e.g. in GTK+ version
 * 3.1.5 this is 5.) 
 *
 * 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.
 *
258 259 260
 * Returns: the micro version number of the GTK+ library.
 *
 * Since: 3.0
261 262
 */
guint
263
gtk_get_micro_version (void)
264 265 266 267 268
{
  return GTK_MICRO_VERSION;
}

/**
269
 * gtk_get_binary_age:
270 271 272 273 274 275 276
 *
 * Returns the binary age as passed to
 * <application>libtool</application> when building the GTK+ library
 * the process is running against. If
 * <application>libtool</application> means nothing to you, don't
 * worry about it.
 *
277 278 279
 * Returns: the binary age of the GTK+ library.
 *
 * Since: 3.0
280 281
 */
guint
282
gtk_get_binary_age (void)
283 284 285 286 287
{
  return GTK_BINARY_AGE;
}

/**
288
 * gtk_get_interface_age:
289 290 291 292 293 294 295
 *
 * Returns the interface age as passed to
 * <application>libtool</application> when building the GTK+ library
 * the process is running against. If
 * <application>libtool</application> means nothing to you, don't
 * worry about it.
 *
296 297 298
 * Returns: the interface age of the GTK+ library.
 *
 * Since: 3.0
299 300
 */
guint
301
gtk_get_interface_age (void)
302 303 304 305
{
  return GTK_INTERFACE_AGE;
}

306 307 308
/**
 * gtk_check_version:
 * @required_major: the required major version.
309 310
 * @required_minor: the required minor version.
 * @required_micro: the required micro version.
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
 * 
 * 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
 * can call this function to check that it wasn't loaded
 * into an incompatible version of GTK+. However, such a
330
 * check isn't completely reliable, since the module may be
331 332 333 334 335 336 337 338 339
 * 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+.
 *
 * Return value: %NULL if the GTK+ library is compatible with the
 *   given version, or a string describing the version mismatch.
 *   The returned string is owned by GTK+ and should not be modified
 *   or freed.
 **/
340
const gchar*
341 342 343 344
gtk_check_version (guint required_major,
		   guint required_minor,
		   guint required_micro)
{
345 346 347
  gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
  gint required_effective_micro = 100 * required_minor + required_micro;

348
  if (required_major > GTK_MAJOR_VERSION)
349
    return "Gtk+ version too old (major mismatch)";
350
  if (required_major < GTK_MAJOR_VERSION)
351
    return "Gtk+ version too new (major mismatch)";
352
  if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
353
    return "Gtk+ version too new (micro mismatch)";
354
  if (required_effective_micro > gtk_effective_micro)
355
    return "Gtk+ version too old (micro mismatch)";
356 357 358
  return NULL;
}

Owen Taylor's avatar
Owen Taylor committed
359 360 361 362 363 364 365 366 367 368
/* This checks to see if the process is running suid or sgid
 * at the current time. If so, we don't allow GTK+ to be initialized.
 * 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
 * don't use issetugid() on BSD or prctl (PR_GET_DUMPABLE).
 */
static gboolean
check_setugid (void)
{
369
/* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
Hans Breuer's avatar
Hans Breuer committed
370
#ifndef G_OS_WIN32
Owen Taylor's avatar
Owen Taylor committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  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"
		 "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+.");
      exit (1);
    }
Hans Breuer's avatar
Hans Breuer committed
400
#endif
Owen Taylor's avatar
Owen Taylor committed
401 402 403
  return TRUE;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
404 405
#ifdef G_OS_WIN32

406 407 408 409 410
const gchar *
_gtk_get_datadir (void)
{
  static char *gtk_datadir = NULL;
  if (gtk_datadir == NULL)
411 412 413 414 415
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gtk_datadir = g_build_filename (root, "share", NULL);
      g_free (root);
    }
416 417 418 419

  return gtk_datadir;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
420 421 422 423 424
const gchar *
_gtk_get_sysconfdir (void)
{
  static char *gtk_sysconfdir = NULL;
  if (gtk_sysconfdir == NULL)
425 426 427 428 429
    {
      gchar *root = g_win32_get_package_installation_directory_of_module (gtk_dll);
      gtk_sysconfdir = g_build_filename (root, "etc", NULL);
      g_free (root);
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
430 431 432 433 434 435 436 437 438

  return gtk_sysconfdir;
}

const gchar *
_gtk_get_data_prefix (void)
{
  static char *gtk_data_prefix = NULL;
  if (gtk_data_prefix == NULL)
439
    gtk_data_prefix = g_win32_get_package_installation_directory_of_module (gtk_dll);
Tor Lillqvist's avatar
Tor Lillqvist committed
440 441 442 443 444 445

  return gtk_data_prefix;
}

#endif /* G_OS_WIN32 */

446 447 448 449 450
static gboolean do_setlocale = TRUE;

/**
 * gtk_disable_setlocale:
 * 
Matthias Clasen's avatar
Matthias Clasen committed
451 452
 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
 * gtk_parse_args() from automatically
453 454 455 456
 * calling <literal>setlocale (LC_ALL, "")</literal>. You would 
 * want to use this function if you wanted to set the locale for 
 * your program to something other than the user's locale, or if 
 * you wanted to set different values for different locale categories.
457 458 459
 *
 * Most programs should not need to call this function.
 **/
460
void
461 462
gtk_disable_setlocale (void)
{
Matthias Clasen's avatar
Matthias Clasen committed
463
  if (pre_initialized)
464 465 466 467 468
    g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
    
  do_setlocale = FALSE;
}

469
#ifdef G_PLATFORM_WIN32
470
#undef gtk_init_check
471
#endif
472

473
static GString *gtk_modules_string = NULL;
474
static gboolean g_fatal_warnings = FALSE;
475 476 477 478

#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
479
{
480 481 482
  debug_flags |= g_parse_debug_string (value,
				       gtk_debug_keys,
				       G_N_ELEMENTS (gtk_debug_keys));
483

484 485
  return TRUE;
}
Tim Janik's avatar
Tim Janik committed
486

487 488 489
static gboolean
gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
{
490 491 492
  debug_flags &= ~g_parse_debug_string (value,
					gtk_debug_keys,
					G_N_ELEMENTS (gtk_debug_keys));
493 494

  return TRUE;
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
}
#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)
	g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
      else
	gtk_modules_string = g_string_new (NULL);
      
      g_string_append (gtk_modules_string, value);
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
514
static const GOptionEntry gtk_args[] = {
515 516 517
  { "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") },
518
  { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, 
519
    /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
520
#ifdef G_ENABLE_DEBUG
521 522 523 524 525 526 527
  { "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 
528 529 530
  { NULL }
};

Tor Lillqvist's avatar
Tor Lillqvist committed
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 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 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
#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 &&
	  ((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;
	}
    }

  return TRUE;
}
  
#endif

620
static void
621
setlocale_initialization (void)
622
{
623
  static gboolean initialized = FALSE;
624

625
  if (initialized)
Matthias Clasen's avatar
Matthias Clasen committed
626
    return;
627
  initialized = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
628

629
  if (do_setlocale)
630
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
#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)
	p = getenv ("LANG");

      if (p != NULL)
	{
	  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);
	}
      if (!setlocale_called)
	setlocale (LC_ALL, "");
#else
688 689
      if (!setlocale (LC_ALL, ""))
	g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
Tor Lillqvist's avatar
Tor Lillqvist committed
690
#endif
691
    }
692 693
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
static void
check_mixed_deps (void)
{
  GModule *module;
  gpointer func;

  module = g_module_open (NULL, 0);

  if (g_module_symbol (module, "gtk_progress_get_type", &func))
    {
      g_error ("GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported");
    }

  g_module_close (module);
}

710 711 712 713 714 715 716 717 718 719
static void
do_pre_parse_initialization (int    *argc,
			     char ***argv)
{
  const gchar *env_string;
  
  if (pre_initialized)
    return;

  pre_initialized = TRUE;
720

721 722
  check_mixed_deps ();

Matthias Clasen's avatar
Matthias Clasen committed
723
  gdk_pre_parse_libgtk_only ();
Owen Taylor's avatar
Owen Taylor committed
724
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
725
  
726
#ifdef G_ENABLE_DEBUG
Matthias Clasen's avatar
Matthias Clasen committed
727
  env_string = g_getenv ("GTK_DEBUG");
728 729
  if (env_string != NULL)
    {
730 731 732
      debug_flags = g_parse_debug_string (env_string,
					  gtk_debug_keys,
					  G_N_ELEMENTS (gtk_debug_keys));
733 734
      env_string = NULL;
    }
735
#endif	/* G_ENABLE_DEBUG */
736

Matthias Clasen's avatar
Matthias Clasen committed
737
  env_string = g_getenv ("GTK_MODULES");
738
  if (env_string)
739
    gtk_modules_string = g_string_new (env_string);
740
}
741

742 743 744
static void
gettext_initialization (void)
{
745 746
  setlocale_initialization ();

747 748 749 750 751 752 753 754 755 756
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
  bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
#    endif
#endif  
}

757 758 759 760
static void
do_post_parse_initialization (int    *argc,
			      char ***argv)
{
761 762 763
  if (gtk_initialized)
    return;

764 765
  gettext_initialization ();

766
#ifdef SIGPIPE
767
  signal (SIGPIPE, SIG_IGN);
768
#endif
769

770 771 772 773 774 775 776 777 778
  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);
    }

779
  if (debug_flags & GTK_DEBUG_UPDATES)
780
    gdk_window_set_debug_updates (TRUE);
781

782 783
  {
  /* Translate to default:RTL if you want your widgets
Robert Brady's avatar
Robert Brady committed
784 785 786
   * 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 
787 788
   */
    char *e = _("default:LTR");
Matthias Clasen's avatar
Matthias Clasen committed
789
    if (strcmp (e, "default:RTL")==0) 
790
      gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
Matthias Clasen's avatar
Matthias Clasen committed
791
    else if (strcmp (e, "default:LTR"))
792 793 794
      g_warning ("Whoever translated default:LTR did so wrongly.\n");
  }

795 796 797 798
  /* do what the call to gtk_type_init() used to do */
  g_type_init ();

  _gtk_accel_map_init ();
799
  _gtk_rc_init ();
800

Elliot Lee's avatar
Elliot Lee committed
801 802
  /* Set the 'initialized' flag.
   */
803
  gtk_initialized = TRUE;
804

805 806
  /* load gtk modules */
  if (gtk_modules_string)
807
    {
808 809
      _gtk_modules_init (argc, argv, gtk_modules_string->str);
      g_string_free (gtk_modules_string, TRUE);
810
    }
811 812 813 814
  else
    {
      _gtk_modules_init (argc, argv, NULL);
    }
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
}


typedef struct
{
  gboolean open_default_display;
} OptionGroupInfo;

static gboolean
pre_parse_hook (GOptionContext *context,
		GOptionGroup   *group,
		gpointer	data,
		GError        **error)
{
  do_pre_parse_initialization (NULL, NULL);
  
  return TRUE;
}

static gboolean
post_parse_hook (GOptionContext *context,
		 GOptionGroup   *group,
		 gpointer	data,
		 GError        **error)
{
  OptionGroupInfo *info = data;

  
  do_post_parse_initialization (NULL, NULL);
  
  if (info->open_default_display)
846 847 848
    {
      if (gdk_display_open_default_libgtk_only () == NULL)
	{
849
	  const char *display_name = gdk_get_display_arg_name ();
850 851 852
	  g_set_error (error, 
		       G_OPTION_ERROR, 
		       G_OPTION_ERROR_FAILED,
853
		       _("Cannot open display: %s"),
854
		       display_name ? display_name : "" );
855 856 857 858 859 860
	  
	  return FALSE;
	}
    }

  return TRUE;
861 862 863
}


864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
/**
 * gtk_get_debug_flags:
 *
 * Returns the GTK+ debug flags setting.
 */
guint
gtk_get_debug_flags (void)
{
  return debug_flags;
}

/**
 * gtk_set_debug_flags:
 *
 * Sets the GTK+ debug flags.
 */
void
gtk_set_debug_flags (guint flags)
{
  debug_flags = flags;
}

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

907 908
  gettext_initialization ();

909 910 911 912 913 914 915 916
  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);

  gdk_add_option_entries_libgtk_only (group);
  g_option_group_add_entries (group, gtk_args);
917
  g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
918 919 920 921
  
  return group;
}

Matthias Clasen's avatar
Matthias Clasen committed
922 923 924 925 926
/**
 * gtk_init_with_args:
 * @argc: a pointer to the number of command line arguments.
 * @argv: a pointer to the array of command line arguments.
 * @parameter_string: a string which is displayed in
927
 *    the first line of <option>--help</option> output, after
Matthias Clasen's avatar
Matthias Clasen committed
928 929 930 931 932
 *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
 *    describing the options of your program
 * @translation_domain: a translation domain to use for translating
 *    the <option>--help</option> output for the options in @entries
933 934
 *    and the @parameter_string with gettext(), or %NULL
 * @error: a return location for errors
Matthias Clasen's avatar
Matthias Clasen committed
935
 *
936 937 938
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
939 940 941
 * <option>--help</option> output. Note that your program will
 * be terminated after writing out the help output.
 *
942
 * Returns: %TRUE if the GUI has been successfully initialized,
Matthias Clasen's avatar
Matthias Clasen committed
943
 *               %FALSE otherwise.
944
 *
Matthias Clasen's avatar
Matthias Clasen committed
945 946
 * Since: 2.6
 */
947
gboolean
948 949 950 951 952 953
gtk_init_with_args (gint                 *argc,
                    gchar              ***argv,
                    const gchar          *parameter_string,
                    const GOptionEntry   *entries,
                    const gchar          *translation_domain,
                    GError              **error)
954 955 956 957
{
  GOptionContext *context;
  GOptionGroup *gtk_group;
  gboolean retval;
Matthias Clasen's avatar
Matthias Clasen committed
958

959
  if (gtk_initialized)
960
    return gdk_display_open_default_libgtk_only () != NULL;
961

962 963
  gettext_initialization ();

Matthias Clasen's avatar
Matthias Clasen committed
964 965 966
  if (!check_setugid ())
    return FALSE;

967
  gtk_group = gtk_get_option_group (TRUE);
968

969 970
  context = g_option_context_new (parameter_string);
  g_option_context_add_group (context, gtk_group);
971 972
  g_option_context_set_translation_domain (context, translation_domain);

973 974 975
  if (entries)
    g_option_context_add_main_entries (context, entries, translation_domain);
  retval = g_option_context_parse (context, argc, argv, error);
976

977 978 979 980 981 982 983 984
  g_option_context_free (context);

  return retval;
}


/**
 * gtk_parse_args:
985 986 987
 * @argc: (inout): a pointer to the number of command line arguments.
 * @argv: (array) (inout): a pointer to the array of command line arguments.
 *
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
 * 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.
 *
 * You shouldn't call this function explicitely if you are using
 * gtk_init(), or gtk_init_check().
 *
 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
 **/
gboolean
gtk_parse_args (int    *argc,
		char ***argv)
{
  GOptionContext *option_context;
1005
  GOptionGroup *gtk_group;
1006
  GError *error = NULL;
1007 1008 1009 1010
  
  if (gtk_initialized)
    return TRUE;

1011 1012
  gettext_initialization ();

1013 1014 1015 1016 1017 1018
  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);
1019 1020
  gtk_group = gtk_get_option_group (FALSE);
  g_option_context_set_main_group (option_context, gtk_group);
1021 1022 1023 1024 1025 1026
  if (!g_option_context_parse (option_context, argc, argv, &error))
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }

1027 1028
  g_option_context_free (option_context);

1029 1030
  return TRUE;
}
1031

1032
#ifdef G_PLATFORM_WIN32
1033
#undef gtk_init_check
1034
#endif
1035 1036 1037

/**
 * gtk_init_check:
1038
 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of your
Matthias Clasen's avatar
Matthias Clasen committed
1039
 *   main() function. Changed if any arguments were handled.
1040
 * @argv: (array length=argc) (inout) (allow-none): Address of the <parameter>argv</parameter> parameter of main().
Matthias Clasen's avatar
Matthias Clasen committed
1041
 *   Any parameters understood by gtk_init() are stripped before return.
1042 1043 1044
 *
 * This function does the same work as gtk_init() with only
 * a single change: It does not terminate the program if the GUI can't be
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
 * initialized. Instead it returns %FALSE on failure.
 *
 * This way the application can fall back to some other means of communication 
 * with the user - for example a curses or command line interface.
 * 
 * Return value: %TRUE if the GUI has been successfully initialized, 
 *               %FALSE otherwise.
 **/
gboolean
gtk_init_check (int	 *argc,
		char   ***argv)
{
  if (!gtk_parse_args (argc, argv))
    return FALSE;

1060
  return gdk_display_open_default_libgtk_only () != NULL;
1061 1062
}

1063
#ifdef G_PLATFORM_WIN32
1064
#undef gtk_init
1065
#endif
1066

1067
/**
Matthias Clasen's avatar
Matthias Clasen committed
1068
 * gtk_init:
1069
 * @argc: (inout): Address of the <parameter>argc</parameter> parameter of your
Matthias Clasen's avatar
Matthias Clasen committed
1070
 *   main() function. Changed if any arguments were handled.
1071
 * @argv: (array length=argc) (inout) (allow-none): Address of the <parameter>argv</parameter> parameter of main().
Matthias Clasen's avatar
Matthias Clasen committed
1072
 *   Any parameters understood by gtk_init() are stripped before return.
1073
 *
1074
 * Call this function before using any other GTK+ functions in your GUI
1075
 * applications.  It will initialize everything needed to operate the
Matthias Clasen's avatar
Matthias Clasen committed
1076
 * toolkit and parses some standard command line options. @argc and 
Matthias Clasen's avatar
Matthias Clasen committed
1077
 * @argv are adjusted accordingly so your own code will 
Matthias Clasen's avatar
Matthias Clasen committed
1078 1079 1080 1081 1082 1083 1084
 * 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(), you 
 * <emphasis>don't</emphasis> have to call gtk_init().
1085 1086 1087 1088 1089 1090
 *
 * <note><para>
 * This function will terminate your program if it was unable to initialize 
 * the GUI for some reason. If you want your program to fall back to a 
 * textual interface you want to call gtk_init_check() instead.
 * </para></note>
1091 1092 1093 1094 1095 1096 1097 1098 1099
 *
 * <note><para>
 * Since 2.18, GTK+ calls <literal>signal (SIGPIPE, SIG_IGN)</literal>
 * 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.
 * </para></note>
1100
 **/
1101 1102 1103 1104 1105
void
gtk_init (int *argc, char ***argv)
{
  if (!gtk_init_check (argc, argv))
    {
1106
      const char *display_name_arg = gdk_get_display_arg_name ();
1107 1108 1109
      if (display_name_arg == NULL)
        display_name_arg = getenv("DISPLAY");
      g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : "");
1110
      exit (1);
1111
    }
Elliot Lee's avatar
Elliot Lee committed
1112 1113
}

1114
#ifdef G_PLATFORM_WIN32
1115 1116 1117 1118 1119 1120 1121 1122 1123

static void
check_sizeof_GtkWindow (size_t sizeof_GtkWindow)
{
  if (sizeof_GtkWindow != sizeof (GtkWindow))
    g_error ("Incompatible build!\n"
	     "The code using GTK+ thinks GtkWindow is of different\n"
             "size than it actually is in this build of GTK+.\n"
	     "On Windows, this probably means that you have compiled\n"
1124 1125
	     "your code with gcc without the -mms-bitfields switch,\n"
	     "or that you are using an unsupported compiler.");
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
}

/* In GTK+ 2.0 the GtkWindow struct actually is the same size in
 * gcc-compiled code on Win32 whether compiled with -fnative-struct or
 * not. Unfortunately this wan't noticed until after GTK+ 2.0.1. So,
 * 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"
	     "The code using GTK+ thinks GtkBox is of different\n"
             "size than it actually is in this build of GTK+.\n"
	     "On Windows, this probably means that you have compiled\n"
1142 1143
	     "your code with gcc without the -mms-bitfields switch,\n"
	     "or that you are using an unsupported compiler.");
1144 1145 1146 1147 1148 1149
}

/* These two functions might get more checks added later, thus pass
 * in the number of extra args.
 */
void
1150
gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1151 1152
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
1153 1154
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
1155 1156 1157 1158
  gtk_init (argc, argv);
}

gboolean
1159
gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1160 1161
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
1162 1163
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
1164 1165 1166 1167 1168
  return gtk_init_check (argc, argv);
}

#endif

1169 1170 1171
/**
 * gtk_set_locale:
 *
1172 1173 1174
 * Initializes internationalization support for GTK+. gtk_init()
 * automatically does this, so there is typically no point
 * in calling this function.
1175
 *
1176 1177 1178 1179 1180
 * If you are calling this function because you changed the locale
 * after GTK+ is was initialized, then calling this function
 * may help a bit. (Note, however, that changing the locale
 * after GTK+ is initialized may produce inconsistent results and
 * is not really supported.)
1181
 * 
1182
 * In detail - sets the current locale according to the
1183 1184 1185
 * program environment. This is the same as calling the C library function
 * <literal>setlocale (LC_ALL, "")</literal> but also takes care of the 
 * locale specific setup of the windowing system used by GDK.
1186
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1187
 * Returns: a string corresponding to the locale set, typically in the
1188 1189
 * form lang_COUNTRY, where lang is an ISO-639 language code, and
 * COUNTRY is an ISO-3166 country code. On Unix, this form matches the
Matthias Clasen's avatar
Matthias Clasen committed
1190 1191 1192
 * result of the setlocale(); it is also used on other machines, such as 
 * Windows, where the C library returns a different result. The string is 
 * owned by GTK+ and should not be modified or freed.
1193
 **/
Sven Neumann's avatar
Sven Neumann committed
1194
gchar *
1195
gtk_set_locale (void)
Elliot Lee's avatar
Elliot Lee committed
1196 1197 1198 1199
{
  return gdk_set_locale ();
}

1200
/**
1201
 * _gtk_get_lc_ctype:
1202
 *
1203 1204 1205 1206 1207 1208 1209 1210 1211
 * Return the Unix-style locale string for the language currently in
 * effect. On Unix systems, this is the return value from
 * <literal>setlocale(LC_CTYPE, NULL)</literal>, and the user can
 * affect this through the environment variables LC_ALL, LC_CTYPE or
 * LANG (checked in that order). The locale strings typically is in
 * the form lang_COUNTRY, where lang is an ISO-639 language code, and
 * COUNTRY is an ISO-3166 country code. For instance, sv_FI for
 * Swedish as written in Finland or pt_BR for Portuguese as written in
 * Brazil.
1212
 * 
1213 1214
 * On Windows, the C library doesn't use any such environment
 * variables, and setting them won't affect the behaviour of functions
Matthias Clasen's avatar
Matthias Clasen committed
1215 1216 1217 1218 1219
 * like ctime(). The user sets the locale through the Regional Options 
 * in the Control Panel. The C library (in the setlocale() function) 
 * does not use country and language codes, but country and language 
 * names spelled out in English. 
 * However, this function does check the above environment
1220 1221 1222 1223 1224 1225 1226 1227
 * variables, and does return a Unix-style locale string based on
 * either said environment variables or the thread's current locale.
 *
 * Return value: a dynamically allocated string, free with g_free().
 */

gchar *
_gtk_get_lc_ctype (void)
1228
{
1229 1230 1231 1232 1233
#ifdef G_OS_WIN32
  /* Somebody might try to set the locale for this process using the
   * LANG or LC_ environment variables. The Microsoft C library
   * doesn't know anything about them. You set the locale in the
   * Control Panel. Setting these env vars won't have any affect on
1234 1235
   * locale-dependent C library functions like ctime(). But just for
   * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1236 1237 1238 1239
   * it easier to test GTK and Pango in various default languages, you
   * don't have to clickety-click in the Control Panel, you can simply
   * start the program with LC_ALL=something on the command line.)
   */
Owen Taylor's avatar
Owen Taylor committed
1240 1241
  gchar *p;

1242 1243
  p = getenv ("LC_ALL");
  if (p != NULL)
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
    return g_strdup (p);

  p = getenv ("LC_CTYPE");
  if (p != NULL)
    return g_strdup (p);

  p = getenv ("LANG");
  if (p != NULL)
    return g_strdup (p);

  return g_win32_getlocale ();
1255
#else
1256
  return g_strdup (setlocale (LC_CTYPE, NULL));
1257
#endif
1258 1259 1260 1261 1262 1263 1264 1265 1266
}

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

Elliot Lee's avatar
Elliot Lee committed
1281
void
1282
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
1283 1284
{
  GList *tmp_list;
1285 1286
  GMainLoop *loop;

Tim Janik's avatar
Tim Janik committed
1287
  gtk_main_loop_level++;
1288
  
Manish Singh's avatar
Manish Singh committed
1289
  loop = g_main_loop_new (NULL, TRUE);
1290 1291
  main_loops = g_slist_prepend (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1292
  if (g_main_loop_is_running (main_loops->data))
1293 1294
    {
      GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1295
      g_main_loop_run (loop);
1296 1297 1298
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308

  if (quit_functions)
    {
      GList *reinvoke_list = NULL;
      GtkQuitFunction *quitf;

      while (quit_functions)
	{
	  quitf = quit_functions->data;

1309
	  tmp_list = quit_functions;
1310
	  quit_functions = g_list_remove_link (quit_functions, quit_functions);
1311
	  g_list_free_1 (tmp_list);
1312

Tim Janik's avatar
Tim Janik committed
1313
	  if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1314
	      gtk_quit_invoke_function (quitf))
1315
	    {
1316
	      reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1317 1318 1319
	    }
	  else
	    {
1320
	      gtk_quit_destroy (quitf);
1321 1322 1323 1324
	    }
	}
      if (reinvoke_list)
	{
Tim Janik's avatar
Tim Janik committed
1325
	  GList *work;
1326
	  
Tim Janik's avatar
Tim Janik committed
1327
	  work = g_list_last (reinvoke_list);
1328
	  if (quit_functions)
Tim Janik's avatar
Tim Janik committed
1329 1330 1331
	    quit_functions->prev = work;
	  work->next = quit_functions;
	  quit_functions = work;
1332
	}
1333 1334

      gdk_flush ();
1335
    }
1336
    
1337 1338
  main_loops = g_slist_remove (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1339
  g_main_loop_unref (loop);
1340

Tim Janik's avatar
Tim Janik committed
1341
  gtk_main_loop_level--;
1342 1343

  if (gtk_main_loop_level == 0)
1344 1345 1346 1347 1348 1349 1350
    {
      /* Try storing all clipboard data we have */
      _gtk_clipboard_store_all ();

      /* Synchronize the recent manager singleton */
      _gtk_recent_manager_sync ();
    }
1351 1352 1353 1354 1355
}

guint
gtk_main_level (void)
{
Tim Janik's avatar
Tim Janik committed
1356
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
1357 1358 1359
}

void