]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
d4852fd063655c9379b6e9e788b08bc22b47c163
[~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   GtkStyleContext *context;
254
255   frame->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame,
256                                              GTK_TYPE_FRAME,
257                                              GtkFramePrivate);
258   priv = frame->priv;
259
260   priv->label_widget = NULL;
261   priv->shadow_type = GTK_SHADOW_ETCHED_IN;
262   priv->label_xalign = 0.0;
263   priv->label_yalign = 0.5;
264
265   context = gtk_widget_get_style_context (GTK_WIDGET (frame));
266   gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
267 }
268
269 static void 
270 gtk_frame_set_property (GObject         *object,
271                         guint            prop_id,
272                         const GValue    *value,
273                         GParamSpec      *pspec)
274 {
275   GtkFrame *frame = GTK_FRAME (object);
276   GtkFramePrivate *priv = frame->priv;
277
278   switch (prop_id)
279     {
280     case PROP_LABEL:
281       gtk_frame_set_label (frame, g_value_get_string (value));
282       break;
283     case PROP_LABEL_XALIGN:
284       gtk_frame_set_label_align (frame, g_value_get_float (value), 
285                                  priv->label_yalign);
286       break;
287     case PROP_LABEL_YALIGN:
288       gtk_frame_set_label_align (frame, priv->label_xalign,
289                                  g_value_get_float (value));
290       break;
291     case PROP_SHADOW_TYPE:
292       gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
293       break;
294     case PROP_LABEL_WIDGET:
295       gtk_frame_set_label_widget (frame, g_value_get_object (value));
296       break;
297     default:      
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300     }
301 }
302
303 static void 
304 gtk_frame_get_property (GObject         *object,
305                         guint            prop_id,
306                         GValue          *value,
307                         GParamSpec      *pspec)
308 {
309   GtkFrame *frame = GTK_FRAME (object);
310   GtkFramePrivate *priv = frame->priv;
311
312   switch (prop_id)
313     {
314     case PROP_LABEL:
315       g_value_set_string (value, gtk_frame_get_label (frame));
316       break;
317     case PROP_LABEL_XALIGN:
318       g_value_set_float (value, priv->label_xalign);
319       break;
320     case PROP_LABEL_YALIGN:
321       g_value_set_float (value, priv->label_yalign);
322       break;
323     case PROP_SHADOW_TYPE:
324       g_value_set_enum (value, priv->shadow_type);
325       break;
326     case PROP_LABEL_WIDGET:
327       g_value_set_object (value,
328                           priv->label_widget ?
329                           G_OBJECT (priv->label_widget) : NULL);
330       break;
331     default:
332       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333       break;
334     }
335 }
336
337 /**
338  * gtk_frame_new:
339  * @label: the text to use as the label of the frame
340  * 
341  * Creates a new #GtkFrame, with optional label @label.
342  * If @label is %NULL, the label is omitted.
343  * 
344  * Return value: a new #GtkFrame widget
345  **/
346 GtkWidget*
347 gtk_frame_new (const gchar *label)
348 {
349   return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
350 }
351
352 static void
353 gtk_frame_remove (GtkContainer *container,
354                   GtkWidget    *child)
355 {
356   GtkFrame *frame = GTK_FRAME (container);
357   GtkFramePrivate *priv = frame->priv;
358
359   if (priv->label_widget == child)
360     gtk_frame_set_label_widget (frame, NULL);
361   else
362     GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
363 }
364
365 static void
366 gtk_frame_forall (GtkContainer *container,
367                   gboolean      include_internals,
368                   GtkCallback   callback,
369                   gpointer      callback_data)
370 {
371   GtkBin *bin = GTK_BIN (container);
372   GtkFrame *frame = GTK_FRAME (container);
373   GtkFramePrivate *priv = frame->priv;
374   GtkWidget *child;
375
376   child = gtk_bin_get_child (bin);
377   if (child)
378     (* callback) (child, callback_data);
379
380   if (priv->label_widget)
381     (* callback) (priv->label_widget, callback_data);
382 }
383
384 static GtkWidgetPath *
385 gtk_frame_get_path_for_child (GtkContainer *container,
386                               GtkWidget    *child)
387 {
388   GtkFramePrivate *priv = GTK_FRAME (container)->priv;
389   GtkWidgetPath *path;
390
391   path = GTK_CONTAINER_CLASS (gtk_frame_parent_class)->get_path_for_child (container, child);
392
393   if (child == priv->label_widget)
394     gtk_widget_path_iter_add_class (path,
395                                     gtk_widget_path_length (path) - 2,
396                                     GTK_STYLE_CLASS_FRAME);
397
398   return path;
399 }
400
401 /**
402  * gtk_frame_set_label:
403  * @frame: a #GtkFrame
404  * @label: (allow-none): the text to use as the label of the frame
405  *
406  * Sets the text of the label. If @label is %NULL,
407  * the current label is removed.
408  **/
409 void
410 gtk_frame_set_label (GtkFrame *frame,
411                      const gchar *label)
412 {
413   g_return_if_fail (GTK_IS_FRAME (frame));
414
415   if (!label)
416     {
417       gtk_frame_set_label_widget (frame, NULL);
418     }
419   else
420     {
421       GtkWidget *child = gtk_label_new (label);
422       gtk_widget_show (child);
423
424       gtk_frame_set_label_widget (frame, child);
425     }
426 }
427
428 /**
429  * gtk_frame_get_label:
430  * @frame: a #GtkFrame
431  * 
432  * If the frame's label widget is a #GtkLabel, returns the
433  * text in the label widget. (The frame will have a #GtkLabel
434  * for the label widget if a non-%NULL argument was passed
435  * to gtk_frame_new().)
436  * 
437  * Return value: the text in the label, or %NULL if there
438  *               was no label widget or the lable widget was not
439  *               a #GtkLabel. This string is owned by GTK+ and
440  *               must not be modified or freed.
441  **/
442 const gchar *
443 gtk_frame_get_label (GtkFrame *frame)
444 {
445   GtkFramePrivate *priv;
446
447   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
448
449   priv = frame->priv;
450
451   if (GTK_IS_LABEL (priv->label_widget))
452     return gtk_label_get_text (GTK_LABEL (priv->label_widget));
453   else
454     return NULL;
455 }
456
457 /**
458  * gtk_frame_set_label_widget:
459  * @frame: a #GtkFrame
460  * @label_widget: the new label widget
461  * 
462  * Sets the label widget for the frame. This is the widget that
463  * will appear embedded in the top edge of the frame as a
464  * title.
465  **/
466 void
467 gtk_frame_set_label_widget (GtkFrame  *frame,
468                             GtkWidget *label_widget)
469 {
470   GtkFramePrivate *priv;
471   gboolean need_resize = FALSE;
472
473   g_return_if_fail (GTK_IS_FRAME (frame));
474   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
475   g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
476
477   priv = frame->priv;
478
479   if (priv->label_widget == label_widget)
480     return;
481
482   if (priv->label_widget)
483     {
484       need_resize = gtk_widget_get_visible (priv->label_widget);
485       gtk_widget_unparent (priv->label_widget);
486     }
487
488   priv->label_widget = label_widget;
489
490   if (label_widget)
491     {
492       priv->label_widget = label_widget;
493       gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
494       need_resize |= gtk_widget_get_visible (label_widget);
495     }
496
497   if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
498     gtk_widget_queue_resize (GTK_WIDGET (frame));
499
500   g_object_freeze_notify (G_OBJECT (frame));
501   g_object_notify (G_OBJECT (frame), "label-widget");
502   g_object_notify (G_OBJECT (frame), "label");
503   g_object_thaw_notify (G_OBJECT (frame));
504 }
505
506 /**
507  * gtk_frame_get_label_widget:
508  * @frame: a #GtkFrame
509  *
510  * Retrieves the label widget for the frame. See
511  * gtk_frame_set_label_widget().
512  *
513  * Return value: (transfer none): the label widget, or %NULL if there is none.
514  **/
515 GtkWidget *
516 gtk_frame_get_label_widget (GtkFrame *frame)
517 {
518   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
519
520   return frame->priv->label_widget;
521 }
522
523 /**
524  * gtk_frame_set_label_align:
525  * @frame: a #GtkFrame
526  * @xalign: The position of the label along the top edge
527  *   of the widget. A value of 0.0 represents left alignment;
528  *   1.0 represents right alignment.
529  * @yalign: The y alignment of the label. A value of 0.0 aligns under 
530  *   the frame; 1.0 aligns above the frame. If the values are exactly
531  *   0.0 or 1.0 the gap in the frame won't be painted because the label
532  *   will be completely above or below the frame.
533  * 
534  * Sets the alignment of the frame widget's label. The
535  * default values for a newly created frame are 0.0 and 0.5.
536  **/
537 void
538 gtk_frame_set_label_align (GtkFrame *frame,
539                            gfloat    xalign,
540                            gfloat    yalign)
541 {
542   GtkFramePrivate *priv;
543
544   g_return_if_fail (GTK_IS_FRAME (frame));
545
546   priv = frame->priv;
547
548   xalign = CLAMP (xalign, 0.0, 1.0);
549   yalign = CLAMP (yalign, 0.0, 1.0);
550
551   g_object_freeze_notify (G_OBJECT (frame));
552   if (xalign != priv->label_xalign)
553     {
554       priv->label_xalign = xalign;
555       g_object_notify (G_OBJECT (frame), "label-xalign");
556     }
557
558   if (yalign != priv->label_yalign)
559     {
560       priv->label_yalign = yalign;
561       g_object_notify (G_OBJECT (frame), "label-yalign");
562     }
563
564   g_object_thaw_notify (G_OBJECT (frame));
565   gtk_widget_queue_resize (GTK_WIDGET (frame));
566 }
567
568 /**
569  * gtk_frame_get_label_align:
570  * @frame: a #GtkFrame
571  * @xalign: (out) (allow-none): location to store X alignment of
572  *     frame's label, or %NULL
573  * @yalign: (out) (allow-none): location to store X alignment of
574  *     frame's label, or %NULL
575  * 
576  * Retrieves the X and Y alignment of the frame's label. See
577  * gtk_frame_set_label_align().
578  **/
579 void
580 gtk_frame_get_label_align (GtkFrame *frame,
581                            gfloat   *xalign,
582                            gfloat   *yalign)
583 {
584   GtkFramePrivate *priv;
585
586   g_return_if_fail (GTK_IS_FRAME (frame));
587
588   priv = frame->priv;
589
590   if (xalign)
591     *xalign = priv->label_xalign;
592   if (yalign)
593     *yalign = priv->label_yalign;
594 }
595
596 /**
597  * gtk_frame_set_shadow_type:
598  * @frame: a #GtkFrame
599  * @type: the new #GtkShadowType
600  * 
601  * Sets the shadow type for @frame.
602  **/
603 void
604 gtk_frame_set_shadow_type (GtkFrame      *frame,
605                            GtkShadowType  type)
606 {
607   GtkFramePrivate *priv;
608   GtkWidget *widget;
609
610   g_return_if_fail (GTK_IS_FRAME (frame));
611
612   priv = frame->priv;
613
614   if ((GtkShadowType) priv->shadow_type != type)
615     {
616       widget = GTK_WIDGET (frame);
617       priv->shadow_type = type;
618       g_object_notify (G_OBJECT (frame), "shadow-type");
619
620       if (gtk_widget_is_drawable (widget))
621         {
622           gtk_widget_queue_draw (widget);
623         }
624       
625       gtk_widget_queue_resize (widget);
626     }
627 }
628
629 /**
630  * gtk_frame_get_shadow_type:
631  * @frame: a #GtkFrame
632  *
633  * Retrieves the shadow type of the frame. See
634  * gtk_frame_set_shadow_type().
635  *
636  * Return value: the current shadow type of the frame.
637  **/
638 GtkShadowType
639 gtk_frame_get_shadow_type (GtkFrame *frame)
640 {
641   g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
642
643   return frame->priv->shadow_type;
644 }
645
646 static void
647 get_padding_and_border (GtkFrame *frame,
648                         GtkBorder *border)
649 {
650   GtkStyleContext *context;
651   GtkStateFlags state;
652
653   context = gtk_widget_get_style_context (GTK_WIDGET (frame));
654   state = gtk_widget_get_state_flags (GTK_WIDGET (frame));
655
656   gtk_style_context_get_padding (context, state, border);
657
658   if (frame->priv->shadow_type != GTK_SHADOW_NONE)
659     {
660       GtkBorder tmp;
661
662       gtk_style_context_get_border (context, state, &tmp);
663       border->top += tmp.top;
664       border->right += tmp.right;
665       border->bottom += tmp.bottom;
666       border->left += tmp.left;
667     }
668 }
669
670 static gboolean
671 gtk_frame_draw (GtkWidget *widget,
672                 cairo_t   *cr)
673 {
674   GtkFrame *frame;
675   GtkFramePrivate *priv;
676   GtkStyleContext *context;
677   gint x, y, width, height;
678   GtkAllocation allocation;
679   GtkBorder padding;
680
681   frame = GTK_FRAME (widget);
682   priv = frame->priv;
683
684   gtk_widget_get_allocation (widget, &allocation);
685   get_padding_and_border (frame, &padding);
686   context = gtk_widget_get_style_context (widget);
687
688   x = priv->child_allocation.x - allocation.x - padding.left;
689   y = priv->child_allocation.y - allocation.y - padding.top;
690   width = priv->child_allocation.width + padding.left + padding.right;
691   height =  priv->child_allocation.height + padding.top + padding.bottom;
692
693   if (priv->shadow_type != GTK_SHADOW_NONE)
694     {
695       if (priv->label_widget)
696         {
697           gfloat xalign;
698           gint height_extra;
699           gint x2;
700
701           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
702             xalign = priv->label_xalign;
703           else
704             xalign = 1 - priv->label_xalign;
705
706           height_extra = MAX (0, priv->label_allocation.height - padding.top)
707             - priv->label_yalign * priv->label_allocation.height;
708           y -= height_extra;
709           height += height_extra;
710
711           x2 = padding.left + (priv->child_allocation.width - priv->label_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
712           /* If the label is completely over or under the frame we can omit the gap */
713           if (priv->label_yalign == 0.0 || priv->label_yalign == 1.0)
714             gtk_render_frame (context, cr, x, y, width, height);
715           else
716             gtk_render_frame_gap (context, cr,
717                                   x, y, width, height,
718                                   GTK_POS_TOP, x2,
719                                   x2 + priv->label_allocation.width + 2 * LABEL_PAD);
720         }
721       else
722         gtk_render_frame (context, cr, x, y, width, height);
723     }
724
725   GTK_WIDGET_CLASS (gtk_frame_parent_class)->draw (widget, cr);
726
727   return FALSE;
728 }
729
730 static void
731 gtk_frame_size_allocate (GtkWidget     *widget,
732                          GtkAllocation *allocation)
733 {
734   GtkFrame *frame = GTK_FRAME (widget);
735   GtkFramePrivate *priv = frame->priv;
736   GtkBin *bin = GTK_BIN (widget);
737   GtkAllocation new_allocation;
738   GtkWidget *child;
739
740   gtk_widget_set_allocation (widget, allocation);
741
742   gtk_frame_compute_child_allocation (frame, &new_allocation);
743   
744   /* If the child allocation changed, that means that the frame is drawn
745    * in a new place, so we must redraw the entire widget.
746    */
747   if (gtk_widget_get_mapped (widget))
748     {
749       gdk_window_invalidate_rect (gtk_widget_get_window (widget), allocation, FALSE);
750     }
751
752   child = gtk_bin_get_child (bin);
753   if (child && gtk_widget_get_visible (child))
754     gtk_widget_size_allocate (child, &new_allocation);
755
756   priv->child_allocation = new_allocation;
757
758   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
759     {
760       GtkBorder padding;
761       gint nat_width, width, height;
762       gfloat xalign;
763
764       get_padding_and_border (frame, &padding);
765
766       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
767         xalign = priv->label_xalign;
768       else
769         xalign = 1 - priv->label_xalign;
770
771       gtk_widget_get_preferred_width (priv->label_widget, NULL, &nat_width);
772       width = new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD;
773       width = MIN (width, nat_width);
774
775       gtk_widget_get_preferred_height_for_width (priv->label_widget, width,
776                                                  &height, NULL);
777
778
779       priv->label_allocation.x = priv->child_allocation.x + LABEL_SIDE_PAD +
780         (new_allocation.width - width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
781
782       priv->label_allocation.width = width;
783
784       priv->label_allocation.y = priv->child_allocation.y - MAX (height, padding.top);
785       priv->label_allocation.height = height;
786
787       gtk_widget_size_allocate (priv->label_widget, &priv->label_allocation);
788     }
789 }
790
791 static void
792 gtk_frame_compute_child_allocation (GtkFrame      *frame,
793                                     GtkAllocation *child_allocation)
794 {
795   g_return_if_fail (GTK_IS_FRAME (frame));
796   g_return_if_fail (child_allocation != NULL);
797
798   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
799 }
800
801 static void
802 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
803                                          GtkAllocation *child_allocation)
804 {
805   GtkFramePrivate *priv = frame->priv;
806   GtkWidget *widget = GTK_WIDGET (frame);
807   GtkAllocation allocation;
808   GtkBorder padding;
809   gint top_margin;
810   guint border_width;
811
812   gtk_widget_get_allocation (widget, &allocation);
813   get_padding_and_border (frame, &padding);
814   border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
815
816   if (priv->label_widget)
817     {
818       gint nat_width, width, height;
819
820       gtk_widget_get_preferred_width (priv->label_widget, NULL, &nat_width);
821
822       width = allocation.width;
823       width -= 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
824       width -= (border_width * 2) + padding.left + padding.right;
825
826       width = MIN (width, nat_width);
827
828       gtk_widget_get_preferred_height_for_width (priv->label_widget, width,
829                                                  &height, NULL);
830
831       top_margin = MAX (height, padding.top);
832     }
833   else
834     top_margin = padding.top;
835
836   child_allocation->x = border_width + padding.left;
837   child_allocation->y = border_width + top_margin;
838   child_allocation->width = MAX (1, (gint) (allocation.width - (border_width * 2) -
839                                             padding.left - padding.right));
840   child_allocation->height = MAX (1, (gint) (allocation.height - child_allocation->y -
841                                              border_width - padding.bottom));
842
843   child_allocation->x += allocation.x;
844   child_allocation->y += allocation.y;
845 }
846
847 static void
848 gtk_frame_get_preferred_size (GtkWidget      *request,
849                               GtkOrientation  orientation,
850                               gint           *minimum_size,
851                               gint           *natural_size)
852 {
853   GtkFrame *frame = GTK_FRAME (request);
854   GtkFramePrivate *priv = frame->priv;
855   GtkBorder padding;
856   GtkWidget *widget = GTK_WIDGET (request);
857   GtkWidget *child;
858   GtkBin *bin = GTK_BIN (widget);
859   gint child_min, child_nat;
860   gint minimum, natural;
861   guint border_width;
862
863   get_padding_and_border (frame, &padding);
864   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
865
866   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
867     {
868       if (orientation == GTK_ORIENTATION_HORIZONTAL)
869         {
870           gtk_widget_get_preferred_width (priv->label_widget,
871                                           &child_min, &child_nat);
872           minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
873           natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
874         }
875       else
876         {
877           gtk_widget_get_preferred_height (priv->label_widget,
878                                            &child_min, &child_nat);
879           minimum = MAX (0, child_min - padding.top);
880           natural = MAX (0, child_nat - padding.top);
881         }
882     }
883   else
884     {
885       minimum = 0;
886       natural = 0;
887     }
888
889   child = gtk_bin_get_child (bin);
890   if (child && gtk_widget_get_visible (child))
891     {
892       if (orientation == GTK_ORIENTATION_HORIZONTAL)
893         {
894           gtk_widget_get_preferred_width (child,
895                                           &child_min, &child_nat);
896           minimum = MAX (minimum, child_min);
897           natural = MAX (natural, child_nat);
898         }
899       else
900         {
901           gtk_widget_get_preferred_height (child,
902                                            &child_min, &child_nat);
903           minimum += child_min;
904           natural += child_nat;
905         }
906     }
907
908   if (orientation == GTK_ORIENTATION_HORIZONTAL)
909     {
910       minimum += (border_width * 2) + padding.left + padding.right;
911       natural += (border_width * 2) + padding.left + padding.right;
912     }
913   else
914     {
915       minimum += (border_width * 2) + padding.top + padding.bottom;
916       natural += (border_width * 2) + padding.top + padding.bottom;
917     }
918
919  if (minimum_size)
920     *minimum_size = minimum;
921
922   if (natural_size)
923     *natural_size = natural;
924 }
925
926 static void
927 gtk_frame_get_preferred_width (GtkWidget *widget,
928                                gint      *minimum_size,
929                                gint      *natural_size)
930 {
931   gtk_frame_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
932 }
933
934 static void
935 gtk_frame_get_preferred_height (GtkWidget *widget,
936                                 gint      *minimum_size,
937                                 gint      *natural_size)
938 {
939   gtk_frame_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
940 }
941
942
943 static void
944 gtk_frame_get_preferred_height_for_width (GtkWidget *request,
945                                           gint       width,
946                                           gint      *minimum_height,
947                                           gint      *natural_height)
948 {
949   GtkWidget *widget = GTK_WIDGET (request);
950   GtkWidget *child;
951   GtkFrame *frame = GTK_FRAME (widget);
952   GtkFramePrivate *priv = frame->priv;
953   GtkBin *bin = GTK_BIN (widget);
954   GtkBorder padding;
955   gint child_min, child_nat, label_width;
956   gint minimum, natural;
957   guint border_width;
958
959   get_padding_and_border (frame, &padding);
960   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
961
962   minimum = (border_width * 2) + padding.top + padding.bottom;
963   natural = (border_width * 2) + padding.top + padding.bottom;
964
965   width -= (border_width * 2) + padding.left + padding.right;
966   label_width = width - 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
967
968   if (priv->label_widget && gtk_widget_get_visible (priv->label_widget))
969     {
970       gtk_widget_get_preferred_height_for_width (priv->label_widget,
971                                                  label_width, &child_min, &child_nat);
972       minimum += child_min;
973       natural += child_nat;
974     }
975
976   child = gtk_bin_get_child (bin);
977   if (child && gtk_widget_get_visible (child))
978     {
979       gtk_widget_get_preferred_height_for_width (child,
980                                                  width, &child_min, &child_nat);
981       minimum += child_min;
982       natural += child_nat;
983     }
984
985  if (minimum_height)
986     *minimum_height = minimum;
987
988   if (natural_height)
989     *natural_height = natural;
990 }
991
992 static void
993 gtk_frame_get_preferred_width_for_height (GtkWidget *widget,
994                                           gint       height,
995                                           gint      *minimum_width,
996                                           gint      *natural_width)
997 {
998   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
999 }
1000