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