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"
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_request (GtkWidget *widget,
62 GtkRequisition *requisition);
63 static void gtk_frame_size_allocate (GtkWidget *widget,
64 GtkAllocation *allocation);
65 static void gtk_frame_remove (GtkContainer *container,
67 static void gtk_frame_forall (GtkContainer *container,
68 gboolean include_internals,
70 gpointer callback_data);
72 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
73 GtkAllocation *child_allocation);
74 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
75 GtkAllocation *child_allocation);
78 static void gtk_frame_buildable_init (GtkBuildableIface *iface);
79 static void gtk_frame_buildable_add_child (GtkBuildable *buildable,
84 G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
85 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
86 gtk_frame_buildable_init))
89 gtk_frame_class_init (GtkFrameClass *class)
91 GObjectClass *gobject_class;
92 GtkWidgetClass *widget_class;
93 GtkContainerClass *container_class;
95 gobject_class = (GObjectClass*) class;
96 widget_class = GTK_WIDGET_CLASS (class);
97 container_class = GTK_CONTAINER_CLASS (class);
99 gobject_class->set_property = gtk_frame_set_property;
100 gobject_class->get_property = gtk_frame_get_property;
102 g_object_class_install_property (gobject_class,
104 g_param_spec_string ("label",
106 P_("Text of the frame's label"),
109 GTK_PARAM_WRITABLE));
110 g_object_class_install_property (gobject_class,
112 g_param_spec_float ("label-xalign",
114 P_("The horizontal alignment of the label"),
118 GTK_PARAM_READWRITE));
119 g_object_class_install_property (gobject_class,
121 g_param_spec_float ("label-yalign",
123 P_("The vertical alignment of the label"),
127 GTK_PARAM_READWRITE));
128 g_object_class_install_property (gobject_class,
130 g_param_spec_enum ("shadow", NULL,
131 P_("Deprecated property, use shadow_type instead"),
132 GTK_TYPE_SHADOW_TYPE,
133 GTK_SHADOW_ETCHED_IN,
134 GTK_PARAM_READWRITE));
135 g_object_class_install_property (gobject_class,
137 g_param_spec_enum ("shadow-type",
139 P_("Appearance of the frame border"),
140 GTK_TYPE_SHADOW_TYPE,
141 GTK_SHADOW_ETCHED_IN,
142 GTK_PARAM_READWRITE));
144 g_object_class_install_property (gobject_class,
146 g_param_spec_object ("label-widget",
148 P_("A widget to display in place of the usual frame label"),
150 GTK_PARAM_READWRITE));
152 widget_class->expose_event = gtk_frame_expose;
153 widget_class->size_request = gtk_frame_size_request;
154 widget_class->size_allocate = gtk_frame_size_allocate;
156 container_class->remove = gtk_frame_remove;
157 container_class->forall = gtk_frame_forall;
159 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
163 gtk_frame_buildable_init (GtkBuildableIface *iface)
165 iface->add_child = gtk_frame_buildable_add_child;
169 gtk_frame_buildable_add_child (GtkBuildable *buildable,
174 if (type && strcmp (type, "label") == 0)
175 gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
177 gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
179 GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
183 gtk_frame_init (GtkFrame *frame)
185 frame->label_widget = NULL;
186 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
187 frame->label_xalign = 0.0;
188 frame->label_yalign = 0.5;
192 gtk_frame_set_property (GObject *object,
199 frame = GTK_FRAME (object);
204 gtk_frame_set_label (frame, g_value_get_string (value));
206 case PROP_LABEL_XALIGN:
207 gtk_frame_set_label_align (frame, g_value_get_float (value),
208 frame->label_yalign);
210 case PROP_LABEL_YALIGN:
211 gtk_frame_set_label_align (frame, frame->label_xalign,
212 g_value_get_float (value));
215 case PROP_SHADOW_TYPE:
216 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
218 case PROP_LABEL_WIDGET:
219 gtk_frame_set_label_widget (frame, g_value_get_object (value));
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
228 gtk_frame_get_property (GObject *object,
235 frame = GTK_FRAME (object);
240 g_value_set_string (value, gtk_frame_get_label (frame));
242 case PROP_LABEL_XALIGN:
243 g_value_set_float (value, frame->label_xalign);
245 case PROP_LABEL_YALIGN:
246 g_value_set_float (value, frame->label_yalign);
249 case PROP_SHADOW_TYPE:
250 g_value_set_enum (value, frame->shadow_type);
252 case PROP_LABEL_WIDGET:
253 g_value_set_object (value,
254 frame->label_widget ?
255 G_OBJECT (frame->label_widget) : NULL);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 * @label: the text to use as the label of the frame
267 * Creates a new #GtkFrame, with optional label @label.
268 * If @label is %NULL, the label is omitted.
270 * Return value: a new #GtkFrame widget
273 gtk_frame_new (const gchar *label)
275 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
279 gtk_frame_remove (GtkContainer *container,
282 GtkFrame *frame = GTK_FRAME (container);
284 if (frame->label_widget == child)
285 gtk_frame_set_label_widget (frame, NULL);
287 GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
291 gtk_frame_forall (GtkContainer *container,
292 gboolean include_internals,
293 GtkCallback callback,
294 gpointer callback_data)
296 GtkBin *bin = GTK_BIN (container);
297 GtkFrame *frame = GTK_FRAME (container);
300 (* callback) (bin->child, callback_data);
302 if (frame->label_widget)
303 (* callback) (frame->label_widget, callback_data);
307 * gtk_frame_set_label:
308 * @frame: a #GtkFrame
309 * @label: (allow-none): the text to use as the label of the frame
311 * Sets the text of the label. If @label is %NULL,
312 * the current label is removed.
315 gtk_frame_set_label (GtkFrame *frame,
318 g_return_if_fail (GTK_IS_FRAME (frame));
322 gtk_frame_set_label_widget (frame, NULL);
326 GtkWidget *child = gtk_label_new (label);
327 gtk_widget_show (child);
329 gtk_frame_set_label_widget (frame, child);
334 * gtk_frame_get_label:
335 * @frame: a #GtkFrame
337 * If the frame's label widget is a #GtkLabel, returns the
338 * text in the label widget. (The frame will have a #GtkLabel
339 * for the label widget if a non-%NULL argument was passed
340 * to gtk_frame_new().)
342 * Return value: the text in the label, or %NULL if there
343 * was no label widget or the lable widget was not
344 * a #GtkLabel. This string is owned by GTK+ and
345 * must not be modified or freed.
347 G_CONST_RETURN gchar *
348 gtk_frame_get_label (GtkFrame *frame)
350 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
352 if (GTK_IS_LABEL (frame->label_widget))
353 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
359 * gtk_frame_set_label_widget:
360 * @frame: a #GtkFrame
361 * @label_widget: the new label widget
363 * Sets the label widget for the frame. This is the widget that
364 * will appear embedded in the top edge of the frame as a
368 gtk_frame_set_label_widget (GtkFrame *frame,
369 GtkWidget *label_widget)
371 gboolean need_resize = FALSE;
373 g_return_if_fail (GTK_IS_FRAME (frame));
374 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
375 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
377 if (frame->label_widget == label_widget)
380 if (frame->label_widget)
382 need_resize = gtk_widget_get_visible (frame->label_widget);
383 gtk_widget_unparent (frame->label_widget);
386 frame->label_widget = label_widget;
390 frame->label_widget = label_widget;
391 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
392 need_resize |= gtk_widget_get_visible (label_widget);
395 if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
396 gtk_widget_queue_resize (GTK_WIDGET (frame));
398 g_object_freeze_notify (G_OBJECT (frame));
399 g_object_notify (G_OBJECT (frame), "label-widget");
400 g_object_notify (G_OBJECT (frame), "label");
401 g_object_thaw_notify (G_OBJECT (frame));
405 * gtk_frame_get_label_widget:
406 * @frame: a #GtkFrame
408 * Retrieves the label widget for the frame. See
409 * gtk_frame_set_label_widget().
411 * Return value: the label widget, or %NULL if there is none.
414 gtk_frame_get_label_widget (GtkFrame *frame)
416 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
418 return frame->label_widget;
422 * gtk_frame_set_label_align:
423 * @frame: a #GtkFrame
424 * @xalign: The position of the label along the top edge
425 * of the widget. A value of 0.0 represents left alignment;
426 * 1.0 represents right alignment.
427 * @yalign: The y alignment of the label. A value of 0.0 aligns under
428 * the frame; 1.0 aligns above the frame. If the values are exactly
429 * 0.0 or 1.0 the gap in the frame won't be painted because the label
430 * will be completely above or below the frame.
432 * Sets the alignment of the frame widget's label. The
433 * default values for a newly created frame are 0.0 and 0.5.
436 gtk_frame_set_label_align (GtkFrame *frame,
440 g_return_if_fail (GTK_IS_FRAME (frame));
442 xalign = CLAMP (xalign, 0.0, 1.0);
443 yalign = CLAMP (yalign, 0.0, 1.0);
445 g_object_freeze_notify (G_OBJECT (frame));
446 if (xalign != frame->label_xalign)
448 frame->label_xalign = xalign;
449 g_object_notify (G_OBJECT (frame), "label-xalign");
452 if (yalign != frame->label_yalign)
454 frame->label_yalign = yalign;
455 g_object_notify (G_OBJECT (frame), "label-yalign");
458 g_object_thaw_notify (G_OBJECT (frame));
459 gtk_widget_queue_resize (GTK_WIDGET (frame));
463 * gtk_frame_get_label_align:
464 * @frame: a #GtkFrame
465 * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
466 * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
468 * Retrieves the X and Y alignment of the frame's label. See
469 * gtk_frame_set_label_align().
472 gtk_frame_get_label_align (GtkFrame *frame,
476 g_return_if_fail (GTK_IS_FRAME (frame));
479 *xalign = frame->label_xalign;
481 *yalign = frame->label_yalign;
485 * gtk_frame_set_shadow_type:
486 * @frame: a #GtkFrame
487 * @type: the new #GtkShadowType
489 * Sets the shadow type for @frame.
492 gtk_frame_set_shadow_type (GtkFrame *frame,
497 g_return_if_fail (GTK_IS_FRAME (frame));
499 if ((GtkShadowType) frame->shadow_type != type)
501 widget = GTK_WIDGET (frame);
502 frame->shadow_type = type;
503 g_object_notify (G_OBJECT (frame), "shadow-type");
505 if (gtk_widget_is_drawable (widget))
507 gtk_widget_queue_draw (widget);
510 gtk_widget_queue_resize (widget);
515 * gtk_frame_get_shadow_type:
516 * @frame: a #GtkFrame
518 * Retrieves the shadow type of the frame. See
519 * gtk_frame_set_shadow_type().
521 * Return value: the current shadow type of the frame.
524 gtk_frame_get_shadow_type (GtkFrame *frame)
526 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
528 return frame->shadow_type;
532 gtk_frame_paint (GtkWidget *widget,
536 gint x, y, width, height;
538 if (gtk_widget_is_drawable (widget))
540 frame = GTK_FRAME (widget);
542 x = frame->child_allocation.x - widget->style->xthickness;
543 y = frame->child_allocation.y - widget->style->ythickness;
544 width = frame->child_allocation.width + 2 * widget->style->xthickness;
545 height = frame->child_allocation.height + 2 * widget->style->ythickness;
547 if (frame->label_widget)
549 GtkRequisition child_requisition;
554 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
556 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
557 xalign = frame->label_xalign;
559 xalign = 1 - frame->label_xalign;
561 height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
562 - frame->label_yalign * child_requisition.height;
564 height += height_extra;
566 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
568 /* If the label is completely over or under the frame we can omit the gap */
569 if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
570 gtk_paint_shadow (widget->style, widget->window,
571 widget->state, frame->shadow_type,
572 area, widget, "frame",
573 x, y, width, height);
575 gtk_paint_shadow_gap (widget->style, widget->window,
576 widget->state, frame->shadow_type,
577 area, widget, "frame",
580 x2, child_requisition.width + 2 * LABEL_PAD);
583 gtk_paint_shadow (widget->style, widget->window,
584 widget->state, frame->shadow_type,
585 area, widget, "frame",
586 x, y, width, height);
591 gtk_frame_expose (GtkWidget *widget,
592 GdkEventExpose *event)
594 if (gtk_widget_is_drawable (widget))
596 gtk_frame_paint (widget, &event->area);
598 GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
605 gtk_frame_size_request (GtkWidget *widget,
606 GtkRequisition *requisition)
608 GtkFrame *frame = GTK_FRAME (widget);
609 GtkBin *bin = GTK_BIN (widget);
610 GtkRequisition child_requisition;
612 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
614 gtk_widget_size_request (frame->label_widget, &child_requisition);
616 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
617 requisition->height =
618 MAX (0, child_requisition.height - widget->style->ythickness);
622 requisition->width = 0;
623 requisition->height = 0;
626 if (bin->child && gtk_widget_get_visible (bin->child))
628 gtk_widget_size_request (bin->child, &child_requisition);
630 requisition->width = MAX (requisition->width, child_requisition.width);
631 requisition->height += child_requisition.height;
634 requisition->width += (GTK_CONTAINER (widget)->border_width +
635 GTK_WIDGET (widget)->style->xthickness) * 2;
636 requisition->height += (GTK_CONTAINER (widget)->border_width +
637 GTK_WIDGET (widget)->style->ythickness) * 2;
641 gtk_frame_size_allocate (GtkWidget *widget,
642 GtkAllocation *allocation)
644 GtkFrame *frame = GTK_FRAME (widget);
645 GtkBin *bin = GTK_BIN (widget);
646 GtkAllocation new_allocation;
648 widget->allocation = *allocation;
650 gtk_frame_compute_child_allocation (frame, &new_allocation);
652 /* If the child allocation changed, that means that the frame is drawn
653 * in a new place, so we must redraw the entire widget.
655 if (GTK_WIDGET_MAPPED (widget) &&
656 (new_allocation.x != frame->child_allocation.x ||
657 new_allocation.y != frame->child_allocation.y ||
658 new_allocation.width != frame->child_allocation.width ||
659 new_allocation.height != frame->child_allocation.height))
660 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
662 if (bin->child && gtk_widget_get_visible (bin->child))
663 gtk_widget_size_allocate (bin->child, &new_allocation);
665 frame->child_allocation = new_allocation;
667 if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
669 GtkRequisition child_requisition;
670 GtkAllocation child_allocation;
673 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
675 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
676 xalign = frame->label_xalign;
678 xalign = 1 - frame->label_xalign;
680 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
681 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
682 child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
684 child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
685 child_allocation.height = child_requisition.height;
687 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
692 gtk_frame_compute_child_allocation (GtkFrame *frame,
693 GtkAllocation *child_allocation)
695 g_return_if_fail (GTK_IS_FRAME (frame));
696 g_return_if_fail (child_allocation != NULL);
698 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
702 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
703 GtkAllocation *child_allocation)
705 GtkWidget *widget = GTK_WIDGET (frame);
706 GtkAllocation *allocation = &widget->allocation;
707 GtkRequisition child_requisition;
710 if (frame->label_widget)
712 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
713 top_margin = MAX (child_requisition.height, widget->style->ythickness);
716 top_margin = widget->style->ythickness;
718 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
719 widget->style->xthickness);
720 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
722 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
723 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
724 (gint)GTK_CONTAINER (frame)->border_width -
725 (gint)widget->style->ythickness));
727 child_allocation->x += allocation->x;
728 child_allocation->y += allocation->y;
731 #define __GTK_FRAME_C__
732 #include "gtkaliasdef.c"