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