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