aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-color-chooser-widget.c
blob: 5761ebf2ffd8a78e761be4b291b84a2b3f6b773e (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

/* e-color-chooser-widget.c
 *
 * Copyright (C) 2012 Dan Vrátil <dvratil@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * 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 Lesser 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "e-color-chooser-widget.h"

#include <glib/gi18n-lib.h>

#define E_COLOR_CHOOSER_WIDGET_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_COLOR_CHOOSER_WIDGET, EColorChooserWidgetPrivate))

/**
 * EColorChooserWidget:
 *
 * This widget wrapps around #GtkColorChooserWidget and allows the widget to be
 * used as a delegate for #GtkComboBox for instance.
 */

struct _EColorChooserWidgetPrivate {
    gboolean showing_editor;
};

enum {
    SIGNAL_EDITOR_ACTIVATED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

G_DEFINE_TYPE (
    EColorChooserWidget,
    e_color_chooser_widget,
    GTK_TYPE_COLOR_CHOOSER_WIDGET);

/* UGLY UGLY UGLY!
 * GtkColorChooserWidget sends "color-activated" signal
 * only when user double-clicks the color. This is totally stupid
 * and since we want to use it in a combobox-like widget, we need
 * to be notified upon single click (which by default only selects the color).
 *
 * Unfortunatelly the GtkColorSwatch widget, which handles the button-press
 * event is a non-public widget embedded within the GtkColorChooserWidget,
 * so we can't just subclass it and fix the behavior.
 *
 * Here we override button_press_event of the GtkColorSwatch and manually
 * emit the 'activate' signal on single click. This is stupid, ugly and I
 * want to punch someone for such a stupid design...
 */
static gboolean
color_chooser_widget_button_press_event (GtkWidget *widget,
                                         GdkEventButton *event)
{
    if ((event->type == GDK_BUTTON_PRESS) &&
        (event->button == GDK_BUTTON_PRIMARY)) {

        g_signal_emit_by_name (widget, "activate");

        return TRUE;
    }

    return FALSE;
}

static void
color_chooser_widget_color_activated (GtkColorChooser *chooser,
                                      GdkRGBA *color,
                                      gpointer user_data)
{
    /* Because we are simulating the double-click by accepting only
     * single click, the color in the swatch is actually not selected,
     * so we must do it manually */
    gtk_color_chooser_set_rgba (chooser, color);
}

static gboolean
run_color_chooser_dialog (gpointer user_data)
{
    EColorChooserWidgetPrivate *priv;
    GtkWidget *parent_window;
    GtkWidget *parent_chooser;
    GtkWidget *dialog;
    GtkWidget *chooser;

    parent_chooser = user_data;

    g_object_set (
        G_OBJECT (parent_chooser), "show-editor", FALSE, NULL);

    parent_window = g_object_get_data (G_OBJECT (parent_chooser), "window");
    if (!parent_window)
        parent_window = gtk_widget_get_toplevel (parent_chooser);
    dialog = gtk_dialog_new_with_buttons (
        N_("Choose custom color"),
        GTK_WINDOW (parent_window),
        GTK_DIALOG_MODAL,
        GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
        GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);

    chooser = gtk_color_chooser_widget_new ();
    g_object_set (G_OBJECT (chooser), "show-editor", TRUE, NULL);
    gtk_box_pack_start (
        GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
        chooser, TRUE, TRUE, 5);

    gtk_widget_show_all (chooser);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
        GdkRGBA color;

        gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (chooser), &color);
        gtk_color_chooser_set_rgba (GTK_COLOR_CHOOSER (parent_chooser), &color);

        g_signal_emit_by_name (parent_chooser, "color-activated", &color);
    }

    gtk_widget_destroy (dialog);

    priv = E_COLOR_CHOOSER_WIDGET_GET_PRIVATE (parent_chooser);
    priv->showing_editor = FALSE;

    return FALSE;
}

static void
color_chooser_show_editor_notify_cb (EColorChooserWidget *chooser,
                                     GParamSpec *pspec,
                                     gpointer user_data)
{
    gboolean show_editor;

    g_object_get (G_OBJECT (chooser), "show-editor", &show_editor, NULL);

    /* Nothing to do here... */
    if ((show_editor == FALSE) || (chooser->priv->showing_editor == TRUE))
        return;

    chooser->priv->showing_editor = TRUE;

    /* Hide the editor - we don't want to display the single-color editor
     * within this widget. We rather create a dialog window with the editor
     * (we can't do it from this callback as Gtk would stop it in order to
     * prevent endless recursion probably) */
    g_idle_add (run_color_chooser_dialog, chooser);
    g_signal_emit (chooser, signals[SIGNAL_EDITOR_ACTIVATED], 0);
}

void
e_color_chooser_widget_class_init (EColorChooserWidgetClass *class)
{
    g_type_class_add_private (class, sizeof (EColorChooserWidgetPrivate));

    signals[SIGNAL_EDITOR_ACTIVATED] = g_signal_new (
        "editor-activated",
        E_TYPE_COLOR_CHOOSER_WIDGET,
        G_SIGNAL_RUN_FIRST,
        G_STRUCT_OFFSET (EColorChooserWidgetClass, editor_activated),
        NULL,
        NULL,
        g_cclosure_marshal_VOID__VOID,
        G_TYPE_NONE, 0);
}

/* Recursively go through GtkContainers within the GtkColorChooserWidget
 * and try to find GtkColorSwatch widget. */
static GtkWidget *
find_swatch (GtkContainer *container)
{
    GList *children, *child;

    children = gtk_container_get_children (container);
    for (child = children; child; child = g_list_next (child)) {
        GtkWidget *widget = child->data;
        GtkWidget *swatch;

        if (GTK_IS_CONTAINER (widget)) {
            swatch = find_swatch (GTK_CONTAINER (widget));

            if (swatch != NULL) {
                g_list_free (children);
                return swatch;
            }
        }

        if (g_strcmp0 (G_OBJECT_TYPE_NAME (widget), "GtkColorSwatch") == 0) {
            g_list_free (children);
            return widget;
        }
    }

    g_list_free (children);

    return NULL;
}

void
e_color_chooser_widget_init (EColorChooserWidget *widget)
{
    GtkWidget *swatch;

    widget->priv = E_COLOR_CHOOSER_WIDGET_GET_PRIVATE (widget);
    widget->priv->showing_editor = FALSE;

    swatch = find_swatch (GTK_CONTAINER (widget));

    /* If swatch is NULL then GTK changed something and this widget
     * becomes broken... */
    g_return_if_fail (swatch != NULL);

    if (swatch) {
        GtkWidgetClass *swatch_class;
        swatch_class = GTK_WIDGET_GET_CLASS (swatch);
        swatch_class->button_press_event = color_chooser_widget_button_press_event;
    }

    g_signal_connect (
        widget, "color-activated",
        G_CALLBACK (color_chooser_widget_color_activated), NULL);

    g_signal_connect (
        widget, "notify::show-editor",
        G_CALLBACK (color_chooser_show_editor_notify_cb), NULL);
}

GtkWidget *
e_color_chooser_widget_new (void)
{
    return g_object_new (
        E_TYPE_COLOR_CHOOSER_WIDGET,
        "show-editor", FALSE,
        "use-alpha", FALSE,
        NULL);
}