]> Pileus Git - ~andy/gtk/blob - gtk/gtkframe.c
Check for FreeType using freetype-config, since we can't rely on Xft to
[~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
31 #define LABEL_PAD 1
32 #define LABEL_SIDE_PAD 2
33
34 enum {
35   ARG_0,
36   ARG_LABEL,
37   ARG_LABEL_XALIGN,
38   ARG_LABEL_YALIGN,
39   ARG_SHADOW
40 };
41
42
43 static void gtk_frame_class_init    (GtkFrameClass  *klass);
44 static void gtk_frame_init          (GtkFrame       *frame);
45 static void gtk_frame_set_arg       (GtkObject      *object,
46                                      GtkArg         *arg,
47                                      guint           arg_id);
48 static void gtk_frame_get_arg       (GtkObject      *object,
49                                      GtkArg         *arg,
50                                      guint           arg_id);
51 static void gtk_frame_paint         (GtkWidget      *widget,
52                                      GdkRectangle   *area);
53 static gint gtk_frame_expose        (GtkWidget      *widget,
54                                      GdkEventExpose *event);
55 static void gtk_frame_size_request  (GtkWidget      *widget,
56                                      GtkRequisition *requisition);
57 static void gtk_frame_size_allocate (GtkWidget      *widget,
58                                      GtkAllocation  *allocation);
59 static void gtk_frame_map           (GtkWidget      *widget);
60 static void gtk_frame_unmap         (GtkWidget      *widget);
61 static void gtk_frame_remove        (GtkContainer   *container,
62                                      GtkWidget      *child);
63 static void gtk_frame_forall        (GtkContainer   *container,
64                                      gboolean        include_internals,
65                                      GtkCallback     callback,
66                                      gpointer        callback_data);
67
68 static void gtk_frame_compute_child_allocation      (GtkFrame      *frame,
69                                                      GtkAllocation *child_allocation);
70 static void gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
71                                                      GtkAllocation *child_allocation);
72
73 static GtkBinClass *parent_class = NULL;
74
75
76 /* Here until I convince timj about memory management behavior
77  */
78 gchar *    gtk_frame_get_label        (GtkFrame      *frame);
79 gchar *    gtk_label_get_text (GtkLabel         *label);
80
81
82 GtkType
83 gtk_frame_get_type (void)
84 {
85   static GtkType frame_type = 0;
86
87   if (!frame_type)
88     {
89       static const GtkTypeInfo frame_info =
90       {
91         "GtkFrame",
92         sizeof (GtkFrame),
93         sizeof (GtkFrameClass),
94         (GtkClassInitFunc) gtk_frame_class_init,
95         (GtkObjectInitFunc) gtk_frame_init,
96         /* reserved_1 */ NULL,
97         /* reserved_2 */ NULL,
98         (GtkClassInitFunc) NULL,
99       };
100
101       frame_type = gtk_type_unique (gtk_bin_get_type (), &frame_info);
102     }
103
104   return frame_type;
105 }
106
107 static void
108 gtk_frame_class_init (GtkFrameClass *class)
109 {
110   GtkObjectClass *object_class;
111   GtkWidgetClass *widget_class;
112   GtkContainerClass *container_class;
113
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   gtk_object_add_arg_type ("GtkFrame::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
121   gtk_object_add_arg_type ("GtkFrame::label_xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_XALIGN);
122   gtk_object_add_arg_type ("GtkFrame::label_yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_LABEL_YALIGN);
123   gtk_object_add_arg_type ("GtkFrame::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
124
125   object_class->set_arg = gtk_frame_set_arg;
126   object_class->get_arg = gtk_frame_get_arg;
127
128   widget_class->expose_event = gtk_frame_expose;
129   widget_class->size_request = gtk_frame_size_request;
130   widget_class->size_allocate = gtk_frame_size_allocate;
131   widget_class->map = gtk_frame_map;
132   widget_class->unmap = gtk_frame_unmap;
133
134   container_class->remove = gtk_frame_remove;
135   container_class->forall = gtk_frame_forall;
136
137   class->compute_child_allocation = gtk_frame_real_compute_child_allocation;
138 }
139
140 static void
141 gtk_frame_init (GtkFrame *frame)
142 {
143   frame->label_widget = NULL;
144   frame->shadow_type = GTK_SHADOW_ETCHED_IN;
145   frame->label_xalign = 0.0;
146   frame->label_yalign = 0.5;
147 }
148
149 static void
150 gtk_frame_set_arg (GtkObject      *object,
151                    GtkArg         *arg,
152                    guint           arg_id)
153 {
154   GtkFrame *frame;
155
156   frame = GTK_FRAME (object);
157
158   switch (arg_id)
159     {
160     case ARG_LABEL:
161       gtk_frame_set_label (frame, GTK_VALUE_STRING (*arg));
162       break;
163     case ARG_LABEL_XALIGN:
164       gtk_frame_set_label_align (frame, GTK_VALUE_FLOAT (*arg), frame->label_yalign);
165       break;
166     case ARG_LABEL_YALIGN:
167       gtk_frame_set_label_align (frame, frame->label_xalign, GTK_VALUE_FLOAT (*arg));
168       break;
169     case ARG_SHADOW:
170       gtk_frame_set_shadow_type (frame, GTK_VALUE_ENUM (*arg));
171       break;
172     default:
173       break;
174     }
175 }
176
177 static void
178 gtk_frame_get_arg (GtkObject      *object,
179                    GtkArg         *arg,
180                    guint           arg_id)
181 {
182   GtkFrame *frame;
183
184   frame = GTK_FRAME (object);
185
186   switch (arg_id)
187     {
188     case ARG_LABEL:
189       GTK_VALUE_STRING (*arg) = gtk_frame_get_label (frame);
190       break;
191     case ARG_LABEL_XALIGN:
192       GTK_VALUE_FLOAT (*arg) = frame->label_xalign;
193       break;
194     case ARG_LABEL_YALIGN:
195       GTK_VALUE_FLOAT (*arg) = frame->label_yalign;
196       break;
197     case ARG_SHADOW:
198       GTK_VALUE_ENUM (*arg) = frame->shadow_type;
199       break;
200     default:
201       arg->type = GTK_TYPE_INVALID;
202       break;
203     }
204 }
205
206 GtkWidget*
207 gtk_frame_new (const gchar *label)
208 {
209   GtkFrame *frame;
210
211   frame = gtk_type_new (gtk_frame_get_type ());
212
213   gtk_frame_set_label (frame, label);
214
215   return GTK_WIDGET (frame);
216 }
217
218 static void
219 gtk_frame_remove (GtkContainer *container,
220                   GtkWidget    *child)
221 {
222   GtkFrame *frame = GTK_FRAME (container);
223
224   if (frame->label_widget == child)
225     gtk_frame_set_label_widget (frame, NULL);
226   else
227     GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
228 }
229
230 static void
231 gtk_frame_forall (GtkContainer *container,
232                   gboolean      include_internals,
233                   GtkCallback   callback,
234                   gpointer      callback_data)
235 {
236   GtkBin *bin = GTK_BIN (container);
237   GtkFrame *frame = GTK_FRAME (container);
238
239   if (bin->child)
240     (* callback) (bin->child, callback_data);
241
242   if (frame->label_widget)
243     (* callback) (frame->label_widget, callback_data);
244 }
245
246 void
247 gtk_frame_set_label (GtkFrame *frame,
248                      const gchar *label)
249 {
250   g_return_if_fail (frame != NULL);
251   g_return_if_fail (GTK_IS_FRAME (frame));
252
253   if (!label)
254     {
255       gtk_frame_set_label_widget (frame, NULL);
256     }
257   else
258     {
259       GtkWidget *child = gtk_label_new (label);
260       gtk_widget_show (child);
261
262       gtk_frame_set_label_widget (frame, child);
263     }
264 }
265
266 /**
267  * gtk_frame_get_label:
268  * @frame: a #GtkFrame
269  * 
270  * If the frame's label widget is a #GtkLabel, return the
271  * text in the label widget. (The frame will have a #GtkLabel
272  * for the label widget if a non-%NULL argument was passed
273  * to gtk_frame_new().)
274  * 
275  * Return value: the text in the label, or %NULL if there
276  *               was no label widget or the lable widget was not
277  *               a #GtkLabel. This value must be freed with g_free().
278  **/
279 gchar *
280 gtk_frame_get_label (GtkFrame *frame)
281 {
282   g_return_val_if_fail (frame != NULL, NULL);
283   g_return_val_if_fail (GTK_IS_FRAME (frame), NULL);
284
285   if (frame->label_widget && GTK_IS_LABEL (frame->label_widget))
286     return gtk_label_get_text (GTK_LABEL (frame->label_widget));
287   else
288     return NULL;
289 }
290
291 /**
292  * gtk_frame_set_label_widget:
293  * @frame: a #GtkFrame
294  * @label_widget: the new label widget
295  * 
296  * Set the label widget for the frame. This is the widget that
297  * will appear embedded in the top edge of the frame as a
298  * title.
299  **/
300 void
301 gtk_frame_set_label_widget (GtkFrame  *frame,
302                             GtkWidget *label_widget)
303 {
304   gboolean need_resize = FALSE;
305   
306   g_return_if_fail (frame != NULL);
307   g_return_if_fail (GTK_IS_FRAME (frame));
308   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
309   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
310   
311   if (frame->label_widget == label_widget)
312     return;
313   
314   if (frame->label_widget)
315     {
316       need_resize = GTK_WIDGET_VISIBLE (frame->label_widget);
317       gtk_widget_unparent (frame->label_widget);
318     }
319
320   frame->label_widget = label_widget;
321     
322   if (label_widget)
323     {
324       frame->label_widget = label_widget;
325       gtk_widget_set_parent (label_widget, GTK_WIDGET (frame));
326       need_resize |= GTK_WIDGET_VISIBLE (label_widget);
327     }
328
329   if (GTK_WIDGET_VISIBLE (frame) && need_resize)
330     gtk_widget_queue_resize (GTK_WIDGET (frame));
331 }
332
333 void
334 gtk_frame_set_label_align (GtkFrame *frame,
335                            gfloat    xalign,
336                            gfloat    yalign)
337 {
338   g_return_if_fail (frame != NULL);
339   g_return_if_fail (GTK_IS_FRAME (frame));
340
341   xalign = CLAMP (xalign, 0.0, 1.0);
342   yalign = CLAMP (yalign, 0.0, 1.0);
343
344   if ((xalign != frame->label_xalign) || (yalign != frame->label_yalign))
345     {
346       frame->label_xalign = xalign;
347       frame->label_yalign = yalign;
348
349       gtk_widget_queue_resize (GTK_WIDGET (frame));
350     }
351 }
352
353 void
354 gtk_frame_set_shadow_type (GtkFrame      *frame,
355                            GtkShadowType  type)
356 {
357   g_return_if_fail (frame != NULL);
358   g_return_if_fail (GTK_IS_FRAME (frame));
359
360   if ((GtkShadowType) frame->shadow_type != type)
361     {
362       frame->shadow_type = type;
363
364       if (GTK_WIDGET_DRAWABLE (frame))
365         {
366           gtk_widget_queue_clear (GTK_WIDGET (frame));
367         }
368       gtk_widget_queue_resize (GTK_WIDGET (frame));
369     }
370 }
371
372 static void
373 gtk_frame_paint (GtkWidget    *widget,
374                  GdkRectangle *area)
375 {
376   GtkFrame *frame;
377   gint x, y, width, height;
378
379   if (GTK_WIDGET_DRAWABLE (widget))
380     {
381       frame = GTK_FRAME (widget);
382
383       x = frame->child_allocation.x - widget->style->xthickness;
384       y = frame->child_allocation.y - widget->style->ythickness;
385       width = frame->child_allocation.width + 2 * widget->style->xthickness;
386       height =  frame->child_allocation.height + 2 * widget->style->ythickness;
387
388       if (frame->label_widget)
389         {
390           GtkRequisition child_requisition;
391           gfloat xalign;
392           gint height_extra;
393           gint x2;
394
395           gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
396
397           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
398             xalign = frame->label_xalign;
399           else
400             xalign = 1 - frame->label_xalign;
401
402           height_extra = MAX (0, child_requisition.height - widget->style->xthickness);
403           y -= height_extra * (1 - frame->label_yalign);
404           height += height_extra * (1 - frame->label_yalign);
405           
406           x2 = widget->style->xthickness + (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_SIDE_PAD;
407
408           gtk_paint_shadow_gap (widget->style, widget->window,
409                                 GTK_STATE_NORMAL, frame->shadow_type,
410                                 area, widget, "frame",
411                                 x, y, width, height,
412                                 GTK_POS_TOP, 
413                                 x2, child_requisition.width + 2 * LABEL_PAD);
414         }
415        else
416          gtk_paint_shadow (widget->style, widget->window,
417                            GTK_STATE_NORMAL, frame->shadow_type,
418                            area, widget, "frame",
419                            x, y, width, height);
420     }
421 }
422
423 static gboolean
424 gtk_frame_expose (GtkWidget      *widget,
425                   GdkEventExpose *event)
426 {
427   GtkBin *bin = GTK_BIN (widget);
428   GtkFrame *frame = GTK_FRAME (widget);
429   GdkEventExpose child_event;
430
431   if (GTK_WIDGET_DRAWABLE (widget))
432     {
433       gtk_frame_paint (widget, &event->area);
434
435       if (bin->child && GTK_WIDGET_NO_WINDOW (bin->child))
436         {
437           child_event = *event;
438           if (gtk_widget_intersect (bin->child, &event->area, &child_event.area))
439             gtk_widget_event (bin->child, (GdkEvent*) &child_event);
440         }
441       
442       if (frame->label_widget && GTK_WIDGET_NO_WINDOW (frame->label_widget))
443         {
444           child_event = *event;
445           if (gtk_widget_intersect (frame->label_widget, &event->area, &child_event.area))
446             gtk_widget_event (frame->label_widget, (GdkEvent*) &child_event);
447         }
448     }
449
450   return FALSE;
451 }
452
453 static void
454 gtk_frame_size_request (GtkWidget      *widget,
455                         GtkRequisition *requisition)
456 {
457   GtkFrame *frame = GTK_FRAME (widget);
458   GtkBin *bin = GTK_BIN (widget);
459   GtkRequisition child_requisition;
460   
461   if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
462     {
463       gtk_widget_size_request (frame->label_widget, &child_requisition);
464
465       requisition->width = child_requisition.width + 2 * LABEL_PAD + 2 * LABEL_SIDE_PAD;
466       requisition->height =
467         MAX (0, child_requisition.height - GTK_WIDGET (widget)->style->xthickness);
468     }
469   else
470     {
471       requisition->width = 0;
472       requisition->height = 0;
473     }
474   
475   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
476     {
477       gtk_widget_size_request (bin->child, &child_requisition);
478
479       requisition->width = MAX (requisition->width, child_requisition.width);
480       requisition->height += child_requisition.height;
481     }
482
483   requisition->width += (GTK_CONTAINER (widget)->border_width +
484                          GTK_WIDGET (widget)->style->xthickness) * 2;
485   requisition->height += (GTK_CONTAINER (widget)->border_width +
486                           GTK_WIDGET (widget)->style->ythickness) * 2;
487 }
488
489 static void
490 gtk_frame_size_allocate (GtkWidget     *widget,
491                          GtkAllocation *allocation)
492 {
493   GtkFrame *frame = GTK_FRAME (widget);
494   GtkBin *bin = GTK_BIN (widget);
495   GtkAllocation new_allocation;
496
497   widget->allocation = *allocation;
498
499   gtk_frame_compute_child_allocation (frame, &new_allocation);
500   
501   /* If the child allocation changed, that means that the frame is drawn
502    * in a new place, so we must redraw the entire widget.
503    */
504   if (GTK_WIDGET_MAPPED (widget) &&
505       (new_allocation.x != frame->child_allocation.x ||
506        new_allocation.y != frame->child_allocation.y ||
507        new_allocation.width != frame->child_allocation.width ||
508        new_allocation.height != frame->child_allocation.height))
509     gtk_widget_queue_clear (widget);
510   
511   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
512     gtk_widget_size_allocate (bin->child, &new_allocation);
513   
514   frame->child_allocation = new_allocation;
515   
516   if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget))
517     {
518       GtkRequisition child_requisition;
519       GtkAllocation child_allocation;
520       gfloat xalign;
521
522       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
523
524       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
525         xalign = frame->label_xalign;
526       else
527         xalign = 1 - frame->label_xalign;
528       
529       child_allocation.x = frame->child_allocation.x + LABEL_SIDE_PAD +
530         (frame->child_allocation.width - child_requisition.width - 2 * LABEL_PAD - 2 * LABEL_SIDE_PAD) * xalign + LABEL_PAD;
531       child_allocation.width = child_requisition.width;
532
533       child_allocation.y = frame->child_allocation.y - child_requisition.height;
534       child_allocation.height = child_requisition.height;
535
536       gtk_widget_size_allocate (frame->label_widget, &child_allocation);
537     }
538 }
539
540 static void
541 gtk_frame_map (GtkWidget *widget)
542 {
543   GtkFrame *frame = GTK_FRAME (widget);
544   
545   if (frame->label_widget &&
546       GTK_WIDGET_VISIBLE (frame->label_widget) &&
547       !GTK_WIDGET_MAPPED (frame->label_widget))
548     gtk_widget_map (frame->label_widget);
549
550   if (GTK_WIDGET_CLASS (parent_class)->map)
551     (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
552 }
553
554 static void
555 gtk_frame_unmap (GtkWidget *widget)
556 {
557   GtkFrame *frame = GTK_FRAME (widget);
558
559   if (GTK_WIDGET_CLASS (parent_class)->unmap)
560     (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
561
562   if (frame->label_widget && GTK_WIDGET_MAPPED (frame->label_widget))
563     gtk_widget_unmap (frame->label_widget);
564 }
565
566 static void
567 gtk_frame_compute_child_allocation (GtkFrame      *frame,
568                                     GtkAllocation *child_allocation)
569 {
570   g_return_if_fail (frame != NULL);
571   g_return_if_fail (GTK_IS_FRAME (frame));
572   g_return_if_fail (child_allocation != NULL);
573
574   GTK_FRAME_GET_CLASS (frame)->compute_child_allocation (frame, child_allocation);
575 }
576
577 static void
578 gtk_frame_real_compute_child_allocation (GtkFrame      *frame,
579                                          GtkAllocation *child_allocation)
580 {
581   GtkWidget *widget = GTK_WIDGET (frame);
582   GtkAllocation *allocation = &widget->allocation;
583   GtkRequisition child_requisition;
584   gint top_margin;
585
586   if (frame->label_widget)
587     {
588       gtk_widget_get_child_requisition (frame->label_widget, &child_requisition);
589       top_margin = MAX (child_requisition.height, widget->style->ythickness);
590     }
591   else
592     top_margin = widget->style->ythickness;
593   
594   child_allocation->x = (GTK_CONTAINER (frame)->border_width +
595                          widget->style->xthickness);
596   child_allocation->width = MAX(1, (gint)allocation->width - child_allocation->x * 2);
597   
598   child_allocation->y = (GTK_CONTAINER (frame)->border_width + top_margin);
599   child_allocation->height = MAX (1, ((gint)allocation->height - child_allocation->y -
600                                       (gint)GTK_CONTAINER (frame)->border_width -
601                                       (gint)widget->style->ythickness));
602   
603   child_allocation->x += allocation->x;
604   child_allocation->y += allocation->y;
605 }