]> Pileus Git - ~andy/gtk/blob - gtk/gtklabel.c
Initial revision
[~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 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 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       /* We only draw the label if we have been allocated at least as
251        *  much space as we requested. If we have less space than we
252        *  need to draw the string then we _should_ have asked our
253        *  parent container to resize and a new allocation _should_
254        *  be forthcoming so there is no reason to redraw (incorrectly)
255        *  here.
256        */
257       if ((widget->allocation.width >= widget->requisition.width) &&
258           (widget->allocation.height >= widget->requisition.height))
259         {
260           maxl = widget->requisition.width - misc->xpad * 2;
261           x = widget->allocation.x + misc->xpad +
262                (widget->allocation.width - widget->requisition.width) * misc->xalign + 0.5;
263           y = (widget->allocation.y * (1.0 - misc->yalign) +
264                (widget->allocation.y + widget->allocation.height - (widget->requisition.height -
265                                                                     misc->ypad * 2)) *
266                misc->yalign + widget->style->font->ascent) + 1.5;
267           
268           row = label->row;
269           while (row && row->next)
270             {
271               len = (gchar*) row->next->data - (gchar*) row->data;
272               offset = 0;
273               if (label->jtype == GTK_JUSTIFY_CENTER)
274                 offset = (maxl - gdk_text_width (widget->style->font, row->data, len)) / 2;
275               else if (label->jtype == GTK_JUSTIFY_RIGHT)
276                 offset = (maxl - gdk_text_width (widget->style->font, row->data, len));
277               if (state == GTK_STATE_INSENSITIVE)
278                 gdk_draw_text (widget->window, widget->style->font,
279                                widget->style->white_gc,
280                                offset + x + 1, y + 1, row->data, len);
281               
282               gdk_draw_text (widget->window, widget->style->font,
283                              widget->style->fg_gc[state],
284                              offset + x, y, row->data, len);
285               row = row->next;
286               y += widget->style->font->ascent + widget->style->font->descent + 2;
287             }
288           
289          /* COMMENT: we can avoid gdk_text_width() calls here storing in label->row
290             the widths of the rows calculated in gtk_label_set.
291             Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
292           */
293          offset = 0;
294          if (label->jtype == GTK_JUSTIFY_CENTER)
295            offset = (maxl - gdk_string_width (widget->style->font, row->data)) / 2;
296          else if (label->jtype == GTK_JUSTIFY_RIGHT)
297            offset = (maxl - gdk_string_width (widget->style->font, row->data));
298          if (state == GTK_STATE_INSENSITIVE)
299            gdk_draw_string (widget->window, widget->style->font,
300                             widget->style->white_gc,
301                             offset + x + 1, y + 1, row->data);
302          
303          gdk_draw_string (widget->window, widget->style->font,
304                           widget->style->fg_gc[state],
305                           offset + x, y, row->data);
306
307          /*
308          gdk_draw_rectangle (widget->window,
309                              widget->style->bg_gc[GTK_STATE_SELECTED], FALSE,
310                              widget->allocation.x, widget->allocation.y,
311                              widget->allocation.width - 1, widget->allocation.height - 1);
312                              */
313         }
314       else
315         {
316           /*
317           g_print ("gtk_label_expose: allocation too small: %d %d ( %d %d )\n",
318                    widget->allocation.width, widget->allocation.height,
319                    widget->requisition.width, widget->requisition.height);
320                    */
321         }
322     }
323
324   return TRUE;
325 }
326
327
328
329