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/.
34 #define LABEL_SIDE_PAD 2
47 static void gtk_frame_class_init (GtkFrameClass *klass);
48 static void gtk_frame_init (GtkFrame *frame);
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);
77 static GtkBinClass *parent_class = NULL;
81 gtk_frame_get_type (void)
83 static GType frame_type = 0;
87 static const GTypeInfo frame_info =
89 sizeof (GtkFrameClass),
91 NULL, /* base_finalize */
92 (GClassInitFunc) gtk_frame_class_init,
93 NULL, /* class_finalize */
94 NULL, /* class_data */
97 (GInstanceInitFunc) gtk_frame_init,
100 frame_type = g_type_register_static (GTK_TYPE_BIN, "GtkFrame",
108 gtk_frame_class_init (GtkFrameClass *class)
110 GObjectClass *gobject_class;
111 GtkWidgetClass *widget_class;
112 GtkContainerClass *container_class;
114 gobject_class = (GObjectClass*) class;
115 widget_class = GTK_WIDGET_CLASS (class);
116 container_class = GTK_CONTAINER_CLASS (class);
118 parent_class = g_type_class_peek_parent (class);
120 gobject_class->set_property = gtk_frame_set_property;
121 gobject_class->get_property = gtk_frame_get_property;
123 g_object_class_install_property (gobject_class,
125 g_param_spec_string ("label",
127 P_("Text of the frame's label"),
131 g_object_class_install_property (gobject_class,
133 g_param_spec_float ("label_xalign",
135 P_("The horizontal alignment of the label"),
141 g_object_class_install_property (gobject_class,
143 g_param_spec_float ("label_yalign",
145 P_("The vertical alignment of the label"),
151 g_object_class_install_property (gobject_class,
153 g_param_spec_enum ("shadow", NULL,
154 P_("Deprecated property, use shadow_type instead"),
155 GTK_TYPE_SHADOW_TYPE,
156 GTK_SHADOW_ETCHED_IN,
157 G_PARAM_READABLE | G_PARAM_WRITABLE));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_enum ("shadow_type",
162 P_("Appearance of the frame border"),
163 GTK_TYPE_SHADOW_TYPE,
164 GTK_SHADOW_ETCHED_IN,
165 G_PARAM_READABLE | G_PARAM_WRITABLE));
167 g_object_class_install_property (gobject_class,
169 g_param_spec_object ("label_widget",
171 P_("A widget to display in place of the usual frame label"),
173 G_PARAM_READABLE | G_PARAM_WRITABLE));
175 widget_class->expose_event = gtk_frame_expose;
176 widget_class->size_request = gtk_frame_size_request;
177 widget_class->size_allocate = gtk_frame_size_allocate;
179 container_class->remove = gtk_frame_remove;
180 container_class->forall = gtk_frame_forall;
182 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
186 gtk_frame_init (GtkFrame *frame)
188 frame->label_widget = NULL;
189 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
190 frame->label_xalign = 0.0;
191 frame->label_yalign = 0.5;
195 gtk_frame_set_property (GObject *object,
202 frame = GTK_FRAME (object);
207 gtk_frame_set_label (frame, g_value_get_string (value));
209 case PROP_LABEL_XALIGN:
210 gtk_frame_set_label_align (frame, g_value_get_float (value),
211 frame->label_yalign);
213 case PROP_LABEL_YALIGN:
214 gtk_frame_set_label_align (frame, frame->label_xalign,
215 g_value_get_float (value));
218 case PROP_SHADOW_TYPE:
219 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
221 case PROP_LABEL_WIDGET:
222 gtk_frame_set_label_widget (frame, g_value_get_object (value));
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gtk_frame_get_property (GObject *object,
238 frame = GTK_FRAME (object);
243 g_value_set_string (value, gtk_frame_get_label (frame));
245 case PROP_LABEL_XALIGN:
246 g_value_set_float (value, frame->label_xalign);
248 case PROP_LABEL_YALIGN:
249 g_value_set_float (value, frame->label_yalign);
252 case PROP_SHADOW_TYPE:
253 g_value_set_enum (value, frame->shadow_type);
255 case PROP_LABEL_WIDGET:
256 g_value_set_object (value,
257 frame->label_widget ?
258 G_OBJECT (frame->label_widget) : NULL);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 * @label: the text to use as the label of the frame
270 * Creates a new #GtkFrame, with optional label @label.
271 * If @label is %NULL, the label is omitted.
273 * Return value: a new #GtkFrame widget
276 gtk_frame_new (const gchar *label)
278 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
282 gtk_frame_remove (GtkContainer *container,
285 GtkFrame *frame = GTK_FRAME (container);
287 if (frame->label_widget == child)
288 gtk_frame_set_label_widget (frame, NULL);
290 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
294 gtk_frame_forall (GtkContainer *container,
295 gboolean include_internals,
296 GtkCallback callback,
297 gpointer callback_data)
299 GtkBin *bin = GTK_BIN (container);
300 GtkFrame *frame = GTK_FRAME (container);
303 (* callback) (bin->child, callback_data);
305 if (frame->label_widget)
306 (* callback) (frame->label_widget, callback_data);
310 * gtk_frame_set_label:
311 * @frame: a #GtkFrame
312 * @label: the text to use as the label of the frame
314 * Sets the text of the label. If @label is %NULL,
315 * the current label is removed.
318 gtk_frame_set_label (GtkFrame *frame,
321 g_return_if_fail (GTK_IS_FRAME (frame));
325 gtk_frame_set_label_widget (frame, NULL);
329 GtkWidget *child = gtk_label_new (label);
330 gtk_widget_show (child);
332 gtk_frame_set_label_widget (frame, child);
337 * gtk_frame_get_label:
338 * @frame: a #GtkFrame
340 * If the frame's label widget is a #GtkLabel, returns the
341 * text in the label widget. (The frame will have a #GtkLabel
342 * for the label widget if a non-%NULL argument was passed
343 * to gtk_frame_new().)
345 * Return value: the text in the label, or %NULL if there
346 * was no label widget or the lable widget was not
347 * a #GtkLabel. This string is owned by GTK+ and
348 * must not be modified or freed.
350 G_CONST_RETURN gchar *
351 gtk_frame_get_label (GtkFrame *frame)
353 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
355 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
356 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
362 * gtk_frame_set_label_widget:
363 * @frame: a #GtkFrame
364 * @label_widget: the new label widget
366 * Sets the label widget for the frame. This is the widget that
367 * will appear embedded in the top edge of the frame as a
371 gtk_frame_set_label_widget (GtkFrame *frame,
372 GtkWidget *label_widget)
374 gboolean need_resize = FALSE;
376 g_return_if_fail (GTK_IS_FRAME (frame));
377 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
378 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
380 if (frame->label_widget == label_widget)
383 if (frame->label_widget)
385 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
386 gtk_widget_unparent (frame->label_widget);
389 frame->label_widget = label_widget;
393 frame->label_widget = label_widget;
394 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
395 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
398 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
399 gtk_widget_queue_resize (GTK_WIDGET (frame));
401 g_object_freeze_notify (G_OBJECT (frame));
402 g_object_notify (G_OBJECT (frame), "label_widget");
403 g_object_notify (G_OBJECT (frame), "label");
404 g_object_thaw_notify (G_OBJECT (frame));
408 * gtk_frame_get_label_widget:
409 * @frame: a #GtkFrame
411 * Retrieves the label widget for the frame. See
412 * gtk_frame_set_label_widget().
414 * Return value: the label widget, or %NULL if there is none.
417 gtk_frame_get_label_widget (GtkFrame *frame)
419 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
421 return frame->label_widget;
425 * gtk_frame_set_label_align:
426 * @frame: a #GtkFrame
427 * @xalign: The position of the label along the top edge
428 * of the widget. A value of 0.0 represents left alignment;
429 * 1.0 represents right alignment.
430 * @yalign: The y alignment of the label. A value of 0.0 aligns under
431 * the frame; 1.0 aligns above the frame.
433 * Sets the alignment of the frame widget's label. The
434 * default values for a newly created frame are 0.0 and 0.5.
437 gtk_frame_set_label_align (GtkFrame *frame,
441 g_return_if_fail (GTK_IS_FRAME (frame));
443 xalign = CLAMP (xalign, 0.0, 1.0);
444 yalign = CLAMP (yalign, 0.0, 1.0);
446 g_object_freeze_notify (G_OBJECT (frame));
447 if (xalign != frame->label_xalign)
449 frame->label_xalign = xalign;
450 g_object_notify (G_OBJECT (frame), "label_xalign");
453 if (yalign != frame->label_yalign)
455 frame->label_yalign = yalign;
456 g_object_notify (G_OBJECT (frame), "label_yalign");
459 g_object_thaw_notify (G_OBJECT (frame));
460 gtk_widget_queue_resize (GTK_WIDGET (frame));
464 * gtk_frame_get_label_align:
465 * @frame: a #GtkFrame
466 * @xalign: location to store X alignment of frame's label, or %NULL
467 * @yalign: location to store X alignment of frame's label, or %NULL
469 * Retrieves the X and Y alignment of the frame's label. See
470 * gtk_frame_set_label_align().
473 gtk_frame_get_label_align (GtkFrame *frame,
477 g_return_if_fail (GTK_IS_FRAME (frame));
480 *xalign = frame->label_xalign;
482 *yalign = frame->label_yalign;
486 * gtk_frame_set_shadow_type:
487 * @frame: a #GtkFrame
488 * @type: the new #GtkShadowType
490 * Sets the shadow type for @frame.
493 gtk_frame_set_shadow_type (GtkFrame *frame,
496 g_return_if_fail (GTK_IS_FRAME (frame));
498 if ((GtkShadowType) frame->shadow_type != type)
500 frame->shadow_type = type;
501 g_object_notify (G_OBJECT (frame), "shadow_type");
503 if (GTK_WIDGET_DRAWABLE (frame))
505 gtk_widget_queue_draw (GTK_WIDGET (frame));
508 gtk_widget_queue_resize (GTK_WIDGET (frame));
513 * gtk_frame_get_shadow_type:
514 * @frame: a #GtkFrame
516 * Retrieves the shadow type of the frame. See
517 * gtk_frame_set_shadow_type().
519 * Return value: the current shadow type of the frame.
522 gtk_frame_get_shadow_type (GtkFrame *frame)
524 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
526 return frame->shadow_type;
530 gtk_frame_paint (GtkWidget *widget,
534 gint x, y, width, height;
536 if (GTK_WIDGET_DRAWABLE (widget))
538 frame = GTK_FRAME (widget);
540 x = frame->child_allocation.x - widget->style->xthickness;
541 y = frame->child_allocation.y - widget->style->ythickness;
542 width = frame->child_allocation.width + 2 * widget->style->xthickness;
543 height = frame->child_allocation.height + 2 * widget->style->ythickness;
545 if (frame->label_widget)
547 GtkRequisition child_requisition;
552 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
554 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
555 xalign = frame->label_xalign;
557 xalign = 1 - frame->label_xalign;
559 height_extra = MAX (0, child_requisition.height - widget->style->ythickness);
560 height_extra *= (1 - frame->label_yalign);
562 height += height_extra;
564 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
567 gtk_paint_shadow_gap (widget->style, widget->window,
568 GTK_STATE_NORMAL, frame->shadow_type,
569 area, widget, "frame",
572 x2, child_requisition.width + 2 * LABEL_PAD);
575 gtk_paint_shadow (widget->style, widget->window,
576 GTK_STATE_NORMAL, frame->shadow_type,
577 area, widget, "frame",
578 x, y, width, height);
583 gtk_frame_expose (GtkWidget *widget,
584 GdkEventExpose *event)
586 if (GTK_WIDGET_DRAWABLE (widget))
588 gtk_frame_paint (widget, &event->area);
590 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
597 gtk_frame_size_request (GtkWidget *widget,
598 GtkRequisition *requisition)
600 GtkFrame *frame = GTK_FRAME (widget);
601 GtkBin *bin = GTK_BIN (widget);
602 GtkRequisition child_requisition;
604 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
606 gtk_widget_size_request (frame->label_widget, &child_requisition);
608 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
609 requisition->height =
610 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->ythickness);
614 requisition->width = 0;
615 requisition->height = 0;
618 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
620 gtk_widget_size_request (bin->child, &child_requisition);
622 requisition->width = MAX (requisition->width, child_requisition.width);
623 requisition->height += child_requisition.height;
626 requisition->width += (GTK_CONTAINER (widget)->border_width +
627 GTK_WIDGET (widget)->style->xthickness) * 2;
628 requisition->height += (GTK_CONTAINER (widget)->border_width +
629 GTK_WIDGET (widget)->style->ythickness) * 2;
633 gtk_frame_size_allocate (GtkWidget *widget,
634 GtkAllocation *allocation)
636 GtkFrame *frame = GTK_FRAME (widget);
637 GtkBin *bin = GTK_BIN (widget);
638 GtkAllocation new_allocation;
640 widget->allocation = *allocation;
642 gtk_frame_compute_child_allocation (frame, &new_allocation);
644 /* If the child allocation changed, that means that the frame is drawn
645 * in a new place, so we must redraw the entire widget.
647 if (GTK_WIDGET_MAPPED (widget) &&
648 (new_allocation.x != frame->child_allocation.x ||
649 new_allocation.y != frame->child_allocation.y ||
650 new_allocation.width != frame->child_allocation.width ||
651 new_allocation.height != frame->child_allocation.height))
652 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
654 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
655 gtk_widget_size_allocate (bin->child, &new_allocation);
657 frame->child_allocation = new_allocation;
659 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
661 GtkRequisition child_requisition;
662 GtkAllocation child_allocation;
665 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
667 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
668 xalign = frame->label_xalign;
670 xalign = 1 - frame->label_xalign;
672 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
673 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
674 child_allocation.width = child_requisition.width;
676 child_allocation.y = frame->child_allocation.y - child_requisition.height;
677 child_allocation.height = child_requisition.height;
679 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
684 gtk_frame_compute_child_allocation (GtkFrame *frame,
685 GtkAllocation *child_allocation)
687 g_return_if_fail (GTK_IS_FRAME (frame));
688 g_return_if_fail (child_allocation != NULL);
690 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
694 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
695 GtkAllocation *child_allocation)
697 GtkWidget *widget = GTK_WIDGET (frame);
698 GtkAllocation *allocation = &widget->allocation;
699 GtkRequisition child_requisition;
702 if (frame->label_widget)
704 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
705 top_margin = MAX (child_requisition.height, widget->style->ythickness);
708 top_margin = widget->style->ythickness;
710 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
711 widget->style->xthickness);
712 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
714 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
715 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
716 (gint)GTK_CONTAINER (frame)->border_width -
717 (gint)widget->style->ythickness));
719 child_allocation->x += allocation->x;
720 child_allocation->y += allocation->y;