2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@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; either 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 * @file hildon-color-popup.c
28 * This file contains the implementation of Hildon Color Selector Popup
29 * widget containing the custom color palette selector popup for selecting
30 * different colors based on RGB values.
37 #include <gtk/gtkbox.h>
38 #include <gtk/gtktable.h>
39 #include <gtk/gtklabel.h>
40 #include <gtk/gtkdrawingarea.h>
42 #include "hildon-color-selector.h"
43 #include "hildon-color-popup.h"
44 #include "hildon-controlbar.h"
47 #define _(String) dgettext(PACKAGE, String)
50 #define HILDON_COLOR_PALETTE_SIZE 120
51 #define HILDON_COLOR_CONTROLBAR_MAX 31
52 #define HILDON_COLOR_CONTROLBAR_MIN 0
53 #define HILDON_COLOR_LABELS_LEFT_PAD 35
54 #define HILDON_COLOR_PALETTE_POS_PAD 45
55 #define HILDON_COLOR_BAR_WIDTH 449
56 #define HILDON_COLOR_COL_SPACING 18
59 * Private function prototype definitions
63 hildon_popup_palette_expose (GtkWidget * widget,
64 GdkEventExpose * event,
67 * hildon_color_popup_new:
68 * @parent: the parent window of the dialog.
69 * @initial_color: a #GdkColor with the initial values to be used.
70 * @popup_data: a #HildonColorPopup.
72 * This function creates a new popup dialog with three controlbars
73 * (red, green, blue) and a drawing area with the current color.
75 * Used as normal GtkDialog (run with gtk_dialog_run() and read
76 * stardard responses (GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL).
78 * Returns: the newly created popup dialog.
82 hildon_color_popup_new(GtkWindow *parent, const GdkColor *initial_color,
83 HildonColorPopup *popup_data)
88 GtkWidget *l_red, *l_green, *l_blue;
90 /* Create control bars for HildonColorPopup */
91 popup_data->ctrlbar_red = hildon_controlbar_new ();
92 popup_data->ctrlbar_green = hildon_controlbar_new ();
93 popup_data->ctrlbar_blue = hildon_controlbar_new ();
94 area = gtk_drawing_area_new();
95 layout = GTK_TABLE(gtk_table_new(12, 2, FALSE));
97 /* Set widgets' size */
98 gtk_widget_set_size_request (area,
99 HILDON_COLOR_PALETTE_SIZE,
100 HILDON_COLOR_PALETTE_SIZE);
101 gtk_widget_set_size_request(popup_data->ctrlbar_red,
102 HILDON_COLOR_BAR_WIDTH, -1);
103 gtk_widget_set_size_request(popup_data->ctrlbar_green,
104 HILDON_COLOR_BAR_WIDTH, -1);
105 gtk_widget_set_size_request(popup_data->ctrlbar_blue,
106 HILDON_COLOR_BAR_WIDTH, -1);
108 /* Create labels for three kinds of color */
109 l_red = gtk_label_new( _("Ecdg_fi_5bit_colour_selector_red"));
110 l_green = gtk_label_new(_("Ecdg_fi_5bit_colour_selector_green"));
111 l_blue = gtk_label_new(_("Ecdg_fi_5bit_colour_selector_blue"));
113 /* Position the labels to start about the same label as the controlbars */
114 gtk_misc_set_alignment(GTK_MISC(l_red), 0.08f, 0.5f);
115 gtk_misc_set_alignment(GTK_MISC(l_green), 0.08f, 0.5f);
116 gtk_misc_set_alignment(GTK_MISC(l_blue), 0.08f, 0.5f);
118 /* Add labels and control bars to the layout table */
119 gtk_table_set_col_spacing(layout, 0, HILDON_COLOR_COL_SPACING);
120 gtk_table_attach_defaults(layout, l_red, 0, 1, 0, 2);
121 gtk_table_attach_defaults(layout, popup_data->ctrlbar_red, 0, 1, 2, 4);
122 gtk_table_attach_defaults(layout, l_green, 0, 1, 4, 6);
123 gtk_table_attach_defaults(layout, popup_data->ctrlbar_green, 0, 1, 6, 8);
124 gtk_table_attach_defaults(layout, l_blue, 0, 1, 8, 10);
125 gtk_table_attach_defaults(layout, popup_data->ctrlbar_blue, 0, 1, 10, 12);
126 gtk_table_attach(layout, area, 1, 2, 3, 11, GTK_SHRINK, GTK_SHRINK, 0, 0);
128 /* Give the maximum and minimum limits for each control bar */
129 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_red),
130 HILDON_COLOR_CONTROLBAR_MIN,
131 HILDON_COLOR_CONTROLBAR_MAX);
132 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_green),
133 HILDON_COLOR_CONTROLBAR_MIN,
134 HILDON_COLOR_CONTROLBAR_MAX);
135 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_blue),
136 HILDON_COLOR_CONTROLBAR_MIN,
137 HILDON_COLOR_CONTROLBAR_MAX);
139 /* Give the initial values for each control bar */
140 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_red),
141 (initial_color->red >> 11)&0x1F);
142 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_green),
143 (initial_color->green >> 11)&0x1F);
144 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_blue),
145 (initial_color->blue >> 11)&0x1F);
147 /* Register controlbar callbacks */
148 g_signal_connect_swapped(popup_data->ctrlbar_red, "value-changed",
149 G_CALLBACK(gtk_widget_queue_draw), area);
150 g_signal_connect_swapped(popup_data->ctrlbar_green, "value-changed",
151 G_CALLBACK(gtk_widget_queue_draw), area);
152 g_signal_connect_swapped(popup_data->ctrlbar_blue, "value-changed",
153 G_CALLBACK(gtk_widget_queue_draw), area);
155 /* Attach expose_event callback function to drawing area */
156 g_signal_connect (area, "expose_event",
157 G_CALLBACK(hildon_popup_palette_expose),
160 /* Create popup dialog */
161 popup = gtk_dialog_new_with_buttons (_("ecdg_ti_5bit_colour_selector"),
163 GTK_DIALOG_DESTROY_WITH_PARENT |
164 GTK_DIALOG_NO_SEPARATOR,
165 _("ecdg_bd_5bit_colour_selector_ok"), GTK_RESPONSE_OK,
166 _("Ecdg_bd_5bit_colour_selector_cancel"),
170 /* Select-key shouldn't do anything unless dialog's button is focused */
171 gtk_dialog_set_default_response(GTK_DIALOG(popup), GTK_RESPONSE_NONE);
173 /* Add layout table to the Vbox of the popup dialog */
174 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(popup)->vbox),
175 GTK_WIDGET(layout), TRUE, TRUE, 0);
177 /* Show thw Vbox of the popup dialog */
178 gtk_widget_show_all(GTK_DIALOG(popup)->vbox);
184 * hildon_color_popup_set_color_from_sliders:
185 * @color: a pointer to #GdkColor to which the new values will be put.
186 * @popup_data: a #HildonColorPopup.
188 * This function sets the values of the given #GdkColor to the values
189 * in the sliders of controlbars.
195 hildon_color_popup_set_color_from_sliders(GdkColor *color,
196 HildonColorPopup *popup_data)
199 color->red = hildon_controlbar_get_value (
200 HILDON_CONTROLBAR(popup_data->ctrlbar_red)) << 11;
201 color->green = hildon_controlbar_get_value (
202 HILDON_CONTROLBAR(popup_data->ctrlbar_green)) << 11;
203 color->blue = hildon_controlbar_get_value (
204 HILDON_CONTROLBAR(popup_data->ctrlbar_blue)) << 11;
207 /* expose_event callback function */
209 hildon_popup_palette_expose (GtkWidget * widget,
210 GdkEventExpose *event, gpointer data)
212 if (GTK_WIDGET_DRAWABLE(widget))
215 GdkGC * gc = gdk_gc_new (widget->window);
217 /* Get the current color value */
218 hildon_color_popup_set_color_from_sliders(&color, data);
219 gdk_gc_set_rgb_fg_color(gc, &color);
221 /* draw the color area */
222 gdk_draw_rectangle( widget->window, gc, TRUE /* filled */,
223 1, 1, widget->allocation.width - 2,
224 widget->allocation.height - 2);
226 color.pixel = color.red = color.green = color.blue = 0;
227 gdk_gc_set_rgb_fg_color(gc, &color);
229 /* Draw frames on color box */
230 gdk_draw_rectangle( widget->window, gc, FALSE,
231 0, 0, widget->allocation.width - 1,
232 widget->allocation.height - 1);
234 /* Free memory used by the graphics contexts */