1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 #define LABEL_SIDE_PAD 2
45 static void gtk_frame_class_init (GtkFrameClass *klass);
46 static void gtk_frame_init (GtkFrame *frame);
47 static void gtk_frame_set_property (GObject *object,
51 static void gtk_frame_get_property (GObject *object,
55 static void gtk_frame_paint (GtkWidget *widget,
57 static gint gtk_frame_expose (GtkWidget *widget,
58 GdkEventExpose *event);
59 static void gtk_frame_size_request (GtkWidget *widget,
60 GtkRequisition *requisition);
61 static void gtk_frame_size_allocate (GtkWidget *widget,
62 GtkAllocation *allocation);
63 static void gtk_frame_remove (GtkContainer *container,
65 static void gtk_frame_forall (GtkContainer *container,
66 gboolean include_internals,
68 gpointer callback_data);
70 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
71 GtkAllocation *child_allocation);
72 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
73 GtkAllocation *child_allocation);
75 static GtkBinClass *parent_class = NULL;
79 gtk_frame_get_type (void)
81 static GtkType frame_type = 0;
85 static const GtkTypeInfo frame_info =
89 sizeof (GtkFrameClass),
90 (GtkClassInitFunc) gtk_frame_class_init,
91 (GtkObjectInitFunc) gtk_frame_init,
92 /* reserved_1 */ NULL,
93 /* reserved_2 */ NULL,
94 (GtkClassInitFunc) NULL,
97 frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
104 gtk_frame_class_init (GtkFrameClass *class)
106 GObjectClass *gobject_class;
107 GtkObjectClass *object_class;
108 GtkWidgetClass *widget_class;
109 GtkContainerClass *container_class;
111 gobject_class = (GObjectClass*) class;
112 object_class = GTK_OBJECT_CLASS (class);
113 widget_class = GTK_WIDGET_CLASS (class);
114 container_class = GTK_CONTAINER_CLASS (class);
116 parent_class = gtk_type_class (gtk_bin_get_type ());
118 gobject_class->set_property = gtk_frame_set_property;
119 gobject_class->get_property = gtk_frame_get_property;
121 g_object_class_install_property (gobject_class,
123 g_param_spec_string ("label",
125 _("Text of the frame's label."),
129 g_object_class_install_property (gobject_class,
131 g_param_spec_double ("label_xalign",
133 _("The horizontal alignment of the label."),
139 g_object_class_install_property (gobject_class,
141 g_param_spec_double ("label_yalign",
143 _("The vertical alignment of the label."),
149 g_object_class_install_property (gobject_class,
151 g_param_spec_enum ("shadow",
153 _("Appearance of the frame border."),
154 GTK_TYPE_SHADOW_TYPE,
155 GTK_SHADOW_ETCHED_IN,
156 G_PARAM_READABLE | G_PARAM_WRITABLE));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_object ("label_widget",
162 _("A widget to display in place of the usual frame label."),
164 G_PARAM_READABLE | G_PARAM_WRITABLE));
166 widget_class->expose_event = gtk_frame_expose;
167 widget_class->size_request = gtk_frame_size_request;
168 widget_class->size_allocate = gtk_frame_size_allocate;
170 container_class->remove = gtk_frame_remove;
171 container_class->forall = gtk_frame_forall;
173 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
177 gtk_frame_init (GtkFrame *frame)
179 frame->label_widget = NULL;
180 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
181 frame->label_xalign = 0.0;
182 frame->label_yalign = 0.5;
186 gtk_frame_set_property (GObject *object,
193 frame = GTK_FRAME (object);
198 gtk_frame_set_label (frame, g_value_get_string (value));
200 case PROP_LABEL_XALIGN:
201 gtk_frame_set_label_align (frame, g_value_get_double (value),
202 frame->label_yalign);
204 case PROP_LABEL_YALIGN:
205 gtk_frame_set_label_align (frame, frame->label_xalign,
206 g_value_get_double (value));
209 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
211 case PROP_LABEL_WIDGET:
212 gtk_frame_set_label_widget (frame, g_value_get_object (value));
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gtk_frame_get_property (GObject *object,
228 frame = GTK_FRAME (object);
233 g_value_set_string (value, gtk_frame_get_label (frame));
235 case PROP_LABEL_XALIGN:
236 g_value_set_double (value, frame->label_xalign);
238 case PROP_LABEL_YALIGN:
239 g_value_set_double (value, frame->label_yalign);
242 g_value_set_enum (value, frame->shadow_type);
244 case PROP_LABEL_WIDGET:
245 g_value_set_object (value,
246 frame->label_widget ?
247 G_OBJECT (frame->label_widget) : NULL);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gtk_frame_new (const gchar *label)
260 frame = gtk_type_new (gtk_frame_get_type ());
262 gtk_frame_set_label (frame, label);
264 return GTK_WIDGET (frame);
268 gtk_frame_remove (GtkContainer *container,
271 GtkFrame *frame = GTK_FRAME (container);
273 if (frame->label_widget == child)
274 gtk_frame_set_label_widget (frame, NULL);
276 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
280 gtk_frame_forall (GtkContainer *container,
281 gboolean include_internals,
282 GtkCallback callback,
283 gpointer callback_data)
285 GtkBin *bin = GTK_BIN (container);
286 GtkFrame *frame = GTK_FRAME (container);
289 (* callback) (bin->child, callback_data);
291 if (frame->label_widget)
292 (* callback) (frame->label_widget, callback_data);
296 gtk_frame_set_label (GtkFrame *frame,
299 g_return_if_fail (GTK_IS_FRAME (frame));
303 gtk_frame_set_label_widget (frame, NULL);
307 GtkWidget *child = gtk_label_new (label);
308 gtk_widget_show (child);
310 gtk_frame_set_label_widget (frame, child);
313 g_object_notify (G_OBJECT (frame), "label");
317 * gtk_frame_get_label:
318 * @frame: a #GtkFrame
320 * If the frame's label widget is a #GtkLabel, return the
321 * text in the label widget. (The frame will have a #GtkLabel
322 * for the label widget if a non-%NULL argument was passed
323 * to gtk_frame_new().)
325 * Return value: the text in the label, or %NULL if there
326 * was no label widget or the lable widget was not
327 * a #GtkLabel. This value must be freed with g_free().
329 G_CONST_RETURN gchar *
330 gtk_frame_get_label (GtkFrame *frame)
332 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
334 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
335 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
341 * gtk_frame_set_label_widget:
342 * @frame: a #GtkFrame
343 * @label_widget: the new label widget
345 * Set the label widget for the frame. This is the widget that
346 * will appear embedded in the top edge of the frame as a
350 gtk_frame_set_label_widget (GtkFrame *frame,
351 GtkWidget *label_widget)
353 gboolean need_resize = FALSE;
355 g_return_if_fail (GTK_IS_FRAME (frame));
356 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
357 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
359 if (frame->label_widget == label_widget)
362 if (frame->label_widget)
364 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
365 gtk_widget_unparent (frame->label_widget);
368 frame->label_widget = label_widget;
372 frame->label_widget = label_widget;
373 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
374 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
377 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
378 gtk_widget_queue_resize (GTK_WIDGET (frame));
380 g_object_notify (G_OBJECT (frame), "label_widget");
384 * gtk_frame_get_label_widget:
385 * @frame: a #GtkFrame
387 * Retrieves the label widget for the frame. See
388 * gtk_frame_set_label_widget().
390 * Return value: the label widget, or %NULL if there is none.
393 gtk_frame_get_label_widget (GtkFrame *frame)
395 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
397 return frame->label_widget;
401 gtk_frame_set_label_align (GtkFrame *frame,
405 g_return_if_fail (GTK_IS_FRAME (frame));
407 xalign = CLAMP (xalign, 0.0, 1.0);
408 yalign = CLAMP (yalign, 0.0, 1.0);
410 if (xalign != frame->label_xalign)
412 frame->label_xalign = xalign;
413 g_object_notify (G_OBJECT (frame), "label_xalign");
416 if (yalign != frame->label_yalign)
418 frame->label_yalign = yalign;
419 g_object_notify (G_OBJECT (frame), "label_yalign");
422 gtk_widget_queue_resize (GTK_WIDGET (frame));
426 * gtk_frame_get_label_align:
427 * @frame: a #GtkFrame
428 * @xalign: location to store X alignment of frame's label, or %NULL
429 * @yalign: location to store X alignment of frame's label, or %NULL
431 * Retrieves the X and Y alignment of the frame's label. See
432 * gtk_frame_set_label_align().
435 gtk_frame_get_label_align (GtkFrame *frame,
439 g_return_if_fail (GTK_IS_FRAME (frame));
442 *xalign = frame->label_xalign;
444 *yalign = frame->label_yalign;
448 gtk_frame_set_shadow_type (GtkFrame *frame,
451 g_return_if_fail (GTK_IS_FRAME (frame));
453 if ((GtkShadowType) frame->shadow_type != type)
455 frame->shadow_type = type;
456 g_object_notify (G_OBJECT (frame), "shadow");
458 if (GTK_WIDGET_DRAWABLE (frame))
460 gtk_widget_queue_clear (GTK_WIDGET (frame));
463 gtk_widget_queue_resize (GTK_WIDGET (frame));
468 * gtk_frame_get_shadow_type:
469 * @frame: a #GtkFrame
471 * Retrieves the shadow type of the frame. See
472 * gtk_frame_set_shadow_type().
474 * Return value: the current shadow type of the frame.
477 gtk_frame_get_shadow_type (GtkFrame *frame)
479 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
481 return frame->shadow_type;
485 gtk_frame_paint (GtkWidget *widget,
489 gint x, y, width, height;
491 if (GTK_WIDGET_DRAWABLE (widget))
493 frame = GTK_FRAME (widget);
495 x = frame->child_allocation.x - widget->style->xthickness;
496 y = frame->child_allocation.y - widget->style->ythickness;
497 width = frame->child_allocation.width + 2 * widget->style->xthickness;
498 height = frame->child_allocation.height + 2 * widget->style->ythickness;
500 if (frame->label_widget)
502 GtkRequisition child_requisition;
507 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
509 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
510 xalign = frame->label_xalign;
512 xalign = 1 - frame->label_xalign;
514 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
515 y -= height_extra * (1 - frame->label_yalign);
516 height += height_extra * (1 - frame->label_yalign);
518 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
521 gtk_paint_shadow_gap (widget->style, widget->window,
522 GTK_STATE_NORMAL, frame->shadow_type,
523 area, widget, "frame",
526 x2, child_requisition.width + 2 * LABEL_PAD);
529 gtk_paint_shadow (widget->style, widget->window,
530 GTK_STATE_NORMAL, frame->shadow_type,
531 area, widget, "frame",
532 x, y, width, height);
537 gtk_frame_expose (GtkWidget *widget,
538 GdkEventExpose *event)
540 if (GTK_WIDGET_DRAWABLE (widget))
542 gtk_frame_paint (widget, &event->area);
544 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
551 gtk_frame_size_request (GtkWidget *widget,
552 GtkRequisition *requisition)
554 GtkFrame *frame = GTK_FRAME (widget);
555 GtkBin *bin = GTK_BIN (widget);
556 GtkRequisition child_requisition;
558 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
560 gtk_widget_size_request (frame->label_widget, &child_requisition);
562 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
563 requisition->height =
564 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
568 requisition->width = 0;
569 requisition->height = 0;
572 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
574 gtk_widget_size_request (bin->child, &child_requisition);
576 requisition->width = MAX (requisition->width, child_requisition.width);
577 requisition->height += child_requisition.height;
580 requisition->width += (GTK_CONTAINER (widget)->border_width +
581 GTK_WIDGET (widget)->style->xthickness) * 2;
582 requisition->height += (GTK_CONTAINER (widget)->border_width +
583 GTK_WIDGET (widget)->style->ythickness) * 2;
587 gtk_frame_size_allocate (GtkWidget *widget,
588 GtkAllocation *allocation)
590 GtkFrame *frame = GTK_FRAME (widget);
591 GtkBin *bin = GTK_BIN (widget);
592 GtkAllocation new_allocation;
594 widget->allocation = *allocation;
596 gtk_frame_compute_child_allocation (frame, &new_allocation);
598 /* If the child allocation changed, that means that the frame is drawn
599 * in a new place, so we must redraw the entire widget.
601 if (GTK_WIDGET_MAPPED (widget) &&
602 (new_allocation.x != frame->child_allocation.x ||
603 new_allocation.y != frame->child_allocation.y ||
604 new_allocation.width != frame->child_allocation.width ||
605 new_allocation.height != frame->child_allocation.height))
606 gtk_widget_queue_clear (widget);
608 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
609 gtk_widget_size_allocate (bin->child, &new_allocation);
611 frame->child_allocation = new_allocation;
613 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
615 GtkRequisition child_requisition;
616 GtkAllocation child_allocation;
619 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
621 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
622 xalign = frame->label_xalign;
624 xalign = 1 - frame->label_xalign;
626 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
627 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
628 child_allocation.width = child_requisition.width;
630 child_allocation.y = frame->child_allocation.y - child_requisition.height;
631 child_allocation.height = child_requisition.height;
633 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
638 gtk_frame_compute_child_allocation (GtkFrame *frame,
639 GtkAllocation *child_allocation)
641 g_return_if_fail (GTK_IS_FRAME (frame));
642 g_return_if_fail (child_allocation != NULL);
644 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
648 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
649 GtkAllocation *child_allocation)
651 GtkWidget *widget = GTK_WIDGET (frame);
652 GtkAllocation *allocation = &widget->allocation;
653 GtkRequisition child_requisition;
656 if (frame->label_widget)
658 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
659 top_margin = MAX (child_requisition.height, widget->style->ythickness);
662 top_margin = widget->style->ythickness;
664 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
665 widget->style->xthickness);
666 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
668 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
669 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
670 (gint)GTK_CONTAINER (frame)->border_width -
671 (gint)widget->style->ythickness));
673 child_allocation->x += allocation->x;
674 child_allocation->y += allocation->y;