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