* hildon-widgets/hildon-color-popup.c (hildon_color_popup_new): removed current_color...
[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 #define HILDON_COLOR_COL_SPACING          18
57
58 /* 
59  * Private function prototype definitions 
60  */
61
62 static gboolean 
63 hildon_popup_palette_expose (GtkWidget * widget,
64                              GdkEventExpose * event,
65                              gpointer data);
66 /**
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.
71  *
72  * This function creates a new popup dialog with three controlbars
73  * (red, green, blue) and a drawing area with the current color.
74  *
75  * Used as normal GtkDialog (run with gtk_dialog_run() and read 
76  * stardard responses (GTK_RESPONSE_OK, GTK_RESPONSE_CANCEL). 
77  *
78  * Returns: the newly created popup dialog. 
79  */
80
81 GtkWidget *
82 hildon_color_popup_new(GtkWindow *parent, const GdkColor *initial_color, 
83                 HildonColorPopup *popup_data)
84 {
85   GtkWidget *popup;
86   GtkTable *layout;
87   GtkWidget *area;
88   GtkWidget *l_red, *l_green, *l_blue;
89
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));
96
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); 
107
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"));
112   
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);
117
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);
127
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);
138
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);
146
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);
154
155   /* Attach expose_event callback function to drawing area */
156   g_signal_connect (area,  "expose_event",
157                       G_CALLBACK(hildon_popup_palette_expose), 
158                       popup_data);
159   
160   /* Create popup dialog */
161   popup = gtk_dialog_new_with_buttons (_("ecdg_ti_5bit_colour_selector"), 
162                                        GTK_WINDOW(parent),
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"),
167                                        GTK_RESPONSE_CANCEL,
168                                        NULL);
169
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);
172
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);
176
177   /* Show thw Vbox of the popup dialog */
178   gtk_widget_show_all(GTK_DIALOG(popup)->vbox);      
179
180   return popup;
181 }
182
183 /**
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.
187  *
188  * This function sets the values of the given #GdkColor to the values
189  * in the sliders of controlbars.
190  * 
191  * Returns: void. 
192  */
193
194 void
195 hildon_color_popup_set_color_from_sliders(GdkColor *color,
196   HildonColorPopup *popup_data)
197 {
198   color->pixel = 0;
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;
205 }
206
207 /* expose_event callback function */
208 static gboolean 
209 hildon_popup_palette_expose (GtkWidget * widget,
210                              GdkEventExpose *event, gpointer data)
211 {
212   if (GTK_WIDGET_DRAWABLE(widget))
213   {
214     GdkColor color;
215     GdkGC * gc = gdk_gc_new (widget->window); 
216
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);
220                          
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);    
225
226     color.pixel = color.red = color.green = color.blue = 0;
227     gdk_gc_set_rgb_fg_color(gc, &color);
228
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); 
233
234     /* Free memory used by the graphics contexts */
235     g_object_unref(gc);
236   }
237
238   return TRUE;
239 }