gtkmain.c 43.2 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 "gdkconfig.h"

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

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

Elliot Lee's avatar
Elliot Lee committed
35 36
#include <stdio.h>
#include <stdlib.h>
37
#include <string.h>
38
#include <gmodule.h>
39 40 41
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
42
#include "gtkdnd.h"
43
#include "gtkcompat.h"
Elliot Lee's avatar
Elliot Lee committed
44 45 46
#include "gtkmain.h"
#include "gtkrc.h"
#include "gtkselection.h"
47
#include "gtksettings.h"
Elliot Lee's avatar
Elliot Lee committed
48 49 50
#include "gtksignal.h"
#include "gtkwidget.h"
#include "gtkwindow.h"
51
#include "gtkprivate.h"
52
#include "gdk/gdki18n.h"
53
#include "config.h"
54
#include "gtkdebug.h"
Owen Taylor's avatar
Owen Taylor committed
55
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
56 57 58

/* Private type definitions
 */
59 60
typedef struct _GtkInitFunction		 GtkInitFunction;
typedef struct _GtkQuitFunction		 GtkQuitFunction;
61
typedef struct _GtkClosure	         GtkClosure;
62
typedef struct _GtkKeySnooperData	 GtkKeySnooperData;
Elliot Lee's avatar
Elliot Lee committed
63 64 65 66 67 68 69

struct _GtkInitFunction
{
  GtkFunction function;
  gpointer data;
};

70 71
struct _GtkQuitFunction
{
72
  guint id;
73 74 75 76 77 78 79
  guint main_level;
  GtkCallbackMarshal marshal;
  GtkFunction function;
  gpointer data;
  GtkDestroyNotify destroy;
};

80
struct _GtkClosure
Elliot Lee's avatar
Elliot Lee committed
81
{
82
  GtkCallbackMarshal marshal;
Elliot Lee's avatar
Elliot Lee committed
83 84 85 86
  gpointer data;
  GtkDestroyNotify destroy;
};

87 88 89 90
struct _GtkKeySnooperData
{
  GtkKeySnoopFunc func;
  gpointer func_data;
91
  guint id;
92 93
};

94
static void  gtk_exit_func		 (void);
95 96 97 98
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);
99 100 101 102 103 104 105

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
106
#if 0
107 108 109 110
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
111
#endif
112

113 114
static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);

115 116 117
const guint gtk_major_version = GTK_MAJOR_VERSION;
const guint gtk_minor_version = GTK_MINOR_VERSION;
const guint gtk_micro_version = GTK_MICRO_VERSION;
118 119
const guint gtk_binary_age = GTK_BINARY_AGE;
const guint gtk_interface_age = GTK_INTERFACE_AGE;
120

Tim Janik's avatar
Tim Janik committed
121
static guint gtk_main_loop_level = 0;
122
static gint gtk_initialized = FALSE;
123
static GList *current_events = NULL;
Elliot Lee's avatar
Elliot Lee committed
124

125 126
static GSList *main_loops = NULL;      /* stack of currently executing main loops */

127
static GList *init_functions = NULL;	   /* A list of init functions.
Elliot Lee's avatar
Elliot Lee committed
128
					    */
129 130 131
static GList *quit_functions = NULL;	   /* A list of quit functions.
					    */
static GMemChunk *quit_mem_chunk = NULL;
Elliot Lee's avatar
Elliot Lee committed
132

133 134
static GSList *key_snoopers = NULL;

135
static GdkVisual *gtk_visual;		   /* The visual to be used in creating new
Elliot Lee's avatar
Elliot Lee committed
136 137
					    *  widgets.
					    */
138
static GdkColormap *gtk_colormap;	   /* The colormap to be used in creating new
Elliot Lee's avatar
Elliot Lee committed
139 140 141
					    *  widgets.
					    */

142
guint gtk_debug_flags = 0;		   /* Global GTK debug flag */
143 144

#ifdef G_ENABLE_DEBUG
145
static const GDebugKey gtk_debug_keys[] = {
146
  {"misc", GTK_DEBUG_MISC},
147
  {"dnd", GTK_DEBUG_DND},
148
  {"plugsocket", GTK_DEBUG_PLUGSOCKET},
149
  {"text", GTK_DEBUG_TEXT},
Havoc Pennington's avatar
Havoc Pennington committed
150 151
  {"tree", GTK_DEBUG_TREE},
  {"updates", GTK_DEBUG_UPDATES}
152
};
153

154
static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
155

156
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
157

158 159 160 161 162 163
gchar*
gtk_check_version (guint required_major,
		   guint required_minor,
		   guint required_micro)
{
  if (required_major > GTK_MAJOR_VERSION)
164
    return "Gtk+ version too old (major mismatch)";
165
  if (required_major < GTK_MAJOR_VERSION)
166
    return "Gtk+ version too new (major mismatch)";
167
  if (required_minor > GTK_MINOR_VERSION)
168
    return "Gtk+ version too old (minor mismatch)";
169
  if (required_minor < GTK_MINOR_VERSION)
170
    return "Gtk+ version too new (minor mismatch)";
171
  if (required_micro < GTK_MICRO_VERSION - GTK_BINARY_AGE)
172
    return "Gtk+ version too new (micro mismatch)";
173
  if (required_micro > GTK_MICRO_VERSION)
174
    return "Gtk+ version too old (micro mismatch)";
175 176 177
  return NULL;
}

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#ifdef __EMX__
static gchar *add_dll_suffix(gchar *module_name)
{
    gchar *suffix = strrchr(module_name, '.');
    
    if (!suffix || stricmp(suffix, ".dll"))
    {
	gchar *old = module_name;
	  
	module_name = g_strconcat (module_name, ".dll", NULL);
	g_free (old);
    }
    return (module_name);
}
#endif
193

194 195
#undef gtk_init_check

Owen Taylor's avatar
Owen Taylor committed
196 197 198 199 200 201 202 203 204 205
/* 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)
{
Hans Breuer's avatar
Hans Breuer committed
206 207
/* this isn't at all relevant on Windoze and doesn't compile ... --hb */
#ifndef G_OS_WIN32
Owen Taylor's avatar
Owen Taylor committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  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
237
#endif
Owen Taylor's avatar
Owen Taylor committed
238 239 240
  return TRUE;
}

241 242 243
gboolean
gtk_init_check (int	 *argc,
		char   ***argv)
Elliot Lee's avatar
Elliot Lee committed
244
{
245 246 247
  GSList *gtk_modules = NULL;
  GSList *slist;
  gchar *env_string = NULL;
248

Tim Janik's avatar
Tim Janik committed
249
  if (gtk_initialized)
250
    return TRUE;
Tim Janik's avatar
Tim Janik committed
251

Owen Taylor's avatar
Owen Taylor committed
252 253 254
  if (!check_setugid ())
    return FALSE;
  
255 256 257 258 259 260
#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
261
  
262 263
  /* Initialize "gdk". We pass along the 'argc' and 'argv'
   *  parameters as they contain information that GDK uses
Elliot Lee's avatar
Elliot Lee committed
264
   */
265 266
  if (!gdk_init_check (argc, argv))
    return FALSE;
267

Owen Taylor's avatar
Owen Taylor committed
268
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
269
  
270
#ifdef G_ENABLE_DEBUG
271 272 273 274
  env_string = getenv ("GTK_DEBUG");
  if (env_string != NULL)
    {
      gtk_debug_flags = g_parse_debug_string (env_string,
275 276
					      gtk_debug_keys,
					      gtk_ndebug_keys);
277 278
      env_string = NULL;
    }
279
#endif	/* G_ENABLE_DEBUG */
280

281 282 283 284 285
  env_string = getenv ("GTK_MODULES");
  if (env_string)
    {
      gchar **modules, **as;

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
286
#ifndef __EMX__
287
      modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
288 289 290
#else
      modules = g_strsplit (env_string, ";", -1);
#endif
291 292 293 294 295 296 297 298 299 300 301
      for (as = modules; *as; as++)
	{
	  if (**as)
	    gtk_modules = g_slist_prepend (gtk_modules, *as);
	  else
	    g_free (*as);
	}
      g_free (modules);
      env_string = NULL;
    }

302 303
  if (argc && argv)
    {
Owen Taylor's avatar
Owen Taylor committed
304
      gint i, j, k;
305 306 307
      
      for (i = 1; i < *argc;)
	{
308 309
	  if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
	      strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
310
	    {
311
	      gchar *module_name = (*argv)[i] + 12;
312
	      
313 314
	      if (*module_name == '=')
		module_name++;
315
	      else if (i + 1 < *argc)
316 317 318 319 320 321
		{
		  (*argv)[i] = NULL;
		  i += 1;
		  module_name = (*argv)[i];
		}
	      (*argv)[i] = NULL;
322

323 324
	      if (module_name && *module_name)
		gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
325
	    }
326 327
	  else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
	    {
328
	      GLogLevelFlags fatal_mask;
329
	      
330 331 332
	      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);
333 334
	      (*argv)[i] = NULL;
	    }
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
#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 */
379 380
	  i += 1;
	}
381
      
Owen Taylor's avatar
Owen Taylor committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395
      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;
	    }
	}
396
    }
Havoc Pennington's avatar
Havoc Pennington committed
397 398 399

  if (gtk_debug_flags & GTK_DEBUG_UPDATES)
    gdk_window_set_debug_updates (TRUE);
400
  
401 402 403 404 405 406 407 408 409 410
  /* load gtk modules */
  gtk_modules = g_slist_reverse (gtk_modules);
  for (slist = gtk_modules; slist; slist = slist->next)
    {
      gchar *module_name;
      GModule *module = NULL;
      GtkModuleInitFunc modinit_func = NULL;
      
      module_name = slist->data;
      slist->data = NULL;
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
411
#ifndef __EMX__
412
      if (!g_path_is_absolute (module_name))
413 414 415
	{
	  gchar *old = module_name;
	  
416
	  module_name = g_module_build_path (NULL, module_name);
417 418
	  g_free (old);
	}
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
419 420 421
#else
      module_name = add_dll_suffix(module_name);
#endif
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
      if (g_module_supported ())
	{
	  module = g_module_open (module_name, G_MODULE_BIND_LAZY);
	  if (module &&
	      g_module_symbol (module, "gtk_module_init", (gpointer*) &modinit_func) &&
	      modinit_func)
	    {
	      if (!g_slist_find (gtk_modules, modinit_func))
		{
		  g_module_make_resident (module);
		  slist->data = modinit_func;
		}
	      else
		{
		  g_module_close (module);
		  module = NULL;
		}
	    }
	}
      if (!modinit_func)
	{
443
	  g_message ("Failed to load module \"%s\": %s",
444 445 446 447 448 449 450 451
		     module ? g_module_name (module) : module_name,
		     g_module_error ());
	  if (module)
	    g_module_close (module);
	}
      g_free (module_name);
    }

Owen Taylor's avatar
Owen Taylor committed
452
#ifdef ENABLE_NLS
453
#  ifndef G_OS_WIN32
454
  bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
Robert Brady's avatar
Robert Brady committed
455
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
456 457 458
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#    endif
#  else /* !G_OS_WIN32 */
459
  {
Tor Lillqvist's avatar
Tor Lillqvist committed
460
    bindtextdomain (GETTEXT_PACKAGE,
Tor Lillqvist's avatar
Tor Lillqvist committed
461 462 463
		    g_win32_get_package_installation_subdirectory (GETTEXT_PACKAGE,
								   g_strdup_printf ("gtk-%d.%d.dll", GTK_MAJOR_VERSION, GTK_MINOR_VERSION),
								   "locale"));
464 465
  }
#endif
Owen Taylor's avatar
Owen Taylor committed
466 467
#endif  

468 469
  {
  /* Translate to default:RTL if you want your widgets
Robert Brady's avatar
Robert Brady committed
470 471 472
   * 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 
473 474 475 476 477 478 479 480 481
   */
    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");
    }
  }

Elliot Lee's avatar
Elliot Lee committed
482 483 484 485 486 487
  /* Initialize the default visual and colormap to be
   *  used in creating widgets. (We want to use the system
   *  defaults so as to be nice to the colormap).
   */
  gtk_visual = gdk_visual_get_system ();
  gtk_colormap = gdk_colormap_get_system ();
488

489
  gtk_type_init (0);
490
  _gtk_rc_init ();
491 492
  
  
Elliot Lee's avatar
Elliot Lee committed
493 494
  /* Register an exit function to make sure we are able to cleanup.
   */
Tim Janik's avatar
Tim Janik committed
495
  g_atexit (gtk_exit_func);
496
  
Elliot Lee's avatar
Elliot Lee committed
497 498
  /* Set the 'initialized' flag.
   */
499
  gtk_initialized = TRUE;
500

501
  /* initialize gtk modules
502
   */
503
  for (slist = gtk_modules; slist; slist = slist->next)
504
    {
505
      if (slist->data)
506 507
	{
	  GtkModuleInitFunc modinit;
508
	  
509 510 511 512
	  modinit = slist->data;
	  modinit (argc, argv);
	}
    }
513
  g_slist_free (gtk_modules);
514
  
Manish Singh's avatar
Manish Singh committed
515
#ifndef G_OS_WIN32
516
  /* No use warning on Win32, there aren't any non-devel versions anyhow... */
517
  g_message (""              "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
518 519 520 521 522
	     "                UNDER HEAVY DEVELOPMENT AND FREQUENTLY INTRODUCES INSTABILITIES.\n"
	     "                if you don't know why you are getting this, you probably want to\n"
	     "                use the stable branch which can be retrived from\n"
	     "                ftp://ftp.gtk.org/pub/gtk/v1.2/ or via CVS with\n"
	     "                cvs checkout -r glib-1-2 glib; cvs checkout -r gtk-1-2 gtk+");
523 524
#endif

525 526
  return TRUE;
}
527 528 529

#undef gtk_init

530 531 532 533 534 535 536 537
void
gtk_init (int *argc, char ***argv)
{
  if (!gtk_init_check (argc, argv))
    {
      g_warning ("cannot open display: %s", gdk_get_display ());
      exit(1);
    }
Elliot Lee's avatar
Elliot Lee committed
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
#ifdef G_OS_WIN32

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"
	     "your code with gcc without the -fnative-struct switch.");
}

/* These two functions might get more checks added later, thus pass
 * in the number of extra args.
 */
void
gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
  gtk_init (argc, argv);
}

gboolean
gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow)
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
  return gtk_init_check (argc, argv);
}

#endif

Elliot Lee's avatar
Elliot Lee committed
572
void
Tor Lillqvist's avatar
Tor Lillqvist committed
573
gtk_exit (gint errorcode)
Elliot Lee's avatar
Elliot Lee committed
574 575 576
{
  /* Only if "gtk" has been initialized should we de-initialize.
   */
577 578 579
  /* de-initialisation is done by the gtk_exit_funct(),
   * no need to do this here (Alex J.)
   */
Elliot Lee's avatar
Elliot Lee committed
580 581 582 583
  gdk_exit(errorcode);
}

gchar*
584
gtk_set_locale (void)
Elliot Lee's avatar
Elliot Lee committed
585 586 587 588
{
  return gdk_set_locale ();
}

589 590 591 592 593 594 595 596 597 598 599
/**
 * gtk_get_default_language:
 *
 * Returns the ISO language code 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.
 * 
 * Return value: the default language as an allocated string, must be freed
 **/
600
PangoLanguage *
601 602 603
gtk_get_default_language (void)
{
  gchar *lang;
604
  PangoLanguage *result;
605 606 607 608 609 610 611 612 613 614
  gchar *p;
  
  lang = g_strdup (setlocale (LC_CTYPE, NULL));
  p = strchr (lang, '.');
  if (p)
    *p = '\0';
  p = strchr (lang, '@');
  if (p)
    *p = '\0';

615 616 617
  result = pango_language_from_string (lang);
  g_free (lang);
  
Hans Breuer's avatar
Hans Breuer committed
618
  return result;
619 620
}

Elliot Lee's avatar
Elliot Lee committed
621
void
622
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
623 624 625 626
{
  GList *tmp_list;
  GList *functions;
  GtkInitFunction *init;
627 628
  GMainLoop *loop;

Tim Janik's avatar
Tim Janik committed
629
  gtk_main_loop_level++;
630
  
631 632 633
  loop = g_main_new (TRUE);
  main_loops = g_slist_prepend (main_loops, loop);

Elliot Lee's avatar
Elliot Lee committed
634 635
  tmp_list = functions = init_functions;
  init_functions = NULL;
636
  
Elliot Lee's avatar
Elliot Lee committed
637 638 639 640
  while (tmp_list)
    {
      init = tmp_list->data;
      tmp_list = tmp_list->next;
641
      
Elliot Lee's avatar
Elliot Lee committed
642 643 644 645
      (* init->function) (init->data);
      g_free (init);
    }
  g_list_free (functions);
646

647 648 649 650 651 652 653
  if (g_main_is_running (main_loops->data))
    {
      GDK_THREADS_LEAVE ();
      g_main_run (loop);
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
654 655 656 657 658 659 660 661 662 663

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

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

664
	  tmp_list = quit_functions;
665
	  quit_functions = g_list_remove_link (quit_functions, quit_functions);
666
	  g_list_free_1 (tmp_list);
667

Tim Janik's avatar
Tim Janik committed
668
	  if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
669
	      gtk_quit_invoke_function (quitf))
670
	    {
671
	      reinvoke_list = g_list_prepend (reinvoke_list, quitf);
672 673 674
	    }
	  else
	    {
675
	      gtk_quit_destroy (quitf);
676 677 678 679
	    }
	}
      if (reinvoke_list)
	{
Tim Janik's avatar
Tim Janik committed
680
	  GList *work;
681
	  
Tim Janik's avatar
Tim Janik committed
682
	  work = g_list_last (reinvoke_list);
683
	  if (quit_functions)
Tim Janik's avatar
Tim Janik committed
684 685 686
	    quit_functions->prev = work;
	  work->next = quit_functions;
	  quit_functions = work;
687
	}
688 689

      gdk_flush ();
690 691
    }
	      
692 693 694 695
  main_loops = g_slist_remove (main_loops, loop);

  g_main_destroy (loop);

Tim Janik's avatar
Tim Janik committed
696
  gtk_main_loop_level--;
697 698 699 700 701
}

guint
gtk_main_level (void)
{
Tim Janik's avatar
Tim Janik committed
702
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
703 704 705
}

void
706
gtk_main_quit (void)
Elliot Lee's avatar
Elliot Lee committed
707
{
708 709
  g_return_if_fail (main_loops != NULL);

710
  g_main_quit (main_loops->data);
Elliot Lee's avatar
Elliot Lee committed
711 712
}

Owen Taylor's avatar
Owen Taylor committed
713 714 715
gint
gtk_events_pending (void)
{
716 717 718 719 720 721 722
  gboolean result;
  
  GDK_THREADS_LEAVE ();  
  result = g_main_pending();
  GDK_THREADS_ENTER ();

  return result;
Owen Taylor's avatar
Owen Taylor committed
723 724
}

725
gboolean
726
gtk_main_iteration (void)
727
{
728
  GDK_THREADS_LEAVE ();
729
  g_main_iteration (TRUE);
730
  GDK_THREADS_ENTER ();
731

732 733
  if (main_loops)
    return !g_main_is_running (main_loops->data);
734 735
  else
    return TRUE;
736 737
}

738
gboolean
739
gtk_main_iteration_do (gboolean blocking)
740
{
741
  GDK_THREADS_LEAVE ();
742
  g_main_iteration (blocking);
743
  GDK_THREADS_ENTER ();
744

745 746
  if (main_loops)
    return !g_main_is_running (main_loops->data);
747 748
  else
    return TRUE;
749 750
}

Owen Taylor's avatar
Owen Taylor committed
751
void 
752
gtk_main_do_event (GdkEvent *event)
Elliot Lee's avatar
Elliot Lee committed
753 754 755
{
  GtkWidget *event_widget;
  GtkWidget *grab_widget;
756
  GtkWindowGroup *window_group;
757
  GdkEvent *next_event;
758
  GList *tmp_list;
759 760 761

  /* If there are any events pending then get the next one.
   */
762
  next_event = gdk_event_peek ();
763
  
764 765 766 767 768 769 770
  /* Try to compress enter/leave notify events. These event
   *  pairs occur when the mouse is dragged quickly across
   *  a window with many buttons (or through a menu). Instead
   *  of highlighting and de-highlighting each widget that
   *  is crossed it is better to simply de-highlight the widget
   *  which contained the mouse initially and highlight the
   *  widget which ends up containing the mouse.
771
   */
772 773 774 775 776 777 778 779
  if (next_event)
    if (((event->type == GDK_ENTER_NOTIFY) ||
	 (event->type == GDK_LEAVE_NOTIFY)) &&
	((next_event->type == GDK_ENTER_NOTIFY) ||
	 (next_event->type == GDK_LEAVE_NOTIFY)) &&
	(next_event->type != event->type) &&
	(next_event->any.window == event->any.window))
      {
780 781 782 783
	/* Throw both the peeked copy and the queued copy away 
	 */
	gdk_event_free (next_event);
	next_event = gdk_event_get ();
784 785 786 787
	gdk_event_free (next_event);
	
	return;
      }
788

789
  if (next_event)
790
    gdk_event_free (next_event);
791 792 793 794 795 796 797 798 799 800

  /* Find the widget which got the event. We store the widget
   *  in the user_data field of GdkWindow's.
   *  Ignore the event if we don't have a widget for it, except
   *  for GDK_PROPERTY_NOTIFY events which are handled specialy.
   *  Though this happens rarely, bogus events can occour
   *  for e.g. destroyed GdkWindows. 
   */
  event_widget = gtk_get_event_widget (event);
  if (!event_widget)
Elliot Lee's avatar
Elliot Lee committed
801
    {
802 803 804 805 806 807 808 809 810
      /* To handle selection INCR transactions, we select
       * PropertyNotify events on the requestor window and create
       * a corresponding (fake) GdkWindow so that events get
       * here. There won't be a widget though, so we have to handle
	   * them specially
	   */
      if (event->type == GDK_PROPERTY_NOTIFY)
	gtk_selection_incr_event (event->any.window,
				  &event->property);
811 812 813
      else if (event->type == GDK_SETTING)
	_gtk_settings_handle_event (&event->setting);

814
      return;
Elliot Lee's avatar
Elliot Lee committed
815
    }
816
  
817 818
  /* Push the event onto a stack of current events for
   * gtk_current_event_get().
Elliot Lee's avatar
Elliot Lee committed
819
   */
820
  current_events = g_list_prepend (current_events, event);
821 822

  window_group = gtk_main_get_window_group (event_widget);
823
  
824
  /* If there is a grab in effect...
Elliot Lee's avatar
Elliot Lee committed
825
   */
826
  if (window_group->grabs)
Elliot Lee's avatar
Elliot Lee committed
827
    {
828
      grab_widget = window_group->grabs->data;
829
      
830 831 832 833 834 835 836 837 838 839 840
      /* If the grab widget is an ancestor of the event widget
       *  then we send the event to the original event widget.
       *  This is the key to implementing modality.
       */
      if (GTK_WIDGET_IS_SENSITIVE (event_widget) &&
	  gtk_widget_is_ancestor (event_widget, grab_widget))
	grab_widget = event_widget;
    }
  else
    {
      grab_widget = event_widget;
Elliot Lee's avatar
Elliot Lee committed
841
    }
842

843 844 845 846 847 848 849 850 851
  /* Not all events get sent to the grabbing widget.
   * The delete, destroy, expose, focus change and resize
   *  events still get sent to the event widget because
   *  1) these events have no meaning for the grabbing widget
   *  and 2) redirecting these events to the grabbing widget
   *  could cause the display to be messed up.
   * 
   * Drag events are also not redirected, since it isn't
   *  clear what the semantics of that would be.
Elliot Lee's avatar
Elliot Lee committed
852
   */
853
  switch (event->type)
Elliot Lee's avatar
Elliot Lee committed
854
    {
855 856
    case GDK_NOTHING:
      break;
857
      
858 859
    case GDK_DELETE:
      gtk_widget_ref (event_widget);
860
      if ((!window_group->grabs || gtk_widget_get_toplevel (window_group->grabs->data) == event_widget) &&
Owen Taylor's avatar
Owen Taylor committed
861
	  !gtk_widget_event (event_widget, event))
862 863 864
	gtk_widget_destroy (event_widget);
      gtk_widget_unref (event_widget);
      break;
865
      
866
    case GDK_DESTROY:
867 868 869 870 871 872 873
      /* Unexpected GDK_DESTROY from the outside, ignore for
       * child windows, handle like a GDK_DELETE for toplevels
       */
      if (!event_widget->parent)
	{
	  gtk_widget_ref (event_widget);
	  if (!gtk_widget_event (event_widget, event) &&
874
	      GTK_WIDGET_REALIZED (event_widget))
875 876 877
	    gtk_widget_destroy (event_widget);
	  gtk_widget_unref (event_widget);
	}
878
      break;
879
      
880
    case GDK_EXPOSE:
881
      if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
882 883 884 885 886 887 888
	{
	  gdk_window_begin_paint_region (event->any.window, event->expose.region);
	  gtk_widget_send_expose (event_widget, event);
	  gdk_window_end_paint (event->any.window);
	}
      else
	gtk_widget_send_expose (event_widget, event);
889 890 891
      break;

    case GDK_PROPERTY_NOTIFY:
892 893 894 895 896 897 898 899 900 901
    case GDK_NO_EXPOSE:
    case GDK_FOCUS_CHANGE:
    case GDK_CONFIGURE:
    case GDK_MAP:
    case GDK_UNMAP:
    case GDK_SELECTION_CLEAR:
    case GDK_SELECTION_REQUEST:
    case GDK_SELECTION_NOTIFY:
    case GDK_CLIENT_EVENT:
    case GDK_VISIBILITY_NOTIFY:
Havoc Pennington's avatar
Havoc Pennington committed
902
    case GDK_WINDOW_STATE:
903 904
      gtk_widget_event (event_widget, event);
      break;
905

906
    case GDK_SCROLL:
907 908 909 910 911 912
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
      gtk_propagate_event (grab_widget, event);
      break;

913 914 915
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      if (key_snoopers)
Elliot Lee's avatar
Elliot Lee committed
916
	{
917 918
	  if (gtk_invoke_key_snoopers (grab_widget, event))
	    break;
Elliot Lee's avatar
Elliot Lee committed
919
	}
920 921 922 923 924 925 926 927 928 929
      /* else fall through */
    case GDK_MOTION_NOTIFY:
    case GDK_BUTTON_RELEASE:
    case GDK_PROXIMITY_IN:
    case GDK_PROXIMITY_OUT:
      gtk_propagate_event (grab_widget, event);
      break;
      
    case GDK_ENTER_NOTIFY:
      if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
Elliot Lee's avatar
Elliot Lee committed
930
	{
931 932 933
	  gtk_widget_event (grab_widget, event);
	  if (event_widget == grab_widget)
	    GTK_PRIVATE_SET_FLAG (event_widget, GTK_LEAVE_PENDING);
Elliot Lee's avatar
Elliot Lee committed
934
	}
935 936 937 938
      break;
      
    case GDK_LEAVE_NOTIFY:
      if (GTK_WIDGET_LEAVE_PENDING (event_widget))
Elliot Lee's avatar
Elliot Lee committed
939
	{
940
	  GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
941
	  gtk_widget_event (event_widget, event);
Elliot Lee's avatar
Elliot Lee committed
942
	}
943 944 945
      else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
	gtk_widget_event (grab_widget, event);
      break;
946
      
947 948
    case GDK_DRAG_STATUS:
    case GDK_DROP_FINISHED:
949
      _gtk_drag_source_handle_event (event_widget, event);
950 951 952 953 954
      break;
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DROP_START:
955
      _gtk_drag_dest_handle_event (event_widget, event);
956
      break;
957 958 959
    default:
      g_assert_not_reached ();
      break;
Elliot Lee's avatar
Elliot Lee committed
960
    }
961
  
962 963 964
  tmp_list = current_events;
  current_events = g_list_remove_link (current_events, tmp_list);
  g_list_free_1 (tmp_list);
Elliot Lee's avatar
Elliot Lee committed
965 966
}

967
gboolean
Elliot Lee's avatar
Elliot Lee committed
968 969 970 971 972
gtk_true (void)
{
  return TRUE;
}

973
gboolean
Elliot Lee's avatar
Elliot Lee committed
974 975 976 977 978
gtk_false (void)
{
  return FALSE;
}

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
static GtkWindowGroup *
gtk_main_get_window_group (GtkWidget   *widget)
{
  GtkWidget *toplevel = NULL;

  if (widget)
    toplevel = gtk_widget_get_toplevel (widget);

  if (toplevel && GTK_IS_WINDOW (toplevel))
    return _gtk_window_get_group (GTK_WINDOW (toplevel));
  else
    return _gtk_window_get_group (NULL);
}

typedef struct
{
  gboolean was_grabbed;
  GtkWidget *grab_widget;
} GrabNotifyInfo;

static void
gtk_grab_notify_foreach (GtkWidget *child,
			 gpointer   data)
                        
{
  GrabNotifyInfo *info = data;

  if (child != info->grab_widget)
    {
      g_object_ref (G_OBJECT (child));

      gtk_signal_emit_by_name (GTK_OBJECT (child), "grab_notify", info->was_grabbed);

      if (GTK_IS_CONTAINER (child))
       gtk_container_foreach (GTK_CONTAINER (child), gtk_grab_notify_foreach, info);
      
      g_object_unref (G_OBJECT (child));
    }
}

static void
gtk_grab_notify (GtkWindowGroup *group,
		 GtkWidget      *grab_widget,
		 gboolean        was_grabbed)
{
  GList *toplevels;
  GrabNotifyInfo info;

  info.grab_widget = grab_widget;
  info.was_grabbed = was_grabbed;

  g_object_ref (group);
  g_object_ref (grab_widget);

  toplevels = gtk_window_list_toplevels ();
  g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
			    
  while (toplevels)
    {
      GtkWindow *toplevel = toplevels->data;
      toplevels = g_list_delete_link (toplevels, toplevels);

      if (group == toplevel->group)
	gtk_container_foreach (GTK_CONTAINER (toplevel), gtk_grab_notify_foreach, &info);
      g_object_unref (toplevel);
    }

  g_object_unref (group);
  g_object_unref (grab_widget);
}

Elliot Lee's avatar
Elliot Lee committed
1050 1051 1052
void
gtk_grab_add (GtkWidget *widget)
{
1053 1054 1055
  GtkWindowGroup *group;
  gboolean was_grabbed;
  
1056
  g_return_if_fail (widget != NULL);
1057
  
1058
  if (!GTK_WIDGET_HAS_GRAB (widget) && GTK_WIDGET_IS_SENSITIVE (widget))
1059 1060
    {
      GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
1061
      
1062 1063 1064 1065
      group = gtk_main_get_window_group (widget);

      was_grabbed = (group->grabs != NULL);
      
1066
      gtk_widget_ref (widget);
1067 1068 1069 1070
      group->grabs = g_slist_prepend (group->grabs, widget);

      if (!was_grabbed)
	gtk_grab_notify (group, widget, FALSE);
1071
    }
Elliot Lee's avatar
Elliot Lee committed
1072 1073
}

1074 1075 1076
GtkWidget*
gtk_grab_get_current (void)
{
1077 1078 1079 1080 1081 1082
  GtkWindowGroup *group;

  group = gtk_main_get_window_group (NULL);

  if (group->grabs)
    return GTK_WIDGET (group->grabs->data);
1083 1084 1085
  return NULL;
}

Elliot Lee's avatar
Elliot Lee committed
1086 1087 1088
void
gtk_grab_remove (GtkWidget *widget)
{
1089 1090
  GtkWindowGroup *group;
  
1091
  g_return_if_fail (widget != NULL);
1092
  
1093 1094 1095
  if (GTK_WIDGET_HAS_GRAB (widget))
    {
      GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
1096 1097 1098

      group = gtk_main_get_window_group (widget);
      group->grabs = g_slist_remove (group->grabs, widget);
1099
      
1100
      gtk_widget_unref (widget);
1101 1102 1103

      if (!group->grabs)
	gtk_grab_notify (group, widget, TRUE);
Tim Janik's avatar