1 /*
2  * go-palette.c :
3  *
4  * Copyright (C) 2006 Emmanuel Pacaud (emmanuel.pacaud@lapp.in2p3.fr)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #include <goffice/goffice-config.h>
23 #include <goffice/goffice.h>
24 
25 #include <glib/gi18n-lib.h>
26 #include <gdk/gdkkeysyms.h>
27 
28 #include <math.h>
29 
30 enum {
31 	GO_PALETTE_ACTIVATE,
32 	GO_PALETTE_AUTOMATIC_ACTIVATE,
33 	GO_PALETTE_CUSTOM_ACTIVATE,
34 	GO_PALETTE_LAST_SIGNAL
35 };
36 
37 static guint go_palette_signals[GO_PALETTE_LAST_SIGNAL] = {0,};
38 
39 static void 		go_palette_finalize   	 (GObject		*object);
40 static GtkWidget       *go_palette_menu_item_new (GOPalette *palette, int index);
41 static void 		cb_automatic_activate 	 (GtkWidget *item, GOPalette *palette);
42 static void 		cb_custom_activate 	 (GtkWidget *item, GOPalette *palette);
43 
44 struct _GOPalettePrivate {
45 	int		 n_swatches;
46 	int 		 n_columns;
47 
48 	int		 swatch_width;
49 	int		 swatch_height;
50 
51 	GOPaletteSwatchRenderCallback 	swatch_render;
52 	GOPaletteSwatchTooltipCallback 	get_tooltip;
53 
54 	gpointer 	 data;
55 	GDestroyNotify	 destroy;
56 
57 	gboolean 	 show_automatic;
58 	GtkWidget	*automatic;
59 	GtkWidget	*automatic_separator;
60 	char		*automatic_label;
61 	int		 automatic_index;
62 
63 	gboolean	 show_custom;
64 	GtkWidget	*custom;
65 	GtkWidget	*custom_separator;
66 	char		*custom_label;
67 };
68 
G_DEFINE_TYPE(GOPalette,go_palette,GTK_TYPE_MENU)69 G_DEFINE_TYPE (GOPalette, go_palette, GTK_TYPE_MENU)
70 
71 static void
72 go_palette_init (GOPalette *palette)
73 {
74 	GOPalettePrivate *priv;
75 	PangoLayout *layout;
76 	PangoRectangle rect;
77 
78 	priv = G_TYPE_INSTANCE_GET_PRIVATE (palette, GO_TYPE_PALETTE, GOPalettePrivate);
79 
80 	palette->priv = priv;
81 
82 	priv->n_swatches = 0;
83 	priv->n_columns = 1;
84 
85 	priv->swatch_render = NULL;
86 	priv->data = NULL;
87 	priv->destroy = NULL;
88 
89 	priv->automatic = NULL;
90 	priv->automatic_separator = NULL;
91 	priv->automatic_label = NULL;
92 	priv->custom = NULL;
93 	priv->custom_separator = NULL;
94 	priv->custom_label = NULL;
95 	priv->show_automatic = FALSE;
96 	priv->show_custom = FALSE;
97 
98 	priv->automatic_index = 0;
99 
100 	layout = gtk_widget_create_pango_layout (GTK_WIDGET (palette), "A");
101 	pango_layout_get_pixel_extents (layout, NULL, &rect);
102 	g_object_unref (layout);
103 
104 	priv->swatch_height = rect.height + 2;
105 	priv->swatch_width = priv->swatch_height;
106 }
107 
108 static void
go_palette_realize(GtkWidget * widget)109 go_palette_realize (GtkWidget *widget)
110 {
111 	GOPalette *palette = GO_PALETTE (widget);
112 	GOPalettePrivate *priv = palette->priv;
113 	GtkWidget *item;
114 	int i, row;
115 
116 	for (i = 0; i < priv->n_swatches; i++) {
117 		item = go_palette_menu_item_new (GO_PALETTE (palette), i);
118 		gtk_menu_attach (GTK_MENU (palette), item, i % priv->n_columns, i % priv->n_columns + 1,
119 				 i / priv->n_columns + 2, i / priv->n_columns + 3);
120 		gtk_widget_show (item);
121 	}
122 
123 	if (priv->show_automatic) {
124 		priv->automatic = gtk_menu_item_new_with_label (priv->automatic_label);
125 		gtk_menu_attach	(GTK_MENU (palette), priv->automatic, 0, priv->n_columns, 0, 1);
126 		g_signal_connect (priv->automatic, "activate", G_CALLBACK (cb_automatic_activate), palette);
127 		priv->automatic_separator = gtk_separator_menu_item_new ();
128 		gtk_menu_attach	(GTK_MENU (palette), priv->automatic_separator, 0, priv->n_columns, 1, 2);
129 		gtk_widget_show (GTK_WIDGET (palette->priv->automatic));
130 		gtk_widget_show (GTK_WIDGET (palette->priv->automatic_separator));
131 	}
132 
133 	if (priv->show_custom) {
134 		row = ((priv->n_swatches - 1) / priv->n_columns) + 3;
135 
136 		priv->custom_separator = gtk_separator_menu_item_new ();
137 		gtk_menu_attach (GTK_MENU (palette), priv->custom_separator, 0, priv->n_columns,
138 				 row, row + 1);
139 		priv->custom = gtk_menu_item_new_with_label (priv->custom_label);
140 		gtk_menu_attach (GTK_MENU (palette), priv->custom, 0, priv->n_columns,
141 				 row + 1, row + 2);
142 		g_signal_connect (priv->custom, "activate", G_CALLBACK (cb_custom_activate), palette);
143 		gtk_widget_show (GTK_WIDGET (palette->priv->custom));
144 		gtk_widget_show (GTK_WIDGET (palette->priv->custom_separator));
145 	}
146 
147 	GTK_WIDGET_CLASS (go_palette_parent_class)->realize (widget);
148 }
149 
150 static void
go_palette_class_init(GOPaletteClass * class)151 go_palette_class_init (GOPaletteClass *class)
152 {
153 	GObjectClass *object_class = G_OBJECT_CLASS (class);
154 	GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
155 
156 	object_class->finalize = go_palette_finalize;
157 	widget_class->realize = go_palette_realize;
158 
159 	go_palette_signals[GO_PALETTE_ACTIVATE] =
160 		g_signal_new ("activate",
161 			      G_OBJECT_CLASS_TYPE (class),
162 			      G_SIGNAL_RUN_LAST,
163 			      G_STRUCT_OFFSET (GOPaletteClass, activate),
164 			      NULL, NULL,
165 			      g_cclosure_marshal_VOID__INT,
166 			      G_TYPE_NONE, 1, G_TYPE_INT);
167 
168 	go_palette_signals[GO_PALETTE_AUTOMATIC_ACTIVATE] =
169 		g_signal_new ("automatic-activate",
170 			      G_OBJECT_CLASS_TYPE (class),
171 			      G_SIGNAL_RUN_LAST,
172 			      G_STRUCT_OFFSET (GOPaletteClass, automatic_activate),
173 			      NULL, NULL,
174 			      g_cclosure_marshal_VOID__INT,
175 			      G_TYPE_NONE, 1, G_TYPE_INT);
176 
177 	go_palette_signals[GO_PALETTE_CUSTOM_ACTIVATE] =
178 		g_signal_new ("custom-activate",
179 			      G_OBJECT_CLASS_TYPE (class),
180 			      G_SIGNAL_RUN_LAST,
181 			      G_STRUCT_OFFSET (GOPaletteClass, custom_activate),
182 			      NULL, NULL,
183 			      g_cclosure_marshal_VOID__VOID,
184 			      G_TYPE_NONE, 0);
185 
186 	g_type_class_add_private (object_class, sizeof (GOPalettePrivate));
187 
188 #ifdef HAVE_GTK_WIDGET_CLASS_SET_CSS_NAME
189 	gtk_widget_class_set_css_name (widget_class, "palette");
190 #endif
191 }
192 
193 static void
go_palette_finalize(GObject * object)194 go_palette_finalize (GObject *object)
195 {
196 	GOPalettePrivate *priv;
197 
198 	priv = GO_PALETTE(object)->priv;
199 
200 	if (priv->data && priv->destroy)
201 		(priv->destroy) (priv->data);
202 	g_free (priv->automatic_label);
203 	g_free (priv->custom_label);
204 
205 	(* G_OBJECT_CLASS (go_palette_parent_class)->finalize) (object);
206 }
207 
208 static gboolean
cb_swatch_draw(GtkWidget * swatch,cairo_t * cr,GOPalette * palette)209 cb_swatch_draw (GtkWidget *swatch, cairo_t *cr, GOPalette *palette)
210 {
211 	if (palette->priv->swatch_render) {
212 		GdkRectangle area;
213 		int index;
214 		GtkAllocation allocation;
215 
216 		gtk_widget_get_allocation (swatch, &allocation);
217 		area.x = 0;
218 		area.y = 0;
219 		area.width = allocation.width;
220 		area.height = allocation.height;
221 
222 		index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (swatch), "index"));
223 
224 		(palette->priv->swatch_render) (cr, &area, index, palette->priv->data);
225 
226 	}
227 	return TRUE;
228 }
229 
230 static void
cb_menu_item_activate(GtkWidget * item,GOPalette * palette)231 cb_menu_item_activate (GtkWidget *item, GOPalette *palette)
232 {
233 	int index;
234 
235 	index = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (gtk_bin_get_child (GTK_BIN (item))), "index"));
236 	g_signal_emit (palette, go_palette_signals[GO_PALETTE_ACTIVATE], 0, index);
237 }
238 
239 static void
cb_menu_item_toggle_size_request(GtkWidget * item,gint * requitision)240 cb_menu_item_toggle_size_request (GtkWidget *item, gint *requitision)
241 {
242 	*requitision = 1;
243 }
244 
245 static GtkWidget *
go_palette_menu_item_new(GOPalette * palette,int index)246 go_palette_menu_item_new (GOPalette *palette, int index)
247 {
248 	GtkWidget *swatch;
249 	GtkWidget *item;
250 	GOPalettePrivate *priv = palette->priv;
251 
252 	item = gtk_menu_item_new ();
253 	swatch = go_palette_swatch_new (palette, index);
254 	gtk_container_add (GTK_CONTAINER (item), swatch);
255 
256 	if (priv->get_tooltip) {
257 		char const *tip;
258 
259 		tip = priv->get_tooltip (index, priv->data);
260 		gtk_widget_set_tooltip_text (item, tip);
261 	}
262 
263 	g_signal_connect (item, "activate", G_CALLBACK (cb_menu_item_activate), palette);
264 
265 	/* Workaround for bug http://bugzilla.gnome.org/show_bug.cgi?id=585421 */
266 	g_signal_connect (item, "toggle-size-request", G_CALLBACK (cb_menu_item_toggle_size_request), NULL);
267 
268 	return item;
269 }
270 
271 static void
cb_automatic_activate(GtkWidget * item,GOPalette * palette)272 cb_automatic_activate (GtkWidget *item, GOPalette *palette)
273 {
274 	g_signal_emit (palette, go_palette_signals[GO_PALETTE_AUTOMATIC_ACTIVATE], 0,
275 		       palette->priv->automatic_index);
276 }
277 
278 static void
cb_custom_activate(GtkWidget * item,GOPalette * palette)279 cb_custom_activate (GtkWidget *item, GOPalette *palette)
280 {
281 	g_signal_emit (palette, go_palette_signals[GO_PALETTE_CUSTOM_ACTIVATE], 0);
282 }
283 
284 /**
285  * go_palette_new:
286  * @n_swatches: number of palette items
287  * @swatch_width: swatch width as multiple of swatch height
288  * @n_columns: number of columns for displaying palette items
289  * @swatch_render: (scope notified): a user function used for swatch rendering
290  * @get_tooltip: (scope notified): a user function for tooltips.
291  * @data: user data for use by swatch render function
292  * @destroy: (scope async): a function to destroy user data on widget finalization
293  *
294  * Returns: a new #GOPalette object.
295  **/
296 GtkWidget *
go_palette_new(int n_swatches,double swatch_width,int n_columns,GOPaletteSwatchRenderCallback swatch_render,GOPaletteSwatchTooltipCallback get_tooltip,gpointer data,GDestroyNotify destroy)297 go_palette_new (int n_swatches,
298 		double swatch_width,
299 		int n_columns,
300 		GOPaletteSwatchRenderCallback swatch_render,
301 		GOPaletteSwatchTooltipCallback get_tooltip,
302 		gpointer data,
303 		GDestroyNotify destroy)
304 {
305 	GOPalettePrivate *priv;
306 	GtkWidget *palette;
307 
308 	palette = g_object_new (GO_TYPE_PALETTE, NULL);
309 
310 	g_return_val_if_fail (n_swatches >= 1, palette);
311 
312 	priv = GO_PALETTE (palette)->priv;
313 
314 	priv->n_swatches = n_swatches;
315 	priv->swatch_render = swatch_render;
316 	priv->get_tooltip = get_tooltip;
317 	priv->data = data;
318 	priv->destroy = destroy;
319 	if (swatch_width > 0.)
320 		priv->swatch_width = priv->swatch_height * swatch_width;
321 
322 	if (n_columns < 1)
323 		n_columns = 1;
324 	priv->n_columns = n_columns;
325 
326 	return palette;
327 }
328 
329 /**
330  * go_palette_show_automatic:
331  * @palette: a #GOPalette
332  * @index: index to use on automatic item activation
333  * @label: if not %NULL, replace automatic button label
334  *
335  * Adds an automatic button to @palette.
336  **/
337 void
go_palette_show_automatic(GOPalette * palette,int index,char const * label)338 go_palette_show_automatic (GOPalette *palette,
339 			   int index,
340 			   char const *label)
341 {
342 	GOPalettePrivate *priv;
343 
344 	g_return_if_fail (GO_IS_PALETTE (palette));
345 
346 	priv = palette->priv;
347 	g_return_if_fail (!priv->show_automatic);
348 
349 	priv->automatic_label = g_strdup (label == NULL ?  _("Automatic"): _(label));
350 	priv->automatic_index = index;
351 	priv->show_automatic = TRUE;
352 }
353 
354 /**
355  * go_palette_show_custom:
356  * @palette: a #GOPalette
357  * @label: if not %NULL, replaces custom button label
358  *
359  * Adds a custom button to bottom of @palette. An activation
360  * of custom button will cause an emition of "custom_activate" signal.
361  **/
362 void
go_palette_show_custom(GOPalette * palette,char const * label)363 go_palette_show_custom (GOPalette *palette,
364 			char const *label)
365 {
366 	GOPalettePrivate *priv;
367 
368 	g_return_if_fail (GO_IS_PALETTE (palette));
369 
370 	priv = palette->priv;
371 	g_return_if_fail (!priv->show_custom);
372 
373 	priv->custom_label = g_strdup (label == NULL ?  _("Custom...") : _(label));
374 	priv->show_custom = TRUE;
375 }
376 
377 /**
378  * go_palette_get_user_data:
379  * @palette: a #GOPalette
380  *
381  * Returns: (transfer none): a pointer to user data given to go_palette_new function.
382  **/
383 gpointer
go_palette_get_user_data(GOPalette * palette)384 go_palette_get_user_data (GOPalette *palette)
385 {
386 	g_return_val_if_fail (GO_IS_PALETTE (palette), NULL);
387 
388 	return palette->priv->data;
389 }
390 
391 /**
392  * go_palette_swatch_new:
393  * @palette: a #GOPalette
394  * @index: default index
395  *
396  * Returns: (transfer full): a new #GtkDrawingArea which will be rendered like a @palette
397  * swatch. @index can be changed later by changing swatch "index" data.
398  **/
399 GtkWidget *
go_palette_swatch_new(GOPalette * palette,int index)400 go_palette_swatch_new (GOPalette *palette, int index)
401 {
402 	GtkWidget *swatch;
403 
404 	g_return_val_if_fail (GO_IS_PALETTE (palette), NULL);
405 
406 	swatch = gtk_drawing_area_new ();
407 
408 	g_object_set_data (G_OBJECT (swatch), "index", GINT_TO_POINTER (index));
409 	g_signal_connect (G_OBJECT (swatch), "draw", G_CALLBACK (cb_swatch_draw), palette);
410 	gtk_widget_set_size_request (swatch,
411 				     palette->priv->swatch_width,
412 				     palette->priv->swatch_height);
413 
414 	gtk_widget_show (swatch);
415 
416 	return swatch;
417 }
418 
419 /**
420  * go_palette_get_n_swatches:
421  * @palette: a #GOPalette
422  *
423  * A convenience function.
424  *
425  * Returns: the number of palette items.
426  **/
427 int
go_palette_get_n_swatches(GOPalette * palette)428 go_palette_get_n_swatches (GOPalette *palette)
429 {
430 	g_return_val_if_fail (GO_IS_PALETTE (palette), 0);
431 
432 	return palette->priv->n_swatches;
433 }
434 
435