Open With feature
[geeqie.git] / src / bar-histogram.cc
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 "filedata.h"
27 #include "ui-menu.h"
28 #include "ui-misc.h"
29 #include "histogram.h"
30 #include "rcfile.h"
31
32 /*
33  *-------------------------------------------------------------------
34  * keyword / comment utils
35  *-------------------------------------------------------------------
36  */
37
38
39
40 struct PaneHistogramData
41 {
42         PaneData pane;
43         GtkWidget *widget;
44         GtkWidget *drawing_area;
45         Histogram *histogram;
46         gint histogram_width;
47         gint histogram_height;
48         GdkPixbuf *pixbuf;
49         FileData *fd;
50         gboolean need_update;
51         guint idle_id; /* event source id */
52 };
53
54 static gboolean bar_pane_histogram_update_cb(gpointer data);
55
56
57 static void bar_pane_histogram_update(PaneHistogramData *phd)
58 {
59         if (phd->pixbuf) g_object_unref(phd->pixbuf);
60         phd->pixbuf = nullptr;
61
62         gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
63
64         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
65
66         /** histmap_get is relatively expensive, run it only when we really need it
67            and with lower priority than pixbuf_renderer
68            @FIXME this does not work for fullscreen */
69         if (gtk_widget_is_drawable(phd->drawing_area))
70                 {
71                 if (!phd->idle_id)
72                         {
73                         phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, nullptr);
74                         }
75                 }
76         else
77                 {
78                 phd->need_update = TRUE;
79                 }
80 }
81
82 static gboolean bar_pane_histogram_update_cb(gpointer data)
83 {
84         const HistMap *histmap;
85         auto phd = static_cast<PaneHistogramData *>(data);
86
87         phd->idle_id = 0;
88         phd->need_update = FALSE;
89
90         gq_gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
91
92         if (phd->fd == nullptr) return G_SOURCE_REMOVE;
93         histmap = histmap_get(phd->fd);
94
95         if (!histmap)
96                 {
97                 histmap_start_idle(phd->fd);
98                 return G_SOURCE_REMOVE;
99                 }
100
101         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
102         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
103         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
104
105         return G_SOURCE_REMOVE;
106 }
107
108
109 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
110 {
111         PaneHistogramData *phd;
112
113         phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
114         if (!phd) return;
115
116         file_data_unref(phd->fd);
117         phd->fd = file_data_ref(fd);
118
119         bar_pane_histogram_update(phd);
120 }
121
122 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
123 {
124         PaneHistogramData *phd;
125
126         phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
127         if (!phd) return;
128
129         WRITE_NL(); WRITE_STRING("<pane_histogram ");
130         write_char_option(outstr, indent, "id", phd->pane.id);
131         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(phd->pane.title)));
132         WRITE_BOOL(phd->pane, expanded);
133         WRITE_INT(*phd->histogram, histogram_channel);
134         WRITE_INT(*phd->histogram, histogram_mode);
135         WRITE_STRING("/>");
136 }
137
138 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
139 {
140         auto phd = static_cast<PaneHistogramData *>(data);
141         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
142                 {
143                 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
144                 bar_pane_histogram_update(phd);
145                 }
146 }
147
148 static gboolean bar_pane_histogram_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
149 {
150         auto phd = static_cast<PaneHistogramData *>(data);
151         if (!phd) return TRUE;
152
153         if (phd->need_update)
154                 {
155                 bar_pane_histogram_update(phd);
156                 }
157
158         if (!phd->pixbuf) return TRUE;
159
160         gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
161         cairo_paint (cr);
162
163         return TRUE;
164 }
165
166 static void bar_pane_histogram_size_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
167 {
168         auto phd = static_cast<PaneHistogramData *>(data);
169
170         phd->histogram_width = allocation->width;
171         phd->histogram_height = allocation->height;
172         bar_pane_histogram_update(phd);
173 }
174
175 static void bar_pane_histogram_destroy(GtkWidget *, gpointer data)
176 {
177         auto phd = static_cast<PaneHistogramData *>(data);
178
179         if (phd->idle_id) g_source_remove(phd->idle_id);
180         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
181
182         file_data_unref(phd->fd);
183         histogram_free(phd->histogram);
184         if (phd->pixbuf) g_object_unref(phd->pixbuf);
185         g_free(phd->pane.id);
186
187         g_free(phd);
188 }
189
190 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
191 {
192         auto phd = static_cast<PaneHistogramData *>(data);
193         gint channel;
194
195         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
196
197         if (!phd) return;
198
199         channel = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
200         if (channel == histogram_get_channel(phd->histogram)) return;
201
202         histogram_set_channel(phd->histogram, channel);
203         bar_pane_histogram_update(phd);
204 }
205
206 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
207 {
208         auto phd = static_cast<PaneHistogramData *>(data);
209         gint logmode;
210
211         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
212
213         if (!phd) return;
214
215         logmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
216         if (logmode == histogram_get_mode(phd->histogram)) return;
217
218         histogram_set_mode(phd->histogram, logmode);
219         bar_pane_histogram_update(phd);
220 }
221
222 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
223 {
224         GtkWidget *menu;
225         gint channel = histogram_get_channel(phd->histogram);
226         gint mode = histogram_get_mode(phd->histogram);
227
228         menu = popup_menu_short_lived();
229
230         /* use the same strings as in layout-util.cc */
231         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);
232         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);
233         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);
234         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);
235         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);
236
237         menu_item_add_divider(menu);
238
239         menu_item_add_radio(menu, _("Li_near Histogram"), GINT_TO_POINTER(0), (mode == 0), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
240         menu_item_add_radio(menu, _("L_og Histogram"),    GINT_TO_POINTER(1), (mode == 1), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
241
242         return menu;
243 }
244
245 static gboolean bar_pane_histogram_press_cb(GtkGesture *, gint, gdouble, gdouble, gpointer data)
246 {
247         auto phd = static_cast<PaneHistogramData *>(data);
248         GtkWidget *menu;
249
250         menu = bar_pane_histogram_menu(phd);
251         gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
252
253         return TRUE;
254 }
255
256
257 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
258 {
259         PaneHistogramData *phd;
260         GtkGesture *gesture;
261
262         phd = g_new0(PaneHistogramData, 1);
263
264         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
265         phd->pane.pane_write_config = bar_pane_histogram_write_config;
266         phd->pane.title = bar_pane_expander_title(title);
267         phd->pane.id = g_strdup(id);
268         phd->pane.type = PANE_HISTOGRAM;
269
270         phd->pane.expanded = expanded;
271
272         phd->histogram = histogram_new();
273
274         histogram_set_channel(phd->histogram, histogram_channel);
275         histogram_set_mode(phd->histogram, histogram_mode);
276
277         phd->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
278
279         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
280         g_signal_connect(G_OBJECT(phd->widget), "destroy",
281                          G_CALLBACK(bar_pane_histogram_destroy), phd);
282
283
284         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
285
286         phd->drawing_area = gtk_drawing_area_new();
287         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
288                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
289
290         g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
291                          G_CALLBACK(bar_pane_histogram_draw_cb), phd);
292
293         gq_gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
294         gtk_widget_show(phd->drawing_area);
295         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
296
297
298 #ifdef HAVE_GTK4
299         gesture = gtk_gesture_click_new();
300         gtk_widget_add_controller(phd->drawing_area, GTK_EVENT_CONTROLLER(gesture));
301 #else
302         gesture = gtk_gesture_multi_press_new(phd->drawing_area);
303 #endif
304         gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(gesture), MOUSE_BUTTON_RIGHT);
305         g_signal_connect(gesture, "pressed", G_CALLBACK(bar_pane_histogram_press_cb), phd);
306
307         gtk_widget_show(phd->widget);
308
309         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
310
311         return phd->widget;
312 }
313
314 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
315 {
316         gchar *title = nullptr;
317         gchar *id = g_strdup("histogram");
318         gboolean expanded = TRUE;
319         gint height = 80;
320         gint histogram_channel = HCHAN_RGB;
321         gint histogram_mode = 0;
322         GtkWidget *ret;
323
324         while (*attribute_names)
325                 {
326                 const gchar *option = *attribute_names++;
327                 const gchar *value = *attribute_values++;
328
329                 if (READ_CHAR_FULL("id", id)) continue;
330                 if (READ_CHAR_FULL("title", title)) continue;
331                 if (READ_BOOL_FULL("expanded", expanded)) continue;
332                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
333                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
334
335                 log_printf("unknown attribute %s = %s\n", option, value);
336                 }
337
338         bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
339         ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
340         g_free(title);
341         g_free(id);
342         return ret;
343 }
344
345 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
346 {
347         PaneHistogramData *phd;
348
349         phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
350         if (!phd) return;
351
352         gint histogram_channel = phd->histogram->histogram_channel;
353         gint histogram_mode = phd->histogram->histogram_mode;
354
355         while (*attribute_names)
356                 {
357                 const gchar *option = *attribute_names++;
358                 const gchar *value = *attribute_values++;
359
360                 if (READ_CHAR_FULL("id", phd->pane.id)) 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: */