aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-prefs-utils.c
blob: 0e0cf4a50c6ded7f6af0c32e6316e93c1c46512f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 *  Copyright (C) 2000 Marco Pesenti Gritti
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "ephy-prefs-utils.h"
#include "ephy-gui.h"
#include "eel-gconf-extensions.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenushell.h>
#include <gtk/gtkspinbutton.h>
#include <gtk/gtktogglebutton.h>
#include <gtk/gtkoptionmenu.h>
#include <gtk/gtklist.h>
#include <libgnomeui/gnome-color-picker.h>

void
ephy_pu_set_config_from_editable (GtkWidget *editable, const char *config_name)
{
    GConfValue *gcvalue = eel_gconf_get_value (config_name);
    GConfValueType value_type;
    char *value;
    gint ivalue;
    gfloat fvalue;

    if (gcvalue == NULL) {
        /* ugly hack around what appears to be a gconf bug
         * it returns a NULL GConfValue for a valid string pref
         * which is "" by default */
        value_type = GCONF_VALUE_STRING;
    } else {
        value_type = gcvalue->type;
        gconf_value_free (gcvalue);
    }

    /* get all the text into a new string */
    value = gtk_editable_get_chars (GTK_EDITABLE(editable), 0, -1);

    switch (value_type) {
    case GCONF_VALUE_STRING:
        eel_gconf_set_string (config_name,
                      value);
        break;
    /* FIXME : handle possible errors in the input for int and float */
    case GCONF_VALUE_INT:
        ivalue = atoi (value);
        eel_gconf_set_integer (config_name, ivalue);
        break;
    case GCONF_VALUE_FLOAT:
        fvalue = strtod (value, (char**)NULL);
        eel_gconf_set_float (config_name, fvalue);
        break;
    default:
        break;
    }

    /* free the allocated strings */
    g_free (value);
}

void
ephy_pu_set_config_from_optionmenu (GtkWidget *optionmenu, const char *config_name)
{
    int index = ephy_pu_get_int_from_optionmenu (optionmenu);

    eel_gconf_set_integer (config_name, index);
}

void
ephy_pu_set_config_from_radiobuttongroup (GtkWidget *radiobutton, const char *config_name)
{
    gint index;

    /* get value from radio button group */
    index = ephy_gui_gtk_radio_button_get (GTK_RADIO_BUTTON (radiobutton));

    eel_gconf_set_integer (config_name, index);
}

void
ephy_pu_set_config_from_spin_button (GtkWidget *spinbutton, const char *config_name)
{
    gdouble value;
    gboolean use_int;

    /* read the value as an integer */
    value = gtk_spin_button_get_value (GTK_SPIN_BUTTON(spinbutton));

    use_int = (gtk_spin_button_get_digits (GTK_SPIN_BUTTON(spinbutton)) == 0);

    if (use_int)
    {
        eel_gconf_set_integer (config_name, value);
    }
    else
    {
        eel_gconf_set_float (config_name, value);
    }
}

void
ephy_pu_set_config_from_togglebutton (GtkWidget *togglebutton, const char *config_name)
{
    gboolean value;

    /* read the value */
    value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(togglebutton));

    eel_gconf_set_boolean (config_name, value);
}

void
ephy_pu_set_config_from_color (GtkWidget *colorpicker, const char *config_name)
{
    guint8 r, g, b, a;
    gchar color_string[9];

    /* get color values from color picker */
    gnome_color_picker_get_i8 (GNOME_COLOR_PICKER (colorpicker),
                   &r, &g, &b, &a);

    /* write into string (bounded size) */
    snprintf (color_string, 9, "#%02X%02X%02X", r, g, b);

    /* set the configuration value */
    eel_gconf_set_string (config_name, color_string);
}

void
ephy_pu_set_editable_from_config (GtkWidget *editable, const char *config_name)
{
    GConfValue *gcvalue = eel_gconf_get_value (config_name);
    GConfValueType value_type;
    gchar *value;

    if (gcvalue == NULL)
    {
        /* ugly hack around what appears to be a gconf bug
         * it returns a NULL GConfValue for a valid string pref
         * which is "" by default */
        value_type = GCONF_VALUE_STRING;
    }
    else
    {
        value_type = gcvalue->type;
        gconf_value_free (gcvalue);
    }

    switch (value_type)
    {
    case GCONF_VALUE_STRING:
        value = eel_gconf_get_string (config_name);
        break;
    case GCONF_VALUE_INT:
        value = g_strdup_printf ("%d",eel_gconf_get_integer (config_name));
        break;
    case GCONF_VALUE_FLOAT:
        value = g_strdup_printf ("%.2f",eel_gconf_get_float (config_name));
        break;
    default:
        value = NULL;
    }

    /* set this string value in the widget */
    if (value)
    {
        gtk_entry_set_text(GTK_ENTRY(editable), value);
    }

    /* free the allocated string */
    g_free (value);
}

void
ephy_pu_set_optionmenu_from_config (GtkWidget *optionmenu, const char *config_name)
{
    gint index;

    /* get the current value from the configuration space */
    index = eel_gconf_get_integer (config_name);

    /* set this option value in the widget */
    gtk_option_menu_set_history (GTK_OPTION_MENU (optionmenu), index);
}

void
ephy_pu_set_radiobuttongroup_from_config (GtkWidget *radiobutton, const char *config_name)
{
    gint index;

        /* get the current value from the configuration space */
        index = eel_gconf_get_integer (config_name);

    /* set it (finds the group for us) */
    ephy_gui_gtk_radio_button_set (GTK_RADIO_BUTTON (radiobutton), index);
}

void
ephy_pu_set_spin_button_from_config (GtkWidget *spinbutton, const char *config_name)
{
    gdouble value;
    gint use_int;

    use_int = (gtk_spin_button_get_digits (GTK_SPIN_BUTTON(spinbutton)) == 0);

    if (use_int)
    {
        /* get the current value from the configuration space */
        value = eel_gconf_get_integer (config_name);
    }
    else
    {
        /* get the current value from the configuration space */
        value = eel_gconf_get_float (config_name);
    }

    /* set this option value in the widget */
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton), value);
}

void
ephy_pu_set_togglebutton_from_config (GtkWidget *togglebutton, const char *config_name)
{
    gboolean value;

    /* get the current value from the configuration space */
    value = eel_gconf_get_boolean (config_name);

    /* set this option value in the widget */
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (togglebutton), value);
}

void
ephy_pu_set_color_from_config (GtkWidget *colorpicker, const char *config_name)
{
    gchar *color_string;
    guint r, g, b;

    /* get the string from config */
    color_string = eel_gconf_get_string (config_name);

    if (color_string)
    {
        /* parse it and setup the color picker */
        sscanf (color_string, "#%2X%2X%2X", &r, &g, &b);
        gnome_color_picker_set_i8 (GNOME_COLOR_PICKER (colorpicker),
                       r, g, b, 0);
        /* free the string */
        g_free (color_string);
    }
}

int
ephy_pu_get_int_from_optionmenu (GtkWidget *optionmenu)
{
        GtkWidget *menu;
        GList *list;
        gpointer item;
        gint index;

        /* extract the selection */
        menu = GTK_OPTION_MENU(optionmenu)->menu;
        list = GTK_MENU_SHELL(menu)->children;
        item = gtk_menu_get_active (GTK_MENU(menu));
        index = g_list_index (list, item);

        return index;
}