]> Pileus Git - ~andy/gtk/blob - gtk/gtklabel.c
Minor fixup to gtklabel for prototype/definition consistency
[~andy/gtk] / gtk / gtklabel.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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include <string.h>
19 #include "gtklabel.h"
20
21
22 static void gtk_label_class_init   (GtkLabelClass  *klass);
23 static void gtk_label_init         (GtkLabel       *label);
24 static void gtk_label_destroy      (GtkObject      *object);
25 static void gtk_label_size_request (GtkWidget      *widget,
26                                     GtkRequisition *requisition);
27 static gint gtk_label_expose       (GtkWidget      *widget,
28                                     GdkEventExpose *event);
29
30
31 static GtkMiscClass *parent_class = NULL;
32
33
34 guint
35 gtk_label_get_type ()
36 {
37   static guint label_type = 0;
38
39   if (!label_type)
40     {
41       GtkTypeInfo label_info =
42       {
43         "GtkLabel",
44         sizeof (GtkLabel),
45         sizeof (GtkLabelClass),
46         (GtkClassInitFunc) gtk_label_class_init,
47         (GtkObjectInitFunc) gtk_label_init,
48         (GtkArgFunc) NULL,
49       };
50
51       label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
52     }
53
54   return label_type;
55 }
56
57 static void
58 gtk_label_class_init (GtkLabelClass *class)
59 {
60   GtkObjectClass *object_class;
61   GtkWidgetClass *widget_class;
62
63   object_class = (GtkObjectClass*) class;
64   widget_class = (GtkWidgetClass*) class;
65
66   parent_class = gtk_type_class (gtk_misc_get_type ());
67
68   object_class->destroy = gtk_label_destroy;
69
70   widget_class->size_request = gtk_label_size_request;
71   widget_class->expose_event = gtk_label_expose;
72 }
73
74 static void
75 gtk_label_init (GtkLabel *label)
76 {
77   GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
78
79   label->label = NULL;
80   label->row = NULL;
81   label->jtype = GTK_JUSTIFY_CENTER;
82 }
83
84 GtkWidget*
85 gtk_label_new (const char *str)
86 {
87   GtkLabel *label;
88
89   g_return_val_if_fail (str != NULL, NULL);
90
91   label = gtk_type_new (gtk_label_get_type ());
92
93   gtk_label_set (label, str);
94
95   return GTK_WIDGET (label);
96 }
97
98 void
99 gtk_label_set (GtkLabel *label,
100                const char *str)
101 {
102   char* p;
103
104   g_return_if_fail (label != NULL);
105   g_return_if_fail (GTK_IS_LABEL (label));
106   g_return_if_fail (str != NULL);
107
108   if (label->label)
109     g_free (label->label);
110   label->label = g_strdup (str);
111
112   if (label->row)
113     g_slist_free (label->row);
114   label->row = NULL;
115   label->row = g_slist_append (label->row, label->label);
116   p = label->label;
117   while ((p = strchr(p, '\n')))
118     label->row = g_slist_append (label->row, ++p);
119
120   if (GTK_WIDGET_VISIBLE (label))
121     {
122       if (GTK_WIDGET_MAPPED (label))
123         gdk_window_clear_area (GTK_WIDGET (label)->window,
124                                GTK_WIDGET (label)->allocation.x,
125                                GTK_WIDGET (label)->allocation.y,
126                                GTK_WIDGET (label)->allocation.width,
127                                GTK_WIDGET (label)->allocation.height);
128
129       gtk_widget_queue_resize (GTK_WIDGET (label));
130     }
131 }
132
133 void
134 gtk_label_set_justify (GtkLabel *label, GtkJustification jtype)
135 {
136   g_return_if_fail (label != NULL);
137   g_return_if_fail (GTK_IS_LABEL (label));
138
139   if ((GtkJustification) label->jtype != jtype)
140     {
141       label->jtype = jtype;
142       
143       if (GTK_WIDGET_VISIBLE (label))
144         {
145           if (GTK_WIDGET_MAPPED (label))
146             gdk_window_clear_area (GTK_WIDGET (label)->window,
147                                    GTK_WIDGET (label)->allocation.x,
148                                    GTK_WIDGET (label)->allocation.y,
149                                    GTK_WIDGET (label)->allocation.width,
150                                    GTK_WIDGET (label)->allocation.height);
151           
152           gtk_widget_queue_resize (GTK_WIDGET (label));
153         }
154     }
155 }
156
157 void
158 gtk_label_get (GtkLabel  *label,
159                char     **str)
160 {
161   g_return_if_fail (label != NULL);
162   g_return_if_fail (GTK_IS_LABEL (label));
163   g_return_if_fail (str != NULL);
164
165   *str = label->label;
166 }
167
168
169 static void
170 gtk_label_destroy (GtkObject *object)
171 {
172   GtkLabel *label;
173
174   g_return_if_fail (object != NULL);
175   g_return_if_fail (GTK_IS_LABEL (object));
176
177   label = GTK_LABEL (object);
178
179   if (GTK_WIDGET (object)->parent &&
180       GTK_WIDGET_MAPPED (object))
181     gtk_widget_unmap (GTK_WIDGET (object));
182
183   g_free (label->label);
184   g_slist_free (label->row);
185
186   if (GTK_OBJECT_CLASS (parent_class)->destroy)
187     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
188 }
189
190 static void
191 gtk_label_size_request (GtkWidget      *widget,
192                         GtkRequisition *requisition)
193 {
194   GtkLabel *label;
195   GSList *row;
196   gint width;
197
198   g_return_if_fail (widget != NULL);
199   g_return_if_fail (GTK_IS_LABEL (widget));
200   g_return_if_fail (requisition != NULL);
201
202   label = GTK_LABEL (widget);
203
204   row = label->row;
205   width = 0;
206   while (row)
207     {
208       if (row->next)
209           width = MAX (width,
210                        gdk_text_width (GTK_WIDGET (label)->style->font, row->data,
211                                        (gchar*) row->next->data - (gchar*) row->data));
212       else
213         width = MAX (width, gdk_string_width (GTK_WIDGET (label)->style->font, row->data));
214       row = row->next;
215     }
216   
217   requisition->width = width + label->misc.xpad * 2;
218   requisition->height = ((GTK_WIDGET (label)->style->font->ascent +
219                           GTK_WIDGET (label)->style->font->descent + 2) *
220                          g_slist_length(label->row) +
221                          label->misc.ypad * 2);
222 }
223
224 static gint
225 gtk_label_expose (GtkWidget      *widget,
226                   GdkEventExpose *event)
227 {
228   GtkLabel *label;
229   GtkMisc *misc;
230   GSList *row;
231   gint state;
232   gint offset;
233   gint len;
234   gint maxl;
235   gint x, y;
236
237   g_return_val_if_fail (widget != NULL, FALSE);
238   g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
239   g_return_val_if_fail (event != NULL, FALSE);
240
241   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
242     {
243       label = GTK_LABEL (widget);
244       misc = GTK_MISC (widget);
245
246       state = widget->state;
247       if (!GTK_WIDGET_IS_SENSITIVE (widget))
248         state = GTK_STATE_INSENSITIVE;
249
250       maxl = widget->requisition.width - misc->xpad * 2;
251
252       /*
253        * GC Clipping
254        */
255       gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
256       gdk_gc_set_clip_rectangle (widget->style->fg_gc[state], &event->area);
257
258       x = widget->allocation.x + misc->xpad +
259         (widget->allocation.width - widget->requisition.width) 
260         * misc->xalign + 0.5;
261
262       y = (widget->allocation.y * (1.0 - misc->yalign) +
263            (widget->allocation.y + widget->allocation.height -
264             (widget->requisition.height - misc->ypad * 2)) *
265            misc->yalign + widget->style->font->ascent) + 1.5;
266           
267       row = label->row;
268       while (row && row->next)
269         {
270           len = (gchar*) row->next->data - (gchar*) row->data;
271           offset = 0;
272
273           if (label->jtype == GTK_JUSTIFY_CENTER)
274             offset = (maxl - gdk_text_width (widget->style->font, row->data, len)) / 2;
275
276           else if (label->jtype == GTK_JUSTIFY_RIGHT)
277             offset = (maxl - gdk_text_width (widget->style->font, row->data, len));
278
279           if (state == GTK_STATE_INSENSITIVE)
280             gdk_draw_text (widget->window, widget->style->font,
281                            widget->style->white_gc,
282                            offset + x + 1, y + 1, row->data, len);
283           
284           gdk_draw_text (widget->window, widget->style->font,
285                          widget->style->fg_gc[state],
286                          offset + x, y, row->data, len);
287           row = row->next;
288           y += widget->style->font->ascent + widget->style->font->descent + 2;
289         }
290       
291       /* 
292        * COMMENT: we can avoid gdk_text_width() calls here storing in label->row
293        * the widths of the rows calculated in gtk_label_set.
294        * Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
295        */
296       offset = 0;
297
298       if (label->jtype == GTK_JUSTIFY_CENTER)
299         offset = (maxl - gdk_string_width (widget->style->font, row->data)) / 2;
300
301       else if (label->jtype == GTK_JUSTIFY_RIGHT)
302         offset = (maxl - gdk_string_width (widget->style->font, row->data));
303
304       if (state == GTK_STATE_INSENSITIVE)
305         gdk_draw_string (widget->window, widget->style->font,
306                          widget->style->white_gc,
307                          offset + x + 1, y + 1, row->data);
308       
309       gdk_draw_string (widget->window, widget->style->font,
310                        widget->style->fg_gc[state],
311                        offset + x, y, row->data);
312
313       gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
314       gdk_gc_set_clip_mask (widget->style->fg_gc[state], NULL);
315
316     }
317   return TRUE;
318 }
319
320
321
322
323