gtkmain.c 34.7 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
#ifdef GDK_WINDOWING_X11
28
#include <X11/Xlocale.h>	/* so we get the right setlocale */
29 30 31
#else
#include <locale.h>
#endif
Elliot Lee's avatar
Elliot Lee committed
32 33
#include <stdio.h>
#include <stdlib.h>
34
#include <string.h>
35
#include <gmodule.h>
Elliot Lee's avatar
Elliot Lee committed
36
#include "gtkbutton.h"
37
#include "gtkdnd.h"
38
#include "gtkcompat.h"
Elliot Lee's avatar
Elliot Lee committed
39 40 41 42 43
#include "gtkhscrollbar.h"
#include "gtkhseparator.h"
#include "gtkmain.h"
#include "gtkpreview.h"
#include "gtkrc.h"
44
#include "gtkscrolledwindow.h"
Elliot Lee's avatar
Elliot Lee committed
45 46 47 48 49 50 51 52
#include "gtkselection.h"
#include "gtksignal.h"
#include "gtktable.h"
#include "gtktext.h"
#include "gtkvbox.h"
#include "gtkvscrollbar.h"
#include "gtkwidget.h"
#include "gtkwindow.h"
53
#include "gtkprivate.h"
54
#include "gdk/gdki18n.h"
55
#include "config.h"
56
#include "gtkdebug.h"
Owen Taylor's avatar
Owen Taylor committed
57
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
58 59 60

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

struct _GtkInitFunction
{
  GtkFunction function;
  gpointer data;
};

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

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

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

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

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

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 128
static GSList *grabs = NULL;		   /* A stack of unique grabs. The grabbing
					    *  widget is the first one on the list.
Elliot Lee's avatar
Elliot Lee committed
129
					    */
130
static GList *init_functions = NULL;	   /* A list of init functions.
Elliot Lee's avatar
Elliot Lee committed
131
					    */
132 133 134
static GList *quit_functions = NULL;	   /* A list of quit functions.
					    */
static GMemChunk *quit_mem_chunk = NULL;
Elliot Lee's avatar
Elliot Lee committed
135

136 137
static GSList *key_snoopers = NULL;

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

145
guint gtk_debug_flags = 0;		   /* Global GTK debug flag */
146 147

#ifdef G_ENABLE_DEBUG
148
static const GDebugKey gtk_debug_keys[] = {
149
  {"objects", GTK_DEBUG_OBJECTS},
150
  {"misc", GTK_DEBUG_MISC},
151
  {"signals", GTK_DEBUG_SIGNALS},
152
  {"dnd", GTK_DEBUG_DND},
153 154
  {"plugsocket", GTK_DEBUG_PLUGSOCKET},
  {"text", GTK_DEBUG_TEXT}
155
};
156

157
static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
158

159
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
160

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

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#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
196

197 198
#undef gtk_init_check

199 200 201
gboolean
gtk_init_check (int	 *argc,
		char   ***argv)
Elliot Lee's avatar
Elliot Lee committed
202
{
203 204 205
  GSList *gtk_modules = NULL;
  GSList *slist;
  gchar *env_string = NULL;
206

Tim Janik's avatar
Tim Janik committed
207
  if (gtk_initialized)
208
    return TRUE;
Tim Janik's avatar
Tim Janik committed
209

210 211 212 213 214 215
#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
216
  
217 218
  /* Initialize "gdk". We pass along the 'argc' and 'argv'
   *  parameters as they contain information that GDK uses
Elliot Lee's avatar
Elliot Lee committed
219
   */
220 221
  if (!gdk_init_check (argc, argv))
    return FALSE;
222

Owen Taylor's avatar
Owen Taylor committed
223
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
224
  
225
#ifdef G_ENABLE_DEBUG
226 227 228 229
  env_string = getenv ("GTK_DEBUG");
  if (env_string != NULL)
    {
      gtk_debug_flags = g_parse_debug_string (env_string,
230 231
					      gtk_debug_keys,
					      gtk_ndebug_keys);
232 233
      env_string = NULL;
    }
234
#endif	/* G_ENABLE_DEBUG */
235

236 237 238 239 240
  env_string = getenv ("GTK_MODULES");
  if (env_string)
    {
      gchar **modules, **as;

Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
241
#ifndef __EMX__
242
      modules = g_strsplit (env_string, G_SEARCHPATH_SEPARATOR_S, -1);
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
243 244 245
#else
      modules = g_strsplit (env_string, ";", -1);
#endif
246 247 248 249 250 251 252 253 254 255 256
      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;
    }

257 258
  if (argc && argv)
    {
Owen Taylor's avatar
Owen Taylor committed
259
      gint i, j, k;
260 261 262
      
      for (i = 1; i < *argc;)
	{
263 264
	  if (strcmp ("--gtk-module", (*argv)[i]) == 0 ||
	      strncmp ("--gtk-module=", (*argv)[i], 13) == 0)
265
	    {
266
	      gchar *module_name = (*argv)[i] + 12;
267
	      
268 269
	      if (*module_name == '=')
		module_name++;
270
	      else if (i + 1 < *argc)
271 272 273 274 275 276
		{
		  (*argv)[i] = NULL;
		  i += 1;
		  module_name = (*argv)[i];
		}
	      (*argv)[i] = NULL;
277

278 279
	      if (module_name && *module_name)
		gtk_modules = g_slist_prepend (gtk_modules, g_strdup (module_name));
280
	    }
281 282
	  else if (strcmp ("--g-fatal-warnings", (*argv)[i]) == 0)
	    {
283
	      GLogLevelFlags fatal_mask;
284
	      
285 286 287
	      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);
288 289
	      (*argv)[i] = NULL;
	    }
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
#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 */
334 335
	  i += 1;
	}
336
      
Owen Taylor's avatar
Owen Taylor committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350
      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;
	    }
	}
351
    }
352
  
353 354 355 356 357 358 359 360 361 362
  /* 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
363
#ifndef __EMX__
364
      if (!g_path_is_absolute (module_name))
365 366 367
	{
	  gchar *old = module_name;
	  
368
	  module_name = g_module_build_path (NULL, module_name);
369 370
	  g_free (old);
	}
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
371 372 373
#else
      module_name = add_dll_suffix(module_name);
#endif
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 400 401 402 403
      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)
	{
	  g_warning ("Failed to load module \"%s\": %s",
		     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
404
#ifdef ENABLE_NLS
Manish Singh's avatar
Manish Singh committed
405
#ifndef G_OS_WIN32
406
  bindtextdomain(GETTEXT_PACKAGE, GTK_LOCALEDIR);
407 408
#else
  {
409
    /* GTk+ locale dir is %WinDir%\gtk+\locale */
Tor Lillqvist's avatar
Tor Lillqvist committed
410 411 412 413 414
    bindtextdomain (GETTEXT_PACKAGE,
		    g_strconcat (gtk_win32_get_installation_directory (),
				 G_DIR_SEPARATOR_S,
				 "locale",
				 NULL));
415 416
  }
#endif
Owen Taylor's avatar
Owen Taylor committed
417 418
#endif  

Elliot Lee's avatar
Elliot Lee committed
419 420 421 422 423 424
  /* 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 ();
425 426 427

  gtk_type_init ();
  gtk_signal_init ();
Elliot Lee's avatar
Elliot Lee committed
428
  gtk_rc_init ();
429 430
  
  
Elliot Lee's avatar
Elliot Lee committed
431 432
  /* Register an exit function to make sure we are able to cleanup.
   */
Tim Janik's avatar
Tim Janik committed
433
  g_atexit (gtk_exit_func);
434
  
Elliot Lee's avatar
Elliot Lee committed
435 436
  /* Set the 'initialized' flag.
   */
437
  gtk_initialized = TRUE;
438

439
  /* initialize gtk modules
440
   */
441
  for (slist = gtk_modules; slist; slist = slist->next)
442
    {
443
      if (slist->data)
444 445
	{
	  GtkModuleInitFunc modinit;
446
	  
447 448 449 450
	  modinit = slist->data;
	  modinit (argc, argv);
	}
    }
451
  g_slist_free (gtk_modules);
452
  
Manish Singh's avatar
Manish Singh committed
453
#ifndef G_OS_WIN32
454
  /* No use warning on Win32, there aren't any non-devel versions anyhow... */
455
  g_message (""              "YOU ARE USING THE DEVEL BRANCH 1.3.x OF GTK+ WHICH IS CURRENTLY\n"
456 457 458 459 460
	     "                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+");
461 462
#endif

463 464
  return TRUE;
}
465 466 467

#undef gtk_init

468 469 470 471 472 473 474 475
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
476 477
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
#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
510
void
Tor Lillqvist's avatar
Tor Lillqvist committed
511
gtk_exit (gint errorcode)
Elliot Lee's avatar
Elliot Lee committed
512 513 514
{
  /* Only if "gtk" has been initialized should we de-initialize.
   */
515 516 517
  /* de-initialisation is done by the gtk_exit_funct(),
   * no need to do this here (Alex J.)
   */
Elliot Lee's avatar
Elliot Lee committed
518 519 520 521
  gdk_exit(errorcode);
}

gchar*
522
gtk_set_locale (void)
Elliot Lee's avatar
Elliot Lee committed
523 524 525 526
{
  return gdk_set_locale ();
}

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
gchar*
gtk_get_default_language (void)
{
  gchar *lang;
  gchar *p;
  
  lang = g_strdup (setlocale (LC_CTYPE, NULL));
  p = strchr (lang, '.');
  if (p)
    *p = '\0';
  p = strchr (lang, '@');
  if (p)
    *p = '\0';

  return lang;
}

Elliot Lee's avatar
Elliot Lee committed
544
void
545
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
546 547 548 549
{
  GList *tmp_list;
  GList *functions;
  GtkInitFunction *init;
550 551
  GMainLoop *loop;

Tim Janik's avatar
Tim Janik committed
552
  gtk_main_loop_level++;
553
  
554 555 556
  loop = g_main_new (TRUE);
  main_loops = g_slist_prepend (main_loops, loop);

Elliot Lee's avatar
Elliot Lee committed
557 558
  tmp_list = functions = init_functions;
  init_functions = NULL;
559
  
Elliot Lee's avatar
Elliot Lee committed
560 561 562 563
  while (tmp_list)
    {
      init = tmp_list->data;
      tmp_list = tmp_list->next;
564
      
Elliot Lee's avatar
Elliot Lee committed
565 566 567 568
      (* init->function) (init->data);
      g_free (init);
    }
  g_list_free (functions);
569

570 571 572 573 574 575 576
  if (g_main_is_running (main_loops->data))
    {
      GDK_THREADS_LEAVE ();
      g_main_run (loop);
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
577 578 579 580 581 582 583 584 585 586

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

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

587
	  tmp_list = quit_functions;
588
	  quit_functions = g_list_remove_link (quit_functions, quit_functions);
589
	  g_list_free_1 (tmp_list);
590

Tim Janik's avatar
Tim Janik committed
591
	  if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
592
	      gtk_quit_invoke_function (quitf))
593
	    {
594
	      reinvoke_list = g_list_prepend (reinvoke_list, quitf);
595 596 597
	    }
	  else
	    {
598
	      gtk_quit_destroy (quitf);
599 600 601 602
	    }
	}
      if (reinvoke_list)
	{
Tim Janik's avatar
Tim Janik committed
603
	  GList *work;
604
	  
Tim Janik's avatar
Tim Janik committed
605
	  work = g_list_last (reinvoke_list);
606
	  if (quit_functions)
Tim Janik's avatar
Tim Janik committed
607 608 609
	    quit_functions->prev = work;
	  work->next = quit_functions;
	  quit_functions = work;
610
	}
611 612

      gdk_flush ();
613 614
    }
	      
615 616 617 618
  main_loops = g_slist_remove (main_loops, loop);

  g_main_destroy (loop);

Tim Janik's avatar
Tim Janik committed
619
  gtk_main_loop_level--;
620 621 622 623 624
}

guint
gtk_main_level (void)
{
Tim Janik's avatar
Tim Janik committed
625
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
626 627 628
}

void
629
gtk_main_quit (void)
Elliot Lee's avatar
Elliot Lee committed
630
{
631 632
  g_return_if_fail (main_loops != NULL);

633
  g_main_quit (main_loops->data);
Elliot Lee's avatar
Elliot Lee committed
634 635
}

Owen Taylor's avatar
Owen Taylor committed
636 637 638
gint
gtk_events_pending (void)
{
639 640 641 642 643 644 645
  gboolean result;
  
  GDK_THREADS_LEAVE ();  
  result = g_main_pending();
  GDK_THREADS_ENTER ();

  return result;
Owen Taylor's avatar
Owen Taylor committed
646 647 648
}

gint 
649
gtk_main_iteration (void)
650
{
651
  GDK_THREADS_LEAVE ();
652
  g_main_iteration (TRUE);
653
  GDK_THREADS_ENTER ();
654

655 656
  if (main_loops)
    return !g_main_is_running (main_loops->data);
657 658
  else
    return TRUE;
659 660
}

661
gint 
662
gtk_main_iteration_do (gboolean blocking)
663
{
664
  GDK_THREADS_LEAVE ();
665
  g_main_iteration (blocking);
666
  GDK_THREADS_ENTER ();
667

668 669
  if (main_loops)
    return !g_main_is_running (main_loops->data);
670 671
  else
    return TRUE;
672 673
}

Owen Taylor's avatar
Owen Taylor committed
674
void 
675
gtk_main_do_event (GdkEvent *event)
Elliot Lee's avatar
Elliot Lee committed
676 677 678
{
  GtkWidget *event_widget;
  GtkWidget *grab_widget;
679
  GdkEvent *next_event;
680
  GList *tmp_list;
681 682 683

  /* If there are any events pending then get the next one.
   */
684
  next_event = gdk_event_peek ();
685
  
686 687 688 689 690 691 692
  /* 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.
693
   */
694 695 696 697 698 699 700 701
  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))
      {
702 703 704 705
	/* Throw both the peeked copy and the queued copy away 
	 */
	gdk_event_free (next_event);
	next_event = gdk_event_get ();
706 707 708 709
	gdk_event_free (next_event);
	
	return;
      }
710

711
  if (next_event)
712
    gdk_event_free (next_event);
713 714 715 716 717 718 719 720 721 722

  /* 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
723
    {
724 725 726 727 728 729 730 731 732 733 734
      /* 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);
      
      return;
Elliot Lee's avatar
Elliot Lee committed
735
    }
736
  
737 738
  /* Push the event onto a stack of current events for
   * gtk_current_event_get().
Elliot Lee's avatar
Elliot Lee committed
739
   */
740
  current_events = g_list_prepend (current_events, event);
741
  
742
  /* If there is a grab in effect...
Elliot Lee's avatar
Elliot Lee committed
743
   */
744
  if (grabs)
Elliot Lee's avatar
Elliot Lee committed
745
    {
746
      grab_widget = grabs->data;
747
      
748 749 750 751 752 753 754 755 756 757 758
      /* 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
759
    }
760

761 762 763 764 765 766 767 768 769
  /* 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
770
   */
771
  switch (event->type)
Elliot Lee's avatar
Elliot Lee committed
772
    {
773 774
    case GDK_NOTHING:
      break;
775
      
776 777
    case GDK_DELETE:
      gtk_widget_ref (event_widget);
Owen Taylor's avatar
Owen Taylor committed
778 779
      if ((!grabs || gtk_widget_get_toplevel (grabs->data) == event_widget) &&
	  !gtk_widget_event (event_widget, event))
780 781 782
	gtk_widget_destroy (event_widget);
      gtk_widget_unref (event_widget);
      break;
783
      
784
    case GDK_DESTROY:
785 786 787 788 789 790 791 792 793 794 795
      /* 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) &&
	      !GTK_OBJECT_DESTROYED (event_widget))
	    gtk_widget_destroy (event_widget);
	  gtk_widget_unref (event_widget);
	}
796
      break;
797
      
798
    case GDK_EXPOSE:
799
      if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
800 801 802 803
	gdk_window_begin_paint_rect (event->any.window, &event->expose.area);

      gtk_widget_event (event_widget, event);

804
      if (event->any.window && GTK_WIDGET_DOUBLE_BUFFERED (event_widget))
805 806 807 808
	gdk_window_end_paint (event->any.window);
      break;

    case GDK_PROPERTY_NOTIFY:
809 810 811 812 813 814 815 816 817 818 819 820
    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:
      gtk_widget_event (event_widget, event);
      break;
821

822
    case GDK_SCROLL:
823 824 825 826 827 828
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
      gtk_propagate_event (grab_widget, event);
      break;

829 830 831
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
      if (key_snoopers)
Elliot Lee's avatar
Elliot Lee committed
832
	{
833 834
	  if (gtk_invoke_key_snoopers (grab_widget, event))
	    break;
Elliot Lee's avatar
Elliot Lee committed
835
	}
836 837 838 839 840 841 842 843 844 845
      /* 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
846
	{
847 848 849
	  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
850
	}
851 852 853 854
      break;
      
    case GDK_LEAVE_NOTIFY:
      if (GTK_WIDGET_LEAVE_PENDING (event_widget))
Elliot Lee's avatar
Elliot Lee committed
855
	{
856
	  GTK_PRIVATE_UNSET_FLAG (event_widget, GTK_LEAVE_PENDING);
857
	  gtk_widget_event (event_widget, event);
Elliot Lee's avatar
Elliot Lee committed
858
	}
859 860 861
      else if (GTK_WIDGET_IS_SENSITIVE (grab_widget))
	gtk_widget_event (grab_widget, event);
      break;
862
      
863 864 865 866 867 868 869 870 871 872
    case GDK_DRAG_STATUS:
    case GDK_DROP_FINISHED:
      gtk_drag_source_handle_event (event_widget, event);
      break;
    case GDK_DRAG_ENTER:
    case GDK_DRAG_LEAVE:
    case GDK_DRAG_MOTION:
    case GDK_DROP_START:
      gtk_drag_dest_handle_event (event_widget, event);
      break;
Elliot Lee's avatar
Elliot Lee committed
873
    }
874
  
875 876 877
  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
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
}

gint
gtk_true (void)
{
  return TRUE;
}

gint
gtk_false (void)
{
  return FALSE;
}

void
gtk_grab_add (GtkWidget *widget)
{
895
  g_return_if_fail (widget != NULL);
896
  
897
  if (!GTK_WIDGET_HAS_GRAB (widget))
898 899
    {
      GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_GRAB);
900
      
901
      grabs = g_slist_prepend (grabs, widget);
902
      gtk_widget_ref (widget);
903
    }
Elliot Lee's avatar
Elliot Lee committed
904 905
}

906 907 908 909 910 911 912 913
GtkWidget*
gtk_grab_get_current (void)
{
  if (grabs)
    return GTK_WIDGET (grabs->data);
  return NULL;
}

Elliot Lee's avatar
Elliot Lee committed
914 915 916
void
gtk_grab_remove (GtkWidget *widget)
{
917
  g_return_if_fail (widget != NULL);
918
  
919 920 921
  if (GTK_WIDGET_HAS_GRAB (widget))
    {
      GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_GRAB);
922
      
923
      grabs = g_slist_remove (grabs, widget);
924
      gtk_widget_unref (widget);
925
    }
Elliot Lee's avatar
Elliot Lee committed
926 927 928 929
}

void
gtk_init_add (GtkFunction function,
930
	      gpointer	  data)
Elliot Lee's avatar
Elliot Lee committed
931 932
{
  GtkInitFunction *init;
933
  
Elliot Lee's avatar
Elliot Lee committed
934 935 936
  init = g_new (GtkInitFunction, 1);
  init->function = function;
  init->data = data;
937
  
Elliot Lee's avatar
Elliot Lee committed
938 939 940
  init_functions = g_list_prepend (init_functions, init);
}

941
guint
942
gtk_key_snooper_install (GtkKeySnoopFunc snooper,
943
			 gpointer	 func_data)
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
{
  GtkKeySnooperData *data;
  static guint snooper_id = 1;

  g_return_val_if_fail (snooper != NULL, 0);

  data = g_new (GtkKeySnooperData, 1);
  data->func = snooper;
  data->func_data = func_data;
  data->id = snooper_id++;
  key_snoopers = g_slist_prepend (key_snoopers, data);

  return data->id;
}

void
960
gtk_key_snooper_remove (guint		 snooper_id)
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
{
  GtkKeySnooperData *data = NULL;
  GSList *slist;

  slist = key_snoopers;
  while (slist)
    {
      data = slist->data;
      if (data->id == snooper_id)
	break;

      slist = slist->next;
      data = NULL;
    }
  if (data)
    key_snoopers = g_slist_remove (key_snoopers, data);
}

static gint
gtk_invoke_key_snoopers (GtkWidget *grab_widget,
			 GdkEvent  *event)
{
  GSList *slist;
  gint return_val = FALSE;

  slist = key_snoopers;
  while (slist && !return_val)
    {
      GtkKeySnooperData *data;

      data = slist->data;
      slist = slist->next;
      return_val = (*data->func) (grab_widget, (GdkEventKey*) event, data->func_data);
    }

  return return_val;
}

999
guint
1000
gtk_quit_add_full (guint		main_level,
1001
		   GtkFunction		function,
1002 1003
		   GtkCallbackMarshal	marshal,
		   gpointer		data,
1004
		   GtkDestroyNotify	destroy)
1005
{
1006
  static guint quit_id = 1;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
  GtkQuitFunction *quitf;
  
  g_return_val_if_fail ((function != NULL) || (marshal != NULL), 0);

  if (!quit_mem_chunk)
    quit_me