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 child = gtk_bin_get_child (bin);
309 (* callback) (child, callback_data);
311 if (frame->label_widget)
312 (* callback) (frame->label_widget, callback_data);
316 * gtk_frame_set_label:
317 * @frame: a #GtkFrame
318 * @label: (allow-none): the text to use as the label of the frame
320 * Sets the text of the label. If @label is %NULL,
321 * the current label is removed.
324 gtk_frame_set_label (GtkFrame *frame,
327 g_return_if_fail (GTK_IS_FRAME (frame));
331 gtk_frame_set_label_widget (frame, NULL);
335 GtkWidget *child = gtk_label_new (label);
336 gtk_widget_show (child);
338 gtk_frame_set_label_widget (frame, child);
343 * gtk_frame_get_label:
344 * @frame: a #GtkFrame
346 * If the frame's label widget is a #GtkLabel, returns the
347 * text in the label widget. (The frame will have a #GtkLabel
348 * for the label widget if a non-%NULL argument was passed
349 * to gtk_frame_new().)
351 * Return value: the text in the label, or %NULL if there
352 * was no label widget or the lable widget was not
353 * a #GtkLabel. This string is owned by GTK+ and
354 * must not be modified or freed.
356 G_CONST_RETURN gchar *
357 gtk_frame_get_label (GtkFrame *frame)
359 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
361 if (GTK_IS_LABEL (frame->label_widget))
362 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
368 * gtk_frame_set_label_widget:
369 * @frame: a #GtkFrame
370 * @label_widget: the new label widget
372 * Sets the label widget for the frame. This is the widget that
373 * will appear embedded in the top edge of the frame as a
377 gtk_frame_set_label_widget (GtkFrame *frame,
378 GtkWidget *label_widget)
380 gboolean need_resize = FALSE;
382 g_return_if_fail (GTK_IS_FRAME (frame));
383 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
384 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
386 if (frame->label_widget == label_widget)
389 if (frame->label_widget)
391 need_resize = gtk_widget_get_visible (frame->label_widget);
392 gtk_widget_unparent (frame->label_widget);
395 frame->label_widget = label_widget;
399 frame->label_widget = label_widget;
400 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
401 need_resize |= gtk_widget_get_visible (label_widget);
404 if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
405 gtk_widget_queue_resize (GTK_WIDGET (frame));
407 g_object_freeze_notify (G_OBJECT (frame));
408 g_object_notify (G_OBJECT (frame), "label-widget");
409 g_object_notify (G_OBJECT (frame), "label");
410 g_object_thaw_notify (G_OBJECT (frame));
414 * gtk_frame_get_label_widget:
415 * @frame: a #GtkFrame
417 * Retrieves the label widget for the frame. See
418 * gtk_frame_set_label_widget().
420 * Return value: the label widget, or %NULL if there is none.
423 gtk_frame_get_label_widget (GtkFrame *frame)
425 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
427 return frame->label_widget;
431 * gtk_frame_set_label_align:
432 * @frame: a #GtkFrame
433 * @xalign: The position of the label along the top edge
434 * of the widget. A value of 0.0 represents left alignment;
435 * 1.0 represents right alignment.
436 * @yalign: The y alignment of the label. A value of 0.0 aligns under
437 * the frame; 1.0 aligns above the frame. If the values are exactly
438 * 0.0 or 1.0 the gap in the frame won't be painted because the label
439 * will be completely above or below the frame.
441 * Sets the alignment of the frame widget's label. The
442 * default values for a newly created frame are 0.0 and 0.5.
445 gtk_frame_set_label_align (GtkFrame *frame,
449 g_return_if_fail (GTK_IS_FRAME (frame));
451 xalign = CLAMP (xalign, 0.0, 1.0);
452 yalign = CLAMP (yalign, 0.0, 1.0);
454 g_object_freeze_notify (G_OBJECT (frame));
455 if (xalign != frame->label_xalign)
457 frame->label_xalign = xalign;
458 g_object_notify (G_OBJECT (frame), "label-xalign");
461 if (yalign != frame->label_yalign)
463 frame->label_yalign = yalign;
464 g_object_notify (G_OBJECT (frame), "label-yalign");
467 g_object_thaw_notify (G_OBJECT (frame));
468 gtk_widget_queue_resize (GTK_WIDGET (frame));
472 * gtk_frame_get_label_align:
473 * @frame: a #GtkFrame
474 * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
475 * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
477 * Retrieves the X and Y alignment of the frame's label. See
478 * gtk_frame_set_label_align().
481 gtk_frame_get_label_align (GtkFrame *frame,
485 g_return_if_fail (GTK_IS_FRAME (frame));
488 *xalign = frame->label_xalign;
490 *yalign = frame->label_yalign;
494 * gtk_frame_set_shadow_type:
495 * @frame: a #GtkFrame
496 * @type: the new #GtkShadowType
498 * Sets the shadow type for @frame.
501 gtk_frame_set_shadow_type (GtkFrame *frame,
506 g_return_if_fail (GTK_IS_FRAME (frame));
508 if ((GtkShadowType) frame->shadow_type != type)
510 widget = GTK_WIDGET (frame);
511 frame->shadow_type = type;
512 g_object_notify (G_OBJECT (frame), "shadow-type");
514 if (gtk_widget_is_drawable (widget))
516 gtk_widget_queue_draw (widget);
519 gtk_widget_queue_resize (widget);
524 * gtk_frame_get_shadow_type:
525 * @frame: a #GtkFrame
527 * Retrieves the shadow type of the frame. See
528 * gtk_frame_set_shadow_type().
530 * Return value: the current shadow type of the frame.
533 gtk_frame_get_shadow_type (GtkFrame *frame)
535 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
537 return frame->shadow_type;
541 gtk_frame_paint (GtkWidget *widget,
545 gint x, y, width, height;
547 if (gtk_widget_is_drawable (widget))
549 frame = GTK_FRAME (widget);
551 x = frame->child_allocation.x - widget->style->xthickness;
552 y = frame->child_allocation.y - widget->style->ythickness;
553 width = frame->child_allocation.width + 2 * widget->style->xthickness;
554 height = frame->child_allocation.height + 2 * widget->style->ythickness;
556 if (frame->label_widget)
558 GtkRequisition child_requisition;
563 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
565 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
566 xalign = frame->label_xalign;
568 xalign = 1 - frame->label_xalign;
570 height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
571 - frame->label_yalign * child_requisition.height;
573 height += height_extra;
575 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
577 /* If the label is completely over or under the frame we can omit the gap */
578 if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
579 gtk_paint_shadow (widget->style, widget->window,
580 widget->state, frame->shadow_type,
581 area, widget, "frame",
582 x, y, width, height);
584 gtk_paint_shadow_gap (widget->style, widget->window,
585 widget->state, frame->shadow_type,
586 area, widget, "frame",
589 x2, child_requisition.width + 2 * LABEL_PAD);
592 gtk_paint_shadow (widget->style, widget->window,
593 widget->state, frame->shadow_type,
594 area, widget, "frame",
595 x, y, width, height);
600 gtk_frame_expose (GtkWidget *widget,
601 GdkEventExpose *event)
603 if (gtk_widget_is_drawable (widget))
605 gtk_frame_paint (widget, &event->area);
607 GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
614 gtk_frame_size_allocate (GtkWidget *widget,
615 GtkAllocation *allocation)
617 GtkFrame *frame = GTK_FRAME (widget);
618 GtkBin *bin = GTK_BIN (widget);
619 GtkAllocation new_allocation;
622 widget->allocation = *allocation;
624 gtk_frame_compute_child_allocation (frame, &new_allocation);
626 /* If the child allocation changed, that means that the frame is drawn
627 * in a new place, so we must redraw the entire widget.
629 if (gtk_widget_get_mapped (widget)
632 (new_allocation.x != frame->child_allocation.x ||
633 new_allocation.y != frame->child_allocation.y ||
634 new_allocation.width != frame->child_allocation.width ||
635 new_allocation.height != frame->child_allocation.height)
638 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
640 child = gtk_bin_get_child (bin);
641 if (child && gtk_widget_get_visible (child))
642 gtk_widget_size_allocate (child, &new_allocation);
644 frame->child_allocation = new_allocation;
646 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
648 GtkRequisition child_requisition;
649 GtkAllocation child_allocation;
652 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
654 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
655 xalign = frame->label_xalign;
657 xalign = 1 - frame->label_xalign;
659 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
660 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
661 child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
663 child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
664 child_allocation.height = child_requisition.height;
666 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
671 gtk_frame_compute_child_allocation (GtkFrame *frame,
672 GtkAllocation *child_allocation)
674 g_return_if_fail (GTK_IS_FRAME (frame));
675 g_return_if_fail (child_allocation != NULL);
677 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
681 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
682 GtkAllocation *child_allocation)
684 GtkWidget *widget = GTK_WIDGET (frame);
685 GtkAllocation *allocation = &widget->allocation;
686 GtkRequisition child_requisition;
690 if (frame->label_widget)
692 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
693 top_margin = MAX (child_requisition.height, widget->style->ythickness);
696 top_margin = widget->style->ythickness;
698 border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
700 child_allocation->x = border_width + widget->style->xthickness;
701 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
703 child_allocation->y = border_width + top_margin;
704 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
706 (gint)widget->style->ythickness));
708 child_allocation->x += allocation->x;
709 child_allocation->y += allocation->y;
713 gtk_frame_get_size (GtkSizeRequest *request,
714 GtkOrientation orientation,
718 GtkWidget *widget = GTK_WIDGET (request);
720 GtkFrame *frame = GTK_FRAME (widget);
721 GtkBin *bin = GTK_BIN (widget);
722 gint child_min, child_nat;
723 gint minimum, natural;
726 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
728 if (orientation == GTK_ORIENTATION_HORIZONTAL)
730 gtk_size_request_get_width (GTK_SIZE_REQUEST (frame->label_widget),
731 &child_min, &child_nat);
732 minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
733 natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
737 gtk_size_request_get_height (GTK_SIZE_REQUEST (frame->label_widget),
738 &child_min, &child_nat);
739 minimum = MAX (0, child_min - widget->style->ythickness);
740 natural = MAX (0, child_nat - widget->style->ythickness);
749 child = gtk_bin_get_child (bin);
750 if (child && gtk_widget_get_visible (child))
752 if (orientation == GTK_ORIENTATION_HORIZONTAL)
754 gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
755 &child_min, &child_nat);
756 minimum = MAX (minimum, child_min);
757 natural = MAX (natural, child_nat);
761 gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
762 &child_min, &child_nat);
763 minimum += child_min;
764 natural += child_nat;
768 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
770 if (orientation == GTK_ORIENTATION_HORIZONTAL)
772 minimum += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
773 natural += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
777 minimum += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
778 natural += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
782 *minimum_size = minimum;
785 *natural_size = natural;
789 gtk_frame_get_width (GtkSizeRequest *widget,
793 gtk_frame_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
797 gtk_frame_get_height (GtkSizeRequest *widget,
801 gtk_frame_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
805 gtk_frame_size_request_init (GtkSizeRequestIface *iface)
807 iface->get_width = gtk_frame_get_width;
808 iface->get_height = gtk_frame_get_height;