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