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 GType frame_type = 0;
86 static const GTypeInfo frame_info =
88 sizeof (GtkFrameClass),
90 NULL, /* base_finalize */
91 (GClassInitFunc) gtk_frame_class_init,
92 NULL, /* class_finalize */
93 NULL, /* class_data */
96 (GInstanceInitFunc) gtk_frame_init,
99 frame_type = g_type_register_static (GTK_TYPE_BIN, "GtkFrame",
107 gtk_frame_class_init (GtkFrameClass *class)
109 GObjectClass *gobject_class;
110 GtkWidgetClass *widget_class;
111 GtkContainerClass *container_class;
113 gobject_class = (GObjectClass*) class;
114 widget_class = GTK_WIDGET_CLASS (class);
115 container_class = GTK_CONTAINER_CLASS (class);
117 parent_class = g_type_class_peek_parent (class);
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_float ("label_xalign",
134 _("The horizontal alignment of the label"),
140 g_object_class_install_property (gobject_class,
142 g_param_spec_float ("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_float (value),
210 frame->label_yalign);
212 case PROP_LABEL_YALIGN:
213 gtk_frame_set_label_align (frame, frame->label_xalign,
214 g_value_get_float (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_float (value, frame->label_xalign);
247 case PROP_LABEL_YALIGN:
248 g_value_set_float (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)
268 return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
272 gtk_frame_remove (GtkContainer *container,
275 GtkFrame *frame = GTK_FRAME (container);
277 if (frame->label_widget == child)
278 gtk_frame_set_label_widget (frame, NULL);
280 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
284 gtk_frame_forall (GtkContainer *container,
285 gboolean include_internals,
286 GtkCallback callback,
287 gpointer callback_data)
289 GtkBin *bin = GTK_BIN (container);
290 GtkFrame *frame = GTK_FRAME (container);
293 (* callback) (bin->child, callback_data);
295 if (frame->label_widget)
296 (* callback) (frame->label_widget, callback_data);
300 gtk_frame_set_label (GtkFrame *frame,
303 g_return_if_fail (GTK_IS_FRAME (frame));
307 gtk_frame_set_label_widget (frame, NULL);
311 GtkWidget *child = gtk_label_new (label);
312 gtk_widget_show (child);
314 gtk_frame_set_label_widget (frame, child);
317 g_object_notify (G_OBJECT (frame), "label");
321 * gtk_frame_get_label:
322 * @frame: a #GtkFrame
324 * If the frame's label widget is a #GtkLabel, return the
325 * text in the label widget. (The frame will have a #GtkLabel
326 * for the label widget if a non-%NULL argument was passed
327 * to gtk_frame_new().)
329 * Return value: the text in the label, or %NULL if there
330 * was no label widget or the lable widget was not
331 * a #GtkLabel. This string is owned by GTK+ and
332 * must not be modified or freed.
334 G_CONST_RETURN gchar *
335 gtk_frame_get_label (GtkFrame *frame)
337 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
339 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
340 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
346 * gtk_frame_set_label_widget:
347 * @frame: a #GtkFrame
348 * @label_widget: the new label widget
350 * Set the label widget for the frame. This is the widget that
351 * will appear embedded in the top edge of the frame as a
355 gtk_frame_set_label_widget (GtkFrame *frame,
356 GtkWidget *label_widget)
358 gboolean need_resize = FALSE;
360 g_return_if_fail (GTK_IS_FRAME (frame));
361 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
362 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
364 if (frame->label_widget == label_widget)
367 if (frame->label_widget)
369 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
370 gtk_widget_unparent (frame->label_widget);
373 frame->label_widget = label_widget;
377 frame->label_widget = label_widget;
378 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
379 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
382 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
383 gtk_widget_queue_resize (GTK_WIDGET (frame));
385 g_object_notify (G_OBJECT (frame), "label_widget");
389 * gtk_frame_get_label_widget:
390 * @frame: a #GtkFrame
392 * Retrieves the label widget for the frame. See
393 * gtk_frame_set_label_widget().
395 * Return value: the label widget, or %NULL if there is none.
398 gtk_frame_get_label_widget (GtkFrame *frame)
400 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
402 return frame->label_widget;
406 gtk_frame_set_label_align (GtkFrame *frame,
410 g_return_if_fail (GTK_IS_FRAME (frame));
412 xalign = CLAMP (xalign, 0.0, 1.0);
413 yalign = CLAMP (yalign, 0.0, 1.0);
415 g_object_freeze_notify (G_OBJECT (frame));
416 if (xalign != frame->label_xalign)
418 frame->label_xalign = xalign;
419 g_object_notify (G_OBJECT (frame), "label_xalign");
422 if (yalign != frame->label_yalign)
424 frame->label_yalign = yalign;
425 g_object_notify (G_OBJECT (frame), "label_yalign");
428 g_object_thaw_notify (G_OBJECT (frame));
429 gtk_widget_queue_resize (GTK_WIDGET (frame));
433 * gtk_frame_get_label_align:
434 * @frame: a #GtkFrame
435 * @xalign: location to store X alignment of frame's label, or %NULL
436 * @yalign: location to store X alignment of frame's label, or %NULL
438 * Retrieves the X and Y alignment of the frame's label. See
439 * gtk_frame_set_label_align().
442 gtk_frame_get_label_align (GtkFrame *frame,
446 g_return_if_fail (GTK_IS_FRAME (frame));
449 *xalign = frame->label_xalign;
451 *yalign = frame->label_yalign;
455 gtk_frame_set_shadow_type (GtkFrame *frame,
458 g_return_if_fail (GTK_IS_FRAME (frame));
460 if ((GtkShadowType) frame->shadow_type != type)
462 frame->shadow_type = type;
463 g_object_notify (G_OBJECT (frame), "shadow_type");
465 if (GTK_WIDGET_DRAWABLE (frame))
467 gtk_widget_queue_draw (GTK_WIDGET (frame));
470 gtk_widget_queue_resize (GTK_WIDGET (frame));
475 * gtk_frame_get_shadow_type:
476 * @frame: a #GtkFrame
478 * Retrieves the shadow type of the frame. See
479 * gtk_frame_set_shadow_type().
481 * Return value: the current shadow type of the frame.
484 gtk_frame_get_shadow_type (GtkFrame *frame)
486 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
488 return frame->shadow_type;
492 gtk_frame_paint (GtkWidget *widget,
496 gint x, y, width, height;
498 if (GTK_WIDGET_DRAWABLE (widget))
500 frame = GTK_FRAME (widget);
502 x = frame->child_allocation.x - widget->style->xthickness;
503 y = frame->child_allocation.y - widget->style->ythickness;
504 width = frame->child_allocation.width + 2 * widget->style->xthickness;
505 height = frame->child_allocation.height + 2 * widget->style->ythickness;
507 if (frame->label_widget)
509 GtkRequisition child_requisition;
514 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
516 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
517 xalign = frame->label_xalign;
519 xalign = 1 - frame->label_xalign;
521 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
522 y -= height_extra * (1 - frame->label_yalign);
523 height += height_extra * (1 - frame->label_yalign);
525 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
528 gtk_paint_shadow_gap (widget->style, widget->window,
529 GTK_STATE_NORMAL, frame->shadow_type,
530 area, widget, "frame",
533 x2, child_requisition.width + 2 * LABEL_PAD);
536 gtk_paint_shadow (widget->style, widget->window,
537 GTK_STATE_NORMAL, frame->shadow_type,
538 area, widget, "frame",
539 x, y, width, height);
544 gtk_frame_expose (GtkWidget *widget,
545 GdkEventExpose *event)
547 if (GTK_WIDGET_DRAWABLE (widget))
549 gtk_frame_paint (widget, &event->area);
551 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
558 gtk_frame_size_request (GtkWidget *widget,
559 GtkRequisition *requisition)
561 GtkFrame *frame = GTK_FRAME (widget);
562 GtkBin *bin = GTK_BIN (widget);
563 GtkRequisition child_requisition;
565 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
567 gtk_widget_size_request (frame->label_widget, &child_requisition);
569 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
570 requisition->height =
571 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
575 requisition->width = 0;
576 requisition->height = 0;
579 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
581 gtk_widget_size_request (bin->child, &child_requisition);
583 requisition->width = MAX (requisition->width, child_requisition.width);
584 requisition->height += child_requisition.height;
587 requisition->width += (GTK_CONTAINER (widget)->border_width +
588 GTK_WIDGET (widget)->style->xthickness) * 2;
589 requisition->height += (GTK_CONTAINER (widget)->border_width +
590 GTK_WIDGET (widget)->style->ythickness) * 2;
594 gtk_frame_size_allocate (GtkWidget *widget,
595 GtkAllocation *allocation)
597 GtkFrame *frame = GTK_FRAME (widget);
598 GtkBin *bin = GTK_BIN (widget);
599 GtkAllocation new_allocation;
601 widget->allocation = *allocation;
603 gtk_frame_compute_child_allocation (frame, &new_allocation);
605 /* If the child allocation changed, that means that the frame is drawn
606 * in a new place, so we must redraw the entire widget.
608 if (GTK_WIDGET_MAPPED (widget) &&
609 (new_allocation.x != frame->child_allocation.x ||
610 new_allocation.y != frame->child_allocation.y ||
611 new_allocation.width != frame->child_allocation.width ||
612 new_allocation.height != frame->child_allocation.height))
613 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
615 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
616 gtk_widget_size_allocate (bin->child, &new_allocation);
618 frame->child_allocation = new_allocation;
620 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
622 GtkRequisition child_requisition;
623 GtkAllocation child_allocation;
626 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
628 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
629 xalign = frame->label_xalign;
631 xalign = 1 - frame->label_xalign;
633 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
634 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
635 child_allocation.width = child_requisition.width;
637 child_allocation.y = frame->child_allocation.y - child_requisition.height;
638 child_allocation.height = child_requisition.height;
640 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
645 gtk_frame_compute_child_allocation (GtkFrame *frame,
646 GtkAllocation *child_allocation)
648 g_return_if_fail (GTK_IS_FRAME (frame));
649 g_return_if_fail (child_allocation != NULL);
651 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
655 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
656 GtkAllocation *child_allocation)
658 GtkWidget *widget = GTK_WIDGET (frame);
659 GtkAllocation *allocation = &widget->allocation;
660 GtkRequisition child_requisition;
663 if (frame->label_widget)
665 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
666 top_margin = MAX (child_requisition.height, widget->style->ythickness);
669 top_margin = widget->style->ythickness;
671 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
672 widget->style->xthickness);
673 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
675 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
676 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
677 (gint)GTK_CONTAINER (frame)->border_width -
678 (gint)widget->style->ythickness));
680 child_allocation->x += allocation->x;
681 child_allocation->y += allocation->y;