]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
Enforce the widget/child realization/mapping invariants.
[~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 <string.h>
28 #include "gtkframe.h"
29 #include "gtklabel.h"
30 #include "gtkintl.h"
31
32 #define LABEL_PAD 1
33 #define LABEL_SIDE_PAD 2
34
35 enum {
36   PROP_0,
37   PROP_LABEL,
38   PROP_LABEL_XALIGN,
39   PROP_LABEL_YALIGN,
40   PROP_SHADOW,
41   PROP_LABEL_WIDGET
42 };
43
44
45 static void gtk_frame_class_init    (GtkFrameClass  *klass);
46 static void gtk_frame_init          (GtkFrame       *frame);
47 static void gtk_frame_set_property (GObject      *object,
48                                     guint         param_id,
49                                     const GValue *value,
50                                     GParamSpec   *pspec);
51 static void gtk_frame_get_property (GObject     *object,
52                                     guint        param_id,
53                                     GValue      *value,
54                                     GParamSpec  *pspec);
55 static void gtk_frame_paint         (GtkWidget      *widget,
56                                      GdkRectangle   *area);
57 static gint gtk_frame_expose        (GtkWidget      *widget,
58                                      GdkEventExpose *event);
59 static void gtk_frame_size_request  (GtkWidget      *widget,
60                                      GtkRequisition *requisition);
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 static GtkBinClass *parent_class = NULL;
76
77
78 GtkType
79 gtk_frame_get_type (void)
80 {
81   static GtkType frame_type = 0;
82
83   if (!frame_type)
84     {
85       static const GtkTypeInfo frame_info =
86       {
87         "GtkFrame",
88         sizeof (GtkFrame),
89         sizeof (GtkFrameClass),
90         (GtkClassInitFunc) gtk_frame_class_init,
91         (GtkObjectInitFunc) gtk_frame_init,
92         /* reserved_1 */ NULL,
93         /* reserved_2 */ NULL,
94         (GtkClassInitFunc) NULL,
95       };
96
97       frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
98     }
99
100   return frame_type;
101 }
102
103 static void
104 gtk_frame_class_init (GtkFrameClass *class)
105 {
106   GObjectClass *gobject_class;
107   GtkObjectClass *object_class;
108   GtkWidgetClass *widget_class;
109   GtkContainerClass *container_class;
110
111   gobject_class = (GObjectClass*) class;
112   object_class = GTK_OBJECT_CLASS (class);
113   widget_class = GTK_WIDGET_CLASS (class);
114   container_class = GTK_CONTAINER_CLASS (class);
115
116   parent_class = gtk_type_class (gtk_bin_get_type ());
117
118   gobject_class->set_property = gtk_frame_set_property;
119   gobject_class->get_property = gtk_frame_get_property;
120
121   g_object_class_install_property (gobject_class,
122                                    PROP_LABEL,
123                                    g_param_spec_string ("label",
124                                                         _("Label"),
125                                                         _("Text of the frame's label."),
126                                                         NULL,
127                                                         G_PARAM_READABLE |
128                                                         G_PARAM_WRITABLE));
129   g_object_class_install_property (gobject_class,
130                                    PROP_LABEL_XALIGN,
131                                    g_param_spec_double ("label_xalign",
132                                                         _("Label xalign"),
133                                                         _("The horizontal alignment of the label."),
134                                                         0.0,
135                                                         1.0,
136                                                         0.5,
137                                                         G_PARAM_READABLE |
138                                                         G_PARAM_WRITABLE));
139   g_object_class_install_property (gobject_class,
140                                    PROP_LABEL_YALIGN,
141                                    g_param_spec_double ("label_yalign",
142                                                         _("Label yalign"),
143                                                         _("The vertical alignment of the label."),
144                                                         0.0,
145                                                         1.0,
146                                                         0.5,
147                                                         G_PARAM_READABLE |
148                                                         G_PARAM_WRITABLE));
149   g_object_class_install_property (gobject_class,
150                                    PROP_SHADOW,
151                                    g_param_spec_enum ("shadow",
152                                                       _("Frame shadow"),
153                                                       _("Appearance of the frame border."),
154                                                       GTK_TYPE_SHADOW_TYPE,
155                                                       GTK_SHADOW_ETCHED_IN,
156                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));
157
158   g_object_class_install_property (gobject_class,
159                                    PROP_LABEL_WIDGET,
160                                    g_param_spec_object ("label_widget",
161                                                         _("Label widget"),
162                                                         _("A widget to display in place of the usual frame label."),
163                                                         GTK_TYPE_WIDGET,
164                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
165   
166   widget_class->expose_event = gtk_frame_expose;
167   widget_class->size_request = gtk_frame_size_request;
168   widget_class->size_allocate = gtk_frame_size_allocate;
169
170   container_class->remove = gtk_frame_remove;
171   container_class->forall = gtk_frame_forall;
172
173   class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
174 }
175
176 static void
177 gtk_frame_init (GtkFrame *frame)
178 {
179   frame->label_widget = NULL;
180   frame->shadow_type = GTK_SHADOW_ETCHED_IN;
181   frame->label_xalign = 0.0;
182   frame->label_yalign = 0.5;
183 }
184
185 static void 
186 gtk_frame_set_property (GObject         *object,
187                         guint            prop_id,
188                         const GValue    *value,
189                         GParamSpec      *pspec)
190 {
191   GtkFrame *frame;
192
193   frame = GTK_FRAME (object);
194
195   switch (prop_id)
196     {
197     case PROP_LABEL:
198       gtk_frame_set_label (frame, g_value_get_string (value));
199       break;
200     case PROP_LABEL_XALIGN:
201       gtk_frame_set_label_align (frame, g_value_get_double (value), 
202                                  frame->label_yalign);
203       break;
204     case PROP_LABEL_YALIGN:
205       gtk_frame_set_label_align (frame, frame->label_xalign, 
206                                  g_value_get_double (value));
207       break;
208     case PROP_SHADOW:
209       gtk_frame_set_shadow_type (frame, g_value_get_enum (value));
210       break;
211     case PROP_LABEL_WIDGET:
212       gtk_frame_set_label_widget (frame, g_value_get_object (value));
213       break;
214     default:      
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217     }
218 }
219
220 static void 
221 gtk_frame_get_property (GObject         *object,
222                         guint            prop_id,
223                         GValue          *value,
224                         GParamSpec      *pspec)
225 {
226   GtkFrame *frame;
227
228   frame = GTK_FRAME (object);
229
230   switch (prop_id)
231     {
232     case PROP_LABEL:
233       g_value_set_string (value, gtk_frame_get_label (frame));
234       break;
235     case PROP_LABEL_XALIGN:
236       g_value_set_double (value, frame->label_xalign);
237       break;
238     case PROP_LABEL_YALIGN:
239       g_value_set_double (value, frame->label_yalign);
240       break;
241     case PROP_SHADOW:
242       g_value_set_enum (value, frame->shadow_type);
243       break;
244     case PROP_LABEL_WIDGET:
245       g_value_set_object (value,
246                           frame->label_widget ?
247                           G_OBJECT (frame->label_widget) : NULL);
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252     }
253 }
254
255 GtkWidget*
256 gtk_frame_new (const gchar *label)
257 {
258   GtkFrame *frame;
259
260   frame = gtk_type_new (gtk_frame_get_type ());
261
262   gtk_frame_set_label (frame, label);
263
264   return GTK_WIDGET (frame);
265 }
266
267 static void
268 gtk_frame_remove (GtkContainer *container,
269                   GtkWidget    *child)
270 {
271   GtkFrame *frame = GTK_FRAME (container);
272
273   if (frame->label_widget == child)
274     gtk_frame_set_label_widget (frame, NULL);
275   else
276     GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
277 }
278
279 static void
280 gtk_frame_forall (GtkContainer *container,
281                   gboolean      include_internals,
282                   GtkCallback   callback,
283                   gpointer      callback_data)
284 {
285   GtkBin *bin = GTK_BIN (container);
286   GtkFrame *frame = GTK_FRAME (container);
287
288   if (bin->child)
289     (* callback) (bin->child, callback_data);
290
291   if (frame->label_widget)
292     (* callback) (frame->label_widget, callback_data);
293 }
294
295 void
296 gtk_frame_set_label (GtkFrame *frame,
297                      const gchar *label)
298 {
299   g_return_if_fail (GTK_IS_FRAME (frame));
300
301   if (!label)
302     {
303       gtk_frame_set_label_widget (frame, NULL);
304     }
305   else
306     {
307       GtkWidget *child = gtk_label_new (label);
308       gtk_widget_show (child);
309
310       gtk_frame_set_label_widget (frame, child);
311     }
312
313   g_object_notify (G_OBJECT (frame), "label");
314 }
315
316 /**
317  * gtk_frame_get_label:
318  * @frame: a #GtkFrame
319  * 
320  * If the frame's label widget is a #GtkLabel, return the
321  * text in the label widget. (The frame will have a #GtkLabel
322  * for the label widget if a non-%NULL argument was passed
323  * to gtk_frame_new().)
324  * 
325  * Return value: the text in the label, or %NULL if there
326  *               was no label widget or the lable widget was not
327  *               a #GtkLabel. This value must be freed with g_free().
328  **/
329 G_CONST_RETURN gchar *
330 gtk_frame_get_label (GtkFrame *frame)
331 {
332   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
333
334   if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
335     return gtk_label_get_text (GTK_LABEL (frame->label_widget));
336   else
337     return NULL;
338 }
339
340 /**
341  * gtk_frame_set_label_widget:
342  * @frame: a #GtkFrame
343  * @label_widget: the new label widget
344  * 
345  * Set the label widget for the frame. This is the widget that
346  * will appear embedded in the top edge of the frame as a
347  * title.
348  **/
349 void
350 gtk_frame_set_label_widget (GtkFrame  *frame,
351                             GtkWidget *label_widget)
352 {
353   gboolean need_resize = FALSE;
354   
355   g_return_if_fail (GTK_IS_FRAME (frame));
356   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
357   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
358   
359   if (frame->label_widget == label_widget)
360     return;
361   
362   if (frame->label_widget)
363     {
364       need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
365       gtk_widget_unparent (frame->label_widget);
366     }
367
368   frame->label_widget = label_widget;
369     
370   if (label_widget)
371     {
372       frame->label_widget = label_widget;
373       gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
374       need_resize |= GTK_WIDGET_VISIBLE (label_widget);
375     }
376   
377   if (GTK_WIDGET_VISIBLE (frame) && need_resize)
378     gtk_widget_queue_resize (GTK_WIDGET (frame));
379
380   g_object_notify (G_OBJECT (frame), "label_widget");
381 }
382
383 /**
384  * gtk_frame_get_label_widget:
385  * @frame: a #GtkFrame
386  *
387  * Retrieves the label widget for the frame. See
388  * gtk_frame_set_label_widget().
389  *
390  * Return value: the label widget, or %NULL if there is none.
391  **/
392 GtkWidget *
393 gtk_frame_get_label_widget (GtkFrame *frame)
394 {
395   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
396
397   return frame->label_widget;
398 }
399
400 void
401 gtk_frame_set_label_align (GtkFrame *frame,
402                            gfloat    xalign,
403                            gfloat    yalign)
404 {
405   g_return_if_fail (GTK_IS_FRAME (frame));
406
407   xalign = CLAMP (xalign, 0.0, 1.0);
408   yalign = CLAMP (yalign, 0.0, 1.0);
409
410   if (xalign != frame->label_xalign)
411     {
412       frame->label_xalign = xalign;
413       g_object_notify (G_OBJECT (frame), "label_xalign");
414     }
415
416   if (yalign != frame->label_yalign)
417     {
418       frame->label_yalign = yalign;
419       g_object_notify (G_OBJECT (frame), "label_yalign");
420     }
421
422   gtk_widget_queue_resize (GTK_WIDGET (frame));
423 }
424
425 /**
426  * gtk_frame_get_label_align:
427  * @frame: a #GtkFrame
428  * @xalign: location to store X alignment of frame's label, or %NULL
429  * @yalign: location to store X alignment of frame's label, or %NULL
430  * 
431  * Retrieves the X and Y alignment of the frame's label. See
432  * gtk_frame_set_label_align().
433  **/
434 void
435 gtk_frame_get_label_align (GtkFrame *frame,
436                            gfloat   *xalign,
437                            gfloat   *yalign)
438 {
439   g_return_if_fail (GTK_IS_FRAME (frame));
440
441   if (xalign)
442     *xalign = frame->label_xalign;
443   if (yalign)
444     *yalign = frame->label_yalign;
445 }
446
447 void
448 gtk_frame_set_shadow_type (GtkFrame      *frame,
449                            GtkShadowType  type)
450 {
451   g_return_if_fail (GTK_IS_FRAME (frame));
452
453   if ((GtkShadowType) frame->shadow_type != type)
454     {
455       frame->shadow_type = type;
456       g_object_notify (G_OBJECT (frame), "shadow");
457
458       if (GTK_WIDGET_DRAWABLE (frame))
459         {
460           gtk_widget_queue_clear (GTK_WIDGET (frame));
461         }
462       
463       gtk_widget_queue_resize (GTK_WIDGET (frame));
464     }
465 }
466
467 /**
468  * gtk_frame_get_shadow_type:
469  * @frame: a #GtkFrame
470  *
471  * Retrieves the shadow type of the frame. See
472  * gtk_frame_set_shadow_type().
473  *
474  * Return value: the current shadow type of the frame.
475  **/
476 GtkShadowType
477 gtk_frame_get_shadow_type (GtkFrame *frame)
478 {
479   g_return_val_if_fail (GTK_IS_FRAME (frame), GTK_SHADOW_ETCHED_IN);
480
481   return frame->shadow_type;
482 }
483
484 static void
485 gtk_frame_paint (GtkWidget    *widget,
486                  GdkRectangle *area)
487 {
488   GtkFrame *frame;
489   gint x, y, width, height;
490
491   if (GTK_WIDGET_DRAWABLE (widget))
492     {
493       frame = GTK_FRAME (widget);
494
495       x = frame->child_allocation.x - widget->style->xthickness;
496       y = frame->child_allocation.y - widget->style->ythickness;
497       width = frame->child_allocation.width + 2 * widget->style->xthickness;
498       height =  frame->child_allocation.height + 2 * widget->style->ythickness;
499
500       if (frame->label_widget)
501         {
502           GtkRequisition child_requisition;
503           gfloat xalign;
504           gint height_extra;
505           gint x2;
506
507           gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
508
509           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
510             xalign = frame->label_xalign;
511           else
512             xalign = 1 - frame->label_xalign;
513
514           height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
515           y -= height_extra * (1 - frame->label_yalign);
516           height += height_extra * (1 - frame->label_yalign);
517           
518           x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
519
520           
521           gtk_paint_shadow_gap (widget->style, widget->window,
522                                 GTK_STATE_NORMAL, frame->shadow_type,
523                                 area, widget, "frame",
524                                 x, y, width, height,
525                                 GTK_POS_TOP, 
526                                 x2, child_requisition.width + 2 * LABEL_PAD);
527         }
528        else
529          gtk_paint_shadow (widget->style, widget->window,
530                            GTK_STATE_NORMAL, frame->shadow_type,
531                            area, widget, "frame",
532                            x, y, width, height);
533     }
534 }
535
536 static gboolean
537 gtk_frame_expose (GtkWidget      *widget,
538                   GdkEventExpose *event)
539 {
540   if (GTK_WIDGET_DRAWABLE (widget))
541     {
542       gtk_frame_paint (widget, &event->area);
543
544       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
545     }
546
547   return FALSE;
548 }
549
550 static void
551 gtk_frame_size_request (GtkWidget      *widget,
552                         GtkRequisition *requisition)
553 {
554   GtkFrame *frame = GTK_FRAME (widget);
555   GtkBin *bin = GTK_BIN (widget);
556   GtkRequisition child_requisition;
557   
558   if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
559     {
560       gtk_widget_size_request (frame->label_widget, &child_requisition);
561
562       requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
563       requisition->height =
564         MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
565     }
566   else
567     {
568       requisition->width = 0;
569       requisition->height = 0;
570     }
571   
572   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
573     {
574       gtk_widget_size_request (bin->child, &child_requisition);
575
576       requisition->width = MAX (requisition->width, child_requisition.width);
577       requisition->height += child_requisition.height;
578     }
579
580   requisition->width += (GTK_CONTAINER (widget)->border_width +
581                          GTK_WIDGET (widget)->style->xthickness) * 2;
582   requisition->height += (GTK_CONTAINER (widget)->border_width +
583                           GTK_WIDGET (widget)->style->ythickness) * 2;
584 }
585
586 static void
587 gtk_frame_size_allocate (GtkWidget     *widget,
588                          GtkAllocation *allocation)
589 {
590   GtkFrame *frame = GTK_FRAME (widget);
591   GtkBin *bin = GTK_BIN (widget);
592   GtkAllocation new_allocation;
593
594   widget->allocation = *allocation;
595
596   gtk_frame_compute_child_allocation (frame, &new_allocation);
597   
598   /* If the child allocation changed, that means that the frame is drawn
599    * in a new place, so we must redraw the entire widget.
600    */
601   if (GTK_WIDGET_MAPPED (widget) &&
602       (new_allocation.x != frame->child_allocation.x ||
603        new_allocation.y != frame->child_allocation.y ||
604        new_allocation.width != frame->child_allocation.width ||
605        new_allocation.height != frame->child_allocation.height))
606     gtk_widget_queue_clear (widget);
607   
608   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
609     gtk_widget_size_allocate (bin->child, &new_allocation);
610   
611   frame->child_allocation = new_allocation;
612   
613   if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
614     {
615       GtkRequisition child_requisition;
616       GtkAllocation child_allocation;
617       gfloat xalign;
618
619       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
620
621       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
622         xalign = frame->label_xalign;
623       else
624         xalign = 1 - frame->label_xalign;
625       
626       child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
627         (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
628       child_allocation.width = child_requisition.width;
629
630       child_allocation.y = frame->child_allocation.y - child_requisition.height;
631       child_allocation.height = child_requisition.height;
632
633       gtk_widget_size_allocate (frame->label_widget, &child_allocation);
634     }
635 }
636
637 static void
638 gtk_frame_compute_child_allocation (GtkFrame      *frame,
639                                     GtkAllocation *child_allocation)
640 {
641   g_return_if_fail (GTK_IS_FRAME (frame));
642   g_return_if_fail (child_allocation != NULL);
643
644   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
645 }
646
647 static void
648 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
649                                          GtkAllocation *child_allocation)
650 {
651   GtkWidget *widget = GTK_WIDGET (frame);
652   GtkAllocation *allocation = &widget->allocation;
653   GtkRequisition child_requisition;
654   gint top_margin;
655
656   if (frame->label_widget)
657     {
658       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
659       top_margin = MAX (child_requisition.height, widget->style->ythickness);
660     }
661   else
662     top_margin = widget->style->ythickness;
663   
664   child_allocation->x = (GTK_CONTAINER (frame)->border_width +
665                          widget->style->xthickness);
666   child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
667   
668   child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
669   child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
670                                       (gint)GTK_CONTAINER (frame)->border_width -
671                                       (gint)widget->style->ythickness));
672   
673   child_allocation->x += allocation->x;
674   child_allocation->y += allocation->y;
675 }