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