1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * util.c: various small utility functions
8 * Copyright (C) 2001 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <gtk/gtkliststore.h>
33 #include <gtk/gtkcellrenderertext.h>
34 #include <gtk/gtkcelllayout.h>
36 #define FRAC_EPSILON 0.00005
39 /****************************************************************************/
40 /* Append ".glabels" extension to filename if needed. */
41 /****************************************************************************/
43 gl_util_add_extension (const gchar *orig_filename)
45 gchar *new_filename, *extension;
47 extension = strrchr (orig_filename, '.');
48 if (extension == NULL) {
49 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
51 if (g_strcasecmp (extension, ".glabels") != 0) {
53 g_strconcat (orig_filename, ".glabels", NULL);
55 new_filename = g_strdup (orig_filename);
62 /****************************************************************************/
63 /* Remove ".glabels" extension from filename if needed. */
64 /****************************************************************************/
66 gl_util_remove_extension (const gchar *orig_filename)
68 gchar *new_filename, *extension;
70 new_filename = g_strdup (orig_filename);
72 extension = strrchr (new_filename, '.');
73 if (extension != NULL) {
74 if (g_strcasecmp (extension, ".glabels") == 0) {
75 *extension = 0; /* truncate string, rm extension */
82 /****************************************************************************/
83 /* Make sure we have an absolute path to filename. */
84 /****************************************************************************/
86 gl_util_make_absolute (const gchar *filename)
88 gchar *pwd, *absolute_filename;
90 if (g_path_is_absolute (filename)) {
91 absolute_filename = g_strdup (filename);
93 pwd = g_get_current_dir ();
94 absolute_filename = g_build_filename (pwd, filename, NULL);
98 return absolute_filename;
101 /****************************************************************************/
102 /* Create fractional representation of number, if possible. */
103 /****************************************************************************/
105 gl_util_fraction (gdouble x)
107 static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
109 gdouble product, remainder;
112 for ( i=0; denom[i] != 0.0; i++ ) {
113 product = x * denom[i];
114 remainder = fabs(product - ((gint)(product+0.5)));
115 if ( remainder < FRAC_EPSILON ) break;
118 if ( denom[i] == 0.0 ) {
119 /* None of our denominators work. */
120 return g_strdup_printf ("%.5g", x);
122 if ( denom[i] == 1.0 ) {
123 /* Simple integer. */
124 return g_strdup_printf ("%d", (gint)x);
126 n = (gint)( x * denom[i] + 0.5 );
129 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
131 return g_strdup_printf ("%d/%d", (n%d), d);
135 /****************************************************************************/
136 /* Utilities to deal with PangoAlignment types. */
137 /****************************************************************************/
139 gl_util_align_to_string (PangoAlignment align)
142 case PANGO_ALIGN_LEFT:
144 case PANGO_ALIGN_CENTER:
146 case PANGO_ALIGN_RIGHT:
154 gl_util_string_to_align (const gchar *string)
157 if (g_strcasecmp (string, "Left") == 0) {
158 return PANGO_ALIGN_LEFT;
159 } else if (g_strcasecmp (string, "Center") == 0) {
160 return PANGO_ALIGN_CENTER;
161 } else if (g_strcasecmp (string, "Right") == 0) {
162 return PANGO_ALIGN_RIGHT;
164 return PANGO_ALIGN_LEFT;
169 /****************************************************************************/
170 /* Utilities to deal with PangoWeight types */
171 /****************************************************************************/
173 gl_util_weight_to_string (PangoWeight weight)
176 case PANGO_WEIGHT_NORMAL:
178 case PANGO_WEIGHT_BOLD:
186 gl_util_string_to_weight (const gchar *string)
189 if (g_strcasecmp (string, "Regular") == 0) {
190 return PANGO_WEIGHT_NORMAL;
191 } else if (g_strcasecmp (string, "Bold") == 0) {
192 return PANGO_WEIGHT_BOLD;
194 return PANGO_WEIGHT_NORMAL;
199 /****************************************************************************/
200 /* Convienience function to set strings in a text combo_box from a GList */
201 /****************************************************************************/
203 gl_util_combo_box_set_strings (GtkComboBox *combo,
209 g_return_if_fail (list);
211 model = gtk_combo_box_get_model(combo);
212 gtk_list_store_clear (GTK_LIST_STORE (model));
214 for (p=list; p!=NULL; p=p->next) {
216 gtk_combo_box_append_text (combo, p->data);
221 /*---------------------------------------------------------------------------*/
222 /* PRIVATE. gl_util_combo_box_set_active_text support. */
223 /*---------------------------------------------------------------------------*/
232 search_text_func (GtkTreeModel *model,
237 TextSearchData *search_data = (TextSearchData *)data;
240 gtk_tree_model_get (model, iter, 0, &text, -1);
242 if (strcmp (text,search_data->text) == 0) {
243 search_data->found = TRUE;
244 search_data->iter = *iter;
252 /****************************************************************************/
253 /* Convienience function to set active text in a text combo_box from text */
254 /****************************************************************************/
256 gl_util_combo_box_set_active_text (GtkComboBox *combo,
259 GtkTreeModel *model = gtk_combo_box_get_model(combo);
261 g_return_if_fail (GTK_IS_LIST_STORE (model));
265 gtk_combo_box_set_active (combo, -1);
268 TextSearchData search_data;
270 search_data.text = text;
271 search_data.found = FALSE;
273 gtk_tree_model_foreach (model, search_text_func, &search_data);
274 if (search_data.found) {
275 gtk_combo_box_set_active_iter (combo,
278 gtk_combo_box_set_active (combo, -1);
285 /****************************************************************************/
286 /* Convienience function to add a simple text model to an existing */
287 /* combo_box. This is needed since combo_boxes created with glade do not */
288 /* use the gtk_combo_box_new_text() constructor. */
289 /****************************************************************************/
291 gl_util_combo_box_add_text_model (GtkComboBox *combo)
293 GtkCellRenderer *cell;
296 store = gtk_list_store_new (1, G_TYPE_STRING);
297 gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
298 g_object_unref (store);
300 cell = gtk_cell_renderer_text_new ();
301 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
302 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
307 /****************************************************************************/
308 /* Get list of available font families. */
309 /****************************************************************************/
311 gl_util_get_font_family_list (void)
314 PangoFontMap *fontmap;
315 PangoContext *context;
316 PangoFontFamily **families;
321 fontmap = pango_cairo_font_map_new ();
322 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
324 pango_context_list_families (context, &families, &n);
326 for ( i=0; i<n; i++ )
328 name = g_strdup (pango_font_family_get_name (families[i]));
329 list = g_list_append (list, name);
334 g_object_unref (context);
335 g_object_unref (fontmap);
340 /****************************************************************************/
341 /* Free previosly allocated list of font families. */
342 /****************************************************************************/
343 void gl_util_font_family_list_free (GList *list)
347 for (p = list; p != NULL; p = p->next) {