gtkmain.c 62.6 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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 28
#include <config.h>

29 30
#include "gdkconfig.h"

31
#include <locale.h>
Robert Brady's avatar
Robert Brady committed
32 33 34 35 36

#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
#include <libintl.h>
#endif

Elliot Lee's avatar
Elliot Lee committed
37 38
#include <stdio.h>
#include <stdlib.h>
39
#include <string.h>
40
#include <gmodule.h>
41 42
#ifdef G_OS_UNIX
#include <unistd.h>
43
#include <sys/types.h>		/* For uid_t, gid_t */
44
#endif
Tor Lillqvist's avatar
Tor Lillqvist committed
45 46 47 48 49
#ifdef G_OS_WIN32
#define STRICT
#include <windows.h>
#undef STRICT
#endif
50 51 52

#include <pango/pango-utils.h>	/* For pango_split_file_list */

Tor Lillqvist's avatar
Tor Lillqvist committed
53 54
#include "gtkintl.h"

55
#include "gtkaccelmap.h"
56
#include "gtkbox.h"
57
#include "gtkdnd.h"
58
#include "gtkversion.h"
Elliot Lee's avatar
Elliot Lee committed
59 60 61
#include "gtkmain.h"
#include "gtkrc.h"
#include "gtkselection.h"
62
#include "gtksettings.h"
Elliot Lee's avatar
Elliot Lee committed
63 64
#include "gtkwidget.h"
#include "gtkwindow.h"
65
#include "gtkprivate.h"
66
#include "gtkdebug.h"
Elliot Lee's avatar
Elliot Lee committed
67 68 69

/* Private type definitions
 */
70 71
typedef struct _GtkInitFunction		 GtkInitFunction;
typedef struct _GtkQuitFunction		 GtkQuitFunction;
72
typedef struct _GtkClosure	         GtkClosure;
73
typedef struct _GtkKeySnooperData	 GtkKeySnooperData;
74
typedef struct _GtkModuleInfo            GtkModuleInfo;
Elliot Lee's avatar
Elliot Lee committed
75 76 77 78 79 80 81

struct _GtkInitFunction
{
  GtkFunction function;
  gpointer data;
};

82 83
struct _GtkQuitFunction
{
84
  guint id;
85 86 87 88 89 90 91
  guint main_level;
  GtkCallbackMarshal marshal;
  GtkFunction function;
  gpointer data;
  GtkDestroyNotify destroy;
};

92
struct _GtkClosure
Elliot Lee's avatar
Elliot Lee committed
93
{
94
  GtkCallbackMarshal marshal;
Elliot Lee's avatar
Elliot Lee committed
95 96 97 98
  gpointer data;
  GtkDestroyNotify destroy;
};

99 100 101 102
struct _GtkKeySnooperData
{
  GtkKeySnoopFunc func;
  gpointer func_data;
103
  guint id;
104 105
};

106 107 108 109 110 111
struct _GtkModuleInfo
{
  GtkModuleInitFunc init_func;
  GtkModuleDisplayInitFunc display_init_func;
};

112 113 114 115
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);
116 117 118 119 120 121 122

static void     gtk_destroy_closure      (gpointer            data);
static gboolean gtk_invoke_idle_timeout  (gpointer            data);
static void     gtk_invoke_input         (gpointer            data,
					  gint                source,
					  GdkInputCondition   condition);

Manish Singh's avatar
Manish Singh committed
123
#if 0
124 125 126 127
static void  gtk_error			 (gchar		     *str);
static void  gtk_warning		 (gchar		     *str);
static void  gtk_message		 (gchar		     *str);
static void  gtk_print			 (gchar		     *str);
Manish Singh's avatar
Manish Singh committed
128
#endif
129

130 131
static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);

132 133 134
const guint gtk_major_version = GTK_MAJOR_VERSION;
const guint gtk_minor_version = GTK_MINOR_VERSION;
const guint gtk_micro_version = GTK_MICRO_VERSION;
135 136
const guint gtk_binary_age = GTK_BINARY_AGE;
const guint gtk_interface_age = GTK_INTERFACE_AGE;
137

138 139 140 141 142 143 144
static GSList *gtk_modules;

/* Saved argc,argv for delayed module initialization
 */
static gint    gtk_argc = 0;
static gchar **gtk_argv = NULL;

Tim Janik's avatar
Tim Janik committed
145
static guint gtk_main_loop_level = 0;
146
static gint gtk_initialized = FALSE;
147
static GList *current_events = NULL;
Elliot Lee's avatar
Elliot Lee committed
148

149 150
static GSList *main_loops = NULL;      /* stack of currently executing main loops */

151
static GList *init_functions = NULL;	   /* A list of init functions.
Elliot Lee's avatar
Elliot Lee committed
152
					    */
153 154 155
static GList *quit_functions = NULL;	   /* A list of quit functions.
					    */
static GMemChunk *quit_mem_chunk = NULL;
Elliot Lee's avatar
Elliot Lee committed
156

157 158
static GSList *key_snoopers = NULL;

159
guint gtk_debug_flags = 0;		   /* Global GTK debug flag */
160 161

#ifdef G_ENABLE_DEBUG
162
static const GDebugKey gtk_debug_keys[] = {
163
  {"misc", GTK_DEBUG_MISC},
164
  {"plugsocket", GTK_DEBUG_PLUGSOCKET},
165
  {"text", GTK_DEBUG_TEXT},
166
  {"tree", GTK_DEBUG_TREE},
167
  {"updates", GTK_DEBUG_UPDATES},
168 169
  {"keybindings", GTK_DEBUG_KEYBINDINGS},
  {"multihead", GTK_DEBUG_MULTIHEAD}
170
};
171

172
static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
173

174
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
/**
 * gtk_check_version:
 * @required_major: the required major version.
 * @required_minor: the required major version.
 * @required_micro: the required major version.
 * 
 * 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
 * a check isn't completely reliable, since the module may be
 * 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.
 **/
210 211 212 213 214
gchar*
gtk_check_version (guint required_major,
		   guint required_minor,
		   guint required_micro)
{
215 216 217
  gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
  gint required_effective_micro = 100 * required_minor + required_micro;

218
  if (required_major > GTK_MAJOR_VERSION)
219
    return "Gtk+ version too old (major mismatch)";
220
  if (required_major < GTK_MAJOR_VERSION)
221
    return "Gtk+ version too new (major mismatch)";
222
  if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
223
    return "Gtk+ version too new (micro mismatch)";
224
  if (required_effective_micro > gtk_effective_micro)
225
    return "Gtk+ version too old (micro mismatch)";
226 227 228
  return NULL;
}

Owen Taylor's avatar
Owen Taylor committed
229 230 231 232 233 234 235 236 237 238
/* 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)
{
239
/* this isn't at all relevant on MS Windows and doesn't compile ... --hb */
Hans Breuer's avatar
Hans Breuer committed
240
#ifndef G_OS_WIN32
Owen Taylor's avatar
Owen Taylor committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  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
270
#endif
Owen Taylor's avatar
Owen Taylor committed
271 272 273
  return TRUE;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
274 275 276 277
#ifdef G_OS_WIN32

G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)

278 279 280 281 282 283 284 285 286 287 288
const gchar *
_gtk_get_datadir (void)
{
  static char *gtk_datadir = NULL;
  if (gtk_datadir == NULL)
    gtk_datadir = g_win32_get_package_installation_subdirectory
      (GETTEXT_PACKAGE, dll_name, "share");

  return gtk_datadir;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
const gchar *
_gtk_get_libdir (void)
{
  static char *gtk_libdir = NULL;
  if (gtk_libdir == NULL)
    gtk_libdir = g_win32_get_package_installation_subdirectory
      (GETTEXT_PACKAGE, dll_name, "lib");

  return gtk_libdir;
}

const gchar *
_gtk_get_localedir (void)
{
  static char *gtk_localedir = NULL;
  if (gtk_localedir == NULL)
    gtk_localedir = g_win32_get_package_installation_subdirectory
      (GETTEXT_PACKAGE, dll_name, "lib\\locale");

  return gtk_localedir;
}

const gchar *
_gtk_get_sysconfdir (void)
{
  static char *gtk_sysconfdir = NULL;
  if (gtk_sysconfdir == NULL)
    gtk_sysconfdir = g_win32_get_package_installation_subdirectory
      (GETTEXT_PACKAGE, dll_name, "etc");

  return gtk_sysconfdir;
}

const gchar *
_gtk_get_data_prefix (void)
{
  static char *gtk_data_prefix = NULL;
  if (gtk_data_prefix == NULL)
    gtk_data_prefix = g_win32_get_package_installation_directory
      (GETTEXT_PACKAGE, dll_name);

  return gtk_data_prefix;
}

#endif /* G_OS_WIN32 */

335 336 337
static gchar **
get_module_path (void)
{
338 339 340 341
  const gchar *module_path_env;
  const gchar *exe_prefix;
  const gchar *home_dir;
  gchar *home_gtk_dir = NULL;
342
  gchar *module_path;
343
  gchar *default_dir;
344 345 346 347 348 349 350 351 352 353 354
  static gchar **result = NULL;

  if (result)
    return result;

  home_dir = g_get_home_dir();
  if (home_dir)
    home_gtk_dir = g_build_filename (home_dir, ".gtk-2.0", NULL);

  module_path_env = g_getenv ("GTK_PATH");
  exe_prefix = g_getenv ("GTK_EXE_PREFIX");
355 356

  if (exe_prefix)
357 358 359 360 361 362 363 364 365 366 367 368 369
    default_dir = g_build_filename (exe_prefix, "lib", "gtk-2.0", NULL);
  else
    default_dir = g_build_filename (GTK_LIBDIR, "gtk-2.0", NULL);

  if (module_path_env && home_gtk_dir)
    module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
				module_path_env, home_gtk_dir, default_dir, NULL);
  else if (module_path_env)
    module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
				module_path_env, default_dir, NULL);
  else if (home_gtk_dir)
    module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
				home_gtk_dir, default_dir, NULL);
370
  else
371 372
    module_path = g_build_path (G_SEARCHPATH_SEPARATOR_S,
				default_dir, NULL);
Tor Lillqvist's avatar
Tor Lillqvist committed
373

374 375
  g_free (home_gtk_dir);
  g_free (default_dir);
376 377 378 379 380 381 382

  result = pango_split_file_list (module_path);
  g_free (module_path);

  return result;
}

383 384 385 386 387 388 389 390 391 392
/**
 * _gtk_get_module_path:
 * @type: the type of the module, for instance 'modules', 'engines', immodules'
 * 
 * Determines the search path for a particular type of module.
 * 
 * Return value: the search path for the module type. Free with g_strfreev().
 **/
gchar **
_gtk_get_module_path (const gchar *type)
393
{
394 395 396 397
  gchar **paths = get_module_path();
  gchar **path;
  gchar **result;
  gint count = 0;
398

399 400
  for (path = paths; *path; path++)
    count++;
401

402
  result = g_new (gchar *, count * 4 + 1);
403

404 405 406 407
  count = 0;
  for (path = get_module_path (); *path; path++)
    {
      gint use_version, use_host;
408
      
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
      for (use_version = TRUE; use_version >= FALSE; use_version--)
	for (use_host = TRUE; use_host >= FALSE; use_host--)
	  {
	    gchar *tmp_dir;
	    
	    if (use_version && use_host)
	      tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, GTK_HOST, type, NULL);
	    else if (use_version)
	      tmp_dir = g_build_filename (*path, GTK_BINARY_VERSION, type, NULL);
	    else if (use_host)
	      tmp_dir = g_build_filename (*path, GTK_HOST, type, NULL);
	    else
	      tmp_dir = g_build_filename (*path, type, NULL);

	    result[count++] = tmp_dir;
	  }
    }
426

427 428 429 430 431
  result[count++] = NULL;

  return result;
}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/* Like g_module_path, but use .la as the suffix
 */
static gchar*
module_build_la_path (const gchar *directory,
		      const gchar *module_name)
{
	gchar *filename;
	gchar *result;
	
	if (strncmp (module_name, "lib", 3) == 0)
		filename = (gchar *)module_name;
	else
		filename =  g_strconcat ("lib", module_name, ".la", NULL);

	if (directory && *directory)
		result = g_build_filename (directory, filename, NULL);
	else
		result = g_strdup (filename);

	if (filename != module_name)
		g_free (filename);

	return result;
}

457 458 459 460 461
/**
 * _gtk_find_module:
 * @name: the name of the module
 * @type: the type of the module, for instance 'modules', 'engines', immodules'
 * 
Matthias Clasen's avatar
Matthias Clasen committed
462 463
 * Looks for a dynamically loadable module named @name of type @type in the
 * standard GTK+ module search path.
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
 * 
 * Return value: the pathname to the found module, or %NULL if it wasn't found.
 *  Free with g_free().
 **/
gchar *
_gtk_find_module (const gchar *name,
		  const gchar *type)
{
  gchar **paths;
  gchar **path;
  gchar *module_name = NULL;

  if (g_path_is_absolute (name))
    return g_strdup (name);

  paths = _gtk_get_module_path (type);
  for (path = paths; *path; path++)
    {
482 483 484 485 486 487 488 489 490 491 492
      gchar *tmp_name;

      tmp_name = g_module_build_path (*path, name);
      if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
	{
	  module_name = tmp_name;
	  goto found;
	}
      g_free(tmp_name);

      tmp_name = module_build_la_path (*path, name);
493
      if (g_file_test (tmp_name, G_FILE_TEST_EXISTS))
494
	{
495 496
	  module_name = tmp_name;
	  goto found;
497
	}
498
      g_free(tmp_name);
499 500
    }

501
 found:
Michael Meeks's avatar
Michael Meeks committed
502
  g_strfreev (paths);
503 504 505 506
  return module_name;
}

static GModule *
507
find_module (const gchar *name)
508 509 510 511 512 513 514 515 516 517 518 519 520
{
  GModule *module;
  gchar *module_name;

  module_name = _gtk_find_module (name, "modules");
  if (!module_name)
    {
      /* As last resort, try loading without an absolute path (using system
       * library path)
       */
      module_name = g_module_build_path (NULL, name);
    }
  
521 522 523 524 525 526 527
  module = g_module_open (module_name, G_MODULE_BIND_LAZY);
  g_free(module_name);

  return module;
}

static GSList *
528
load_module (GSList      *module_list,
529 530 531
	     const gchar *name)
{
  GtkModuleInitFunc modinit_func = NULL;
532
  GtkModuleInfo *info;
533 534 535 536
  GModule *module = NULL;
  
  if (g_module_supported ())
    {
537
      module = find_module (name);
538
      if (module &&
539
	  g_module_symbol (module, "gtk_module_init", (gpointer *) &modinit_func) &&
540 541
	  modinit_func)
	{
542
	  if (!g_slist_find (module_list, (gconstpointer) modinit_func))
543 544
	    {
	      g_module_make_resident (module);
545 546 547 548 549 550 551
	      info = g_new (GtkModuleInfo, 1);

	      info->init_func = modinit_func;
	      g_module_symbol (module, "gtk_module_display_init",
			       (gpointer *) &info->display_init_func);
	      
	      module_list = g_slist_prepend (module_list, info);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	    }
	  else
	    {
	      g_module_close (module);
	      module = NULL;
	    }
	}
    }
  if (!modinit_func)
    {
      g_message ("Failed to load module \"%s\": %s",
		 module ? g_module_name (module) : name,
		 g_module_error ());
      if (module)
	g_module_close (module);
    }
  
569
  return module_list;
570 571 572 573 574 575
}

static GSList *
load_modules (const char *module_str)
{
  gchar **module_names = pango_split_file_list (module_str);
576
  GSList *module_list = NULL;
577 578 579
  gint i;
  
  for (i = 0; module_names[i]; i++)
580
    module_list = load_module (module_list, module_names[i]);
581
  
582
  module_list = g_slist_reverse (module_list);
583 584 585
  
  g_strfreev (module_names);

586
  return module_list;
587 588
}

589 590 591 592 593 594
static gboolean do_setlocale = TRUE;

/**
 * gtk_disable_setlocale:
 * 
 * Prevents gtk_init() and gtk_init_check() from automatically
595 596 597 598
 * 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.
599 600 601
 *
 * Most programs should not need to call this function.
 **/
602
void
603 604 605 606 607 608 609 610
gtk_disable_setlocale (void)
{
  if (gtk_initialized)
    g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
    
  do_setlocale = FALSE;
}

611 612
#undef gtk_init_check

613 614 615 616 617 618 619 620 621 622
static void
default_display_notify_cb (GdkDisplayManager *display_manager)
{
  GSList *slist;

  /* Initialize non-multihead-aware modules when the
   * default display is first set to a non-NULL value.
   */
  static gboolean initialized = FALSE;

Owen Taylor's avatar
Owen Taylor committed
623
  if (!gdk_display_get_default () || initialized)
624 625 626 627 628 629 630 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
    return;

  initialized = TRUE;

  for (slist = gtk_modules; slist; slist = slist->next)
    {
      if (slist->data)
	{
	  GtkModuleInfo *info = slist->data;

	  if (!info->display_init_func)
	    info->init_func (&gtk_argc, &gtk_argv);
	}
    }
}

static void
display_opened_cb (GdkDisplayManager *display_manager,
		   GdkDisplay        *display)
{
  GSList *slist;
  
  for (slist = gtk_modules; slist; slist = slist->next)
    {
      if (slist->data)
	{
	  GtkModuleInfo *info = slist->data;

	  if (info->display_init_func)
	    info->display_init_func (display);
	}
    }
}

/**
 * gdk_parse_args:
Matthias Clasen's avatar
Matthias Clasen committed
660 661
 * @argc: a pointer to the number of command line arguments.
 * @argv: a pointer to the array of command line arguments.
662 663 664
 * 
 * Parses command line arguments, and initializes global
 * attributes of GTK+, but does not actually open a connection
Owen Taylor's avatar
Owen Taylor committed
665
 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
666 667 668 669 670 671 672 673 674
 *
 * 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.
 **/
675
gboolean
676 677
gtk_parse_args (int    *argc,
		char ***argv)
Elliot Lee's avatar
Elliot Lee committed
678
{
679
  GString *gtk_modules_string = NULL;
680
  GSList *slist;
681
  GdkDisplayManager *display_manager;
682
  const gchar *env_string;
683

Tim Janik's avatar
Tim Janik committed
684
  if (gtk_initialized)
685
    return TRUE;
Tim Janik's avatar
Tim Janik committed
686

Owen Taylor's avatar
Owen Taylor committed
687 688 689
  if (!check_setugid ())
    return FALSE;
  
690 691 692 693 694 695
#if	0
  g_set_error_handler (gtk_error);
  g_set_warning_handler (gtk_warning);
  g_set_message_handler (gtk_message);
  g_set_print_handler (gtk_print);
#endif
696 697

  if (do_setlocale)
698 699 700 701
    {
      if (!setlocale (LC_ALL, ""))
	g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
    }
702

703
  gdk_parse_args (argc, argv);
Owen Taylor's avatar
Owen Taylor committed
704
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
705
  
706
#ifdef G_ENABLE_DEBUG
Matthias Clasen's avatar
Matthias Clasen committed
707
  env_string = g_getenv ("GTK_DEBUG");
708 709 710
  if (env_string != NULL)
    {
      gtk_debug_flags = g_parse_debug_string (env_string,
711 712
					      gtk_debug_keys,
					      gtk_ndebug_keys);
713 714
      env_string = NULL;
    }
715
#endif	/* G_ENABLE_DEBUG */
716

Matthias Clasen's avatar
Matthias Clasen committed
717
  env_string = g_getenv ("GTK_MODULES");
718
  if (env_string)
719
    gtk_modules_string = g_string_new (env_string);
720

721 722
  if (argc && argv)
    {
Owen Taylor's avatar
Owen Taylor committed
723
      gint i, j, k;
724 725 726
      
      for (i = 1; i < *argc;)
	{
727 728
	  if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
	      strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
729
	    {
730
	      gchar *module_name = (*argv)[i] + 12;
731
	      
732 733
	      if (*module_name == '=')
		module_name++;
734
	      else if (i + 1 < *argc)
735 736 737 738 739 740
		{
		  (*argv)[i] = NULL;
		  i += 1;
		  module_name = (*argv)[i];
		}
	      (*argv)[i] = NULL;
741

742
	      if (module_name && *module_name)
743 744 745 746 747 748 749 750
		{
		  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, module_name);
		}
751
	    }
752 753
	  else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
	    {
754
	      GLogLevelFlags fatal_mask;
755
	      
756 757 758
	      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);
759 760
	      (*argv)[i] = NULL;
	    }
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
#ifdef G_ENABLE_DEBUG
	  else if ((strcmp ("--gtk-debug", (*argv)[i]) == 0) ||
		   (strncmp ("--gtk-debug=", (*argv)[i], 12) == 0))
	    {
	      gchar *equal_pos = strchr ((*argv)[i], '=');
	      
	      if (equal_pos != NULL)
		{
		  gtk_debug_flags |= g_parse_debug_string (equal_pos+1,
							   gtk_debug_keys,
							   gtk_ndebug_keys);
		}
	      else if ((i + 1) < *argc && (*argv)[i + 1])
		{
		  gtk_debug_flags |= g_parse_debug_string ((*argv)[i+1],
							   gtk_debug_keys,
							   gtk_ndebug_keys);
		  (*argv)[i] = NULL;
		  i += 1;
		}
	      (*argv)[i] = NULL;
	    }
	  else if ((strcmp ("--gtk-no-debug", (*argv)[i]) == 0) ||
		   (strncmp ("--gtk-no-debug=", (*argv)[i], 15) == 0))
	    {
	      gchar *equal_pos = strchr ((*argv)[i], '=');
	      
	      if (equal_pos != NULL)
		{
		  gtk_debug_flags &= ~g_parse_debug_string (equal_pos+1,
							    gtk_debug_keys,
							    gtk_ndebug_keys);
		}
	      else if ((i + 1) < *argc && (*argv)[i + 1])
		{
		  gtk_debug_flags &= ~g_parse_debug_string ((*argv)[i+1],
							    gtk_debug_keys,
							    gtk_ndebug_keys);
		  (*argv)[i] = NULL;
		  i += 1;
		}
	      (*argv)[i] = NULL;
	    }
#endif /* G_ENABLE_DEBUG */
805 806
	  i += 1;
	}
807
      
Owen Taylor's avatar
Owen Taylor committed
808 809 810 811 812 813 814 815 816 817 818 819 820 821
      for (i = 1; i < *argc; i++)
	{
	  for (k = i; k < *argc; k++)
	    if ((*argv)[k] != NULL)
	      break;
	  
	  if (k > i)
	    {
	      k -= i;
	      for (j = i + k; j < *argc; j++)
		(*argv)[j-k] = (*argv)[j];
	      *argc -= k;
	    }
	}
822 823 824 825 826

      gtk_argv = g_malloc ((gtk_argc + 1) * sizeof (char*));
      for (i = 0; i < gtk_argc; i++)
	gtk_argv[i] = g_strdup ((*argv)[i]);
      gtk_argv[gtk_argc] = NULL;
827
    }
828 829 830

  if (gtk_debug_flags & GTK_DEBUG_UPDATES)
    gdk_window_set_debug_updates (TRUE);
831

832
  /* load gtk modules */
833
  if (gtk_modules_string)
834
    {
835 836
      gtk_modules = load_modules (gtk_modules_string->str);
      g_string_free (gtk_modules_string, TRUE);
837 838
    }

Owen Taylor's avatar
Owen Taylor committed
839
#ifdef ENABLE_NLS
840
  bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
841
  bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
Robert Brady's avatar
Robert Brady committed
842
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
843
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
844
  bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
845
#    endif
Owen Taylor's avatar
Owen Taylor committed
846 847
#endif  

848 849
  {
  /* Translate to default:RTL if you want your widgets
Robert Brady's avatar
Robert Brady committed
850 851 852
   * 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 
853 854 855 856 857 858 859 860 861
   */
    char *e = _("default:LTR");
    if (strcmp (e, "default:RTL")==0) {
      gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
    } else if (strcmp (e, "default:LTR")) {
      g_warning ("Whoever translated default:LTR did so wrongly.\n");
    }
  }

862
  gtk_type_init (0);
863
  _gtk_accel_map_init ();  
864
  _gtk_rc_init ();
865
  
Elliot Lee's avatar
Elliot Lee committed
866 867
  /* Set the 'initialized' flag.
   */
868
  gtk_initialized = TRUE;
869

870 871 872 873 874 875 876 877
  display_manager = gdk_display_manager_get ();
  g_signal_connect (display_manager, "notify::default-display",
		    G_CALLBACK (default_display_notify_cb), NULL);
  g_signal_connect (display_manager, "display-opened",
		    G_CALLBACK (display_opened_cb), NULL);

  /* initialize multhead aware gtk modules; for other modules,
   * we wait until we have a display open;
878
   */
879
  for (slist = gtk_modules; slist; slist = slist->next)
880
    {
881
      if (slist->data)
882
	{
883 884 885 886
	  GtkModuleInfo *info = slist->data;

	  if (info->display_init_func)
	    info->init_func (argc, argv);
887 888
	}
    }
889
  
890 891
  return TRUE;
}
892

893 894 895 896 897
#undef gtk_init_check

/**
 * gtk_init_check:
 * @argc: Address of the <parameter>argc</parameter> parameter of your 
Matthias Clasen's avatar
Matthias Clasen committed
898 899 900
 *   main() function. Changed if any arguments were handled.
 * @argv: Address of the <parameter>argv</parameter> parameter of main(). 
 *   Any parameters understood by gtk_init() are stripped before return.
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
 * 
 * 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 
 * 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;

919
  return gdk_display_open_default_libgtk_only () != NULL;
920 921
}

922 923
#undef gtk_init

924
/**
Matthias Clasen's avatar
Matthias Clasen committed
925
 * gtk_init:
926
 * @argc: Address of the <parameter>argc</parameter> parameter of your 
Matthias Clasen's avatar
Matthias Clasen committed
927 928 929
 *   main() function. Changed if any arguments were handled.
 * @argv: Address of the <parameter>argv</parameter> parameter of main(). 
 *   Any parameters understood by gtk_init() are stripped before return.
930 931
 * 
 * Call this function before using any other GTK+ functions in your GUI
Matthias Clasen's avatar
Matthias Clasen committed
932 933
 * applications.  It will initialize everything needed to operate the 
 * toolkit and parses some standard command line options. @argc and 
Matthias Clasen's avatar
Matthias Clasen committed
934
 * @argv are adjusted accordingly so your own code will 
935 936 937 938 939 940 941 942
 * never see those standard arguments.
 *
 * <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>
 **/
943 944 945 946 947
void
gtk_init (int *argc, char ***argv)
{
  if (!gtk_init_check (argc, argv))
    {
948
      const char *display_name_arg = gdk_get_display_arg_name ();
949
      g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
950
      exit (1);
951
    }
Elliot Lee's avatar
Elliot Lee committed
952 953
}

954
#ifdef G_PLATFORM_WIN32
955 956 957 958 959 960 961 962 963

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"
964 965 966
	     "your code with gcc without the -fnative-struct\n"
	     "(or -mms-bitfields) switch, or that you are using\n"
	     "an unsupported compiler.");
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
}

/* 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"
983 984 985
	     "your code with gcc without the -fnative-struct\n"
	     "(or -mms-bitfields) switch, or that you are using\n"
	     "an unsupported compiler.");
986 987 988 989 990 991
}

/* These two functions might get more checks added later, thus pass
 * in the number of extra args.
 */
void
992
gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
993 994
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
995 996
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
997 998 999 1000
  gtk_init (argc, argv);
}

gboolean
1001
gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
1002 1003
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
1004 1005
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
1006 1007 1008 1009 1010
  return gtk_init_check (argc, argv);
}

#endif

Elliot Lee's avatar
Elliot Lee committed
1011
void
Tor Lillqvist's avatar
Tor Lillqvist committed
1012
gtk_exit (gint errorcode)
Elliot Lee's avatar
Elliot Lee committed
1013
{
Manish Singh's avatar
Manish Singh committed
1014
  exit (errorcode);
Elliot Lee's avatar
Elliot Lee committed
1015 1016
}

1017 1018 1019 1020

/**
 * gtk_set_locale:
 *
1021 1022 1023
 * Initializes internationalization support for GTK+. gtk_init()
 * automatically does this, so there is typically no point
 * in calling this function.
1024
 *
1025 1026 1027 1028 1029
 * 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.)
1030
 * 
1031
 * In detail - sets the current locale according to the
1032 1033 1034
 * 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.
1035
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1036
 * Returns: a string corresponding to the locale set, typically in the
1037 1038
 * 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
1039 1040 1041
 * 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.
1042
 **/
Sven Neumann's avatar
Sven Neumann committed
1043
gchar *
1044
gtk_set_locale (void)
Elliot Lee's avatar
Elliot Lee committed
1045 1046 1047 1048
{
  return gdk_set_locale ();
}

1049
/**
1050
 * _gtk_get_lc_ctype:
1051
 *
1052 1053 1054 1055 1056 1057 1058 1059 1060
 * 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.
1061
 * 
1062 1063
 * 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
1064 1065 1066 1067 1068
 * 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
1069 1070 1071 1072 1073 1074 1075 1076
 * 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)
1077
{
1078 1079 1080 1081 1082
#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
1083 1084
   * locale-dependent C library functions like ctime(). But just for
   * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
1085 1086 1087 1088
   * 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
1089 1090
  gchar *p;

1091 1092
  p = getenv ("LC_ALL");
  if (p != NULL)
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    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 ();
1104
#else
1105
  return g_strdup (setlocale (LC_CTYPE, NULL));
1106
#endif
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
}

/**
 * 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
 * right-to-left or left-to-right text direction. See
Matthias Clasen's avatar
Matthias Clasen committed
1117
 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
 * 
 * Return value: the default language as a #PangoLanguage, must not be
 * freed
 **/
PangoLanguage *
gtk_get_default_language (void)
{
  gchar *lang;
  PangoLanguage *result;
  gchar *p;
  
  lang = _gtk_get_lc_ctype ();
1130 1131 1132 1133 1134 1135 1136
  p = strchr (lang, '.');
  if (p)
    *p = '\0';
  p = strchr (lang, '@');
  if (p)
    *p = '\0';

1137 1138 1139
  result = pango_language_from_string (lang);
  g_free (lang);
  
Hans Breuer's avatar
Hans Breuer committed
1140
  return result;
1141 1142
}

Elliot Lee's avatar
Elliot Lee committed
1143
void
1144
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
1145 1146 1147 1148
{
  GList *tmp_list;
  GList *functions;
  GtkInitFunction *init;
1149 1150
  GMainLoop *loop;

Tim Janik's avatar
Tim Janik committed
1151
  gtk_main_loop_level++;
1152
  
Manish Singh's avatar
Manish Singh committed
1153
  loop = g_main_loop_new (NULL, TRUE);
1154 1155
  main_loops = g_slist_prepend (main_loops, loop);

Elliot Lee's avatar
Elliot Lee committed
1156 1157
  tmp_list = functions = init_functions;
  init_functions = NULL;
1158
  
Elliot Lee's avatar
Elliot Lee committed
1159 1160 1161 1162
  while (tmp_list)
    {
      init = tmp_list->data;
      tmp_list = tmp_list->next;
1163
      
Elliot Lee's avatar
Elliot Lee committed
1164 1165 1166 1167
      (* init->function) (init->data);
      g_free (init);
    }
  g_list_free (functions);
1168

Manish Singh's avatar
Manish Singh committed
1169
  if (g_main_loop_is_running (main_loops->data))
1170 1171
    {
      GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1172
      g_main_loop_run (loop);
1173 1174 1175
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

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

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

1186
	  tmp_list = quit_functions;
1187
	  quit_functions = g_list_remove_link (quit_functions, quit_functions);
1188
	  g_list_free_1 (tmp_list);
1189

Tim Janik's avatar
Tim Janik committed
1190
	  if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
1191
	      gtk_quit_invoke_function (quitf))
1192
	    {
1193
	      reinvoke_list = g_list_prepend (reinvoke_list, quitf);
1194 1195 1196
	    }
	  else
	    {
1197
	      gtk_quit_destroy (quitf);
1198 1199 1200 1201
	    }
	}
      if (reinvoke_list)
	{
Tim Janik's avatar
Tim Janik committed
1202
	  GList *work;
1203
	  
Tim Janik's avatar
Tim Janik committed
1204
	  work = g_list_last (reinvoke_list);
1205
	  if (quit_functions)
Tim Janik's avatar
Tim Janik committed
1206 1207 1208
	    quit_functions->prev = work;
	  work->next = quit_functions;
	  quit_functions = work;
1209
	}
1210 1211

      gdk_flush ();
1212 1213
    }
	      
1214 1215
  main_loops = g_slist_remove (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1216
  g_main_loop_unref (loop);
1217

Tim Janik's avatar
Tim Janik committed
1218
  gtk_main_loop_level--;
1219 1220 1221 1222 1223
}

guint
gtk_main_level (void)
{
Tim Janik's avatar
Tim Janik committed
1224
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
1225 1226 1227
}

void
1228
gtk_main_quit (void)
Elliot Lee's avatar
Elliot Lee committed
1229
{
1230 1231
  g_return_if_fail (main_loops != NULL);

Manish Singh's avatar
Manish Singh committed
1232
  g_main_loop_quit (main_loops->data);
Elliot Lee's avatar
Elliot Lee committed
1233 1234
}

1235
gboolean
1236 1237
gtk_events_pending (void)
{
1238 1239 1240
  gboolean result;
  
  GDK_THREADS_LEAVE ();  
Manish Singh's avatar
Manish Singh committed
1241
  result = g_main_context_pending (NULL);
1242 1243 1244
  GDK_THREADS_ENTER ();

  return result;
1245 1246
}

1247
gboolean
1248
gtk_main_iteration (void)
1249
{
1250
  GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1251
  g_main_context_iteration (NULL, TRUE);
1252
  GDK_THREADS_ENTER ();
1253

1254
  if (main_loops)
Manish Singh's avatar
Manish Singh committed
1255
    return !g_main_loop_is_running (main_loops->data);
1256 1257
  else
    return TRUE;
1258 1259
}

1260
gboolean
1261
gtk_main_iteration_do (gboolean blocking)
1262
{
1263
  GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1264
  g_main_context_iteration (NULL, blocking);
1265
  GDK_THREADS_ENTER ();
1266

1267
  if (main_loops)
Manish Singh's avatar
Manish Singh committed
1268
    return !g_main_loop_is_running (main_loops->data);
1269 1270
  else
    return TRUE;
1271 1272
}

1273 1274
/* private libgtk to libgdk interfaces
 */
1275 1276
gboolean gdk_pointer_grab_info_libgtk_only  (GdkDisplay *display,
					     GdkWindow **grab_window,
1277
					     gboolean   *owner_events);
1278 1279
gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
					     GdkWindow **grab_window,
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
					     gboolean   *owner_events);

static void
rewrite_events_translate (GdkWindow *old_window,
			  GdkWindow *new_window,
			  gdouble   *x,
			  gdouble   *y)
{
  gint old_origin_x, old_origin_y;
  gint new_origin_x, new_origin_y;

  gdk_window_get_origin	(old_window, &old_origin_x, &old_origin_y);
  gdk_window_get_origin	(new_window, &new_origin_x, &new_origin_y);

1294 1295
  *x += old_origin_x - new_origin_x;
  *y += old_origin_y - new_origin_y;
1296 1297
}

1298
static GdkEvent *
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
rewrite_event_for_window (GdkEvent  *event,
			  GdkWindow *new_window)
{
  event = gdk_event_copy (event);

  switch (event->type)
    {
    case GDK_SCROLL:
      rewrite_events_translate (event->any.window,
				new_window,
				&event->scroll.x, &event->scroll.y);
      break;
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
      rewrite_events_translate (event->any.window,
				new_window,
				&event->button.x, &event->button.y);