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/.
31 #include "gtkprivate.h"
33 #include "gtkbuildable.h"
34 #include "gtksizerequest.h"
37 #define LABEL_SIDE_PAD 2
49 static void gtk_frame_set_property (GObject *object,
53 static void gtk_frame_get_property (GObject *object,
57 static void gtk_frame_paint (GtkWidget *widget,
59 static gint gtk_frame_expose (GtkWidget *widget,
60 GdkEventExpose *event);
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);
76 static void gtk_frame_buildable_init (GtkBuildableIface *iface);
77 static void gtk_frame_buildable_add_child (GtkBuildable *buildable,
82 static void gtk_frame_size_request_init (GtkSizeRequestIface *iface);
83 static void gtk_frame_get_width (GtkSizeRequest *widget,
86 static void gtk_frame_get_height (GtkSizeRequest *widget,
90 G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
91 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
92 gtk_frame_buildable_init)
93 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
94 gtk_frame_size_request_init))
97 gtk_frame_class_init (GtkFrameClass *class)
99 GObjectClass *gobject_class;
100 GtkWidgetClass *widget_class;
101 GtkContainerClass *container_class;
103 gobject_class = (GObjectClass*) class;
104 widget_class = GTK_WIDGET_CLASS (class);
105 container_class = GTK_CONTAINER_CLASS (class);
107 gobject_class->set_property = gtk_frame_set_property;
108 gobject_class->get_property = gtk_frame_get_property;
110 g_object_class_install_property (gobject_class,
112 g_param_spec_string ("label",
114 P_("Text of the frame's label"),
117 GTK_PARAM_WRITABLE));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_float ("label-xalign",
122 P_("The horizontal alignment of the label"),
126 GTK_PARAM_READWRITE));
127 g_object_class_install_property (gobject_class,
129 g_param_spec_float ("label-yalign",
131 P_("The vertical alignment of the label"),
135 GTK_PARAM_READWRITE));
136 g_object_class_install_property (gobject_class,
138 g_param_spec_enum ("shadow", NULL,
139 P_("Deprecated property, use shadow_type instead"),
140 GTK_TYPE_SHADOW_TYPE,
141 GTK_SHADOW_ETCHED_IN,
142 GTK_PARAM_READWRITE));
143 g_object_class_install_property (gobject_class,
145 g_param_spec_enum ("shadow-type",
147 P_("Appearance of the frame border"),
148 GTK_TYPE_SHADOW_TYPE,
149 GTK_SHADOW_ETCHED_IN,
150 GTK_PARAM_READWRITE));
152 g_object_class_install_property (gobject_class,
154 g_param_spec_object ("label-widget",
156 P_("A widget to display in place of the usual frame label"),
158 GTK_PARAM_READWRITE));
160 widget_class->expose_event = gtk_frame_expose;
161 widget_class->size_allocate = gtk_frame_size_allocate;
163 container_class->remove = gtk_frame_remove;
164 container_class->forall = gtk_frame_forall;
166 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
170 gtk_frame_buildable_init (GtkBuildableIface *iface)
172 iface->add_child = gtk_frame_buildable_add_child;
176 gtk_frame_buildable_add_child (GtkBuildable *buildable,
181 if (type && strcmp (type, "label") == 0)
182 gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
184 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
186 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
190 gtk_frame_init (GtkFrame *frame)
192 frame->label_widget = NULL;
193 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
194 frame->label_xalign = 0.0;
195 frame->label_yalign = 0.5;
199 gtk_frame_set_property (GObject *object,
206 frame = GTK_FRAME (object);
211 gtk_frame_set_label (frame, g_value_get_string (value));
213 case PROP_LABEL_XALIGN:
214 gtk_frame_set_label_align (frame, g_value_get_float (value),
215 frame->label_yalign);
217 case PROP_LABEL_YALIGN:
218 gtk_frame_set_label_align (frame, frame->label_xalign,
219 g_value_get_float (value));
222 case PROP_SHADOW_TYPE:
223 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
225 case PROP_LABEL_WIDGET:
226 gtk_frame_set_label_widget (frame, g_value_get_object (value));
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 gtk_frame_get_property (GObject *object,
242 frame = GTK_FRAME (object);
247 g_value_set_string (value, gtk_frame_get_label (frame));
249 case PROP_LABEL_XALIGN:
250 g_value_set_float (value, frame->label_xalign);
252 case PROP_LABEL_YALIGN:
253 g_value_set_float (value, frame->label_yalign);
256 case PROP_SHADOW_TYPE:
257 g_value_set_enum (value, frame->shadow_type);
259 case PROP_LABEL_WIDGET:
260 g_value_set_object (value,
261 frame->label_widget ?
262 G_OBJECT (frame->label_widget) : NULL);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 * @label: the text to use as the label of the frame
274 * Creates a new #GtkFrame, with optional label @label.
275 * If @label is %NULL, the label is omitted.
277 * Return value: a new #GtkFrame widget
280 gtk_frame_new (const gchar *label)
282 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
286 gtk_frame_remove (GtkContainer *container,
289 GtkFrame *frame = GTK_FRAME (container);
291 if (frame->label_widget == child)
292 gtk_frame_set_label_widget (frame, NULL);
294 GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
298 gtk_frame_forall (GtkContainer *container,
299 gboolean include_internals,
300 GtkCallback callback,
301 gpointer callback_data)
303 GtkBin *bin = GTK_BIN (container);
304 GtkFrame *frame = GTK_FRAME (container);
307 (* callback) (bin->child, callback_data);
309 if (frame->label_widget)
310 (* callback) (frame->label_widget, callback_data);
314 * gtk_frame_set_label:
315 * @frame: a #GtkFrame
316 * @label: (allow-none): the text to use as the label of the frame
318 * Sets the text of the label. If @label is %NULL,
319 * the current label is removed.
322 gtk_frame_set_label (GtkFrame *frame,
325 g_return_if_fail (GTK_IS_FRAME (frame));
329 gtk_frame_set_label_widget (frame, NULL);
333 GtkWidget *child = gtk_label_new (label);
334 gtk_widget_show (child);
336 gtk_frame_set_label_widget (frame, child);
341 * gtk_frame_get_label:
342 * @frame: a #GtkFrame
344 * If the frame's label widget is a #GtkLabel, returns the
345 * text in the label widget. (The frame will have a #GtkLabel
346 * for the label widget if a non-%NULL argument was passed
347 * to gtk_frame_new().)
349 * Return value: the text in the label, or %NULL if there
350 * was no label widget or the lable widget was not
351 * a #GtkLabel. This string is owned by GTK+ and
352 * must not be modified or freed.
354 G_CONST_RETURN gchar *
355 gtk_frame_get_label (GtkFrame *frame)
357 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
359 if (GTK_IS_LABEL (frame->label_widget))
360 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
366 * gtk_frame_set_label_widget:
367 * @frame: a #GtkFrame
368 * @label_widget: the new label widget
370 * Sets the label widget for the frame. This is the widget that
371 * will appear embedded in the top edge of the frame as a
375 gtk_frame_set_label_widget (GtkFrame *frame,
376 GtkWidget *label_widget)
378 gboolean need_resize = FALSE;
380 g_return_if_fail (GTK_IS_FRAME (frame));
381 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
382 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
384 if (frame->label_widget == label_widget)
387 if (frame->label_widget)
389 need_resize = gtk_widget_get_visible (frame->label_widget);
390 gtk_widget_unparent (frame->label_widget);
393 frame->label_widget = label_widget;
397 frame->label_widget = label_widget;
398 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
399 need_resize |= gtk_widget_get_visible (label_widget);
402 if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
403 gtk_widget_queue_resize (GTK_WIDGET (frame));
405 g_object_freeze_notify (G_OBJECT (frame));
406 g_object_notify (G_OBJECT (frame), "label-widget");
407 g_object_notify (G_OBJECT (frame), "label");
408 g_object_thaw_notify (G_OBJECT (frame));
412 * gtk_frame_get_label_widget:
413 * @frame: a #GtkFrame
415 * Retrieves the label widget for the frame. See
416 * gtk_frame_set_label_widget().
418 * Return value: the label widget, or %NULL if there is none.
421 gtk_frame_get_label_widget (GtkFrame *frame)
423 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
425 return frame->label_widget;
429 * gtk_frame_set_label_align:
430 * @frame: a #GtkFrame
431 * @xalign: The position of the label along the top edge
432 * of the widget. A value of 0.0 represents left alignment;
433 * 1.0 represents right alignment.
434 * @yalign: The y alignment of the label. A value of 0.0 aligns under
435 * the frame; 1.0 aligns above the frame. If the values are exactly
436 * 0.0 or 1.0 the gap in the frame won't be painted because the label
437 * will be completely above or below the frame.
439 * Sets the alignment of the frame widget's label. The
440 * default values for a newly created frame are 0.0 and 0.5.
443 gtk_frame_set_label_align (GtkFrame *frame,
447 g_return_if_fail (GTK_IS_FRAME (frame));
449 xalign = CLAMP (xalign, 0.0, 1.0);
450 yalign = CLAMP (yalign, 0.0, 1.0);
452 g_object_freeze_notify (G_OBJECT (frame));
453 if (xalign != frame->label_xalign)
455 frame->label_xalign = xalign;
456 g_object_notify (G_OBJECT (frame), "label-xalign");
459 if (yalign != frame->label_yalign)
461 frame->label_yalign = yalign;
462 g_object_notify (G_OBJECT (frame), "label-yalign");
465 g_object_thaw_notify (G_OBJECT (frame));
466 gtk_widget_queue_resize (GTK_WIDGET (frame));
470 * gtk_frame_get_label_align:
471 * @frame: a #GtkFrame
472 * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
473 * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
475 * Retrieves the X and Y alignment of the frame's label. See
476 * gtk_frame_set_label_align().
479 gtk_frame_get_label_align (GtkFrame *frame,
483 g_return_if_fail (GTK_IS_FRAME (frame));
486 *xalign = frame->label_xalign;
488 *yalign = frame->label_yalign;
492 * gtk_frame_set_shadow_type:
493 * @frame: a #GtkFrame
494 * @type: the new #GtkShadowType
496 * Sets the shadow type for @frame.
499 gtk_frame_set_shadow_type (GtkFrame *frame,
504 g_return_if_fail (GTK_IS_FRAME (frame));
506 if ((GtkShadowType) frame->shadow_type != type)
508 widget = GTK_WIDGET (frame);
509 frame->shadow_type = type;
510 g_object_notify (G_OBJECT (frame), "shadow-type");
512 if (gtk_widget_is_drawable (widget))
514 gtk_widget_queue_draw (widget);
517 gtk_widget_queue_resize (widget);
522 * gtk_frame_get_shadow_type:
523 * @frame: a #GtkFrame
525 * Retrieves the shadow type of the frame. See
526 * gtk_frame_set_shadow_type().
528 * Return value: the current shadow type of the frame.
531 gtk_frame_get_shadow_type (GtkFrame *frame)
533 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
535 return frame->shadow_type;
539 gtk_frame_paint (GtkWidget *widget,
543 gint x, y, width, height;
545 if (gtk_widget_is_drawable (widget))
547 frame = GTK_FRAME (widget);
549 x = frame->child_allocation.x - widget->style->xthickness;
550 y = frame->child_allocation.y - widget->style->ythickness;
551 width = frame->child_allocation.width + 2 * widget->style->xthickness;
552 height = frame->child_allocation.height + 2 * widget->style->ythickness;
554 if (frame->label_widget)
556 GtkRequisition child_requisition;
561 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
563 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
564 xalign = frame->label_xalign;
566 xalign = 1 - frame->label_xalign;
568 height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
569 - frame->label_yalign * child_requisition.height;
571 height += height_extra;
573 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
575 /* If the label is completely over or under the frame we can omit the gap */
576 if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
577 gtk_paint_shadow (widget->style, widget->window,
578 widget->state, frame->shadow_type,
579 area, widget, "frame",
580 x, y, width, height);
582 gtk_paint_shadow_gap (widget->style, widget->window,
583 widget->state, frame->shadow_type,
584 area, widget, "frame",
587 x2, child_requisition.width + 2 * LABEL_PAD);
590 gtk_paint_shadow (widget->style, widget->window,
591 widget->state, frame->shadow_type,
592 area, widget, "frame",
593 x, y, width, height);
598 gtk_frame_expose (GtkWidget *widget,
599 GdkEventExpose *event)
601 if (gtk_widget_is_drawable (widget))
603 gtk_frame_paint (widget, &event->area);
605 GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
612 gtk_frame_size_allocate (GtkWidget *widget,
613 GtkAllocation *allocation)
615 GtkFrame *frame = GTK_FRAME (widget);
616 GtkBin *bin = GTK_BIN (widget);
617 GtkAllocation new_allocation;
619 widget->allocation = *allocation;
621 gtk_frame_compute_child_allocation (frame, &new_allocation);
623 /* If the child allocation changed, that means that the frame is drawn
624 * in a new place, so we must redraw the entire widget.
626 if (gtk_widget_get_mapped (widget)
629 (new_allocation.x != frame->child_allocation.x ||
630 new_allocation.y != frame->child_allocation.y ||
631 new_allocation.width != frame->child_allocation.width ||
632 new_allocation.height != frame->child_allocation.height)
635 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
637 if (bin->child && gtk_widget_get_visible (bin->child))
638 gtk_widget_size_allocate (bin->child, &new_allocation);
640 frame->child_allocation = new_allocation;
642 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
644 GtkRequisition child_requisition;
645 GtkAllocation child_allocation;
648 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
650 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
651 xalign = frame->label_xalign;
653 xalign = 1 - frame->label_xalign;
655 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
656 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
657 child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
659 child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
660 child_allocation.height = child_requisition.height;
662 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
667 gtk_frame_compute_child_allocation (GtkFrame *frame,
668 GtkAllocation *child_allocation)
670 g_return_if_fail (GTK_IS_FRAME (frame));
671 g_return_if_fail (child_allocation != NULL);
673 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
677 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
678 GtkAllocation *child_allocation)
680 GtkWidget *widget = GTK_WIDGET (frame);
681 GtkAllocation *allocation = &widget->allocation;
682 GtkRequisition child_requisition;
686 if (frame->label_widget)
688 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
689 top_margin = MAX (child_requisition.height, widget->style->ythickness);
692 top_margin = widget->style->ythickness;
694 border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
696 child_allocation->x = border_width + widget->style->xthickness;
697 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
699 child_allocation->y = border_width + top_margin;
700 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
702 (gint)widget->style->ythickness));
704 child_allocation->x += allocation->x;
705 child_allocation->y += allocation->y;
709 gtk_frame_get_size (GtkSizeRequest *request,
710 GtkOrientation orientation,
714 GtkWidget *widget = GTK_WIDGET (request);
715 GtkFrame *frame = GTK_FRAME (widget);
716 GtkBin *bin = GTK_BIN (widget);
717 gint child_min, child_nat;
718 gint minimum, natural;
721 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
723 if (orientation == GTK_ORIENTATION_HORIZONTAL)
725 gtk_size_request_get_width (GTK_SIZE_REQUEST (frame->label_widget),
726 &child_min, &child_nat);
727 minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
728 natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
732 gtk_size_request_get_height (GTK_SIZE_REQUEST (frame->label_widget),
733 &child_min, &child_nat);
734 minimum = MAX (0, child_min - widget->style->ythickness);
735 natural = MAX (0, child_nat - widget->style->ythickness);
744 if (bin->child && gtk_widget_get_visible (bin->child))
746 if (orientation == GTK_ORIENTATION_HORIZONTAL)
748 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child),
749 &child_min, &child_nat);
750 minimum = MAX (minimum, child_min);
751 natural = MAX (natural, child_nat);
755 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child),
756 &child_min, &child_nat);
757 minimum += child_min;
758 natural += child_nat;
762 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
764 if (orientation == GTK_ORIENTATION_HORIZONTAL)
766 minimum += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
767 natural += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
771 minimum += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
772 natural += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
776 *minimum_size = minimum;
779 *natural_size = natural;
783 gtk_frame_get_width (GtkSizeRequest *widget,
787 gtk_frame_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
791 gtk_frame_get_height (GtkSizeRequest *widget,
795 gtk_frame_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
799 gtk_frame_size_request_init (GtkSizeRequestIface *iface)
801 iface->get_width = gtk_frame_get_width;
802 iface->get_height = gtk_frame_get_height;