latest update
[hildon] / hildon-widgets / hildon-color-popup.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
7  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  *
23  */
24
25 /*
26  * @file hildon-color-popup.c
27  *
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.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include <gtk/gtkbox.h>
38 #include <gtk/gtktable.h>
39 #include <gtk/gtklabel.h>
40 #include <gtk/gtkdrawingarea.h>
41
42 #include "hildon-color-selector.h"
43 #include "hildon-color-popup.h"
44 #include "hildon-controlbar.h"
45
46 #include <libintl.h>
47 #define _(String) dgettext(PACKAGE, String)
48
49 /* Pixel sizes */
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
57 /* 
58  * Private function prototype definitions 
59  */
60
61 static gboolean 
62 hildon_popup_palette_expose (GtkWidget * widget,
63                              GdkEventExpose * event,
64                              gpointer data);
65 /**
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.
70  *
71  * This function creates a new popup dialog with three controlbars
72  * (red, green, blue) and a drawing area with the current color.
73  *
74  * Used as normal GtkDialog (run with gtk_dialog_run() and read 
75  * stardard responses (GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL). 
76  *
77  * Returns: the newly created popup dialog. 
78  */
79
80 GtkWidget *
81 hildon_color_popup_new(GtkWindow *parent, GdkColor *initial_color, 
82                 HildonColorPopup *popup_data)
83 {
84   GtkWidget *popup;
85   GtkTable *layout;
86   GtkWidget *area;
87   GtkWidget *l_red, *l_green, *l_blue;
88   GdkColor  *current_color;
89
90   current_color = initial_color;
91
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));
97
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); 
107
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"));
111   
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);
116
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);
124
125   /* control bars */
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);
135
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);
142
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);
150
151   g_signal_connect (area,  "expose_event",
152                       G_CALLBACK(hildon_popup_palette_expose), 
153                       popup_data);
154   
155   popup = gtk_dialog_new_with_buttons (_("ecdg_ti_5bit_colour_selector"), 
156                                        GTK_WINDOW(parent),
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"),
161                                        GTK_RESPONSE_CANCEL,
162                                        NULL);
163
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);      
168
169   return popup;
170 }
171
172 /**
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.
176  *
177  * This function sets the values of the given #GdkColor to the values
178  * in the sliders of controlbars.
179  * 
180  * Returns: void. 
181  */
182
183 void
184 hildon_color_popup_set_color_from_sliders(GdkColor *color,
185   HildonColorPopup *popup_data)
186 {
187   color->pixel = 0;
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;
194 }
195
196 static gboolean 
197 hildon_popup_palette_expose (GtkWidget * widget,
198                              GdkEventExpose *event, gpointer data)
199 {
200   GTimer *timer = g_timer_new();
201   g_timer_start(timer);
202   if (GTK_WIDGET_DRAWABLE(widget))
203   {
204     GdkColor color;
205     GdkGC * gc = gdk_gc_new (widget->window); 
206
207     hildon_color_popup_set_color_from_sliders(&color, data);
208     gdk_gc_set_rgb_fg_color(gc, &color);
209                          
210     /* draw the color */
211     gdk_draw_rectangle( widget->window, gc, TRUE /* filled */,  
212                         1, 1, widget->allocation.width - 2, 
213                         widget->allocation.height - 2);    
214
215     color.pixel = color.red = color.green = color.blue = 0;
216     gdk_gc_set_rgb_fg_color(gc, &color);
217
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); 
222
223     g_object_unref(gc);
224   }
225   g_timer_stop(timer);
226   g_print("---%f---\n",g_timer_elapsed(timer,NULL));
227   g_timer_destroy(timer);
228   return TRUE;
229 }