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