2 * This file is a part of hildon
4 * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
6 * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * SECTION:hildon-color-button
27 * @short_description: A widget to open HildonColorChooserDialog.
28 * @see_also: #HildonColorChooserDialog, #HildonColorPopup
30 * HildonColorButton is a widget to open a HildonColorChooserDialog.
31 * The selected color is shown in the button.
32 * The selected color is a property of the button.
33 * The property name is "color" and its type is GtkColor.
36 * <title>HildonColorButton example</title>
38 * HildonColorButton *cbutton;
41 * cbutton = hildon_color_button_new();
42 * gtk_object_get( GTK_OBJECT(cbutton), "color", color );
52 #include <gdk/gdkkeysyms.h>
54 #include "hildon-color-button.h"
55 #include "hildon-defines.h"
56 #include "hildon-color-chooser-dialog.h"
57 #include "hildon-color-button-private.h"
59 #define COLOR_FILLED_HEIGHT 22
61 #define COLOR_FILLED_WIDTH 22
63 #define COLOR_BUTTON_WIDTH 52
65 #define COLOR_BUTTON_HEIGHT 48
67 #define OUTER_BORDER_RED 0
69 #define OUTER_BORDER_BLUE 0
71 #define OUTER_BORDER_GREEN 0
73 #define OUTER_BORDER_THICKNESS 1
75 #define INNER_BORDER_RED 65535
77 #define INNER_BORDER_BLUE 65535
79 #define INNER_BORDER_GREEN 65535
81 #define INNER_BORDER_THICKNESS 2
97 hildon_color_button_class_init (HildonColorButtonClass *klass);
100 hildon_color_button_init (HildonColorButton *color_button);
103 hildon_color_button_finalize (GObject *object);
106 hildon_color_button_set_property (GObject *object,
112 hildon_color_button_get_property (GObject *object,
118 hildon_color_button_realize (GtkWidget *widget);
121 hildon_color_button_unrealize (GtkWidget *widget);
124 hildon_color_button_clicked (GtkButton *button);
127 hildon_color_button_key_pressed (GtkWidget *button,
132 hildon_color_field_expose_event (GtkWidget *widget,
133 GdkEventExpose *event,
134 HildonColorButton *cb);
137 hildon_color_button_mnemonic_activate (GtkWidget *widget,
138 gboolean group_cycling);
141 draw_grid (GdkDrawable *drawable,
148 static gpointer parent_class = NULL;
149 static guint signals [LAST_SIGNAL] = { 0, };
152 * hildon_color_button_get_type:
154 * Initializes and returns the type of a hildon color button.
156 * @Returns: GType of #HildonColorButton.
159 hildon_color_button_get_type (void)
161 static GType color_button_type = 0;
163 if (! color_button_type)
165 static const GTypeInfo color_button_info =
167 sizeof (HildonColorButtonClass),
168 NULL, /* base_init */
169 NULL, /* base_finalize */
170 (GClassInitFunc) hildon_color_button_class_init,
171 NULL, /* class_finalize */
172 NULL, /* class_data */
173 sizeof (HildonColorButton),
175 (GInstanceInitFunc) hildon_color_button_init,
178 color_button_type = g_type_register_static (GTK_TYPE_BUTTON, "HildonColorButton",
179 &color_button_info, 0);
182 return color_button_type;
186 hildon_color_button_class_init (HildonColorButtonClass *klass)
188 GObjectClass *gobject_class;
189 GtkButtonClass *button_class;
190 GtkWidgetClass *widget_class;
192 gobject_class = G_OBJECT_CLASS (klass);
193 button_class = GTK_BUTTON_CLASS (klass);
194 widget_class = GTK_WIDGET_CLASS (klass);
196 parent_class = g_type_class_peek_parent (klass);
198 gobject_class->get_property = hildon_color_button_get_property;
199 gobject_class->set_property = hildon_color_button_set_property;
200 gobject_class->finalize = hildon_color_button_finalize;
201 widget_class->realize = hildon_color_button_realize;
202 widget_class->unrealize = hildon_color_button_unrealize;
203 button_class->clicked = hildon_color_button_clicked;
204 widget_class->mnemonic_activate = hildon_color_button_mnemonic_activate;
206 signals[SETUP_DIALOG] =
207 g_signal_new ("setup-dialog",
208 G_TYPE_FROM_CLASS (klass),
212 gtk_marshal_VOID__OBJECT,
214 HILDON_TYPE_COLOR_CHOOSER_DIALOG);
217 * HildonColorButton:color:
219 * The currently selected color.
221 g_object_class_install_property (gobject_class, PROP_COLOR,
222 g_param_spec_boxed ("color",
224 "The selected color",
229 * HildonColorButton:popup-shown:
231 * If the color selection dialog is currently popped-up (visible)
233 g_object_class_install_property (gobject_class, PROP_POPUP_SHOWN,
234 g_param_spec_boolean ("popup-shown",
236 "If the color selection dialog is popped up",
240 g_type_class_add_private (gobject_class, sizeof (HildonColorButtonPrivate));
243 /* FIXME Draw a dotted grid over the specified area to make it look
244 * insensitive. Actually, we should generate that pixbuf once and
245 * just render it over later... */
247 draw_grid (GdkDrawable *drawable,
256 for (currenty = y; currenty <= h; currenty++)
257 for (currentx = ((currenty % 2 == 0) ? x : x + 1); currentx <= w; currentx += 2)
258 gdk_draw_point (drawable, gc, currentx, currenty);
261 /* Handle exposure events for the color picker's drawing area */
263 hildon_color_field_expose_event (GtkWidget *widget,
264 GdkEventExpose *event,
265 HildonColorButton *cb)
267 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
268 GdkColor outer_border, inner_border;
272 /* Create the outer border color */
273 outer_border.pixel = 0;
274 outer_border.red = OUTER_BORDER_RED;
275 outer_border.blue = OUTER_BORDER_BLUE;
276 outer_border.green = OUTER_BORDER_GREEN;
278 /* Create the inner border color */
279 inner_border.pixel = 0;
280 inner_border.red = INNER_BORDER_RED;
281 inner_border.blue = INNER_BORDER_BLUE;
282 inner_border.green = INNER_BORDER_GREEN;
284 /* serve the outer border color to the Graphic Context */
285 gdk_gc_set_rgb_fg_color (priv->gc, &outer_border);
286 /* draw the outer border as a filled rectangle */
287 gdk_draw_rectangle (widget->window,
288 (GTK_WIDGET_IS_SENSITIVE (widget)) ? priv->gc : widget->style->bg_gc [GTK_STATE_INSENSITIVE],
292 widget->allocation.width,
293 widget->allocation.height);
295 /* serve the inner border color to the Graphic Context */
296 gdk_gc_set_rgb_fg_color (priv->gc, &inner_border);
298 /* draw the inner border as a filled rectangle */
299 gdk_draw_rectangle (widget->window,
302 OUTER_BORDER_THICKNESS,
303 OUTER_BORDER_THICKNESS,
304 widget->allocation.width - (OUTER_BORDER_THICKNESS * 2),
305 widget->allocation.height - (OUTER_BORDER_THICKNESS * 2));
307 /* serve the actual color to the Graphic Context */
308 gdk_gc_set_rgb_fg_color(priv->gc, &priv->color);
310 /* draw the actual rectangle */
311 gdk_draw_rectangle(widget->window,
314 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
315 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
316 widget->allocation.width - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2),
317 widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2));
319 if (! GTK_WIDGET_IS_SENSITIVE (widget)) {
320 draw_grid (GDK_DRAWABLE (widget->window), widget->style->bg_gc [GTK_STATE_INSENSITIVE],
321 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
322 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
323 widget->allocation.width - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2,
324 widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2);
331 hildon_color_button_init (HildonColorButton *cb)
334 GtkWidget *drawing_area;
335 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
339 priv->popped = FALSE;
341 gtk_widget_push_composite_child ();
343 /* create widgets and pixbuf */
344 align = gtk_alignment_new (0.5, 0.5, 0, 0); /* composite widget */
346 drawing_area = gtk_drawing_area_new (); /* composite widget */
348 /* setting minimum sizes */
349 gtk_widget_set_size_request (GTK_WIDGET (cb), COLOR_BUTTON_WIDTH,
350 COLOR_BUTTON_HEIGHT);
352 gtk_widget_set_size_request (GTK_WIDGET(drawing_area),
353 COLOR_FILLED_WIDTH, COLOR_FILLED_HEIGHT);
355 /* Connect the callback function for exposure event */
356 g_signal_connect (drawing_area, "expose-event",
357 G_CALLBACK (hildon_color_field_expose_event), cb);
359 /* Connect to callback function for key press event */
360 g_signal_connect (G_OBJECT(cb), "key-press-event",
361 G_CALLBACK(hildon_color_button_key_pressed), cb);
364 gtk_container_add (GTK_CONTAINER (align), drawing_area);
365 gtk_container_add (GTK_CONTAINER (cb), align);
367 gtk_widget_show_all (align);
369 gtk_widget_pop_composite_child ();
372 /* Free memory used by HildonColorButton */
374 hildon_color_button_finalize (GObject *object)
376 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (object);
381 gtk_widget_destroy (priv->dialog);
385 if (G_OBJECT_CLASS (parent_class)->finalize)
386 G_OBJECT_CLASS (parent_class)->finalize (object);
390 hildon_color_button_realize (GtkWidget *widget)
392 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
395 GTK_WIDGET_CLASS (parent_class)->realize (widget);
397 priv->gc = gdk_gc_new (widget->window);
401 hildon_color_button_unrealize (GtkWidget *widget)
403 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
406 if (priv->gc != NULL) {
407 g_object_unref (priv->gc);
411 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
414 /* Make the widget sensitive with the keyboard event */
416 hildon_color_button_mnemonic_activate (GtkWidget *widget,
417 gboolean group_cycling)
419 gtk_widget_grab_focus (widget);
423 /* Popup a color selector dialog on button click */
425 hildon_color_button_clicked (GtkButton *button)
427 HildonColorButton *cb = HILDON_COLOR_BUTTON (button);
428 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
429 HildonColorChooserDialog *cs_dialog;
433 cs_dialog = (HildonColorChooserDialog *) priv->dialog;
435 /* Popup the color selector dialog */
438 /* The dialog hasn't been created yet, do it */
439 GtkWidget *parent = gtk_widget_get_toplevel (GTK_WIDGET(cb));
440 priv->dialog = hildon_color_chooser_dialog_new ();
441 cs_dialog = HILDON_COLOR_CHOOSER_DIALOG (priv->dialog);
443 gtk_window_set_transient_for (GTK_WINDOW (cs_dialog), GTK_WINDOW (parent));
445 g_signal_emit (button, signals[SETUP_DIALOG], 0, priv->dialog);
448 /* Set the initial color for the color selector dialog */
449 hildon_color_chooser_dialog_set_color (cs_dialog, &priv->color);
451 /* Update the color for color button if selection was made */
453 if (gtk_dialog_run (GTK_DIALOG (cs_dialog)) == GTK_RESPONSE_OK)
455 hildon_color_chooser_dialog_get_color (cs_dialog, &priv->color);
456 hildon_color_button_set_color (HILDON_COLOR_BUTTON (button), &priv->color);
457 // FIXME A queue-draw should be enough here (not set needed)
460 gtk_widget_hide (GTK_WIDGET(cs_dialog));
461 priv->popped = FALSE;
464 /* Popup a color selector dialog on hardkey Select press.
465 * FIXME This is a bit hacky, should work without thi. Check. */
467 hildon_color_button_key_pressed (GtkWidget *button,
471 g_return_val_if_fail (HILDON_IS_COLOR_BUTTON (button), FALSE);
473 if (event->keyval == HILDON_HARDKEY_SELECT)
475 hildon_color_button_clicked (GTK_BUTTON (button));
483 hildon_color_button_set_property (GObject *object,
488 HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
489 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
496 priv->color = *(GdkColor *) g_value_get_boxed (value);
497 gtk_widget_queue_draw (GTK_WIDGET (cb));
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
507 hildon_color_button_get_property (GObject *object,
512 HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
513 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
520 g_value_set_boxed (value, &priv->color);
523 case PROP_POPUP_SHOWN:
524 g_value_set_boolean (value, priv->popped);
527 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
533 * hildon_color_button_new:
535 * Creates a new color button. This returns a widget in the form of a
536 * small button containing a swatch representing the selected color.
537 * When the button is clicked, a color-selection dialog will open,
538 * allowing the user to select a color. The swatch will be updated to
539 * reflect the new color when the user finishes.
541 * Returns: a new color button
544 hildon_color_button_new (void)
546 return g_object_new (HILDON_TYPE_COLOR_BUTTON, NULL);
550 * hildon_color_button_new_with_color:
551 * @color: a #GdkColor for the initial color
553 * Creates a new color button with @color as the initial color.
555 * Returns: a new color button
558 hildon_color_button_new_with_color (const GdkColor *color)
560 return g_object_new (HILDON_TYPE_COLOR_BUTTON, "color", color, NULL);
564 * hildon_color_button_set_color:
565 * @button: a #HildonColorButton
566 * @color: a color to be set
568 * Sets the color selected by the button.
571 hildon_color_button_set_color (HildonColorButton *button,
574 g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
576 g_object_set (G_OBJECT (button), "color", color, NULL);
580 * hildon_color_button_get_popup_shown
581 * @button: a #HildonColorButton
583 * This function checks if the color button has the color
584 * selection dialog currently popped-up.
586 * Returns: TRUE if the dialog is popped-up (visible to user).
590 hildon_color_button_get_popup_shown (HildonColorButton *button)
592 HildonColorButtonPrivate *priv = NULL;
593 g_return_val_if_fail (HILDON_IS_COLOR_BUTTON (button), FALSE);
595 priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
602 * hildon_color_button_popdown
603 * @button: a #HildonColorButton
605 * If the color selection dialog is currently popped-up (visible)
606 * it will be popped-down (hidden).
610 hildon_color_button_popdown (HildonColorButton *button)
612 HildonColorButtonPrivate *priv = NULL;
613 g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
615 priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
618 if (priv->popped && priv->dialog) {
619 gtk_dialog_response (GTK_DIALOG (priv->dialog), GTK_RESPONSE_CANCEL);
624 * hildon_color_button_get_color:
625 * @button: a #HildonColorButton
626 * @color: a color #GdkColor to be fillled with the current color
630 hildon_color_button_get_color (HildonColorButton *button,
633 HildonColorButtonPrivate *priv = NULL;
634 g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
635 g_return_if_fail (color != NULL);
637 priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
640 color->red = priv->color.red;
641 color->green = priv->color.green;
642 color->blue = priv->color.blue;
643 color->pixel = priv->color.pixel;