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"
36 #define LABEL_SIDE_PAD 2
48 static void gtk_frame_set_property (GObject *object,
52 static void gtk_frame_get_property (GObject *object,
56 static void gtk_frame_paint (GtkWidget *widget,
58 static gint gtk_frame_expose (GtkWidget *widget,
59 GdkEventExpose *event);
60 static void gtk_frame_size_request (GtkWidget *widget,
61 GtkRequisition *requisition);
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);
76 G_DEFINE_TYPE (GtkFrame, gtk_frame, GTK_TYPE_BIN);
79 gtk_frame_class_init (GtkFrameClass *class)
81 GObjectClass *gobject_class;
82 GtkWidgetClass *widget_class;
83 GtkContainerClass *container_class;
85 gobject_class = (GObjectClass*) class;
86 widget_class = GTK_WIDGET_CLASS (class);
87 container_class = GTK_CONTAINER_CLASS (class);
89 gobject_class->set_property = gtk_frame_set_property;
90 gobject_class->get_property = gtk_frame_get_property;
92 g_object_class_install_property (gobject_class,
94 g_param_spec_string ("label",
96 P_("Text of the frame's label"),
100 g_object_class_install_property (gobject_class,
102 g_param_spec_float ("label-xalign",
104 P_("The horizontal alignment of the label"),
108 GTK_PARAM_READWRITE));
109 g_object_class_install_property (gobject_class,
111 g_param_spec_float ("label-yalign",
113 P_("The vertical alignment of the label"),
117 GTK_PARAM_READWRITE));
118 g_object_class_install_property (gobject_class,
120 g_param_spec_enum ("shadow", NULL,
121 P_("Deprecated property, use shadow_type instead"),
122 GTK_TYPE_SHADOW_TYPE,
123 GTK_SHADOW_ETCHED_IN,
124 GTK_PARAM_READWRITE));
125 g_object_class_install_property (gobject_class,
127 g_param_spec_enum ("shadow-type",
129 P_("Appearance of the frame border"),
130 GTK_TYPE_SHADOW_TYPE,
131 GTK_SHADOW_ETCHED_IN,
132 GTK_PARAM_READWRITE));
134 g_object_class_install_property (gobject_class,
136 g_param_spec_object ("label-widget",
138 P_("A widget to display in place of the usual frame label"),
140 GTK_PARAM_READWRITE));
142 widget_class->expose_event = gtk_frame_expose;
143 widget_class->size_request = gtk_frame_size_request;
144 widget_class->size_allocate = gtk_frame_size_allocate;
146 container_class->remove = gtk_frame_remove;
147 container_class->forall = gtk_frame_forall;
149 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
153 gtk_frame_init (GtkFrame *frame)
155 frame->label_widget = NULL;
156 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
157 frame->label_xalign = 0.0;
158 frame->label_yalign = 0.5;
162 gtk_frame_set_property (GObject *object,
169 frame = GTK_FRAME (object);
174 gtk_frame_set_label (frame, g_value_get_string (value));
176 case PROP_LABEL_XALIGN:
177 gtk_frame_set_label_align (frame, g_value_get_float (value),
178 frame->label_yalign);
180 case PROP_LABEL_YALIGN:
181 gtk_frame_set_label_align (frame, frame->label_xalign,
182 g_value_get_float (value));
185 case PROP_SHADOW_TYPE:
186 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
188 case PROP_LABEL_WIDGET:
189 gtk_frame_set_label_widget (frame, g_value_get_object (value));
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gtk_frame_get_property (GObject *object,
205 frame = GTK_FRAME (object);
210 g_value_set_string (value, gtk_frame_get_label (frame));
212 case PROP_LABEL_XALIGN:
213 g_value_set_float (value, frame->label_xalign);
215 case PROP_LABEL_YALIGN:
216 g_value_set_float (value, frame->label_yalign);
219 case PROP_SHADOW_TYPE:
220 g_value_set_enum (value, frame->shadow_type);
222 case PROP_LABEL_WIDGET:
223 g_value_set_object (value,
224 frame->label_widget ?
225 G_OBJECT (frame->label_widget) : NULL);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 * @label: the text to use as the label of the frame
237 * Creates a new #GtkFrame, with optional label @label.
238 * If @label is %NULL, the label is omitted.
240 * Return value: a new #GtkFrame widget
243 gtk_frame_new (const gchar *label)
245 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
249 gtk_frame_remove (GtkContainer *container,
252 GtkFrame *frame = GTK_FRAME (container);
254 if (frame->label_widget == child)
255 gtk_frame_set_label_widget (frame, NULL);
257 GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
261 gtk_frame_forall (GtkContainer *container,
262 gboolean include_internals,
263 GtkCallback callback,
264 gpointer callback_data)
266 GtkBin *bin = GTK_BIN (container);
267 GtkFrame *frame = GTK_FRAME (container);
270 (* callback) (bin->child, callback_data);
272 if (frame->label_widget)
273 (* callback) (frame->label_widget, callback_data);
277 * gtk_frame_set_label:
278 * @frame: a #GtkFrame
279 * @label: the text to use as the label of the frame
281 * Sets the text of the label. If @label is %NULL,
282 * the current label is removed.
285 gtk_frame_set_label (GtkFrame *frame,
288 g_return_if_fail (GTK_IS_FRAME (frame));
292 gtk_frame_set_label_widget (frame, NULL);
296 GtkWidget *child = gtk_label_new (label);
297 gtk_widget_show (child);
299 gtk_frame_set_label_widget (frame, child);
304 * gtk_frame_get_label:
305 * @frame: a #GtkFrame
307 * If the frame's label widget is a #GtkLabel, returns the
308 * text in the label widget. (The frame will have a #GtkLabel
309 * for the label widget if a non-%NULL argument was passed
310 * to gtk_frame_new().)
312 * Return value: the text in the label, or %NULL if there
313 * was no label widget or the lable widget was not
314 * a #GtkLabel. This string is owned by GTK+ and
315 * must not be modified or freed.
317 G_CONST_RETURN gchar *
318 gtk_frame_get_label (GtkFrame *frame)
320 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
322 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
323 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
329 * gtk_frame_set_label_widget:
330 * @frame: a #GtkFrame
331 * @label_widget: the new label widget
333 * Sets the label widget for the frame. This is the widget that
334 * will appear embedded in the top edge of the frame as a
338 gtk_frame_set_label_widget (GtkFrame *frame,
339 GtkWidget *label_widget)
341 gboolean need_resize = FALSE;
343 g_return_if_fail (GTK_IS_FRAME (frame));
344 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
345 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
347 if (frame->label_widget == label_widget)
350 if (frame->label_widget)
352 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
353 gtk_widget_unparent (frame->label_widget);
356 frame->label_widget = label_widget;
360 frame->label_widget = label_widget;
361 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
362 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
365 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
366 gtk_widget_queue_resize (GTK_WIDGET (frame));
368 g_object_freeze_notify (G_OBJECT (frame));
369 g_object_notify (G_OBJECT (frame), "label-widget");
370 g_object_notify (G_OBJECT (frame), "label");
371 g_object_thaw_notify (G_OBJECT (frame));
375 * gtk_frame_get_label_widget:
376 * @frame: a #GtkFrame
378 * Retrieves the label widget for the frame. See
379 * gtk_frame_set_label_widget().
381 * Return value: the label widget, or %NULL if there is none.
384 gtk_frame_get_label_widget (GtkFrame *frame)
386 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
388 return frame->label_widget;
392 * gtk_frame_set_label_align:
393 * @frame: a #GtkFrame
394 * @xalign: The position of the label along the top edge
395 * of the widget. A value of 0.0 represents left alignment;
396 * 1.0 represents right alignment.
397 * @yalign: The y alignment of the label. A value of 0.0 aligns under
398 * the frame; 1.0 aligns above the frame.
400 * Sets the alignment of the frame widget's label. The
401 * default values for a newly created frame are 0.0 and 0.5.
404 gtk_frame_set_label_align (GtkFrame *frame,
408 g_return_if_fail (GTK_IS_FRAME (frame));
410 xalign = CLAMP (xalign, 0.0, 1.0);
411 yalign = CLAMP (yalign, 0.0, 1.0);
413 g_object_freeze_notify (G_OBJECT (frame));
414 if (xalign != frame->label_xalign)
416 frame->label_xalign = xalign;
417 g_object_notify (G_OBJECT (frame), "label-xalign");
420 if (yalign != frame->label_yalign)
422 frame->label_yalign = yalign;
423 g_object_notify (G_OBJECT (frame), "label-yalign");
426 g_object_thaw_notify (G_OBJECT (frame));
427 gtk_widget_queue_resize (GTK_WIDGET (frame));
431 * gtk_frame_get_label_align:
432 * @frame: a #GtkFrame
433 * @xalign: location to store X alignment of frame's label, or %NULL
434 * @yalign: location to store X alignment of frame's label, or %NULL
436 * Retrieves the X and Y alignment of the frame's label. See
437 * gtk_frame_set_label_align().
440 gtk_frame_get_label_align (GtkFrame *frame,
444 g_return_if_fail (GTK_IS_FRAME (frame));
447 *xalign = frame->label_xalign;
449 *yalign = frame->label_yalign;
453 * gtk_frame_set_shadow_type:
454 * @frame: a #GtkFrame
455 * @type: the new #GtkShadowType
457 * Sets the shadow type for @frame.
460 gtk_frame_set_shadow_type (GtkFrame *frame,
463 g_return_if_fail (GTK_IS_FRAME (frame));
465 if ((GtkShadowType) frame->shadow_type != type)
467 frame->shadow_type = type;
468 g_object_notify (G_OBJECT (frame), "shadow-type");
470 if (GTK_WIDGET_DRAWABLE (frame))
472 gtk_widget_queue_draw (GTK_WIDGET (frame));
475 gtk_widget_queue_resize (GTK_WIDGET (frame));
480 * gtk_frame_get_shadow_type:
481 * @frame: a #GtkFrame
483 * Retrieves the shadow type of the frame. See
484 * gtk_frame_set_shadow_type().
486 * Return value: the current shadow type of the frame.
489 gtk_frame_get_shadow_type (GtkFrame *frame)
491 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
493 return frame->shadow_type;
497 gtk_frame_paint (GtkWidget *widget,
501 gint x, y, width, height;
503 if (GTK_WIDGET_DRAWABLE (widget))
505 frame = GTK_FRAME (widget);
507 x = frame->child_allocation.x - widget->style->xthickness;
508 y = frame->child_allocation.y - widget->style->ythickness;
509 width = frame->child_allocation.width + 2 * widget->style->xthickness;
510 height = frame->child_allocation.height + 2 * widget->style->ythickness;
512 if (frame->label_widget)
514 GtkRequisition child_requisition;
519 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
521 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
522 xalign = frame->label_xalign;
524 xalign = 1 - frame->label_xalign;
526 height_extra = MAX (0, child_requisition.height - widget->style->ythickness);
527 height_extra *= (1 - frame->label_yalign);
529 height += height_extra;
531 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
534 gtk_paint_shadow_gap (widget->style, widget->window,
535 widget->state, frame->shadow_type,
536 area, widget, "frame",
539 x2, child_requisition.width + 2 * LABEL_PAD);
542 gtk_paint_shadow (widget->style, widget->window,
543 widget->state, frame->shadow_type,
544 area, widget, "frame",
545 x, y, width, height);
550 gtk_frame_expose (GtkWidget *widget,
551 GdkEventExpose *event)
553 if (GTK_WIDGET_DRAWABLE (widget))
555 gtk_frame_paint (widget, &event->area);
557 (* GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event) (widget, event);
564 gtk_frame_size_request (GtkWidget *widget,
565 GtkRequisition *requisition)
567 GtkFrame *frame = GTK_FRAME (widget);
568 GtkBin *bin = GTK_BIN (widget);
569 GtkRequisition child_requisition;
571 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
573 gtk_widget_size_request (frame->label_widget, &child_requisition);
575 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
576 requisition->height =
577 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->ythickness);
581 requisition->width = 0;
582 requisition->height = 0;
585 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
587 gtk_widget_size_request (bin->child, &child_requisition);
589 requisition->width = MAX (requisition->width, child_requisition.width);
590 requisition->height += child_requisition.height;
593 requisition->width += (GTK_CONTAINER (widget)->border_width +
594 GTK_WIDGET (widget)->style->xthickness) * 2;
595 requisition->height += (GTK_CONTAINER (widget)->border_width +
596 GTK_WIDGET (widget)->style->ythickness) * 2;
600 gtk_frame_size_allocate (GtkWidget *widget,
601 GtkAllocation *allocation)
603 GtkFrame *frame = GTK_FRAME (widget);
604 GtkBin *bin = GTK_BIN (widget);
605 GtkAllocation new_allocation;
607 widget->allocation = *allocation;
609 gtk_frame_compute_child_allocation (frame, &new_allocation);
611 /* If the child allocation changed, that means that the frame is drawn
612 * in a new place, so we must redraw the entire widget.
614 if (GTK_WIDGET_MAPPED (widget) &&
615 (new_allocation.x != frame->child_allocation.x ||
616 new_allocation.y != frame->child_allocation.y ||
617 new_allocation.width != frame->child_allocation.width ||
618 new_allocation.height != frame->child_allocation.height))
619 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
621 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
622 gtk_widget_size_allocate (bin->child, &new_allocation);
624 frame->child_allocation = new_allocation;
626 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
628 GtkRequisition child_requisition;
629 GtkAllocation child_allocation;
632 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
634 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
635 xalign = frame->label_xalign;
637 xalign = 1 - frame->label_xalign;
639 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
640 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
641 child_allocation.width = child_requisition.width;
643 child_allocation.y = frame->child_allocation.y - child_requisition.height;
644 child_allocation.height = child_requisition.height;
646 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
651 gtk_frame_compute_child_allocation (GtkFrame *frame,
652 GtkAllocation *child_allocation)
654 g_return_if_fail (GTK_IS_FRAME (frame));
655 g_return_if_fail (child_allocation != NULL);
657 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
661 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
662 GtkAllocation *child_allocation)
664 GtkWidget *widget = GTK_WIDGET (frame);
665 GtkAllocation *allocation = &widget->allocation;
666 GtkRequisition child_requisition;
669 if (frame->label_widget)
671 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
672 top_margin = MAX (child_requisition.height, widget->style->ythickness);
675 top_margin = widget->style->ythickness;
677 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
678 widget->style->xthickness);
679 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
681 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
682 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
683 (gint)GTK_CONTAINER (frame)->border_width -
684 (gint)widget->style->ythickness));
686 child_allocation->x += allocation->x;
687 child_allocation->y += allocation->y;
690 #define __GTK_FRAME_C__
691 #include "gtkaliasdef.c"