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>
35 #include <libglabels/str.h>
37 #define FRAC_EPSILON 0.00005
40 /****************************************************************************/
41 /* Append ".glabels" extension to filename if needed. */
42 /****************************************************************************/
44 gl_util_add_extension (const gchar *orig_filename)
46 gchar *new_filename, *extension;
48 extension = strrchr (orig_filename, '.');
49 if (extension == NULL) {
50 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
52 if (g_strcasecmp (extension, ".glabels") != 0) {
54 g_strconcat (orig_filename, ".glabels", NULL);
56 new_filename = g_strdup (orig_filename);
63 /****************************************************************************/
64 /* Remove ".glabels" extension from filename if needed. */
65 /****************************************************************************/
67 gl_util_remove_extension (const gchar *orig_filename)
69 gchar *new_filename, *extension;
71 new_filename = g_strdup (orig_filename);
73 extension = strrchr (new_filename, '.');
74 if (extension != NULL) {
75 if (g_strcasecmp (extension, ".glabels") == 0) {
76 *extension = 0; /* truncate string, rm extension */
83 /****************************************************************************/
84 /* Make sure we have an absolute path to filename. */
85 /****************************************************************************/
87 gl_util_make_absolute (const gchar *filename)
89 gchar *pwd, *absolute_filename;
91 if (g_path_is_absolute (filename)) {
92 absolute_filename = g_strdup (filename);
94 pwd = g_get_current_dir ();
95 absolute_filename = g_build_filename (pwd, filename, NULL);
99 return absolute_filename;
102 /****************************************************************************/
103 /* Create fractional representation of number, if possible. */
104 /****************************************************************************/
106 gl_util_fraction (gdouble x)
108 static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
110 gdouble product, remainder;
113 for ( i=0; denom[i] != 0.0; i++ ) {
114 product = x * denom[i];
115 remainder = fabs(product - ((gint)(product+0.5)));
116 if ( remainder < FRAC_EPSILON ) break;
119 if ( denom[i] == 0.0 ) {
120 /* None of our denominators work. */
121 return g_strdup_printf ("%.5g", x);
123 if ( denom[i] == 1.0 ) {
124 /* Simple integer. */
125 return g_strdup_printf ("%d", (gint)x);
127 n = (gint)( x * denom[i] + 0.5 );
130 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
132 return g_strdup_printf ("%d/%d", (n%d), d);
136 /****************************************************************************/
137 /* Utilities to deal with PangoAlignment types. */
138 /****************************************************************************/
140 gl_util_align_to_string (PangoAlignment align)
143 case PANGO_ALIGN_LEFT:
145 case PANGO_ALIGN_CENTER:
147 case PANGO_ALIGN_RIGHT:
155 gl_util_string_to_align (const gchar *string)
158 if (g_strcasecmp (string, "Left") == 0) {
159 return PANGO_ALIGN_LEFT;
160 } else if (g_strcasecmp (string, "Center") == 0) {
161 return PANGO_ALIGN_CENTER;
162 } else if (g_strcasecmp (string, "Right") == 0) {
163 return PANGO_ALIGN_RIGHT;
165 return PANGO_ALIGN_LEFT;
170 /****************************************************************************/
171 /* Utilities to deal with PangoWeight types */
172 /****************************************************************************/
174 gl_util_weight_to_string (PangoWeight weight)
177 case PANGO_WEIGHT_NORMAL:
179 case PANGO_WEIGHT_BOLD:
187 gl_util_string_to_weight (const gchar *string)
190 if (g_strcasecmp (string, "Regular") == 0) {
191 return PANGO_WEIGHT_NORMAL;
192 } else if (g_strcasecmp (string, "Bold") == 0) {
193 return PANGO_WEIGHT_BOLD;
195 return PANGO_WEIGHT_NORMAL;
200 /****************************************************************************/
201 /* Convienience function to set strings in a text combo_box from a GList */
202 /****************************************************************************/
204 gl_util_combo_box_set_strings (GtkComboBox *combo,
210 g_return_if_fail (list);
212 model = gtk_combo_box_get_model(combo);
213 gtk_list_store_clear (GTK_LIST_STORE (model));
215 for (p=list; p!=NULL; p=p->next) {
217 gtk_combo_box_append_text (combo, p->data);
222 /*---------------------------------------------------------------------------*/
223 /* PRIVATE. gl_util_combo_box_set_active_text support. */
224 /*---------------------------------------------------------------------------*/
233 search_text_func (GtkTreeModel *model,
238 TextSearchData *search_data = (TextSearchData *)data;
241 gtk_tree_model_get (model, iter, 0, &text, -1);
243 if (strcmp (text,search_data->text) == 0) {
244 search_data->found = TRUE;
245 search_data->iter = *iter;
253 /****************************************************************************/
254 /* Convienience function to set active text in a text combo_box from text */
255 /****************************************************************************/
257 gl_util_combo_box_set_active_text (GtkComboBox *combo,
260 GtkTreeModel *model = gtk_combo_box_get_model(combo);
262 g_return_if_fail (GTK_IS_LIST_STORE (model));
266 gtk_combo_box_set_active (combo, -1);
269 TextSearchData search_data;
271 search_data.text = text;
272 search_data.found = FALSE;
274 gtk_tree_model_foreach (model, search_text_func, &search_data);
275 if (search_data.found) {
276 gtk_combo_box_set_active_iter (combo,
279 gtk_combo_box_set_active (combo, -1);
286 /****************************************************************************/
287 /* Convienience function to add a simple text model to an existing */
288 /* combo_box. This is needed since combo_boxes created with glade do not */
289 /* use the gtk_combo_box_new_text() constructor. */
290 /****************************************************************************/
292 gl_util_combo_box_add_text_model (GtkComboBox *combo)
294 GtkCellRenderer *cell;
297 store = gtk_list_store_new (1, G_TYPE_STRING);
298 gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
299 g_object_unref (store);
301 cell = gtk_cell_renderer_text_new ();
302 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
303 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
309 /****************************************************************************/
310 /* Get list of available font families. */
311 /****************************************************************************/
313 gl_util_get_font_family_list (void)
315 static GList *list = NULL;
316 PangoFontMap *fontmap;
317 PangoContext *context;
318 PangoFontFamily **families;
325 fontmap = pango_cairo_font_map_new ();
326 context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap));
328 pango_context_list_families (context, &families, &n);
330 for ( i=0; i<n; i++ )
332 name = g_strdup (pango_font_family_get_name (families[i]));
333 list = g_list_insert_sorted (list, name,
334 (GCompareFunc)lgl_str_utf8_casecmp);
339 g_object_unref (context);
340 g_object_unref (fontmap);