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/.
33 #define LABEL_SIDE_PAD 2
46 static void gtk_frame_class_init (GtkFrameClass *klass);
47 static void gtk_frame_init (GtkFrame *frame);
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 static GtkBinClass *parent_class = NULL;
80 gtk_frame_get_type (void)
82 static GtkType frame_type = 0;
86 static const GtkTypeInfo frame_info =
90 sizeof (GtkFrameClass),
91 (GtkClassInitFunc) gtk_frame_class_init,
92 (GtkObjectInitFunc) gtk_frame_init,
93 /* reserved_1 */ NULL,
94 /* reserved_2 */ NULL,
95 (GtkClassInitFunc) NULL,
98 frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
105 gtk_frame_class_init (GtkFrameClass *class)
107 GObjectClass *gobject_class;
108 GtkObjectClass *object_class;
109 GtkWidgetClass *widget_class;
110 GtkContainerClass *container_class;
112 gobject_class = (GObjectClass*) class;
113 object_class = GTK_OBJECT_CLASS (class);
114 widget_class = GTK_WIDGET_CLASS (class);
115 container_class = GTK_CONTAINER_CLASS (class);
117 parent_class = gtk_type_class (gtk_bin_get_type ());
119 gobject_class->set_property = gtk_frame_set_property;
120 gobject_class->get_property = gtk_frame_get_property;
122 g_object_class_install_property (gobject_class,
124 g_param_spec_string ("label",
126 _("Text of the frame's label."),
130 g_object_class_install_property (gobject_class,
132 g_param_spec_double ("label_xalign",
134 _("The horizontal alignment of the label."),
140 g_object_class_install_property (gobject_class,
142 g_param_spec_double ("label_yalign",
144 _("The vertical alignment of the label."),
150 g_object_class_install_property (gobject_class,
152 g_param_spec_enum ("shadow", NULL,
153 _("Deprecated property, use shadow_type instead."),
154 GTK_TYPE_SHADOW_TYPE,
155 GTK_SHADOW_ETCHED_IN,
156 G_PARAM_READABLE | G_PARAM_WRITABLE));
157 g_object_class_install_property (gobject_class,
159 g_param_spec_enum ("shadow_type",
161 _("Appearance of the frame border."),
162 GTK_TYPE_SHADOW_TYPE,
163 GTK_SHADOW_ETCHED_IN,
164 G_PARAM_READABLE | G_PARAM_WRITABLE));
166 g_object_class_install_property (gobject_class,
168 g_param_spec_object ("label_widget",
170 _("A widget to display in place of the usual frame label."),
172 G_PARAM_READABLE | G_PARAM_WRITABLE));
174 widget_class->expose_event = gtk_frame_expose;
175 widget_class->size_request = gtk_frame_size_request;
176 widget_class->size_allocate = gtk_frame_size_allocate;
178 container_class->remove = gtk_frame_remove;
179 container_class->forall = gtk_frame_forall;
181 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
185 gtk_frame_init (GtkFrame *frame)
187 frame->label_widget = NULL;
188 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
189 frame->label_xalign = 0.0;
190 frame->label_yalign = 0.5;
194 gtk_frame_set_property (GObject *object,
201 frame = GTK_FRAME (object);
206 gtk_frame_set_label (frame, g_value_get_string (value));
208 case PROP_LABEL_XALIGN:
209 gtk_frame_set_label_align (frame, g_value_get_double (value),
210 frame->label_yalign);
212 case PROP_LABEL_YALIGN:
213 gtk_frame_set_label_align (frame, frame->label_xalign,
214 g_value_get_double (value));
217 case PROP_SHADOW_TYPE:
218 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
220 case PROP_LABEL_WIDGET:
221 gtk_frame_set_label_widget (frame, g_value_get_object (value));
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 gtk_frame_get_property (GObject *object,
237 frame = GTK_FRAME (object);
242 g_value_set_string (value, gtk_frame_get_label (frame));
244 case PROP_LABEL_XALIGN:
245 g_value_set_double (value, frame->label_xalign);
247 case PROP_LABEL_YALIGN:
248 g_value_set_double (value, frame->label_yalign);
251 case PROP_SHADOW_TYPE:
252 g_value_set_enum (value, frame->shadow_type);
254 case PROP_LABEL_WIDGET:
255 g_value_set_object (value,
256 frame->label_widget ?
257 G_OBJECT (frame->label_widget) : NULL);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 gtk_frame_new (const gchar *label)
270 frame = gtk_type_new (gtk_frame_get_type ());
272 gtk_frame_set_label (frame, label);
274 return GTK_WIDGET (frame);
278 gtk_frame_remove (GtkContainer *container,
281 GtkFrame *frame = GTK_FRAME (container);
283 if (frame->label_widget == child)
284 gtk_frame_set_label_widget (frame, NULL);
286 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
290 gtk_frame_forall (GtkContainer *container,
291 gboolean include_internals,
292 GtkCallback callback,
293 gpointer callback_data)
295 GtkBin *bin = GTK_BIN (container);
296 GtkFrame *frame = GTK_FRAME (container);
299 (* callback) (bin->child, callback_data);
301 if (frame->label_widget)
302 (* callback) (frame->label_widget, callback_data);
306 gtk_frame_set_label (GtkFrame *frame,
309 g_return_if_fail (GTK_IS_FRAME (frame));
313 gtk_frame_set_label_widget (frame, NULL);
317 GtkWidget *child = gtk_label_new (label);
318 gtk_widget_show (child);
320 gtk_frame_set_label_widget (frame, child);
323 g_object_notify (G_OBJECT (frame), "label");
327 * gtk_frame_get_label:
328 * @frame: a #GtkFrame
330 * If the frame's label widget is a #GtkLabel, return the
331 * text in the label widget. (The frame will have a #GtkLabel
332 * for the label widget if a non-%NULL argument was passed
333 * to gtk_frame_new().)
335 * Return value: the text in the label, or %NULL if there
336 * was no label widget or the lable widget was not
337 * a #GtkLabel. This value must be freed with g_free().
339 G_CONST_RETURN gchar *
340 gtk_frame_get_label (GtkFrame *frame)
342 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
344 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
345 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
351 * gtk_frame_set_label_widget:
352 * @frame: a #GtkFrame
353 * @label_widget: the new label widget
355 * Set the label widget for the frame. This is the widget that
356 * will appear embedded in the top edge of the frame as a
360 gtk_frame_set_label_widget (GtkFrame *frame,
361 GtkWidget *label_widget)
363 gboolean need_resize = FALSE;
365 g_return_if_fail (GTK_IS_FRAME (frame));
366 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
367 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
369 if (frame->label_widget == label_widget)
372 if (frame->label_widget)
374 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
375 gtk_widget_unparent (frame->label_widget);
378 frame->label_widget = label_widget;
382 frame->label_widget = label_widget;
383 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
384 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
387 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
388 gtk_widget_queue_resize (GTK_WIDGET (frame));
390 g_object_notify (G_OBJECT (frame), "label_widget");
394 * gtk_frame_get_label_widget:
395 * @frame: a #GtkFrame
397 * Retrieves the label widget for the frame. See
398 * gtk_frame_set_label_widget().
400 * Return value: the label widget, or %NULL if there is none.
403 gtk_frame_get_label_widget (GtkFrame *frame)
405 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
407 return frame->label_widget;
411 gtk_frame_set_label_align (GtkFrame *frame,
415 g_return_if_fail (GTK_IS_FRAME (frame));
417 xalign = CLAMP (xalign, 0.0, 1.0);
418 yalign = CLAMP (yalign, 0.0, 1.0);
420 if (xalign != frame->label_xalign)
422 frame->label_xalign = xalign;
423 g_object_notify (G_OBJECT (frame), "label_xalign");
426 if (yalign != frame->label_yalign)
428 frame->label_yalign = yalign;
429 g_object_notify (G_OBJECT (frame), "label_yalign");
432 gtk_widget_queue_resize (GTK_WIDGET (frame));
436 * gtk_frame_get_label_align:
437 * @frame: a #GtkFrame
438 * @xalign: location to store X alignment of frame's label, or %NULL
439 * @yalign: location to store X alignment of frame's label, or %NULL
441 * Retrieves the X and Y alignment of the frame's label. See
442 * gtk_frame_set_label_align().
445 gtk_frame_get_label_align (GtkFrame *frame,
449 g_return_if_fail (GTK_IS_FRAME (frame));
452 *xalign = frame->label_xalign;
454 *yalign = frame->label_yalign;
458 gtk_frame_set_shadow_type (GtkFrame *frame,
461 g_return_if_fail (GTK_IS_FRAME (frame));
463 if ((GtkShadowType) frame->shadow_type != type)
465 frame->shadow_type = type;
466 g_object_notify (G_OBJECT (frame), "shadow_type");
468 if (GTK_WIDGET_DRAWABLE (frame))
470 gtk_widget_queue_clear (GTK_WIDGET (frame));
473 gtk_widget_queue_resize (GTK_WIDGET (frame));
478 * gtk_frame_get_shadow_type:
479 * @frame: a #GtkFrame
481 * Retrieves the shadow type of the frame. See
482 * gtk_frame_set_shadow_type().
484 * Return value: the current shadow type of the frame.
487 gtk_frame_get_shadow_type (GtkFrame *frame)
489 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
491 return frame->shadow_type;
495 gtk_frame_paint (GtkWidget *widget,
499 gint x, y, width, height;
501 if (GTK_WIDGET_DRAWABLE (widget))
503 frame = GTK_FRAME (widget);
505 x = frame->child_allocation.x - widget->style->xthickness;
506 y = frame->child_allocation.y - widget->style->ythickness;
507 width = frame->child_allocation.width + 2 * widget->style->xthickness;
508 height = frame->child_allocation.height + 2 * widget->style->ythickness;
510 if (frame->label_widget)
512 GtkRequisition child_requisition;
517 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
519 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
520 xalign = frame->label_xalign;
522 xalign = 1 - frame->label_xalign;
524 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
525 y -= height_extra * (1 - frame->label_yalign);
526 height += height_extra * (1 - frame->label_yalign);
528 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
531 gtk_paint_shadow_gap (widget->style, widget->window,
532 GTK_STATE_NORMAL, frame->shadow_type,
533 area, widget, "frame",
536 x2, child_requisition.width + 2 * LABEL_PAD);
539 gtk_paint_shadow (widget->style, widget->window,
540 GTK_STATE_NORMAL, frame->shadow_type,
541 area, widget, "frame",
542 x, y, width, height);
547 gtk_frame_expose (GtkWidget *widget,
548 GdkEventExpose *event)
550 if (GTK_WIDGET_DRAWABLE (widget))
552 gtk_frame_paint (widget, &event->area);
554 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
561 gtk_frame_size_request (GtkWidget *widget,
562 GtkRequisition *requisition)
564 GtkFrame *frame = GTK_FRAME (widget);
565 GtkBin *bin = GTK_BIN (widget);
566 GtkRequisition child_requisition;
568 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
570 gtk_widget_size_request (frame->label_widget, &child_requisition);
572 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
573 requisition->height =
574 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
578 requisition->width = 0;
579 requisition->height = 0;
582 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
584 gtk_widget_size_request (bin->child, &child_requisition);
586 requisition->width = MAX (requisition->width, child_requisition.width);
587 requisition->height += child_requisition.height;
590 requisition->width += (GTK_CONTAINER (widget)->border_width +
591 GTK_WIDGET (widget)->style->xthickness) * 2;
592 requisition->height += (GTK_CONTAINER (widget)->border_width +
593 GTK_WIDGET (widget)->style->ythickness) * 2;
597 gtk_frame_size_allocate (GtkWidget *widget,
598 GtkAllocation *allocation)
600 GtkFrame *frame = GTK_FRAME (widget);
601 GtkBin *bin = GTK_BIN (widget);
602 GtkAllocation new_allocation;
604 widget->allocation = *allocation;
606 gtk_frame_compute_child_allocation (frame, &new_allocation);
608 /* If the child allocation changed, that means that the frame is drawn
609 * in a new place, so we must redraw the entire widget.
611 if (GTK_WIDGET_MAPPED (widget) &&
612 (new_allocation.x != frame->child_allocation.x ||
613 new_allocation.y != frame->child_allocation.y ||
614 new_allocation.width != frame->child_allocation.width ||
615 new_allocation.height != frame->child_allocation.height))
616 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
618 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
619 gtk_widget_size_allocate (bin->child, &new_allocation);
621 frame->child_allocation = new_allocation;
623 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
625 GtkRequisition child_requisition;
626 GtkAllocation child_allocation;
629 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
631 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
632 xalign = frame->label_xalign;
634 xalign = 1 - frame->label_xalign;
636 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
637 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
638 child_allocation.width = child_requisition.width;
640 child_allocation.y = frame->child_allocation.y - child_requisition.height;
641 child_allocation.height = child_requisition.height;
643 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
648 gtk_frame_compute_child_allocation (GtkFrame *frame,
649 GtkAllocation *child_allocation)
651 g_return_if_fail (GTK_IS_FRAME (frame));
652 g_return_if_fail (child_allocation != NULL);
654 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
658 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
659 GtkAllocation *child_allocation)
661 GtkWidget *widget = GTK_WIDGET (frame);
662 GtkAllocation *allocation = &widget->allocation;
663 GtkRequisition child_requisition;
666 if (frame->label_widget)
668 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
669 top_margin = MAX (child_requisition.height, widget->style->ythickness);
672 top_margin = widget->style->ythickness;
674 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
675 widget->style->xthickness);
676 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
678 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
679 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
680 (gint)GTK_CONTAINER (frame)->border_width -
681 (gint)widget->style->ythickness));
683 child_allocation->x += allocation->x;
684 child_allocation->y += allocation->y;