]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
Use accessor functions to acces GtkContainer
[~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 "gtkintl.h"
33 #include "gtkbuildable.h"
34 #include "gtksizerequest.h"
35
36 #define LABEL_PAD 1
37 #define LABEL_SIDE_PAD 2
38
39 enum {
40   PROP_0,
41   PROP_LABEL,
42   PROP_LABEL_XALIGN,
43   PROP_LABEL_YALIGN,
44   PROP_SHADOW,
45   PROP_SHADOW_TYPE,
46   PROP_LABEL_WIDGET
47 };
48
49 static void gtk_frame_set_property (GObject      *object,
50                                     guint         param_id,
51                                     const GValue *value,
52                                     GParamSpec   *pspec);
53 static void gtk_frame_get_property (GObject     *object,
54                                     guint        param_id,
55                                     GValue      *value,
56                                     GParamSpec  *pspec);
57 static void gtk_frame_paint         (GtkWidget      *widget,
58                                      GdkRectangle   *area);
59 static gint gtk_frame_expose        (GtkWidget      *widget,
60                                      GdkEventExpose *event);
61 static void gtk_frame_size_allocate (GtkWidget      *widget,
62                                      GtkAllocation  *allocation);
63 static void gtk_frame_remove        (GtkContainer   *container,
64                                      GtkWidget      *child);
65 static void gtk_frame_forall        (GtkContainer   *container,
66                                      gboolean        include_internals,
67                                      GtkCallback     callback,
68                                      gpointer        callback_data);
69
70 static void gtk_frame_compute_child_allocation      (GtkFrame      *frame,
71                                                      GtkAllocation *child_allocation);
72 static void gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
73                                                      GtkAllocation *child_allocation);
74
75 /* GtkBuildable */
76 static void gtk_frame_buildable_init                (GtkBuildableIface *iface);
77 static void gtk_frame_buildable_add_child           (GtkBuildable *buildable,
78                                                      GtkBuilder   *builder,
79                                                      GObject      *child,
80                                                      const gchar  *type);
81
82 static void gtk_frame_size_request_init             (GtkSizeRequestIface *iface);
83 static void gtk_frame_get_width                     (GtkSizeRequest      *widget,
84                                                      gint                *minimum_size,
85                                                      gint                *natural_size);
86 static void gtk_frame_get_height                    (GtkSizeRequest      *widget,
87                                                      gint                *minimum_size,
88                                                      gint                *natural_size);
89
90 G_DEFINE_TYPE_WITH_CODE (GtkFrame, gtk_frame, GTK_TYPE_BIN,
91                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
92                                                 gtk_frame_buildable_init)
93                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
94                                                 gtk_frame_size_request_init))
95
96 static void
97 gtk_frame_class_init (GtkFrameClass *class)
98 {
99   GObjectClass *gobject_class;
100   GtkWidgetClass *widget_class;
101   GtkContainerClass *container_class;
102
103   gobject_class = (GObjectClass*) class;
104   widget_class = GTK_WIDGET_CLASS (class);
105   container_class = GTK_CONTAINER_CLASS (class);
106
107   gobject_class->set_property = gtk_frame_set_property;
108   gobject_class->get_property = gtk_frame_get_property;
109
110   g_object_class_install_property (gobject_class,
111                                    PROP_LABEL,
112                                    g_param_spec_string ("label",
113                                                         P_("Label"),
114                                                         P_("Text of the frame's label"),
115                                                         NULL,
116                                                         GTK_PARAM_READABLE |
117                                                         GTK_PARAM_WRITABLE));
118   g_object_class_install_property (gobject_class,
119                                    PROP_LABEL_XALIGN,
120                                    g_param_spec_float ("label-xalign",
121                                                        P_("Label xalign"),
122                                                        P_("The horizontal alignment of the label"),
123                                                        0.0,
124                                                        1.0,
125                                                        0.0,
126                                                        GTK_PARAM_READWRITE));
127   g_object_class_install_property (gobject_class,
128                                    PROP_LABEL_YALIGN,
129                                    g_param_spec_float ("label-yalign",
130                                                        P_("Label yalign"),
131                                                        P_("The vertical alignment of the label"),
132                                                        0.0,
133                                                        1.0,
134                                                        0.5,
135                                                        GTK_PARAM_READWRITE));
136   g_object_class_install_property (gobject_class,
137                                    PROP_SHADOW,
138                                    g_param_spec_enum ("shadow", NULL,
139                                                       P_("Deprecated property, use shadow_type instead"),
140                                                       GTK_TYPE_SHADOW_TYPE,
141                                                       GTK_SHADOW_ETCHED_IN,
142                                                       GTK_PARAM_READWRITE));
143   g_object_class_install_property (gobject_class,
144                                    PROP_SHADOW_TYPE,
145                                    g_param_spec_enum ("shadow-type",
146                                                       P_("Frame shadow"),
147                                                       P_("Appearance of the frame border"),
148                                                       GTK_TYPE_SHADOW_TYPE,
149                                                       GTK_SHADOW_ETCHED_IN,
150                                                       GTK_PARAM_READWRITE));
151
152   g_object_class_install_property (gobject_class,
153                                    PROP_LABEL_WIDGET,
154                                    g_param_spec_object ("label-widget",
155                                                         P_("Label widget"),
156                                                         P_("A widget to display in place of the usual frame label"),
157                                                         GTK_TYPE_WIDGET,
158                                                         GTK_PARAM_READWRITE));
159   
160   widget_class->expose_event = gtk_frame_expose;
161   widget_class->size_allocate = gtk_frame_size_allocate;
162
163   container_class->remove = gtk_frame_remove;
164   container_class->forall = gtk_frame_forall;
165
166   class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
167 }
168
169 static void
170 gtk_frame_buildable_init (GtkBuildableIface *iface)
171 {
172   iface->add_child = gtk_frame_buildable_add_child;
173 }
174
175 static void
176 gtk_frame_buildable_add_child (GtkBuildable *buildable,
177                                GtkBuilder   *builder,
178                                GObject      *child,
179                                const gchar  *type)
180 {
181   if (type && strcmp (type, "label") == 0)
182     gtk_frame_set_label_widget (GTK_FRAME (buildable), GTK_WIDGET (child));
183   else if (!type)
184     gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
185   else
186     GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_FRAME (buildable), type);
187 }
188
189 static void
190 gtk_frame_init (GtkFrame *frame)
191 {
192   frame->label_widget = NULL;
193   frame->shadow_type = GTK_SHADOW_ETCHED_IN;
194   frame->label_xalign = 0.0;
195   frame->label_yalign = 0.5;
196 }
197
198 static void 
199 gtk_frame_set_property (GObject         *object,
200                         guint            prop_id,
201                         const GValue    *value,
202                         GParamSpec      *pspec)
203 {
204   GtkFrame *frame;
205
206   frame = GTK_FRAME (object);
207
208   switch (prop_id)
209     {
210     case PROP_LABEL:
211       gtk_frame_set_label (frame, g_value_get_string (value));
212       break;
213     case PROP_LABEL_XALIGN:
214       gtk_frame_set_label_align (frame, g_value_get_float (value), 
215                                  frame->label_yalign);
216       break;
217     case PROP_LABEL_YALIGN:
218       gtk_frame_set_label_align (frame, frame->label_xalign, 
219                                  g_value_get_float (value));
220       break;
221     case PROP_SHADOW:
222     case PROP_SHADOW_TYPE:
223       gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
224       break;
225     case PROP_LABEL_WIDGET:
226       gtk_frame_set_label_widget (frame, g_value_get_object (value));
227       break;
228     default:      
229       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230       break;
231     }
232 }
233
234 static void 
235 gtk_frame_get_property (GObject         *object,
236                         guint            prop_id,
237                         GValue          *value,
238                         GParamSpec      *pspec)
239 {
240   GtkFrame *frame;
241
242   frame = GTK_FRAME (object);
243
244   switch (prop_id)
245     {
246     case PROP_LABEL:
247       g_value_set_string (value, gtk_frame_get_label (frame));
248       break;
249     case PROP_LABEL_XALIGN:
250       g_value_set_float (value, frame->label_xalign);
251       break;
252     case PROP_LABEL_YALIGN:
253       g_value_set_float (value, frame->label_yalign);
254       break;
255     case PROP_SHADOW:
256     case PROP_SHADOW_TYPE:
257       g_value_set_enum (value, frame->shadow_type);
258       break;
259     case PROP_LABEL_WIDGET:
260       g_value_set_object (value,
261                           frame->label_widget ?
262                           G_OBJECT (frame->label_widget) : NULL);
263       break;
264     default:
265       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266       break;
267     }
268 }
269
270 /**
271  * gtk_frame_new:
272  * @label: the text to use as the label of the frame
273  * 
274  * Creates a new #GtkFrame, with optional label @label.
275  * If @label is %NULL, the label is omitted.
276  * 
277  * Return value: a new #GtkFrame widget
278  **/
279 GtkWidget*
280 gtk_frame_new (const gchar *label)
281 {
282   return g_object_new (GTK_TYPE_FRAME, "label", label, NULL);
283 }
284
285 static void
286 gtk_frame_remove (GtkContainer *container,
287                   GtkWidget    *child)
288 {
289   GtkFrame *frame = GTK_FRAME (container);
290
291   if (frame->label_widget == child)
292     gtk_frame_set_label_widget (frame, NULL);
293   else
294     GTK_CONTAINER_CLASS (gtk_frame_parent_class)->remove (container, child);
295 }
296
297 static void
298 gtk_frame_forall (GtkContainer *container,
299                   gboolean      include_internals,
300                   GtkCallback   callback,
301                   gpointer      callback_data)
302 {
303   GtkBin *bin = GTK_BIN (container);
304   GtkFrame *frame = GTK_FRAME (container);
305
306   if (bin->child)
307     (* callback) (bin->child, callback_data);
308
309   if (frame->label_widget)
310     (* callback) (frame->label_widget, callback_data);
311 }
312
313 /**
314  * gtk_frame_set_label:
315  * @frame: a #GtkFrame
316  * @label: (allow-none): the text to use as the label of the frame
317  *
318  * Sets the text of the label. If @label is %NULL,
319  * the current label is removed.
320  **/
321 void
322 gtk_frame_set_label (GtkFrame *frame,
323                      const gchar *label)
324 {
325   g_return_if_fail (GTK_IS_FRAME (frame));
326
327   if (!label)
328     {
329       gtk_frame_set_label_widget (frame, NULL);
330     }
331   else
332     {
333       GtkWidget *child = gtk_label_new (label);
334       gtk_widget_show (child);
335
336       gtk_frame_set_label_widget (frame, child);
337     }
338 }
339
340 /**
341  * gtk_frame_get_label:
342  * @frame: a #GtkFrame
343  * 
344  * If the frame's label widget is a #GtkLabel, returns the
345  * text in the label widget. (The frame will have a #GtkLabel
346  * for the label widget if a non-%NULL argument was passed
347  * to gtk_frame_new().)
348  * 
349  * Return value: the text in the label, or %NULL if there
350  *               was no label widget or the lable widget was not
351  *               a #GtkLabel. This string is owned by GTK+ and
352  *               must not be modified or freed.
353  **/
354 G_CONST_RETURN gchar *
355 gtk_frame_get_label (GtkFrame *frame)
356 {
357   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
358
359   if (GTK_IS_LABEL (frame->label_widget))
360     return gtk_label_get_text (GTK_LABEL (frame->label_widget));
361   else
362     return NULL;
363 }
364
365 /**
366  * gtk_frame_set_label_widget:
367  * @frame: a #GtkFrame
368  * @label_widget: the new label widget
369  * 
370  * Sets the label widget for the frame. This is the widget that
371  * will appear embedded in the top edge of the frame as a
372  * title.
373  **/
374 void
375 gtk_frame_set_label_widget (GtkFrame  *frame,
376                             GtkWidget *label_widget)
377 {
378   gboolean need_resize = FALSE;
379   
380   g_return_if_fail (GTK_IS_FRAME (frame));
381   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
382   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
383   
384   if (frame->label_widget == label_widget)
385     return;
386   
387   if (frame->label_widget)
388     {
389       need_resize = gtk_widget_get_visible (frame->label_widget);
390       gtk_widget_unparent (frame->label_widget);
391     }
392
393   frame->label_widget = label_widget;
394     
395   if (label_widget)
396     {
397       frame->label_widget = label_widget;
398       gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
399       need_resize |= gtk_widget_get_visible (label_widget);
400     }
401   
402   if (gtk_widget_get_visible (GTK_WIDGET (frame)) && need_resize)
403     gtk_widget_queue_resize (GTK_WIDGET (frame));
404
405   g_object_freeze_notify (G_OBJECT (frame));
406   g_object_notify (G_OBJECT (frame), "label-widget");
407   g_object_notify (G_OBJECT (frame), "label");
408   g_object_thaw_notify (G_OBJECT (frame));
409 }
410
411 /**
412  * gtk_frame_get_label_widget:
413  * @frame: a #GtkFrame
414  *
415  * Retrieves the label widget for the frame. See
416  * gtk_frame_set_label_widget().
417  *
418  * Return value: the label widget, or %NULL if there is none.
419  **/
420 GtkWidget *
421 gtk_frame_get_label_widget (GtkFrame *frame)
422 {
423   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
424
425   return frame->label_widget;
426 }
427
428 /**
429  * gtk_frame_set_label_align:
430  * @frame: a #GtkFrame
431  * @xalign: The position of the label along the top edge
432  *   of the widget. A value of 0.0 represents left alignment;
433  *   1.0 represents right alignment.
434  * @yalign: The y alignment of the label. A value of 0.0 aligns under 
435  *   the frame; 1.0 aligns above the frame. If the values are exactly
436  *   0.0 or 1.0 the gap in the frame won't be painted because the label
437  *   will be completely above or below the frame.
438  * 
439  * Sets the alignment of the frame widget's label. The
440  * default values for a newly created frame are 0.0 and 0.5.
441  **/
442 void
443 gtk_frame_set_label_align (GtkFrame *frame,
444                            gfloat    xalign,
445                            gfloat    yalign)
446 {
447   g_return_if_fail (GTK_IS_FRAME (frame));
448
449   xalign = CLAMP (xalign, 0.0, 1.0);
450   yalign = CLAMP (yalign, 0.0, 1.0);
451
452   g_object_freeze_notify (G_OBJECT (frame));
453   if (xalign != frame->label_xalign)
454     {
455       frame->label_xalign = xalign;
456       g_object_notify (G_OBJECT (frame), "label-xalign");
457     }
458
459   if (yalign != frame->label_yalign)
460     {
461       frame->label_yalign = yalign;
462       g_object_notify (G_OBJECT (frame), "label-yalign");
463     }
464
465   g_object_thaw_notify (G_OBJECT (frame));
466   gtk_widget_queue_resize (GTK_WIDGET (frame));
467 }
468
469 /**
470  * gtk_frame_get_label_align:
471  * @frame: a #GtkFrame
472  * @xalign: (allow-none): location to store X alignment of frame's label, or %NULL
473  * @yalign: (allow-none): location to store X alignment of frame's label, or %NULL
474  * 
475  * Retrieves the X and Y alignment of the frame's label. See
476  * gtk_frame_set_label_align().
477  **/
478 void
479 gtk_frame_get_label_align (GtkFrame *frame,
480                            gfloat   *xalign,
481                            gfloat   *yalign)
482 {
483   g_return_if_fail (GTK_IS_FRAME (frame));
484
485   if (xalign)
486     *xalign = frame->label_xalign;
487   if (yalign)
488     *yalign = frame->label_yalign;
489 }
490
491 /**
492  * gtk_frame_set_shadow_type:
493  * @frame: a #GtkFrame
494  * @type: the new #GtkShadowType
495  * 
496  * Sets the shadow type for @frame.
497  **/
498 void
499 gtk_frame_set_shadow_type (GtkFrame      *frame,
500                            GtkShadowType  type)
501 {
502   GtkWidget *widget;
503
504   g_return_if_fail (GTK_IS_FRAME (frame));
505
506   if ((GtkShadowType) frame->shadow_type != type)
507     {
508       widget = GTK_WIDGET (frame);
509       frame->shadow_type = type;
510       g_object_notify (G_OBJECT (frame), "shadow-type");
511
512       if (gtk_widget_is_drawable (widget))
513         {
514           gtk_widget_queue_draw (widget);
515         }
516       
517       gtk_widget_queue_resize (widget);
518     }
519 }
520
521 /**
522  * gtk_frame_get_shadow_type:
523  * @frame: a #GtkFrame
524  *
525  * Retrieves the shadow type of the frame. See
526  * gtk_frame_set_shadow_type().
527  *
528  * Return value: the current shadow type of the frame.
529  **/
530 GtkShadowType
531 gtk_frame_get_shadow_type (GtkFrame *frame)
532 {
533   g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
534
535   return frame->shadow_type;
536 }
537
538 static void
539 gtk_frame_paint (GtkWidget    *widget,
540                  GdkRectangle *area)
541 {
542   GtkFrame *frame;
543   gint x, y, width, height;
544
545   if (gtk_widget_is_drawable (widget))
546     {
547       frame = GTK_FRAME (widget);
548
549       x = frame->child_allocation.x - widget->style->xthickness;
550       y = frame->child_allocation.y - widget->style->ythickness;
551       width = frame->child_allocation.width + 2 * widget->style->xthickness;
552       height =  frame->child_allocation.height + 2 * widget->style->ythickness;
553
554       if (frame->label_widget)
555         {
556           GtkRequisition child_requisition;
557           gfloat xalign;
558           gint height_extra;
559           gint x2;
560
561           gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
562
563           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
564             xalign = frame->label_xalign;
565           else
566             xalign = 1 - frame->label_xalign;
567
568           height_extra = MAX (0, child_requisition.height - widget->style->ythickness)
569             - frame->label_yalign * child_requisition.height;
570           y -= height_extra;
571           height += height_extra;
572           
573           x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
574           
575           /* If the label is completely over or under the frame we can omit the gap */
576           if (frame->label_yalign == 0.0 || frame->label_yalign == 1.0)
577             gtk_paint_shadow (widget->style, widget->window,
578                               widget->state, frame->shadow_type,
579                               area, widget, "frame",
580                               x, y, width, height);
581           else
582             gtk_paint_shadow_gap (widget->style, widget->window,
583                                   widget->state, frame->shadow_type,
584                                   area, widget, "frame",
585                                   x, y, width, height,
586                                   GTK_POS_TOP,
587                                   x2, child_requisition.width + 2 * LABEL_PAD);
588         }
589        else
590          gtk_paint_shadow (widget->style, widget->window,
591                            widget->state, frame->shadow_type,
592                            area, widget, "frame",
593                            x, y, width, height);
594     }
595 }
596
597 static gboolean
598 gtk_frame_expose (GtkWidget      *widget,
599                   GdkEventExpose *event)
600 {
601   if (gtk_widget_is_drawable (widget))
602     {
603       gtk_frame_paint (widget, &event->area);
604
605       GTK_WIDGET_CLASS (gtk_frame_parent_class)->expose_event (widget, event);
606     }
607
608   return FALSE;
609 }
610
611 static void
612 gtk_frame_size_allocate (GtkWidget     *widget,
613                          GtkAllocation *allocation)
614 {
615   GtkFrame *frame = GTK_FRAME (widget);
616   GtkBin *bin = GTK_BIN (widget);
617   GtkAllocation new_allocation;
618
619   widget->allocation = *allocation;
620
621   gtk_frame_compute_child_allocation (frame, &new_allocation);
622   
623   /* If the child allocation changed, that means that the frame is drawn
624    * in a new place, so we must redraw the entire widget.
625    */
626   if (gtk_widget_get_mapped (widget)
627 #if 0
628       &&
629       (new_allocation.x != frame->child_allocation.x ||
630        new_allocation.y != frame->child_allocation.y ||
631        new_allocation.width != frame->child_allocation.width ||
632        new_allocation.height != frame->child_allocation.height)
633 #endif
634      )
635     gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
636   
637   if (bin->child && gtk_widget_get_visible (bin->child))
638     gtk_widget_size_allocate (bin->child, &new_allocation);
639   
640   frame->child_allocation = new_allocation;
641   
642   if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
643     {
644       GtkRequisition child_requisition;
645       GtkAllocation child_allocation;
646       gfloat xalign;
647
648       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
649
650       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
651         xalign = frame->label_xalign;
652       else
653         xalign = 1 - frame->label_xalign;
654       
655       child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
656         (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
657       child_allocation.width = MIN (child_requisition.width, new_allocation.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD);
658
659       child_allocation.y = frame->child_allocation.y - MAX (child_requisition.height, widget->style->ythickness);
660       child_allocation.height = child_requisition.height;
661
662       gtk_widget_size_allocate (frame->label_widget, &child_allocation);
663     }
664 }
665
666 static void
667 gtk_frame_compute_child_allocation (GtkFrame      *frame,
668                                     GtkAllocation *child_allocation)
669 {
670   g_return_if_fail (GTK_IS_FRAME (frame));
671   g_return_if_fail (child_allocation != NULL);
672
673   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
674 }
675
676 static void
677 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
678                                          GtkAllocation *child_allocation)
679 {
680   GtkWidget *widget = GTK_WIDGET (frame);
681   GtkAllocation *allocation = &widget->allocation;
682   GtkRequisition child_requisition;
683   gint top_margin;
684   guint border_width;
685
686   if (frame->label_widget)
687     {
688       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
689       top_margin = MAX (child_requisition.height, widget->style->ythickness);
690     }
691   else
692     top_margin = widget->style->ythickness;
693
694   border_width = gtk_container_get_border_width (GTK_CONTAINER (frame));
695   
696   child_allocation->x = border_width + widget->style->xthickness;
697   child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
698   
699   child_allocation->y = border_width + top_margin;
700   child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
701                                       border_width -
702                                       (gint)widget->style->ythickness));
703   
704   child_allocation->x += allocation->x;
705   child_allocation->y += allocation->y;
706 }
707
708 static void
709 gtk_frame_get_size (GtkSizeRequest *request,
710                     GtkOrientation  orientation,
711                     gint           *minimum_size,
712                     gint           *natural_size)
713 {
714   GtkWidget *widget = GTK_WIDGET (request);
715   GtkFrame *frame = GTK_FRAME (widget);
716   GtkBin *bin = GTK_BIN (widget);
717   gint child_min, child_nat;
718   gint minimum, natural;
719   guint border_width;
720
721   if (frame->label_widget && gtk_widget_get_visible (frame->label_widget))
722     {
723       if (orientation == GTK_ORIENTATION_HORIZONTAL)
724         {
725           gtk_size_request_get_width (GTK_SIZE_REQUEST (frame->label_widget),
726                                       &child_min, &child_nat);
727           minimum = child_min + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
728           natural = child_nat + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
729         }
730       else
731         {
732           gtk_size_request_get_height (GTK_SIZE_REQUEST (frame->label_widget),
733                                        &child_min, &child_nat);
734           minimum = MAX (0, child_min - widget->style->ythickness);
735           natural = MAX (0, child_nat - widget->style->ythickness);
736         }
737     }
738   else
739     {
740       minimum = 0;
741       natural = 0;
742     }
743
744   if (bin->child && gtk_widget_get_visible (bin->child))
745     {
746       if (orientation == GTK_ORIENTATION_HORIZONTAL)
747         {
748           gtk_size_request_get_width (GTK_SIZE_REQUEST (bin->child),
749                                       &child_min, &child_nat);
750           minimum = MAX (minimum, child_min);
751           natural = MAX (natural, child_nat);
752         }
753       else
754         {
755           gtk_size_request_get_height (GTK_SIZE_REQUEST (bin->child),
756                                        &child_min, &child_nat);
757           minimum += child_min;
758           natural += child_nat;
759         }
760     }
761
762   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
763
764   if (orientation == GTK_ORIENTATION_HORIZONTAL)
765     {
766       minimum += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
767       natural += (border_width + GTK_WIDGET (widget)->style->xthickness) * 2;
768     }
769   else
770     {
771       minimum += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
772       natural += (border_width + GTK_WIDGET (widget)->style->ythickness) * 2;
773     }
774
775  if (minimum_size)
776     *minimum_size = minimum;
777
778   if (natural_size)
779     *natural_size = natural;
780 }
781
782 static void
783 gtk_frame_get_width (GtkSizeRequest *widget,
784                      gint           *minimum_size,
785                      gint           *natural_size)
786 {
787   gtk_frame_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
788 }
789
790 static void
791 gtk_frame_get_height (GtkSizeRequest *widget,
792                       gint           *minimum_size,
793                       gint           *natural_size)
794 {
795   gtk_frame_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
796 }
797
798 static void
799 gtk_frame_size_request_init (GtkSizeRequestIface *iface)
800 {
801   iface->get_width = gtk_frame_get_width;
802   iface->get_height = gtk_frame_get_height;
803 }