Simplify vflist_get_formatted()
[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 #if GTK_CHECK_VERSION(3,0,0)
152 static gboolean bar_pane_histogram_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
153 {
154         PaneHistogramData *phd = data;
155         if (!phd) return TRUE;
156
157         if (phd->need_update)
158                 {
159                 bar_pane_histogram_update(phd);
160                 }
161
162         if (!phd->pixbuf) return TRUE;
163
164         gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
165         cairo_paint (cr);
166
167         return TRUE;
168 }
169
170 #else
171 static gboolean bar_pane_histogram_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
172 {
173         PaneHistogramData *phd = data;
174         if (!phd) return TRUE;
175
176         if (phd->need_update)
177                 {
178                 bar_pane_histogram_update(phd);
179                 }
180
181         if (!phd->pixbuf) return TRUE;
182
183         cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
184         gdk_cairo_set_source_pixbuf (cr, phd->pixbuf, 0, 0);
185         cairo_paint (cr);
186         cairo_destroy (cr);
187
188         return TRUE;
189 }
190 #endif
191
192 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
193 {
194         PaneHistogramData *phd = data;
195
196         phd->histogram_width = allocation->width;
197         phd->histogram_height = allocation->height;
198         bar_pane_histogram_update(phd);
199 }
200
201 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
202 {
203         PaneHistogramData *phd = data;
204
205         if (phd->idle_id) g_source_remove(phd->idle_id);
206         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
207
208         file_data_unref(phd->fd);
209         histogram_free(phd->histogram);
210         if (phd->pixbuf) g_object_unref(phd->pixbuf);
211         g_free(phd->pane.id);
212
213         g_free(phd);
214 }
215
216 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
217 {
218         PaneHistogramData *phd = data;
219         gint channel;
220
221         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
222
223         if (!phd) return;
224
225         channel = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
226         if (channel == histogram_get_channel(phd->histogram)) return;
227
228         histogram_set_channel(phd->histogram, channel);
229         bar_pane_histogram_update(phd);
230 }
231
232 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
233 {
234         PaneHistogramData *phd = data;
235         gint logmode;
236
237         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
238
239         if (!phd) return;
240
241         logmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
242         if (logmode == histogram_get_mode(phd->histogram)) return;
243
244         histogram_set_mode(phd->histogram, logmode);
245         bar_pane_histogram_update(phd);
246 }
247
248 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
249 {
250         GtkWidget *menu;
251         gint channel = histogram_get_channel(phd->histogram);
252         gint mode = histogram_get_mode(phd->histogram);
253
254         menu = popup_menu_short_lived();
255
256         /* use the same strings as in layout_util.c */
257         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);
258         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);
259         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);
260         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);
261         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);
262
263         menu_item_add_divider(menu);
264
265         menu_item_add_radio(menu, _("Li_near Histogram"), GINT_TO_POINTER(0), (mode == 0), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
266         menu_item_add_radio(menu, _("L_og Histogram"),    GINT_TO_POINTER(1), (mode == 1), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
267
268         return menu;
269 }
270
271 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
272 {
273         PaneHistogramData *phd = data;
274
275         if (bevent->button == MOUSE_BUTTON_RIGHT)
276                 {
277                 GtkWidget *menu;
278
279                 menu = bar_pane_histogram_menu(phd);
280                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
281                 return TRUE;
282         }
283
284         return FALSE;
285 }
286
287
288 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
289 {
290         PaneHistogramData *phd;
291
292         phd = g_new0(PaneHistogramData, 1);
293
294         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
295         phd->pane.pane_write_config = bar_pane_histogram_write_config;
296         phd->pane.title = bar_pane_expander_title(title);
297         phd->pane.id = g_strdup(id);
298         phd->pane.type = PANE_HISTOGRAM;
299
300         phd->pane.expanded = expanded;
301
302         phd->histogram = histogram_new();
303
304         histogram_set_channel(phd->histogram, histogram_channel);
305         histogram_set_mode(phd->histogram, histogram_mode);
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 #if GTK_CHECK_VERSION(3,0,0)
321         g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
322                          G_CALLBACK(bar_pane_histogram_draw_cb), phd);
323 #else
324         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",
325                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
326 #endif
327
328         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
329         gtk_widget_show(phd->drawing_area);
330         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
331
332         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
333
334         gtk_widget_show(phd->widget);
335
336         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
337
338         return phd->widget;
339 }
340
341 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
342 {
343         gchar *title = NULL;
344         gchar *id = g_strdup("histogram");
345         gboolean expanded = TRUE;
346         gint height = 80;
347         gint histogram_channel = HCHAN_RGB;
348         gint histogram_mode = 0;
349         GtkWidget *ret;
350
351         while (*attribute_names)
352                 {
353                 const gchar *option = *attribute_names++;
354                 const gchar *value = *attribute_values++;
355
356                 if (READ_CHAR_FULL("id", id)) continue;
357                 if (READ_CHAR_FULL("title", title)) continue;
358                 if (READ_BOOL_FULL("expanded", expanded)) continue;
359                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
360                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
361
362                 log_printf("unknown attribute %s = %s\n", option, value);
363                 }
364
365         bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
366         ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
367         g_free(title);
368         g_free(id);
369         return ret;
370 }
371
372 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
373 {
374         PaneHistogramData *phd;
375
376         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
377         if (!phd) return;
378
379         gint histogram_channel = phd->histogram->histogram_channel;
380         gint histogram_mode = phd->histogram->histogram_mode;
381
382         while (*attribute_names)
383                 {
384                 const gchar *option = *attribute_names++;
385                 const gchar *value = *attribute_values++;
386
387                 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
388 //              if (READ_CHAR_FULL("pane.title", title)) continue;
389                 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
390                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
391                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
392
393
394                 log_printf("unknown attribute %s = %s\n", option, value);
395                 }
396
397         histogram_set_channel(phd->histogram, histogram_channel);
398         histogram_set_mode(phd->histogram, histogram_mode);
399
400         bar_update_expander(pane);
401         bar_pane_histogram_update(phd);
402 }
403
404
405 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */