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
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 "hildon-color-button.h"
53 #include <gtk/gtkbutton.h>
54 #include <gtk/gtkalignment.h>
55 #include <gtk/gtkdrawingarea.h>
56 #include <gtk/gtksignal.h>
57 #include <gdk/gdkkeysyms.h>
58 #include "hildon-defines.h"
59 #include "hildon-color-chooser-dialog.h"
60 #include "hildon-color-button-private.h"
62 #define COLOR_FILLED_HEIGHT 22
64 #define COLOR_FILLED_WIDTH 22
66 #define COLOR_BUTTON_WIDTH 52
68 #define COLOR_BUTTON_HEIGHT 48
70 #define OUTER_BORDER_RED 0
72 #define OUTER_BORDER_BLUE 0
74 #define OUTER_BORDER_GREEN 0
76 #define OUTER_BORDER_THICKNESS 1
78 #define INNER_BORDER_RED 65535
80 #define INNER_BORDER_BLUE 65535
82 #define INNER_BORDER_GREEN 65535
84 #define INNER_BORDER_THICKNESS 2
93 hildon_color_button_class_init (HildonColorButtonClass *klass);
96 hildon_color_button_init (HildonColorButton *color_button);
99 hildon_color_button_finalize (GObject *object);
102 hildon_color_button_set_property (GObject *object,
108 hildon_color_button_get_property (GObject *object,
114 hildon_color_button_realize (GtkWidget *widget);
117 hildon_color_button_unrealize (GtkWidget *widget);
120 hildon_color_button_clicked (GtkButton *button);
123 hildon_color_button_key_pressed (GtkWidget *button,
128 hildon_color_field_expose_event (GtkWidget *widget,
129 GdkEventExpose *event,
130 HildonColorButton *cb);
133 hildon_color_button_mnemonic_activate (GtkWidget *widget,
134 gboolean group_cycling);
137 draw_grid (GdkDrawable *drawable,
144 static gpointer parent_class = NULL;
147 hildon_color_button_get_type (void)
149 static GType color_button_type = 0;
151 if (! color_button_type)
153 static const GTypeInfo color_button_info =
155 sizeof (HildonColorButtonClass),
156 NULL, /* base_init */
157 NULL, /* base_finalize */
158 (GClassInitFunc) hildon_color_button_class_init,
159 NULL, /* class_finalize */
160 NULL, /* class_data */
161 sizeof (HildonColorButton),
163 (GInstanceInitFunc) hildon_color_button_init,
166 color_button_type = g_type_register_static (GTK_TYPE_BUTTON, "HildonColorButton",
167 &color_button_info, 0);
170 return color_button_type;
174 hildon_color_button_class_init (HildonColorButtonClass *klass)
176 GObjectClass *gobject_class;
177 GtkButtonClass *button_class;
178 GtkWidgetClass *widget_class;
180 gobject_class = G_OBJECT_CLASS (klass);
181 button_class = GTK_BUTTON_CLASS (klass);
182 widget_class = GTK_WIDGET_CLASS (klass);
184 parent_class = g_type_class_peek_parent (klass);
186 gobject_class->get_property = hildon_color_button_get_property;
187 gobject_class->set_property = hildon_color_button_set_property;
188 gobject_class->finalize = hildon_color_button_finalize;
189 widget_class->realize = hildon_color_button_realize;
190 widget_class->unrealize = hildon_color_button_unrealize;
191 button_class->clicked = hildon_color_button_clicked;
192 widget_class->mnemonic_activate = hildon_color_button_mnemonic_activate;
195 * HildonColorButton:color:
197 * The currently selected color.
199 g_object_class_install_property (gobject_class, PROP_COLOR,
200 g_param_spec_boxed ("color",
202 "The selected color",
206 g_type_class_add_private (gobject_class, sizeof (HildonColorButtonPrivate));
209 /* FIXME Draw a dotted grid over the specified area to make it look
210 * insensitive. Actually, we should generate that pixbuf once and
211 * just render it over later... */
213 draw_grid (GdkDrawable *drawable,
222 for (currenty = y; currenty <= h; currenty++)
223 for (currentx = ((currenty % 2 == 0) ? x : x + 1); currentx <= w; currentx += 2)
224 gdk_draw_point (drawable, gc, currentx, currenty);
227 /* Handle exposure events for the color picker's drawing area */
229 hildon_color_field_expose_event (GtkWidget *widget,
230 GdkEventExpose *event,
231 HildonColorButton *cb)
233 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
234 GdkColor outer_border, inner_border;
238 /* Create the outer border color */
239 outer_border.pixel = 0;
240 outer_border.red = OUTER_BORDER_RED;
241 outer_border.blue = OUTER_BORDER_BLUE;
242 outer_border.green = OUTER_BORDER_GREEN;
244 /* Create the inner border color */
245 inner_border.pixel = 0;
246 inner_border.red = INNER_BORDER_RED;
247 inner_border.blue = INNER_BORDER_BLUE;
248 inner_border.green = INNER_BORDER_GREEN;
250 /* serve the outer border color to the Graphic Context */
251 gdk_gc_set_rgb_fg_color (priv->gc, &outer_border);
252 /* draw the outer border as a filled rectangle */
253 gdk_draw_rectangle (widget->window,
254 (GTK_WIDGET_IS_SENSITIVE (widget)) ? priv->gc : widget->style->bg_gc [GTK_STATE_INSENSITIVE],
258 widget->allocation.width,
259 widget->allocation.height);
261 /* serve the inner border color to the Graphic Context */
262 gdk_gc_set_rgb_fg_color (priv->gc, &inner_border);
264 /* draw the inner border as a filled rectangle */
265 gdk_draw_rectangle (widget->window,
268 OUTER_BORDER_THICKNESS,
269 OUTER_BORDER_THICKNESS,
270 widget->allocation.width - (OUTER_BORDER_THICKNESS * 2),
271 widget->allocation.height - (OUTER_BORDER_THICKNESS * 2));
273 /* serve the actual color to the Graphic Context */
274 gdk_gc_set_rgb_fg_color(priv->gc, &priv->color);
276 /* draw the actual rectangle */
277 gdk_draw_rectangle(widget->window,
280 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
281 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
282 widget->allocation.width - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2),
283 widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2));
285 if (! GTK_WIDGET_IS_SENSITIVE (widget)) {
286 draw_grid (GDK_DRAWABLE (widget->window), widget->style->bg_gc [GTK_STATE_INSENSITIVE],
287 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
288 INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS,
289 widget->allocation.width - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2,
290 widget->allocation.height - ((INNER_BORDER_THICKNESS + OUTER_BORDER_THICKNESS)*2) + 2);
297 hildon_color_button_init (HildonColorButton *cb)
300 GtkWidget *drawing_area;
301 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
306 gtk_widget_push_composite_child ();
308 /* create widgets and pixbuf */
309 align = gtk_alignment_new (0.5, 0.5, 0, 0); /* composite widget */
311 drawing_area = gtk_drawing_area_new (); /* composite widget */
313 /* setting minimum sizes */
314 gtk_widget_set_size_request (GTK_WIDGET (cb), COLOR_BUTTON_WIDTH,
315 COLOR_BUTTON_HEIGHT);
317 gtk_widget_set_size_request (GTK_WIDGET(drawing_area),
318 COLOR_FILLED_WIDTH, COLOR_FILLED_HEIGHT);
320 /* Connect the callback function for exposure event */
321 g_signal_connect (drawing_area, "expose-event",
322 G_CALLBACK (hildon_color_field_expose_event), cb);
324 /* Connect to callback function for key press event */
325 g_signal_connect (G_OBJECT(cb), "key-press-event",
326 G_CALLBACK(hildon_color_button_key_pressed), cb);
329 gtk_container_add (GTK_CONTAINER (align), drawing_area);
330 gtk_container_add (GTK_CONTAINER (cb), align);
332 gtk_widget_show_all (align);
334 gtk_widget_pop_composite_child ();
337 /* Free memory used by HildonColorButton */
339 hildon_color_button_finalize (GObject *object)
341 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (object);
346 gtk_widget_destroy(priv->dialog);
350 if (G_OBJECT_CLASS (parent_class)->finalize)
351 G_OBJECT_CLASS (parent_class)->finalize (object);
355 hildon_color_button_realize (GtkWidget *widget)
357 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
360 GTK_WIDGET_CLASS (parent_class)->realize (widget);
362 priv->gc = gdk_gc_new (widget->window);
366 hildon_color_button_unrealize (GtkWidget *widget)
368 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (widget);
371 if (priv->gc != NULL) {
372 g_object_unref (priv->gc);
376 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
379 /* Make the widget sensitive with the keyboard event */
381 hildon_color_button_mnemonic_activate (GtkWidget *widget,
382 gboolean group_cycling)
384 gtk_widget_grab_focus (widget);
388 /* Popup a color selector dialog on button click */
390 hildon_color_button_clicked (GtkButton *button)
392 HildonColorButton *cb = HILDON_COLOR_BUTTON (button);
393 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
394 HildonColorChooserDialog *cs_dialog;
398 cs_dialog = (HildonColorChooserDialog *) priv->dialog;
400 /* Popup the color selector dialog */
403 /* The dialog hasn't been created yet, do it */
404 GtkWidget *parent = gtk_widget_get_toplevel (GTK_WIDGET(cb));
405 priv->dialog = hildon_color_chooser_dialog_new ();
406 cs_dialog = HILDON_COLOR_CHOOSER_DIALOG (priv->dialog);
408 gtk_window_set_transient_for (GTK_WINDOW (cs_dialog), GTK_WINDOW (parent));
411 /* Set the initial color for the color selector dialog */
412 hildon_color_chooser_dialog_set_color (cs_dialog, &priv->color);
414 /* Update the color for color button if selection was made */
415 if (gtk_dialog_run (GTK_DIALOG (cs_dialog)) == GTK_RESPONSE_OK)
417 hildon_color_chooser_dialog_get_color (cs_dialog, &priv->color);
418 hildon_color_button_set_color (HILDON_COLOR_BUTTON (button), &priv->color);
419 // FIXME A queue-draw should be enough here (not set needed)
422 gtk_widget_hide (GTK_WIDGET(cs_dialog));
425 /* Popup a color selector dialog on hardkey Select press.
426 * FIXME This is a bit hacky, should work without thi. Check. */
428 hildon_color_button_key_pressed (GtkWidget *button,
432 g_return_val_if_fail (HILDON_IS_COLOR_BUTTON (button), FALSE);
434 if (event->keyval == HILDON_HARDKEY_SELECT)
436 hildon_color_button_clicked (GTK_BUTTON (button));
444 hildon_color_button_set_property (GObject *object,
449 HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
450 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
457 priv->color = *(GdkColor *) g_value_get_boxed (value);
458 gtk_widget_queue_draw (GTK_WIDGET (cb));
462 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
468 hildon_color_button_get_property (GObject *object,
473 HildonColorButton *cb = HILDON_COLOR_BUTTON (object);
474 HildonColorButtonPrivate *priv = HILDON_COLOR_BUTTON_GET_PRIVATE (cb);
481 g_value_set_boxed (value, &priv->color);
485 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
491 * hildon_color_button_new:
493 * Creates a new color button. This returns a widget in the form of a
494 * small button containing a swatch representing the selected color.
495 * When the button is clicked, a color-selection dialog will open,
496 * allowing the user to select a color. The swatch will be updated to
497 * reflect the new color when the user finishes.
499 * Returns: a new color button
502 hildon_color_button_new (void)
504 return g_object_new (HILDON_TYPE_COLOR_BUTTON, NULL);
508 * hildon_color_button_new_with_color:
509 * @color: a #GdkColor for the initial color
511 * Creates a new color button with @color as the initial color.
513 * Returns: a new color button
516 hildon_color_button_new_with_color (const GdkColor *color)
518 return g_object_new (HILDON_TYPE_COLOR_BUTTON, "color", color, NULL);
522 * hildon_color_button_set_color:
523 * @button: a #HildonColorButton
524 * @color: a color to be set
526 * Sets the color selected by the button.
529 hildon_color_button_set_color (HildonColorButton *button,
532 g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
534 g_object_set (G_OBJECT (button), "color", color, NULL);
538 * hildon_color_button_get_color:
539 * @button: a #HildonColorButton
540 * @color: a color #GdkColor to be fillled with the current color
544 hildon_color_button_get_color (HildonColorButton *button,
547 HildonColorButtonPrivate *priv = NULL;
548 g_return_if_fail (HILDON_IS_COLOR_BUTTON (button));
549 g_return_if_fail (color != NULL);
551 priv = HILDON_COLOR_BUTTON_GET_PRIVATE (button);
554 color->red = priv->color.red;
555 color->green = priv->color.green;
556 color->blue = priv->color.blue;
557 color->pixel = priv->color.pixel;