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/.
32 #define LABEL_SIDE_PAD 2
43 static void gtk_frame_class_init (GtkFrameClass *klass);
44 static void gtk_frame_init (GtkFrame *frame);
45 static void gtk_frame_set_arg (GtkObject *object,
48 static void gtk_frame_get_arg (GtkObject *object,
51 static void gtk_frame_paint (GtkWidget *widget,
53 static gint gtk_frame_expose (GtkWidget *widget,
54 GdkEventExpose *event);
55 static void gtk_frame_size_request (GtkWidget *widget,
56 GtkRequisition *requisition);
57 static void gtk_frame_size_allocate (GtkWidget *widget,
58 GtkAllocation *allocation);
59 static void gtk_frame_map (GtkWidget *widget);
60 static void gtk_frame_unmap (GtkWidget *widget);
61 static void gtk_frame_remove (GtkContainer *container,
63 static void gtk_frame_forall (GtkContainer *container,
64 gboolean include_internals,
66 gpointer callback_data);
68 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
69 GtkAllocation *child_allocation);
70 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
71 GtkAllocation *child_allocation);
73 static GtkBinClass *parent_class = NULL;
76 /* Here until I convince timj about memory management behavior
78 gchar * gtk_frame_get_label (GtkFrame *frame);
79 gchar * gtk_label_get_text (GtkLabel *label);
83 gtk_frame_get_type (void)
85 static GtkType frame_type = 0;
89 static const GtkTypeInfo frame_info =
93 sizeof (GtkFrameClass),
94 (GtkClassInitFunc) gtk_frame_class_init,
95 (GtkObjectInitFunc) gtk_frame_init,
96 /* reserved_1 */ NULL,
97 /* reserved_2 */ NULL,
98 (GtkClassInitFunc) NULL,
101 frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
108 gtk_frame_class_init (GtkFrameClass *class)
110 GtkObjectClass *object_class;
111 GtkWidgetClass *widget_class;
112 GtkContainerClass *container_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 gtk_object_add_arg_type ("GtkFrame::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
121 gtk_object_add_arg_type ("GtkFrame::label_xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_XALIGN);
122 gtk_object_add_arg_type ("GtkFrame::label_yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_YALIGN);
123 gtk_object_add_arg_type ("GtkFrame::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
125 object_class->set_arg = gtk_frame_set_arg;
126 object_class->get_arg = gtk_frame_get_arg;
128 widget_class->expose_event = gtk_frame_expose;
129 widget_class->size_request = gtk_frame_size_request;
130 widget_class->size_allocate = gtk_frame_size_allocate;
131 widget_class->map = gtk_frame_map;
132 widget_class->unmap = gtk_frame_unmap;
134 container_class->remove = gtk_frame_remove;
135 container_class->forall = gtk_frame_forall;
137 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
141 gtk_frame_init (GtkFrame *frame)
143 frame->label_widget = NULL;
144 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
145 frame->label_xalign = 0.0;
146 frame->label_yalign = 0.5;
150 gtk_frame_set_arg (GtkObject *object,
156 frame = GTK_FRAME (object);
161 gtk_frame_set_label (frame, GTK_VALUE_STRING (*arg));
163 case ARG_LABEL_XALIGN:
164 gtk_frame_set_label_align (frame, GTK_VALUE_FLOAT (*arg), frame->label_yalign);
166 case ARG_LABEL_YALIGN:
167 gtk_frame_set_label_align (frame, frame->label_xalign, GTK_VALUE_FLOAT (*arg));
170 gtk_frame_set_shadow_type (frame, GTK_VALUE_ENUM (*arg));
178 gtk_frame_get_arg (GtkObject *object,
184 frame = GTK_FRAME (object);
189 GTK_VALUE_STRING (*arg) = gtk_frame_get_label (frame);
191 case ARG_LABEL_XALIGN:
192 GTK_VALUE_FLOAT (*arg) = frame->label_xalign;
194 case ARG_LABEL_YALIGN:
195 GTK_VALUE_FLOAT (*arg) = frame->label_yalign;
198 GTK_VALUE_ENUM (*arg) = frame->shadow_type;
201 arg->type = GTK_TYPE_INVALID;
207 gtk_frame_new (const gchar *label)
211 frame = gtk_type_new (gtk_frame_get_type ());
213 gtk_frame_set_label (frame, label);
215 return GTK_WIDGET (frame);
219 gtk_frame_remove (GtkContainer *container,
222 GtkFrame *frame = GTK_FRAME (container);
224 if (frame->label_widget == child)
225 gtk_frame_set_label_widget (frame, NULL);
227 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
231 gtk_frame_forall (GtkContainer *container,
232 gboolean include_internals,
233 GtkCallback callback,
234 gpointer callback_data)
236 GtkBin *bin = GTK_BIN (container);
237 GtkFrame *frame = GTK_FRAME (container);
240 (* callback) (bin->child, callback_data);
242 if (frame->label_widget)
243 (* callback) (frame->label_widget, callback_data);
247 gtk_frame_set_label (GtkFrame *frame,
250 g_return_if_fail (frame != NULL);
251 g_return_if_fail (GTK_IS_FRAME (frame));
255 gtk_frame_set_label_widget (frame, NULL);
259 GtkWidget *child = gtk_label_new (label);
260 gtk_widget_show (child);
262 gtk_frame_set_label_widget (frame, child);
267 * gtk_frame_get_label:
268 * @frame: a #GtkFrame
270 * If the frame's label widget is a #GtkLabel, return the
271 * text in the label widget. (The frame will have a #GtkLabel
272 * for the label widget if a non-%NULL argument was passed
273 * to gtk_frame_new().)
275 * Return value: the text in the label, or %NULL if there
276 * was no label widget or the lable widget was not
277 * a #GtkLabel. This value must be freed with g_free().
280 gtk_frame_get_label (GtkFrame *frame)
282 g_return_val_if_fail (frame != NULL, NULL);
283 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
285 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
286 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
292 * gtk_frame_set_label_widget:
293 * @frame: a #GtkFrame
294 * @label_widget: the new label widget
296 * Set the label widget for the frame. This is the widget that
297 * will appear embedded in the top edge of the frame as a
301 gtk_frame_set_label_widget (GtkFrame *frame,
302 GtkWidget *label_widget)
304 gboolean need_resize = FALSE;
306 g_return_if_fail (frame != NULL);
307 g_return_if_fail (GTK_IS_FRAME (frame));
308 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
309 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
311 if (frame->label_widget == label_widget)
314 if (frame->label_widget)
316 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
317 gtk_widget_unparent (frame->label_widget);
320 frame->label_widget = label_widget;
324 frame->label_widget = label_widget;
325 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
326 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
329 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
330 gtk_widget_queue_resize (GTK_WIDGET (frame));
334 gtk_frame_set_label_align (GtkFrame *frame,
338 g_return_if_fail (frame != NULL);
339 g_return_if_fail (GTK_IS_FRAME (frame));
341 xalign = CLAMP (xalign, 0.0, 1.0);
342 yalign = CLAMP (yalign, 0.0, 1.0);
344 if ((xalign != frame->label_xalign) || (yalign != frame->label_yalign))
346 frame->label_xalign = xalign;
347 frame->label_yalign = yalign;
349 gtk_widget_queue_resize (GTK_WIDGET (frame));
354 gtk_frame_set_shadow_type (GtkFrame *frame,
357 g_return_if_fail (frame != NULL);
358 g_return_if_fail (GTK_IS_FRAME (frame));
360 if ((GtkShadowType) frame->shadow_type != type)
362 frame->shadow_type = type;
364 if (GTK_WIDGET_DRAWABLE (frame))
366 gtk_widget_queue_clear (GTK_WIDGET (frame));
368 gtk_widget_queue_resize (GTK_WIDGET (frame));
373 gtk_frame_paint (GtkWidget *widget,
377 gint x, y, width, height;
379 if (GTK_WIDGET_DRAWABLE (widget))
381 frame = GTK_FRAME (widget);
383 x = frame->child_allocation.x - widget->style->xthickness;
384 y = frame->child_allocation.y - widget->style->ythickness;
385 width = frame->child_allocation.width + 2 * widget->style->xthickness;
386 height = frame->child_allocation.height + 2 * widget->style->ythickness;
388 if (frame->label_widget)
390 GtkRequisition child_requisition;
395 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
397 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
398 xalign = frame->label_xalign;
400 xalign = 1 - frame->label_xalign;
402 height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
403 y -= height_extra * (1 - frame->label_yalign);
404 height += height_extra * (1 - frame->label_yalign);
406 x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
408 gtk_paint_shadow_gap (widget->style, widget->window,
409 GTK_STATE_NORMAL, frame->shadow_type,
410 area, widget, "frame",
413 x2, child_requisition.width + 2 * LABEL_PAD);
416 gtk_paint_shadow (widget->style, widget->window,
417 GTK_STATE_NORMAL, frame->shadow_type,
418 area, widget, "frame",
419 x, y, width, height);
424 gtk_frame_expose (GtkWidget *widget,
425 GdkEventExpose *event)
427 GtkBin *bin = GTK_BIN (widget);
428 GtkFrame *frame = GTK_FRAME (widget);
429 GdkEventExpose child_event;
431 if (GTK_WIDGET_DRAWABLE (widget))
433 gtk_frame_paint (widget, &event->area);
435 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child))
437 child_event = *event;
438 if (gtk_widget_intersect (bin->child, &event->area, &child_event.area))
439 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
442 if (frame->label_widget && GTK_WIDGET_NO_WINDOW (frame->label_widget))
444 child_event = *event;
445 if (gtk_widget_intersect (frame->label_widget, &event->area, &child_event.area))
446 gtk_widget_event (frame->label_widget, (GdkEvent*) &child_event);
454 gtk_frame_size_request (GtkWidget *widget,
455 GtkRequisition *requisition)
457 GtkFrame *frame = GTK_FRAME (widget);
458 GtkBin *bin = GTK_BIN (widget);
459 GtkRequisition child_requisition;
461 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
463 gtk_widget_size_request (frame->label_widget, &child_requisition);
465 requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
466 requisition->height =
467 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
471 requisition->width = 0;
472 requisition->height = 0;
475 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
477 gtk_widget_size_request (bin->child, &child_requisition);
479 requisition->width = MAX (requisition->width, child_requisition.width);
480 requisition->height += child_requisition.height;
483 requisition->width += (GTK_CONTAINER (widget)->border_width +
484 GTK_WIDGET (widget)->style->xthickness) * 2;
485 requisition->height += (GTK_CONTAINER (widget)->border_width +
486 GTK_WIDGET (widget)->style->ythickness) * 2;
490 gtk_frame_size_allocate (GtkWidget *widget,
491 GtkAllocation *allocation)
493 GtkFrame *frame = GTK_FRAME (widget);
494 GtkBin *bin = GTK_BIN (widget);
495 GtkAllocation new_allocation;
497 widget->allocation = *allocation;
499 gtk_frame_compute_child_allocation (frame, &new_allocation);
501 /* If the child allocation changed, that means that the frame is drawn
502 * in a new place, so we must redraw the entire widget.
504 if (GTK_WIDGET_MAPPED (widget) &&
505 (new_allocation.x != frame->child_allocation.x ||
506 new_allocation.y != frame->child_allocation.y ||
507 new_allocation.width != frame->child_allocation.width ||
508 new_allocation.height != frame->child_allocation.height))
509 gtk_widget_queue_clear (widget);
511 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
512 gtk_widget_size_allocate (bin->child, &new_allocation);
514 frame->child_allocation = new_allocation;
516 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
518 GtkRequisition child_requisition;
519 GtkAllocation child_allocation;
522 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
524 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
525 xalign = frame->label_xalign;
527 xalign = 1 - frame->label_xalign;
529 child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
530 (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
531 child_allocation.width = child_requisition.width;
533 child_allocation.y = frame->child_allocation.y - child_requisition.height;
534 child_allocation.height = child_requisition.height;
536 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
541 gtk_frame_map (GtkWidget *widget)
543 GtkFrame *frame = GTK_FRAME (widget);
545 if (frame->label_widget &&
546 GTK_WIDGET_VISIBLE (frame->label_widget) &&
547 !GTK_WIDGET_MAPPED (frame->label_widget))
548 gtk_widget_map (frame->label_widget);
550 if (GTK_WIDGET_CLASS (parent_class)->map)
551 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
555 gtk_frame_unmap (GtkWidget *widget)
557 GtkFrame *frame = GTK_FRAME (widget);
559 if (GTK_WIDGET_CLASS (parent_class)->unmap)
560 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
562 if (frame->label_widget && GTK_WIDGET_MAPPED (frame->label_widget))
563 gtk_widget_unmap (frame->label_widget);
567 gtk_frame_compute_child_allocation (GtkFrame *frame,
568 GtkAllocation *child_allocation)
570 g_return_if_fail (frame != NULL);
571 g_return_if_fail (GTK_IS_FRAME (frame));
572 g_return_if_fail (child_allocation != NULL);
574 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
578 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
579 GtkAllocation *child_allocation)
581 GtkWidget *widget = GTK_WIDGET (frame);
582 GtkAllocation *allocation = &widget->allocation;
583 GtkRequisition child_requisition;
586 if (frame->label_widget)
588 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
589 top_margin = MAX (child_requisition.height, widget->style->ythickness);
592 top_margin = widget->style->ythickness;
594 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
595 widget->style->xthickness);
596 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
598 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
599 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
600 (gint)GTK_CONTAINER (frame)->border_width -
601 (gint)widget->style->ythickness));
603 child_allocation->x += allocation->x;
604 child_allocation->y += allocation->y;