gtkwindow.c 60.6 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * 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
 * 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27 28 29 30
#include <string.h>
#include <limits.h>
#include "gdk/gdk.h"
#include "gdk/gdkkeysyms.h"
31 32 33 34 35

#if defined (GDK_WINDOWING_X11)
#include "x11/gdkx.h"
#elif defined (GDK_WINDOWING_WIN32)
#include "win32/gdkwin32.h"
36 37
#elif defined (GDK_WINDOWING_NANOX)
#include "nanox/gdkprivate-nanox.h"
Elliot Lee's avatar
Elliot Lee committed
38 39
#elif defined (GDK_WINDOWING_FB)
#include "linux-fb/gdkfb.h"
40 41
#endif

42
#include "gtkprivate.h"
43
#include "gtkrc.h"
Elliot Lee's avatar
Elliot Lee committed
44 45
#include "gtksignal.h"
#include "gtkwindow.h"
Tim Janik's avatar
Tim Janik committed
46
#include "gtkbindings.h"
47
#include "gtkmain.h"
Elliot Lee's avatar
Elliot Lee committed
48

49 50 51 52 53
/* TODO: remove this define and assorted code in 1.3 and fix up the
 * real culprits.
 */
#define FIXME_ZVT_ME_HARDER

Elliot Lee's avatar
Elliot Lee committed
54
enum {
55
  SET_FOCUS,
Elliot Lee's avatar
Elliot Lee committed
56 57
  LAST_SIGNAL
};
58 59 60 61 62 63
enum {
  ARG_0,
  ARG_TYPE,
  ARG_TITLE,
  ARG_AUTO_SHRINK,
  ARG_ALLOW_SHRINK,
64
  ARG_ALLOW_GROW,
65
  ARG_MODAL,
66 67 68
  ARG_WIN_POS,
  ARG_DEFAULT_WIDTH,
  ARG_DEFAULT_HEIGHT
69
};
Elliot Lee's avatar
Elliot Lee committed
70

71
typedef struct {
72 73
  GdkGeometry    geometry; /* Last set of geometry hints we set */
  GdkWindowHints flags;
74 75
  gint           width;
  gint           height;
76 77 78 79 80 81 82 83 84 85 86 87
} GtkWindowLastGeometryInfo;

typedef struct {
  /* Properties that the app has set on the window
   */
  GdkGeometry    geometry;	/* Geometry hints */
  GdkWindowHints mask;
  GtkWidget     *widget;	/* subwidget to which hints apply */
  gint           width;		/* Default size */
  gint           height;

  GtkWindowLastGeometryInfo last;
88 89
} GtkWindowGeometryInfo;

Elliot Lee's avatar
Elliot Lee committed
90 91
static void gtk_window_class_init         (GtkWindowClass    *klass);
static void gtk_window_init               (GtkWindow         *window);
92
static void gtk_window_set_arg            (GtkObject         *object,
93 94
					   GtkArg            *arg,
					   guint	      arg_id);
95
static void gtk_window_get_arg            (GtkObject         *object,
96 97
					   GtkArg            *arg,
					   guint	      arg_id);
98
static void gtk_window_shutdown           (GObject           *object);
99
static void gtk_window_destroy            (GtkObject         *object);
100
static void gtk_window_finalize           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
static void gtk_window_show               (GtkWidget         *widget);
static void gtk_window_hide               (GtkWidget         *widget);
static void gtk_window_map                (GtkWidget         *widget);
static void gtk_window_unmap              (GtkWidget         *widget);
static void gtk_window_realize            (GtkWidget         *widget);
static void gtk_window_size_request       (GtkWidget         *widget,
					   GtkRequisition    *requisition);
static void gtk_window_size_allocate      (GtkWidget         *widget,
					   GtkAllocation     *allocation);
static gint gtk_window_configure_event    (GtkWidget         *widget,
					   GdkEventConfigure *event);
static gint gtk_window_key_press_event    (GtkWidget         *widget,
					   GdkEventKey       *event);
static gint gtk_window_key_release_event  (GtkWidget         *widget,
					   GdkEventKey       *event);
static gint gtk_window_enter_notify_event (GtkWidget         *widget,
					   GdkEventCrossing  *event);
static gint gtk_window_leave_notify_event (GtkWidget         *widget,
					   GdkEventCrossing  *event);
static gint gtk_window_focus_in_event     (GtkWidget         *widget,
					   GdkEventFocus     *event);
static gint gtk_window_focus_out_event    (GtkWidget         *widget,
					   GdkEventFocus     *event);
static gint gtk_window_client_event	  (GtkWidget	     *widget,
125
					   GdkEventClient    *event);
126
static void gtk_window_check_resize       (GtkContainer      *container);
127
static void gtk_window_real_set_focus     (GtkWindow         *window,
128
					   GtkWidget         *focus);
129

130
static void gtk_window_move_resize        (GtkWindow         *window);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static gboolean gtk_window_compare_hints  (GdkGeometry       *geometry_a,
					   guint              flags_a,
					   GdkGeometry       *geometry_b,
					   guint              flags_b);
static void gtk_window_compute_default_size (GtkWindow       *window,
					     guint           *width,
					     guint           *height);
static void  gtk_window_constrain_size      (GtkWindow       *window,
					     GdkGeometry     *geometry,
					     guint            flags,
					     gint             width,
					     gint             height,
					     gint            *new_width,
					     gint            *new_height);
static void gtk_window_compute_hints      (GtkWindow         *window, 
					   GdkGeometry       *new_geometry,
					   guint             *new_flags);
static void gtk_window_compute_reposition (GtkWindow         *window,
					   gint               new_width,
					   gint               new_height,
					   gint              *x,
					   gint              *y);
Elliot Lee's avatar
Elliot Lee committed
153

154 155
static void gtk_window_read_rcfiles       (GtkWidget         *widget,
					   GdkEventClient    *event);
Owen Taylor's avatar
Owen Taylor committed
156 157
static void gtk_window_draw               (GtkWidget         *widget,
				           GdkRectangle      *area);
158 159
static void gtk_window_paint              (GtkWidget         *widget,
					   GdkRectangle      *area);
Owen Taylor's avatar
Owen Taylor committed
160 161
static gint gtk_window_expose             (GtkWidget         *widget,
				           GdkEventExpose    *event);
162 163 164 165 166 167 168 169 170
static void gtk_window_unset_transient_for         (GtkWindow  *window);
static void gtk_window_transient_parent_realized   (GtkWidget  *parent,
						    GtkWidget  *window);
static void gtk_window_transient_parent_unrealized (GtkWidget  *parent,
						    GtkWidget  *window);

static GtkWindowGeometryInfo* gtk_window_get_geometry_info (GtkWindow *window,
							    gboolean   create);
static void gtk_window_geometry_destroy  (GtkWindowGeometryInfo *info);
Elliot Lee's avatar
Elliot Lee committed
171

172
static GSList      *toplevel_list = NULL;
Elliot Lee's avatar
Elliot Lee committed
173
static GtkBinClass *parent_class = NULL;
174
static guint        window_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
175 176


177
GtkType
178
gtk_window_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
179
{
180
  static GtkType window_type = 0;
Elliot Lee's avatar
Elliot Lee committed
181 182 183

  if (!window_type)
    {
184
      static const GtkTypeInfo window_info =
Elliot Lee's avatar
Elliot Lee committed
185 186 187 188 189 190
      {
	"GtkWindow",
	sizeof (GtkWindow),
	sizeof (GtkWindowClass),
	(GtkClassInitFunc) gtk_window_class_init,
	(GtkObjectInitFunc) gtk_window_init,
191 192
        /* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
193
	(GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
194 195 196 197 198 199 200 201 202 203 204
      };

      window_type = gtk_type_unique (gtk_bin_get_type (), &window_info);
    }

  return window_type;
}

static void
gtk_window_class_init (GtkWindowClass *klass)
{
205
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
206 207 208 209 210 211 212 213 214 215
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  object_class = (GtkObjectClass*) klass;
  widget_class = (GtkWidgetClass*) klass;
  container_class = (GtkContainerClass*) klass;

  parent_class = gtk_type_class (gtk_bin_get_type ());

216 217 218
  gobject_class->shutdown = gtk_window_shutdown;
  gobject_class->finalize = gtk_window_finalize;

219 220 221 222 223
  gtk_object_add_arg_type ("GtkWindow::type", GTK_TYPE_WINDOW_TYPE, GTK_ARG_READWRITE, ARG_TYPE);
  gtk_object_add_arg_type ("GtkWindow::title", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_TITLE);
  gtk_object_add_arg_type ("GtkWindow::auto_shrink", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_AUTO_SHRINK);
  gtk_object_add_arg_type ("GtkWindow::allow_shrink", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_ALLOW_SHRINK);
  gtk_object_add_arg_type ("GtkWindow::allow_grow", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_ALLOW_GROW);
224
  gtk_object_add_arg_type ("GtkWindow::modal", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_MODAL);
225
  gtk_object_add_arg_type ("GtkWindow::window_position", GTK_TYPE_WINDOW_POSITION, GTK_ARG_READWRITE, ARG_WIN_POS);
226 227 228
  gtk_object_add_arg_type ("GtkWindow::default_width", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_DEFAULT_WIDTH);
  gtk_object_add_arg_type ("GtkWindow::default_height", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_DEFAULT_HEIGHT);
  
229 230 231
  window_signals[SET_FOCUS] =
    gtk_signal_new ("set_focus",
                    GTK_RUN_LAST,
232
                    GTK_CLASS_TYPE (object_class),
233
                    GTK_SIGNAL_OFFSET (GtkWindowClass, set_focus),
234
                    gtk_marshal_NONE__POINTER,
235
		    GTK_TYPE_NONE, 1,
236
                    GTK_TYPE_WIDGET);
237

Elliot Lee's avatar
Elliot Lee committed
238 239
  gtk_object_class_add_signals (object_class, window_signals, LAST_SIGNAL);

240 241
  object_class->set_arg = gtk_window_set_arg;
  object_class->get_arg = gtk_window_get_arg;
242
  object_class->destroy = gtk_window_destroy;
Elliot Lee's avatar
Elliot Lee committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

  widget_class->show = gtk_window_show;
  widget_class->hide = gtk_window_hide;
  widget_class->map = gtk_window_map;
  widget_class->unmap = gtk_window_unmap;
  widget_class->realize = gtk_window_realize;
  widget_class->size_request = gtk_window_size_request;
  widget_class->size_allocate = gtk_window_size_allocate;
  widget_class->configure_event = gtk_window_configure_event;
  widget_class->key_press_event = gtk_window_key_press_event;
  widget_class->key_release_event = gtk_window_key_release_event;
  widget_class->enter_notify_event = gtk_window_enter_notify_event;
  widget_class->leave_notify_event = gtk_window_leave_notify_event;
  widget_class->focus_in_event = gtk_window_focus_in_event;
  widget_class->focus_out_event = gtk_window_focus_out_event;
  widget_class->client_event = gtk_window_client_event;

260 261 262
  widget_class->draw = gtk_window_draw;
  widget_class->expose_event = gtk_window_expose;
   
263
  container_class->check_resize = gtk_window_check_resize;
Elliot Lee's avatar
Elliot Lee committed
264

265
  klass->set_focus = gtk_window_real_set_focus;
Elliot Lee's avatar
Elliot Lee committed
266 267 268 269 270 271
}

static void
gtk_window_init (GtkWindow *window)
{
  GTK_WIDGET_UNSET_FLAGS (window, GTK_NO_WINDOW);
272
  GTK_WIDGET_SET_FLAGS (window, GTK_TOPLEVEL);
Elliot Lee's avatar
Elliot Lee committed
273

274 275
  gtk_container_set_resize_mode (GTK_CONTAINER (window), GTK_RESIZE_QUEUE);

Elliot Lee's avatar
Elliot Lee committed
276
  window->title = NULL;
277
  window->wmclass_name = g_strdup (g_get_prgname ());
278
  window->wmclass_class = g_strdup (gdk_progclass);
Elliot Lee's avatar
Elliot Lee committed
279 280 281 282 283 284 285 286 287 288
  window->type = GTK_WINDOW_TOPLEVEL;
  window->focus_widget = NULL;
  window->default_widget = NULL;
  window->resize_count = 0;
  window->allow_shrink = FALSE;
  window->allow_grow = TRUE;
  window->auto_shrink = FALSE;
  window->handling_resize = FALSE;
  window->position = GTK_WIN_POS_NONE;
  window->use_uposition = TRUE;
289
  window->modal = FALSE;
290 291 292 293 294

  gtk_widget_ref (GTK_WIDGET (window));
  gtk_object_sink (GTK_OBJECT (window));
  window->has_user_ref_count = TRUE;
  toplevel_list = g_slist_prepend (toplevel_list, window);
Elliot Lee's avatar
Elliot Lee committed
295 296 297
}

static void
298
gtk_window_set_arg (GtkObject  *object,
299 300
		    GtkArg     *arg,
		    guint	arg_id)
Elliot Lee's avatar
Elliot Lee committed
301
{
302 303 304 305
  GtkWindow  *window;

  window = GTK_WINDOW (object);

306
  switch (arg_id)
Elliot Lee's avatar
Elliot Lee committed
307
    {
308
    case ARG_TYPE:
309
      window->type = GTK_VALUE_ENUM (*arg);
310 311
      break;
    case ARG_TITLE:
312
      gtk_window_set_title (window, GTK_VALUE_STRING (*arg));
313 314
      break;
    case ARG_AUTO_SHRINK:
315
      window->auto_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
316
      gtk_widget_queue_resize (GTK_WIDGET (window));
317 318
      break;
    case ARG_ALLOW_SHRINK:
319
      window->allow_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
320
      gtk_widget_queue_resize (GTK_WIDGET (window));
321 322
      break;
    case ARG_ALLOW_GROW:
323
      window->allow_grow = (GTK_VALUE_BOOL (*arg) != FALSE);
324
      gtk_widget_queue_resize (GTK_WIDGET (window));
325
      break;
326 327 328
    case ARG_MODAL:
      gtk_window_set_modal (window, GTK_VALUE_BOOL (*arg));
      break;
329
    case ARG_WIN_POS:
330
      gtk_window_set_position (window, GTK_VALUE_ENUM (*arg));
331
      break;
332 333 334 335 336 337
    case ARG_DEFAULT_WIDTH:
      gtk_window_set_default_size (window, GTK_VALUE_INT (*arg), -2);
      break;
    case ARG_DEFAULT_HEIGHT:
      gtk_window_set_default_size (window, -2, GTK_VALUE_INT (*arg));
      break;
Tim Janik's avatar
Tim Janik committed
338 339
    default:
      break;
Elliot Lee's avatar
Elliot Lee committed
340 341 342
    }
}

343
static void
344
gtk_window_get_arg (GtkObject  *object,
345 346 347
		    GtkArg     *arg,
		    guint	arg_id)
{
348 349 350 351
  GtkWindow  *window;

  window = GTK_WINDOW (object);

352 353
  switch (arg_id)
    {
354
      GtkWindowGeometryInfo *info;
355
    case ARG_TYPE:
356
      GTK_VALUE_ENUM (*arg) = window->type;
357 358
      break;
    case ARG_TITLE:
359
      GTK_VALUE_STRING (*arg) = g_strdup (window->title);
360 361
      break;
    case ARG_AUTO_SHRINK:
362
      GTK_VALUE_BOOL (*arg) = window->auto_shrink;
363 364
      break;
    case ARG_ALLOW_SHRINK:
365
      GTK_VALUE_BOOL (*arg) = window->allow_shrink;
366 367
      break;
    case ARG_ALLOW_GROW:
368 369
      GTK_VALUE_BOOL (*arg) = window->allow_grow;
      break;
370 371 372
    case ARG_MODAL:
      GTK_VALUE_BOOL (*arg) = window->modal;
      break;
373 374
    case ARG_WIN_POS:
      GTK_VALUE_ENUM (*arg) = window->position;
375
      break;
376 377 378 379 380 381 382 383 384 385 386 387 388 389
    case ARG_DEFAULT_WIDTH:
      info = gtk_window_get_geometry_info (window, FALSE);
      if (!info)
	GTK_VALUE_INT (*arg) = -1;
      else
	GTK_VALUE_INT (*arg) = info->width;
      break;
    case ARG_DEFAULT_HEIGHT:
      info = gtk_window_get_geometry_info (window, FALSE);
      if (!info)
	GTK_VALUE_INT (*arg) = -1;
      else
	GTK_VALUE_INT (*arg) = info->height;
      break;
390 391 392 393 394 395
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
396 397 398 399 400
GtkWidget*
gtk_window_new (GtkWindowType type)
{
  GtkWindow *window;

401 402 403
  g_return_val_if_fail (type >= GTK_WINDOW_TOPLEVEL && type <= GTK_WINDOW_POPUP, NULL);

  window = gtk_type_new (GTK_TYPE_WINDOW);
Elliot Lee's avatar
Elliot Lee committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

  window->type = type;

  return GTK_WIDGET (window);
}

void
gtk_window_set_title (GtkWindow   *window,
		      const gchar *title)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  if (window->title)
    g_free (window->title);
  window->title = g_strdup (title);

  if (GTK_WIDGET_REALIZED (window))
    gdk_window_set_title (GTK_WIDGET (window)->window, window->title);
}

void
gtk_window_set_wmclass (GtkWindow *window,
427 428
			const gchar *wmclass_name,
			const gchar *wmclass_class)
Elliot Lee's avatar
Elliot Lee committed
429 430 431 432
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

Tim Janik's avatar
Tim Janik committed
433
  g_free (window->wmclass_name);
Elliot Lee's avatar
Elliot Lee committed
434 435
  window->wmclass_name = g_strdup (wmclass_name);

Tim Janik's avatar
Tim Janik committed
436
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
437 438 439 440 441 442 443 444 445 446
  window->wmclass_class = g_strdup (wmclass_class);

  if (GTK_WIDGET_REALIZED (window))
    g_warning ("shouldn't set wmclass after window is realized!\n");
}

void
gtk_window_set_focus (GtkWindow *window,
		      GtkWidget *focus)
{
447 448 449 450 451 452 453 454
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
  if (focus)
    {
      g_return_if_fail (GTK_IS_WIDGET (focus));
      g_return_if_fail (GTK_WIDGET_CAN_FOCUS (focus));
    }

455 456
  if ((window->focus_widget != focus) ||
      (focus && !GTK_WIDGET_HAS_FOCUS (focus)))
457
    gtk_signal_emit (GTK_OBJECT (window), window_signals[SET_FOCUS], focus);
Elliot Lee's avatar
Elliot Lee committed
458 459 460 461
}

void
gtk_window_set_default (GtkWindow *window,
462
			GtkWidget *default_widget)
Elliot Lee's avatar
Elliot Lee committed
463 464 465 466
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

467 468 469 470
  if (default_widget)
    g_return_if_fail (GTK_WIDGET_CAN_DEFAULT (default_widget));

  if (window->default_widget != default_widget)
Elliot Lee's avatar
Elliot Lee committed
471 472 473
    {
      if (window->default_widget)
	{
474 475 476
	  if (window->focus_widget != window->default_widget ||
	      !GTK_WIDGET_RECEIVES_DEFAULT (window->default_widget))
	    GTK_WIDGET_UNSET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
Elliot Lee's avatar
Elliot Lee committed
477 478 479
	  gtk_widget_draw_default (window->default_widget);
	}

480
      window->default_widget = default_widget;
Elliot Lee's avatar
Elliot Lee committed
481 482 483

      if (window->default_widget)
	{
484 485 486
	  if (window->focus_widget == NULL ||
	      !GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget))
	    GTK_WIDGET_SET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
Elliot Lee's avatar
Elliot Lee committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	  gtk_widget_draw_default (window->default_widget);
	}
    }
}

void
gtk_window_set_policy (GtkWindow *window,
		       gint       allow_shrink,
		       gint       allow_grow,
		       gint       auto_shrink)
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  window->allow_shrink = (allow_shrink != FALSE);
  window->allow_grow = (allow_grow != FALSE);
  window->auto_shrink = (auto_shrink != FALSE);
Tim Janik's avatar
Tim Janik committed
504

505
  gtk_widget_queue_resize (GTK_WIDGET (window));
Elliot Lee's avatar
Elliot Lee committed
506 507 508
}

void
Tim Janik's avatar
Tim Janik committed
509 510
gtk_window_add_accel_group (GtkWindow        *window,
			    GtkAccelGroup    *accel_group)
Elliot Lee's avatar
Elliot Lee committed
511 512 513
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
Tim Janik's avatar
Tim Janik committed
514
  g_return_if_fail (accel_group != NULL);
Elliot Lee's avatar
Elliot Lee committed
515

Tim Janik's avatar
Tim Janik committed
516
  gtk_accel_group_attach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
517 518 519
}

void
Tim Janik's avatar
Tim Janik committed
520 521
gtk_window_remove_accel_group (GtkWindow       *window,
			       GtkAccelGroup   *accel_group)
Elliot Lee's avatar
Elliot Lee committed
522 523 524
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
Tim Janik's avatar
Tim Janik committed
525
  g_return_if_fail (accel_group != NULL);
Elliot Lee's avatar
Elliot Lee committed
526

Tim Janik's avatar
Tim Janik committed
527
  gtk_accel_group_detach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
528 529 530
}

void
531 532
gtk_window_set_position (GtkWindow         *window,
			 GtkWindowPosition  position)
Elliot Lee's avatar
Elliot Lee committed
533 534 535 536 537 538 539
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  window->position = position;
}

540 541 542 543 544 545 546 547
gint
gtk_window_activate_focus (GtkWindow      *window)
{
  g_return_val_if_fail (window != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);

  if (window->focus_widget)
    {
548 549
      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
	gtk_widget_activate (window->focus_widget);
550 551 552 553 554 555 556 557 558 559 560 561
      return TRUE;
    }

  return FALSE;
}

gint
gtk_window_activate_default (GtkWindow      *window)
{
  g_return_val_if_fail (window != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (window), FALSE);

562
  if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget))
563 564 565 566 567 568
    {
      gtk_widget_activate (window->default_widget);
      return TRUE;
    }

  return FALSE;
569 570
}

571
void
572 573
gtk_window_set_modal (GtkWindow *window,
		      gboolean   modal)
574 575 576 577
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

578
  window->modal = modal != FALSE;
579

580 581 582 583 584
  /* adjust desired modality state */
  if (GTK_WIDGET_VISIBLE (window) && window->modal)
    gtk_grab_add (GTK_WIDGET (window));
  else
    gtk_grab_remove (GTK_WIDGET (window));
585 586
}

587 588 589 590 591 592 593 594 595 596 597 598
GList*
gtk_window_list_toplevels (void)
{
  GList *list = NULL;
  GSList *slist;

  for (slist = toplevel_list; slist; slist = slist->next)
    list = g_list_prepend (list, gtk_widget_ref (slist->data));

  return list;
}

599 600 601 602 603 604 605
void
gtk_window_add_embedded_xid (GtkWindow *window, guint xid)
{
  GList *embedded_windows;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
606

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  embedded_windows = gtk_object_get_data (GTK_OBJECT (window), "gtk-embedded");
  if (embedded_windows)
    gtk_object_remove_no_notify_by_id (GTK_OBJECT (window), 
				       g_quark_from_static_string ("gtk-embedded"));
  embedded_windows = g_list_prepend (embedded_windows,
				     GUINT_TO_POINTER (xid));

  gtk_object_set_data_full (GTK_OBJECT (window), "gtk-embedded", 
			    embedded_windows,
			    embedded_windows ?
			      (GtkDestroyNotify) g_list_free : NULL);
}

void
gtk_window_remove_embedded_xid (GtkWindow *window, guint xid)
{
  GList *embedded_windows;
  GList *node;

  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
  
  embedded_windows = gtk_object_get_data (GTK_OBJECT (window), "gtk-embedded");
  if (embedded_windows)
    gtk_object_remove_no_notify_by_id (GTK_OBJECT (window), 
				       g_quark_from_static_string ("gtk-embedded"));

  node = g_list_find (embedded_windows, GUINT_TO_POINTER (xid));
  if (node)
    {
      embedded_windows = g_list_remove_link (embedded_windows, node);
      g_list_free_1 (node);
    }
  
  gtk_object_set_data_full (GTK_OBJECT (window), 
			    "gtk-embedded", embedded_windows,
			    embedded_windows ?
			      (GtkDestroyNotify) g_list_free : NULL);
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
void       
gtk_window_reposition (GtkWindow *window,
		       gint       x,
		       gint       y)
{
  GtkWindowGeometryInfo *info;
  
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  /* keep this in sync with gtk_window_compute_reposition()
   */
  if (GTK_WIDGET_REALIZED (window))
    {
      info = gtk_window_get_geometry_info (window, TRUE);

      if (!(info->last.flags & GDK_HINT_POS))
	{
	  info->last.flags |= GDK_HINT_POS;
	  gdk_window_set_geometry_hints (GTK_WIDGET (window)->window,
					 &info->last.geometry,
					 info->last.flags);
	}
  
      gdk_window_move (GTK_WIDGET (window)->window, x, y);
    }
}

675
static void
676
gtk_window_shutdown (GObject *object)
677 678 679 680 681 682 683 684 685 686
{
  GtkWindow *window;

  g_return_if_fail (GTK_IS_WINDOW (object));

  window = GTK_WINDOW (object);

  gtk_window_set_focus (window, NULL);
  gtk_window_set_default (window, NULL);

687
  G_OBJECT_CLASS (parent_class)->shutdown (object);
688 689
}

690 691 692 693 694
static void
gtk_window_transient_parent_realized (GtkWidget *parent,
				      GtkWidget *window)
{
  if (GTK_WIDGET_REALIZED (window))
695
    gdk_window_set_transient_for (window->window, parent->window);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
}

static void
gtk_window_transient_parent_unrealized (GtkWidget *parent,
					GtkWidget *window)
{
  if (GTK_WIDGET_REALIZED (window))
    gdk_property_delete (window->window, 
			 gdk_atom_intern ("WM_TRANSIENT_FOR", FALSE));
}

static void       
gtk_window_unset_transient_for  (GtkWindow *window)
{
  if (window->transient_parent)
    {
      gtk_signal_disconnect_by_func (GTK_OBJECT (window->transient_parent),
				     GTK_SIGNAL_FUNC (gtk_window_transient_parent_realized),
				     window);
      gtk_signal_disconnect_by_func (GTK_OBJECT (window->transient_parent),
				     GTK_SIGNAL_FUNC (gtk_window_transient_parent_unrealized),
				     window);
      gtk_signal_disconnect_by_func (GTK_OBJECT (window->transient_parent),
				     GTK_SIGNAL_FUNC (gtk_widget_destroyed),
				     &window->transient_parent);

      window->transient_parent = NULL;
    }
}

void       
gtk_window_set_transient_for  (GtkWindow *window, 
			       GtkWindow *parent)
{
  g_return_if_fail (window != 0);

  if (window->transient_parent)
    {
      if (GTK_WIDGET_REALIZED (window) && 
	  GTK_WIDGET_REALIZED (window->transient_parent) && 
	  (!parent || !GTK_WIDGET_REALIZED (parent)))
	gtk_window_transient_parent_unrealized (GTK_WIDGET (window->transient_parent),
						GTK_WIDGET (window));
739 740

      gtk_window_unset_transient_for (window);
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    }

  window->transient_parent = parent;

  if (parent)
    {
      gtk_signal_connect (GTK_OBJECT (parent), "destroy",
			  GTK_SIGNAL_FUNC (gtk_widget_destroyed),
			  &window->transient_parent);
      gtk_signal_connect (GTK_OBJECT (parent), "realize",
			  GTK_SIGNAL_FUNC (gtk_window_transient_parent_realized),
			  window);
      gtk_signal_connect (GTK_OBJECT (parent), "unrealize",
			  GTK_SIGNAL_FUNC (gtk_window_transient_parent_unrealized),
			  window);

      if (GTK_WIDGET_REALIZED (window) &&
	  GTK_WIDGET_REALIZED (parent))
	gtk_window_transient_parent_realized (GTK_WIDGET (parent),
					      GTK_WIDGET (window));
    }
}

static void
gtk_window_geometry_destroy (GtkWindowGeometryInfo *info)
{
  if (info->widget)
    gtk_signal_disconnect_by_func (GTK_OBJECT (info->widget),
				   GTK_SIGNAL_FUNC (gtk_widget_destroyed),
				   &info->widget);
  g_free (info);
}

774 775 776
static GtkWindowGeometryInfo*
gtk_window_get_geometry_info (GtkWindow *window,
			      gboolean   create)
777 778 779 780 781 782 783
{
  GtkWindowGeometryInfo *info;

  info = gtk_object_get_data (GTK_OBJECT (window), "gtk-window-geometry");

  if (!info && create)
    {
784
      info = g_new0 (GtkWindowGeometryInfo, 1);
785

786 787 788 789
      info->width = 0;
      info->height = 0;
      info->last.width = -1;
      info->last.height = -1;
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
      info->widget = NULL;
      info->mask = 0;

      gtk_object_set_data_full (GTK_OBJECT (window), 
				"gtk-window-geometry",
				info, 
				(GtkDestroyNotify) gtk_window_geometry_destroy);
    }

  return info;
}

void       
gtk_window_set_geometry_hints (GtkWindow       *window,
			       GtkWidget       *geometry_widget,
			       GdkGeometry     *geometry,
			       GdkWindowHints   geom_mask)
{
  GtkWindowGeometryInfo *info;

  g_return_if_fail (window != NULL);

  info = gtk_window_get_geometry_info (window, TRUE);
  
  if (info->widget)
    gtk_signal_disconnect_by_func (GTK_OBJECT (info->widget),
				   GTK_SIGNAL_FUNC (gtk_widget_destroyed),
				   &info->widget);
  
  info->widget = geometry_widget;
  if (info->widget)
    gtk_signal_connect (GTK_OBJECT (geometry_widget), "destroy",
			GTK_SIGNAL_FUNC (gtk_widget_destroyed),
			&info->widget);

  if (geometry)
    info->geometry = *geometry;

  info->mask = geom_mask;
829 830

  gtk_widget_queue_resize (GTK_WIDGET (window));
831 832 833 834 835 836 837 838 839
}

void       
gtk_window_set_default_size (GtkWindow   *window,
			     gint         width,
			     gint         height)
{
  GtkWindowGeometryInfo *info;

840
  g_return_if_fail (GTK_IS_WINDOW (window));
841 842 843

  info = gtk_window_get_geometry_info (window, TRUE);

844 845 846 847 848 849
  if (width >= 0)
    info->width = width;
  if (height >= 0)
    info->height = height;

  gtk_widget_queue_resize (GTK_WIDGET (window));
850 851
}
  
852 853 854
static void
gtk_window_destroy (GtkObject *object)
{
855 856
  GtkWindow *window;
  
857 858
  g_return_if_fail (GTK_IS_WINDOW (object));

859
  window = GTK_WINDOW (object);
860

861 862 863
  if (window->transient_parent)
    gtk_window_unset_transient_for (window);

864 865 866 867 868 869
  if (window->has_user_ref_count)
    {
      window->has_user_ref_count = FALSE;
      gtk_widget_unref (GTK_WIDGET (window));
    }

870
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
871 872
}

Elliot Lee's avatar
Elliot Lee committed
873
static void
874
gtk_window_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
875 876 877 878 879 880
{
  GtkWindow *window;

  g_return_if_fail (GTK_IS_WINDOW (object));

  window = GTK_WINDOW (object);
881 882 883

  toplevel_list = g_slist_remove (toplevel_list, window);

Elliot Lee's avatar
Elliot Lee committed
884
  g_free (window->title);
Tim Janik's avatar
Tim Janik committed
885 886
  g_free (window->wmclass_name);
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
887

888
  G_OBJECT_CLASS(parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
889 890 891 892 893
}

static void
gtk_window_show (GtkWidget *widget)
{
894 895 896
  GtkWindow *window = GTK_WINDOW (widget);
  GtkContainer *container = GTK_CONTAINER (window);
  gboolean need_resize;
Elliot Lee's avatar
Elliot Lee committed
897 898

  GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

  need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
  container->need_resize = FALSE;

  if (need_resize)
    {
      GtkWindowGeometryInfo *info = gtk_window_get_geometry_info (window, TRUE);
      GtkAllocation allocation = { 0, 0 };
      GdkGeometry new_geometry;
      guint width, height, new_flags;

      /* determine default size to initially show the window with */
      gtk_widget_size_request (widget, NULL);
      gtk_window_compute_default_size (window, &width, &height);

      /* save away the last default size for later comparisions */
      info->last.width = width;
      info->last.height = height;

      /* constrain size to geometry */
      gtk_window_compute_hints (window, &new_geometry, &new_flags);
      gtk_window_constrain_size (window,
				 &new_geometry, new_flags,
				 width, height,
				 &width, &height);

      /* and allocate the window */
      allocation.width  = width;
      allocation.height = height;
      gtk_widget_size_allocate (widget, &allocation);
      
      if (GTK_WIDGET_REALIZED (widget))
	gdk_window_resize (widget->window, width, height);
      else
	gtk_widget_realize (widget);
    }
  
  gtk_container_check_resize (container);

Elliot Lee's avatar
Elliot Lee committed
938
  gtk_widget_map (widget);
939

940
  if (window->modal)
941
    gtk_grab_add (widget);
Elliot Lee's avatar
Elliot Lee committed
942 943 944 945 946
}

static void
gtk_window_hide (GtkWidget *widget)
{
947 948
  GtkWindow *window;

Elliot Lee's avatar
Elliot Lee committed
949 950 951
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));

952 953
  window = GTK_WINDOW (widget);

Elliot Lee's avatar
Elliot Lee committed
954 955
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
  gtk_widget_unmap (widget);
956

957 958
  if (window->modal)
    gtk_grab_remove (widget);
Elliot Lee's avatar
Elliot Lee committed
959 960 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
}

static void
gtk_window_map (GtkWidget *widget)
{
  GtkWindow *window;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  window = GTK_WINDOW (widget);

  if (window->bin.child &&
      GTK_WIDGET_VISIBLE (window->bin.child) &&
      !GTK_WIDGET_MAPPED (window->bin.child))
    gtk_widget_map (window->bin.child);

  gdk_window_show (widget->window);
}

static void
gtk_window_unmap (GtkWidget *widget)
{
  GtkWindow *window;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
990
  gdk_window_withdraw (widget->window);
Elliot Lee's avatar
Elliot Lee committed
991 992 993

  window = GTK_WINDOW (widget);
  window->use_uposition = TRUE;
994 995
  window->resize_count = 0;
  window->handling_resize = FALSE;
Elliot Lee's avatar
Elliot Lee committed
996 997 998 999 1000 1001 1002 1003
}

static void
gtk_window_realize (GtkWidget *widget)
{
  GtkWindow *window;
  GdkWindowAttr attributes;
  gint attributes_mask;
1004
  
Elliot Lee's avatar
Elliot Lee committed
1005
  g_return_if_fail (GTK_IS_WINDOW (widget));
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

  window = GTK_WINDOW (widget);

  /* ensure widget tree is properly size allocated */
  if (widget->allocation.x == -1 &&
      widget->allocation.y == -1 &&
      widget->allocation.width == 1 &&
      widget->allocation.height == 1)
    {
      GtkRequisition requisition;
      GtkAllocation allocation = { 0, 0, 200, 200 };

      gtk_widget_size_request (widget, &requisition);
      if (requisition.width || requisition.height)
	{
	  /* non-empty window */
	  allocation.width = requisition.width;
	  allocation.height = requisition.height;
	}
      gtk_widget_size_allocate (widget, &allocation);
      
      gtk_container_queue_resize (GTK_CONTAINER (widget));

      g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
    }
1031
  
Elliot Lee's avatar
Elliot Lee committed
1032
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1033
  
Elliot Lee's avatar
Elliot Lee committed
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
  switch (window->type)
    {
    case GTK_WINDOW_TOPLEVEL:
      attributes.window_type = GDK_WINDOW_TOPLEVEL;
      break;
    case GTK_WINDOW_DIALOG:
      attributes.window_type = GDK_WINDOW_DIALOG;
      break;
    case GTK_WINDOW_POPUP:
      attributes.window_type = GDK_WINDOW_TEMP;
      break;
    }
1046
   
Elliot Lee's avatar
Elliot Lee committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
  attributes.title = window->title;
  attributes.wmclass_name = window->wmclass_name;
  attributes.wmclass_class = window->wmclass_class;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = gtk_widget_get_events (widget);
  attributes.event_mask |= (GDK_EXPOSURE_MASK |
			    GDK_KEY_PRESS_MASK |
			    GDK_ENTER_NOTIFY_MASK |
			    GDK_LEAVE_NOTIFY_MASK |
			    GDK_FOCUS_CHANGE_MASK |
			    GDK_STRUCTURE_MASK);
1062
   
Elliot Lee's avatar
Elliot Lee committed
1063 1064 1065
  attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
  attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
  attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
1066
   
Elliot Lee's avatar
Elliot Lee committed
1067 1068 1069 1070 1071
  widget->window = gdk_window_new (NULL, &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, window);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1072
  gtk_window_paint (widget, NULL);
1073 1074 1075 1076 1077

  if (window->transient_parent &&
      GTK_WIDGET_REALIZED (window->transient_parent))
    gdk_window_set_transient_for (widget->window,
				  GTK_WIDGET (window->transient_parent)->window);
Elliot Lee's avatar
Elliot Lee committed
1078 1079 1080 1081 1082 1083 1084
}

static void
gtk_window_size_request (GtkWidget      *widget,
			 GtkRequisition *requisition)
{
  GtkWindow *window;
1085
  GtkBin *bin;
Elliot Lee's avatar
Elliot Lee committed
1086 1087 1088 1089 1090 1091

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));
  g_return_if_fail (requisition != NULL);

  window = GTK_WINDOW (widget);
1092 1093 1094 1095
  bin = GTK_BIN (window);
  
  requisition->width = GTK_CONTAINER (window)->border_width * 2;
  requisition->height = GTK_CONTAINER (window)->border_width * 2;
Elliot Lee's avatar
Elliot Lee committed
1096

1097
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
Elliot Lee's avatar
Elliot Lee committed
1098
    {
1099 1100 1101
      GtkRequisition child_requisition;
      
      gtk_widget_size_request (bin->child, &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
1102

1103 1104
      requisition->width += child_requisition.width;
      requisition->height += child_requisition.height;
Elliot Lee's avatar
Elliot Lee committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
    }
}

static void
gtk_window_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
  GtkWindow *window;
  GtkAllocation child_allocation;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));
  g_return_if_fail (allocation != NULL);

  window = GTK_WINDOW (widget);
  widget->allocation = *allocation;

  if (window->bin.child && GTK_WIDGET_VISIBLE (window->bin.child))
    {
      child_allocation.x = GTK_CONTAINER (window)->border_width;
      child_allocation.y = GTK_CONTAINER (window)->border_width;
1126 1127 1128 1129
      child_allocation.width =
	MAX (1, (gint)allocation->width - child_allocation.x * 2);
      child_allocation.height =
	MAX (1, (gint)allocation->height - child_allocation.y * 2);
Elliot Lee's avatar
Elliot Lee committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139

      gtk_widget_size_allocate (window->bin.child, &child_allocation);
    }
}

static gint
gtk_window_configure_event (GtkWidget         *widget,
			    GdkEventConfigure *event)
{
  GtkWindow *window;
1140
  
Elliot Lee's avatar
Elliot Lee committed
1141 1142 1143
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
1144
  
1145 1146
  window = GTK_WINDOW (widget);

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
  /* we got a configure event specifying the new window size and position,
   * in principle we have to distinguish 4 cases here:
   * 1) the size didn't change and resize_count == 0
   *    -> the window was merely moved (sometimes not even that)
   * 2) the size didn't change and resize_count > 0
   *    -> we requested a new size, but didn't get it
   * 3) the size changed and resize_count > 0
   *    -> we asked for a new size and we got one
   * 4) the size changed and resize_count == 0
   *    -> we got resized from outside the toolkit, and have to
   *    accept that size since we don't want to fight neither the
   *    window manager nor the user
   * in the three latter cases we have to reallocate the widget tree,
   * which happens in gtk_window_move_resize(), so we set a flag for
   * that function and assign the new size. if resize_count > 1,
   * we simply do nothing and wait for more configure events.
   */

  if (window->resize_count > 0 ||
      widget->allocation.width != event->width ||
      widget->allocation.height != event->height)
1168
    {
1169 1170 1171 1172
      if (window->resize_count > 0)
	window->resize_count -= 1;

      if (window->resize_count == 0)
1173
	{
1174 1175 1176 1177 1178 1179
	  window->handling_resize = TRUE;
	  
	  widget->allocation.width = event->width;
	  widget->allocation.height = event->height;
	  
	  gtk_widget_queue_resize (widget);
1180 1181 1182
	}
    }

1183
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1184 1185 1186 1187 1188 1189 1190 1191
}

static gint
gtk_window_key_press_event (GtkWidget   *widget,
			    GdkEventKey *event)
{
  GtkWindow *window;
  GtkDirectionType direction = 0;
Tim Janik's avatar
Tim Janik committed
1192
  gboolean handled;
Elliot Lee's avatar
Elliot Lee committed
1193 1194 1195 1196 1197 1198 1199

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  window = GTK_WINDOW (widget);

Tim Janik's avatar
Tim Janik committed
1200 1201
  handled = FALSE;
  
1202 1203 1204
  if (window->focus_widget &&
      window->focus_widget != widget &&
      GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
Tim Janik's avatar
Tim Janik committed
1205 1206 1207 1208 1209 1210
    {
      handled = gtk_widget_event (window->focus_widget, (GdkEvent*) event);
    }
    
  if (!handled)
    handled = gtk_accel_groups_activate (GTK_OBJECT (window), event->keyval, event->state);
Elliot Lee's avatar
Elliot Lee committed
1211

Tim Janik's avatar
Tim Janik committed
1212
  if (!handled)
Elliot Lee's avatar
Elliot Lee committed
1213 1214 1215 1216 1217 1218
    {
      switch (event->keyval)
	{
	case GDK_space:
	  if (window->focus_widget)
	    {
1219 1220
	      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
		gtk_widget_activate (window->focus_widget);
Tim Janik's avatar
Tim Janik committed
1221
	      handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1222 1223 1224 1225
	    }
	  break;
	case GDK_Return:
	case GDK_KP_Enter:
1226 1227
	  if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget) &&
	      (!window->focus_widget || !GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget)))
Elliot Lee's avatar
Elliot Lee committed
1228 1229
	    {
	      gtk_widget_activate (window->default_widget);
Tim Janik's avatar
Tim Janik committed
1230
	      handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1231 1232 1233
	    }
          else if (window->focus_widget)
	    {
1234 1235
	      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
		gtk_widget_activate (window->focus_widget);
Tim Janik's avatar
Tim Janik committed
1236
	      handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1237 1238 1239 1240 1241 1242
	    }
	  break;
	case GDK_Up:
	case GDK_Down:
	case GDK_Left:
	case GDK_Right:
1243 1244 1245 1246
	case GDK_KP_Up:
	case GDK_KP_Down:
	case GDK_KP_Left:
	case GDK_KP_Right:
Elliot Lee's avatar
Elliot Lee committed
1247
	case GDK_Tab:
1248
	case GDK_ISO_Left_Tab:
Elliot Lee's avatar
Elliot Lee committed
1249 1250 1251
	  switch (event->keyval)
	    {
	    case GDK_Up:
1252
	    case GDK_KP_Up:
Elliot Lee's avatar
Elliot Lee committed
1253 1254 1255
	      direction = GTK_DIR_UP;
	      break;
	    case GDK_Down:
1256
	    case GDK_KP_Down:
Elliot Lee's avatar
Elliot Lee committed
1257 1258 1259
	      direction = GTK_DIR_DOWN;
	      break;
	    case GDK_Left:
1260
	    case GDK_KP_Left:
Elliot Lee's avatar
Elliot Lee committed
1261 1262 1263
	      direction = GTK_DIR_LEFT;
	      break;
	    case GDK_Right:
1264
	    case GDK_KP_Right:
Elliot Lee's avatar
Elliot Lee committed
1265 1266 1267
	      direction = GTK_DIR_RIGHT;
	      break;
	    case GDK_Tab:
1268
	    case GDK_ISO_Left_Tab:
Elliot Lee's avatar
Elliot Lee committed
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
	      if (event->state & GDK_SHIFT_MASK)
		direction = GTK_DIR_TAB_BACKWARD;
	      else
		direction = GTK_DIR_TAB_FORWARD;
              break;
            default :
              direction = GTK_DIR_UP; /* never reached, but makes compiler happy */
	    }

	  gtk_container_focus (GTK_CONTAINER (widget), direction);

	  if (!GTK_CONTAINER (window)->focus_child)
	    gtk_window_set_focus (GTK_WINDOW (widget), NULL);
	  else
Tim Janik's avatar
Tim Janik committed
1283
	    handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1284 1285 1286 1287
	  break;
	}
    }

1288 1289 1290
  if (!handled && GTK_WIDGET_CLASS (parent_class)->key_press_event)
    handled = GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);

Tim Janik's avatar
Tim Janik committed
1291
  return handled;
Elliot Lee's avatar
Elliot Lee committed
1292 1293 1294 1295 1296 1297 1298
}

static gint
gtk_window_key_release_event (GtkWidget   *widget,
			      GdkEventKey *event)
{
  GtkWindow *window;
Tim Janik's avatar
Tim Janik committed
1299 1300
  gint handled;
  
Elliot Lee's avatar
Elliot Lee committed
1301 1302 1303
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Tim Janik's avatar
Tim Janik committed
1304
  
Elliot Lee's avatar
Elliot Lee committed
1305
  window = GTK_WINDOW (widget);
Tim Janik's avatar
Tim Janik committed
1306
  handled = FALSE;
1307 1308 1309
  if (window->focus_widget &&
      window->focus_widget != widget &&
      GTK_WIDGET_SENSITIVE (window->focus_widget))
Tim Janik's avatar
Tim Janik committed
1310 1311 1312
    {
      handled = gtk_widget_event (window->focus_widget, (GdkEvent*) event);
    }
1313 1314 1315 1316

  if (!handled && GTK_WIDGET_CLASS (parent_class)->key_release_event)
    handled = GTK_WIDGET_CLASS (parent_class)->key_release_event (widget, event);

Tim Janik's avatar
Tim Janik committed
1317
  return handled;
Elliot Lee's avatar
Elliot Lee committed
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
}

static gint
gtk_window_enter_notify_event (GtkWidget        *widget,
			       GdkEventCrossing *event)
{
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  return FALSE;
}

static gint
gtk_window_leave_notify_event (GtkWidget        *widget,
			       GdkEventCrossing *event)
{
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  return FALSE;
}

static gint
gtk_window_focus_in_event (GtkWidget     *widget,
			   GdkEventFocus *event)
{
  GtkWindow *window;
  GdkEventFocus fevent;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  /* It appears spurious focus in events can occur when
   *  the window is hidden. So we'll just check to see if
   *  the window is visible before actually handling the
   *  event
   */
  if (GTK_WIDGET_VISIBLE (widget))
    {
      window = GTK_WINDOW (widget);
1361 1362 1363
      if (window->focus_widget &&
	  window->focus_widget != widget &&
	  !GTK_WIDGET_HAS_FOCUS (window->focus_widget))
Elliot Lee's avatar
Elliot Lee committed
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	{
	  fevent.type = GDK_FOCUS_CHANGE;
	  fevent.window = window->focus_widget->window;
	  fevent.in = TRUE;

	  gtk_widget_event (window->focus_widget, (GdkEvent*) &fevent);
	}
    }

  return FALSE;
}

static gint
gtk_window_focus_out_event (GtkWidget     *widget,
			    GdkEventFocus *event)
{
  GtkWindow *window;
  GdkEventFocus fevent;

  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

  window = GTK_WINDOW (widget);
1388 1389 1390
  if (window->focus_widget &&
      window->focus_widget != widget &&
      GTK_WIDGET_HAS_FOCUS (window->focus_widget))
Elliot Lee's avatar
Elliot Lee committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
    {
      fevent.type = GDK_FOCUS_CHANGE;
      fevent.window = window->focus_widget->window;
      fevent.in = FALSE;

      gtk_widget_event (window->focus_widget, (GdkEvent*) &fevent);
    }

  return FALSE;
}

1402 1403
static GdkAtom atom_rcfiles = GDK_NONE;

Elliot Lee's avatar
Elliot Lee committed
1404
static void
1405 1406
gtk_window_read_rcfiles (GtkWidget *widget,
			 GdkEventClient *event)
Elliot Lee's avatar
Elliot Lee committed
1407
{
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
  GList *embedded_windows;

  embedded_windows = gtk_object_get_data (GTK_OBJECT (widget), "gtk-embedded");
  if (embedded_windows)
    {
      GdkEventClient sev;
      int i;
      
      for(i = 0; i < 5; i++)
	sev.data.l[i] = 0;
      sev.data_format = 32;
      sev.message_type = atom_rcfiles;
      
      while (embedded_windows)
	{
	  guint xid = GPOINTER_TO_UINT (embedded_windows->data);
	  gdk_event_send_client_message ((GdkEvent *) &sev, xid);
	  embedded_windows = embedded_windows->next;
	}
    }

1429 1430
  if (gtk_rc_reparse_all ())
    {
1431 1432 1433 1434 1435 1436
      /* If the above returned true, some of our RC files are out
       * of date, so we need to reset all our widgets. Our other
       * toplevel windows will also get the message, but by
       * then, the RC file will up to date, so we have to tell
       * them now.
       */
1437 1438 1439
      GList *list, *toplevels = gtk_window_list_toplevels ();

      for (list = toplevels; list; list = list->next)
1440
	{
1441 1442
	  gtk_widget_reset_rc_styles (list->data);
	  gtk_widget_unref (list->data);
1443
	}
1444
      g_list_free (toplevels);
1445
    }
Elliot Lee's avatar
Elliot Lee committed
1446 1447 1448 1449
}

static gint
gtk_window_client_event (GtkWidget	*widget,
1450
			 GdkEventClient	*event)
Elliot Lee's avatar
Elliot Lee committed
1451 1452 1453 1454 1455
{
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_WINDOW (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);

1456 1457 1458 1459 1460
  if (!atom_rcfiles)
    atom_rcfiles = gdk_atom_intern("_GTK_READ_RCFILES", FALSE);

  if(event->message_type == atom_rcfiles) 
    gtk_window_read_rcfiles (widget, event);    
Elliot Lee's avatar
Elliot Lee committed
1461 1462 1463 1464

  return FALSE;
}

1465 1466
static void
gtk_window_check_resize (GtkContainer *container)
Elliot Lee's avatar
Elliot Lee committed
1467 1468 1469
{
  GtkWindow *window;

1470 1471
  g_return_if_fail (container != NULL);
  g_return_if_fail (GTK_IS_WINDOW (container));
Elliot Lee's avatar
Elliot Lee committed
1472 1473

  window = GTK_WINDOW (container);
1474

1475 1476
  if (GTK_WIDGET_VISIBLE (container))
    gtk_window_move_resize (window);
Elliot Lee's avatar
Elliot Lee committed
1477 1478
}

1479
static void
1480
gtk_window_real_set_focus (GtkWindow *window,
1481 1482 1483
			   GtkWidget *focus)
{
  GdkEventFocus event;
1484
  gboolean def_flags = 0;
1485 1486 1487

  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
1488 1489 1490 1491
  
  if (window->default_widget)
    def_flags = GTK_WIDGET_HAS_DEFAULT (window->default_widget);
  
1492
  if (window->focus_widget)
1493
    {
1494 1495 1496 1497
      event.type = GDK_FOCUS_CHANGE;
      event.window = window->focus_widget->window;
      event.in = FALSE;
      
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
      if (GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget) &&
	  (window->focus_widget != window->default_widget))
        {
	  GTK_WIDGET_UNSET_FLAGS (window->focus_widget, GTK_HAS_DEFAULT);
	  /* if any widget had the default set there should be
	     a default_widget, but might not so this is a sanity
	     check */
	  if (window->default_widget)
	    GTK_WIDGET_SET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
        }
	
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
      gtk_widget_event (window->focus_widget, (GdkEvent*) &event);
    }
  
  window->focus_widget = focus;
  
  if (window->focus_widget)
    {
      event.type = GDK_FOCUS_CHANGE;
      event.window = window->focus_widget->window;
      event.in = TRUE;
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

      if (window->default_widget)
        {
          if (GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget) &&
	      (window->focus_widget != window->default_widget))
            {
	      if (GTK_WIDGET_CAN_DEFAULT (window->focus_widget))
	        GTK_WIDGET_SET_FLAGS (window->focus_widget, GTK_HAS_DEFAULT);
	      GTK_WIDGET_UNSET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
            }
	  else
	    {
	      GTK_WIDGET_SET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
	    }
	}
1534 1535
      
      gtk_widget_event (window->focus_widget, (GdkEvent*) &event);
1536
    }
1537 1538 1539 1540 1541 1542 1543 1544
  else if (window->default_widget)
    {
      GTK_WIDGET_SET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
    }
  
  if (window->default_widget &&
      (def_flags != GTK_WIDGET_FLAGS (window->default_widget)))
    gtk_widget_queue_draw (window->default_widget);
1545 1546
}

1547 1548 1549 1550
/*********************************