]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
stylecontext: Do invalidation on first resize container
[~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
712           gtk_render_background (context, cr, x, y, width, height);
713
714           /* If the label is completely over or under the frame we can omit the gap */
715           if (priv->label_yalign == 0.0 || priv->label_yalign == 1.0)
716             gtk_render_frame (context, cr, x, y, width, height);
717           else
718             gtk_render_frame_gap (context, cr,
719                                   x, y, width, height,
720                                   GTK_POS_TOP, x2,
721                                   x2 + priv->label_allocation.width + 2 * LABEL_PAD);
722         }
723       else
724         {
725           gtk_render_background (context, cr, x, y, width, height);
726           gtk_render_frame (context, cr, x, y, width, height);
727         }
728     }
729   else
730     {
731       gtk_render_background (context, cr, x, y, width, height);
732     }
733
734   GTK_WIDGET_CLASS (gtk_frame_parent_class)->draw (widget, cr);
735
736   return FALSE;
737 }
738
739 static void
740 gtk_frame_size_allocate (GtkWidget     *widget,
741                          GtkAllocation *allocation)
742 {
743   GtkFrame *frame = GTK_FRAME (widget);
744   GtkFramePrivate *priv = frame->priv;
745   GtkBin *bin = GTK_BIN (widget);
746   GtkAllocation new_allocation;
747   GtkWidget *child;
748
749   gtk_widget_set_allocation (widget, allocation);
750
751   gtk_frame_compute_child_allocation (frame, &new_allocation);
752   
753   /* If the child allocation changed, that means that the frame is drawn
754    * in a new place, so we must redraw the entire widget.
755    */
756   if (gtk_widget_get_mapped (widget))
757     {
758       gdk_window_invalidate_rect (gtk_widget_get_window (widget), allocation, FALSE);
759     }
760
761   child = gtk_bin_get_child (bin);
762   if (child && gtk_widget_get_visible (child))
763     gtk_widget_size_allocate (child, &new_allocation);
764
765   priv->child_allocation = new_allocation;
766
767   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
768     {
769       GtkBorder padding;
770       gint nat_width, width, height;
771       gfloat xalign;
772
773       get_padding_and_border (frame, &padding);
774
775       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
776         xalign = priv->label_xalign;
777       else
778         xalign = 1 - priv->label_xalign;
779
780       gtk_widget_get_preferred_width (priv->label_widget, NULL, &nat_width);
781       width = new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD;
782       width = MIN (width, nat_width);
783
784       gtk_widget_get_preferred_height_for_width (priv->label_widget, width,
785                                                  &height, NULL);
786
787
788       priv->label_allocation.x = priv->child_allocation.x + LABEL_SIDE_PAD +
789         (new_allocation.width - width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
790
791       priv->label_allocation.width = width;
792
793       priv->label_allocation.y = priv->child_allocation.y - MAX (height, padding.top);
794       priv->label_allocation.height = height;
795
796       gtk_widget_size_allocate (priv->label_widget, &priv->label_allocation);
797     }
798 }
799
800 static void
801 gtk_frame_compute_child_allocation (GtkFrame      *frame,
802                                     GtkAllocation *child_allocation)
803 {
804   g_return_if_fail (GTK_IS_FRAME (frame));
805   g_return_if_fail (child_allocation != NULL);
806
807   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
808 }
809
810 static void
811 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
812                                          GtkAllocation *child_allocation)
813 {
814   GtkFramePrivate *priv = frame->priv;
815   GtkWidget *widget = GTK_WIDGET (frame);
816   GtkAllocation allocation;
817   GtkBorder padding;
818   gint top_margin;
819   guint border_width;
820
821   gtk_widget_get_allocation (widget, &allocation);
822   get_padding_and_border (frame, &padding);
823   border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
824
825   if (priv->label_widget)
826     {
827       gint nat_width, width, height;
828
829       gtk_widget_get_preferred_width (priv->label_widget, NULL, &nat_width);
830
831       width = allocation.width;
832       width -= 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
833       width -= (border_width * 2) + padding.left + padding.right;
834
835       width = MIN (width, nat_width);
836
837       gtk_widget_get_preferred_height_for_width (priv->label_widget, width,
838                                                  &height, NULL);
839
840       top_margin = MAX (height, padding.top);
841     }
842   else
843     top_margin = padding.top;
844
845   child_allocation->x = border_width + padding.left;
846   child_allocation->y = border_width + top_margin;
847   child_allocation->width = MAX (1, (gint) (allocation.width - (border_width * 2) -
848                                             padding.left - padding.right));
849   child_allocation->height = MAX (1, (gint) (allocation.height - child_allocation->y -
850                                              border_width - padding.bottom));
851
852   child_allocation->x += allocation.x;
853   child_allocation->y += allocation.y;
854 }
855
856 static void
857 gtk_frame_get_preferred_size (GtkWidget      *request,
858                               GtkOrientation  orientation,
859                               gint           *minimum_size,
860                               gint           *natural_size)
861 {
862   GtkFrame *frame = GTK_FRAME (request);
863   GtkFramePrivate *priv = frame->priv;
864   GtkBorder padding;
865   GtkWidget *widget = GTK_WIDGET (request);
866   GtkWidget *child;
867   GtkBin *bin = GTK_BIN (widget);
868   gint child_min, child_nat;
869   gint minimum, natural;
870   guint border_width;
871
872   get_padding_and_border (frame, &padding);
873   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
874
875   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
876     {
877       if (orientation == GTK_ORIENTATION_HORIZONTAL)
878         {
879           gtk_widget_get_preferred_width (priv->label_widget,
880                                           &child_min, &child_nat);
881           minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
882           natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
883         }
884       else
885         {
886           gtk_widget_get_preferred_height (priv->label_widget,
887                                            &child_min, &child_nat);
888           minimum = MAX (0, child_min - padding.top);
889           natural = MAX (0, child_nat - padding.top);
890         }
891     }
892   else
893     {
894       minimum = 0;
895       natural = 0;
896     }
897
898   child = gtk_bin_get_child (bin);
899   if (child && gtk_widget_get_visible (child))
900     {
901       if (orientation == GTK_ORIENTATION_HORIZONTAL)
902         {
903           gtk_widget_get_preferred_width (child,
904                                           &child_min, &child_nat);
905           minimum = MAX (minimum, child_min);
906           natural = MAX (natural, child_nat);
907         }
908       else
909         {
910           gtk_widget_get_preferred_height (child,
911                                            &child_min, &child_nat);
912           minimum += child_min;
913           natural += child_nat;
914         }
915     }
916
917   if (orientation == GTK_ORIENTATION_HORIZONTAL)
918     {
919       minimum += (border_width * 2) + padding.left + padding.right;
920       natural += (border_width * 2) + padding.left + padding.right;
921     }
922   else
923     {
924       minimum += (border_width * 2) + padding.top + padding.bottom;
925       natural += (border_width * 2) + padding.top + padding.bottom;
926     }
927
928  if (minimum_size)
929     *minimum_size = minimum;
930
931   if (natural_size)
932     *natural_size = natural;
933 }
934
935 static void
936 gtk_frame_get_preferred_width (GtkWidget *widget,
937                                gint      *minimum_size,
938                                gint      *natural_size)
939 {
940   gtk_frame_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
941 }
942
943 static void
944 gtk_frame_get_preferred_height (GtkWidget *widget,
945                                 gint      *minimum_size,
946                                 gint      *natural_size)
947 {
948   gtk_frame_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
949 }
950
951
952 static void
953 gtk_frame_get_preferred_height_for_width (GtkWidget *request,
954                                           gint       width,
955                                           gint      *minimum_height,
956                                           gint      *natural_height)
957 {
958   GtkWidget *widget = GTK_WIDGET (request);
959   GtkWidget *child;
960   GtkFrame *frame = GTK_FRAME (widget);
961   GtkFramePrivate *priv = frame->priv;
962   GtkBin *bin = GTK_BIN (widget);
963   GtkBorder padding;
964   gint child_min, child_nat, label_width;
965   gint minimum, natural;
966   guint border_width;
967
968   get_padding_and_border (frame, &padding);
969   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
970
971   minimum = (border_width * 2) + padding.top + padding.bottom;
972   natural = (border_width * 2) + padding.top + padding.bottom;
973
974   width -= (border_width * 2) + padding.left + padding.right;
975   label_width = width - 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
976
977   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
978     {
979       gtk_widget_get_preferred_height_for_width (priv->label_widget,
980                                                  label_width, &child_min, &child_nat);
981       minimum += child_min;
982       natural += child_nat;
983     }
984
985   child = gtk_bin_get_child (bin);
986   if (child && gtk_widget_get_visible (child))
987     {
988       gtk_widget_get_preferred_height_for_width (child,
989                                                  width, &child_min, &child_nat);
990       minimum += child_min;
991       natural += child_nat;
992     }
993
994  if (minimum_height)
995     *minimum_height = minimum;
996
997   if (natural_height)
998     *natural_height = natural;
999 }
1000
1001 static void
1002 gtk_frame_get_preferred_width_for_height (GtkWidget *widget,
1003                                           gint       height,
1004                                           gint      *minimum_width,
1005                                           gint      *natural_width)
1006 {
1007   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
1008 }
1009