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