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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
40 static void gtk_frame_class_init (GtkFrameClass *klass);
41 static void gtk_frame_init (GtkFrame *frame);
42 static void gtk_frame_set_arg (GtkObject *object,
45 static void gtk_frame_get_arg (GtkObject *object,
48 static void gtk_frame_paint (GtkWidget *widget,
50 static void gtk_frame_draw (GtkWidget *widget,
52 static gint gtk_frame_expose (GtkWidget *widget,
53 GdkEventExpose *event);
54 static void gtk_frame_size_request (GtkWidget *widget,
55 GtkRequisition *requisition);
56 static void gtk_frame_size_allocate (GtkWidget *widget,
57 GtkAllocation *allocation);
58 static void gtk_frame_map (GtkWidget *widget);
59 static void gtk_frame_unmap (GtkWidget *widget);
60 static void gtk_frame_remove (GtkContainer *container,
62 static void gtk_frame_forall (GtkContainer *container,
63 gboolean include_internals,
65 gpointer callback_data);
67 static void gtk_frame_compute_child_allocation (GtkFrame *frame,
68 GtkAllocation *child_allocation);
69 static void gtk_frame_real_compute_child_allocation (GtkFrame *frame,
70 GtkAllocation *child_allocation);
72 static GtkBinClass *parent_class = NULL;
76 gtk_frame_get_type (void)
78 static GtkType frame_type = 0;
82 static const GtkTypeInfo frame_info =
86 sizeof (GtkFrameClass),
87 (GtkClassInitFunc) gtk_frame_class_init,
88 (GtkObjectInitFunc) gtk_frame_init,
89 /* reserved_1 */ NULL,
90 /* reserved_2 */ NULL,
91 (GtkClassInitFunc) NULL,
94 frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
101 gtk_frame_class_init (GtkFrameClass *class)
103 GtkObjectClass *object_class;
104 GtkWidgetClass *widget_class;
105 GtkContainerClass *container_class;
107 object_class = GTK_OBJECT_CLASS (class);
108 widget_class = GTK_WIDGET_CLASS (class);
109 container_class = GTK_CONTAINER_CLASS (class);
111 parent_class = gtk_type_class (gtk_bin_get_type ());
113 gtk_object_add_arg_type ("GtkFrame::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
114 gtk_object_add_arg_type ("GtkFrame::label_xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_XALIGN);
115 gtk_object_add_arg_type ("GtkFrame::label_yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_YALIGN);
116 gtk_object_add_arg_type ("GtkFrame::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
118 object_class->set_arg = gtk_frame_set_arg;
119 object_class->get_arg = gtk_frame_get_arg;
121 widget_class->draw = gtk_frame_draw;
122 widget_class->expose_event = gtk_frame_expose;
123 widget_class->size_request = gtk_frame_size_request;
124 widget_class->size_allocate = gtk_frame_size_allocate;
125 widget_class->map = gtk_frame_map;
126 widget_class->unmap = gtk_frame_unmap;
128 container_class->remove = gtk_frame_remove;
129 container_class->forall = gtk_frame_forall;
131 class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
135 gtk_frame_init (GtkFrame *frame)
137 frame->label_widget = NULL;
138 frame->shadow_type = GTK_SHADOW_ETCHED_IN;
139 frame->label_xalign = 0.0;
140 frame->label_yalign = 0.5;
144 gtk_frame_set_arg (GtkObject *object,
150 frame = GTK_FRAME (object);
155 gtk_frame_set_label (frame, GTK_VALUE_STRING (*arg));
157 case ARG_LABEL_XALIGN:
158 gtk_frame_set_label_align (frame, GTK_VALUE_FLOAT (*arg), frame->label_yalign);
160 case ARG_LABEL_YALIGN:
161 gtk_frame_set_label_align (frame, frame->label_xalign, GTK_VALUE_FLOAT (*arg));
164 gtk_frame_set_shadow_type (frame, GTK_VALUE_ENUM (*arg));
172 gtk_frame_get_arg (GtkObject *object,
178 frame = GTK_FRAME (object);
183 GTK_VALUE_STRING (*arg) = gtk_frame_get_label (frame);
185 case ARG_LABEL_XALIGN:
186 GTK_VALUE_FLOAT (*arg) = frame->label_xalign;
188 case ARG_LABEL_YALIGN:
189 GTK_VALUE_FLOAT (*arg) = frame->label_yalign;
192 GTK_VALUE_ENUM (*arg) = frame->shadow_type;
195 arg->type = GTK_TYPE_INVALID;
201 gtk_frame_new (const gchar *label)
205 frame = gtk_type_new (gtk_frame_get_type ());
207 gtk_frame_set_label (frame, label);
209 return GTK_WIDGET (frame);
213 gtk_frame_remove (GtkContainer *container,
216 GtkFrame *frame = GTK_FRAME (container);
218 if (frame->label_widget == child)
219 gtk_frame_set_label_widget (frame, NULL);
221 GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
225 gtk_frame_forall (GtkContainer *container,
226 gboolean include_internals,
227 GtkCallback callback,
228 gpointer callback_data)
230 GtkBin *bin = GTK_BIN (container);
231 GtkFrame *frame = GTK_FRAME (container);
234 (* callback) (bin->child, callback_data);
236 if (frame->label_widget)
237 (* callback) (frame->label_widget, callback_data);
241 gtk_frame_set_label (GtkFrame *frame,
244 g_return_if_fail (frame != NULL);
245 g_return_if_fail (GTK_IS_FRAME (frame));
249 gtk_frame_set_label_widget (frame, NULL);
253 GtkWidget *child = gtk_label_new (label);
254 gtk_widget_show (child);
256 gtk_frame_set_label_widget (frame, child);
261 * gtk_frame_get_label:
262 * @frame: a #GtkFrame
264 * If the frame's label widget is a #GtkLabel, return the
265 * text in the label widget. (The frame will have a #GtkLabel
266 * for the label widget if a non-%NULL argument was passed
267 * to gtk_frame_new().)
269 * Return value: the text in the label, or %NULL if there
270 * was no label widget or the lable widget was not
271 * a #GtkLabel. This value must be freed with g_free().
274 gtk_frame_get_label (GtkFrame *frame)
276 g_return_val_if_fail (frame != NULL, NULL);
277 g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
279 if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
280 return gtk_label_get_text (GTK_LABEL (frame->label_widget));
286 * gtk_frame_set_label_widget:
287 * @frame: a #GtkFrame
288 * @label_widget: the new label widget
290 * Set the label widget for the frame. This is the widget that
291 * will appear embedded in the top edge of the frame as a
295 gtk_frame_set_label_widget (GtkFrame *frame,
296 GtkWidget *label_widget)
298 gboolean need_resize = FALSE;
300 g_return_if_fail (frame != NULL);
301 g_return_if_fail (GTK_IS_FRAME (frame));
302 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
303 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
305 if (frame->label_widget == label_widget)
308 if (frame->label_widget)
310 need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
311 gtk_widget_unparent (frame->label_widget);
314 frame->label_widget = label_widget;
318 frame->label_widget = label_widget;
319 gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
320 need_resize |= GTK_WIDGET_VISIBLE (label_widget);
323 if (GTK_WIDGET_VISIBLE (frame) && need_resize)
324 gtk_widget_queue_resize (GTK_WIDGET (frame));
328 gtk_frame_set_label_align (GtkFrame *frame,
332 g_return_if_fail (frame != NULL);
333 g_return_if_fail (GTK_IS_FRAME (frame));
335 xalign = CLAMP (xalign, 0.0, 1.0);
336 yalign = CLAMP (yalign, 0.0, 1.0);
338 if ((xalign != frame->label_xalign) || (yalign != frame->label_yalign))
340 frame->label_xalign = xalign;
341 frame->label_yalign = yalign;
343 gtk_widget_queue_resize (GTK_WIDGET (frame));
348 gtk_frame_set_shadow_type (GtkFrame *frame,
351 g_return_if_fail (frame != NULL);
352 g_return_if_fail (GTK_IS_FRAME (frame));
354 if ((GtkShadowType) frame->shadow_type != type)
356 frame->shadow_type = type;
358 if (GTK_WIDGET_DRAWABLE (frame))
360 gtk_widget_queue_clear (GTK_WIDGET (frame));
362 gtk_widget_queue_resize (GTK_WIDGET (frame));
367 gtk_frame_paint (GtkWidget *widget,
371 gint x, y, width, height;
373 if (GTK_WIDGET_DRAWABLE (widget))
375 frame = GTK_FRAME (widget);
377 x = frame->child_allocation.x - widget->style->klass->xthickness;
378 y = frame->child_allocation.y - widget->style->klass->ythickness;
379 width = frame->child_allocation.width + 2 * widget->style->klass->xthickness;
380 height = frame->child_allocation.height + 2 * widget->style->klass->ythickness;
382 if (frame->label_widget)
384 GtkRequisition child_requisition;
389 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
391 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
392 xalign = frame->label_xalign;
394 xalign = 1 - frame->label_xalign;
396 height_extra = MAX (0, child_requisition.height - widget->style->klass->xthickness);
397 y -= height_extra * (1 - frame->label_yalign);
398 height += height_extra * (1 - frame->label_yalign);
400 x2 = 2 + (frame->child_allocation.width - child_requisition.width) * xalign;
402 gtk_paint_shadow_gap (widget->style, widget->window,
403 GTK_STATE_NORMAL, frame->shadow_type,
404 area, widget, "frame",
407 x2, child_requisition.width - 4);
410 gtk_paint_shadow (widget->style, widget->window,
411 GTK_STATE_NORMAL, frame->shadow_type,
412 area, widget, "frame",
413 x, y, width, height);
418 gtk_frame_draw (GtkWidget *widget,
421 GtkBin *bin = GTK_BIN (widget);
422 GtkFrame *frame = GTK_FRAME (widget);
423 GdkRectangle child_area;
425 if (GTK_WIDGET_DRAWABLE (widget))
427 gtk_frame_paint (widget, area);
429 if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
430 gtk_widget_draw (bin->child, &child_area);
432 if (frame->label_widget && gtk_widget_intersect (frame->label_widget, area, &child_area))
433 gtk_widget_draw (frame->label_widget, &child_area);
438 gtk_frame_expose (GtkWidget *widget,
439 GdkEventExpose *event)
441 GtkBin *bin = GTK_BIN (widget);
442 GtkFrame *frame = GTK_FRAME (widget);
443 GdkEventExpose child_event;
445 if (GTK_WIDGET_DRAWABLE (widget))
447 gtk_frame_paint (widget, &event->area);
449 if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child))
451 child_event = *event;
452 if (gtk_widget_intersect (bin->child, &event->area, &child_event.area))
453 gtk_widget_event (bin->child, (GdkEvent*) &child_event);
456 if (frame->label_widget && GTK_WIDGET_NO_WINDOW (frame->label_widget))
458 child_event = *event;
459 if (gtk_widget_intersect (frame->label_widget, &event->area, &child_event.area))
460 gtk_widget_event (frame->label_widget, (GdkEvent*) &child_event);
468 gtk_frame_size_request (GtkWidget *widget,
469 GtkRequisition *requisition)
471 GtkFrame *frame = GTK_FRAME (widget);
472 GtkBin *bin = GTK_BIN (widget);
473 GtkRequisition child_requisition;
475 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
477 gtk_widget_size_request (frame->label_widget, &child_requisition);
479 requisition->width = child_requisition.width;
480 requisition->height =
481 MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->klass->xthickness);
485 requisition->width = 0;
486 requisition->height = 0;
489 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
491 gtk_widget_size_request (bin->child, &child_requisition);
493 requisition->width = MAX (requisition->width, child_requisition.width);
494 requisition->height += child_requisition.height;
497 requisition->width += (GTK_CONTAINER (widget)->border_width +
498 GTK_WIDGET (widget)->style->klass->xthickness) * 2;
499 requisition->height += (GTK_CONTAINER (widget)->border_width +
500 GTK_WIDGET (widget)->style->klass->xthickness) * 2;
504 gtk_frame_size_allocate (GtkWidget *widget,
505 GtkAllocation *allocation)
507 GtkFrame *frame = GTK_FRAME (widget);
508 GtkBin *bin = GTK_BIN (widget);
510 widget->allocation = *allocation;
512 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
514 GtkAllocation new_allocation;
516 gtk_frame_compute_child_allocation (frame, &new_allocation);
518 /* If the child allocation changed, that means that the frame is drawn
519 * in a new place, so we must redraw the entire widget.
521 if (GTK_WIDGET_MAPPED (widget) &&
522 (new_allocation.x != frame->child_allocation.x ||
523 new_allocation.y != frame->child_allocation.y ||
524 new_allocation.width != frame->child_allocation.width ||
525 new_allocation.height != frame->child_allocation.height))
526 gtk_widget_queue_clear (widget);
528 gtk_widget_size_allocate (bin->child, &new_allocation);
529 frame->child_allocation = new_allocation;
532 if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
534 GtkRequisition child_requisition;
535 GtkAllocation child_allocation;
538 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
540 if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
541 xalign = frame->label_xalign;
543 xalign = 1 - frame->label_xalign;
545 child_allocation.x = frame->child_allocation.x +
546 (frame->child_allocation.width - child_requisition.width) * xalign;
547 child_allocation.width = child_requisition.width;
549 child_allocation.y = frame->child_allocation.y - child_requisition.height;
550 child_allocation.height = child_requisition.height;
552 gtk_widget_size_allocate (frame->label_widget, &child_allocation);
557 gtk_frame_map (GtkWidget *widget)
559 GtkFrame *frame = GTK_FRAME (widget);
561 if (frame->label_widget &&
562 GTK_WIDGET_VISIBLE (frame->label_widget) &&
563 !GTK_WIDGET_MAPPED (frame->label_widget))
564 gtk_widget_map (frame->label_widget);
566 if (GTK_WIDGET_CLASS (parent_class)->map)
567 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
571 gtk_frame_unmap (GtkWidget *widget)
573 GtkFrame *frame = GTK_FRAME (widget);
575 if (GTK_WIDGET_CLASS (parent_class)->unmap)
576 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
578 if (frame->label_widget && GTK_WIDGET_MAPPED (frame->label_widget))
579 gtk_widget_unmap (frame->label_widget);
583 gtk_frame_compute_child_allocation (GtkFrame *frame,
584 GtkAllocation *child_allocation)
586 g_return_if_fail (frame != NULL);
587 g_return_if_fail (GTK_IS_FRAME (frame));
588 g_return_if_fail (child_allocation != NULL);
590 GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
594 gtk_frame_real_compute_child_allocation (GtkFrame *frame,
595 GtkAllocation *child_allocation)
597 GtkWidget *widget = GTK_WIDGET (frame);
598 GtkAllocation *allocation = &widget->allocation;
599 GtkRequisition child_requisition;
602 if (frame->label_widget)
604 gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
605 top_margin = MAX (child_requisition.height, widget->style->klass->ythickness);
608 top_margin = widget->style->klass->ythickness;
610 child_allocation->x = (GTK_CONTAINER (frame)->border_width +
611 widget->style->klass->xthickness);
612 child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
614 child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
615 child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
616 (gint)GTK_CONTAINER (frame)->border_width -
617 (gint)widget->style->klass->ythickness));
619 child_allocation->x += allocation->x;
620 child_allocation->y += allocation->y;