gtkframe.c 21.8 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * 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 "config.h"
28
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
29
#include "gtkframe.h"
30
#include "gtklabel.h"
31
#include "gtkprivate.h"
32
#include "gtkintl.h"
33
#include "gtkbuildable.h"
34
#include "gtkalias.h"
Elliot Lee's avatar
Elliot Lee committed
35

36 37 38
#define LABEL_PAD 1
#define LABEL_SIDE_PAD 2

39
enum {
40 41 42 43 44
  PROP_0,
  PROP_LABEL,
  PROP_LABEL_XALIGN,
  PROP_LABEL_YALIGN,
  PROP_SHADOW,
45
  PROP_SHADOW_TYPE,
46
  PROP_LABEL_WIDGET
47 48
};

49 50 51 52 53 54 55 56
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
57 58 59 60 61 62 63 64
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);
65 66 67 68 69 70 71 72 73 74 75
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
76

77 78
/* GtkBuildable */
static void gtk_frame_buildable_init                (GtkBuildableIface *iface);
79
static void gtk_frame_buildable_add_child           (GtkBuildable *buildable,
80 81 82 83 84 85 86
						     GtkBuilder   *builder,
						     GObject      *child,
						     const gchar  *type);

G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_frame_buildable_init))
Elliot Lee's avatar
Elliot Lee committed
87 88 89 90

static void
gtk_frame_class_init (GtkFrameClass *class)
{
91
  GObjectClass *gobject_class;
Elliot Lee's avatar
Elliot Lee committed
92
  GtkWidgetClass *widget_class;
93
  GtkContainerClass *container_class;
Elliot Lee's avatar
Elliot Lee committed
94

95
  gobject_class = (GObjectClass*) class;
96 97
  widget_class = GTK_WIDGET_CLASS (class);
  container_class = GTK_CONTAINER_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
98

99 100 101 102 103 104
  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",
105 106
                                                        P_("Label"),
                                                        P_("Text of the frame's label"),
107
                                                        NULL,
108 109
                                                        GTK_PARAM_READABLE |
							GTK_PARAM_WRITABLE));
110 111
  g_object_class_install_property (gobject_class,
				   PROP_LABEL_XALIGN,
112
				   g_param_spec_float ("label-xalign",
113 114
						       P_("Label xalign"),
						       P_("The horizontal alignment of the label"),
115 116
						       0.0,
						       1.0,
117
						       0.0,
118
						       GTK_PARAM_READWRITE));
119 120
  g_object_class_install_property (gobject_class,
				   PROP_LABEL_YALIGN,
121
				   g_param_spec_float ("label-yalign",
122 123
						       P_("Label yalign"),
						       P_("The vertical alignment of the label"),
124 125 126
						       0.0,
						       1.0,
						       0.5,
127
						       GTK_PARAM_READWRITE));
128 129
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW,
130
                                   g_param_spec_enum ("shadow", NULL,
131
                                                      P_("Deprecated property, use shadow_type instead"),
132 133
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_IN,
134
                                                      GTK_PARAM_READWRITE));
135 136
  g_object_class_install_property (gobject_class,
                                   PROP_SHADOW_TYPE,
137
                                   g_param_spec_enum ("shadow-type",
138 139
                                                      P_("Frame shadow"),
                                                      P_("Appearance of the frame border"),
140 141
						      GTK_TYPE_SHADOW_TYPE,
						      GTK_SHADOW_ETCHED_IN,
142
                                                      GTK_PARAM_READWRITE));
143 144 145

  g_object_class_install_property (gobject_class,
                                   PROP_LABEL_WIDGET,
146
                                   g_param_spec_object ("label-widget",
147 148
                                                        P_("Label widget"),
                                                        P_("A widget to display in place of the usual frame label"),
149
                                                        GTK_TYPE_WIDGET,
150
                                                        GTK_PARAM_READWRITE));
151
  
Elliot Lee's avatar
Elliot Lee committed
152 153 154
  widget_class->expose_event = gtk_frame_expose;
  widget_class->size_request = gtk_frame_size_request;
  widget_class->size_allocate = gtk_frame_size_allocate;
155 156 157 158 159

  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
160 161
}

162 163 164
static void
gtk_frame_buildable_init (GtkBuildableIface *iface)
{
165
  iface->add_child = gtk_frame_buildable_add_child;
166 167 168
}

static void
169 170 171 172
gtk_frame_buildable_add_child (GtkBuildable *buildable,
			       GtkBuilder   *builder,
			       GObject      *child,
			       const gchar  *type)
173 174 175 176 177 178 179 180 181
{
  if (type && strcmp (type, "label") == 0)
    gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
  else if (!type)
    gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
  else
    GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
}

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

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

  frame = GTK_FRAME (object);

201
  switch (prop_id)
202
    {
203 204
    case PROP_LABEL:
      gtk_frame_set_label (frame, g_value_get_string (value));
205
      break;
206
    case PROP_LABEL_XALIGN:
207
      gtk_frame_set_label_align (frame, g_value_get_float (value), 
208
				 frame->label_yalign);
209
      break;
210 211
    case PROP_LABEL_YALIGN:
      gtk_frame_set_label_align (frame, frame->label_xalign, 
212
				 g_value_get_float (value));
213
      break;
214
    case PROP_SHADOW:
215
    case PROP_SHADOW_TYPE:
216
      gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
217
      break;
218 219 220 221 222
    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
223
      break;
224 225 226
    }
}

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

  frame = GTK_FRAME (object);

237
  switch (prop_id)
238
    {
239 240
    case PROP_LABEL:
      g_value_set_string (value, gtk_frame_get_label (frame));
241
      break;
242
    case PROP_LABEL_XALIGN:
243
      g_value_set_float (value, frame->label_xalign);
244
      break;
245
    case PROP_LABEL_YALIGN:
246
      g_value_set_float (value, frame->label_yalign);
247
      break;
248
    case PROP_SHADOW:
249
    case PROP_SHADOW_TYPE:
250 251 252 253 254 255
      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);
256 257
      break;
    default:
258
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 260 261 262
      break;
    }
}

263 264 265 266 267 268 269 270 271
/**
 * gtk_frame_new:
 * @label: the text to use as the label of the frame
 * 
 * Creates a new #GtkFrame, with optional label @label.
 * If @label is %NULL, the label is omitted.
 * 
 * Return value: a new #GtkFrame widget
 **/
Elliot Lee's avatar
Elliot Lee committed
272 273 274
GtkWidget*
gtk_frame_new (const gchar *label)
{
Manish Singh's avatar
Manish Singh committed
275
  return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
Elliot Lee's avatar
Elliot Lee committed
276 277
}

278
static void
279 280
gtk_frame_remove (GtkContainer *container,
		  GtkWidget    *child)
281
{
282
  GtkFrame *frame = GTK_FRAME (container);
283

284 285 286
  if (frame->label_widget == child)
    gtk_frame_set_label_widget (frame, NULL);
  else
Matthias Clasen's avatar
Matthias Clasen committed
287
    GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
288
}
289

290 291 292 293 294 295 296 297 298 299 300
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);
301

302 303
  if (frame->label_widget)
    (* callback) (frame->label_widget, callback_data);
304 305
}

306 307 308
/**
 * gtk_frame_set_label:
 * @frame: a #GtkFrame
309 310
 * @label: (allow-none): the text to use as the label of the frame
 *
311 312 313
 * Sets the text of the label. If @label is %NULL,
 * the current label is removed.
 **/
Elliot Lee's avatar
Elliot Lee committed
314 315 316 317 318 319
void
gtk_frame_set_label (GtkFrame *frame,
		     const gchar *label)
{
  g_return_if_fail (GTK_IS_FRAME (frame));

320
  if (!label)
Elliot Lee's avatar
Elliot Lee committed
321
    {
322
      gtk_frame_set_label_widget (frame, NULL);
Elliot Lee's avatar
Elliot Lee committed
323 324 325
    }
  else
    {
326 327 328 329
      GtkWidget *child = gtk_label_new (label);
      gtk_widget_show (child);

      gtk_frame_set_label_widget (frame, child);
Elliot Lee's avatar
Elliot Lee committed
330
    }
331
}
Elliot Lee's avatar
Elliot Lee committed
332

333 334 335 336
/**
 * gtk_frame_get_label:
 * @frame: a #GtkFrame
 * 
337
 * If the frame's label widget is a #GtkLabel, returns the
338 339 340 341 342 343
 * 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
344 345
 *               a #GtkLabel. This string is owned by GTK+ and
 *               must not be modified or freed.
346
 **/
347
G_CONST_RETURN gchar *
348 349 350
gtk_frame_get_label (GtkFrame *frame)
{
  g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
Elliot Lee's avatar
Elliot Lee committed
351

352
  if (GTK_IS_LABEL (frame->label_widget))
353 354 355 356
    return gtk_label_get_text (GTK_LABEL (frame->label_widget));
  else
    return NULL;
}
Elliot Lee's avatar
Elliot Lee committed
357

358 359 360 361 362
/**
 * gtk_frame_set_label_widget:
 * @frame: a #GtkFrame
 * @label_widget: the new label widget
 * 
363
 * Sets the label widget for the frame. This is the widget that
364 365 366 367 368 369 370 371 372 373 374 375
 * 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
376
  
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
  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);
    }
394
  
395 396
  if (GTK_WIDGET_VISIBLE (frame) && need_resize)
    gtk_widget_queue_resize (GTK_WIDGET (frame));
397

398
  g_object_freeze_notify (G_OBJECT (frame));
399
  g_object_notify (G_OBJECT (frame), "label-widget");
400 401
  g_object_notify (G_OBJECT (frame), "label");
  g_object_thaw_notify (G_OBJECT (frame));
Elliot Lee's avatar
Elliot Lee committed
402 403
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/**
 * 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;
}

421 422 423 424 425 426 427
/**
 * gtk_frame_set_label_align:
 * @frame: a #GtkFrame
 * @xalign: The position of the label along the top edge
 *   of the widget. A value of 0.0 represents left alignment;
 *   1.0 represents right alignment.
 * @yalign: The y alignment of the label. A value of 0.0 aligns under 
428 429 430
 *   the frame; 1.0 aligns above the frame. If the values are exactly
 *   0.0 or 1.0 the gap in the frame won't be painted because the label
 *   will be completely above or below the frame.
431 432 433 434
 * 
 * Sets the alignment of the frame widget's label. The
 * default values for a newly created frame are 0.0 and 0.5.
 **/
Elliot Lee's avatar
Elliot Lee committed
435 436 437 438 439 440 441 442 443 444
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);

445
  g_object_freeze_notify (G_OBJECT (frame));
446
  if (xalign != frame->label_xalign)
Elliot Lee's avatar
Elliot Lee committed
447 448
    {
      frame->label_xalign = xalign;
449
      g_object_notify (G_OBJECT (frame), "label-xalign");
450
    }
Elliot Lee's avatar
Elliot Lee committed
451

452 453 454
  if (yalign != frame->label_yalign)
    {
      frame->label_yalign = yalign;
455
      g_object_notify (G_OBJECT (frame), "label-yalign");
Elliot Lee's avatar
Elliot Lee committed
456
    }
457

458
  g_object_thaw_notify (G_OBJECT (frame));
459
  gtk_widget_queue_resize (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
460 461
}

462 463 464
/**
 * gtk_frame_get_label_align:
 * @frame: a #GtkFrame
465 466
 * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
 * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
 * 
 * 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;
}

484 485 486 487 488 489 490
/**
 * gtk_frame_set_shadow_type:
 * @frame: a #GtkFrame
 * @type: the new #GtkShadowType
 * 
 * Sets the shadow type for @frame.
 **/
Elliot Lee's avatar
Elliot Lee committed
491 492 493 494 495 496 497 498 499
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;
500
      g_object_notify (G_OBJECT (frame), "shadow-type");
Elliot Lee's avatar
Elliot Lee committed
501

Gnome CVS User's avatar
Gnome CVS User committed
502
      if (GTK_WIDGET_DRAWABLE (frame))
Elliot Lee's avatar
Elliot Lee committed
503
	{
Manish Singh's avatar
Manish Singh committed
504
	  gtk_widget_queue_draw (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
505
	}
506
      
Gnome CVS User's avatar
Gnome CVS User committed
507
      gtk_widget_queue_resize (GTK_WIDGET (frame));
Elliot Lee's avatar
Elliot Lee committed
508 509 510
    }
}

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
/**
 * 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
528 529 530 531 532
static void
gtk_frame_paint (GtkWidget    *widget,
		 GdkRectangle *area)
{
  GtkFrame *frame;
533
  gint x, y, width, height;
Elliot Lee's avatar
Elliot Lee committed
534 535 536 537 538

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

539
      x = frame->child_allocation.x - widget->style->xthickness;
Matthias Clasen's avatar
Matthias Clasen committed
540
      y = frame->child_allocation.y - widget->style->ythickness;
541 542
      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
543

544
      if (frame->label_widget)
Elliot Lee's avatar
Elliot Lee committed
545
	{
546 547 548 549 550 551 552 553 554 555 556 557
	  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;

558 559
	  height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
	    - frame->label_yalign * child_requisition.height;
560 561
	  y -= height_extra;
	  height += height_extra;
562
	  
563
	  x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
564
	  
565 566 567 568 569 570 571 572 573 574 575 576 577
	  /* If the label is completely over or under the frame we can omit the gap */
	  if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
	    gtk_paint_shadow (widget->style, widget->window,
			      widget->state, frame->shadow_type,
			      area, widget, "frame",
			      x, y, width, height);
	  else
	    gtk_paint_shadow_gap (widget->style, widget->window,
				  widget->state, frame->shadow_type,
				  area, widget, "frame",
				  x, y, width, height,
				  GTK_POS_TOP,
				  x2, child_requisition.width + 2 * LABEL_PAD);
Elliot Lee's avatar
Elliot Lee committed
578
	}
579 580
       else
	 gtk_paint_shadow (widget->style, widget->window,
581
			   widget->state, frame->shadow_type,
582
			   area, widget, "frame",
583
			   x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
584 585 586
    }
}

587
static gboolean
Elliot Lee's avatar
Elliot Lee committed
588 589 590 591 592 593 594
gtk_frame_expose (GtkWidget      *widget,
		  GdkEventExpose *event)
{
  if (GTK_WIDGET_DRAWABLE (widget))
    {
      gtk_frame_paint (widget, &event->area);

595
      GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
Elliot Lee's avatar
Elliot Lee committed
596 597 598 599 600 601 602 603 604
    }

  return FALSE;
}

static void
gtk_frame_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
605 606 607 608 609 610 611
  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
612

613
      requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
614
      requisition->height =
615
	MAX (0, child_requisition.height - widget->style->ythickness);
616 617 618 619 620 621 622 623 624 625
    }
  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
626

627 628 629
      requisition->width = MAX (requisition->width, child_requisition.width);
      requisition->height += child_requisition.height;
    }
Elliot Lee's avatar
Elliot Lee committed
630

631
  requisition->width += (GTK_CONTAINER (widget)->border_width +
632
			 GTK_WIDGET (widget)->style->xthickness) * 2;
633
  requisition->height += (GTK_CONTAINER (widget)->border_width +
634
			  GTK_WIDGET (widget)->style->ythickness) * 2;
635
}
Elliot Lee's avatar
Elliot Lee committed
636

637 638 639 640 641 642
static void
gtk_frame_size_allocate (GtkWidget     *widget,
			 GtkAllocation *allocation)
{
  GtkFrame *frame = GTK_FRAME (widget);
  GtkBin *bin = GTK_BIN (widget);
643
  GtkAllocation new_allocation;
Elliot Lee's avatar
Elliot Lee committed
644

645
  widget->allocation = *allocation;
Elliot Lee's avatar
Elliot Lee committed
646

647 648 649 650 651 652 653 654 655 656
  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))
657
    gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
658
  
Elliot Lee's avatar
Elliot Lee committed
659
  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
660 661 662 663
    gtk_widget_size_allocate (bin->child, &new_allocation);
  
  frame->child_allocation = new_allocation;
  
664
  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
Elliot Lee's avatar
Elliot Lee committed
665
    {
666 667 668 669 670 671 672 673 674 675 676
      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;
      
677 678
      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;
679
      child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
680

681
      child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
682 683 684
      child_allocation.height = child_requisition.height;

      gtk_widget_size_allocate (frame->label_widget, &child_allocation);
Elliot Lee's avatar
Elliot Lee committed
685 686 687
    }
}

688 689 690 691 692 693
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
694

695 696
  GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
}
Elliot Lee's avatar
Elliot Lee committed
697

698 699 700 701 702 703 704 705
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
706

707 708 709
  if (frame->label_widget)
    {
      gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
710
      top_margin = MAX (child_requisition.height, widget->style->ythickness);
Elliot Lee's avatar
Elliot Lee committed
711
    }
712
  else
713
    top_margin = widget->style->ythickness;
714 715
  
  child_allocation->x = (GTK_CONTAINER (frame)->border_width +
716
			 widget->style->xthickness);
717 718 719 720 721
  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 -
722
				      (gint)widget->style->ythickness));
723 724 725
  
  child_allocation->x += allocation->x;
  child_allocation->y += allocation->y;
Elliot Lee's avatar
Elliot Lee committed
726
}
727 728 729

#define __GTK_FRAME_C__
#include "gtkaliasdef.c"