]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
Use GtkBin accessors
[~andy/gtk] / gtk / gtkframe.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28 #include <string.h>
29 #include "gtkframe.h"
30 #include "gtklabel.h"
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33 #include "gtkbuildable.h"
34 #include "gtksizerequest.h"
35
36 #define LABEL_PAD 1
37 #define LABEL_SIDE_PAD 2
38
39 enum {
40   PROP_0,
41   PROP_LABEL,
42   PROP_LABEL_XALIGN,
43   PROP_LABEL_YALIGN,
44   PROP_SHADOW,
45   PROP_SHADOW_TYPE,
46   PROP_LABEL_WIDGET
47 };
48
49 static void gtk_frame_set_property (GObject      *object,
50                                     guint         param_id,
51                                     const GValue *value,
52                                     GParamSpec   *pspec);
53 static void gtk_frame_get_property (GObject     *object,
54                                     guint        param_id,
55                                     GValue      *value,
56                                     GParamSpec  *pspec);
57 static void gtk_frame_paint         (GtkWidget      *widget,
58                                      GdkRectangle   *area);
59 static gint gtk_frame_expose        (GtkWidget      *widget,
60                                      GdkEventExpose *event);
61 static void gtk_frame_size_allocate (GtkWidget      *widget,
62                                      GtkAllocation  *allocation);
63 static void gtk_frame_remove        (GtkContainer   *container,
64                                      GtkWidget      *child);
65 static void gtk_frame_forall        (GtkContainer   *container,
66                                      gboolean        include_internals,
67                                      GtkCallback     callback,
68                                      gpointer        callback_data);
69
70 static void gtk_frame_compute_child_allocation      (GtkFrame      *frame,
71                                                      GtkAllocation *child_allocation);
72 static void gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
73                                                      GtkAllocation *child_allocation);
74
75 /* GtkBuildable */
76 static void gtk_frame_buildable_init                (GtkBuildableIface *iface);
77 static void gtk_frame_buildable_add_child           (GtkBuildable *buildable,
78                                                      GtkBuilder   *builder,
79                                                      GObject      *child,
80                                                      const gchar  *type);
81
82 static void gtk_frame_size_request_init             (GtkSizeRequestIface *iface);
83 static void gtk_frame_get_width                     (GtkSizeRequest      *widget,
84                                                      gint                *minimum_size,
85                                                      gint                *natural_size);
86 static void gtk_frame_get_height                    (GtkSizeRequest      *widget,
87                                                      gint                *minimum_size,
88                                                      gint                *natural_size);
89
90 G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
91                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
92                                                 gtk_frame_buildable_init)
93                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
94                                                 gtk_frame_size_request_init))
95
96 static void
97 gtk_frame_class_init (GtkFrameClass *class)
98 {
99   GObjectClass *gobject_class;
100   GtkWidgetClass *widget_class;
101   GtkContainerClass *container_class;
102
103   gobject_class = (GObjectClass*) class;
104   widget_class = GTK_WIDGET_CLASS (class);
105   container_class = GTK_CONTAINER_CLASS (class);
106
107   gobject_class->set_property = gtk_frame_set_property;
108   gobject_class->get_property = gtk_frame_get_property;
109
110   g_object_class_install_property (gobject_class,
111                                    PROP_LABEL,
112                                    g_param_spec_string ("label",
113                                                         P_("Label"),
114                                                         P_("Text of the frame's label"),
115                                                         NULL,
116                                                         GTK_PARAM_READABLE |
117                                                         GTK_PARAM_WRITABLE));
118   g_object_class_install_property (gobject_class,
119                                    PROP_LABEL_XALIGN,
120                                    g_param_spec_float ("label-xalign",
121                                                        P_("Label xalign"),
122                                                        P_("The horizontal alignment of the label"),
123                                                        0.0,
124                                                        1.0,
125                                                        0.0,
126                                                        GTK_PARAM_READWRITE));
127   g_object_class_install_property (gobject_class,
128                                    PROP_LABEL_YALIGN,
129                                    g_param_spec_float ("label-yalign",
130                                                        P_("Label yalign"),
131                                                        P_("The vertical alignment of the label"),
132                                                        0.0,
133                                                        1.0,
134                                                        0.5,
135                                                        GTK_PARAM_READWRITE));
136   g_object_class_install_property (gobject_class,
137                                    PROP_SHADOW,
138                                    g_param_spec_enum ("shadow", NULL,
139                                                       P_("Deprecated property, use shadow_type instead"),
140                                                       GTK_TYPE_SHADOW_TYPE,
141                                                       GTK_SHADOW_ETCHED_IN,
142                                                       GTK_PARAM_READWRITE));
143   g_object_class_install_property (gobject_class,
144                                    PROP_SHADOW_TYPE,
145                                    g_param_spec_enum ("shadow-type",
146                                                       P_("Frame shadow"),
147                                                       P_("Appearance of the frame border"),
148                                                       GTK_TYPE_SHADOW_TYPE,
149                                                       GTK_SHADOW_ETCHED_IN,
150                                                       GTK_PARAM_READWRITE));
151
152   g_object_class_install_property (gobject_class,
153                                    PROP_LABEL_WIDGET,
154                                    g_param_spec_object ("label-widget",
155                                                         P_("Label widget"),
156                                                         P_("A widget to display in place of the usual frame label"),
157                                                         GTK_TYPE_WIDGET,
158                                                         GTK_PARAM_READWRITE));
159   
160   widget_class->expose_event = gtk_frame_expose;
161   widget_class->size_allocate = gtk_frame_size_allocate;
162
163   container_class->remove = gtk_frame_remove;
164   container_class->forall = gtk_frame_forall;
165
166   class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
167 }
168
169 static void
170 gtk_frame_buildable_init (GtkBuildableIface *iface)
171 {
172   iface->add_child = gtk_frame_buildable_add_child;
173 }
174
175 static void
176 gtk_frame_buildable_add_child (GtkBuildable *buildable,
177                                GtkBuilder   *builder,
178                                GObject      *child,
179                                const gchar  *type)
180 {
181   if (type && strcmp (type, "label") == 0)
182     gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
183   else if (!type)
184     gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
185   else
186     GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
187 }
188
189 static void
190 gtk_frame_init (GtkFrame *frame)
191 {
192   frame->label_widget = NULL;
193   frame->shadow_type = GTK_SHADOW_ETCHED_IN;
194   frame->label_xalign = 0.0;
195   frame->label_yalign = 0.5;
196 }
197
198 static void 
199 gtk_frame_set_property (GObject         *object,
200                         guint            prop_id,
201                         const GValue    *value,
202                         GParamSpec      *pspec)
203 {
204   GtkFrame *frame;
205
206   frame = GTK_FRAME (object);
207
208   switch (prop_id)
209     {
210     case PROP_LABEL:
211       gtk_frame_set_label (frame, g_value_get_string (value));
212       break;
213     case PROP_LABEL_XALIGN:
214       gtk_frame_set_label_align (frame, g_value_get_float (value), 
215                                  frame->label_yalign);
216       break;
217     case PROP_LABEL_YALIGN:
218       gtk_frame_set_label_align (frame, frame->label_xalign, 
219                                  g_value_get_float (value));
220       break;
221     case PROP_SHADOW:
222     case PROP_SHADOW_TYPE:
223       gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
224       break;
225     case PROP_LABEL_WIDGET:
226       gtk_frame_set_label_widget (frame, g_value_get_object (value));
227       break;
228     default:      
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231     }
232 }
233
234 static void 
235 gtk_frame_get_property (GObject         *object,
236                         guint            prop_id,
237                         GValue          *value,
238                         GParamSpec      *pspec)
239 {
240   GtkFrame *frame;
241
242   frame = GTK_FRAME (object);
243
244   switch (prop_id)
245     {
246     case PROP_LABEL:
247       g_value_set_string (value, gtk_frame_get_label (frame));
248       break;
249     case PROP_LABEL_XALIGN:
250       g_value_set_float (value, frame->label_xalign);
251       break;
252     case PROP_LABEL_YALIGN:
253       g_value_set_float (value, frame->label_yalign);
254       break;
255     case PROP_SHADOW:
256     case PROP_SHADOW_TYPE:
257       g_value_set_enum (value, frame->shadow_type);
258       break;
259     case PROP_LABEL_WIDGET:
260       g_value_set_object (value,
261                           frame->label_widget ?
262                           G_OBJECT (frame->label_widget) : NULL);
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267     }
268 }
269
270 /**
271  * gtk_frame_new:
272  * @label: the text to use as the label of the frame
273  * 
274  * Creates a new #GtkFrame, with optional label @label.
275  * If @label is %NULL, the label is omitted.
276  * 
277  * Return value: a new #GtkFrame widget
278  **/
279 GtkWidget*
280 gtk_frame_new (const gchar *label)
281 {
282   return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
283 }
284
285 static void
286 gtk_frame_remove (GtkContainer *container,
287                   GtkWidget    *child)
288 {
289   GtkFrame *frame = GTK_FRAME (container);
290
291   if (frame->label_widget == child)
292     gtk_frame_set_label_widget (frame, NULL);
293   else
294     GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
295 }
296
297 static void
298 gtk_frame_forall (GtkContainer *container,
299                   gboolean      include_internals,
300                   GtkCallback   callback,
301                   gpointer      callback_data)
302 {
303   GtkBin *bin = GTK_BIN (container);
304   GtkFrame *frame = GTK_FRAME (container);
305   GtkWidget *child;
306
307   child = gtk_bin_get_child (bin);
308   if (child)
309     (* callback) (child, callback_data);
310
311   if (frame->label_widget)
312     (* callback) (frame->label_widget, callback_data);
313 }
314
315 /**
316  * gtk_frame_set_label:
317  * @frame: a #GtkFrame
318  * @label: (allow-none): the text to use as the label of the frame
319  *
320  * Sets the text of the label. If @label is %NULL,
321  * the current label is removed.
322  **/
323 void
324 gtk_frame_set_label (GtkFrame *frame,
325                      const gchar *label)
326 {
327   g_return_if_fail (GTK_IS_FRAME (frame));
328
329   if (!label)
330     {
331       gtk_frame_set_label_widget (frame, NULL);
332     }
333   else
334     {
335       GtkWidget *child = gtk_label_new (label);
336       gtk_widget_show (child);
337
338       gtk_frame_set_label_widget (frame, child);
339     }
340 }
341
342 /**
343  * gtk_frame_get_label:
344  * @frame: a #GtkFrame
345  * 
346  * If the frame's label widget is a #GtkLabel, returns the
347  * text in the label widget. (The frame will have a #GtkLabel
348  * for the label widget if a non-%NULL argument was passed
349  * to gtk_frame_new().)
350  * 
351  * Return value: the text in the label, or %NULL if there
352  *               was no label widget or the lable widget was not
353  *               a #GtkLabel. This string is owned by GTK+ and
354  *               must not be modified or freed.
355  **/
356 G_CONST_RETURN gchar *
357 gtk_frame_get_label (GtkFrame *frame)
358 {
359   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
360
361   if (GTK_IS_LABEL (frame->label_widget))
362     return gtk_label_get_text (GTK_LABEL (frame->label_widget));
363   else
364     return NULL;
365 }
366
367 /**
368  * gtk_frame_set_label_widget:
369  * @frame: a #GtkFrame
370  * @label_widget: the new label widget
371  * 
372  * Sets the label widget for the frame. This is the widget that
373  * will appear embedded in the top edge of the frame as a
374  * title.
375  **/
376 void
377 gtk_frame_set_label_widget (GtkFrame  *frame,
378                             GtkWidget *label_widget)
379 {
380   gboolean need_resize = FALSE;
381   
382   g_return_if_fail (GTK_IS_FRAME (frame));
383   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
384   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
385   
386   if (frame->label_widget == label_widget)
387     return;
388   
389   if (frame->label_widget)
390     {
391       need_resize = gtk_widget_get_visible (frame->label_widget);
392       gtk_widget_unparent (frame->label_widget);
393     }
394
395   frame->label_widget = label_widget;
396     
397   if (label_widget)
398     {
399       frame->label_widget = label_widget;
400       gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
401       need_resize |= gtk_widget_get_visible (label_widget);
402     }
403   
404   if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
405     gtk_widget_queue_resize (GTK_WIDGET (frame));
406
407   g_object_freeze_notify (G_OBJECT (frame));
408   g_object_notify (G_OBJECT (frame), "label-widget");
409   g_object_notify (G_OBJECT (frame), "label");
410   g_object_thaw_notify (G_OBJECT (frame));
411 }
412
413 /**
414  * gtk_frame_get_label_widget:
415  * @frame: a #GtkFrame
416  *
417  * Retrieves the label widget for the frame. See
418  * gtk_frame_set_label_widget().
419  *
420  * Return value: the label widget, or %NULL if there is none.
421  **/
422 GtkWidget *
423 gtk_frame_get_label_widget (GtkFrame *frame)
424 {
425   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
426
427   return frame->label_widget;
428 }
429
430 /**
431  * gtk_frame_set_label_align:
432  * @frame: a #GtkFrame
433  * @xalign: The position of the label along the top edge
434  *   of the widget. A value of 0.0 represents left alignment;
435  *   1.0 represents right alignment.
436  * @yalign: The y alignment of the label. A value of 0.0 aligns under 
437  *   the frame; 1.0 aligns above the frame. If the values are exactly
438  *   0.0 or 1.0 the gap in the frame won't be painted because the label
439  *   will be completely above or below the frame.
440  * 
441  * Sets the alignment of the frame widget's label. The
442  * default values for a newly created frame are 0.0 and 0.5.
443  **/
444 void
445 gtk_frame_set_label_align (GtkFrame *frame,
446                            gfloat    xalign,
447                            gfloat    yalign)
448 {
449   g_return_if_fail (GTK_IS_FRAME (frame));
450
451   xalign = CLAMP (xalign, 0.0, 1.0);
452   yalign = CLAMP (yalign, 0.0, 1.0);
453
454   g_object_freeze_notify (G_OBJECT (frame));
455   if (xalign != frame->label_xalign)
456     {
457       frame->label_xalign = xalign;
458       g_object_notify (G_OBJECT (frame), "label-xalign");
459     }
460
461   if (yalign != frame->label_yalign)
462     {
463       frame->label_yalign = yalign;
464       g_object_notify (G_OBJECT (frame), "label-yalign");
465     }
466
467   g_object_thaw_notify (G_OBJECT (frame));
468   gtk_widget_queue_resize (GTK_WIDGET (frame));
469 }
470
471 /**
472  * gtk_frame_get_label_align:
473  * @frame: a #GtkFrame
474  * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
475  * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
476  * 
477  * Retrieves the X and Y alignment of the frame's label. See
478  * gtk_frame_set_label_align().
479  **/
480 void
481 gtk_frame_get_label_align (GtkFrame *frame,
482                            gfloat   *xalign,
483                            gfloat   *yalign)
484 {
485   g_return_if_fail (GTK_IS_FRAME (frame));
486
487   if (xalign)
488     *xalign = frame->label_xalign;
489   if (yalign)
490     *yalign = frame->label_yalign;
491 }
492
493 /**
494  * gtk_frame_set_shadow_type:
495  * @frame: a #GtkFrame
496  * @type: the new #GtkShadowType
497  * 
498  * Sets the shadow type for @frame.
499  **/
500 void
501 gtk_frame_set_shadow_type (GtkFrame      *frame,
502                            GtkShadowType  type)
503 {
504   GtkWidget *widget;
505
506   g_return_if_fail (GTK_IS_FRAME (frame));
507
508   if ((GtkShadowType) frame->shadow_type != type)
509     {
510       widget = GTK_WIDGET (frame);
511       frame->shadow_type = type;
512       g_object_notify (G_OBJECT (frame), "shadow-type");
513
514       if (gtk_widget_is_drawable (widget))
515         {
516           gtk_widget_queue_draw (widget);
517         }
518       
519       gtk_widget_queue_resize (widget);
520     }
521 }
522
523 /**
524  * gtk_frame_get_shadow_type:
525  * @frame: a #GtkFrame
526  *
527  * Retrieves the shadow type of the frame. See
528  * gtk_frame_set_shadow_type().
529  *
530  * Return value: the current shadow type of the frame.
531  **/
532 GtkShadowType
533 gtk_frame_get_shadow_type (GtkFrame *frame)
534 {
535   g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
536
537   return frame->shadow_type;
538 }
539
540 static void
541 gtk_frame_paint (GtkWidget    *widget,
542                  GdkRectangle *area)
543 {
544   GtkFrame *frame;
545   gint x, y, width, height;
546
547   if (gtk_widget_is_drawable (widget))
548     {
549       frame = GTK_FRAME (widget);
550
551       x = frame->child_allocation.x - widget->style->xthickness;
552       y = frame->child_allocation.y - widget->style->ythickness;
553       width = frame->child_allocation.width + 2 * widget->style->xthickness;
554       height =  frame->child_allocation.height + 2 * widget->style->ythickness;
555
556       if (frame->label_widget)
557         {
558           GtkRequisition child_requisition;
559           gfloat xalign;
560           gint height_extra;
561           gint x2;
562
563           gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
564
565           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
566             xalign = frame->label_xalign;
567           else
568             xalign = 1 - frame->label_xalign;
569
570           height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
571             - frame->label_yalign * child_requisition.height;
572           y -= height_extra;
573           height += height_extra;
574           
575           x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
576           
577           /* If the label is completely over or under the frame we can omit the gap */
578           if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
579             gtk_paint_shadow (widget->style, widget->window,
580                               widget->state, frame->shadow_type,
581                               area, widget, "frame",
582                               x, y, width, height);
583           else
584             gtk_paint_shadow_gap (widget->style, widget->window,
585                                   widget->state, frame->shadow_type,
586                                   area, widget, "frame",
587                                   x, y, width, height,
588                                   GTK_POS_TOP,
589                                   x2, child_requisition.width + 2 * LABEL_PAD);
590         }
591        else
592          gtk_paint_shadow (widget->style, widget->window,
593                            widget->state, frame->shadow_type,
594                            area, widget, "frame",
595                            x, y, width, height);
596     }
597 }
598
599 static gboolean
600 gtk_frame_expose (GtkWidget      *widget,
601                   GdkEventExpose *event)
602 {
603   if (gtk_widget_is_drawable (widget))
604     {
605       gtk_frame_paint (widget, &event->area);
606
607       GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
608     }
609
610   return FALSE;
611 }
612
613 static void
614 gtk_frame_size_allocate (GtkWidget     *widget,
615                          GtkAllocation *allocation)
616 {
617   GtkFrame *frame = GTK_FRAME (widget);
618   GtkBin *bin = GTK_BIN (widget);
619   GtkAllocation new_allocation;
620   GtkWidget *child;
621
622   widget->allocation = *allocation;
623
624   gtk_frame_compute_child_allocation (frame, &new_allocation);
625   
626   /* If the child allocation changed, that means that the frame is drawn
627    * in a new place, so we must redraw the entire widget.
628    */
629   if (gtk_widget_get_mapped (widget)
630 #if 0
631       &&
632       (new_allocation.x != frame->child_allocation.x ||
633        new_allocation.y != frame->child_allocation.y ||
634        new_allocation.width != frame->child_allocation.width ||
635        new_allocation.height != frame->child_allocation.height)
636 #endif
637      )
638     gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
639
640   child = gtk_bin_get_child (bin);
641   if (child && gtk_widget_get_visible (child))
642     gtk_widget_size_allocate (child, &new_allocation);
643   
644   frame->child_allocation = new_allocation;
645   
646   if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
647     {
648       GtkRequisition child_requisition;
649       GtkAllocation child_allocation;
650       gfloat xalign;
651
652       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
653
654       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
655         xalign = frame->label_xalign;
656       else
657         xalign = 1 - frame->label_xalign;
658       
659       child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
660         (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
661       child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
662
663       child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
664       child_allocation.height = child_requisition.height;
665
666       gtk_widget_size_allocate (frame->label_widget, &child_allocation);
667     }
668 }
669
670 static void
671 gtk_frame_compute_child_allocation (GtkFrame      *frame,
672                                     GtkAllocation *child_allocation)
673 {
674   g_return_if_fail (GTK_IS_FRAME (frame));
675   g_return_if_fail (child_allocation != NULL);
676
677   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
678 }
679
680 static void
681 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
682                                          GtkAllocation *child_allocation)
683 {
684   GtkWidget *widget = GTK_WIDGET (frame);
685   GtkAllocation *allocation = &widget->allocation;
686   GtkRequisition child_requisition;
687   gint top_margin;
688   guint border_width;
689
690   if (frame->label_widget)
691     {
692       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
693       top_margin = MAX (child_requisition.height, widget->style->ythickness);
694     }
695   else
696     top_margin = widget->style->ythickness;
697
698   border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
699   
700   child_allocation->x = border_width + widget->style->xthickness;
701   child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
702   
703   child_allocation->y = border_width + top_margin;
704   child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
705                                       border_width -
706                                       (gint)widget->style->ythickness));
707   
708   child_allocation->x += allocation->x;
709   child_allocation->y += allocation->y;
710 }
711
712 static void
713 gtk_frame_get_size (GtkSizeRequest *request,
714                     GtkOrientation  orientation,
715                     gint           *minimum_size,
716                     gint           *natural_size)
717 {
718   GtkWidget *widget = GTK_WIDGET (request);
719   GtkWidget *child;
720   GtkFrame *frame = GTK_FRAME (widget);
721   GtkBin *bin = GTK_BIN (widget);
722   gint child_min, child_nat;
723   gint minimum, natural;
724   guint border_width;
725
726   if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
727     {
728       if (orientation == GTK_ORIENTATION_HORIZONTAL)
729         {
730           gtk_size_request_get_width (GTK_SIZE_REQUEST (frame->label_widget),
731                                       &child_min, &child_nat);
732           minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
733           natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
734         }
735       else
736         {
737           gtk_size_request_get_height (GTK_SIZE_REQUEST (frame->label_widget),
738                                        &child_min, &child_nat);
739           minimum = MAX (0, child_min - widget->style->ythickness);
740           natural = MAX (0, child_nat - widget->style->ythickness);
741         }
742     }
743   else
744     {
745       minimum = 0;
746       natural = 0;
747     }
748
749   child = gtk_bin_get_child (bin);
750   if (child && gtk_widget_get_visible (child))
751     {
752       if (orientation == GTK_ORIENTATION_HORIZONTAL)
753         {
754           gtk_size_request_get_width (GTK_SIZE_REQUEST (child),
755                                       &child_min, &child_nat);
756           minimum = MAX (minimum, child_min);
757           natural = MAX (natural, child_nat);
758         }
759       else
760         {
761           gtk_size_request_get_height (GTK_SIZE_REQUEST (child),
762                                        &child_min, &child_nat);
763           minimum += child_min;
764           natural += child_nat;
765         }
766     }
767
768   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
769
770   if (orientation == GTK_ORIENTATION_HORIZONTAL)
771     {
772       minimum += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
773       natural += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
774     }
775   else
776     {
777       minimum += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
778       natural += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
779     }
780
781  if (minimum_size)
782     *minimum_size = minimum;
783
784   if (natural_size)
785     *natural_size = natural;
786 }
787
788 static void
789 gtk_frame_get_width (GtkSizeRequest *widget,
790                      gint           *minimum_size,
791                      gint           *natural_size)
792 {
793   gtk_frame_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
794 }
795
796 static void
797 gtk_frame_get_height (GtkSizeRequest *widget,
798                       gint           *minimum_size,
799                       gint           *natural_size)
800 {
801   gtk_frame_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
802 }
803
804 static void
805 gtk_frame_size_request_init (GtkSizeRequestIface *iface)
806 {
807   iface->get_width = gtk_frame_get_width;
808   iface->get_height = gtk_frame_get_height;
809 }