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_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_VISIBLE (label_widget);
395 if (GTK_WIDGET_VISIBLE (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,
495 g_return_if_fail (GTK_IS_FRAME (frame));
497 if ((GtkShadowType) frame->shadow_type != type)
499 frame->shadow_type = type;
500 g_object_notify (G_OBJECT (frame), "shadow-type");
502 if (GTK_WIDGET_DRAWABLE (frame))
504 gtk_widget_queue_draw (GTK_WIDGET (frame));
507 gtk_widget_queue_resize (GTK_WIDGET (frame));
512 * gtk_frame_get_shadow_type:
513 * @frame: a #GtkFrame
515 * Retrieves the shadow type of the frame. See
516 * gtk_frame_set_shadow_type().
518 * Return value: the current shadow type of the frame.
521 gtk_frame_get_shadow_type (GtkFrame *frame)
523 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
525 return frame->shadow_type;
529 gtk_frame_paint (GtkWidget *widget,
533 gint x, y, width, height;
535 if (GTK_WIDGET_DRAWABLE (widget))
537 frame = GTK_FRAME (widget);
539 x = frame->child_allocation.x - widget->style->xthickness;
540 y = frame->child_allocation.y - widget->style->ythickness;
541 width = frame->child_allocation.width + 2 * widget->style->xthickness;
542 height = frame->child_allocation.height + 2 * widget->style->ythickness;
544 if (frame->label_widget)
546 GtkRequisition child_requisition;
551 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
553 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
554 xalign = frame->label_xalign;
556 xalign = 1 - frame->label_xalign;
558 height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
559 - frame->label_yalign * child_requisition.height;
561 height += height_extra;
563 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
565 /* If the label is completely over or under the frame we can omit the gap */
566 if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
567 gtk_paint_shadow (widget->style, widget->window,
568 widget->state, frame->shadow_type,
569 area, widget, "frame",
570 x, y, width, height);
572 gtk_paint_shadow_gap (widget->style, widget->window,
573 widget->state, frame->shadow_type,
574 area, widget, "frame",
577 x2, child_requisition.width + 2 * LABEL_PAD);
580 gtk_paint_shadow (widget->style, widget->window,
581 widget->state, frame->shadow_type,
582 area, widget, "frame",
583 x, y, width, height);
588 gtk_frame_expose (GtkWidget *widget,
589 GdkEventExpose *event)
591 if (GTK_WIDGET_DRAWABLE (widget))
593 gtk_frame_paint (widget, &event->area);
595 GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
602 gtk_frame_size_request (GtkWidget *widget,
603 GtkRequisition *requisition)
605 GtkFrame *frame = GTK_FRAME (widget);
606 GtkBin *bin = GTK_BIN (widget);
607 GtkRequisition child_requisition;
609 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
611 gtk_widget_size_request (frame->label_widget, &child_requisition);
613 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
614 requisition->height =
615 MAX (0, child_requisition.height - widget->style->ythickness);
619 requisition->width = 0;
620 requisition->height = 0;
623 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
625 gtk_widget_size_request (bin->child, &child_requisition);
627 requisition->width = MAX (requisition->width, child_requisition.width);
628 requisition->height += child_requisition.height;
631 requisition->width += (GTK_CONTAINER (widget)->border_width +
632 GTK_WIDGET (widget)->style->xthickness) * 2;
633 requisition->height += (GTK_CONTAINER (widget)->border_width +
634 GTK_WIDGET (widget)->style->ythickness) * 2;
638 gtk_frame_size_allocate (GtkWidget *widget,
639 GtkAllocation *allocation)
641 GtkFrame *frame = GTK_FRAME (widget);
642 GtkBin *bin = GTK_BIN (widget);
643 GtkAllocation new_allocation;
645 widget->allocation = *allocation;
647 gtk_frame_compute_child_allocation (frame, &new_allocation);
649 /* If the child allocation changed, that means that the frame is drawn
650 * in a new place, so we must redraw the entire widget.
652 if (GTK_WIDGET_MAPPED (widget) &&
653 (new_allocation.x != frame->child_allocation.x ||
654 new_allocation.y != frame->child_allocation.y ||
655 new_allocation.width != frame->child_allocation.width ||
656 new_allocation.height != frame->child_allocation.height))
657 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
659 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
660 gtk_widget_size_allocate (bin->child, &new_allocation);
662 frame->child_allocation = new_allocation;
664 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
666 GtkRequisition child_requisition;
667 GtkAllocation child_allocation;
670 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
672 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
673 xalign = frame->label_xalign;
675 xalign = 1 - frame->label_xalign;
677 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
678 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
679 child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
681 child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
682 child_allocation.height = child_requisition.height;
684 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
689 gtk_frame_compute_child_allocation (GtkFrame *frame,
690 GtkAllocation *child_allocation)
692 g_return_if_fail (GTK_IS_FRAME (frame));
693 g_return_if_fail (child_allocation != NULL);
695 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
699 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
700 GtkAllocation *child_allocation)
702 GtkWidget *widget = GTK_WIDGET (frame);
703 GtkAllocation *allocation = &widget->allocation;
704 GtkRequisition child_requisition;
707 if (frame->label_widget)
709 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
710 top_margin = MAX (child_requisition.height, widget->style->ythickness);
713 top_margin = widget->style->ythickness;
715 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
716 widget->style->xthickness);
717 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
719 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
720 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
721 (gint)GTK_CONTAINER (frame)->border_width -
722 (gint)widget->style->ythickness));
724 child_allocation->x += allocation->x;
725 child_allocation->y += allocation->y;
728 #define __GTK_FRAME_C__
729 #include "gtkaliasdef.c"