2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * label_text.c: GLabels label text object
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <libgnomeprint/gnome-glyphlist.h>
26 #include "label-text.h"
28 #include "pixmaps/checkerboard.xpm"
32 /*========================================================*/
33 /* Private macros and constants. */
34 /*========================================================*/
36 #define DEFAULT_FONT_FAMILY "Helvetica"
37 #define DEFAULT_FONT_SIZE 14.0
38 #define DEFAULT_FONT_WEIGHT GNOME_FONT_BOOK
39 #define DEFAULT_FONT_ITALIC_FLAG FALSE
40 #define DEFAULT_JUST GTK_JUSTIFY_LEFT
41 #define DEFAULT_COLOR GNOME_CANVAS_COLOR (0,0,0)
43 /*========================================================*/
45 /*========================================================*/
47 struct _glLabelTextPrivate {
48 GList *lines; /* list of glLabelTextNode lists */
51 GnomeFontWeight font_weight;
52 gboolean font_italic_flag;
53 GtkJustification just;
57 /*========================================================*/
58 /* Private globals. */
59 /*========================================================*/
61 static GObjectClass *parent_class = NULL;
63 static guint instance = 0;
65 /*========================================================*/
66 /* Private function prototypes. */
67 /*========================================================*/
69 static void gl_label_text_class_init (glLabelTextClass *klass);
70 static void gl_label_text_instance_init (glLabelText *ltext);
71 static void gl_label_text_finalize (GObject *object);
73 static void update_size (glLabelText *ltext);
76 /*****************************************************************************/
77 /* Boilerplate object stuff. */
78 /*****************************************************************************/
80 gl_label_text_get_type (void)
82 static GType type = 0;
86 sizeof (glLabelTextClass),
89 (GClassInitFunc) gl_label_text_class_init,
94 (GInstanceInitFunc) gl_label_text_instance_init,
97 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
98 "glLabelText", &info, 0);
105 gl_label_text_class_init (glLabelTextClass *klass)
107 GObjectClass *object_class = (GObjectClass *) klass;
109 parent_class = g_type_class_peek_parent (klass);
111 object_class->finalize = gl_label_text_finalize;
115 gl_label_text_instance_init (glLabelText *ltext)
117 ltext->private = g_new0 (glLabelTextPrivate, 1);
119 ltext->private->font_family = g_strdup(DEFAULT_FONT_FAMILY);
120 ltext->private->font_size = DEFAULT_FONT_SIZE;
121 ltext->private->font_weight = DEFAULT_FONT_WEIGHT;
122 ltext->private->font_italic_flag = DEFAULT_FONT_ITALIC_FLAG;
123 ltext->private->just = DEFAULT_JUST;
124 ltext->private->color = DEFAULT_COLOR;
128 gl_label_text_finalize (GObject *object)
132 g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
134 ltext = GL_LABEL_TEXT (object);
136 g_free (ltext->private);
138 G_OBJECT_CLASS (parent_class)->finalize (object);
141 /*****************************************************************************/
142 /* NEW label "text" object. */
143 /*****************************************************************************/
145 gl_label_text_new (glLabel *label)
149 ltext = g_object_new (gl_label_text_get_type(), NULL);
151 gl_label_object_set_parent (GL_LABEL_OBJECT(ltext), label);
153 return G_OBJECT (ltext);
156 /*****************************************************************************/
157 /* Duplicate object. */
158 /*****************************************************************************/
160 gl_label_text_dup (glLabelText *ltext,
163 glLabelText *new_ltext;
168 GnomeFontWeight font_weight;
169 gboolean font_italic_flag;
171 GtkJustification just;
173 gl_debug (DEBUG_LABEL, "START");
175 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
176 g_return_if_fail (label && GL_IS_LABEL (label));
178 new_ltext = GL_LABEL_TEXT(gl_label_text_new (label));
180 gl_label_object_get_position (GL_LABEL_OBJECT(ltext), &x, &y);
181 gl_label_object_get_size (GL_LABEL_OBJECT(ltext), &w, &h);
183 gl_label_object_set_position (GL_LABEL_OBJECT(new_ltext), x, y);
184 gl_label_object_set_size (GL_LABEL_OBJECT(new_ltext), w, h);
186 lines = gl_label_text_get_lines (ltext);
187 gl_label_text_get_props (ltext,
188 &font_family, &font_size, &font_weight,
192 gl_label_text_set_lines (new_ltext, lines);
193 gl_label_text_set_props (new_ltext,
194 font_family, font_size, font_weight,
198 gl_text_node_lines_free (&lines);
199 g_free (font_family);
201 gl_debug (DEBUG_LABEL, "END");
207 /*****************************************************************************/
208 /* Set object params. */
209 /*****************************************************************************/
211 gl_label_text_set_lines (glLabelText *ltext,
214 gl_debug (DEBUG_LABEL, "START");
216 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
218 gl_text_node_lines_free (<ext->private->lines);
219 ltext->private->lines = gl_text_node_lines_dup (lines);
223 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
225 gl_debug (DEBUG_LABEL, "END");
229 gl_label_text_set_props (glLabelText *ltext,
232 GnomeFontWeight font_weight,
233 gboolean font_italic_flag,
235 GtkJustification just)
239 gl_debug (DEBUG_LABEL, "START");
241 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
243 g_free (ltext->private->font_family);
245 ltext->private->font_family = g_strdup (font_family);
246 ltext->private->font_size = font_size;
247 ltext->private->font_weight = font_weight;
248 ltext->private->font_italic_flag = font_italic_flag;
249 ltext->private->color = color;
250 ltext->private->just = just;
252 gl_debug (DEBUG_LABEL, "just = %d", ltext->private->just);
256 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
258 gl_debug (DEBUG_LABEL, "END");
262 /*****************************************************************************/
263 /* Get object params. */
264 /*****************************************************************************/
266 gl_label_text_get_lines (glLabelText *ltext)
268 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
270 return gl_text_node_lines_dup (ltext->private->lines);
274 gl_label_text_get_props (glLabelText *ltext,
277 GnomeFontWeight *font_weight,
278 gboolean *font_italic_flag,
280 GtkJustification *just)
282 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
284 *font_family = g_strdup (ltext->private->font_family);
285 *font_size = ltext->private->font_size;
286 *font_weight = ltext->private->font_weight;
287 *font_italic_flag = ltext->private->font_italic_flag;
288 *color = ltext->private->color;
289 *just = ltext->private->just;
291 gl_debug (DEBUG_LABEL, "just = %d", *just);
294 /*---------------------------------------------------------------------------*/
295 /* PRIVATE. Update object size. */
296 /*---------------------------------------------------------------------------*/
298 update_size (glLabelText *ltext)
305 GnomeGlyphList *glyphlist;
309 gl_debug (DEBUG_LABEL, "START");
311 font = gnome_font_find_closest_from_weight_slant (
312 ltext->private->font_family,
313 ltext->private->font_weight,
314 ltext->private->font_italic_flag,
315 ltext->private->font_size);
317 text = gl_text_node_lines_expand (ltext->private->lines, NULL);
318 line = g_strsplit (text, "\n", -1);
321 art_affine_identity (affine);
325 for (i = 0; line[i] != NULL; i++) {
327 glyphlist = gnome_glyphlist_from_text_dumb (font, 0,
331 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
333 if ( bbox.x1 > w ) w = bbox.x1;
335 h += ltext->private->font_size;
341 gl_label_object_set_size (GL_LABEL_OBJECT(ltext), w, h);
343 gl_debug (DEBUG_LABEL, "END");