3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
30 #include <libglabels/libglabels.h>
32 #define FRAC_EPSILON 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);
59 /****************************************************************************/
60 /* Remove ".glabels" extension from filename if needed. */
61 /****************************************************************************/
63 gl_util_remove_extension (const gchar *orig_filename)
65 gchar *new_filename, *extension;
67 new_filename = g_strdup (orig_filename);
69 extension = strrchr (new_filename, '.');
70 if (extension != NULL) {
71 if (g_strcasecmp (extension, ".glabels") == 0) {
72 *extension = 0; /* truncate string, rm extension */
80 /****************************************************************************/
81 /* Make sure we have an absolute path to filename. */
82 /****************************************************************************/
84 gl_util_make_absolute (const gchar *filename)
86 gchar *pwd, *absolute_filename;
88 if (g_path_is_absolute (filename)) {
89 absolute_filename = g_strdup (filename);
91 pwd = g_get_current_dir ();
92 absolute_filename = g_build_filename (pwd, filename, NULL);
96 return absolute_filename;
100 /****************************************************************************/
101 /* Create fractional representation of number, if possible. */
102 /****************************************************************************/
104 gl_util_fraction (gdouble x)
106 static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
108 gdouble product, remainder;
111 for ( i=0; denom[i] != 0.0; i++ ) {
112 product = x * denom[i];
113 remainder = fabs(product - ((gint)(product+0.5)));
114 if ( remainder < FRAC_EPSILON ) break;
117 if ( denom[i] == 0.0 ) {
118 /* None of our denominators work. */
119 return g_strdup_printf ("%.5g", x);
121 if ( denom[i] == 1.0 ) {
122 /* Simple integer. */
123 return g_strdup_printf ("%d", (gint)x);
125 n = (gint)( x * denom[i] + 0.5 );
128 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
130 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:
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;
171 /****************************************************************************/
172 /* Utilities to deal with PangoWeight types */
173 /****************************************************************************/
175 gl_util_weight_to_string (PangoWeight weight)
178 case PANGO_WEIGHT_NORMAL:
180 case PANGO_WEIGHT_BOLD:
189 gl_util_string_to_weight (const gchar *string)
192 if (g_strcasecmp (string, "Regular") == 0) {
193 return PANGO_WEIGHT_NORMAL;
194 } else if (g_strcasecmp (string, "Bold") == 0) {
195 return PANGO_WEIGHT_BOLD;
197 return PANGO_WEIGHT_NORMAL;
203 /****************************************************************************/
204 /* Convienience function to set strings in a text combo_box from a GList */
205 /****************************************************************************/
207 gl_util_combo_box_set_strings (GtkComboBox *combo,
213 g_return_if_fail (list);
215 model = gtk_combo_box_get_model(combo);
216 gtk_list_store_clear (GTK_LIST_STORE (model));
218 for (p=list; p!=NULL; p=p->next) {
220 gtk_combo_box_append_text (combo, p->data);
226 /*---------------------------------------------------------------------------*/
227 /* PRIVATE. gl_util_combo_box_set_active_text support. */
228 /*---------------------------------------------------------------------------*/
237 search_text_func (GtkTreeModel *model,
242 TextSearchData *search_data = (TextSearchData *)data;
245 gtk_tree_model_get (model, iter, 0, &text, -1);
247 if (strcmp (text,search_data->text) == 0) {
248 search_data->found = TRUE;
249 search_data->iter = *iter;
258 /****************************************************************************/
259 /* Convienience function to set active text in a text combo_box from text */
260 /****************************************************************************/
262 gl_util_combo_box_set_active_text (GtkComboBox *combo,
265 GtkTreeModel *model = gtk_combo_box_get_model(combo);
267 g_return_if_fail (GTK_IS_LIST_STORE (model));
271 gtk_combo_box_set_active (combo, -1);
274 TextSearchData search_data;
276 search_data.text = text;
277 search_data.found = FALSE;
279 gtk_tree_model_foreach (model, search_text_func, &search_data);
280 if (search_data.found) {
281 gtk_combo_box_set_active_iter (combo,
284 gtk_combo_box_set_active (combo, -1);
292 /****************************************************************************/
293 /* Convienience function to add a simple text model to an existing */
294 /* combo_box. This is needed since combo_boxes created with glade do not */
295 /* use the gtk_combo_box_new_text() constructor. */
296 /****************************************************************************/
298 gl_util_combo_box_add_text_model (GtkComboBox *combo)
300 GtkCellRenderer *cell;
303 store = gtk_list_store_new (1, G_TYPE_STRING);
304 gtk_combo_box_set_model (combo, GTK_TREE_MODEL (store));
305 g_object_unref (store);
307 cell = gtk_cell_renderer_text_new ();
308 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
309 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
315 /****************************************************************************/
316 /* Get widgets from GtkBuilder "en masse." */
317 /****************************************************************************/
318 void gl_util_get_builder_widgets (GtkBuilder *builder,
324 GtkWidget **p_widget;
326 va_start (args, first_name);
328 for ( name = first_name; name; name = va_arg (args, gchar *) )
330 p_widget = va_arg (args, GtkWidget **);
332 *p_widget = GTK_WIDGET (gtk_builder_get_object (builder, name));
336 g_critical ("Could not load widget \"%s\".\n\ngLabels may not be installed correctly!",
348 * Local Variables: -- emacs
350 * c-basic-offset: 8 -- emacs
351 * tab-width: 8 -- emacs
352 * indent-tabs-mode: nil -- emacs