gtkwindow.c 64.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"
48
#include "gtkiconfactory.h"
Elliot Lee's avatar
Elliot Lee committed
49

50 51 52 53 54
/* 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
55
enum {
56
  SET_FOCUS,
Elliot Lee's avatar
Elliot Lee committed
57 58
  LAST_SIGNAL
};
59 60 61 62 63 64
enum {
  ARG_0,
  ARG_TYPE,
  ARG_TITLE,
  ARG_AUTO_SHRINK,
  ARG_ALLOW_SHRINK,
65
  ARG_ALLOW_GROW,
66
  ARG_MODAL,
67 68
  ARG_WIN_POS,
  ARG_DEFAULT_WIDTH,
69 70
  ARG_DEFAULT_HEIGHT,
  ARG_DESTROY_WITH_PARENT
71
};
Elliot Lee's avatar
Elliot Lee committed
72

73
typedef struct {
74 75
  GdkGeometry    geometry; /* Last set of geometry hints we set */
  GdkWindowHints flags;
76 77
  gint           width;
  gint           height;
78 79 80 81 82 83 84 85 86 87 88 89
} 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;
90 91
} GtkWindowGeometryInfo;

Elliot Lee's avatar
Elliot Lee committed
92 93
static void gtk_window_class_init         (GtkWindowClass    *klass);
static void gtk_window_init               (GtkWindow         *window);
94
static void gtk_window_set_arg            (GtkObject         *object,
95 96
					   GtkArg            *arg,
					   guint	      arg_id);
97
static void gtk_window_get_arg            (GtkObject         *object,
98 99
					   GtkArg            *arg,
					   guint	      arg_id);
100
static void gtk_window_shutdown           (GObject           *object);
101
static void gtk_window_destroy            (GtkObject         *object);
102
static void gtk_window_finalize           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
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,
Elliot Lee's avatar
Elliot Lee committed
127
					   GdkEventClient    *event);
128
static void gtk_window_check_resize       (GtkContainer      *container);
129
static void gtk_window_real_set_focus     (GtkWindow         *window,
130
					   GtkWidget         *focus);
131

132
static void gtk_window_move_resize        (GtkWindow         *window);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
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
155

156 157
static void gtk_window_read_rcfiles       (GtkWidget         *widget,
					   GdkEventClient    *event);
Owen Taylor's avatar
Owen Taylor committed
158 159
static void gtk_window_draw               (GtkWidget         *widget,
				           GdkRectangle      *area);
160 161
static void gtk_window_paint              (GtkWidget         *widget,
					   GdkRectangle      *area);
Owen Taylor's avatar
Owen Taylor committed
162 163
static gint gtk_window_expose             (GtkWidget         *widget,
				           GdkEventExpose    *event);
164 165 166 167 168 169 170 171 172
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
173

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


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

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

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

  return window_type;
}

static void
gtk_window_class_init (GtkWindowClass *klass)
{
207
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
208 209 210 211 212 213 214 215 216 217
  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 ());

218 219 220
  gobject_class->shutdown = gtk_window_shutdown;
  gobject_class->finalize = gtk_window_finalize;

221 222 223 224 225
  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);
226
  gtk_object_add_arg_type ("GtkWindow::modal", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_MODAL);
227
  gtk_object_add_arg_type ("GtkWindow::window_position", GTK_TYPE_WINDOW_POSITION, GTK_ARG_READWRITE, ARG_WIN_POS);
228 229
  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);
230
  gtk_object_add_arg_type ("GtkWindow::destroy_with_parent", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_DESTROY_WITH_PARENT);
231
  
232 233 234
  window_signals[SET_FOCUS] =
    gtk_signal_new ("set_focus",
                    GTK_RUN_LAST,
235
                    GTK_CLASS_TYPE (object_class),
236
                    GTK_SIGNAL_OFFSET (GtkWindowClass, set_focus),
237
                    gtk_marshal_VOID__POINTER,
238
		    GTK_TYPE_NONE, 1,
239
                    GTK_TYPE_WIDGET);
240

Elliot Lee's avatar
Elliot Lee committed
241 242
  gtk_object_class_add_signals (object_class, window_signals, LAST_SIGNAL);

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

  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;

263 264 265
  widget_class->draw = gtk_window_draw;
  widget_class->expose_event = gtk_window_expose;
   
266
  container_class->check_resize = gtk_window_check_resize;
Elliot Lee's avatar
Elliot Lee committed
267

268
  klass->set_focus = gtk_window_real_set_focus;
Elliot Lee's avatar
Elliot Lee committed
269 270 271 272 273 274
}

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

277 278
  gtk_container_set_resize_mode (GTK_CONTAINER (window), GTK_RESIZE_QUEUE);

Elliot Lee's avatar
Elliot Lee committed
279
  window->title = NULL;
280
  window->wmclass_name = g_strdup (g_get_prgname ());
281
  window->wmclass_class = g_strdup (gdk_progclass);
Elliot Lee's avatar
Elliot Lee committed
282 283 284 285 286 287 288 289 290 291
  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;
292
  window->modal = FALSE;
293 294 295 296 297

  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
298 299 300
}

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

  window = GTK_WINDOW (object);

309
  switch (arg_id)
Elliot Lee's avatar
Elliot Lee committed
310
    {
311
    case ARG_TYPE:
312
      window->type = GTK_VALUE_ENUM (*arg);
313 314
      break;
    case ARG_TITLE:
315
      gtk_window_set_title (window, GTK_VALUE_STRING (*arg));
316 317
      break;
    case ARG_AUTO_SHRINK:
318
      window->auto_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
319
      gtk_widget_queue_resize (GTK_WIDGET (window));
320 321
      break;
    case ARG_ALLOW_SHRINK:
322
      window->allow_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
323
      gtk_widget_queue_resize (GTK_WIDGET (window));
324 325
      break;
    case ARG_ALLOW_GROW:
326
      window->allow_grow = (GTK_VALUE_BOOL (*arg) != FALSE);
327
      gtk_widget_queue_resize (GTK_WIDGET (window));
328
      break;
329 330 331
    case ARG_MODAL:
      gtk_window_set_modal (window, GTK_VALUE_BOOL (*arg));
      break;
332
    case ARG_WIN_POS:
333
      gtk_window_set_position (window, GTK_VALUE_ENUM (*arg));
334
      break;
335 336 337 338 339 340
    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;
341 342 343
    case ARG_DESTROY_WITH_PARENT:
      gtk_window_set_destroy_with_parent (window, GTK_VALUE_BOOL (*arg));
      break;
Tim Janik's avatar
Tim Janik committed
344 345
    default:
      break;
Elliot Lee's avatar
Elliot Lee committed
346 347 348
    }
}

349
static void
350
gtk_window_get_arg (GtkObject  *object,
351 352 353
		    GtkArg     *arg,
		    guint	arg_id)
{
354 355 356 357
  GtkWindow  *window;

  window = GTK_WINDOW (object);

358 359
  switch (arg_id)
    {
360
      GtkWindowGeometryInfo *info;
361
    case ARG_TYPE:
362
      GTK_VALUE_ENUM (*arg) = window->type;
363 364
      break;
    case ARG_TITLE:
365
      GTK_VALUE_STRING (*arg) = g_strdup (window->title);
366 367
      break;
    case ARG_AUTO_SHRINK:
368
      GTK_VALUE_BOOL (*arg) = window->auto_shrink;
369 370
      break;
    case ARG_ALLOW_SHRINK:
371
      GTK_VALUE_BOOL (*arg) = window->allow_shrink;
372 373
      break;
    case ARG_ALLOW_GROW:
374 375
      GTK_VALUE_BOOL (*arg) = window->allow_grow;
      break;
376 377 378
    case ARG_MODAL:
      GTK_VALUE_BOOL (*arg) = window->modal;
      break;
379 380
    case ARG_WIN_POS:
      GTK_VALUE_ENUM (*arg) = window->position;
381
      break;
382 383 384 385 386 387 388 389 390 391 392 393 394 395
    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;
396 397 398
    case ARG_DESTROY_WITH_PARENT:
      GTK_VALUE_BOOL (*arg) = window->destroy_with_parent;
      break;
399 400 401 402 403 404
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
405 406 407 408 409
GtkWidget*
gtk_window_new (GtkWindowType type)
{
  GtkWindow *window;

410 411 412
  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
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

  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,
436 437
			const gchar *wmclass_name,
			const gchar *wmclass_class)
Elliot Lee's avatar
Elliot Lee committed
438 439 440 441
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

Tim Janik's avatar
Tim Janik committed
442
  g_free (window->wmclass_name);
Elliot Lee's avatar
Elliot Lee committed
443 444
  window->wmclass_name = g_strdup (wmclass_name);

Tim Janik's avatar
Tim Janik committed
445
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
446 447 448 449 450 451 452 453 454 455
  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)
{
456 457 458 459 460 461 462 463
  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));
    }

464 465
  if ((window->focus_widget != focus) ||
      (focus && !GTK_WIDGET_HAS_FOCUS (focus)))
466
    gtk_signal_emit (GTK_OBJECT (window), window_signals[SET_FOCUS], focus);
Elliot Lee's avatar
Elliot Lee committed
467 468 469 470
}

void
gtk_window_set_default (GtkWindow *window,
471
			GtkWidget *default_widget)
Elliot Lee's avatar
Elliot Lee committed
472 473 474 475
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

476 477 478 479
  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
480 481 482
    {
      if (window->default_widget)
	{
483 484 485
	  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
486 487 488
	  gtk_widget_draw_default (window->default_widget);
	}

489
      window->default_widget = default_widget;
Elliot Lee's avatar
Elliot Lee committed
490 491 492

      if (window->default_widget)
	{
493 494 495
	  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
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	  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
513

514
  gtk_widget_queue_resize (GTK_WIDGET (window));
Elliot Lee's avatar
Elliot Lee committed
515 516 517
}

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

Tim Janik's avatar
Tim Janik committed
525
  gtk_accel_group_attach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
526 527 528
}

void
Tim Janik's avatar
Tim Janik committed
529 530
gtk_window_remove_accel_group (GtkWindow       *window,
			       GtkAccelGroup   *accel_group)
Elliot Lee's avatar
Elliot Lee committed
531 532 533
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
Tim Janik's avatar
Tim Janik committed
534
  g_return_if_fail (accel_group != NULL);
Elliot Lee's avatar
Elliot Lee committed
535

Tim Janik's avatar
Tim Janik committed
536
  gtk_accel_group_detach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
537 538
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
GtkAccelGroup*
gtk_window_get_default_accel_group (GtkWindow *window)
{
  GtkAccelGroup *group;
  
  g_return_val_if_fail (GTK_IS_WINDOW (window), NULL);

  group = gtk_object_get_data (GTK_OBJECT (window),
                               "gtk-accel-group");

  if (group == NULL)
    {
      group = gtk_accel_group_new ();
      gtk_window_add_accel_group (window, group);
      gtk_object_set_data (GTK_OBJECT (window),
                           "gtk-accel-group",
                           group);
      gtk_accel_group_unref (group);
    }

  return group;
}

Elliot Lee's avatar
Elliot Lee committed
562
void
563 564
gtk_window_set_position (GtkWindow         *window,
			 GtkWindowPosition  position)
Elliot Lee's avatar
Elliot Lee committed
565 566 567 568 569 570 571
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  window->position = position;
}

572 573 574 575 576 577 578 579
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)
    {
580 581
      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
	gtk_widget_activate (window->focus_widget);
582 583 584 585 586 587 588 589 590 591 592 593
      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);

594
  if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget))
595 596 597 598 599 600
    {
      gtk_widget_activate (window->default_widget);
      return TRUE;
    }

  return FALSE;
601 602
}

603
void
604 605
gtk_window_set_modal (GtkWindow *window,
		      gboolean   modal)
606 607 608 609
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

610
  window->modal = modal != FALSE;
611

612 613 614 615 616
  /* adjust desired modality state */
  if (GTK_WIDGET_VISIBLE (window) && window->modal)
    gtk_grab_add (GTK_WIDGET (window));
  else
    gtk_grab_remove (GTK_WIDGET (window));
617 618
}

619 620 621 622 623 624 625 626 627 628 629 630
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;
}

631 632 633 634 635 636 637
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));
638

639 640 641 642 643 644 645 646 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 675 676 677 678
  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);
}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
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);
    }
}

707
static void
708
gtk_window_shutdown (GObject *object)
709 710 711 712 713 714 715 716 717 718
{
  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);

719
  G_OBJECT_CLASS (parent_class)->shutdown (object);
720 721
}

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
static void
parent_destroyed_callback (GtkWindow *parent, GtkWindow *child)
{
  gtk_widget_destroy (GTK_WIDGET (child));
}

static void
connect_parent_destroyed (GtkWindow *window)
{
  if (window->transient_parent)
    {
      gtk_signal_connect (GTK_OBJECT (window->transient_parent),
                          "destroy",
                          GTK_SIGNAL_FUNC (parent_destroyed_callback),
                          window);
    }  
}

static void
disconnect_parent_destroyed (GtkWindow *window)
{
  if (window->transient_parent)
    {
      gtk_signal_disconnect_by_func (GTK_OBJECT (window->transient_parent),
                                     GTK_SIGNAL_FUNC (parent_destroyed_callback),
                                     window);
    }
}

751 752 753 754 755
static void
gtk_window_transient_parent_realized (GtkWidget *parent,
				      GtkWidget *window)
{
  if (GTK_WIDGET_REALIZED (window))
756
    gdk_window_set_transient_for (window->window, parent->window);
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
}

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);

783 784 785
      if (window->destroy_with_parent)
        disconnect_parent_destroyed (window);
      
786 787 788 789 790 791 792 793
      window->transient_parent = NULL;
    }
}

void       
gtk_window_set_transient_for  (GtkWindow *window, 
			       GtkWindow *parent)
{
794 795 796
  g_return_if_fail (GTK_IS_WINDOW (window));
  g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
  g_return_if_fail (window != parent);
797

798
    
799 800 801 802 803 804 805
  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));
806 807

      gtk_window_unset_transient_for (window);
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    }

  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);

824 825 826
      if (window->destroy_with_parent)
        connect_parent_destroyed (window);
      
827 828 829 830 831 832 833
      if (GTK_WIDGET_REALIZED (window) &&
	  GTK_WIDGET_REALIZED (parent))
	gtk_window_transient_parent_realized (GTK_WIDGET (parent),
					      GTK_WIDGET (window));
    }
}

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
/**
 * gtk_window_set_destroy_with_parent:
 * @window: a #GtkWindow
 * @setting: whether to destroy @window with its transient parent
 * 
 * If @setting is TRUE, then destroying the transient parent of @window
 * will also destroy @window itself. This is useful for dialogs that
 * shouldn't persist beyond the lifetime of the main window they're
 * associated with, for example.
 **/
void
gtk_window_set_destroy_with_parent  (GtkWindow *window,
                                     gboolean   setting)
{
  g_return_if_fail (GTK_IS_WINDOW (window));

  if (window->destroy_with_parent == (setting != FALSE))
    return;

  if (window->destroy_with_parent)
    {
      disconnect_parent_destroyed (window);
    }
  else
    {
      connect_parent_destroyed (window);
    }
  
  window->destroy_with_parent = setting;
}

865 866 867 868 869 870 871 872 873 874
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);
}

875 876 877
static GtkWindowGeometryInfo*
gtk_window_get_geometry_info (GtkWindow *window,
			      gboolean   create)
878 879 880 881 882 883 884
{
  GtkWindowGeometryInfo *info;

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

  if (!info && create)
    {
885
      info = g_new0 (GtkWindowGeometryInfo, 1);
886

887 888 889 890
      info->width = 0;
      info->height = 0;
      info->last.width = -1;
      info->last.height = -1;
891 892 893 894 895 896 897 898 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
      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;
930 931

  gtk_widget_queue_resize (GTK_WIDGET (window));
932 933 934 935 936 937 938 939 940
}

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

941
  g_return_if_fail (GTK_IS_WINDOW (window));
942 943 944

  info = gtk_window_get_geometry_info (window, TRUE);

945 946 947 948 949 950
  if (width >= 0)
    info->width = width;
  if (height >= 0)
    info->height = height;

  gtk_widget_queue_resize (GTK_WIDGET (window));
951 952
}
  
953 954 955
static void
gtk_window_destroy (GtkObject *object)
{
956 957
  GtkWindow *window;
  
958 959
  g_return_if_fail (GTK_IS_WINDOW (object));

960
  window = GTK_WINDOW (object);
961

962
  if (window->transient_parent)
963
    gtk_window_set_transient_for (window, NULL);
964

965 966 967 968 969 970
  if (window->has_user_ref_count)
    {
      window->has_user_ref_count = FALSE;
      gtk_widget_unref (GTK_WIDGET (window));
    }

971
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
972 973
}

Elliot Lee's avatar
Elliot Lee committed
974
static void
975
gtk_window_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
976 977 978 979 980 981
{
  GtkWindow *window;

  g_return_if_fail (GTK_IS_WINDOW (object));

  window = GTK_WINDOW (object);
982 983 984

  toplevel_list = g_slist_remove (toplevel_list, window);

Elliot Lee's avatar
Elliot Lee committed
985
  g_free (window->title);
Tim Janik's avatar
Tim Janik committed
986 987
  g_free (window->wmclass_name);
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
988

989
  G_OBJECT_CLASS(parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
990 991 992 993 994
}

static void
gtk_window_show (GtkWidget *widget)
{
995 996 997
  GtkWindow *window = GTK_WINDOW (widget);
  GtkContainer *container = GTK_CONTAINER (window);
  gboolean need_resize;
Elliot Lee's avatar
Elliot Lee committed
998 999

  GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

  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
1039
  gtk_widget_map (widget);
1040

1041
  if (window->modal)
1042
    gtk_grab_add (widget);
Elliot Lee's avatar
Elliot Lee committed
1043 1044 1045 1046 1047
}

static void
gtk_window_hide (GtkWidget *widget)
{
1048 1049
  GtkWindow *window;

Elliot Lee's avatar
Elliot Lee committed
1050 1051 1052
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));

1053 1054
  window = GTK_WINDOW (widget);

Elliot Lee's avatar
Elliot Lee committed
1055 1056
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
  gtk_widget_unmap (widget);
1057

1058 1059
  if (window->modal)
    gtk_grab_remove (widget);
Elliot Lee's avatar
Elliot Lee committed
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
}

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);
1091
  gdk_window_withdraw (widget->window);
Elliot Lee's avatar
Elliot Lee committed
1092 1093 1094

  window = GTK_WINDOW (widget);
  window->use_uposition = TRUE;
1095 1096
  window->resize_count = 0;
  window->handling_resize = FALSE;
Elliot Lee's avatar
Elliot Lee committed
1097 1098 1099 1100 1101 1102 1103 1104
}

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

  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));
    }
1132
  
Elliot Lee's avatar
Elliot Lee committed
1133
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1134
  
Elliot Lee's avatar
Elliot Lee committed
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
  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;
    }
1147
   
Elliot Lee's avatar
Elliot Lee committed
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
  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);
1163
   
Elliot Lee's avatar
Elliot Lee committed
1164 1165 1166
  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);
1167
   
Elliot Lee's avatar
Elliot Lee committed
1168 1169 1170 1171 1172
  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);
1173
  gtk_window_paint (widget, NULL);
1174 1175 1176 1177 1178

  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
1179 1180 1181 1182 1183 1184 1185
}

static void
gtk_window_size_request (GtkWidget      *widget,
			 GtkRequisition *requisition)
{
  GtkWindow *window;
1186
  GtkBin *bin;
Elliot Lee's avatar
Elliot Lee committed
1187 1188 1189 1190 1191 1192

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

  window = GTK_WINDOW (widget);
1193 1194 1195 1196
  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
1197

1198
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
Elliot Lee's avatar
Elliot Lee committed
1199
    {
1200 1201 1202
      GtkRequisition child_requisition;
      
      gtk_widget_size_request (bin->child, &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
1203

1204 1205
      requisition->width += child_requisition.width;
      requisition->height += child_requisition.height;
Elliot Lee's avatar
Elliot Lee committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    }
}

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;
1227 1228 1229 1230
      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
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240

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

static gint
gtk_window_configure_event (GtkWidget         *widget,
			    GdkEventConfigure *event)
{
  GtkWindow *window;
1241
  
Elliot Lee's avatar
Elliot Lee committed
1242 1243 1244
  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);
1245
  
1246 1247
  window = GTK_WINDOW (widget);

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
  /* 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)
1269
    {
1270 1271 1272 1273
      if (window->resize_count > 0)
	window->resize_count -= 1;

      if (window->resize_count == 0)
1274
	{
1275 1276 1277 1278 1279 1280
	  window->handling_resize = TRUE;
	  
	  widget->allocation.width = event->width;
	  widget->allocation.height = event->height;
	  
	  gtk_widget_queue_resize (widget);
1281 1282 1283
	}
    }

1284
  return TRUE;
Elliot Lee's avatar
Elliot Lee committed
1285 1286 1287 1288 1289 1290 1291 1292
}

static gint
gtk_window_key_press_event (GtkWidget   *widget,
			    GdkEventKey *event)
{
  GtkWindow *window;
  GtkDirectionType direction = 0;
Tim Janik's avatar
Tim Janik committed
1293
  gboolean handled;
Elliot Lee's avatar
Elliot Lee committed
1294 1295 1296 1297 1298 1299 1300

  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
1301 1302
  handled = FALSE;
  
1303 1304 1305
  if (window->focus_widget &&
      window->focus_widget != widget &&
      GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
Tim Janik's avatar
Tim Janik committed
1306 1307 1308 1309 1310 1311
    {
      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
1312

Tim Janik's avatar
Tim Janik committed
1313
  if (!handled)
Elliot Lee's avatar
Elliot Lee committed
1314 1315 1316 1317 1318 1319
    {
      switch (event->keyval)
	{
	case GDK_space:
	  if (window->focus_widget)
	    {
1320 1321
	      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
		gtk_widget_activate (window->focus_widget);
Tim Janik's avatar
Tim Janik committed
1322
	      handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1323 1324 1325 1326
	    }
	  break;
	case GDK_Return:
	case GDK_KP_Enter:
1327 1328
	  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
1329 1330
	    {
	      gtk_widget_activate (window->default_widget);
Tim Janik's avatar
Tim Janik committed
1331
	      handled = TRUE;
Elliot Lee's avatar
Elliot Lee committed
1332