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 (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 value must be freed with g_free().
333 G_CONST_RETURN gchar *
334 gtk_frame_get_label (GtkFrame *frame)
336 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
338 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
339 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
345 * gtk_frame_set_label_widget:
346 * @frame: a #GtkFrame
347 * @label_widget: the new label widget
349 * Set the label widget for the frame. This is the widget that
350 * will appear embedded in the top edge of the frame as a
354 gtk_frame_set_label_widget (GtkFrame *frame,
355 GtkWidget *label_widget)
357 gboolean need_resize = FALSE;
359 g_return_if_fail (GTK_IS_FRAME (frame));
360 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
361 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
363 if (frame->label_widget == label_widget)
366 if (frame->label_widget)
368 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
369 gtk_widget_unparent (frame->label_widget);
372 frame->label_widget = label_widget;
376 frame->label_widget = label_widget;
377 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
378 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
381 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
382 gtk_widget_queue_resize (GTK_WIDGET (frame));
384 g_object_notify (G_OBJECT (frame), "label_widget");
388 * gtk_frame_get_label_widget:
389 * @frame: a #GtkFrame
391 * Retrieves the label widget for the frame. See
392 * gtk_frame_set_label_widget().
394 * Return value: the label widget, or %NULL if there is none.
397 gtk_frame_get_label_widget (GtkFrame *frame)
399 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
401 return frame->label_widget;
405 gtk_frame_set_label_align (GtkFrame *frame,
409 g_return_if_fail (GTK_IS_FRAME (frame));
411 xalign = CLAMP (xalign, 0.0, 1.0);
412 yalign = CLAMP (yalign, 0.0, 1.0);
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 gtk_widget_queue_resize (GTK_WIDGET (frame));
430 * gtk_frame_get_label_align:
431 * @frame: a #GtkFrame
432 * @xalign: location to store X alignment of frame's label, or %NULL
433 * @yalign: location to store X alignment of frame's label, or %NULL
435 * Retrieves the X and Y alignment of the frame's label. See
436 * gtk_frame_set_label_align().
439 gtk_frame_get_label_align (GtkFrame *frame,
443 g_return_if_fail (GTK_IS_FRAME (frame));
446 *xalign = frame->label_xalign;
448 *yalign = frame->label_yalign;
452 gtk_frame_set_shadow_type (GtkFrame *frame,
455 g_return_if_fail (GTK_IS_FRAME (frame));
457 if ((GtkShadowType) frame->shadow_type != type)
459 frame->shadow_type = type;
460 g_object_notify (G_OBJECT (frame), "shadow");
462 if (GTK_WIDGET_DRAWABLE (frame))
464 gtk_widget_queue_clear (GTK_WIDGET (frame));
467 gtk_widget_queue_resize (GTK_WIDGET (frame));
472 * gtk_frame_get_shadow_type:
473 * @frame: a #GtkFrame
475 * Retrieves the shadow type of the frame. See
476 * gtk_frame_set_shadow_type().
478 * Return value: the current shadow type of the frame.
481 gtk_frame_get_shadow_type (GtkFrame *frame)
483 g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
485 return frame->shadow_type;
489 gtk_frame_paint (GtkWidget *widget,
493 gint x, y, width, height;
495 if (GTK_WIDGET_DRAWABLE (widget))
497 frame = GTK_FRAME (widget);
499 x = frame->child_allocation.x - widget->style->xthickness;
500 y = frame->child_allocation.y - widget->style->ythickness;
501 width = frame->child_allocation.width + 2 * widget->style->xthickness;
502 height = frame->child_allocation.height + 2 * widget->style->ythickness;
504 if (frame->label_widget)
506 GtkRequisition child_requisition;
511 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
513 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
514 xalign = frame->label_xalign;
516 xalign = 1 - frame->label_xalign;
518 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
519 y -= height_extra * (1 - frame->label_yalign);
520 height += height_extra * (1 - frame->label_yalign);
522 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
525 gtk_paint_shadow_gap (widget->style, widget->window,
526 GTK_STATE_NORMAL, frame->shadow_type,
527 area, widget, "frame",
530 x2, child_requisition.width + 2 * LABEL_PAD);
533 gtk_paint_shadow (widget->style, widget->window,
534 GTK_STATE_NORMAL, frame->shadow_type,
535 area, widget, "frame",
536 x, y, width, height);
541 gtk_frame_expose (GtkWidget *widget,
542 GdkEventExpose *event)
544 if (GTK_WIDGET_DRAWABLE (widget))
546 gtk_frame_paint (widget, &event->area);
548 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
555 gtk_frame_size_request (GtkWidget *widget,
556 GtkRequisition *requisition)
558 GtkFrame *frame = GTK_FRAME (widget);
559 GtkBin *bin = GTK_BIN (widget);
560 GtkRequisition child_requisition;
562 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
564 gtk_widget_size_request (frame->label_widget, &child_requisition);
566 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
567 requisition->height =
568 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
572 requisition->width = 0;
573 requisition->height = 0;
576 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
578 gtk_widget_size_request (bin->child, &child_requisition);
580 requisition->width = MAX (requisition->width, child_requisition.width);
581 requisition->height += child_requisition.height;
584 requisition->width += (GTK_CONTAINER (widget)->border_width +
585 GTK_WIDGET (widget)->style->xthickness) * 2;
586 requisition->height += (GTK_CONTAINER (widget)->border_width +
587 GTK_WIDGET (widget)->style->ythickness) * 2;
591 gtk_frame_size_allocate (GtkWidget *widget,
592 GtkAllocation *allocation)
594 GtkFrame *frame = GTK_FRAME (widget);
595 GtkBin *bin = GTK_BIN (widget);
596 GtkAllocation new_allocation;
598 widget->allocation = *allocation;
600 gtk_frame_compute_child_allocation (frame, &new_allocation);
602 /* If the child allocation changed, that means that the frame is drawn
603 * in a new place, so we must redraw the entire widget.
605 if (GTK_WIDGET_MAPPED (widget) &&
606 (new_allocation.x != frame->child_allocation.x ||
607 new_allocation.y != frame->child_allocation.y ||
608 new_allocation.width != frame->child_allocation.width ||
609 new_allocation.height != frame->child_allocation.height))
610 gtk_widget_queue_clear (widget);
612 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
613 gtk_widget_size_allocate (bin->child, &new_allocation);
615 frame->child_allocation = new_allocation;
617 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
619 GtkRequisition child_requisition;
620 GtkAllocation child_allocation;
623 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
625 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
626 xalign = frame->label_xalign;
628 xalign = 1 - frame->label_xalign;
630 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
631 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
632 child_allocation.width = child_requisition.width;
634 child_allocation.y = frame->child_allocation.y - child_requisition.height;
635 child_allocation.height = child_requisition.height;
637 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
642 gtk_frame_map (GtkWidget *widget)
644 GtkFrame *frame = GTK_FRAME (widget);
646 if (frame->label_widget &&
647 GTK_WIDGET_VISIBLE (frame->label_widget) &&
648 !GTK_WIDGET_MAPPED (frame->label_widget))
649 gtk_widget_map (frame->label_widget);
651 if (GTK_WIDGET_CLASS (parent_class)->map)
652 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
656 gtk_frame_unmap (GtkWidget *widget)
658 GtkFrame *frame = GTK_FRAME (widget);
660 if (GTK_WIDGET_CLASS (parent_class)->unmap)
661 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
663 if (frame->label_widget && GTK_WIDGET_MAPPED (frame->label_widget))
664 gtk_widget_unmap (frame->label_widget);
668 gtk_frame_compute_child_allocation (GtkFrame *frame,
669 GtkAllocation *child_allocation)
671 g_return_if_fail (GTK_IS_FRAME (frame));
672 g_return_if_fail (child_allocation != NULL);
674 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
678 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
679 GtkAllocation *child_allocation)
681 GtkWidget *widget = GTK_WIDGET (frame);
682 GtkAllocation *allocation = &widget->allocation;
683 GtkRequisition child_requisition;
686 if (frame->label_widget)
688 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
689 top_margin = MAX (child_requisition.height, widget->style->ythickness);
692 top_margin = widget->style->ythickness;
694 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
695 widget->style->xthickness);
696 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
698 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
699 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
700 (gint)GTK_CONTAINER (frame)->border_width -
701 (gint)widget->style->ythickness));
703 child_allocation->x += allocation->x;
704 child_allocation->y += allocation->y;