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 GtkTextTagTable *tag_table;
49 GtkTextBuffer *buffer;
53 GnomeFontWeight font_weight;
54 gboolean font_italic_flag;
55 GtkJustification just;
59 /*========================================================*/
60 /* Private globals. */
61 /*========================================================*/
63 static glLabelObjectClass *parent_class = NULL;
65 static guint instance = 0;
67 /*========================================================*/
68 /* Private function prototypes. */
69 /*========================================================*/
71 static void gl_label_text_class_init (glLabelTextClass *klass);
72 static void gl_label_text_instance_init (glLabelText *ltext);
73 static void gl_label_text_finalize (GObject *object);
75 static void copy (glLabelObject *dst_object,
76 glLabelObject *src_object);
78 static void buffer_changed_cb (GtkTextBuffer *textbuffer,
81 static void get_size (glLabelObject *object,
86 /*****************************************************************************/
87 /* Boilerplate object stuff. */
88 /*****************************************************************************/
90 gl_label_text_get_type (void)
92 static GType type = 0;
96 sizeof (glLabelTextClass),
99 (GClassInitFunc) gl_label_text_class_init,
102 sizeof (glLabelText),
104 (GInstanceInitFunc) gl_label_text_instance_init,
107 type = g_type_register_static (GL_TYPE_LABEL_OBJECT,
108 "glLabelText", &info, 0);
115 gl_label_text_class_init (glLabelTextClass *klass)
117 GObjectClass *object_class = (GObjectClass *) klass;
118 glLabelObjectClass *label_object_class = (glLabelObjectClass *) klass;
120 parent_class = g_type_class_peek_parent (klass);
122 label_object_class->copy = copy;
123 label_object_class->get_size = get_size;
125 object_class->finalize = gl_label_text_finalize;
129 gl_label_text_instance_init (glLabelText *ltext)
131 ltext->private = g_new0 (glLabelTextPrivate, 1);
133 ltext->private->tag_table = gtk_text_tag_table_new ();
134 ltext->private->buffer = gtk_text_buffer_new (ltext->private->tag_table);
136 ltext->private->font_family = g_strdup(DEFAULT_FONT_FAMILY);
137 ltext->private->font_size = DEFAULT_FONT_SIZE;
138 ltext->private->font_weight = DEFAULT_FONT_WEIGHT;
139 ltext->private->font_italic_flag = DEFAULT_FONT_ITALIC_FLAG;
140 ltext->private->just = DEFAULT_JUST;
141 ltext->private->color = DEFAULT_COLOR;
143 g_signal_connect (G_OBJECT(ltext->private->buffer), "changed",
144 G_CALLBACK(buffer_changed_cb), ltext);
148 gl_label_text_finalize (GObject *object)
152 g_return_if_fail (object && GL_IS_LABEL_TEXT (object));
154 ltext = GL_LABEL_TEXT (object);
156 g_object_unref (ltext->private->tag_table);
157 g_object_unref (ltext->private->buffer);
158 g_free (ltext->private);
160 G_OBJECT_CLASS (parent_class)->finalize (object);
163 /*****************************************************************************/
164 /* NEW label "text" object. */
165 /*****************************************************************************/
167 gl_label_text_new (glLabel *label)
171 ltext = g_object_new (gl_label_text_get_type(), NULL);
173 gl_label_object_set_parent (GL_LABEL_OBJECT(ltext), label);
175 return G_OBJECT (ltext);
178 /*****************************************************************************/
179 /* Copy object contents. */
180 /*****************************************************************************/
182 copy (glLabelObject *dst_object,
183 glLabelObject *src_object)
185 glLabelText *ltext = (glLabelText *)src_object;
186 glLabelText *new_ltext = (glLabelText *)dst_object;
190 GnomeFontWeight font_weight;
191 gboolean font_italic_flag;
193 GtkJustification just;
195 gl_debug (DEBUG_LABEL, "START");
197 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
198 g_return_if_fail (new_ltext && GL_IS_LABEL_TEXT (new_ltext));
200 lines = gl_label_text_get_lines (ltext);
201 gl_label_text_get_props (ltext,
202 &font_family, &font_size, &font_weight,
206 gl_label_text_set_lines (new_ltext, lines);
207 gl_label_text_set_props (new_ltext,
208 font_family, font_size, font_weight,
212 gl_text_node_lines_free (&lines);
213 g_free (font_family);
215 gl_debug (DEBUG_LABEL, "END");
219 /*****************************************************************************/
220 /* Set object params. */
221 /*****************************************************************************/
223 gl_label_text_set_lines (glLabelText *ltext,
228 gl_debug (DEBUG_LABEL, "START");
230 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
232 text = gl_text_node_lines_expand (lines, NULL);
233 gtk_text_buffer_set_text (ltext->private->buffer, text, -1);
236 gl_debug (DEBUG_LABEL, "END");
240 gl_label_text_set_props (glLabelText *ltext,
243 GnomeFontWeight font_weight,
244 gboolean font_italic_flag,
246 GtkJustification just)
249 gchar *good_font_family;
251 gl_debug (DEBUG_LABEL, "START");
253 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
255 /* Make sure we have a valid font family. if not privide a good default. */
256 family_names = gnome_font_family_list ();
257 if (g_list_find_custom (family_names, font_family, (GCompareFunc)g_utf8_collate)) {
258 good_font_family = g_strdup (font_family);
260 if (family_names != NULL) {
261 good_font_family = g_strdup (family_names->data); /* 1st entry */
263 good_font_family = NULL;
266 gnome_font_family_list_free (family_names);
268 g_free (ltext->private->font_family);
270 ltext->private->font_family = good_font_family;
271 ltext->private->font_size = font_size;
272 ltext->private->font_weight = font_weight;
273 ltext->private->font_italic_flag = font_italic_flag;
274 ltext->private->color = color;
275 ltext->private->just = just;
277 gl_debug (DEBUG_LABEL, "just = %d", ltext->private->just);
279 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
281 gl_debug (DEBUG_LABEL, "END");
285 /*****************************************************************************/
286 /* Get object params. */
287 /*****************************************************************************/
289 gl_label_text_get_buffer (glLabelText *ltext)
291 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
293 return ltext->private->buffer;
297 gl_label_text_get_lines (glLabelText *ltext)
299 GtkTextIter start, end;
303 g_return_val_if_fail (ltext && GL_IS_LABEL_TEXT (ltext), NULL);
305 gtk_text_buffer_get_bounds (ltext->private->buffer, &start, &end);
306 text = gtk_text_buffer_get_text (ltext->private->buffer,
307 &start, &end, FALSE);
308 lines = gl_text_node_lines_new_from_text (text);
315 gl_label_text_get_props (glLabelText *ltext,
318 GnomeFontWeight *font_weight,
319 gboolean *font_italic_flag,
321 GtkJustification *just)
323 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
325 *font_family = g_strdup (ltext->private->font_family);
326 *font_size = ltext->private->font_size;
327 *font_weight = ltext->private->font_weight;
328 *font_italic_flag = ltext->private->font_italic_flag;
329 *color = ltext->private->color;
330 *just = ltext->private->just;
332 gl_debug (DEBUG_LABEL, "just = %d", *just);
336 gl_label_text_get_box (glLabelText *ltext,
340 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
342 /* peek at the parent size. */
343 (* parent_class->get_size) (GL_LABEL_OBJECT(ltext), w, h);
346 /*---------------------------------------------------------------------------*/
347 /* PRIVATE. text buffer "changed" callback. */
348 /*---------------------------------------------------------------------------*/
349 void buffer_changed_cb (GtkTextBuffer *textbuffer,
352 gl_label_object_emit_changed (GL_LABEL_OBJECT(ltext));
355 /*---------------------------------------------------------------------------*/
356 /* PRIVATE. get object size method. */
357 /*---------------------------------------------------------------------------*/
359 get_size (glLabelObject *object,
363 glLabelText *ltext = (glLabelText *)object;
365 GtkTextIter start, end;
369 GnomeGlyphList *glyphlist;
372 gdouble w_parent, h_parent;
374 gl_debug (DEBUG_LABEL, "START");
376 g_return_if_fail (ltext && GL_IS_LABEL_TEXT (ltext));
378 (* parent_class->get_size) (object, &w_parent, &h_parent);
380 if ( (w_parent != 0.0) || (h_parent != 0.0) ) {
386 font = gnome_font_find_closest_from_weight_slant (
387 ltext->private->font_family,
388 ltext->private->font_weight,
389 ltext->private->font_italic_flag,
390 ltext->private->font_size);
392 gtk_text_buffer_get_bounds (ltext->private->buffer, &start, &end);
393 text = gtk_text_buffer_get_text (ltext->private->buffer,
394 &start, &end, FALSE);
395 line = g_strsplit (text, "\n", -1);
398 art_affine_identity (affine);
402 for (i = 0; line[i] != NULL; i++) {
404 glyphlist = gnome_glyphlist_from_text_dumb (font, 0,
408 gnome_glyphlist_bbox (glyphlist, affine, 0, &bbox);
410 if ( bbox.x1 > *w ) *w = bbox.x1;
412 *h += ltext->private->font_size;
416 if ( *h == 0.0 ) *h = ltext->private->font_size;
418 *w += 2*GL_LABEL_TEXT_MARGIN;
419 *h += 2*GL_LABEL_TEXT_MARGIN;
424 gl_debug (DEBUG_LABEL, "END");