gtkmain.c 59.3 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>

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

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

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

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

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

52
#include <pango/pango-types.h>	/* For pango_language_from_string */
53

54
#include "gtkalias.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
55
56
#include "gtkintl.h"

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

/* Private type definitions
 */
74
75
typedef struct _GtkInitFunction		 GtkInitFunction;
typedef struct _GtkQuitFunction		 GtkQuitFunction;
76
typedef struct _GtkClosure	         GtkClosure;
77
typedef struct _GtkKeySnooperData	 GtkKeySnooperData;
Elliot Lee's avatar
Elliot Lee committed
78
79
80
81
82
83
84

struct _GtkInitFunction
{
  GtkFunction function;
  gpointer data;
};

85
86
struct _GtkQuitFunction
{
87
  guint id;
88
89
90
91
92
93
94
  guint main_level;
  GtkCallbackMarshal marshal;
  GtkFunction function;
  gpointer data;
  GtkDestroyNotify destroy;
};

95
struct _GtkClosure
Elliot Lee's avatar
Elliot Lee committed
96
{
97
  GtkCallbackMarshal marshal;
Elliot Lee's avatar
Elliot Lee committed
98
99
100
101
  gpointer data;
  GtkDestroyNotify destroy;
};

102
103
104
105
struct _GtkKeySnooperData
{
  GtkKeySnoopFunc func;
  gpointer func_data;
106
  guint id;
107
108
};

109
110
111
112
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);
113
114
115
116
117
118
119

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
120
#if 0
121
122
123
124
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
125
#endif
126

127
128
static GtkWindowGroup *gtk_main_get_window_group (GtkWidget   *widget);

129
130
131
const guint gtk_major_version = GTK_MAJOR_VERSION;
const guint gtk_minor_version = GTK_MINOR_VERSION;
const guint gtk_micro_version = GTK_MICRO_VERSION;
132
133
const guint gtk_binary_age = GTK_BINARY_AGE;
const guint gtk_interface_age = GTK_INTERFACE_AGE;
134

Tim Janik's avatar
Tim Janik committed
135
static guint gtk_main_loop_level = 0;
136
static gint gtk_initialized = FALSE;
137
static GList *current_events = NULL;
Elliot Lee's avatar
Elliot Lee committed
138

139
140
static GSList *main_loops = NULL;      /* stack of currently executing main loops */

141
static GList *init_functions = NULL;	   /* A list of init functions.
Elliot Lee's avatar
Elliot Lee committed
142
					    */
143
144
145
static GList *quit_functions = NULL;	   /* A list of quit functions.
					    */
static GMemChunk *quit_mem_chunk = NULL;
Elliot Lee's avatar
Elliot Lee committed
146

147
148
static GSList *key_snoopers = NULL;

149
guint gtk_debug_flags = 0;		   /* Global GTK debug flag */
150
151

#ifdef G_ENABLE_DEBUG
152
static const GDebugKey gtk_debug_keys[] = {
153
  {"misc", GTK_DEBUG_MISC},
154
  {"plugsocket", GTK_DEBUG_PLUGSOCKET},
155
  {"text", GTK_DEBUG_TEXT},
Havoc Pennington's avatar
Havoc Pennington committed
156
  {"tree", GTK_DEBUG_TREE},
157
  {"updates", GTK_DEBUG_UPDATES},
158
  {"keybindings", GTK_DEBUG_KEYBINDINGS},
159
  {"multihead", GTK_DEBUG_MULTIHEAD},
160
  {"modules", GTK_DEBUG_MODULES},
161
162
  {"geometry", GTK_DEBUG_GEOMETRY},
  {"icontheme", GTK_DEBUG_ICONTHEME}
163
};
164

165
static const guint gtk_ndebug_keys = sizeof (gtk_debug_keys) / sizeof (GDebugKey);
166

167
#endif /* G_ENABLE_DEBUG */
Elliot Lee's avatar
Elliot Lee committed
168

169
170
171
172
173
174
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
/**
 * 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.
 **/
203
204
205
206
207
gchar*
gtk_check_version (guint required_major,
		   guint required_minor,
		   guint required_micro)
{
208
209
210
  gint gtk_effective_micro = 100 * GTK_MINOR_VERSION + GTK_MICRO_VERSION;
  gint required_effective_micro = 100 * required_minor + required_micro;

211
  if (required_major > GTK_MAJOR_VERSION)
212
    return "Gtk+ version too old (major mismatch)";
213
  if (required_major < GTK_MAJOR_VERSION)
214
    return "Gtk+ version too new (major mismatch)";
215
  if (required_effective_micro < gtk_effective_micro - GTK_BINARY_AGE)
216
    return "Gtk+ version too new (micro mismatch)";
217
  if (required_effective_micro > gtk_effective_micro)
218
    return "Gtk+ version too old (micro mismatch)";
219
220
221
  return NULL;
}

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

Tor Lillqvist's avatar
Tor Lillqvist committed
267
268
269
270
#ifdef G_OS_WIN32

G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)

271
272
273
274
275
276
277
278
279
280
281
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
282
283
284
285
286
287
288
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
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 */

328
329
330
331
332
static gboolean do_setlocale = TRUE;

/**
 * gtk_disable_setlocale:
 * 
Matthias Clasen's avatar
Matthias Clasen committed
333
334
 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
 * gtk_parse_args() from automatically
335
336
337
338
 * 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.
339
340
341
 *
 * Most programs should not need to call this function.
 **/
342
void
343
344
345
346
347
348
349
350
gtk_disable_setlocale (void)
{
  if (gtk_initialized)
    g_warning ("gtk_disable_setlocale() must be called before gtk_init()");
    
  do_setlocale = FALSE;
}

351
#ifdef G_PLATFORM_WIN32
352
#undef gtk_init_check
353
#endif
354

355
static GString *gtk_modules_string = NULL;
356
static gboolean g_fatal_warnings = FALSE;
357
358
359
360

#ifdef G_ENABLE_DEBUG
static gboolean
gtk_arg_debug_cb (const char *key, const char *value, gpointer user_data)
Elliot Lee's avatar
Elliot Lee committed
361
{
362
363
364
  gtk_debug_flags |= g_parse_debug_string (value,
					   gtk_debug_keys,
					   gtk_ndebug_keys);
365

366
367
  return TRUE;
}
Tim Janik's avatar
Tim Janik committed
368

369
370
371
372
373
374
static gboolean
gtk_arg_no_debug_cb (const char *key, const char *value, gpointer user_data)
{
  gtk_debug_flags &= ~g_parse_debug_string (value,
					    gtk_debug_keys,
					    gtk_ndebug_keys);
375
376

  return TRUE;
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
}
#endif /* G_ENABLE_DEBUG */

static gboolean
gtk_arg_module_cb (const char *key, const char *value, gpointer user_data)
{
  if (value && *value)
    {
      if (gtk_modules_string)
	g_string_append_c (gtk_modules_string, G_SEARCHPATH_SEPARATOR);
      else
	gtk_modules_string = g_string_new (NULL);
      
      g_string_append (gtk_modules_string, value);
    }

  return TRUE;
}

static GOptionEntry gtk_args[] = {
397
398
399
  { "gtk-module",       0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_module_cb,   
    /* Description of --gtk-module=MODULES in --help output */ N_("Load additional GTK+ modules"), 
    /* Placeholder in --gtk-module=MODULES in --help output */ N_("MODULES") },
400
  { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, 
401
    /* Description of --g-fatal-warnings in --help output */   N_("Make all warnings fatal"), NULL },
402
#ifdef G_ENABLE_DEBUG
403
404
405
406
407
408
409
  { "gtk-debug",        0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_debug_cb,    
    /* Description of --gtk-debug=FLAGS in --help output */    N_("GTK+ debugging flags to set"), 
    /* Placeholder in --gtk-debug=FLAGS in --help output */    N_("FLAGS") },
  { "gtk-no-debug",     0, 0, G_OPTION_ARG_CALLBACK, gtk_arg_no_debug_cb, 
    /* Description of --gtk-no-debug=FLAGS in --help output */ N_("GTK+ debugging flags to unset"), 
    /* Placeholder in --gtk-no-debug=FLAGS in --help output */ N_("FLAGS") },
#endif 
410
411
412
413
414
415
416
417
  { NULL }
};

static void
do_pre_parse_initialization (int    *argc,
			     char ***argv)
{
  const gchar *env_string;
Owen Taylor's avatar
Owen Taylor committed
418
  
419
420
421
422
423
424
#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
425
426

  if (do_setlocale)
427
428
429
430
    {
      if (!setlocale (LC_ALL, ""))
	g_warning ("Locale not supported by C library.\n\tUsing the fallback 'C' locale.");
    }
431

Matthias Clasen's avatar
Matthias Clasen committed
432
  gdk_pre_parse_libgtk_only ();
Owen Taylor's avatar
Owen Taylor committed
433
  gdk_event_handler_set ((GdkEventFunc)gtk_main_do_event, NULL, NULL);
434
  
435
#ifdef G_ENABLE_DEBUG
Matthias Clasen's avatar
Matthias Clasen committed
436
  env_string = g_getenv ("GTK_DEBUG");
437
438
439
  if (env_string != NULL)
    {
      gtk_debug_flags = g_parse_debug_string (env_string,
440
441
					      gtk_debug_keys,
					      gtk_ndebug_keys);
442
443
      env_string = NULL;
    }
444
#endif	/* G_ENABLE_DEBUG */
445

Matthias Clasen's avatar
Matthias Clasen committed
446
  env_string = g_getenv ("GTK_MODULES");
447
  if (env_string)
448
    gtk_modules_string = g_string_new (env_string);
449
}
450

451
452
453
454
static void
do_post_parse_initialization (int    *argc,
			      char ***argv)
{
455
456
457
458
459
460
461
462
463
  if (g_fatal_warnings)
    {
      GLogLevelFlags fatal_mask;

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

Havoc Pennington's avatar
Havoc Pennington committed
464
465
  if (gtk_debug_flags & GTK_DEBUG_UPDATES)
    gdk_window_set_debug_updates (TRUE);
466

Owen Taylor's avatar
Owen Taylor committed
467
#ifdef ENABLE_NLS
468
  bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
469
  bindtextdomain (GETTEXT_PACKAGE "-properties", GTK_LOCALEDIR);
Robert Brady's avatar
Robert Brady committed
470
#    ifdef HAVE_BIND_TEXTDOMAIN_CODESET
471
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
472
  bind_textdomain_codeset (GETTEXT_PACKAGE "-properties", "UTF-8");
473
#    endif
Owen Taylor's avatar
Owen Taylor committed
474
475
#endif  

476
477
  {
  /* Translate to default:RTL if you want your widgets
Robert Brady's avatar
Robert Brady committed
478
479
480
   * 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 
481
482
483
484
485
486
487
488
489
   */
    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");
    }
  }

490
  gtk_type_init (0);
491
  _gtk_accel_map_init ();  
492
  _gtk_rc_init ();
493

Elliot Lee's avatar
Elliot Lee committed
494
495
  /* Set the 'initialized' flag.
   */
496
  gtk_initialized = TRUE;
497

498
499
  /* load gtk modules */
  if (gtk_modules_string)
500
    {
501
502
      _gtk_modules_init (argc, argv, gtk_modules_string->str);
      g_string_free (gtk_modules_string, TRUE);
503
    }
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}


typedef struct
{
  gboolean open_default_display;
} OptionGroupInfo;

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

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

  
  do_post_parse_initialization (NULL, NULL);
  
  if (info->open_default_display)
    return gdk_display_open_default_libgtk_only () != NULL;
536
  else
537
538
539
540
    return TRUE;
}


Matthias Clasen's avatar
Matthias Clasen committed
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
 * gtk_get_option_group:
 * @open_default_display: whether to open the default display 
 *    when parsing the commandline arguments
 * 
 * Returns a #GOptionGroup for the commandline arguments recognized
 * by GTK+ and GDK. You should add this group to your #GOptionContext 
 * with g_option_context_add_group(), if you are using 
 * g_option_context_parse() to parse your commandline arguments.
 *
 * Returns a #GOptionGroup for the commandline arguments recognized
 *   by GTK+
 *
 * Since: 2.6
 */
556
557
558
559
560
561
562
563
564
565
566
567
568
569
GOptionGroup *
gtk_get_option_group (gboolean open_default_display)
{
  GOptionGroup *group;
  OptionGroupInfo *info;

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

  gdk_add_option_entries_libgtk_only (group);
  g_option_group_add_entries (group, gtk_args);
570
  g_option_group_set_translation_domain (group, GETTEXT_PACKAGE);
571
572
573
574
  
  return group;
}

Matthias Clasen's avatar
Matthias Clasen committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
 * gtk_init_with_args:
 * @argc: a pointer to the number of command line arguments.
 * @argv: a pointer to the array of command line arguments.
 * @parameter_string: a string which is displayed in
 *    the first line of <option>--help</option> output, after 
 *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
 *    describing the options of your program
 * @translation_domain: a translation domain to use for translating
 *    the <option>--help</option> output for the options in @entries
 *    with gettext(), or %NULL
 * @error: a return location for errors 
 *
 * This function does the same work as gtk_init_check(). 
 * Additionally, it allows you to add your own commandline options, 
 * and it automatically generated nicely formatted 
 * <option>--help</option> output. Note that your program will
 * be terminated after writing out the help output.
 *
 * Returns: %TRUE if the GUI has been successfully initialized, 
 *               %FALSE otherwise.
 * 
 * Since: 2.6
 */
600
601
602
603
604
605
606
607
608
609
610
gboolean
gtk_init_with_args (int            *argc,
		    char         ***argv,
		    char           *parameter_string,  
		    GOptionEntry   *entries,
		    char           *translation_domain,
		    GError        **error)
{
  GOptionContext *context;
  GOptionGroup *gtk_group;
  gboolean retval;
Matthias Clasen's avatar
Matthias Clasen committed
611

612
613
614
  if (gtk_initialized)
    return TRUE;

Matthias Clasen's avatar
Matthias Clasen committed
615
616
617
  if (!check_setugid ())
    return FALSE;

618
619
620
621
622
623
624
625
  gtk_group = gtk_get_option_group (TRUE);
  
  context = g_option_context_new (parameter_string);
  g_option_context_add_group (context, gtk_group);
  
  if (entries)
    g_option_context_add_main_entries (context, entries, translation_domain);
  retval = g_option_context_parse (context, argc, argv, error);
Matthias Clasen's avatar
Matthias Clasen committed
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
  g_option_context_free (context);

  return retval;
}


/**
 * gtk_parse_args:
 * @argc: a pointer to the number of command line arguments.
 * @argv: a pointer to the array of command line arguments.
 * 
 * Parses command line arguments, and initializes global
 * attributes of GTK+, but does not actually open a connection
 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
 *
 * Any arguments used by GTK+ or GDK are removed from the array and
 * @argc and @argv are updated accordingly.
 *
 * You shouldn't call this function explicitely if you are using
 * gtk_init(), or gtk_init_check().
 *
 * Return value: %TRUE if initialization succeeded, otherwise %FALSE.
 **/
gboolean
gtk_parse_args (int    *argc,
		char ***argv)
{
  GOptionContext *option_context;
655
  GOptionGroup *gtk_group;
656
657
658
659
660
661
662
663
664
665
  
  if (gtk_initialized)
    return TRUE;

  if (!check_setugid ())
    return FALSE;

  option_context = g_option_context_new (NULL);
  g_option_context_set_ignore_unknown_options (option_context, TRUE);
  g_option_context_set_help_enabled (option_context, FALSE);
666
667
  gtk_group = gtk_get_option_group (FALSE);
  g_option_context_set_main_group (option_context, gtk_group);
668
669
670
  g_option_context_parse (option_context, argc, argv, NULL);
  g_option_context_free (option_context);

671
672
  return TRUE;
}
673

674
#ifdef G_PLATFORM_WIN32
675
#undef gtk_init_check
676
#endif
677
678
679
680

/**
 * gtk_init_check:
 * @argc: Address of the <parameter>argc</parameter> parameter of your 
Matthias Clasen's avatar
Matthias Clasen committed
681
682
683
 *   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.
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
 * 
 * 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;

702
  return gdk_display_open_default_libgtk_only () != NULL;
703
704
}

705
#ifdef G_PLATFORM_WIN32
706
#undef gtk_init
707
#endif
708

709
/**
Matthias Clasen's avatar
Matthias Clasen committed
710
 * gtk_init:
711
 * @argc: Address of the <parameter>argc</parameter> parameter of your 
Matthias Clasen's avatar
Matthias Clasen committed
712
713
714
 *   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.
715
716
 * 
 * Call this function before using any other GTK+ functions in your GUI
Matthias Clasen's avatar
Matthias Clasen committed
717
718
 * 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
719
 * @argv are adjusted accordingly so your own code will 
Matthias Clasen's avatar
Matthias Clasen committed
720
721
722
723
724
725
726
 * never see those standard arguments. 
 *
 * Note that there are some alternative ways to initialize GTK+: 
 * if you are calling gtk_parse_args(), gtk_init_check(), 
 * gtk_init_with_args() or g_option_context_parse() with 
 * the option group returned by gtk_get_option_group(), you 
 * <emphasis>don't</emphasis> have to call gtk_init().
727
728
729
730
731
732
733
 *
 * <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>
 **/
734
735
736
737
738
void
gtk_init (int *argc, char ***argv)
{
  if (!gtk_init_check (argc, argv))
    {
739
      const char *display_name_arg = gdk_get_display_arg_name ();
740
      g_warning ("cannot open display: %s", display_name_arg ? display_name_arg : " ");
741
      exit (1);
742
    }
Elliot Lee's avatar
Elliot Lee committed
743
744
}

745
#ifdef G_PLATFORM_WIN32
746
747
748
749
750
751
752
753
754

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"
755
756
757
	     "your code with gcc without the -fnative-struct\n"
	     "(or -mms-bitfields) switch, or that you are using\n"
	     "an unsupported compiler.");
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
}

/* 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"
774
775
776
	     "your code with gcc without the -fnative-struct\n"
	     "(or -mms-bitfields) switch, or that you are using\n"
	     "an unsupported compiler.");
777
778
779
780
781
782
}

/* These two functions might get more checks added later, thus pass
 * in the number of extra args.
 */
void
783
gtk_init_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
784
785
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
786
787
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
788
789
790
791
  gtk_init (argc, argv);
}

gboolean
792
gtk_init_check_abi_check (int *argc, char ***argv, int num_checks, size_t sizeof_GtkWindow, size_t sizeof_GtkBox)
793
794
{
  check_sizeof_GtkWindow (sizeof_GtkWindow);
795
796
  if (num_checks >= 2)
    check_sizeof_GtkBox (sizeof_GtkBox);
797
798
799
800
801
  return gtk_init_check (argc, argv);
}

#endif

Elliot Lee's avatar
Elliot Lee committed
802
void
Tor Lillqvist's avatar
Tor Lillqvist committed
803
gtk_exit (gint errorcode)
Elliot Lee's avatar
Elliot Lee committed
804
{
Manish Singh's avatar
Manish Singh committed
805
  exit (errorcode);
Elliot Lee's avatar
Elliot Lee committed
806
807
}

808
809
810
811

/**
 * gtk_set_locale:
 *
812
813
814
 * Initializes internationalization support for GTK+. gtk_init()
 * automatically does this, so there is typically no point
 * in calling this function.
815
 *
816
817
818
819
820
 * 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.)
821
 * 
822
 * In detail - sets the current locale according to the
823
824
825
 * 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.
826
 * 
Matthias Clasen's avatar
Matthias Clasen committed
827
 * Returns: a string corresponding to the locale set, typically in the
828
829
 * 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
830
831
832
 * 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.
833
 **/
Sven Neumann's avatar
Sven Neumann committed
834
gchar *
835
gtk_set_locale (void)
Elliot Lee's avatar
Elliot Lee committed
836
837
838
839
{
  return gdk_set_locale ();
}

840
/**
841
 * _gtk_get_lc_ctype:
842
 *
843
844
845
846
847
848
849
850
851
 * 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.
852
 * 
853
854
 * 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
855
856
857
858
859
 * 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
860
861
862
863
864
865
866
867
 * 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)
868
{
869
870
871
872
873
#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
874
875
   * locale-dependent C library functions like ctime(). But just for
   * kicks, do obey LC_ALL, LC_CTYPE and LANG in GTK. (This also makes
876
877
878
879
   * 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
880
881
  gchar *p;

882
883
  p = getenv ("LC_ALL");
  if (p != NULL)
884
885
886
887
888
889
890
891
892
893
894
    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 ();
895
#else
896
  return g_strdup (setlocale (LC_CTYPE, NULL));
897
#endif
898
899
900
901
902
903
904
905
906
907
}

/**
 * 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
908
 * _gtk_get_lc_ctype() for notes on behaviour on Windows.
909
910
911
912
913
914
915
916
917
918
919
920
 * 
 * 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 ();
921
922
923
924
925
926
927
  p = strchr (lang, '.');
  if (p)
    *p = '\0';
  p = strchr (lang, '@');
  if (p)
    *p = '\0';

928
929
930
  result = pango_language_from_string (lang);
  g_free (lang);
  
Hans Breuer's avatar
Hans Breuer committed
931
  return result;
932
933
}

Elliot Lee's avatar
Elliot Lee committed
934
void
935
gtk_main (void)
Elliot Lee's avatar
Elliot Lee committed
936
937
938
939
{
  GList *tmp_list;
  GList *functions;
  GtkInitFunction *init;
940
941
  GMainLoop *loop;

Tim Janik's avatar
Tim Janik committed
942
  gtk_main_loop_level++;
943
  
Manish Singh's avatar
Manish Singh committed
944
  loop = g_main_loop_new (NULL, TRUE);
945
946
  main_loops = g_slist_prepend (main_loops, loop);

Elliot Lee's avatar
Elliot Lee committed
947
948
  tmp_list = functions = init_functions;
  init_functions = NULL;
949
  
Elliot Lee's avatar
Elliot Lee committed
950
951
952
953
  while (tmp_list)
    {
      init = tmp_list->data;
      tmp_list = tmp_list->next;
954
      
Elliot Lee's avatar
Elliot Lee committed
955
956
957
958
      (* init->function) (init->data);
      g_free (init);
    }
  g_list_free (functions);
959

Manish Singh's avatar
Manish Singh committed
960
  if (g_main_loop_is_running (main_loops->data))
961
962
    {
      GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
963
      g_main_loop_run (loop);
964
965
966
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
967
968
969
970
971
972
973
974
975
976

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

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

977
	  tmp_list = quit_functions;
978
	  quit_functions = g_list_remove_link (quit_functions, quit_functions);
979
	  g_list_free_1 (tmp_list);
980

Tim Janik's avatar
Tim Janik committed
981
	  if ((quitf->main_level && quitf->main_level != gtk_main_loop_level) ||
982
	      gtk_quit_invoke_function (quitf))
983
	    {
984
	      reinvoke_list = g_list_prepend (reinvoke_list, quitf);
985
986
987
	    }
	  else
	    {
988
	      gtk_quit_destroy (quitf);
989
990
991
992
	    }
	}
      if (reinvoke_list)
	{
Tim Janik's avatar
Tim Janik committed
993
	  GList *work;
994
	  
Tim Janik's avatar
Tim Janik committed
995
	  work = g_list_last (reinvoke_list);
996
	  if (quit_functions)
Tim Janik's avatar
Tim Janik committed
997
998
999
	    quit_functions->prev = work;
	  work->next = quit_functions;
	  quit_functions = work;
1000
	}
1001
1002

      gdk_flush ();
1003
    }
1004
    
1005
1006
  main_loops = g_slist_remove (main_loops, loop);

Manish Singh's avatar
Manish Singh committed
1007
  g_main_loop_unref (loop);
1008

Tim Janik's avatar
Tim Janik committed
1009
  gtk_main_loop_level--;
1010
1011
1012
1013

  /* Try storing all clipboard data we have */
  if (gtk_main_loop_level == 0)
    _gtk_clipboard_store_all ();
1014
1015
1016
1017
1018
}

guint
gtk_main_level (void)
{
Tim Janik's avatar
Tim Janik committed
1019
  return gtk_main_loop_level;
Elliot Lee's avatar
Elliot Lee committed
1020
1021
1022
}

void
1023
gtk_main_quit (void)
Elliot Lee's avatar
Elliot Lee committed
1024
{
1025
1026
  g_return_if_fail (main_loops != NULL);

Manish Singh's avatar
Manish Singh committed
1027
  g_main_loop_quit (main_loops->data);
Elliot Lee's avatar
Elliot Lee committed
1028
1029
}

1030
gboolean
Owen Taylor's avatar
Owen Taylor committed
1031
1032
gtk_events_pending (void)
{
1033
1034
1035
  gboolean result;
  
  GDK_THREADS_LEAVE ();  
Manish Singh's avatar
Manish Singh committed
1036
  result = g_main_context_pending (NULL);
1037
1038
1039
  GDK_THREADS_ENTER ();

  return result;
Owen Taylor's avatar
Owen Taylor committed
1040
1041
}

1042
gboolean
1043
gtk_main_iteration (void)
1044
{
1045
  GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1046
  g_main_context_iteration (NULL, TRUE);
1047
  GDK_THREADS_ENTER ();
1048

1049
  if (main_loops)
Manish Singh's avatar
Manish Singh committed
1050
    return !g_main_loop_is_running (main_loops->data);
1051
1052
  else
    return TRUE;
1053
1054
}

1055
gboolean
1056
gtk_main_iteration_do (gboolean blocking)
1057
{
1058
  GDK_THREADS_LEAVE ();
Manish Singh's avatar
Manish Singh committed
1059
  g_main_context_iteration (NULL, blocking);
1060
  GDK_THREADS_ENTER ();
1061

1062
  if (main_loops)
Manish Singh's avatar
Manish Singh committed
1063
    return !g_main_loop_is_running (main_loops->data);
1064
1065
  else
    return TRUE;
1066
1067
}

1068
1069
/* private libgtk to libgdk interfaces
 */
1070
1071
gboolean gdk_pointer_grab_info_libgtk_only  (GdkDisplay *display,
					     GdkWindow **grab_window,
1072
					     gboolean   *owner_events);
1073
1074
gboolean gdk_keyboard_grab_info_libgtk_only (GdkDisplay *display,
					     GdkWindow **grab_window,
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
					     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);

1089
1090
  *x += old_origin_x - new_origin_x;
  *y += old_origin_y - new_origin_y;
1091
1092
}

1093
static GdkEvent *
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
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);
      break;
    case GDK_MOTION_NOTIFY:
      rewrite_events_translate (event->any.window,
				new_window,
				&event->motion.x, &event->motion.y);
      break;
    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
    case GDK_PROXIMITY_IN:
    case GDK_PROXIMITY_OUT:
      break;

    default:
      return event;
    }

  g_object_unref (event->any.window);
  event->any.window = g_object_ref (new_window);

  return event;
}

/* If there is a pointer or keyboard grab in effect with owner_events = TRUE,
 * then what X11 does is deliver the event normally if it was going to this
 * client, otherwise, delivers it in terms of the grab window. This function
 * rewrites events to the effect that events going to the same window group
 * are delivered normally, otherwise, the event is delivered in terms of the
 * grab window.
 */
static GdkEvent *
rewrite_event_for_grabs (GdkEvent *event)
{
  GdkWindow *grab_window;
1146
  GtkWidget *event_widget, *grab_widget;
1147
  gboolean owner_events;
1148
  GdkDisplay *display;
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159

  switch (event->type)
    {
    case GDK_SCROLL:
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
    case GDK_MOTION_NOTIFY:
    case GDK_PROXIMITY_IN:
    case GDK_PROXIMITY_OUT:
1160
1161
      display = gdk_drawable_get_display (event->proximity.window);
      if (!gdk_pointer_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1162
1163
1164
1165
1166
1167
	  !owner_events)
	return NULL;
      break;

    case GDK_KEY_PRESS:
    case GDK_KEY_RELEASE:
1168
1169
      display = gdk_drawable_get_display (event->key.window);
      if (!gdk_keyboard_grab_info_libgtk_only (display, &grab_window, &owner_events) ||
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
	  !owner_events)
	return NULL;
      break;

    default:
      return NULL;
    }

  event_widget = gtk_get_event_widget (event);
  gdk_window_get_user_data (grab_window, (void**) &grab_widget);

  if (grab_widget &&
      gtk_main_get_window_group (grab_widget) != gtk_main_get_window_group (event_widget))
    return rewrite_event_for_window (event, grab_window);
  else
    return NULL;
}

Owen Taylor's avatar
Owen Taylor committed
1188
void 
1189
gtk_main_do_event (GdkEvent *event)
Elliot Lee's avatar
Elliot Lee committed
1190
1191
1192
{
  GtkWidget *event_widget;
  GtkWidget *grab_widget;
1193
  GtkWindowGroup *window_group;
1194
  GdkEvent *next_event;
1195
  GdkEvent *rewritten_event = NULL;
1196
  GList *tmp_list;
1197
1198
1199

  /* If there are any events pending then get the next one.
   */
1200
  next_event = gdk_event_peek ();
1201
  
1202
1203
1204
1205
1206
1207
1208
  /* 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.
1209
   */
1210
1211
1212
1213
1214
1215
1216
1217
  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))
      {
1218
1219
1220
1221
	/* Throw both the peeked copy and the queued copy away 
	 */
	gdk_event_free (next_event);
	next_event = gdk_event_get ();
1222
1223
1224
1225
	gdk_event_free (next_event);
	
	return;
      }
1226

1227
  if (next_event)
1228
    gdk_event_free (next_event);
1229

1230
1231
1232
1233
1234
1235
  if (event->type == GDK_SETTING)
    {
      _gtk_settings_handle_event (&event->setting);
      return;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1236
1237
1238
1239
1240
1241
  if (event->type == GDK_OWNER_CHANGE)
    {
      _gtk_clipboard_handle_event (&event->owner_change);
      return;
    }

1242
1243
1244
1245
1246
1247
1248
1249
1250
  /* 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
1251
    {
1252
1253
1254
1255
1256
1257
1258
      /* 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)
1259
1260
	_gtk_selection_incr_event (event->any.window,
				   &event->property);
1261

1262
      return;
Elliot Lee's avatar
Elliot Lee committed
1263
    }
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273

  /* If pointer or keyboard grabs are in effect, munge the events
   * so that each window group looks like a separate app.
   */
  rewritten_event = rewrite_event_for_grabs (event);
  if (rewritten_event)
    {
      event = rewritten_event;
      event_widget = gtk_get_event_widget (event);
    }
1274
  
1275
1276
  window_group = gtk_main_get_window_group (event_widget);

1277
1278
  /* Push the event onto a stack of current events for
   * gtk_current_event_get().
Elliot Lee's avatar
Elliot Lee committed
1279
   */
1280
  current_events = g_list_prepend (current_events, event);
1281

1282
  /* If there is a grab in effect...
Elliot Lee's avatar
Elliot Lee committed
1283
   */
1284
  if (window_group->grabs)
Elliot Lee's avatar
Elliot Lee committed
1285
    {
1286
      grab_widget = window_group->grabs->data;