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