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