49b3d12bde9a445e2a25798c28c48bd99b94b719
[geeqie.git] / src / bar_histogram.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "bar_histogram.h"
24
25 #include "bar.h"
26 #include "metadata.h"
27 #include "filedata.h"
28 #include "menu.h"
29 #include "ui_menu.h"
30 #include "ui_misc.h"
31 #include "histogram.h"
32 #include "rcfile.h"
33
34 /*
35  *-------------------------------------------------------------------
36  * keyword / comment utils
37  *-------------------------------------------------------------------
38  */
39
40
41
42 typedef struct _PaneHistogramData PaneHistogramData;
43 struct _PaneHistogramData
44 {
45         PaneData pane;
46         GtkWidget *widget;
47         GtkWidget *drawing_area;
48         Histogram *histogram;
49         gint histogram_width;
50         gint histogram_height;
51         GdkPixbuf *pixbuf;
52         FileData *fd;
53         gboolean need_update;
54         guint idle_id; /* event source id */
55 };
56
57 static gboolean bar_pane_histogram_update_cb(gpointer data);
58
59
60 static void bar_pane_histogram_update(PaneHistogramData *phd)
61 {
62         if (phd->pixbuf) g_object_unref(phd->pixbuf);
63         phd->pixbuf = NULL;
64
65         gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
66
67         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
68
69         /** histmap_get is relatively expensive, run it only when we really need it
70            and with lower priority than pixbuf_renderer
71            @FIXME this does not work for fullscreen */
72         if (gtk_widget_is_drawable(phd->drawing_area))
73                 {
74                 if (!phd->idle_id)
75                         {
76                         phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
77                         }
78                 }
79         else
80                 {
81                 phd->need_update = TRUE;
82                 }
83 }
84
85 static gboolean bar_pane_histogram_update_cb(gpointer data)
86 {
87         const HistMap *histmap;
88         PaneHistogramData *phd = data;
89
90         phd->idle_id = 0;
91         phd->need_update = FALSE;
92
93         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
94
95         if (phd->fd == NULL) return FALSE;
96         histmap = histmap_get(phd->fd);
97
98         if (!histmap)
99                 {
100                 histmap_start_idle(phd->fd);
101                 return FALSE;
102                 }
103
104         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
105         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
106         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
107
108         return FALSE;
109 }
110
111
112 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
113 {
114         PaneHistogramData *phd;
115
116         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
117         if (!phd) return;
118
119         file_data_unref(phd->fd);
120         phd->fd = file_data_ref(fd);
121
122         bar_pane_histogram_update(phd);
123 }
124
125 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
126 {
127         PaneHistogramData *phd;
128
129         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
130         if (!phd) return;
131
132         WRITE_NL(); WRITE_STRING("<pane_histogram ");
133         write_char_option(outstr, indent, "id", phd->pane.id);
134         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(phd->pane.title)));
135         WRITE_BOOL(phd->pane, expanded);
136         WRITE_INT(*phd->histogram, histogram_channel);
137         WRITE_INT(*phd->histogram, histogram_mode);
138         WRITE_STRING("/>");
139 }
140
141 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
142 {
143         PaneHistogramData *phd = data;
144         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
145                 {
146                 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
147                 bar_pane_histogram_update(phd);
148                 }
149 }
150
151 static gboolean bar_pane_histogram_draw_cb(GtkWidget *UNUSED(widget), cairo_t *cr, gpointer data)
152 {
153         PaneHistogramData *phd = data;
154         if (!phd) return TRUE;
155
156         if (phd->need_update)
157                 {
158                 bar_pane_histogram_update(phd);
159                 }
160
161         if (!phd->pixbuf) return TRUE;
162
163         gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
164         cairo_paint (cr);
165
166         return TRUE;
167 }
168
169 static void bar_pane_histogram_size_cb(GtkWidget *UNUSED(widget), GtkAllocation *allocation, gpointer data)
170 {
171         PaneHistogramData *phd = data;
172
173         phd->histogram_width = allocation->width;
174         phd->histogram_height = allocation->height;
175         bar_pane_histogram_update(phd);
176 }
177
178 static void bar_pane_histogram_destroy(GtkWidget *UNUSED(widget), gpointer data)
179 {
180         PaneHistogramData *phd = data;
181
182         if (phd->idle_id) g_source_remove(phd->idle_id);
183         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
184
185         file_data_unref(phd->fd);
186         histogram_free(phd->histogram);
187         if (phd->pixbuf) g_object_unref(phd->pixbuf);
188         g_free(phd->pane.id);
189
190         g_free(phd);
191 }
192
193 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
194 {
195         PaneHistogramData *phd = data;
196         gint channel;
197
198         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
199
200         if (!phd) return;
201
202         channel = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
203         if (channel == histogram_get_channel(phd->histogram)) return;
204
205         histogram_set_channel(phd->histogram, channel);
206         bar_pane_histogram_update(phd);
207 }
208
209 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
210 {
211         PaneHistogramData *phd = data;
212         gint logmode;
213
214         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
215
216         if (!phd) return;
217
218         logmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
219         if (logmode == histogram_get_mode(phd->histogram)) return;
220
221         histogram_set_mode(phd->histogram, logmode);
222         bar_pane_histogram_update(phd);
223 }
224
225 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
226 {
227         GtkWidget *menu;
228         gint channel = histogram_get_channel(phd->histogram);
229         gint mode = histogram_get_mode(phd->histogram);
230
231         menu = popup_menu_short_lived();
232
233         /* use the same strings as in layout_util.c */
234         menu_item_add_radio(menu, _("Histogram on _Red"),   GINT_TO_POINTER(HCHAN_R), (channel == HCHAN_R), G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd);
235         menu_item_add_radio(menu, _("Histogram on _Green"), GINT_TO_POINTER(HCHAN_G), (channel == HCHAN_G), G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd);
236         menu_item_add_radio(menu, _("Histogram on _Blue"),  GINT_TO_POINTER(HCHAN_B), (channel == HCHAN_B), G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd);
237         menu_item_add_radio(menu, _("_Histogram on RGB"),   GINT_TO_POINTER(HCHAN_RGB), (channel == HCHAN_RGB), G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd);
238         menu_item_add_radio(menu, _("Histogram on _Value"), GINT_TO_POINTER(HCHAN_MAX), (channel == HCHAN_MAX), G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd);
239
240         menu_item_add_divider(menu);
241
242         menu_item_add_radio(menu, _("Li_near Histogram"), GINT_TO_POINTER(0), (mode == 0), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
243         menu_item_add_radio(menu, _("L_og Histogram"),    GINT_TO_POINTER(1), (mode == 1), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
244
245         return menu;
246 }
247
248 static gboolean bar_pane_histogram_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *bevent, gpointer data)
249 {
250         PaneHistogramData *phd = data;
251
252         if (bevent->button == MOUSE_BUTTON_RIGHT)
253                 {
254                 GtkWidget *menu;
255
256                 menu = bar_pane_histogram_menu(phd);
257                 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
258                 return TRUE;
259         }
260
261         return FALSE;
262 }
263
264
265 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
266 {
267         PaneHistogramData *phd;
268
269         phd = g_new0(PaneHistogramData, 1);
270
271         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
272         phd->pane.pane_write_config = bar_pane_histogram_write_config;
273         phd->pane.title = bar_pane_expander_title(title);
274         phd->pane.id = g_strdup(id);
275         phd->pane.type = PANE_HISTOGRAM;
276
277         phd->pane.expanded = expanded;
278
279         phd->histogram = histogram_new();
280
281         histogram_set_channel(phd->histogram, histogram_channel);
282         histogram_set_mode(phd->histogram, histogram_mode);
283
284         phd->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
285
286         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
287         g_signal_connect(G_OBJECT(phd->widget), "destroy",
288                          G_CALLBACK(bar_pane_histogram_destroy), phd);
289
290
291         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
292
293         phd->drawing_area = gtk_drawing_area_new();
294         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
295                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
296
297         g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
298                          G_CALLBACK(bar_pane_histogram_draw_cb), phd);
299
300         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
301         gtk_widget_show(phd->drawing_area);
302         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
303
304         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
305
306         gtk_widget_show(phd->widget);
307
308         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
309
310         return phd->widget;
311 }
312
313 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
314 {
315         gchar *title = NULL;
316         gchar *id = g_strdup("histogram");
317         gboolean expanded = TRUE;
318         gint height = 80;
319         gint histogram_channel = HCHAN_RGB;
320         gint histogram_mode = 0;
321         GtkWidget *ret;
322
323         while (*attribute_names)
324                 {
325                 const gchar *option = *attribute_names++;
326                 const gchar *value = *attribute_values++;
327
328                 if (READ_CHAR_FULL("id", id)) continue;
329                 if (READ_CHAR_FULL("title", title)) continue;
330                 if (READ_BOOL_FULL("expanded", expanded)) continue;
331                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
332                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
333
334                 log_printf("unknown attribute %s = %s\n", option, value);
335                 }
336
337         bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
338         ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
339         g_free(title);
340         g_free(id);
341         return ret;
342 }
343
344 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
345 {
346         PaneHistogramData *phd;
347
348         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
349         if (!phd) return;
350
351         gint histogram_channel = phd->histogram->histogram_channel;
352         gint histogram_mode = phd->histogram->histogram_mode;
353
354         while (*attribute_names)
355                 {
356                 const gchar *option = *attribute_names++;
357                 const gchar *value = *attribute_values++;
358
359                 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
360 //              if (READ_CHAR_FULL("pane.title", title)) continue;
361                 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
362                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
363                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
364
365
366                 log_printf("unknown attribute %s = %s\n", option, value);
367                 }
368
369         histogram_set_channel(phd->histogram, histogram_channel);
370         histogram_set_mode(phd->histogram, histogram_mode);
371
372         bar_update_expander(pane);
373         bar_pane_histogram_update(phd);
374 }
375
376
377 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */