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