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
28 #include <libgnomeprint/gnome-font.h>
32 #define FRAC_DELTA 0.00005
35 /****************************************************************************/
36 /* Append ".glabels" extension to filename if needed. */
37 /****************************************************************************/
39 gl_util_add_extension (const gchar * orig_filename)
41 gchar *new_filename, *extension;
43 extension = strrchr (orig_filename, '.');
44 if (extension == NULL) {
45 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
47 if (g_strcasecmp (extension, ".glabels") != 0) {
49 g_strconcat (orig_filename, ".glabels", NULL);
51 new_filename = g_strdup (orig_filename);
58 /****************************************************************************/
59 /* Make sure we have an absolute path to filename. */
60 /****************************************************************************/
62 gl_util_make_absolute (const gchar * filename)
64 gchar *pwd, *absolute_filename;
66 if (g_path_is_absolute (filename)) {
67 absolute_filename = g_strdup (filename);
69 pwd = g_get_current_dir ();
71 g_strjoin (G_DIR_SEPARATOR_S, pwd, filename, NULL);
75 return absolute_filename;
78 /****************************************************************************/
79 /* Create fractional representation of number, if possible. */
80 /****************************************************************************/
82 gl_util_fraction( gdouble x )
84 static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
86 gdouble product, remainder;
89 for ( i=0; denom[i] != 0.0; i++ ) {
90 product = x * denom[i];
91 remainder = fabs(product - ((gint)(product+0.5)));
92 if ( remainder < FRAC_DELTA ) break;
95 if ( denom[i] == 0.0 ) {
96 /* None of our denominators work. */
97 return g_strdup_printf ("%.5g", x);
99 if ( denom[i] == 1.0 ) {
100 /* Simple integer. */
101 return g_strdup_printf ("%d", (gint)x);
103 n = (gint)( x * denom[i] + 0.5 );
106 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
108 return g_strdup_printf ("%d/%d", (n%d), d);
112 /****************************************************************************/
113 /* Create button w/ both text and stock image. */
114 /****************************************************************************/
116 gl_button_new_with_stock_image (const gchar* text, const gchar* stock_id)
125 button = gtk_button_new ();
127 if (GTK_BIN (button)->child)
128 gtk_container_remove (GTK_CONTAINER (button),
129 GTK_BIN (button)->child);
131 if (gtk_stock_lookup (stock_id, &item))
133 label = gtk_label_new_with_mnemonic (text);
135 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
137 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
138 hbox = gtk_hbox_new (FALSE, 2);
140 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
142 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
143 gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
145 gtk_container_add (GTK_CONTAINER (button), align);
146 gtk_container_add (GTK_CONTAINER (align), hbox);
147 gtk_widget_show_all (align);
152 label = gtk_label_new_with_mnemonic (text);
153 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
155 gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
157 gtk_widget_show (label);
158 gtk_container_add (GTK_CONTAINER (button), label);
163 /****************************************************************************/
164 /* Add button to gtk_dialog w/ text and stock image. */
165 /****************************************************************************/
167 gl_util_dialog_add_button (GtkDialog *dialog,
169 const gchar* stock_id,
174 g_return_val_if_fail (GTK_IS_DIALOG (dialog), NULL);
175 g_return_val_if_fail (text != NULL, NULL);
176 g_return_val_if_fail (stock_id != NULL, NULL);
178 button = gl_button_new_with_stock_image (text, stock_id);
179 g_return_val_if_fail (button != NULL, NULL);
181 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
183 gtk_widget_show (button);
185 gtk_dialog_add_action_widget (dialog, button, response_id);
190 /****************************************************************************/
191 /* Utilities to deal with GTK_JUSTIFICATION types. */
192 /****************************************************************************/
194 gl_util_just_to_string (GtkJustification just)
197 case GTK_JUSTIFY_LEFT:
199 case GTK_JUSTIFY_CENTER:
201 case GTK_JUSTIFY_RIGHT:
209 gl_util_string_to_just (const gchar * string)
212 if (g_strcasecmp (string, "Left") == 0) {
213 return GTK_JUSTIFY_LEFT;
214 } else if (g_strcasecmp (string, "Center") == 0) {
215 return GTK_JUSTIFY_CENTER;
216 } else if (g_strcasecmp (string, "Right") == 0) {
217 return GTK_JUSTIFY_RIGHT;
219 return GTK_JUSTIFY_LEFT;
224 /****************************************************************************/
225 /* Utilities to deal with GNOME_FONT_WEIGHT types */
226 /****************************************************************************/
228 gl_util_weight_to_string (GnomeFontWeight weight)
231 case GNOME_FONT_BOOK:
233 case GNOME_FONT_BOLD:
241 gl_util_string_to_weight (const gchar * string)
244 if (g_strcasecmp (string, "Regular") == 0) {
245 return GNOME_FONT_BOOK;
246 } else if (g_strcasecmp (string, "Bold") == 0) {
247 return GNOME_FONT_BOLD;
249 return GNOME_FONT_BOOK;