Fix up bar pane histogram contextual menu: show current state for channel and log...
[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
67
68 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
69 {
70         PaneHistogramData *phd;
71
72         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
73         if (!phd) return;
74
75         file_data_unref(phd->fd);
76         phd->fd = file_data_ref(fd);
77
78         bar_pane_histogram_update(phd);
79 }
80
81 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
82 {
83         PaneHistogramData *phd;
84
85         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
86         if (!phd) return;
87
88         WRITE_STRING("<pane_histogram\n");
89         indent++;
90         WRITE_CHAR(*phd, pane.title);
91         WRITE_BOOL(*phd, pane.expanded);
92         indent--;
93         WRITE_STRING("/>\n");
94 }
95
96
97 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
98 {
99         PaneHistogramData *phd = data;
100         if (fd == phd->fd) bar_pane_histogram_update(phd);
101 }
102
103 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
104 {
105         PaneHistogramData *phd = data;
106         if (!phd || !phd->pixbuf) return TRUE;
107         
108         gdk_draw_pixbuf(widget->window,
109                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
110                         phd->pixbuf,
111                         0, 0,
112                         0, 0,
113                         -1, -1,
114                         GDK_RGB_DITHER_NORMAL, 0, 0);
115         return TRUE;
116 }
117
118 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
119 {
120         PaneHistogramData *phd = data;
121
122         phd->histogram_width = allocation->width;
123         phd->histogram_height = allocation->height;
124         bar_pane_histogram_update(phd);
125 }
126
127 static void bar_pane_histogram_close(GtkWidget *pane)
128 {
129         PaneHistogramData *phd;
130
131         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
132         if (!phd) return;
133
134         gtk_widget_destroy(phd->widget);
135 }
136
137 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
138 {
139         PaneHistogramData *phd = data;
140
141         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
142
143         file_data_unref(phd->fd);
144         g_free(phd->pane.title);
145         histogram_free(phd->histogram);
146         if (phd->pixbuf) g_object_unref(phd->pixbuf);
147
148         g_free(phd);
149 }
150
151 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
152 {
153         PaneHistogramData *phd;
154         gint channel;
155
156         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
157
158         phd = submenu_item_get_data(widget);
159
160         if (!phd) return;
161
162         channel = GPOINTER_TO_INT(data);
163         if (channel == histogram_get_channel(phd->histogram)) return;
164
165         histogram_set_channel(phd->histogram, channel);
166         bar_pane_histogram_update(phd);
167 }
168
169 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
170 {
171         PaneHistogramData *phd;
172         gint logmode;
173         
174         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
175
176         phd = submenu_item_get_data(widget);
177
178         if (!phd) return;
179
180         logmode = GPOINTER_TO_INT(data);
181         if (logmode == histogram_get_mode(phd->histogram)) return;
182
183         histogram_set_mode(phd->histogram, logmode);
184         bar_pane_histogram_update(phd);
185 }
186
187 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
188                                         const gchar *label,
189                                         GCallback func, gint value,
190                                         gboolean show_current, gint current_value)
191 {
192         GtkWidget *item;
193
194         if (show_current)
195                 {
196                 item = menu_item_add_radio(menu, parent,
197                                            label, (value == current_value),
198                                            func, GINT_TO_POINTER((gint)value));
199                 }
200         else
201                 {
202                 item = menu_item_add(menu, label,
203                                      func, GINT_TO_POINTER((gint)value));
204                 }
205
206         return item;
207 }
208
209 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
210                                            gboolean show_current, gint current_value)
211 {
212         GtkWidget *submenu;
213         GtkWidget *parent;
214
215         submenu = gtk_menu_new();
216         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
217
218         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
219         bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
220         bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
221         bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
222         bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
223
224         if (menu)
225                 {
226                 GtkWidget *item;
227
228                 item = menu_item_add(menu, _("Channels"), NULL, NULL);
229                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
230                 return item;
231                 }
232
233         return submenu;
234 }
235 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
236                                            gboolean show_current, gint current_value)
237 {
238         GtkWidget *submenu;
239         GtkWidget *parent;
240
241         submenu = gtk_menu_new();
242         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
243
244         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
245         bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
246
247         if (menu)
248                 {
249                 GtkWidget *item;
250
251                 item = menu_item_add(menu, _("Mode"), NULL, NULL);
252                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
253                 return item;
254                 }
255
256         return submenu;
257 }
258
259
260 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
261 {
262         GtkWidget *menu;
263         static gboolean show_current = TRUE;
264
265         menu = popup_menu_short_lived();
266         bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
267                                         show_current, histogram_get_channel(phd->histogram));
268         bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
269                                        show_current, histogram_get_mode(phd->histogram));
270
271         return menu;
272 }
273
274 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
275 {
276         PaneHistogramData *phd = data;
277
278         if (bevent->button == MOUSE_BUTTON_RIGHT)
279                 {
280                 GtkWidget *menu;
281
282                 menu = bar_pane_histogram_menu(phd);
283                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
284                 return TRUE;
285         }
286
287         return FALSE;
288 }
289
290
291 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gint expanded)
292 {
293         PaneHistogramData *phd;
294
295         phd = g_new0(PaneHistogramData, 1);
296         
297         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
298         phd->pane.pane_write_config = bar_pane_histogram_write_config;
299         phd->pane.title = g_strdup(title);
300         phd->pane.expanded = expanded;
301         
302         phd->histogram = histogram_new();
303
304         histogram_set_channel(phd->histogram, HCHAN_RGB);
305         histogram_set_mode(phd->histogram, 0);
306
307         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
308
309         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
310         g_signal_connect(G_OBJECT(phd->widget), "destroy",
311                          G_CALLBACK(bar_pane_histogram_destroy), phd);
312         
313
314         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
315
316         phd->drawing_area = gtk_drawing_area_new();
317         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
318                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
319
320         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
321                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
322                          
323         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
324         gtk_widget_show(phd->drawing_area);
325         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
326
327         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
328
329         gtk_widget_show(phd->widget);
330
331         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
332
333         return phd->widget;
334 }
335
336 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
337 {
338         gchar *title = g_strdup(_("NoName"));
339         gboolean expanded = TRUE;
340         gint height = 80;
341
342         while (*attribute_names)
343                 {
344                 const gchar *option = *attribute_names++;
345                 const gchar *value = *attribute_values++;
346
347                 if (READ_CHAR_FULL("pane.title", title)) continue;
348                 if (READ_BOOL_FULL("pane.expanded", expanded)) continue;
349                 
350
351                 DEBUG_1("unknown attribute %s = %s", option, value);
352                 }
353         
354         return bar_pane_histogram_new(title, height, expanded);
355 }
356
357
358 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */