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