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