2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * util.c: various small utility functions
6 * Copyright (C) 2001 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
27 #include <libgnome/gnome-util.h>
29 #include <libgnomeprint/gnome-font.h>
33 #define FRAC_DELTA 0.00005
36 /****************************************************************************/
37 /* Get '~/.glabels' directory path. */
38 /****************************************************************************/
40 gl_util_get_home_data_dir (void)
42 gchar *dir = gnome_util_prepend_user_home (".glabels");
44 /* Try to create ~/.glabels directory. If it exists, no problem. */
52 /****************************************************************************/
53 /* Append ".glabels" extension to filename if needed. */
54 /****************************************************************************/
56 gl_util_add_extension (const gchar *orig_filename)
58 gchar *new_filename, *extension;
60 extension = strrchr (orig_filename, '.');
61 if (extension == NULL) {
62 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
64 if (g_strcasecmp (extension, ".glabels") != 0) {
66 g_strconcat (orig_filename, ".glabels", NULL);
68 new_filename = g_strdup (orig_filename);
75 /****************************************************************************/
76 /* Remove ".glabels" extension from filename if needed. */
77 /****************************************************************************/
79 gl_util_remove_extension (const gchar *orig_filename)
81 gchar *new_filename, *extension;
83 new_filename = g_strdup (orig_filename);
85 extension = strrchr (new_filename, '.');
86 if (extension != NULL) {
87 if (g_strcasecmp (extension, ".glabels") == 0) {
88 *extension = 0; /* truncate string, rm extension */
95 /****************************************************************************/
96 /* Make sure we have an absolute path to filename. */
97 /****************************************************************************/
99 gl_util_make_absolute (const gchar *filename)
101 gchar *pwd, *absolute_filename;
103 if (g_path_is_absolute (filename)) {
104 absolute_filename = g_strdup (filename);
106 pwd = g_get_current_dir ();
107 absolute_filename = g_build_filename (pwd, filename, NULL);
111 return absolute_filename;
114 /****************************************************************************/
115 /* Create fractional representation of number, if possible. */
116 /****************************************************************************/
118 gl_util_fraction (gdouble x)
120 static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
122 gdouble product, remainder;
125 for ( i=0; denom[i] != 0.0; i++ ) {
126 product = x * denom[i];
127 remainder = fabs(product - ((gint)(product+0.5)));
128 if ( remainder < FRAC_DELTA ) break;
131 if ( denom[i] == 0.0 ) {
132 /* None of our denominators work. */
133 return g_strdup_printf ("%.5g", x);
135 if ( denom[i] == 1.0 ) {
136 /* Simple integer. */
137 return g_strdup_printf ("%d", (gint)x);
139 n = (gint)( x * denom[i] + 0.5 );
142 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
144 return g_strdup_printf ("%d/%d", (n%d), d);
148 /****************************************************************************/
149 /* Utilities to deal with GTK_JUSTIFICATION types. */
150 /****************************************************************************/
152 gl_util_just_to_string (GtkJustification just)
155 case GTK_JUSTIFY_LEFT:
157 case GTK_JUSTIFY_CENTER:
159 case GTK_JUSTIFY_RIGHT:
167 gl_util_string_to_just (const gchar *string)
170 if (g_strcasecmp (string, "Left") == 0) {
171 return GTK_JUSTIFY_LEFT;
172 } else if (g_strcasecmp (string, "Center") == 0) {
173 return GTK_JUSTIFY_CENTER;
174 } else if (g_strcasecmp (string, "Right") == 0) {
175 return GTK_JUSTIFY_RIGHT;
177 return GTK_JUSTIFY_LEFT;
182 /****************************************************************************/
183 /* Utilities to deal with GNOME_FONT_WEIGHT types */
184 /****************************************************************************/
186 gl_util_weight_to_string (GnomeFontWeight weight)
189 case GNOME_FONT_BOOK:
191 case GNOME_FONT_BOLD:
199 gl_util_string_to_weight (const gchar *string)
202 if (g_strcasecmp (string, "Regular") == 0) {
203 return GNOME_FONT_BOOK;
204 } else if (g_strcasecmp (string, "Bold") == 0) {
205 return GNOME_FONT_BOLD;
207 return GNOME_FONT_BOOK;