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