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