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/.
35 #define LABEL_SIDE_PAD 2
48 static void gtk_frame_class_init (GtkFrameClass *klass);
49 static void gtk_frame_init (GtkFrame *frame);
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_request (GtkWidget *widget,
63 GtkRequisition *requisition);
64 static void gtk_frame_size_allocate (GtkWidget *widget,
65 GtkAllocation *allocation);
66 static void gtk_frame_remove (GtkContainer *container,
68 static void gtk_frame_forall (GtkContainer *container,
69 gboolean include_internals,
71 gpointer callback_data);
73 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
74 GtkAllocation *child_allocation);
75 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
76 GtkAllocation *child_allocation);
78 static GtkBinClass *parent_class = NULL;
82 gtk_frame_get_type (void)
84 static GType frame_type = 0;
88 static const GTypeInfo frame_info =
90 sizeof (GtkFrameClass),
92 NULL, /* base_finalize */
93 (GClassInitFunc) gtk_frame_class_init,
94 NULL, /* class_finalize */
95 NULL, /* class_data */
98 (GInstanceInitFunc) gtk_frame_init,
101 frame_type = g_type_register_static (GTK_TYPE_BIN, "GtkFrame",
109 gtk_frame_class_init (GtkFrameClass *class)
111 GObjectClass *gobject_class;
112 GtkWidgetClass *widget_class;
113 GtkContainerClass *container_class;
115 gobject_class = (GObjectClass*) class;
116 widget_class = GTK_WIDGET_CLASS (class);
117 container_class = GTK_CONTAINER_CLASS (class);
119 parent_class = g_type_class_peek_parent (class);
121 gobject_class->set_property = gtk_frame_set_property;
122 gobject_class->get_property = gtk_frame_get_property;
124 g_object_class_install_property (gobject_class,
126 g_param_spec_string ("label",
128 P_("Text of the frame's label"),
132 g_object_class_install_property (gobject_class,
134 g_param_spec_float ("label_xalign",
136 P_("The horizontal alignment of the label"),
142 g_object_class_install_property (gobject_class,
144 g_param_spec_float ("label_yalign",
146 P_("The vertical alignment of the label"),
152 g_object_class_install_property (gobject_class,
154 g_param_spec_enum ("shadow", NULL,
155 P_("Deprecated property, use shadow_type instead"),
156 GTK_TYPE_SHADOW_TYPE,
157 GTK_SHADOW_ETCHED_IN,
158 G_PARAM_READABLE | G_PARAM_WRITABLE));
159 g_object_class_install_property (gobject_class,
161 g_param_spec_enum ("shadow_type",
163 P_("Appearance of the frame border"),
164 GTK_TYPE_SHADOW_TYPE,
165 GTK_SHADOW_ETCHED_IN,
166 G_PARAM_READABLE | G_PARAM_WRITABLE));
168 g_object_class_install_property (gobject_class,
170 g_param_spec_object ("label_widget",
172 P_("A widget to display in place of the usual frame label"),
174 G_PARAM_READABLE | G_PARAM_WRITABLE));
176 widget_class->expose_event = gtk_frame_expose;
177 widget_class->size_request = gtk_frame_size_request;
178 widget_class->size_allocate = gtk_frame_size_allocate;
180 container_class->remove = gtk_frame_remove;
181 container_class->forall = gtk_frame_forall;
183 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
187 gtk_frame_init (GtkFrame *frame)
189 frame->label_widget = NULL;
190 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
191 frame->label_xalign = 0.0;
192 frame->label_yalign = 0.5;
196 gtk_frame_set_property (GObject *object,
203 frame = GTK_FRAME (object);
208 gtk_frame_set_label (frame, g_value_get_string (value));
210 case PROP_LABEL_XALIGN:
211 gtk_frame_set_label_align (frame, g_value_get_float (value),
212 frame->label_yalign);
214 case PROP_LABEL_YALIGN:
215 gtk_frame_set_label_align (frame, frame->label_xalign,
216 g_value_get_float (value));
219 case PROP_SHADOW_TYPE:
220 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
222 case PROP_LABEL_WIDGET:
223 gtk_frame_set_label_widget (frame, g_value_get_object (value));
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232 gtk_frame_get_property (GObject *object,
239 frame = GTK_FRAME (object);
244 g_value_set_string (value, gtk_frame_get_label (frame));
246 case PROP_LABEL_XALIGN:
247 g_value_set_float (value, frame->label_xalign);
249 case PROP_LABEL_YALIGN:
250 g_value_set_float (value, frame->label_yalign);
253 case PROP_SHADOW_TYPE:
254 g_value_set_enum (value, frame->shadow_type);
256 case PROP_LABEL_WIDGET:
257 g_value_set_object (value,
258 frame->label_widget ?
259 G_OBJECT (frame->label_widget) : NULL);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 * @label: the text to use as the label of the frame
271 * Creates a new #GtkFrame, with optional label @label.
272 * If @label is %NULL, the label is omitted.
274 * Return value: a new #GtkFrame widget
277 gtk_frame_new (const gchar *label)
279 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
283 gtk_frame_remove (GtkContainer *container,
286 GtkFrame *frame = GTK_FRAME (container);
288 if (frame->label_widget == child)
289 gtk_frame_set_label_widget (frame, NULL);
291 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
295 gtk_frame_forall (GtkContainer *container,
296 gboolean include_internals,
297 GtkCallback callback,
298 gpointer callback_data)
300 GtkBin *bin = GTK_BIN (container);
301 GtkFrame *frame = GTK_FRAME (container);
304 (* callback) (bin->child, callback_data);
306 if (frame->label_widget)
307 (* callback) (frame->label_widget, callback_data);
311 * gtk_frame_set_label:
312 * @frame: a #GtkFrame
313 * @label: the text to use as the label of the frame
315 * Sets the text of the label. If @label is %NULL,
316 * the current label is removed.
319 gtk_frame_set_label (GtkFrame *frame,
322 g_return_if_fail (GTK_IS_FRAME (frame));
326 gtk_frame_set_label_widget (frame, NULL);
330 GtkWidget *child = gtk_label_new (label);
331 gtk_widget_show (child);
333 gtk_frame_set_label_widget (frame, child);
338 * gtk_frame_get_label:
339 * @frame: a #GtkFrame
341 * If the frame's label widget is a #GtkLabel, returns the
342 * text in the label widget. (The frame will have a #GtkLabel
343 * for the label widget if a non-%NULL argument was passed
344 * to gtk_frame_new().)
346 * Return value: the text in the label, or %NULL if there
347 * was no label widget or the lable widget was not
348 * a #GtkLabel. This string is owned by GTK+ and
349 * must not be modified or freed.
351 G_CONST_RETURN gchar *
352 gtk_frame_get_label (GtkFrame *frame)
354 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
356 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
357 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
363 * gtk_frame_set_label_widget:
364 * @frame: a #GtkFrame
365 * @label_widget: the new label widget
367 * Sets the label widget for the frame. This is the widget that
368 * will appear embedded in the top edge of the frame as a
372 gtk_frame_set_label_widget (GtkFrame *frame,
373 GtkWidget *label_widget)
375 gboolean need_resize = FALSE;
377 g_return_if_fail (GTK_IS_FRAME (frame));
378 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
379 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
381 if (frame->label_widget == label_widget)
384 if (frame->label_widget)
386 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
387 gtk_widget_unparent (frame->label_widget);
390 frame->label_widget = label_widget;
394 frame->label_widget = label_widget;
395 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
396 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
399 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
400 gtk_widget_queue_resize (GTK_WIDGET (frame));
402 g_object_freeze_notify (G_OBJECT (frame));
403 g_object_notify (G_OBJECT (frame), "label_widget");
404 g_object_notify (G_OBJECT (frame), "label");
405 g_object_thaw_notify (G_OBJECT (frame));
409 * gtk_frame_get_label_widget:
410 * @frame: a #GtkFrame
412 * Retrieves the label widget for the frame. See
413 * gtk_frame_set_label_widget().
415 * Return value: the label widget, or %NULL if there is none.
418 gtk_frame_get_label_widget (GtkFrame *frame)
420 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
422 return frame->label_widget;
426 * gtk_frame_set_label_align:
427 * @frame: a #GtkFrame
428 * @xalign: The position of the label along the top edge
429 * of the widget. A value of 0.0 represents left alignment;
430 * 1.0 represents right alignment.
431 * @yalign: The y alignment of the label. A value of 0.0 aligns under
432 * the frame; 1.0 aligns above the frame.
434 * Sets the alignment of the frame widget's label. The
435 * default values for a newly created frame are 0.0 and 0.5.
438 gtk_frame_set_label_align (GtkFrame *frame,
442 g_return_if_fail (GTK_IS_FRAME (frame));
444 xalign = CLAMP (xalign, 0.0, 1.0);
445 yalign = CLAMP (yalign, 0.0, 1.0);
447 g_object_freeze_notify (G_OBJECT (frame));
448 if (xalign != frame->label_xalign)
450 frame->label_xalign = xalign;
451 g_object_notify (G_OBJECT (frame), "label_xalign");
454 if (yalign != frame->label_yalign)
456 frame->label_yalign = yalign;
457 g_object_notify (G_OBJECT (frame), "label_yalign");
460 g_object_thaw_notify (G_OBJECT (frame));
461 gtk_widget_queue_resize (GTK_WIDGET (frame));
465 * gtk_frame_get_label_align:
466 * @frame: a #GtkFrame
467 * @xalign: location to store X alignment of frame's label, or %NULL
468 * @yalign: location to store X alignment of frame's label, or %NULL
470 * Retrieves the X and Y alignment of the frame's label. See
471 * gtk_frame_set_label_align().
474 gtk_frame_get_label_align (GtkFrame *frame,
478 g_return_if_fail (GTK_IS_FRAME (frame));
481 *xalign = frame->label_xalign;
483 *yalign = frame->label_yalign;
487 * gtk_frame_set_shadow_type:
488 * @frame: a #GtkFrame
489 * @type: the new #GtkShadowType
491 * Sets the shadow type for @frame.
494 gtk_frame_set_shadow_type (GtkFrame *frame,
497 g_return_if_fail (GTK_IS_FRAME (frame));
499 if ((GtkShadowType) frame->shadow_type != type)
501 frame->shadow_type = type;
502 g_object_notify (G_OBJECT (frame), "shadow_type");
504 if (GTK_WIDGET_DRAWABLE (frame))
506 gtk_widget_queue_draw (GTK_WIDGET (frame));
509 gtk_widget_queue_resize (GTK_WIDGET (frame));
514 * gtk_frame_get_shadow_type:
515 * @frame: a #GtkFrame
517 * Retrieves the shadow type of the frame. See
518 * gtk_frame_set_shadow_type().
520 * Return value: the current shadow type of the frame.
523 gtk_frame_get_shadow_type (GtkFrame *frame)
525 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
527 return frame->shadow_type;
531 gtk_frame_paint (GtkWidget *widget,
535 gint x, y, width, height;
537 if (GTK_WIDGET_DRAWABLE (widget))
539 frame = GTK_FRAME (widget);
541 x = frame->child_allocation.x - widget->style->xthickness;
542 y = frame->child_allocation.y - widget->style->ythickness;
543 width = frame->child_allocation.width + 2 * widget->style->xthickness;
544 height = frame->child_allocation.height + 2 * widget->style->ythickness;
546 if (frame->label_widget)
548 GtkRequisition child_requisition;
553 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
555 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
556 xalign = frame->label_xalign;
558 xalign = 1 - frame->label_xalign;
560 height_extra = MAX (0, child_requisition.height - widget->style->ythickness);
561 height_extra *= (1 - frame->label_yalign);
563 height += height_extra;
565 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
568 gtk_paint_shadow_gap (widget->style, widget->window,
569 GTK_STATE_NORMAL, frame->shadow_type,
570 area, widget, "frame",
573 x2, child_requisition.width + 2 * LABEL_PAD);
576 gtk_paint_shadow (widget->style, widget->window,
577 GTK_STATE_NORMAL, frame->shadow_type,
578 area, widget, "frame",
579 x, y, width, height);
584 gtk_frame_expose (GtkWidget *widget,
585 GdkEventExpose *event)
587 if (GTK_WIDGET_DRAWABLE (widget))
589 gtk_frame_paint (widget, &event->area);
591 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
598 gtk_frame_size_request (GtkWidget *widget,
599 GtkRequisition *requisition)
601 GtkFrame *frame = GTK_FRAME (widget);
602 GtkBin *bin = GTK_BIN (widget);
603 GtkRequisition child_requisition;
605 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
607 gtk_widget_size_request (frame->label_widget, &child_requisition);
609 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
610 requisition->height =
611 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->ythickness);
615 requisition->width = 0;
616 requisition->height = 0;
619 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
621 gtk_widget_size_request (bin->child, &child_requisition);
623 requisition->width = MAX (requisition->width, child_requisition.width);
624 requisition->height += child_requisition.height;
627 requisition->width += (GTK_CONTAINER (widget)->border_width +
628 GTK_WIDGET (widget)->style->xthickness) * 2;
629 requisition->height += (GTK_CONTAINER (widget)->border_width +
630 GTK_WIDGET (widget)->style->ythickness) * 2;
634 gtk_frame_size_allocate (GtkWidget *widget,
635 GtkAllocation *allocation)
637 GtkFrame *frame = GTK_FRAME (widget);
638 GtkBin *bin = GTK_BIN (widget);
639 GtkAllocation new_allocation;
641 widget->allocation = *allocation;
643 gtk_frame_compute_child_allocation (frame, &new_allocation);
645 /* If the child allocation changed, that means that the frame is drawn
646 * in a new place, so we must redraw the entire widget.
648 if (GTK_WIDGET_MAPPED (widget) &&
649 (new_allocation.x != frame->child_allocation.x ||
650 new_allocation.y != frame->child_allocation.y ||
651 new_allocation.width != frame->child_allocation.width ||
652 new_allocation.height != frame->child_allocation.height))
653 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
655 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
656 gtk_widget_size_allocate (bin->child, &new_allocation);
658 frame->child_allocation = new_allocation;
660 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
662 GtkRequisition child_requisition;
663 GtkAllocation child_allocation;
666 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
668 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
669 xalign = frame->label_xalign;
671 xalign = 1 - frame->label_xalign;
673 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
674 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
675 child_allocation.width = child_requisition.width;
677 child_allocation.y = frame->child_allocation.y - child_requisition.height;
678 child_allocation.height = child_requisition.height;
680 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
685 gtk_frame_compute_child_allocation (GtkFrame *frame,
686 GtkAllocation *child_allocation)
688 g_return_if_fail (GTK_IS_FRAME (frame));
689 g_return_if_fail (child_allocation != NULL);
691 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
695 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
696 GtkAllocation *child_allocation)
698 GtkWidget *widget = GTK_WIDGET (frame);
699 GtkAllocation *allocation = &widget->allocation;
700 GtkRequisition child_requisition;
703 if (frame->label_widget)
705 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
706 top_margin = MAX (child_requisition.height, widget->style->ythickness);
709 top_margin = widget->style->ythickness;
711 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
712 widget->style->xthickness);
713 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
715 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
716 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
717 (gint)GTK_CONTAINER (frame)->border_width -
718 (gint)widget->style->ythickness));
720 child_allocation->x += allocation->x;
721 child_allocation->y += allocation->y;