]> git.sur5r.net Git - glabels/blob - glabels2/src/util.c
Initial revision
[glabels] / glabels2 / src / util.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  util.c:  various small utility functions
5  *
6  *  Copyright (C) 2001  Jim Evins <evins@snaught.com>.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <glib.h>
27 #include <math.h>
28 #include <libgnomeprint/gnome-font.h>
29
30 #include "util.h"
31
32 #define FRAC_DELTA 0.00005
33
34 \f
35 /****************************************************************************/
36 /* Append ".glabels" extension to filename if needed.                       */
37 /****************************************************************************/
38 gchar *
39 gl_util_add_extension (const gchar * orig_filename)
40 {
41         gchar *new_filename, *extension;
42
43         extension = strrchr (orig_filename, '.');
44         if (extension == NULL) {
45                 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
46         } else {
47                 if (g_strcasecmp (extension, ".glabels") != 0) {
48                         new_filename =
49                             g_strconcat (orig_filename, ".glabels", NULL);
50                 } else {
51                         new_filename = g_strdup (orig_filename);
52                 }
53         }
54
55         return new_filename;
56 }
57
58 /****************************************************************************/
59 /* Make sure we have an absolute path to filename.                          */
60 /****************************************************************************/
61 gchar *
62 gl_util_make_absolute (const gchar * filename)
63 {
64         gchar *pwd, *absolute_filename;
65
66         if (g_path_is_absolute (filename)) {
67                 absolute_filename = g_strdup (filename);
68         } else {
69                 pwd = g_get_current_dir ();
70                 absolute_filename =
71                     g_strjoin (G_DIR_SEPARATOR_S, pwd, filename, NULL);
72                 g_free (pwd);
73         }
74
75         return absolute_filename;
76 }
77
78 /****************************************************************************/
79 /* Create fractional representation of number, if possible.                 */
80 /****************************************************************************/
81 gchar *
82 gl_util_fraction( gdouble x )
83 {
84         static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
85         gint i;
86         gdouble product, remainder;
87         gint n, d;
88
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;
93         }
94
95         if ( denom[i] == 0.0 ) {
96                 /* None of our denominators work. */
97                 return g_strdup_printf ("%.5g", x);
98         }
99         if ( denom[i] == 1.0 ) {
100                 /* Simple integer. */
101                 return g_strdup_printf ("%d", (gint)x);
102         }
103         n = (gint)( x * denom[i] + 0.5 );
104         d = (gint)denom[i];
105         if ( n > d ) {
106                 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
107         } else {
108                 return g_strdup_printf ("%d/%d", (n%d), d);
109         }
110 }
111
112 /****************************************************************************/
113 /* Create button w/ both text and stock image.                              */
114 /****************************************************************************/
115 GtkWidget* 
116 gl_button_new_with_stock_image (const gchar* text, const gchar* stock_id)
117 {
118         GtkWidget *button;
119         GtkStockItem item;
120         GtkWidget *label;
121         GtkWidget *image;
122         GtkWidget *hbox;
123         GtkWidget *align;
124
125         button = gtk_button_new ();
126
127         if (GTK_BIN (button)->child)
128                 gtk_container_remove (GTK_CONTAINER (button),
129                                       GTK_BIN (button)->child);
130
131         if (gtk_stock_lookup (stock_id, &item))
132         {
133                 label = gtk_label_new_with_mnemonic (text);
134
135                 gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
136       
137                 image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_BUTTON);
138                 hbox = gtk_hbox_new (FALSE, 2);
139
140                 align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
141       
142                 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
143                 gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
144       
145                 gtk_container_add (GTK_CONTAINER (button), align);
146                 gtk_container_add (GTK_CONTAINER (align), hbox);
147                 gtk_widget_show_all (align);
148
149                 return button;
150         }
151
152         label = gtk_label_new_with_mnemonic (text);
153         gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
154   
155         gtk_misc_set_alignment (GTK_MISC (label), 0.5, 0.5);
156
157         gtk_widget_show (label);
158         gtk_container_add (GTK_CONTAINER (button), label);
159
160         return button;
161 }
162
163 /****************************************************************************/
164 /* Add button to gtk_dialog w/ text and stock image.                        */
165 /****************************************************************************/
166 GtkWidget*
167 gl_util_dialog_add_button (GtkDialog *dialog,
168                            const gchar* text,
169                            const gchar* stock_id,
170                            gint response_id)
171 {
172         GtkWidget *button;
173         
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);
177
178         button = gl_button_new_with_stock_image (text, stock_id);
179         g_return_val_if_fail (button != NULL, NULL);
180
181         GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
182
183         gtk_widget_show (button);
184
185         gtk_dialog_add_action_widget (dialog, button, response_id);     
186
187         return button;
188 }
189
190 /****************************************************************************/
191 /* Utilities to deal with GTK_JUSTIFICATION types.                          */
192 /****************************************************************************/
193 const gchar *
194 gl_util_just_to_string (GtkJustification just)
195 {
196         switch (just) {
197         case GTK_JUSTIFY_LEFT:
198                 return "Left";
199         case GTK_JUSTIFY_CENTER:
200                 return "Center";
201         case GTK_JUSTIFY_RIGHT:
202                 return "Right";
203         default:
204                 return "?";
205         }
206 }
207
208 GtkJustification
209 gl_util_string_to_just (const gchar * string)
210 {
211
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;
218         } else {
219                 return GTK_JUSTIFY_LEFT;
220         }
221
222 }
223
224 /****************************************************************************/
225 /* Utilities to deal with GNOME_FONT_WEIGHT types                           */
226 /****************************************************************************/
227 const gchar *
228 gl_util_weight_to_string (GnomeFontWeight weight)
229 {
230         switch (weight) {
231         case GNOME_FONT_BOOK:
232                 return "Regular";
233         case GNOME_FONT_BOLD:
234                 return "Bold";
235         default:
236                 return "?";
237         }
238 }
239
240 GnomeFontWeight
241 gl_util_string_to_weight (const gchar * string)
242 {
243
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;
248         } else {
249                 return GNOME_FONT_BOOK;
250         }
251
252 }