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
58 * Private function prototype definitions
62 hildon_popup_palette_expose (GtkWidget * widget,
63 GdkEventExpose * event,
66 * hildon_color_popup_new:
67 * @parent: the parent window of the dialog.
68 * @initial_color: a #GdkColor with the initial values to be used.
69 * @popup_data: a #HildonColorPopup.
71 * This function creates a new popup dialog with three controlbars
72 * (red, green, blue) and a drawing area with the current color.
74 * Used as normal GtkDialog (run with gtk_dialog_run() and read
75 * stardard responses (GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL).
77 * Returns: the newly created popup dialog.
81 hildon_color_popup_new(GtkWindow *parent, GdkColor *initial_color,
82 HildonColorPopup *popup_data)
87 GtkWidget *l_red, *l_green, *l_blue;
88 GdkColor *current_color;
90 current_color = initial_color;
92 popup_data->ctrlbar_red = hildon_controlbar_new ();
93 popup_data->ctrlbar_green = hildon_controlbar_new ();
94 popup_data->ctrlbar_blue = hildon_controlbar_new ();
95 area = gtk_drawing_area_new();
96 layout = GTK_TABLE(gtk_table_new(12, 2, FALSE));
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 l_red = gtk_label_new( _("Ecdg_fi_5bit_colour_selector_red"));
109 l_green = gtk_label_new(_("Ecdg_fi_5bit_colour_selector_green"));
110 l_blue = gtk_label_new(_("Ecdg_fi_5bit_colour_selector_blue"));
112 /* Position the labels to start about the same label as the controlbars */
113 gtk_misc_set_alignment(GTK_MISC(l_red), 0.08f, 0.5f);
114 gtk_misc_set_alignment(GTK_MISC(l_green), 0.08f, 0.5f);
115 gtk_misc_set_alignment(GTK_MISC(l_blue), 0.08f, 0.5f);
117 gtk_table_attach_defaults(layout, l_red, 0, 1, 0, 2);
118 gtk_table_attach_defaults(layout, popup_data->ctrlbar_red, 0, 1, 2, 4);
119 gtk_table_attach_defaults(layout, l_green, 0, 1, 4, 6);
120 gtk_table_attach_defaults(layout, popup_data->ctrlbar_green, 0, 1, 6, 8);
121 gtk_table_attach_defaults(layout, l_blue, 0, 1, 8, 10);
122 gtk_table_attach_defaults(layout, popup_data->ctrlbar_blue, 0, 1, 10, 12);
123 gtk_table_attach(layout, area, 1, 2, 3, 11, GTK_SHRINK, GTK_SHRINK, 0, 0);
126 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_red),
127 HILDON_COLOR_CONTROLBAR_MIN,
128 HILDON_COLOR_CONTROLBAR_MAX);
129 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_green),
130 HILDON_COLOR_CONTROLBAR_MIN,
131 HILDON_COLOR_CONTROLBAR_MAX);
132 hildon_controlbar_set_range (HILDON_CONTROLBAR(popup_data->ctrlbar_blue),
133 HILDON_COLOR_CONTROLBAR_MIN,
134 HILDON_COLOR_CONTROLBAR_MAX);
136 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_red),
137 (current_color->red >> 11)&0x1F);
138 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_green),
139 (current_color->green >> 11)&0x1F);
140 hildon_controlbar_set_value (HILDON_CONTROLBAR(popup_data->ctrlbar_blue),
141 (current_color->blue >> 11)&0x1F);
143 /* controlbars callbacks */
144 g_signal_connect_swapped(popup_data->ctrlbar_red, "value-changed",
145 G_CALLBACK(gtk_widget_queue_draw), area);
146 g_signal_connect_swapped(popup_data->ctrlbar_green, "value-changed",
147 G_CALLBACK(gtk_widget_queue_draw), area);
148 g_signal_connect_swapped(popup_data->ctrlbar_blue, "value-changed",
149 G_CALLBACK(gtk_widget_queue_draw), area);
151 g_signal_connect (area, "expose_event",
152 G_CALLBACK(hildon_popup_palette_expose),
155 popup = gtk_dialog_new_with_buttons (_("ecdg_ti_5bit_colour_selector"),
157 GTK_DIALOG_DESTROY_WITH_PARENT |
158 GTK_DIALOG_NO_SEPARATOR,
159 _("ecdg_bd_5bit_colour_selector_ok"), GTK_RESPONSE_OK,
160 _("Ecdg_bd_5bit_colour_selector_cancel"),
164 gtk_dialog_set_default_response(GTK_DIALOG(popup), GTK_RESPONSE_OK);
165 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(popup)->vbox),
166 GTK_WIDGET(layout), TRUE, TRUE, 0);
167 gtk_widget_show_all(GTK_DIALOG(popup)->vbox);
173 * hildon_color_popup_set_color_from_sliders:
174 * @color: a pointer to #GdkColor to which the new values will be put.
175 * @popup_data: a #HildonColorPopup.
177 * This function sets the values of the given #GdkColor to the values
178 * in the sliders of controlbars.
184 hildon_color_popup_set_color_from_sliders(GdkColor *color,
185 HildonColorPopup *popup_data)
188 color->red = hildon_controlbar_get_value (
189 HILDON_CONTROLBAR(popup_data->ctrlbar_red)) << 11;
190 color->green = hildon_controlbar_get_value (
191 HILDON_CONTROLBAR(popup_data->ctrlbar_green)) << 11;
192 color->blue = hildon_controlbar_get_value (
193 HILDON_CONTROLBAR(popup_data->ctrlbar_blue)) << 11;
197 hildon_popup_palette_expose (GtkWidget * widget,
198 GdkEventExpose *event, gpointer data)
200 GTimer *timer = g_timer_new();
201 g_timer_start(timer);
202 if (GTK_WIDGET_DRAWABLE(widget))
205 GdkGC * gc = gdk_gc_new (widget->window);
207 hildon_color_popup_set_color_from_sliders(&color, data);
208 gdk_gc_set_rgb_fg_color(gc, &color);
211 gdk_draw_rectangle( widget->window, gc, TRUE /* filled */,
212 1, 1, widget->allocation.width - 2,
213 widget->allocation.height - 2);
215 color.pixel = color.red = color.green = color.blue = 0;
216 gdk_gc_set_rgb_fg_color(gc, &color);
218 /* frames on color box */
219 gdk_draw_rectangle( widget->window, gc, FALSE,
220 0, 0, widget->allocation.width - 1,
221 widget->allocation.height - 1);
226 g_print("---%f---\n",g_timer_elapsed(timer,NULL));
227 g_timer_destroy(timer);