aboutsummaryrefslogtreecommitdiff
path: root/hildon/hildon-color-button.c
blob: a9430bad9b8a5e4c5eb1b70a92a4a704232a1b3c (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
 * This file is a part of hildon
 *
 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
 *
 * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:hildon-color-button
 * @short_description: A widget to open HildonColorChooserDialog.
 * @see_also: #HildonColorChooserDialog, #HildonColorPopup
 *
 * HildonColorButton is a widget to open a HildonColorChooserDialog.
 * The selected color is shown in the button.
 * The selected color is a property of the button.
 * The property name is "color" and its type is GtkColor.
 * 
 * <example>
 * <title>HildonColorButton example</title>
 * <programlisting>
 * HildonColorButton *cbutton;
 * GtkColor *color;
 * <!-- -->
 * cbutton = hildon_color_button_new();
 * gtk_object_get( GTK_OBJECT(cbutton), "color", color );
 * </programlisting>
 * </example>
 * 
 */

#undef                                          HILDON_DISABLE_DEPRECATED

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

#include                                        <gdk/gdkkeysyms.h>

#include                                        "hildon-color-button.h"
#include                                        "hildon-defines.h"
#include                                        "hildon-color-chooser-dialog.h"
#include                                        "hildon-color-button-private.h"

#define                                         COLOR_BUTTON_WIDTH 80

#define                                         COLOR_BUTTON_HEIGHT 70

#define                                         OUTER_BORDER_RED 0

#define                                         OUTER_BORDER_BLUE 0

#define                                         OUTER_BORDER_GREEN 0

#define                                         OUTER_BORDER_THICKNESS 1

#define                                         INNER_BORDER_RED 65535

#define                                         INNER_BORDER_BLUE 65535

#define                                         INNER_BORDER_GREEN 65535

#define                                         INNER_BORDER_THICKNESS 2

enum
{
    SETUP_DIALOG,
    LAST_SIGNAL
};

enum
{
    PROP_0,
    PROP_COLOR,
    PROP_POPUP_SHOWN
};

static void
hildon_color_button_class_init                  (HildonColorButtonClass *klass);

static void
hildon_color_button_init                        (HildonColorButton *color_button);

static void
hildon_color_button_finalize                    (GObject *object);

static void
hildon_color_button_set_property                (GObject *object, 
                                                 guint param_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec);

static void
hildon_color_button_get_property                (GObject *object, 
                                                 guint param_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec);

static void
hildon_color_button_realize                     (GtkWidget *widget);

static void
hildon_color_button_unrealize                   (GtkWidget *widget);

static void
hildon_color_button_clicked                     (GtkButton *button);

static gboolean
hildon_color_button_key_pressed                 (GtkWidget *button, 
                                                 GdkEventKey *event,
                                                 gpointer data);

static gint
hildon_color_field_expose_event                 (GtkWidget *widget, 
                                                 GdkEventExpose *event,
                                                 HildonColorButton *cb);

static gboolean
hildon_color_button_mnemonic_activate           (GtkWidget *widget,
                                                 gboolean group_cycling);

static void
draw_grid                                       (GdkDrawable *drawable, 
                                                 GdkGC *gc, 
                                                 int x, 
                                                 int y, 
                                                 gint w, 
                                                 gint h);

static gpointer                                 parent_class = NULL;
static guint                                    signals [LAST_SIGNAL] = { 0, };

/**
 * hildon_color_button_get_type:
 *
 * Initializes and returns the type of a hildon color button.
 *
 * Returns: GType of #HildonColorButton.
 */
GType G_GNUC_CONST
hildon_color_button_get_type                    (void)
{
    static GType color_button_type = 0;

    if (! color_button_type)
    {
        static const GTypeInfo color_button_info =
        {
            sizeof (HildonColorButtonClass),
            NULL,           /* base_init */
            NULL,           /* base_finalize */
            (GClassInitFunc) hildon_color_button_class_init,
            NULL,           /* class_finalize */
            NULL,           /* class_data */
            sizeof (HildonColorButton),
            0,              /* n_preallocs */
            (GInstanceInitFunc) hildon_color_button_init,
        };

        color_button_type = g_type_register_static (GTK_TYPE_BUTTON, "HildonColorButton",
                &color_button_info, 0);
    }

    return color_button_type;
}

static void
hildon_color_button_class_init                  (HildonColorButtonClass *klass)
{
    GObjectClass *gobject_class;
    GtkButtonClass *button_class;
    GtkWidgetClass *widget_class;

    gobject_class = G_OBJECT_CLASS (klass);
    button_class = GTK_BUTTON_CLASS (klass);
    widget_class = GTK_WIDGET_CLASS (klass);
    
    parent_class = g_type_class_peek_parent (klass);

    gobject_class->get_property     = hildon_color_button_get_property;
    gobject_class->set_property     = hildon_color_button_set_property;
    gobject_class->finalize         = hildon_color_button_finalize;
    widget_class->realize           = hildon_color_button_realize;
    widget_class->unrealize         = hildon_color_button_unrealize;
    button_class->clicked           = hildon_color_button_clicked;
    widget_class->mnemonic_activate = hildon_color_button_mnemonic_activate;

   signals[SETUP_DIALOG] =
       g_signal_new ("setup-dialog",
                     G_TYPE_FROM_CLASS (klass),
                     G_SIGNAL_RUN_LAST,
                     0,
                     NULL, NULL,
                     g_cclosure_marshal_VOID__OBJECT,
                     G_TYPE_NONE, 1,
                     HILDON_TYPE_COLOR_CHOOSER_DIALOG);

    /**
     * HildonColorButton:color:
     *
     * The currently selected color.
     */
    g_object_class_install_property (gobject_class, PROP_COLOR,
            g_param_spec_boxed ("color",
                "Current Color",
                "The selected color",
                GDK_TYPE_COLOR,
                G_PARAM_READWRITE));

    /**
     * HildonColorButton:popup-shown:
     *
     * If the color selection dialog is currently popped-up (visible)
     */
    g_object_class_install_property (gobject_class, PROP_POPUP_SHOWN,
            g_param_spec_boolean ("popup-shown",
                "IsPopped",
                "If the color selection dialog is popped up",
                FALSE,
                G_PARAM_READABLE));

    g_type_class_add_private (gobject_class, sizeof (HildonColorButtonPrivate));
}

/* FIXME Draw a dotted grid over the specified area to make it look 
 * insensitive. Actually, we should generate that pixbuf once and 
 * just render it over later... */
static void
draw_grid                                       (GdkDrawable *drawable, 
                                                 GdkGC *gc, 
                                                 int x, 
                                                 int y,
                                                 gint w, 
                                                 gint h)
{
    int currentx;
    int currenty;
    for (currenty = y; currenty <= h; currenty++)
        for (currentx = ((currenty % 2 == 0) ? x : x + 1); currentx <= w; currentx += 2)
            gdk_draw_point (drawable, gc, currentx, currenty);
}

/* Handle exposure events for the color picker's drawing area */
static gint
hildon_color_field_expose_event                 (GtkWidget *widget, 
                                                 GdkEventExpose *event,
                                                 HildonColorButton *cb)
{
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
    GdkColor outer_border, inner_border;

    g_assert (priv);

    /* Create the outer border color */
    outer_border.pixel = 0;
    outer_border.red   = OUTER_BORDER_RED;
    outer_border.blue  = OUTER_BORDER_BLUE;
    outer_border.green = OUTER_BORDER_GREEN;

    /* Create the inner border color */
    inner_border.pixel = 0;
    inner_border.red   = INNER_BORDER_RED;
    inner_border.blue  = INNER_BORDER_BLUE;
    inner_border.green = INNER_BORDER_GREEN;

    /* serve the outer border color to the Graphic Context */
    gdk_gc_set_rgb_fg_color (priv->gc, &outer_border);
    /* draw the outer border as a filled rectangle */
    gdk_draw_rectangle (widget->window,
            (GTK_WIDGET_IS_SENSITIVE (widget)) ?  priv->gc : widget->style->bg_gc [GTK_STATE_INSENSITIVE],
            TRUE,
            0, 
            0,
            widget->allocation.width,
            widget->allocation.height);

    /* serve the inner border color to the Graphic Context */
    gdk_gc_set_rgb_fg_color (priv->gc, &inner_border);

    /* draw the inner border as a filled rectangle */
    gdk_draw_rectangle (widget->window,
            priv->gc,
            TRUE,
            OUTER_BORDER_THICKNESS, 
            OUTER_BORDER_THICKNESS,
            widget->allocation.width - (OUTER_BORDER_THICKNESS * 2),
            widget->allocation.height - (OUTER_BORDER_THICKNESS * 2));

    /* serve the actual color to the Graphic Context */
    gdk_gc_set_rgb_fg_color(priv->gc, &priv->color);

    /* draw the actual rectangle */
    gdk_draw_rectangle(widget->window,
            priv->gc,
            TRUE,
            INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
            INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
            widget->allocation.width - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2),
            widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2));

    if (! GTK_WIDGET_IS_SENSITIVE (widget)) {
        draw_grid (GDK_DRAWABLE (widget->window), widget->style->bg_gc [GTK_STATE_INSENSITIVE], 
                INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
                INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
                widget->allocation.width  - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2,
                widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2);
    }

    return FALSE;
}

static void
hildon_color_button_init                        (HildonColorButton *cb)
{
    GtkWidget *align;
    GtkWidget *drawing_area;
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);

    priv->dialog = NULL;
    priv->gc = NULL;
    priv->popped = FALSE;

    gtk_widget_push_composite_child ();

    /* create widgets and pixbuf */
    align = gtk_alignment_new (0.5, 0.5, 0, 0); /* composite widget */

    drawing_area = gtk_drawing_area_new (); /* composite widget */

    /* setting minimum sizes */
    gtk_widget_set_size_request (GTK_WIDGET (cb), COLOR_BUTTON_WIDTH,
            COLOR_BUTTON_HEIGHT);

    gtk_widget_set_size_request (GTK_WIDGET(drawing_area),
                                 HILDON_ICON_PIXEL_SIZE_FINGER,
                                 HILDON_ICON_PIXEL_SIZE_FINGER);

    /* Connect the callback function for exposure event */
    g_signal_connect (drawing_area, "expose-event",
            G_CALLBACK (hildon_color_field_expose_event), cb);

    /* Connect to callback function for key press event */
    g_signal_connect (G_OBJECT(cb), "key-press-event",
            G_CALLBACK(hildon_color_button_key_pressed), cb);

    /* packing */
    gtk_container_add (GTK_CONTAINER (align), drawing_area);
    gtk_container_add (GTK_CONTAINER (cb), align);

    gtk_widget_show_all (align);

    gtk_widget_pop_composite_child ();
}

/* Free memory used by HildonColorButton */
static void
hildon_color_button_finalize                    (GObject *object)
{
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (object);
    g_assert (priv);

    if (priv->dialog)
    {
        gtk_widget_destroy (priv->dialog);
        priv->dialog = NULL;
    }

    if (G_OBJECT_CLASS (parent_class)->finalize)
        G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
hildon_color_button_realize                     (GtkWidget *widget)
{
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
    g_assert (priv);

    GTK_WIDGET_CLASS (parent_class)->realize (widget);

    priv->gc = gdk_gc_new (widget->window);
}

static void
hildon_color_button_unrealize                   (GtkWidget *widget)
{
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
    g_assert (priv);

    if (priv->gc != NULL) { 
        g_object_unref (priv->gc);
        priv->gc = NULL;
    }

    GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

/* Make the widget sensitive with the keyboard event */
static gboolean
hildon_color_button_mnemonic_activate           (GtkWidget *widget,
                                                 gboolean group_cycling)
{
    gtk_widget_grab_focus (widget);
    return TRUE;
}

/* Popup a color selector dialog on button click */
static void
hildon_color_button_clicked                     (GtkButton *button)
{
    HildonColorButton *cb = HILDON_COLOR_BUTTON (button);
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
    HildonColorChooserDialog *cs_dialog;
    
    g_assert (priv);
    
    cs_dialog = (HildonColorChooserDialog *) priv->dialog;

    /* Popup the color selector dialog */
    if (! cs_dialog)
    {
        /* The dialog hasn't been created yet, do it */
        GtkWidget *parent = gtk_widget_get_toplevel (GTK_WIDGET(cb));
        priv->dialog = hildon_color_chooser_dialog_new ();
        cs_dialog = HILDON_COLOR_CHOOSER_DIALOG (priv->dialog);
        if (parent)
            gtk_window_set_transient_for (GTK_WINDOW (cs_dialog), GTK_WINDOW (parent));

        g_signal_emit (button, signals[SETUP_DIALOG], 0, priv->dialog);
    }

    /* Set the initial color for the color selector dialog */
    hildon_color_chooser_dialog_set_color (cs_dialog, &priv->color);

    /* Update the color for color button if selection was made */
    priv->popped = TRUE;
    if (gtk_dialog_run (GTK_DIALOG (cs_dialog)) == GTK_RESPONSE_OK)
    {
        hildon_color_chooser_dialog_get_color (cs_dialog, &priv->color);
        hildon_color_button_set_color (HILDON_COLOR_BUTTON (button), &priv->color);
        // FIXME A queue-draw should be enough here (not set needed)
    } 

    gtk_widget_hide (GTK_WIDGET(cs_dialog));
    priv->popped = FALSE;
}

/* Popup a color selector dialog on hardkey Select press.
 * FIXME This is a bit hacky, should work without thi. Check. */
static gboolean
hildon_color_button_key_pressed                 (GtkWidget *button, 
                                                 GdkEventKey *event,
                                                 gpointer data)
{
    g_return_val_if_fail (HILDON_IS_COLOR_BUTTON (button), FALSE);

    if (event->keyval == HILDON_HARDKEY_SELECT)
    {
        hildon_color_button_clicked (GTK_BUTTON (button));
        return TRUE;
    }

    return FALSE;
}

static void
hildon_color_button_set_property                (GObject *object, 
                                                 guint param_id,
                                                 const GValue *value, 
                                                 GParamSpec *pspec)
{
    HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
    g_assert (priv);

    switch (param_id) 
    {

        case PROP_COLOR:
            priv->color = *(GdkColor *) g_value_get_boxed (value); 
            gtk_widget_queue_draw (GTK_WIDGET (cb));
            break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
            break;
    }
}

static void
hildon_color_button_get_property                (GObject *object, 
                                                 guint param_id,
                                                 GValue *value, 
                                                 GParamSpec *pspec)
{
    HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
    HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
    g_assert (priv);

    switch (param_id) 
    {

        case PROP_COLOR:
            g_value_set_boxed (value, &priv->color);
            break;

        case PROP_POPUP_SHOWN:
            g_value_set_boolean (value, priv->popped);
	    break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
            break;
    }
}

/**
 * hildon_color_button_new:
 *
 * Creates a new color button. This returns a widget in the form of a
 * small button containing a swatch representing the selected color.
 * When the button is clicked, a color-selection dialog will open,
 * allowing the user to select a color. The swatch will be updated to
 * reflect the new color when the user finishes.
 *
 * Returns: a new color button
 */
GtkWidget*
hildon_color_button_new                         (void)
{
    return g_object_new (HILDON_TYPE_COLOR_BUTTON, NULL);
}

/**
 * hildon_color_button_new_with_color:
 * @color: a #GdkColor for the initial color
 *
 * Creates a new color button with @color as the initial color. 
 *
 * Returns: a new color button
 */
GtkWidget*
hildon_color_button_new_with_color              (const GdkColor *color)
{
    return g_object_new (HILDON_TYPE_COLOR_BUTTON, "color", color, NULL);
}

/**
 * hildon_color_button_set_color:
 * @button: a #HildonColorButton
 * @color: a color to be set
 *
 * Sets the color selected by the button.
 */
void
hildon_color_button_set_color                   (HildonColorButton *button, 
                                                 GdkColor *color)
{
    g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));

    g_object_set (G_OBJECT (button), "color", color, NULL);
}

/**
 * hildon_color_button_get_popup_shown
 * @button: a #HildonColorButton
 *
 * This function checks if the color button has the color 
 * selection dialog currently popped-up. 
 * 
 * Returns: TRUE if the dialog is popped-up (visible to user).
 *
 */
gboolean
hildon_color_button_get_popup_shown             (HildonColorButton *button)
{
    HildonColorButtonPrivate *priv = NULL; 
    g_return_val_if_fail (HILDON_IS_COLOR_BUTTON (button), FALSE);

    priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
    g_assert (priv);

    return priv->popped;
}

/**
 * hildon_color_button_popdown
 * @button: a #HildonColorButton
 *
 * If the color selection dialog is currently popped-up (visible)
 * it will be popped-down (hidden).
 *
 */
void
hildon_color_button_popdown                     (HildonColorButton *button)
{
    HildonColorButtonPrivate *priv = NULL; 
    g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));

    priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
    g_assert (priv);

    if (priv->popped && priv->dialog) {
        gtk_dialog_response (GTK_DIALOG (priv->dialog), GTK_RESPONSE_CANCEL);
    }
}

/**
 * hildon_color_button_get_color:
 * @button: a #HildonColorButton
 * @color: a color #GdkColor to be fillled with the current color
 *
 */
void
hildon_color_button_get_color                   (HildonColorButton *button, 
                                                 GdkColor *color)
{
    HildonColorButtonPrivate *priv = NULL; 
    g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
    g_return_if_fail (color != NULL);
   
    priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
    g_assert (priv);

    color->red = priv->color.red;
    color->green = priv->color.green;
    color->blue = priv->color.blue;
    color->pixel = priv->color.pixel;
}