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