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