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"
38 #define LABEL_SIDE_PAD 2
50 static void gtk_frame_set_property (GObject *object,
54 static void gtk_frame_get_property (GObject *object,
58 static void gtk_frame_paint (GtkWidget *widget,
60 static gint gtk_frame_expose (GtkWidget *widget,
61 GdkEventExpose *event);
62 static void gtk_frame_size_allocate (GtkWidget *widget,
63 GtkAllocation *allocation);
64 static void gtk_frame_remove (GtkContainer *container,
66 static void gtk_frame_forall (GtkContainer *container,
67 gboolean include_internals,
69 gpointer callback_data);
71 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
72 GtkAllocation *child_allocation);
73 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
74 GtkAllocation *child_allocation);
77 static void gtk_frame_buildable_init (GtkBuildableIface *iface);
78 static void gtk_frame_buildable_add_child (GtkBuildable *buildable,
83 static void gtk_frame_size_request_init (GtkSizeRequestIface *iface);
84 static void gtk_frame_get_width (GtkSizeRequest *widget,
87 static void gtk_frame_get_height (GtkSizeRequest *widget,
91 G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
92 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
93 gtk_frame_buildable_init)
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
95 gtk_frame_size_request_init))
98 gtk_frame_class_init (GtkFrameClass *class)
100 GObjectClass *gobject_class;
101 GtkWidgetClass *widget_class;
102 GtkContainerClass *container_class;
104 gobject_class = (GObjectClass*) class;
105 widget_class = GTK_WIDGET_CLASS (class);
106 container_class = GTK_CONTAINER_CLASS (class);
108 gobject_class->set_property = gtk_frame_set_property;
109 gobject_class->get_property = gtk_frame_get_property;
111 g_object_class_install_property (gobject_class,
113 g_param_spec_string ("label",
115 P_("Text of the frame's label"),
118 GTK_PARAM_WRITABLE));
119 g_object_class_install_property (gobject_class,
121 g_param_spec_float ("label-xalign",
123 P_("The horizontal alignment of the label"),
127 GTK_PARAM_READWRITE));
128 g_object_class_install_property (gobject_class,
130 g_param_spec_float ("label-yalign",
132 P_("The vertical alignment of the label"),
136 GTK_PARAM_READWRITE));
137 g_object_class_install_property (gobject_class,
139 g_param_spec_enum ("shadow", NULL,
140 P_("Deprecated property, use shadow_type instead"),
141 GTK_TYPE_SHADOW_TYPE,
142 GTK_SHADOW_ETCHED_IN,
143 GTK_PARAM_READWRITE));
144 g_object_class_install_property (gobject_class,
146 g_param_spec_enum ("shadow-type",
148 P_("Appearance of the frame border"),
149 GTK_TYPE_SHADOW_TYPE,
150 GTK_SHADOW_ETCHED_IN,
151 GTK_PARAM_READWRITE));
153 g_object_class_install_property (gobject_class,
155 g_param_spec_object ("label-widget",
157 P_("A widget to display in place of the usual frame label"),
159 GTK_PARAM_READWRITE));
161 widget_class->expose_event = gtk_frame_expose;
162 widget_class->size_allocate = gtk_frame_size_allocate;
164 container_class->remove = gtk_frame_remove;
165 container_class->forall = gtk_frame_forall;
167 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
171 gtk_frame_buildable_init (GtkBuildableIface *iface)
173 iface->add_child = gtk_frame_buildable_add_child;
177 gtk_frame_buildable_add_child (GtkBuildable *buildable,
182 if (type && strcmp (type, "label") == 0)
183 gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
185 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
187 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
191 gtk_frame_init (GtkFrame *frame)
193 frame->label_widget = NULL;
194 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
195 frame->label_xalign = 0.0;
196 frame->label_yalign = 0.5;
200 gtk_frame_set_property (GObject *object,
207 frame = GTK_FRAME (object);
212 gtk_frame_set_label (frame, g_value_get_string (value));
214 case PROP_LABEL_XALIGN:
215 gtk_frame_set_label_align (frame, g_value_get_float (value),
216 frame->label_yalign);
218 case PROP_LABEL_YALIGN:
219 gtk_frame_set_label_align (frame, frame->label_xalign,
220 g_value_get_float (value));
223 case PROP_SHADOW_TYPE:
224 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
226 case PROP_LABEL_WIDGET:
227 gtk_frame_set_label_widget (frame, g_value_get_object (value));
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gtk_frame_get_property (GObject *object,
243 frame = GTK_FRAME (object);
248 g_value_set_string (value, gtk_frame_get_label (frame));
250 case PROP_LABEL_XALIGN:
251 g_value_set_float (value, frame->label_xalign);
253 case PROP_LABEL_YALIGN:
254 g_value_set_float (value, frame->label_yalign);
257 case PROP_SHADOW_TYPE:
258 g_value_set_enum (value, frame->shadow_type);
260 case PROP_LABEL_WIDGET:
261 g_value_set_object (value,
262 frame->label_widget ?
263 G_OBJECT (frame->label_widget) : NULL);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 * @label: the text to use as the label of the frame
275 * Creates a new #GtkFrame, with optional label @label.
276 * If @label is %NULL, the label is omitted.
278 * Return value: a new #GtkFrame widget
281 gtk_frame_new (const gchar *label)
283 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
287 gtk_frame_remove (GtkContainer *container,
290 GtkFrame *frame = GTK_FRAME (container);
292 if (frame->label_widget == child)
293 gtk_frame_set_label_widget (frame, NULL);
295 GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
299 gtk_frame_forall (GtkContainer *container,
300 gboolean include_internals,
301 GtkCallback callback,
302 gpointer callback_data)
304 GtkBin *bin = GTK_BIN (container);
305 GtkFrame *frame = GTK_FRAME (container);
308 (* callback) (bin->child, callback_data);
310 if (frame->label_widget)
311 (* callback) (frame->label_widget, callback_data);
315 * gtk_frame_set_label:
316 * @frame: a #GtkFrame
317 * @label: (allow-none): the text to use as the label of the frame
319 * Sets the text of the label. If @label is %NULL,
320 * the current label is removed.
323 gtk_frame_set_label (GtkFrame *frame,
326 g_return_if_fail (GTK_IS_FRAME (frame));
330 gtk_frame_set_label_widget (frame, NULL);
334 GtkWidget *child = gtk_label_new (label);
335 gtk_widget_show (child);
337 gtk_frame_set_label_widget (frame, child);
342 * gtk_frame_get_label:
343 * @frame: a #GtkFrame
345 * If the frame's label widget is a #GtkLabel, returns the
346 * text in the label widget. (The frame will have a #GtkLabel
347 * for the label widget if a non-%NULL argument was passed
348 * to gtk_frame_new().)
350 * Return value: the text in the label, or %NULL if there
351 * was no label widget or the lable widget was not
352 * a #GtkLabel. This string is owned by GTK+ and
353 * must not be modified or freed.
355 G_CONST_RETURN gchar *
356 gtk_frame_get_label (GtkFrame *frame)
358 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
360 if (GTK_IS_LABEL (frame->label_widget))
361 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
367 * gtk_frame_set_label_widget:
368 * @frame: a #GtkFrame
369 * @label_widget: the new label widget
371 * Sets the label widget for the frame. This is the widget that
372 * will appear embedded in the top edge of the frame as a
376 gtk_frame_set_label_widget (GtkFrame *frame,
377 GtkWidget *label_widget)
379 gboolean need_resize = FALSE;
381 g_return_if_fail (GTK_IS_FRAME (frame));
382 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
383 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
385 if (frame->label_widget == label_widget)
388 if (frame->label_widget)
390 need_resize = gtk_widget_get_visible (frame->label_widget);
391 gtk_widget_unparent (frame->label_widget);
394 frame->label_widget = label_widget;
398 frame->label_widget = label_widget;
399 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
400 need_resize |= gtk_widget_get_visible (label_widget);
403 if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
404 gtk_widget_queue_resize (GTK_WIDGET (frame));
406 g_object_freeze_notify (G_OBJECT (frame));
407 g_object_notify (G_OBJECT (frame), "label-widget");
408 g_object_notify (G_OBJECT (frame), "label");
409 g_object_thaw_notify (G_OBJECT (frame));
413 * gtk_frame_get_label_widget:
414 * @frame: a #GtkFrame
416 * Retrieves the label widget for the frame. See
417 * gtk_frame_set_label_widget().
419 * Return value: the label widget, or %NULL if there is none.
422 gtk_frame_get_label_widget (GtkFrame *frame)
424 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
426 return frame->label_widget;
430 * gtk_frame_set_label_align:
431 * @frame: a #GtkFrame
432 * @xalign: The position of the label along the top edge
433 * of the widget. A value of 0.0 represents left alignment;
434 * 1.0 represents right alignment.
435 * @yalign: The y alignment of the label. A value of 0.0 aligns under
436 * the frame; 1.0 aligns above the frame. If the values are exactly
437 * 0.0 or 1.0 the gap in the frame won't be painted because the label
438 * will be completely above or below the frame.
440 * Sets the alignment of the frame widget's label. The
441 * default values for a newly created frame are 0.0 and 0.5.
444 gtk_frame_set_label_align (GtkFrame *frame,
448 g_return_if_fail (GTK_IS_FRAME (frame));
450 xalign = CLAMP (xalign, 0.0, 1.0);
451 yalign = CLAMP (yalign, 0.0, 1.0);
453 g_object_freeze_notify (G_OBJECT (frame));
454 if (xalign != frame->label_xalign)
456 frame->label_xalign = xalign;
457 g_object_notify (G_OBJECT (frame), "label-xalign");
460 if (yalign != frame->label_yalign)
462 frame->label_yalign = yalign;
463 g_object_notify (G_OBJECT (frame), "label-yalign");
466 g_object_thaw_notify (G_OBJECT (frame));
467 gtk_widget_queue_resize (GTK_WIDGET (frame));
471 * gtk_frame_get_label_align:
472 * @frame: a #GtkFrame
473 * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
474 * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
476 * Retrieves the X and Y alignment of the frame's label. See
477 * gtk_frame_set_label_align().
480 gtk_frame_get_label_align (GtkFrame *frame,
484 g_return_if_fail (GTK_IS_FRAME (frame));
487 *xalign = frame->label_xalign;
489 *yalign = frame->label_yalign;
493 * gtk_frame_set_shadow_type:
494 * @frame: a #GtkFrame
495 * @type: the new #GtkShadowType
497 * Sets the shadow type for @frame.
500 gtk_frame_set_shadow_type (GtkFrame *frame,
505 g_return_if_fail (GTK_IS_FRAME (frame));
507 if ((GtkShadowType) frame->shadow_type != type)
509 widget = GTK_WIDGET (frame);
510 frame->shadow_type = type;
511 g_object_notify (G_OBJECT (frame), "shadow-type");
513 if (gtk_widget_is_drawable (widget))
515 gtk_widget_queue_draw (widget);
518 gtk_widget_queue_resize (widget);
523 * gtk_frame_get_shadow_type:
524 * @frame: a #GtkFrame
526 * Retrieves the shadow type of the frame. See
527 * gtk_frame_set_shadow_type().
529 * Return value: the current shadow type of the frame.
532 gtk_frame_get_shadow_type (GtkFrame *frame)
534 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
536 return frame->shadow_type;
540 gtk_frame_paint (GtkWidget *widget,
544 gint x, y, width, height;
546 if (gtk_widget_is_drawable (widget))
548 frame = GTK_FRAME (widget);
550 x = frame->child_allocation.x - widget->style->xthickness;
551 y = frame->child_allocation.y - widget->style->ythickness;
552 width = frame->child_allocation.width + 2 * widget->style->xthickness;
553 height = frame->child_allocation.height + 2 * widget->style->ythickness;
555 if (frame->label_widget)
557 GtkRequisition child_requisition;
562 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
564 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
565 xalign = frame->label_xalign;
567 xalign = 1 - frame->label_xalign;
569 height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
570 - frame->label_yalign * child_requisition.height;
572 height += height_extra;
574 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
576 /* If the label is completely over or under the frame we can omit the gap */
577 if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
578 gtk_paint_shadow (widget->style, widget->window,
579 widget->state, frame->shadow_type,
580 area, widget, "frame",
581 x, y, width, height);
583 gtk_paint_shadow_gap (widget->style, widget->window,
584 widget->state, frame->shadow_type,
585 area, widget, "frame",
588 x2, child_requisition.width + 2 * LABEL_PAD);
591 gtk_paint_shadow (widget->style, widget->window,
592 widget->state, frame->shadow_type,
593 area, widget, "frame",
594 x, y, width, height);
599 gtk_frame_expose (GtkWidget *widget,
600 GdkEventExpose *event)
602 if (gtk_widget_is_drawable (widget))
604 gtk_frame_paint (widget, &event->area);
606 GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
613 gtk_frame_size_allocate (GtkWidget *widget,
614 GtkAllocation *allocation)
616 GtkFrame *frame = GTK_FRAME (widget);
617 GtkBin *bin = GTK_BIN (widget);
618 GtkAllocation new_allocation;
620 widget->allocation = *allocation;
622 gtk_frame_compute_child_allocation (frame, &new_allocation);
624 /* If the child allocation changed, that means that the frame is drawn
625 * in a new place, so we must redraw the entire widget.
627 if (gtk_widget_get_mapped (widget)
630 (new_allocation.x != frame->child_allocation.x ||
631 new_allocation.y != frame->child_allocation.y ||
632 new_allocation.width != frame->child_allocation.width ||
633 new_allocation.height != frame->child_allocation.height)
636 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
638 if (bin->child && gtk_widget_get_visible (bin->child))
639 gtk_widget_size_allocate (bin->child, &new_allocation);
641 frame->child_allocation = new_allocation;
643 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
645 GtkRequisition child_requisition;
646 GtkAllocation child_allocation;
649 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
651 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
652 xalign = frame->label_xalign;
654 xalign = 1 - frame->label_xalign;
656 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
657 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
658 child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
660 child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
661 child_allocation.height = child_requisition.height;
663 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
668 gtk_frame_compute_child_allocation (GtkFrame *frame,
669 GtkAllocation *child_allocation)
671 g_return_if_fail (GTK_IS_FRAME (frame));
672 g_return_if_fail (child_allocation != NULL);
674 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
678 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
679 GtkAllocation *child_allocation)
681 GtkWidget *widget = GTK_WIDGET (frame);
682 GtkAllocation *allocation = &widget->allocation;
683 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 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
695 widget->style->xthickness);
696 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
698 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
699 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
700 (gint)GTK_CONTAINER (frame)->border_width -
701 (gint)widget->style->ythickness));
703 child_allocation->x += allocation->x;
704 child_allocation->y += allocation->y;
708 gtk_frame_get_size (GtkSizeRequest *request,
709 GtkOrientation orientation,
713 GtkWidget *widget = GTK_WIDGET (request);
714 GtkFrame *frame = GTK_FRAME (widget);
715 GtkBin *bin = GTK_BIN (widget);
716 gint child_min, child_nat;
717 gint minimum, natural;
719 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
721 if (orientation == GTK_ORIENTATION_HORIZONTAL)
723 gtk_size_request_get_width (GTK_SIZE_REQUEST (frame->label_widget),
724 &child_min, &child_nat);
725 minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
726 natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
730 gtk_size_request_get_height (GTK_SIZE_REQUEST (frame->label_widget),
731 &child_min, &child_nat);
732 minimum = MAX (0, child_min - widget->style->ythickness);
733 natural = MAX (0, child_nat - widget->style->ythickness);
742 if (bin->child && gtk_widget_get_visible (bin->child))
744 if (orientation == GTK_ORIENTATION_HORIZONTAL)
746 gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child),
747 &child_min, &child_nat);
748 minimum = MAX (minimum, child_min);
749 natural = MAX (natural, child_nat);
753 gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child),
754 &child_min, &child_nat);
755 minimum += child_min;
756 natural += child_nat;
760 if (orientation == GTK_ORIENTATION_HORIZONTAL)
762 minimum += (GTK_CONTAINER (widget)->border_width +
763 GTK_WIDGET (widget)->style->xthickness) * 2;
764 natural += (GTK_CONTAINER (widget)->border_width +
765 GTK_WIDGET (widget)->style->xthickness) * 2;
769 minimum += (GTK_CONTAINER (widget)->border_width +
770 GTK_WIDGET (widget)->style->ythickness) * 2;
771 natural += (GTK_CONTAINER (widget)->border_width +
772 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;
805 #define __GTK_FRAME_C__
806 #include "gtkaliasdef.c"