gtkframe.c 19.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/. 
 */

27
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
28
#include "gtkframe.h"
29
#include "gtklabel.h"
30
#include "gtkintl.h"
Elliot Lee's avatar
Elliot Lee committed
31

32 33 34
#define LABEL_PAD 1
#define LABEL_SIDE_PAD 2

35
enum {
36 37 38 39 40
  PROP_0,
  PROP_LABEL,
  PROP_LABEL_XALIGN,
  PROP_LABEL_YALIGN,
  PROP_SHADOW,
41
  PROP_SHADOW_TYPE,
42
  PROP_LABEL_WIDGET
43 44
};

Elliot Lee's avatar
Elliot Lee committed
45 46 47

static void gtk_frame_class_init    (GtkFrameClass  *klass);
static void gtk_frame_init          (GtkFrame       *frame);
48 49 50 51 52 53 54 55
static void gtk_frame_set_property (GObject      *object,
				    guint         param_id,
				    const GValue *value,
				    GParamSpec   *pspec);
static void gtk_frame_get_property (GObject     *object,
				    guint        param_id,
				    GValue      *value,
				    GParamSpec  *pspec);
Elliot Lee's avatar
Elliot Lee committed
56 57 58 59 60 61 62 63
static void gtk_frame_paint         (GtkWidget      *widget,
				     GdkRectangle   *area);
static gint gtk_frame_expose        (GtkWidget      *widget,
				     GdkEventExpose *event);
static void gtk_frame_size_request  (GtkWidget      *widget,
				     GtkRequisition *requisition);
static void gtk_frame_size_allocate (GtkWidget      *widget,
				     GtkAllocation  *allocation);
64 65 66 67 68 69 70 71 72 73 74
static void gtk_frame_remove        (GtkContainer   *container,
				     GtkWidget      *child);
static void gtk_frame_forall        (GtkContainer   *container,
				     gboolean	     include_internals,
			             GtkCallback     callback,
			             gpointer        callback_data);

static void gtk_frame_compute_child_allocation      (GtkFrame      *frame,
						     GtkAllocation *child_allocation);
static void gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
						     GtkAllocation *child_allocation);
Elliot Lee's avatar
Elliot Lee committed
75 76 77 78

static GtkBinClass *parent_class = NULL;


Manish Singh's avatar
Manish Singh committed
79
GType
80
gtk_frame_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
81
{
Manish Singh's avatar
Manish Singh committed
82
  static GType frame_type = 0;
Elliot Lee's avatar
Elliot Lee committed
83 84 85

  if (!frame_type)
    {
Manish Singh's avatar
Manish Singh committed
86
      static const GTypeInfo frame_info =
Elliot Lee's avatar
Elliot Lee committed
87 88
      {
	sizeof (GtkFrameClass),
Manish Singh's avatar
Manish Singh committed
89 90 91 92 93 94 95 96
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_frame_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkFrame),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_frame_init,
Elliot Lee's avatar
Elliot Lee committed
97 98
      };

Manish Singh's avatar
Manish Singh committed
99 100
      frame_type = g_type_register_static (GTK_TYPE_BIN, "GtkFrame",
					   &frame_info, 0);
Elliot Lee's avatar
Elliot Lee committed
101 102 103 104 105 106 107 108
    }

  return frame_type;
}

static void
gtk_frame_class_init (GtkFrameClass *class)
{
109
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
110
  GtkWidgetClass *widget_class;
111
  GtkContainerClass *container_class;
Elliot Lee's avatar
Elliot Lee committed
112

113
  gobject_class = (GObjectClass*) class;
114 115
  widget_class = GTK_WIDGET_CLASS (class);
  container_class = GTK_CONTAINER_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
116

Manish Singh's avatar
Manish Singh committed
117
  parent_class = g_type_class_peek_parent (class);
Elliot Lee's avatar
Elliot Lee committed
118

119 120 121 122 123 124 125
  gobject_class->set_property = gtk_frame_set_property;
  gobject_class->get_property = gtk_frame_get_property;

  g_object_class_install_property (gobject_class,
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
                                                        _("Label"),
126
                                                        _("Text of the frame's label"),
127 128 129 130 131
                                                        NULL,
                                                        G_PARAM_READABLE |
							G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class,
				   PROP_LABEL_XALIGN,
132 133
				   g_param_spec_float ("label_xalign",
						       _("Label xalign"),
134
						       _("The horizontal alignment of the label"),
135 136 137 138 139
						       0.0,
						       1.0,
						       0.5,
						       G_PARAM_READABLE |
						       G_PARAM_WRITABLE));
140 141
  g_object_class_install_property (gobject_class,
				   PROP_LABEL_YALIGN,
142 143
				   g_param_spec_float ("label_yalign",
						       _("Label yalign"),
144
						       _("The vertical alignment of the label"),
145 146 147 148 149
						       0.0,
						       1.0,
						       0.5,
						       G_PARAM_READABLE |
						       G_PARAM_WRITABLE));
150 151
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW,
152
                                   g_param_spec_enum ("shadow", NULL,
153
                                                      _("Deprecated property, use shadow_type instead"),
154 155 156 157 158 159
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_IN,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
                                   g_param_spec_enum ("shadow_type",
160
                                                      _("Frame shadow"),
161
                                                      _("Appearance of the frame border"),
162 163 164 165 166 167 168 169
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_IN,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class,
                                   PROP_LABEL_WIDGET,
                                   g_param_spec_object ("label_widget",
                                                        _("Label widget"),
170
                                                        _("A widget to display in place of the usual frame label"),
171 172 173
                                                        GTK_TYPE_WIDGET,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
  
Elliot Lee's avatar
Elliot Lee committed
174 175 176
  widget_class->expose_event = gtk_frame_expose;
  widget_class->size_request = gtk_frame_size_request;
  widget_class->size_allocate = gtk_frame_size_allocate;
177 178 179 180 181

  container_class->remove = gtk_frame_remove;
  container_class->forall = gtk_frame_forall;

  class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
Elliot Lee's avatar
Elliot Lee committed
182 183 184 185 186
}

static void
gtk_frame_init (GtkFrame *frame)
{
187
  frame->label_widget = NULL;
Elliot Lee's avatar
Elliot Lee committed
188 189 190 191 192
  frame->shadow_type = GTK_SHADOW_ETCHED_IN;
  frame->label_xalign = 0.0;
  frame->label_yalign = 0.5;
}

193 194 195 196 197
static void 
gtk_frame_set_property (GObject         *object,
			guint            prop_id,
			const GValue    *value,
			GParamSpec      *pspec)
198
{
199 200 201 202
  GtkFrame *frame;

  frame = GTK_FRAME (object);

203
  switch (prop_id)
204
    {
205 206
    case PROP_LABEL:
      gtk_frame_set_label (frame, g_value_get_string (value));
207
      break;
208
    case PROP_LABEL_XALIGN:
209
      gtk_frame_set_label_align (frame, g_value_get_float (value), 
210
				 frame->label_yalign);
211
      break;
212 213
    case PROP_LABEL_YALIGN:
      gtk_frame_set_label_align (frame, frame->label_xalign, 
214
				 g_value_get_float (value));
215
      break;
216
    case PROP_SHADOW:
217
    case PROP_SHADOW_TYPE:
218
      gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
219
      break;
220 221 222 223 224
    case PROP_LABEL_WIDGET:
      gtk_frame_set_label_widget (frame, g_value_get_object (value));
      break;
    default:      
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
225
      break;
226 227 228
    }
}

229 230 231 232 233
static void 
gtk_frame_get_property (GObject         *object,
			guint            prop_id,
			GValue          *value,
			GParamSpec      *pspec)
234
{
235 236 237 238
  GtkFrame *frame;

  frame = GTK_FRAME (object);

239
  switch (prop_id)
240
    {
241 242
    case PROP_LABEL:
      g_value_set_string (value, gtk_frame_get_label (frame));
243
      break;
244
    case PROP_LABEL_XALIGN:
245
      g_value_set_float (value, frame->label_xalign);
246
      break;
247
    case PROP_LABEL_YALIGN:
248
      g_value_set_float (value, frame->label_yalign);
249
      break;
250
    case PROP_SHADOW:
251
    case PROP_SHADOW_TYPE:
252 253 254 255 256 257
      g_value_set_enum (value, frame->shadow_type);
      break;
    case PROP_LABEL_WIDGET:
      g_value_set_object (value,
                          frame->label_widget ?
                          G_OBJECT (frame->label_widget) : NULL);
258 259
      break;
    default:
260
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 262 263 264
      break;
    }
}

Elliot Lee's avatar
Elliot Lee committed
265 266 267
GtkWidget*
gtk_frame_new (const gchar *label)
{
Manish Singh's avatar
Manish Singh committed
268
  return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
Elliot Lee's avatar
Elliot Lee committed
269 270
}

271
static void
272 273
gtk_frame_remove (GtkContainer *container,
		  GtkWidget    *child)
274
{
275
  GtkFrame *frame = GTK_FRAME (container);
276

277 278 279 280 281
  if (frame->label_widget == child)
    gtk_frame_set_label_widget (frame, NULL);
  else
    GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
}
282

283 284 285 286 287 288 289 290 291 292 293
static void
gtk_frame_forall (GtkContainer *container,
		  gboolean      include_internals,
		  GtkCallback   callback,
		  gpointer      callback_data)
{
  GtkBin *bin = GTK_BIN (container);
  GtkFrame *frame = GTK_FRAME (container);

  if (bin->child)
    (* callback) (bin->child, callback_data);
294

295 296
  if (frame->label_widget)
    (* callback) (frame->label_widget, callback_data);
297 298
}

Elliot Lee's avatar
Elliot Lee committed
299 300 301 302 303 304
void
gtk_frame_set_label (GtkFrame *frame,
		     const gchar *label)
{
  g_return_if_fail (GTK_IS_FRAME (frame));

305
  if (!label)
Elliot Lee's avatar
Elliot Lee committed
306
    {
307
      gtk_frame_set_label_widget (frame, NULL);
Elliot Lee's avatar
Elliot Lee committed
308 309 310
    }
  else
    {
311 312 313 314
      GtkWidget *child = gtk_label_new (label);
      gtk_widget_show (child);

      gtk_frame_set_label_widget (frame, child);
Elliot Lee's avatar
Elliot Lee committed
315
    }
316
}
Elliot Lee's avatar
Elliot Lee committed
317

318 319 320 321 322 323 324 325 326 327 328
/**
 * gtk_frame_get_label:
 * @frame: a #GtkFrame
 * 
 * If the frame's label widget is a #GtkLabel, return the
 * text in the label widget. (The frame will have a #GtkLabel
 * for the label widget if a non-%NULL argument was passed
 * to gtk_frame_new().)
 * 
 * Return value: the text in the label, or %NULL if there
 *               was no label widget or the lable widget was not
329 330
 *               a #GtkLabel. This string is owned by GTK+ and
 *               must not be modified or freed.
331
 **/
332
G_CONST_RETURN gchar *
333 334 335
gtk_frame_get_label (GtkFrame *frame)
{
  g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
Elliot Lee's avatar
Elliot Lee committed
336

337 338 339 340 341
  if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
    return gtk_label_get_text (GTK_LABEL (frame->label_widget));
  else
    return NULL;
}
Elliot Lee's avatar
Elliot Lee committed
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
/**
 * gtk_frame_set_label_widget:
 * @frame: a #GtkFrame
 * @label_widget: the new label widget
 * 
 * Set the label widget for the frame. This is the widget that
 * will appear embedded in the top edge of the frame as a
 * title.
 **/
void
gtk_frame_set_label_widget (GtkFrame  *frame,
			    GtkWidget *label_widget)
{
  gboolean need_resize = FALSE;
  
  g_return_if_fail (GTK_IS_FRAME (frame));
  g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
  g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
Gnome CVS User's avatar
Gnome CVS User committed
361
  
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
  if (frame->label_widget == label_widget)
    return;
  
  if (frame->label_widget)
    {
      need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
      gtk_widget_unparent (frame->label_widget);
    }

  frame->label_widget = label_widget;
    
  if (label_widget)
    {
      frame->label_widget = label_widget;
      gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
      need_resize |= GTK_WIDGET_VISIBLE (label_widget);
    }
379
  
380 381
  if (GTK_WIDGET_VISIBLE (frame) && need_resize)
    gtk_widget_queue_resize (GTK_WIDGET (frame));
382

383
  g_object_freeze_notify (G_OBJECT (frame));
384
  g_object_notify (G_OBJECT (frame), "label_widget");
385 386
  g_object_notify (G_OBJECT (frame), "label");
  g_object_thaw_notify (G_OBJECT (frame));
Elliot Lee's avatar
Elliot Lee committed
387 388
}

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
/**
 * gtk_frame_get_label_widget:
 * @frame: a #GtkFrame
 *
 * Retrieves the label widget for the frame. See
 * gtk_frame_set_label_widget().
 *
 * Return value: the label widget, or %NULL if there is none.
 **/
GtkWidget *
gtk_frame_get_label_widget (GtkFrame *frame)
{
  g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);

  return frame->label_widget;
}

Elliot Lee's avatar
Elliot Lee committed
406 407 408 409 410 411 412 413 414 415
void
gtk_frame_set_label_align (GtkFrame *frame,
			   gfloat    xalign,
			   gfloat    yalign)
{
  g_return_if_fail (GTK_IS_FRAME (frame));

  xalign = CLAMP (xalign, 0.0, 1.0);
  yalign = CLAMP (yalign, 0.0, 1.0);

416
  g_object_freeze_notify (G_OBJECT (frame));
417
  if (xalign != frame->label_xalign)
Elliot Lee's avatar
Elliot Lee committed
418 419
    {
      frame->label_xalign = xalign;
420 421
      g_object_notify (G_OBJECT (frame), "label_xalign");
    }
Elliot Lee's avatar
Elliot Lee committed
422

423 424 425 426
  if (yalign != frame->label_yalign)
    {
      frame->label_yalign = yalign;
      g_object_notify (G_OBJECT (frame), "label_yalign");
Elliot Lee's avatar
Elliot Lee committed
427
    }
428

429
  g_object_thaw_notify (G_OBJECT (frame));
430
  gtk_widget_queue_resize (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
431 432
}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/**
 * gtk_frame_get_label_align:
 * @frame: a #GtkFrame
 * @xalign: location to store X alignment of frame's label, or %NULL
 * @yalign: location to store X alignment of frame's label, or %NULL
 * 
 * Retrieves the X and Y alignment of the frame's label. See
 * gtk_frame_set_label_align().
 **/
void
gtk_frame_get_label_align (GtkFrame *frame,
		           gfloat   *xalign,
			   gfloat   *yalign)
{
  g_return_if_fail (GTK_IS_FRAME (frame));

  if (xalign)
    *xalign = frame->label_xalign;
  if (yalign)
    *yalign = frame->label_yalign;
}

Elliot Lee's avatar
Elliot Lee committed
455 456 457 458 459 460 461 462 463
void
gtk_frame_set_shadow_type (GtkFrame      *frame,
			   GtkShadowType  type)
{
  g_return_if_fail (GTK_IS_FRAME (frame));

  if ((GtkShadowType) frame->shadow_type != type)
    {
      frame->shadow_type = type;
464
      g_object_notify (G_OBJECT (frame), "shadow_type");
Elliot Lee's avatar
Elliot Lee committed
465

Gnome CVS User's avatar
Gnome CVS User committed
466
      if (GTK_WIDGET_DRAWABLE (frame))
Elliot Lee's avatar
Elliot Lee committed
467
	{
Manish Singh's avatar
Manish Singh committed
468
	  gtk_widget_queue_draw (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
469
	}
470
      
Gnome CVS User's avatar
Gnome CVS User committed
471
      gtk_widget_queue_resize (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
472 473 474
    }
}

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
/**
 * gtk_frame_get_shadow_type:
 * @frame: a #GtkFrame
 *
 * Retrieves the shadow type of the frame. See
 * gtk_frame_set_shadow_type().
 *
 * Return value: the current shadow type of the frame.
 **/
GtkShadowType
gtk_frame_get_shadow_type (GtkFrame *frame)
{
  g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);

  return frame->shadow_type;
}

Elliot Lee's avatar
Elliot Lee committed
492 493 494 495 496
static void
gtk_frame_paint (GtkWidget    *widget,
		 GdkRectangle *area)
{
  GtkFrame *frame;
497
  gint x, y, width, height;
Elliot Lee's avatar
Elliot Lee committed
498 499 500 501 502

  if (GTK_WIDGET_DRAWABLE (widget))
    {
      frame = GTK_FRAME (widget);

503 504 505 506
      x = frame->child_allocation.x - widget->style->xthickness;
      y = frame->child_allocation.y - widget->style->ythickness;
      width = frame->child_allocation.width + 2 * widget->style->xthickness;
      height =  frame->child_allocation.height + 2 * widget->style->ythickness;
Elliot Lee's avatar
Elliot Lee committed
507

508
      if (frame->label_widget)
Elliot Lee's avatar
Elliot Lee committed
509
	{
510 511 512 513 514 515 516 517 518 519 520 521
	  GtkRequisition child_requisition;
	  gfloat xalign;
	  gint height_extra;
	  gint x2;

	  gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);

	  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	    xalign = frame->label_xalign;
	  else
	    xalign = 1 - frame->label_xalign;

522
	  height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
523 524 525
	  y -= height_extra * (1 - frame->label_yalign);
	  height += height_extra * (1 - frame->label_yalign);
	  
526
	  x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
527

528
	  
529 530 531 532 533
	  gtk_paint_shadow_gap (widget->style, widget->window,
				GTK_STATE_NORMAL, frame->shadow_type,
				area, widget, "frame",
				x, y, width, height,
				GTK_POS_TOP, 
534
				x2, child_requisition.width + 2 * LABEL_PAD);
Elliot Lee's avatar
Elliot Lee committed
535
	}
536 537 538 539
       else
	 gtk_paint_shadow (widget->style, widget->window,
			   GTK_STATE_NORMAL, frame->shadow_type,
			   area, widget, "frame",
540
			   x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
541 542 543
    }
}

544
static gboolean
Elliot Lee's avatar
Elliot Lee committed
545 546 547 548 549 550 551
gtk_frame_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
{
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      gtk_frame_paint (widget, &event->area);

552
      (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
Elliot Lee's avatar
Elliot Lee committed
553 554 555 556 557 558 559 560 561
    }

  return FALSE;
}

static void
gtk_frame_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
562 563 564 565 566 567 568
  GtkFrame *frame = GTK_FRAME (widget);
  GtkBin *bin = GTK_BIN (widget);
  GtkRequisition child_requisition;
  
  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
    {
      gtk_widget_size_request (frame->label_widget, &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
569

570
      requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
571
      requisition->height =
572
	MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
573 574 575 576 577 578 579 580 581 582
    }
  else
    {
      requisition->width = 0;
      requisition->height = 0;
    }
  
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
    {
      gtk_widget_size_request (bin->child, &child_requisition);
Elliot Lee's avatar
Elliot Lee committed
583

584 585 586
      requisition->width = MAX (requisition->width, child_requisition.width);
      requisition->height += child_requisition.height;
    }
Elliot Lee's avatar
Elliot Lee committed
587

588
  requisition->width += (GTK_CONTAINER (widget)->border_width +
589
			 GTK_WIDGET (widget)->style->xthickness) * 2;
590
  requisition->height += (GTK_CONTAINER (widget)->border_width +
591
			  GTK_WIDGET (widget)->style->ythickness) * 2;
592
}
Elliot Lee's avatar
Elliot Lee committed
593

594 595 596 597 598 599
static void
gtk_frame_size_allocate (GtkWidget     *widget,
			 GtkAllocation *allocation)
{
  GtkFrame *frame = GTK_FRAME (widget);
  GtkBin *bin = GTK_BIN (widget);
600
  GtkAllocation new_allocation;
Elliot Lee's avatar
Elliot Lee committed
601

602
  widget->allocation = *allocation;
Elliot Lee's avatar
Elliot Lee committed
603

604 605 606 607 608 609 610 611 612 613
  gtk_frame_compute_child_allocation (frame, &new_allocation);
  
  /* If the child allocation changed, that means that the frame is drawn
   * in a new place, so we must redraw the entire widget.
   */
  if (GTK_WIDGET_MAPPED (widget) &&
      (new_allocation.x != frame->child_allocation.x ||
       new_allocation.y != frame->child_allocation.y ||
       new_allocation.width != frame->child_allocation.width ||
       new_allocation.height != frame->child_allocation.height))
614
    gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
615
  
Elliot Lee's avatar
Elliot Lee committed
616
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
617 618 619 620
    gtk_widget_size_allocate (bin->child, &new_allocation);
  
  frame->child_allocation = new_allocation;
  
621
  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
Elliot Lee's avatar
Elliot Lee committed
622
    {
623 624 625 626 627 628 629 630 631 632 633
      GtkRequisition child_requisition;
      GtkAllocation child_allocation;
      gfloat xalign;

      gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);

      if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
	xalign = frame->label_xalign;
      else
	xalign = 1 - frame->label_xalign;
      
634 635
      child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
	(frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
636 637 638 639 640 641
      child_allocation.width = child_requisition.width;

      child_allocation.y = frame->child_allocation.y - child_requisition.height;
      child_allocation.height = child_requisition.height;

      gtk_widget_size_allocate (frame->label_widget, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
642 643 644
    }
}

645 646 647 648 649 650
static void
gtk_frame_compute_child_allocation (GtkFrame      *frame,
				    GtkAllocation *child_allocation)
{
  g_return_if_fail (GTK_IS_FRAME (frame));
  g_return_if_fail (child_allocation != NULL);
Elliot Lee's avatar
Elliot Lee committed
651

652 653
  GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
}
Elliot Lee's avatar
Elliot Lee committed
654

655 656 657 658 659 660 661 662
static void
gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
					 GtkAllocation *child_allocation)
{
  GtkWidget *widget = GTK_WIDGET (frame);
  GtkAllocation *allocation = &widget->allocation;
  GtkRequisition child_requisition;
  gint top_margin;
Elliot Lee's avatar
Elliot Lee committed
663

664 665 666
  if (frame->label_widget)
    {
      gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
667
      top_margin = MAX (child_requisition.height, widget->style->ythickness);
Elliot Lee's avatar
Elliot Lee committed
668
    }
669
  else
670
    top_margin = widget->style->ythickness;
671 672
  
  child_allocation->x = (GTK_CONTAINER (frame)->border_width +
673
			 widget->style->xthickness);
674 675 676 677 678
  child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
  
  child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
  child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
				      (gint)GTK_CONTAINER (frame)->border_width -
679
				      (gint)widget->style->ythickness));
680 681 682
  
  child_allocation->x += allocation->x;
  child_allocation->y += allocation->y;
Elliot Lee's avatar
Elliot Lee committed
683
}