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
45 static void gtk_frame_class_init (GtkFrameClass *klass);
46 static void gtk_frame_init (GtkFrame *frame);
47 static void gtk_frame_set_property (GObject *object,
51 static void gtk_frame_get_property (GObject *object,
55 static void gtk_frame_paint (GtkWidget *widget,
57 static gint gtk_frame_expose (GtkWidget *widget,
58 GdkEventExpose *event);
59 static void gtk_frame_size_request (GtkWidget *widget,
60 GtkRequisition *requisition);
61 static void gtk_frame_size_allocate (GtkWidget *widget,
62 GtkAllocation *allocation);
63 static void gtk_frame_map (GtkWidget *widget);
64 static void gtk_frame_unmap (GtkWidget *widget);
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 GtkType frame_type = 0;
87 static const GtkTypeInfo frame_info =
91 sizeof (GtkFrameClass),
92 (GtkClassInitFunc) gtk_frame_class_init,
93 (GtkObjectInitFunc) gtk_frame_init,
94 /* reserved_1 */ NULL,
95 /* reserved_2 */ NULL,
96 (GtkClassInitFunc) NULL,
99 frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
106 gtk_frame_class_init (GtkFrameClass *class)
108 GObjectClass *gobject_class;
109 GtkObjectClass *object_class;
110 GtkWidgetClass *widget_class;
111 GtkContainerClass *container_class;
113 gobject_class = (GObjectClass*) class;
114 object_class = GTK_OBJECT_CLASS (class);
115 widget_class = GTK_WIDGET_CLASS (class);
116 container_class = GTK_CONTAINER_CLASS (class);
118 parent_class = gtk_type_class (gtk_bin_get_type ());
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 _("Text of the frame's label."),
131 g_object_class_install_property (gobject_class,
133 g_param_spec_double ("label_xalign",
135 _("The horizontal alignment of the label."),
141 g_object_class_install_property (gobject_class,
143 g_param_spec_double ("label_yalign",
145 _("The vertical alignment of the label."),
151 g_object_class_install_property (gobject_class,
153 g_param_spec_enum ("shadow",
155 _("Appearance of the frame border."),
156 GTK_TYPE_SHADOW_TYPE,
157 GTK_SHADOW_ETCHED_IN,
158 G_PARAM_READABLE | G_PARAM_WRITABLE));
160 g_object_class_install_property (gobject_class,
162 g_param_spec_object ("label_widget",
164 _("A widget to display in place of the usual frame label."),
166 G_PARAM_READABLE | G_PARAM_WRITABLE));
168 widget_class->expose_event = gtk_frame_expose;
169 widget_class->size_request = gtk_frame_size_request;
170 widget_class->size_allocate = gtk_frame_size_allocate;
171 widget_class->map = gtk_frame_map;
172 widget_class->unmap = gtk_frame_unmap;
174 container_class->remove = gtk_frame_remove;
175 container_class->forall = gtk_frame_forall;
177 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
181 gtk_frame_init (GtkFrame *frame)
183 frame->label_widget = NULL;
184 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
185 frame->label_xalign = 0.0;
186 frame->label_yalign = 0.5;
190 gtk_frame_set_property (GObject *object,
197 frame = GTK_FRAME (object);
202 gtk_frame_set_label (frame, g_value_get_string (value));
204 case PROP_LABEL_XALIGN:
205 gtk_frame_set_label_align (frame, g_value_get_double (value),
206 frame->label_yalign);
208 case PROP_LABEL_YALIGN:
209 gtk_frame_set_label_align (frame, frame->label_xalign,
210 g_value_get_double (value));
213 gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
215 case PROP_LABEL_WIDGET:
216 gtk_frame_set_label_widget (frame, g_value_get_object (value));
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 gtk_frame_get_property (GObject *object,
232 frame = GTK_FRAME (object);
237 g_value_set_string (value, gtk_frame_get_label (frame));
239 case PROP_LABEL_XALIGN:
240 g_value_set_double (value, frame->label_xalign);
242 case PROP_LABEL_YALIGN:
243 g_value_set_double (value, frame->label_yalign);
246 g_value_set_enum (value, frame->shadow_type);
248 case PROP_LABEL_WIDGET:
249 g_value_set_object (value,
250 frame->label_widget ?
251 G_OBJECT (frame->label_widget) : NULL);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 gtk_frame_new (const gchar *label)
264 frame = gtk_type_new (gtk_frame_get_type ());
266 gtk_frame_set_label (frame, label);
268 return GTK_WIDGET (frame);
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 (frame != NULL);
304 g_return_if_fail (GTK_IS_FRAME (frame));
308 gtk_frame_set_label_widget (frame, NULL);
312 GtkWidget *child = gtk_label_new (label);
313 gtk_widget_show (child);
315 gtk_frame_set_label_widget (frame, child);
318 g_object_notify (G_OBJECT (frame), "label");
322 * gtk_frame_get_label:
323 * @frame: a #GtkFrame
325 * If the frame's label widget is a #GtkLabel, return the
326 * text in the label widget. (The frame will have a #GtkLabel
327 * for the label widget if a non-%NULL argument was passed
328 * to gtk_frame_new().)
330 * Return value: the text in the label, or %NULL if there
331 * was no label widget or the lable widget was not
332 * a #GtkLabel. This value must be freed with g_free().
334 G_CONST_RETURN gchar *
335 gtk_frame_get_label (GtkFrame *frame)
337 g_return_val_if_fail (frame != NULL, NULL);
338 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
340 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
341 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
347 * gtk_frame_set_label_widget:
348 * @frame: a #GtkFrame
349 * @label_widget: the new label widget
351 * Set the label widget for the frame. This is the widget that
352 * will appear embedded in the top edge of the frame as a
356 gtk_frame_set_label_widget (GtkFrame *frame,
357 GtkWidget *label_widget)
359 gboolean need_resize = FALSE;
361 g_return_if_fail (frame != NULL);
362 g_return_if_fail (GTK_IS_FRAME (frame));
363 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
364 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
366 if (frame->label_widget == label_widget)
369 if (frame->label_widget)
371 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
372 gtk_widget_unparent (frame->label_widget);
375 frame->label_widget = label_widget;
379 frame->label_widget = label_widget;
380 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
381 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
384 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
385 gtk_widget_queue_resize (GTK_WIDGET (frame));
387 g_object_notify (G_OBJECT (frame), "label_widget");
391 gtk_frame_set_label_align (GtkFrame *frame,
395 g_return_if_fail (frame != NULL);
396 g_return_if_fail (GTK_IS_FRAME (frame));
398 xalign = CLAMP (xalign, 0.0, 1.0);
399 yalign = CLAMP (yalign, 0.0, 1.0);
401 if (xalign != frame->label_xalign)
403 frame->label_xalign = xalign;
404 g_object_notify (G_OBJECT (frame), "label_xalign");
407 if (yalign != frame->label_yalign)
409 frame->label_yalign = yalign;
410 g_object_notify (G_OBJECT (frame), "label_yalign");
413 gtk_widget_queue_resize (GTK_WIDGET (frame));
417 gtk_frame_set_shadow_type (GtkFrame *frame,
420 g_return_if_fail (frame != NULL);
421 g_return_if_fail (GTK_IS_FRAME (frame));
423 if ((GtkShadowType) frame->shadow_type != type)
425 frame->shadow_type = type;
426 g_object_notify (G_OBJECT (frame), "shadow");
428 if (GTK_WIDGET_DRAWABLE (frame))
430 gtk_widget_queue_clear (GTK_WIDGET (frame));
433 gtk_widget_queue_resize (GTK_WIDGET (frame));
438 gtk_frame_paint (GtkWidget *widget,
442 gint x, y, width, height;
444 if (GTK_WIDGET_DRAWABLE (widget))
446 frame = GTK_FRAME (widget);
448 x = frame->child_allocation.x - widget->style->xthickness;
449 y = frame->child_allocation.y - widget->style->ythickness;
450 width = frame->child_allocation.width + 2 * widget->style->xthickness;
451 height = frame->child_allocation.height + 2 * widget->style->ythickness;
453 if (frame->label_widget)
455 GtkRequisition child_requisition;
460 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
462 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
463 xalign = frame->label_xalign;
465 xalign = 1 - frame->label_xalign;
467 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
468 y -= height_extra * (1 - frame->label_yalign);
469 height += height_extra * (1 - frame->label_yalign);
471 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
474 gtk_paint_shadow_gap (widget->style, widget->window,
475 GTK_STATE_NORMAL, frame->shadow_type,
476 area, widget, "frame",
479 x2, child_requisition.width + 2 * LABEL_PAD);
482 gtk_paint_shadow (widget->style, widget->window,
483 GTK_STATE_NORMAL, frame->shadow_type,
484 area, widget, "frame",
485 x, y, width, height);
490 gtk_frame_expose (GtkWidget *widget,
491 GdkEventExpose *event)
493 if (GTK_WIDGET_DRAWABLE (widget))
495 gtk_frame_paint (widget, &event->area);
497 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
504 gtk_frame_size_request (GtkWidget *widget,
505 GtkRequisition *requisition)
507 GtkFrame *frame = GTK_FRAME (widget);
508 GtkBin *bin = GTK_BIN (widget);
509 GtkRequisition child_requisition;
511 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
513 gtk_widget_size_request (frame->label_widget, &child_requisition);
515 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
516 requisition->height =
517 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
521 requisition->width = 0;
522 requisition->height = 0;
525 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
527 gtk_widget_size_request (bin->child, &child_requisition);
529 requisition->width = MAX (requisition->width, child_requisition.width);
530 requisition->height += child_requisition.height;
533 requisition->width += (GTK_CONTAINER (widget)->border_width +
534 GTK_WIDGET (widget)->style->xthickness) * 2;
535 requisition->height += (GTK_CONTAINER (widget)->border_width +
536 GTK_WIDGET (widget)->style->ythickness) * 2;
540 gtk_frame_size_allocate (GtkWidget *widget,
541 GtkAllocation *allocation)
543 GtkFrame *frame = GTK_FRAME (widget);
544 GtkBin *bin = GTK_BIN (widget);
545 GtkAllocation new_allocation;
547 widget->allocation = *allocation;
549 gtk_frame_compute_child_allocation (frame, &new_allocation);
551 /* If the child allocation changed, that means that the frame is drawn
552 * in a new place, so we must redraw the entire widget.
554 if (GTK_WIDGET_MAPPED (widget) &&
555 (new_allocation.x != frame->child_allocation.x ||
556 new_allocation.y != frame->child_allocation.y ||
557 new_allocation.width != frame->child_allocation.width ||
558 new_allocation.height != frame->child_allocation.height))
559 gtk_widget_queue_clear (widget);
561 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
562 gtk_widget_size_allocate (bin->child, &new_allocation);
564 frame->child_allocation = new_allocation;
566 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
568 GtkRequisition child_requisition;
569 GtkAllocation child_allocation;
572 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
574 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
575 xalign = frame->label_xalign;
577 xalign = 1 - frame->label_xalign;
579 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
580 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
581 child_allocation.width = child_requisition.width;
583 child_allocation.y = frame->child_allocation.y - child_requisition.height;
584 child_allocation.height = child_requisition.height;
586 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
591 gtk_frame_map (GtkWidget *widget)
593 GtkFrame *frame = GTK_FRAME (widget);
595 if (frame->label_widget &&
596 GTK_WIDGET_VISIBLE (frame->label_widget) &&
597 !GTK_WIDGET_MAPPED (frame->label_widget))
598 gtk_widget_map (frame->label_widget);
600 if (GTK_WIDGET_CLASS (parent_class)->map)
601 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
605 gtk_frame_unmap (GtkWidget *widget)
607 GtkFrame *frame = GTK_FRAME (widget);
609 if (GTK_WIDGET_CLASS (parent_class)->unmap)
610 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
612 if (frame->label_widget && GTK_WIDGET_MAPPED (frame->label_widget))
613 gtk_widget_unmap (frame->label_widget);
617 gtk_frame_compute_child_allocation (GtkFrame *frame,
618 GtkAllocation *child_allocation)
620 g_return_if_fail (frame != NULL);
621 g_return_if_fail (GTK_IS_FRAME (frame));
622 g_return_if_fail (child_allocation != NULL);
624 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
628 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
629 GtkAllocation *child_allocation)
631 GtkWidget *widget = GTK_WIDGET (frame);
632 GtkAllocation *allocation = &widget->allocation;
633 GtkRequisition child_requisition;
636 if (frame->label_widget)
638 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
639 top_margin = MAX (child_requisition.height, widget->style->ythickness);
642 top_margin = widget->style->ythickness;
644 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
645 widget->style->xthickness);
646 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
648 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
649 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
650 (gint)GTK_CONTAINER (frame)->border_width -
651 (gint)widget->style->ythickness));
653 child_allocation->x += allocation->x;
654 child_allocation->y += allocation->y;