Add a tooltip showing current histogram state on bar histogram.
[geeqie.git] / src / bar_histogram.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: Vladimir Nadvornik
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "bar_comment.h"
16
17 #include "bar.h"
18 #include "metadata.h"
19 #include "filedata.h"
20 #include "menu.h"
21 #include "ui_menu.h"
22 #include "ui_misc.h"
23 #include "histogram.h"
24 #include "rcfile.h"
25
26 /*
27  *-------------------------------------------------------------------
28  * keyword / comment utils
29  *-------------------------------------------------------------------
30  */
31
32
33
34 typedef struct _PaneHistogramData PaneHistogramData;
35 struct _PaneHistogramData
36 {
37         PaneData pane;
38         GtkWidget *widget;
39         GtkWidget *drawing_area;
40         Histogram *histogram;
41         gint histogram_width;
42         gint histogram_height;
43         GdkPixbuf *pixbuf;
44         FileData *fd;
45 };
46
47
48 static void bar_pane_histogram_update(PaneHistogramData *phd)
49 {
50         const HistMap *histmap;
51         if (phd->pixbuf) g_object_unref(phd->pixbuf);
52         phd->pixbuf = NULL;
53
54         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
55
56         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
57         
58         histmap = histmap_get(phd->fd);
59         
60         if (!histmap) return;
61         
62         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
63         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
64         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
65
66 #if GTK_CHECK_VERSION(2,12,0)
67         gtk_widget_set_tooltip_text(phd->drawing_area, histogram_label(phd->histogram));
68 #endif
69 }
70
71
72 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
73 {
74         PaneHistogramData *phd;
75
76         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
77         if (!phd) return;
78
79         file_data_unref(phd->fd);
80         phd->fd = file_data_ref(fd);
81
82         bar_pane_histogram_update(phd);
83 }
84
85 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
86 {
87         PaneHistogramData *phd;
88
89         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
90         if (!phd) return;
91
92         WRITE_STRING("<pane_histogram\n");
93         indent++;
94         WRITE_CHAR(*phd, pane.title);
95         WRITE_BOOL(*phd, pane.expanded);
96         WRITE_INT(*phd->histogram, histogram_channel);
97         WRITE_INT(*phd->histogram, histogram_mode);
98         indent--;
99         WRITE_STRING("/>\n");
100 }
101
102
103 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
104 {
105         PaneHistogramData *phd = data;
106         if (fd == phd->fd) bar_pane_histogram_update(phd);
107 }
108
109 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
110 {
111         PaneHistogramData *phd = data;
112         if (!phd || !phd->pixbuf) return TRUE;
113         
114         gdk_draw_pixbuf(widget->window,
115                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
116                         phd->pixbuf,
117                         0, 0,
118                         0, 0,
119                         -1, -1,
120                         GDK_RGB_DITHER_NORMAL, 0, 0);
121         return TRUE;
122 }
123
124 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
125 {
126         PaneHistogramData *phd = data;
127
128         phd->histogram_width = allocation->width;
129         phd->histogram_height = allocation->height;
130         bar_pane_histogram_update(phd);
131 }
132
133 static void bar_pane_histogram_close(GtkWidget *pane)
134 {
135         PaneHistogramData *phd;
136
137         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
138         if (!phd) return;
139
140         gtk_widget_destroy(phd->widget);
141 }
142
143 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
144 {
145         PaneHistogramData *phd = data;
146
147         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
148
149         file_data_unref(phd->fd);
150         g_free(phd->pane.title);
151         histogram_free(phd->histogram);
152         if (phd->pixbuf) g_object_unref(phd->pixbuf);
153
154         g_free(phd);
155 }
156
157 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
158 {
159         PaneHistogramData *phd;
160         gint channel;
161
162         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
163
164         phd = submenu_item_get_data(widget);
165
166         if (!phd) return;
167
168         channel = GPOINTER_TO_INT(data);
169         if (channel == histogram_get_channel(phd->histogram)) return;
170
171         histogram_set_channel(phd->histogram, channel);
172         bar_pane_histogram_update(phd);
173 }
174
175 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
176 {
177         PaneHistogramData *phd;
178         gint logmode;
179         
180         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
181
182         phd = submenu_item_get_data(widget);
183
184         if (!phd) return;
185
186         logmode = GPOINTER_TO_INT(data);
187         if (logmode == histogram_get_mode(phd->histogram)) return;
188
189         histogram_set_mode(phd->histogram, logmode);
190         bar_pane_histogram_update(phd);
191 }
192
193 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
194                                         const gchar *label,
195                                         GCallback func, gint value,
196                                         gboolean show_current, gint current_value)
197 {
198         GtkWidget *item;
199
200         if (show_current)
201                 {
202                 item = menu_item_add_radio(menu, parent,
203                                            label, (value == current_value),
204                                            func, GINT_TO_POINTER((gint)value));
205                 }
206         else
207                 {
208                 item = menu_item_add(menu, label,
209                                      func, GINT_TO_POINTER((gint)value));
210                 }
211
212         return item;
213 }
214
215 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
216                                            gboolean show_current, gint current_value)
217 {
218         GtkWidget *submenu;
219         GtkWidget *parent;
220
221         submenu = gtk_menu_new();
222         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
223
224         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
225         bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
226         bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
227         bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
228         bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
229
230         if (menu)
231                 {
232                 GtkWidget *item;
233
234                 item = menu_item_add(menu, _("Channels"), NULL, NULL);
235                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
236                 return item;
237                 }
238
239         return submenu;
240 }
241 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
242                                            gboolean show_current, gint current_value)
243 {
244         GtkWidget *submenu;
245         GtkWidget *parent;
246
247         submenu = gtk_menu_new();
248         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
249
250         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
251         bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
252
253         if (menu)
254                 {
255                 GtkWidget *item;
256
257                 item = menu_item_add(menu, _("Mode"), NULL, NULL);
258                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
259                 return item;
260                 }
261
262         return submenu;
263 }
264
265
266 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
267 {
268         GtkWidget *menu;
269         static gboolean show_current = TRUE;
270
271         menu = popup_menu_short_lived();
272         bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
273                                         show_current, histogram_get_channel(phd->histogram));
274         bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
275                                        show_current, histogram_get_mode(phd->histogram));
276
277         return menu;
278 }
279
280 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
281 {
282         PaneHistogramData *phd = data;
283
284         if (bevent->button == MOUSE_BUTTON_RIGHT)
285                 {
286                 GtkWidget *menu;
287
288                 menu = bar_pane_histogram_menu(phd);
289                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
290                 return TRUE;
291         }
292
293         return FALSE;
294 }
295
296
297 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gint expanded, gint histogram_channel, gint histogram_mode)
298 {
299         PaneHistogramData *phd;
300
301         phd = g_new0(PaneHistogramData, 1);
302         
303         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
304         phd->pane.pane_write_config = bar_pane_histogram_write_config;
305         phd->pane.title = g_strdup(title);
306         phd->pane.expanded = expanded;
307         
308         phd->histogram = histogram_new();
309
310         histogram_set_channel(phd->histogram, histogram_channel);
311         histogram_set_mode(phd->histogram, histogram_mode);
312
313         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
314
315         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
316         g_signal_connect(G_OBJECT(phd->widget), "destroy",
317                          G_CALLBACK(bar_pane_histogram_destroy), phd);
318         
319
320         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
321
322         phd->drawing_area = gtk_drawing_area_new();
323         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
324                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
325
326         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
327                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
328                          
329         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
330         gtk_widget_show(phd->drawing_area);
331         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
332
333         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
334
335 #if GTK_CHECK_VERSION(2,12,0)
336         gtk_widget_set_tooltip_text(phd->drawing_area, histogram_label(phd->histogram));
337 #endif
338         gtk_widget_show(phd->widget);
339
340         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
341
342         return phd->widget;
343 }
344
345 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
346 {
347         gchar *title = g_strdup(_("NoName"));
348         gboolean expanded = TRUE;
349         gint height = 80;
350         gint histogram_channel = HCHAN_RGB;
351         gint histogram_mode = 0;
352
353         while (*attribute_names)
354                 {
355                 const gchar *option = *attribute_names++;
356                 const gchar *value = *attribute_values++;
357
358                 if (READ_CHAR_FULL("pane.title", title)) continue;
359                 if (READ_BOOL_FULL("pane.expanded", expanded)) continue;
360                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
361                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
362
363                 
364                 DEBUG_1("unknown attribute %s = %s", option, value);
365                 }
366         
367         return bar_pane_histogram_new(title, height, expanded, histogram_channel, histogram_mode);
368 }
369
370
371 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */