gtkwindow.c 89.9 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"
46
#include "gtkwindow-decorate.h"
Tim Janik's avatar
Tim Janik committed
47
#include "gtkbindings.h"
48
#include "gtkmain.h"
49
#include "gtkiconfactory.h"
Elliot Lee's avatar
Elliot Lee committed
50

51 52 53 54 55
/* 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
56
enum {
57
  SET_FOCUS,
58
  FRAME_EVENT,
Elliot Lee's avatar
Elliot Lee committed
59 60
  LAST_SIGNAL
};
61

62 63 64 65 66 67
enum {
  ARG_0,
  ARG_TYPE,
  ARG_TITLE,
  ARG_AUTO_SHRINK,
  ARG_ALLOW_SHRINK,
68
  ARG_ALLOW_GROW,
69
  ARG_MODAL,
70 71
  ARG_WIN_POS,
  ARG_DEFAULT_WIDTH,
72 73
  ARG_DEFAULT_HEIGHT,
  ARG_DESTROY_WITH_PARENT
74
};
Elliot Lee's avatar
Elliot Lee committed
75

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

Elliot Lee's avatar
Elliot Lee committed
95 96
static void gtk_window_class_init         (GtkWindowClass    *klass);
static void gtk_window_init               (GtkWindow         *window);
97
static void gtk_window_set_arg            (GtkObject         *object,
98 99
					   GtkArg            *arg,
					   guint	      arg_id);
100
static void gtk_window_get_arg            (GtkObject         *object,
101 102
					   GtkArg            *arg,
					   guint	      arg_id);
103
static void gtk_window_shutdown           (GObject           *object);
104
static void gtk_window_destroy            (GtkObject         *object);
105
static void gtk_window_finalize           (GObject           *object);
Elliot Lee's avatar
Elliot Lee committed
106 107 108 109 110
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);
111
static void gtk_window_unrealize          (GtkWidget         *widget);
Elliot Lee's avatar
Elliot Lee committed
112 113 114 115
static void gtk_window_size_request       (GtkWidget         *widget,
					   GtkRequisition    *requisition);
static void gtk_window_size_allocate      (GtkWidget         *widget,
					   GtkAllocation     *allocation);
116 117 118 119
static gint gtk_window_event              (GtkWidget *widget,
					   GdkEvent *event);
static gboolean gtk_window_frame_event    (GtkWidget *widget,
					   GdkEvent *event);
Elliot Lee's avatar
Elliot Lee committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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,
135
					   GdkEventClient    *event);
136
static void gtk_window_check_resize       (GtkContainer      *container);
137 138
static gint gtk_window_focus              (GtkContainer     *container,
				           GtkDirectionType  direction);
139
static void gtk_window_real_set_focus     (GtkWindow         *window,
140
					   GtkWidget         *focus);
141

142
static void gtk_window_move_resize        (GtkWindow         *window);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
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
165

166 167
static void gtk_window_read_rcfiles       (GtkWidget         *widget,
					   GdkEventClient    *event);
168 169
static void gtk_window_paint              (GtkWidget         *widget,
					   GdkRectangle      *area);
Owen Taylor's avatar
Owen Taylor committed
170 171
static gint gtk_window_expose             (GtkWidget         *widget,
				           GdkEventExpose    *event);
172 173 174 175 176 177 178 179 180
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
181

182

183
static GSList      *toplevel_list = NULL;
Elliot Lee's avatar
Elliot Lee committed
184
static GtkBinClass *parent_class = NULL;
185
static guint        window_signals[LAST_SIGNAL] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
186 187


188
GtkType
189
gtk_window_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
190
{
191
  static GtkType window_type = 0;
Elliot Lee's avatar
Elliot Lee committed
192 193 194

  if (!window_type)
    {
195
      static const GtkTypeInfo window_info =
Elliot Lee's avatar
Elliot Lee committed
196 197 198 199 200 201
      {
	"GtkWindow",
	sizeof (GtkWindow),
	sizeof (GtkWindowClass),
	(GtkClassInitFunc) gtk_window_class_init,
	(GtkObjectInitFunc) gtk_window_init,
202 203
        /* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
204
	(GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
205 206 207 208 209 210 211 212 213 214 215
      };

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

  return window_type;
}

static void
gtk_window_class_init (GtkWindowClass *klass)
{
216
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Elliot Lee's avatar
Elliot Lee committed
217 218 219 220 221 222 223 224 225 226
  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 ());

227 228 229
  gobject_class->shutdown = gtk_window_shutdown;
  gobject_class->finalize = gtk_window_finalize;

230 231
  object_class->set_arg = gtk_window_set_arg;
  object_class->get_arg = gtk_window_get_arg;
232
  object_class->destroy = gtk_window_destroy;
Elliot Lee's avatar
Elliot Lee committed
233 234 235 236 237 238

  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;
239
  widget_class->unrealize = gtk_window_unrealize;
Elliot Lee's avatar
Elliot Lee committed
240 241 242 243 244 245 246 247 248 249
  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;
250
  
251 252
  widget_class->expose_event = gtk_window_expose;
   
253
  container_class->check_resize = gtk_window_check_resize;
254
  container_class->focus = gtk_window_focus;
Elliot Lee's avatar
Elliot Lee committed
255

256
  klass->set_focus = gtk_window_real_set_focus;
257
  klass->frame_event = gtk_window_frame_event;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

  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);
  gtk_object_add_arg_type ("GtkWindow::modal", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_MODAL);
  gtk_object_add_arg_type ("GtkWindow::window_position", GTK_TYPE_WINDOW_POSITION, GTK_ARG_READWRITE, ARG_WIN_POS);
  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);
  gtk_object_add_arg_type ("GtkWindow::destroy_with_parent", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_DESTROY_WITH_PARENT);
  
  window_signals[SET_FOCUS] =
    gtk_signal_new ("set_focus",
                    GTK_RUN_LAST,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkWindowClass, set_focus),
Tim Janik's avatar
Tim Janik committed
275
                    gtk_marshal_VOID__OBJECT,
276 277
		    GTK_TYPE_NONE, 1,
                    GTK_TYPE_WIDGET);
278 279 280 281 282 283
  
  window_signals[FRAME_EVENT] =
    gtk_signal_new ("frame_event",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkWindowClass, frame_event),
Tim Janik's avatar
Tim Janik committed
284
		    gtk_marshal_BOOLEAN__BOXED,
285 286
		    GTK_TYPE_BOOL, 1,
		    GTK_TYPE_GDK_EVENT);
Elliot Lee's avatar
Elliot Lee committed
287 288 289 290 291 292
}

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

Owen Taylor's avatar
Owen Taylor committed
295 296
  GTK_PRIVATE_SET_FLAG (window, GTK_ANCHORED);

297 298
  gtk_container_set_resize_mode (GTK_CONTAINER (window), GTK_RESIZE_QUEUE);

Elliot Lee's avatar
Elliot Lee committed
299
  window->title = NULL;
300
  window->wmclass_name = g_strdup (g_get_prgname ());
301
  window->wmclass_class = g_strdup (gdk_progclass);
Elliot Lee's avatar
Elliot Lee committed
302 303 304 305 306 307 308 309 310 311
  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;
312
  window->modal = FALSE;
313 314 315 316 317 318
  window->frame = NULL;
  window->has_frame = FALSE;
  window->frame_left = 0;
  window->frame_right = 0;
  window->frame_top = 0;
  window->frame_bottom = 0;
319
  window->type_hint = GDK_WINDOW_TYPE_HINT_NORMAL;
320 321
  window->decorated = TRUE;
  
322 323 324 325
  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);
326 327 328 329 330 331 332

  gtk_decorated_window_init (window);

  gtk_signal_connect (GTK_OBJECT (window),
		      "event",
		      GTK_SIGNAL_FUNC (gtk_window_event),
		      NULL);
Elliot Lee's avatar
Elliot Lee committed
333 334 335
}

static void
336
gtk_window_set_arg (GtkObject  *object,
337 338
		    GtkArg     *arg,
		    guint	arg_id)
Elliot Lee's avatar
Elliot Lee committed
339
{
340 341 342 343
  GtkWindow  *window;

  window = GTK_WINDOW (object);

344
  switch (arg_id)
Elliot Lee's avatar
Elliot Lee committed
345
    {
346
    case ARG_TYPE:
347
      window->type = GTK_VALUE_ENUM (*arg);
348 349
      break;
    case ARG_TITLE:
350
      gtk_window_set_title (window, GTK_VALUE_STRING (*arg));
351 352
      break;
    case ARG_AUTO_SHRINK:
353
      window->auto_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
354
      gtk_widget_queue_resize (GTK_WIDGET (window));
355 356
      break;
    case ARG_ALLOW_SHRINK:
357
      window->allow_shrink = (GTK_VALUE_BOOL (*arg) != FALSE);
358
      gtk_widget_queue_resize (GTK_WIDGET (window));
359 360
      break;
    case ARG_ALLOW_GROW:
361
      window->allow_grow = (GTK_VALUE_BOOL (*arg) != FALSE);
362
      gtk_widget_queue_resize (GTK_WIDGET (window));
363
      break;
364 365 366
    case ARG_MODAL:
      gtk_window_set_modal (window, GTK_VALUE_BOOL (*arg));
      break;
367
    case ARG_WIN_POS:
368
      gtk_window_set_position (window, GTK_VALUE_ENUM (*arg));
369
      break;
370 371 372 373 374 375
    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;
376 377 378
    case ARG_DESTROY_WITH_PARENT:
      gtk_window_set_destroy_with_parent (window, GTK_VALUE_BOOL (*arg));
      break;
Tim Janik's avatar
Tim Janik committed
379 380
    default:
      break;
Elliot Lee's avatar
Elliot Lee committed
381 382 383
    }
}

384
static void
385
gtk_window_get_arg (GtkObject  *object,
386 387 388
		    GtkArg     *arg,
		    guint	arg_id)
{
389 390 391 392
  GtkWindow  *window;

  window = GTK_WINDOW (object);

393 394
  switch (arg_id)
    {
395
      GtkWindowGeometryInfo *info;
396
    case ARG_TYPE:
397
      GTK_VALUE_ENUM (*arg) = window->type;
398 399
      break;
    case ARG_TITLE:
400
      GTK_VALUE_STRING (*arg) = g_strdup (window->title);
401 402
      break;
    case ARG_AUTO_SHRINK:
403
      GTK_VALUE_BOOL (*arg) = window->auto_shrink;
404 405
      break;
    case ARG_ALLOW_SHRINK:
406
      GTK_VALUE_BOOL (*arg) = window->allow_shrink;
407 408
      break;
    case ARG_ALLOW_GROW:
409 410
      GTK_VALUE_BOOL (*arg) = window->allow_grow;
      break;
411 412 413
    case ARG_MODAL:
      GTK_VALUE_BOOL (*arg) = window->modal;
      break;
414 415
    case ARG_WIN_POS:
      GTK_VALUE_ENUM (*arg) = window->position;
416
      break;
417 418 419 420 421 422 423 424 425 426 427 428 429 430
    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;
431 432 433
    case ARG_DESTROY_WITH_PARENT:
      GTK_VALUE_BOOL (*arg) = window->destroy_with_parent;
      break;
434 435 436 437 438 439
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

440 441 442 443 444 445 446 447
/**
 * gtk_window_new:
 * @type: type of window
 * 
 * Creates a new #GtkWindow, which is a toplevel window that can
 * contain other widgets. Nearly always, the type of the window should
 * be #GTK_WINDOW_TOPLEVEL. If you're implementing something like a
 * popup menu from scratch (which is a bad idea, just use #GtkMenu),
448 449 450 451 452 453 454
 * you might use #GTK_WINDOW_POPUP. #GTK_WINDOW_POPUP is not for
 * dialogs, though in some other toolkits dialogs are called "popups."
 * In GTK+, #GTK_WINDOW_POPUP means a pop-up menu or pop-up tooltip.
 * Popup windows are not controlled by the window manager.
 *
 * If you simply want an undecorated window (no window borders), use
 * gtk_window_set_decorated(), don't use #GTK_WINDOW_POPUP.
455 456 457
 * 
 * Return value: a new #GtkWindow.
 **/
Elliot Lee's avatar
Elliot Lee committed
458 459 460 461 462
GtkWidget*
gtk_window_new (GtkWindowType type)
{
  GtkWindow *window;

463 464 465
  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
466 467 468 469 470 471

  window->type = type;

  return GTK_WIDGET (window);
}

472 473 474 475 476 477 478 479 480 481 482 483 484
/**
 * gtk_window_set_title:
 * @window: a #GtkWindow
 * @title: title of the window
 * 
 * Sets the title of the #GtkWindow. The title of a window will be displayed in
 * its title bar; on the X Window System, the title bar is rendered by the
 * window manager, so exactly how the title appears to users may vary according
 * to a user's exact configuration. The title should help a user distinguish
 * this window from other windows they may have open. A good title might
 * include the application name and current document filename, for example.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
485 486 487 488 489 490 491 492 493 494 495 496
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))
497 498 499 500 501
    {
      gdk_window_set_title (GTK_WIDGET (window)->window, window->title);

      gtk_decorated_window_set_title (window, title);
    }
Elliot Lee's avatar
Elliot Lee committed
502 503
}

504 505 506 507 508 509
/**
 * gtk_window_set_wmclass:
 * @window: a #GtkWindow
 * @wmclass_name: window name hint
 * @wmclass_class: window class hint
 *
510 511 512 513 514 515 516 517
 * Don't use this function. It sets the X Window System "class" and
 * "name" hints for a window.  According to the ICCCM, you should
 * always set these to the same value for all windows in an
 * application, and GTK sets them to that value by default, so calling
 * this function is sort of pointless. However, you may want to call
 * gtk_window_set_role() on each window in your application, for the
 * benefit of the session manager. Setting the role allows the window
 * manager to restore window positions when loading a saved session.
518 519
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
520 521
void
gtk_window_set_wmclass (GtkWindow *window,
522 523
			const gchar *wmclass_name,
			const gchar *wmclass_class)
Elliot Lee's avatar
Elliot Lee committed
524 525 526 527
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

Tim Janik's avatar
Tim Janik committed
528
  g_free (window->wmclass_name);
Elliot Lee's avatar
Elliot Lee committed
529 530
  window->wmclass_name = g_strdup (wmclass_name);

Tim Janik's avatar
Tim Janik committed
531
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
532 533 534
  window->wmclass_class = g_strdup (wmclass_class);

  if (GTK_WIDGET_REALIZED (window))
535 536 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 562 563 564 565 566 567
    g_warning ("gtk_window_set_wmclass: shouldn't set wmclass after window is realized!\n");
}

/**
 * gtk_window_set_role:
 * @window: a #GtkWindow
 * @role: unique identifier for the window to be used when restoring a session
 *
 * In combination with the window title, the window role allows a
 * window manager to identify "the same" window when an application is
 * restarted. So for example you might set the "toolbox" role on your
 * app's toolbox window, so that when the user restarts their session,
 * the window manager can put the toolbox back in the same place.
 *
 * If a window already has a unique title, you don't need to set the
 * role, since the WM can use the title to identify the window when
 * restoring the session.
 * 
 **/
void
gtk_window_set_role (GtkWindow   *window,
                     const gchar *role)
{
  g_return_if_fail (GTK_IS_WINDOW (window));

  if (role == window->wm_role)
    return;
  
  g_free (window->wm_role);
  window->wm_role = g_strdup (role);
  
  if (GTK_WIDGET_REALIZED (window))
    g_warning ("gtk_window_set_role(): shouldn't set role after window is realized!\n");
Elliot Lee's avatar
Elliot Lee committed
568 569
}

570 571 572 573 574 575 576 577 578 579 580 581
/**
 * gtk_window_set_focus:
 * @window: a #GtkWindow
 * @focus: widget to be the new focus widget
 *
 * If @focus is not the current focus widget, and is focusable, emits
 * the "set_focus" signal to set @focus as the focus widget for the
 * window.  This function is more or less GTK-internal; to focus an
 * entry widget or the like, you should use gtk_widget_grab_focus()
 * instead of this function.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
582 583 584 585
void
gtk_window_set_focus (GtkWindow *window,
		      GtkWidget *focus)
{
586 587 588 589 590 591 592 593
  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));
    }

594 595
  if ((window->focus_widget != focus) ||
      (focus && !GTK_WIDGET_HAS_FOCUS (focus)))
596
    gtk_signal_emit (GTK_OBJECT (window), window_signals[SET_FOCUS], focus);
Elliot Lee's avatar
Elliot Lee committed
597 598
}

599 600 601 602 603 604 605 606 607 608 609 610 611 612
/**
 * gtk_window_set_default:
 * @window: a #GtkWindow
 * @default_widget: widget to be the default
 *
 * The default widget is the widget that's activated when the user
 * presses Enter in a dialog (for example). This function tells a
 * #GtkWindow about the current default widget; it's really a GTK
 * internal function and you shouldn't need it. Instead, to change the
 * default widget, first set the #GTK_CAN_DEFAULT flag on the widget
 * you'd like to make the default using GTK_WIDGET_SET_FLAGS(), then
 * call gtk_widget_grab_default() to move the default.
 * 
 **/
Elliot Lee's avatar
Elliot Lee committed
613 614
void
gtk_window_set_default (GtkWindow *window,
615
			GtkWidget *default_widget)
Elliot Lee's avatar
Elliot Lee committed
616 617 618 619
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

620 621 622 623
  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
624 625 626
    {
      if (window->default_widget)
	{
627 628 629
	  if (window->focus_widget != window->default_widget ||
	      !GTK_WIDGET_RECEIVES_DEFAULT (window->default_widget))
	    GTK_WIDGET_UNSET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
630
	  gtk_widget_queue_draw (window->default_widget);
Elliot Lee's avatar
Elliot Lee committed
631 632
	}

633
      window->default_widget = default_widget;
Elliot Lee's avatar
Elliot Lee committed
634 635 636

      if (window->default_widget)
	{
637 638 639
	  if (window->focus_widget == NULL ||
	      !GTK_WIDGET_RECEIVES_DEFAULT (window->focus_widget))
	    GTK_WIDGET_SET_FLAGS (window->default_widget, GTK_HAS_DEFAULT);
640
	  gtk_widget_queue_draw (window->default_widget);
Elliot Lee's avatar
Elliot Lee committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
	}
    }
}

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
657

658
  gtk_widget_queue_resize (GTK_WIDGET (window));
Elliot Lee's avatar
Elliot Lee committed
659 660 661
}

void
Tim Janik's avatar
Tim Janik committed
662 663
gtk_window_add_accel_group (GtkWindow        *window,
			    GtkAccelGroup    *accel_group)
Elliot Lee's avatar
Elliot Lee committed
664 665 666
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
Tim Janik's avatar
Tim Janik committed
667
  g_return_if_fail (accel_group != NULL);
Elliot Lee's avatar
Elliot Lee committed
668

Tim Janik's avatar
Tim Janik committed
669
  gtk_accel_group_attach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
670 671 672
}

void
Tim Janik's avatar
Tim Janik committed
673 674
gtk_window_remove_accel_group (GtkWindow       *window,
			       GtkAccelGroup   *accel_group)
Elliot Lee's avatar
Elliot Lee committed
675 676 677
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));
Tim Janik's avatar
Tim Janik committed
678
  g_return_if_fail (accel_group != NULL);
Elliot Lee's avatar
Elliot Lee committed
679

Tim Janik's avatar
Tim Janik committed
680
  gtk_accel_group_detach (accel_group, GTK_OBJECT (window));
Elliot Lee's avatar
Elliot Lee committed
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
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
706
void
707 708
gtk_window_set_position (GtkWindow         *window,
			 GtkWindowPosition  position)
Elliot Lee's avatar
Elliot Lee committed
709 710 711 712 713 714 715
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

  window->position = position;
}

716 717 718 719 720 721 722 723
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)
    {
724 725
      if (GTK_WIDGET_IS_SENSITIVE (window->focus_widget))
	gtk_widget_activate (window->focus_widget);
726 727 728 729 730 731 732 733 734 735 736 737
      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);

738
  if (window->default_widget && GTK_WIDGET_IS_SENSITIVE (window->default_widget))
739 740 741 742 743 744
    {
      gtk_widget_activate (window->default_widget);
      return TRUE;
    }

  return FALSE;
745 746
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760
/**
 * gtk_window_set_modal:
 * @window: a #GtkWindow
 * @modal: whether the window is modal
 * 
 * Sets a window modal or non-modal. Modal windows prevent interaction
 * with other windows in the same application. To keep modal dialogs
 * on top of main application windows, use
 * gtk_window_set_transient_for() to make the dialog transient for the
 * parent; most window managers will then disallow lowering the dialog
 * below the parent.
 * 
 * 
 **/
761
void
762 763
gtk_window_set_modal (GtkWindow *window,
		      gboolean   modal)
764 765 766 767
{
  g_return_if_fail (window != NULL);
  g_return_if_fail (GTK_IS_WINDOW (window));

768
  window->modal = modal != FALSE;
769

770 771 772 773 774
  /* adjust desired modality state */
  if (GTK_WIDGET_VISIBLE (window) && window->modal)
    gtk_grab_add (GTK_WIDGET (window));
  else
    gtk_grab_remove (GTK_WIDGET (window));
775 776
}

777 778 779 780 781 782 783 784 785
/**
 * gtk_window_list_toplevels:
 * 
 * Returns a list of all existing toplevel windows. Each widget
 * in the list has a reference added to it; to free the
 * list, first unref each widget in the list, then free the list.
 * 
 * Return value: list of referenced toplevel widgets
 **/
786 787 788 789 790 791 792 793 794 795 796 797
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;
}

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

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  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);
}

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
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);
	}
869 870 871 872 873

      if (window->frame)
	gdk_window_move (window->frame,	 x - window->frame_left, y - window->frame_top);
      else
	gdk_window_move (GTK_WIDGET (window)->window, x, y);
874 875 876
    }
}

877
static void
878
gtk_window_shutdown (GObject *object)
879 880 881 882 883 884 885 886 887 888
{
  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);

889
  G_OBJECT_CLASS (parent_class)->shutdown (object);
890 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
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);
    }
}

921 922 923 924 925
static void
gtk_window_transient_parent_realized (GtkWidget *parent,
				      GtkWidget *window)
{
  if (GTK_WIDGET_REALIZED (window))
926
    gdk_window_set_transient_for (window->window, parent->window);
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
}

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

953 954 955
      if (window->destroy_with_parent)
        disconnect_parent_destroyed (window);
      
956 957 958 959
      window->transient_parent = NULL;
    }
}

960 961 962 963 964 965 966 967 968 969
/**
 * gtk_window_set_transient_for:
 * @window: a #GtkWindow
 * @parent: parent window
 *
 * Dialog windows should be set transient for the main application
 * window they were spawned from. This allows window managers to
 * e.g. keep the dialog on top of the main window, or center the
 * dialog over the main window. gtk_dialog_new_with_buttons() and
 * other convenience functions in GTK+ will sometimes call
970
 * gtk_window_set_transient_for() on your behalf.
971 972
 * 
 **/
973 974 975 976
void       
gtk_window_set_transient_for  (GtkWindow *window, 
			       GtkWindow *parent)
{
977 978 979
  g_return_if_fail (GTK_IS_WINDOW (window));
  g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
  g_return_if_fail (window != parent);
980

981
    
982 983 984 985 986 987 988
  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));
989 990

      gtk_window_unset_transient_for (window);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    }

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

1007 1008 1009
      if (window->destroy_with_parent)
        connect_parent_destroyed (window);
      
1010 1011 1012 1013 1014 1015 1016
      if (GTK_WIDGET_REALIZED (window) &&
	  GTK_WIDGET_REALIZED (parent))
	gtk_window_transient_parent_realized (GTK_WIDGET (parent),
					      GTK_WIDGET (window));
    }
}

1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
/**
 * gtk_window_set_type_hint:
 * @window: a #GtkWindow
 * @hint: the window type
 *
 * By setting the type hint for the window, you allow the window
 * manager to decorate and handle the window in a way which is
 * suitable to the function of the window in your application.
 *
 * This function should be called before the window becomes visible.
 *
 * gtk_dialog_new_with_buttons() and other convenience functions in GTK+
 * will sometimes call gtk_window_set_type_hint() on your behalf.
 * 
 **/
void
gtk_window_set_type_hint (GtkWindow           *window, 
			  GdkWindowTypeHint    hint)
{
  g_return_if_fail (GTK_IS_WINDOW (window));
  g_return_if_fail (!GTK_WIDGET_VISIBLE (window));
  window->type_hint = hint;
}

1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
/**
 * 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;
}

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
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);
}

1082 1083 1084
static GtkWindowGeometryInfo*
gtk_window_get_geometry_info (GtkWindow *window,
			      gboolean   create)
1085 1086 1087 1088 1089 1090 1091
{
  GtkWindowGeometryInfo *info;

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

  if (!info && create)
    {
1092
      info = g_new0 (GtkWindowGeometryInfo, 1);
1093

1094 1095 1096 1097
      info->width = 0;
      info->height = 0;
      info->last.width = -1;
      info->last.height = -1;
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
      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;
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/**
 * gtk_window_set_geometry_hints:
 * @window: a #GdkWindow
 * @geometry_widget: widget the geometry hints will be applied to
 * @geometry: struct containing geometry information
 * @geom_mask: mask indicating which struct fields should be paid attention to
 *
 * This function sets up hints about how a window can be resized by
 * the user.  You can set a minimum and maximum size; allowed resize
 * increments (e.g. for xterm, you can only resize by the size of a
 * character); aspect ratios; and more. See the #GdkGeometry struct.
 * 
 **/
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
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;
1150 1151

  gtk_widget_queue_resize (GTK_WIDGET (window));
1152 1153
}

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
/**
 * gtk_window_set_decorated:
 * @window: a #GtkWindow
 * @setting: %TRUE to decorate the window
 *
 * By default, windows are decorated with a title bar, resize
 * controls, etc.  Some window managers allow GTK+ to disable these
 * decorations, creating a borderless window. If you set the decorated
 * property to %FALSE using this function, GTK+ will do its best to
 * convince the window manager not to decorate the window.
 * 
 **/
void
gtk_window_set_decorated (GtkWindow *window,
                          gboolean   setting)
{
  g_return_if_fail (GTK_IS_WINDOW (window));

  setting = setting != FALSE;

  if (setting == window->decorated)
    return;

  if (GTK_WIDGET (window)->window)
    {
      if (window->decorated)
        gdk_window_set_decorations (GTK_WIDGET (window)->window,
                                    GDK_DECOR_ALL);
      else
        gdk_window_set_decorations (GTK_WIDGET (window)->window,
                                    0);
    }
}

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
/**
 * gtk_window_set_default_size:
 * @window: a #GtkWindow
 * @width: width in pixels, or -1 to leave the default width unchanged
 * @height: height in pixels, or -1 to leave the default height unchanged
 *
 * Sets the default size of a window. If the window's "natural" size
 * (its size request) is larger than the default, the default will be
 * ignored. So the default size is a minimum initial size.  Unlike
 * gtk_widget_set_usize(), which sets a size request for a widget and
 * thus would keep users from shrinking the window, this function only
 * sets the initial size, just as if the user had resized the window
 * themselves. Users can still shrink the window again as they
 * normally would.
 *
 * For more control over a window's initial size and how resizing works,
 * investigate gtk_window_set_geometry_hints().
1205 1206 1207 1208 1209
 *
 * A useful feature: if you set the "geometry widget" via
 * gtk_window_set_geometry_hints(), the default size specified by
 * gtk_window_set_default_size() will be the default size of that
 * widget, not of the entire window.
1210 1211
 * 
 **/
1212 1213 1214 1215 1216 1217 1218
void       
gtk_window_set_default_size (GtkWindow   *window,
			     gint         width,
			     gint         height)
{
  GtkWindowGeometryInfo *info;

1219
  g_return_if_fail (GTK_IS_WINDOW (window));
1220 1221 1222

  info = gtk_window_get_geometry_info (window, TRUE);

1223 1224 1225 1226 1227 1228
  if (width >= 0)
    info->width = width;
  if (height >= 0)
    info->height = height;

  gtk_widget_queue_resize (GTK_WIDGET (window));
1229 1230
}
  
1231 1232 1233
static void
gtk_window_destroy (GtkObject *object)
{
1234 1235
  GtkWindow *window;
  
1236 1237
  g_return_if_fail (GTK_IS_WINDOW (object));

1238
  window = GTK_WINDOW (object);
1239

1240
  if (window->transient_parent)
1241
    gtk_window_set_transient_for (window, NULL);
1242

1243 1244 1245 1246 1247 1248
  if (window->has_user_ref_count)
    {
      window->has_user_ref_count = FALSE;
      gtk_widget_unref (GTK_WIDGET (window));
    }

1249
  GTK_OBJECT_CLASS (parent_class)->destroy (object);
1250 1251
}

Elliot Lee's avatar
Elliot Lee committed
1252
static void
1253
gtk_window_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
1254 1255 1256 1257 1258 1259
{
  GtkWindow *window;

  g_return_if_fail (GTK_IS_WINDOW (object));

  window = GTK_WINDOW (object);
1260 1261 1262

  toplevel_list = g_slist_remove (toplevel_list, window);

Elliot Lee's avatar
Elliot Lee committed
1263
  g_free (window->title);
Tim Janik's avatar
Tim Janik committed
1264 1265
  g_free (window->wmclass_name);
  g_free (window->wmclass_class);
Elliot Lee's avatar
Elliot Lee committed
1266

1267
  G_OBJECT_CLASS(parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
1268 1269 1270 1271 1272
}

static void
gtk_window_show (GtkWidget *widget)
{
1273 1274 1275
  GtkWindow *window = GTK_WINDOW (widget);
  GtkContainer *container = GTK_CONTAINER (window);
  gboolean need_resize;
1276 1277
  gboolean was_realized;
  
Elliot Lee's avatar
Elliot Lee committed
1278
  GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
1279
  
1280 1281
  need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
  container->need_resize = FALSE;
1282
  
1283 1284 1285 1286 1287 1288
  if (need_resize)
    {
      GtkWindowGeometryInfo *info = gtk_window_get_geometry_info (window, TRUE);
      GtkAllocation allocation = { 0, 0 };
      GdkGeometry new_geometry;
      guint width, height, new_flags;
1289
      
1290 1291 1292
      /* determine default size to initially show the window with */
      gtk_widget_size_request (widget, NULL);
      gtk_window_compute_default_size (window, &width, &height);
1293
      
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
      /* 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);
1304
      
1305 1306 1307 1308 1309
      /* and allocate the window */
      allocation.width  = width;
      allocation.height = height;
      gtk_widget_size_allocate (widget, &allocation);
      
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321
      was_realized = FALSE;
      if (!GTK_WIDGET_REALIZED (widget))
	{
	  gtk_widget_realize (widget);
	  was_realized = TRUE;;
	}

      /* Must be done after the windows are realized,
	 so that the decorations can be read */
      gtk_decorated_window_calculate_frame_size (window);
      
      if (!was_realized)
1322 1323 1324 1325 1326
	gdk_window_resize (widget->window, width, height);
    }
  
  gtk_container_check_resize (container);

Elliot Lee's avatar
Elliot Lee committed
1327
  gtk_widget_map (widget);
1328

1329
  if (window->modal)
1330
    gtk_grab_add (widget);
Elliot Lee's avatar
Elliot Lee committed
1331 1332 1333 1334 1335
}

static void
gtk_window_hide (GtkWidget *widget)
{
1336 1337
  GtkWindow *window;

Elliot Lee's avatar
Elliot Lee committed
1338 1339 1340
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_WINDOW (widget));

1341 1342
  window = GTK_WINDOW (widget);

Elliot Lee's avatar
Elliot Lee committed
1343 1344
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
  gtk_widget_unmap (widget);
1345

1346 1347
  if (window->modal)
    gtk_grab_remove (widget);
Elliot Lee's avatar
Elliot Lee committed
1348 1349 1350 1351 1352 1353
}

static void
gtk_window_map (GtkWidget *widget)
{
  GtkWindow *window;
1354 1355
  GdkWindow *toplevel;
  
Elliot Lee's avatar
Elliot Lee committed
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
  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);

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
  if (window->frame)
    toplevel = window->frame;
  else
    toplevel = widget->window;
  
  if (window->maximize_initially)
    gdk_window_maximize (toplevel);
  else
    gdk_window_unmaximize (toplevel);
  
  if (window->stick_initially)
    gdk_window_stick (toplevel);
  else
    gdk_window_unstick (toplevel);
  
  if (window->iconify_initially)
    gdk_window_iconify (toplevel);
  else
    gdk_window_deiconify (toplevel);
  
Elliot Lee's avatar
Elliot Lee committed
1388
  gdk_window_show (widget->window);
1389

1390 1391
  if (window->frame)
    gdk_window_show (window->frame);
Elliot Lee's avatar
Elliot Lee committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
}

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

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

1402 1403
  window = GTK_WINDOW (widget);
  
Elliot Lee's avatar
Elliot Lee committed
1404
  GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
1405 1406 1407 1408
  if (window->frame)
    gdk_window_withdraw (window->frame);
  else 
    gdk_window_withdraw (widget->window);
Elliot Lee's avatar
Elliot Lee committed
1409 1410

  window->use_uposition = TRUE;
1411 1412
  window->resize_count = 0;
  window->handling_resize = FALSE;
1413

Elliot Lee's avatar
Elliot Lee committed
1414 1415 1416 1417 1418 1419
}

static void
gtk_window_realize (GtkWidget *widget)
{
  GtkWindow *window;
1420
  GdkWindow *parent_window;
Elliot Lee's avatar
Elliot Lee committed
1421 1422
  GdkWindowAttr attributes;
  gint attributes_mask;
1423
  
Elliot Lee's avatar
Elliot Lee committed
1424
  g_return_if_fail (GTK_IS_WINDOW (widget));
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

  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));
    }
1450
  
Elliot Lee's avatar
Elliot Lee committed
1451
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1452
  
Elliot Lee's avatar
Elliot Lee committed
1453 1454 1455 1456 1457 1458 1459 1460
  switch (window->type)
    {
    case GTK_WINDOW_TOPLEVEL:
      attributes.window_type = GDK_WINDOW_TOPLEVEL;
      break;
    case GTK_WINDOW_POPUP:
      attributes.window_type = GDK_WINDOW_TEMP;
      break;
1461 1462 1463
    default:
      g_warning (G_STRLOC": Unknown window type %d!", window->type);
      break;
Elliot Lee's avatar
Elliot Lee committed
1464
    }
1465
   
Elliot Lee's avatar
Elliot Lee committed
1466 1467 1468 1469 1470 1471
  attributes.title = window->title;
  attributes.wmclass_name = window->wmclass_name;
  attributes.wmclass_class = window->wmclass_class;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

  if (window->has_frame)
    {
      attributes.width = widget->allocation.width + window->frame_left + window->frame_right;
      attributes.height = widget->allocation.height + window->frame_top + window->frame_bottom;
      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 |
			       GDK_BUTTON_MOTION_MASK |
			       GDK_POINTER_MOTION_HINT_MASK |
			       GDK_BUTTON_PRESS_MASK |
			       GDK_BUTTON_RELEASE_MASK);
      
      attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
      
      window->frame = gdk_window_new (NULL, &attributes, attributes_mask);
      gdk_window_set_user_data (window->frame, widget);
      
      attributes.window_type = GDK_WINDOW_CHILD;
      attributes.x = window->frame_left;
      attributes.y = window->frame_right;
    
      attributes_mask = GDK_WA_X | GDK_WA_Y;

      parent_window = window->frame;
    }
  else
    {
      attributes_mask = 0;
      parent_window = NULL;
    }
  
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
Elliot Lee's avatar
Elliot Lee committed
1509 1510 1511 1512 1513 1514 1515
  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);
1516 1517

  attributes_mask |= GDK_WA_VISUAL | GDK_WA_COLORMAP;
Elliot Lee's avatar
Elliot Lee committed
1518 1519
  attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
  attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
1520
  widget->window = gdk_window_new (parent_window, &attributes, attributes_mask);
Elliot Lee's avatar
Elliot Lee committed
1521
  gdk_window_set_user_data (widget->window, window);
1522
      
Elliot Lee's avatar
Elliot Lee committed
1523 1524
  widget->style = gtk_style_attach (widget->style, widget->window);
  gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1525 1526
  if (window->frame)
    gtk_style_set_background (widget->style, window->frame, GTK_STATE_NORMAL);
1527 1528

  /* This is a bad hack to set the window background. */
1529
  gtk_window_paint (widget, NULL);
1530
  
1531 1532 1533 1534
  if (window->transient_parent &&
      GTK_WIDGET_REALIZED (window->transient_parent))
    gdk_window_set_transient_for (widget->window,
				  GTK_WIDGET (window->transient_parent)->window);
1535

1536 1537 1538 1539 1540 1541
  if (window->wm_role)
    gdk_window_set_role (widget->window, window->wm_role);
  
  if (!window->decorated)
    gdk_window_set_decorations (widget->window, 0);

1542 1543 1544 1545 1546 1547 1548
  gdk_window_set_type_hint (widget->window, window->type_hint);

  /* transient_for must be set to allow the modal hint */
  if (window->transient_parent && window->modal)
    gdk_window_set_modal_hint (widget->window, TRUE);
  else
    gdk_window_set_modal_hint (widget->window, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1549 1550
}

1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static void
gtk_window_unrealize (GtkWidget *widget)
{
  GtkWindow *window;

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

  window = GTK_WINDOW (widget);

  if (window->frame)
    {
      gdk_window_set_user_data (window->frame, NULL);
      gdk_window_destroy (window->frame);
      window->frame = NULL;
    }
  
  (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

Elliot Lee's avatar
Elliot Lee committed
1571 1572 1573 1574 1575
static void
gtk_window_size_request (GtkWidget      *widget,
			 GtkRequisition *requisition)
{
  GtkWindow *window;
1576
  GtkBin *bin;
Elliot Lee's avatar
Elliot Lee committed
1577 1578 1579 1580 1581 1582

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

  window = GTK_WINDOW (widget);
1583 1584 1585 1586
  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
1587

1588
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))