aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ephy-gui.c
blob: fe4018d388177cc4b94d9d71aa06f07096168680 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 *  Copyright (C) 2002 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-gui.h"
#include "eel-gconf-extensions.h"

#include <ctype.h>
#include <string.h>
#include <libgnome/gnome-i18n.h>
#include <gtk/gtktreemodel.h>

/* Styles for tab labels */
GtkStyle *loading_text_style = NULL;
GtkStyle *new_text_style = NULL;

/**
 * gul_gui_menu_position_under_widget:
 */
void
ephy_gui_menu_position_under_widget (GtkMenu   *menu,
                     gint      *x,
                     gint      *y,
                     gboolean  *push_in,
                     gpointer   user_data)
{
    GtkWidget *w = GTK_WIDGET (user_data);
    gint width, height;
    gint screen_width, screen_height;
    GtkRequisition requisition;

    gdk_drawable_get_size (w->window, &width, &height);
    gdk_window_get_origin (w->window, x, y);
    *y = *y + height;

    gtk_widget_size_request (GTK_WIDGET (menu), &requisition);

    screen_width = gdk_screen_width ();
    screen_height = gdk_screen_height ();

    *x = CLAMP (*x, 0, MAX (0, screen_width - requisition.width));
    *y = CLAMP (*y, 0, MAX (0, screen_height - requisition.height));
}

/**
 * gul_gui_gtk_radio_button_get: get the active member of a radiobutton
 * group from one of the buttons in the group. This should be in GTK+!
 */
gint
ephy_gui_gtk_radio_button_get (GtkRadioButton *radio_button)
{
    GtkToggleButton *toggle_button;
    gint i, length;
        GSList *list;

    /* get group list */
        list = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button));
        length = g_slist_length (list);

    /* iterate over list to find active button */
    for (i = 0; list != NULL; i++, list = g_slist_next (list))
    {
        /* get button and text */
        toggle_button = GTK_TOGGLE_BUTTON (list->data);
        if (gtk_toggle_button_get_active (toggle_button))
        {
            break;
        }
    }

    /* check we didn't run off end */
    g_assert (list != NULL);

    /* return index (reverse order!) */
    return (length - 1) - i;
}

/**
 * gul_gui_gtk_radio_button_set: set the active member of a radiobutton
 * group from one of the buttons in the group. This should be in GTK+!
 */
void
ephy_gui_gtk_radio_button_set (GtkRadioButton *radio_button, gint index)
{
    GtkToggleButton *button;
    GSList *list;
    gint length;

    /* get the list */
        list = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button));

    /* check out the length */
        length = g_slist_length (list);

        /* new buttons are *preppended* to the list, so button added as first
         * has last position in the list */
        index = (length - 1) - index;

    /* find the right button */
        button = GTK_TOGGLE_BUTTON (g_slist_nth_data (list, index));

    /* set it... this will de-activate the others in the group */
    if (gtk_toggle_button_get_active (button) == FALSE)
    {
        gtk_toggle_button_set_active (button, TRUE);
    }
}

GtkWidget *
ephy_gui_append_new_menuitem (GtkWidget  *menu,
                  const char *mnemonic,
                  GCallback   callback,
                  gpointer    data)
{
    GtkWidget *menu_item;

    menu_item = gtk_menu_item_new_with_mnemonic (mnemonic);
    gtk_widget_show (menu_item);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                   menu_item);

    if (callback)
    {
        g_signal_connect (G_OBJECT (menu_item),
                  "activate",
                  callback, data);
    }

    return menu_item;
}

GtkWidget *
ephy_gui_append_new_menuitem_stock (GtkWidget  *menu,
                    const char *stock_id,
                    GCallback   callback,
                    gpointer    data)
{
    GtkWidget *menu_item;

    menu_item = gtk_image_menu_item_new_from_stock (stock_id, NULL);
    gtk_widget_show (menu_item);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                   menu_item);

    if (callback)
    {
        g_signal_connect (G_OBJECT (menu_item),
                  "activate",
                  callback, data);
    }

    return menu_item;
}

GtkWidget *
ephy_gui_append_new_menuitem_stock_icon (GtkWidget *menu,
                     const char *stock_id,
                     const char *mnemonic,
                     GCallback callback,
                     gpointer data)
{
    GtkWidget *menu_item;
    GtkWidget *image;

    menu_item = gtk_image_menu_item_new_with_mnemonic (mnemonic);
    image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
    gtk_widget_show (image);
    gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), image);

    gtk_widget_show (menu_item);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                   menu_item);

    if (callback)
    {
        g_signal_connect (G_OBJECT (menu_item),
                  "activate",
                  callback, data);
    }

    return menu_item;
}

GtkWidget *
ephy_gui_append_new_check_menuitem (GtkWidget  *menu,
                    const char *mnemonic,
                    gboolean value,
                    GCallback callback,
                    gpointer data)
{
    GtkWidget *menu_item;

    menu_item = gtk_check_menu_item_new_with_mnemonic (mnemonic);
    gtk_widget_show (menu_item);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                   menu_item);

    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item), value);

    if (callback)
    {
        g_signal_connect (G_OBJECT (menu_item),
                  "activate",
                  callback, data);
    }

    return menu_item;
}

GtkWidget *
ephy_gui_append_separator (GtkWidget *menu)
{
    GtkWidget *menu_item;

    menu_item = gtk_menu_item_new ();
    gtk_widget_show (menu_item);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu),
                   menu_item);

    return menu_item;
}

gboolean
ephy_gui_confirm_overwrite_file (GtkWidget *parent, const char *filename)
{
    char *question;
    GtkWidget *dialog;
    gboolean res;

    if (!g_file_test (filename, G_FILE_TEST_EXISTS))
    {
        return TRUE;
    }

    question = g_strdup_printf (_("File %s will be overwritten.\n"
                    "If you choose yes, the contents will be lost.\n\n"
                    "Do you want to continue?"), filename);
    dialog = gtk_message_dialog_new (parent ? GTK_WINDOW(parent) : NULL,
                             GTK_DIALOG_MODAL,
                         GTK_MESSAGE_QUESTION,
                         GTK_BUTTONS_YES_NO,
                         question);
    res = (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES);
    gtk_widget_destroy (dialog);
    g_free (question);

    return res;
}

static guint32
shift_color_component (guchar component, float shift_by)
{
    guint32 result;
    if (shift_by > 1.0) {
        result = component * (2 - shift_by);
    } else {
        result = 0xff - shift_by * (0xff - component);
    }

    return result & 0xff;
}

/**
 * ephy_gui_rgb_shift_color
 * @color: A color.
 * @shift_by: darken or lighten factor.
 * Returns: An darkened or lightened rgb value.
 *
 * Darkens (@shift_by > 1) or lightens (@shift_by < 1)
 * @color.
 */
guint32
ephy_gui_rgb_shift_color (guint32 color, float shift_by)
{
    guint32 result;

    /* shift red by shift_by */
    result = shift_color_component((color & 0x00ff0000) >> 16, shift_by);
    result <<= 8;
    /* shift green by shift_by */
    result |=  shift_color_component((color & 0x0000ff00) >> 8, shift_by);
    result <<= 8;
    /* shift blue by shift_by */
    result |=  shift_color_component((color & 0x000000ff), shift_by);

    /* alpha doesn't change */
    result |= (0xff000000 & color);

    return result;
}

static guint32
rgb16_to_rgb (gushort r, gushort g, gushort b)
{
    guint32 result;

    result = (0xff0000 | (r & 0xff00));
    result <<= 8;
    result |= ((g & 0xff00) | (b >> 8));

    return result;
}

/**
 * ephy_gui_gdk_color_to_rgb
 * @color: A GdkColor style color.
 * Returns: An rgb value.
 *
 * Converts from a GdkColor stlye color to a gdk_rgb one.
 * Alpha gets set to fully opaque
 */
guint32
ephy_gui_gdk_color_to_rgb (const GdkColor *color)
{
    return rgb16_to_rgb (color->red, color->green, color->blue);
}

/**
 * ephy_gui_rgb_to_color
 * @color: a gdk_rgb style value.
 *
 * Converts from a gdk_rgb value style to a GdkColor one.
 * The gdk_rgb color alpha channel is ignored.
 *
 * Return value: A GdkColor structure version of the given RGB color.
 */
GdkColor
ephy_gui_gdk_rgb_to_color (guint32 color)
{
    GdkColor result;

    result.red = ((color >> 16) & 0xFF) * 0x101;
    result.green = ((color >> 8) & 0xFF) * 0x101;
    result.blue = (color & 0xff) * 0x101;
    result.pixel = 0;

    return result;
}