2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * Author: Vladimir Nadvornik
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.
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.
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.
23 #include "bar-histogram.h"
29 #include "histogram.h"
33 *-------------------------------------------------------------------
34 * keyword / comment utils
35 *-------------------------------------------------------------------
40 struct PaneHistogramData
44 GtkWidget *drawing_area;
47 gint histogram_height;
51 guint idle_id; /* event source id */
54 static gboolean bar_pane_histogram_update_cb(gpointer data);
57 static void bar_pane_histogram_update(PaneHistogramData *phd)
59 if (phd->pixbuf) g_object_unref(phd->pixbuf);
60 phd->pixbuf = nullptr;
62 gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
64 if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
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))
73 phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, nullptr);
78 phd->need_update = TRUE;
82 static gboolean bar_pane_histogram_update_cb(gpointer data)
84 const HistMap *histmap;
85 auto phd = static_cast<PaneHistogramData *>(data);
88 phd->need_update = FALSE;
90 gq_gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
92 if (phd->fd == nullptr) return G_SOURCE_REMOVE;
93 histmap = histmap_get(phd->fd);
97 histmap_start_idle(phd->fd);
98 return G_SOURCE_REMOVE;
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);
105 return G_SOURCE_REMOVE;
109 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
111 PaneHistogramData *phd;
113 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
116 file_data_unref(phd->fd);
117 phd->fd = file_data_ref(fd);
119 bar_pane_histogram_update(phd);
122 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
124 PaneHistogramData *phd;
126 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
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);
138 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
140 auto phd = static_cast<PaneHistogramData *>(data);
141 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
143 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
144 bar_pane_histogram_update(phd);
148 static gboolean bar_pane_histogram_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
150 auto phd = static_cast<PaneHistogramData *>(data);
151 if (!phd) return TRUE;
153 if (phd->need_update)
155 bar_pane_histogram_update(phd);
158 if (!phd->pixbuf) return TRUE;
160 gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
166 static void bar_pane_histogram_size_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
168 auto phd = static_cast<PaneHistogramData *>(data);
170 phd->histogram_width = allocation->width;
171 phd->histogram_height = allocation->height;
172 bar_pane_histogram_update(phd);
175 static void bar_pane_histogram_destroy(GtkWidget *, gpointer data)
177 auto phd = static_cast<PaneHistogramData *>(data);
179 if (phd->idle_id) g_source_remove(phd->idle_id);
180 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
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);
190 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
192 auto phd = static_cast<PaneHistogramData *>(data);
195 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
202 histogram_set_channel(phd->histogram, channel);
203 bar_pane_histogram_update(phd);
206 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
208 auto phd = static_cast<PaneHistogramData *>(data);
211 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
218 histogram_set_mode(phd->histogram, logmode);
219 bar_pane_histogram_update(phd);
222 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
225 gint channel = histogram_get_channel(phd->histogram);
226 gint mode = histogram_get_mode(phd->histogram);
228 menu = popup_menu_short_lived();
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);
237 menu_item_add_divider(menu);
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);
245 static gboolean bar_pane_histogram_press_cb(GtkGesture *, gint, gdouble, gdouble, gpointer data)
247 auto phd = static_cast<PaneHistogramData *>(data);
250 menu = bar_pane_histogram_menu(phd);
251 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
257 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
259 PaneHistogramData *phd;
262 phd = g_new0(PaneHistogramData, 1);
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;
270 phd->pane.expanded = expanded;
272 phd->histogram = histogram_new();
274 histogram_set_channel(phd->histogram, histogram_channel);
275 histogram_set_mode(phd->histogram, histogram_mode);
277 phd->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
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);
284 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
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);
290 g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
291 G_CALLBACK(bar_pane_histogram_draw_cb), phd);
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);
299 gesture = gtk_gesture_click_new();
300 gtk_widget_add_controller(phd->drawing_area, GTK_EVENT_CONTROLLER(gesture));
302 gesture = gtk_gesture_multi_press_new(phd->drawing_area);
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);
307 gtk_widget_show(phd->widget);
309 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
314 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
316 gchar *title = nullptr;
317 gchar *id = g_strdup("histogram");
318 gboolean expanded = TRUE;
320 gint histogram_channel = HCHAN_RGB;
321 gint histogram_mode = 0;
324 while (*attribute_names)
326 const gchar *option = *attribute_names++;
327 const gchar *value = *attribute_values++;
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;
335 log_printf("unknown attribute %s = %s\n", option, value);
338 bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
339 ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
345 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
347 PaneHistogramData *phd;
349 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
352 gint histogram_channel = phd->histogram->histogram_channel;
353 gint histogram_mode = phd->histogram->histogram_mode;
355 while (*attribute_names)
357 const gchar *option = *attribute_names++;
358 const gchar *value = *attribute_values++;
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;
366 log_printf("unknown attribute %s = %s\n", option, value);
369 histogram_set_channel(phd->histogram, histogram_channel);
370 histogram_set_mode(phd->histogram, histogram_mode);
372 bar_update_expander(pane);
373 bar_pane_histogram_update(phd);
377 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */