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.
22 #include "bar-histogram.h"
25 #include <gdk-pixbuf/gdk-pixbuf.h>
27 #include <glib-object.h>
35 #include "histogram.h"
45 *-------------------------------------------------------------------
46 * keyword / comment utils
47 *-------------------------------------------------------------------
52 struct PaneHistogramData
56 GtkWidget *drawing_area;
59 gint histogram_height;
63 guint idle_id; /* event source id */
66 static gboolean bar_pane_histogram_update_cb(gpointer data);
69 static void bar_pane_histogram_update(PaneHistogramData *phd)
71 if (phd->pixbuf) g_object_unref(phd->pixbuf);
72 phd->pixbuf = nullptr;
74 gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
76 if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
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))
85 phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, nullptr);
90 phd->need_update = TRUE;
94 static gboolean bar_pane_histogram_update_cb(gpointer data)
96 const HistMap *histmap;
97 auto phd = static_cast<PaneHistogramData *>(data);
100 phd->need_update = FALSE;
102 gq_gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
104 if (phd->fd == nullptr) return G_SOURCE_REMOVE;
105 histmap = histmap_get(phd->fd);
109 histmap_start_idle(phd->fd);
110 return G_SOURCE_REMOVE;
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);
117 return G_SOURCE_REMOVE;
121 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
123 PaneHistogramData *phd;
125 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
128 file_data_unref(phd->fd);
129 phd->fd = file_data_ref(fd);
131 bar_pane_histogram_update(phd);
134 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
136 PaneHistogramData *phd;
138 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
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);
150 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
152 auto phd = static_cast<PaneHistogramData *>(data);
153 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
155 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
156 bar_pane_histogram_update(phd);
160 static gboolean bar_pane_histogram_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
162 auto phd = static_cast<PaneHistogramData *>(data);
163 if (!phd) return TRUE;
165 if (phd->need_update)
167 bar_pane_histogram_update(phd);
170 if (!phd->pixbuf) return TRUE;
172 gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
178 static void bar_pane_histogram_size_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
180 auto phd = static_cast<PaneHistogramData *>(data);
182 phd->histogram_width = allocation->width;
183 phd->histogram_height = allocation->height;
184 bar_pane_histogram_update(phd);
187 static void bar_pane_histogram_destroy(GtkWidget *, gpointer data)
189 auto phd = static_cast<PaneHistogramData *>(data);
191 if (phd->idle_id) g_source_remove(phd->idle_id);
192 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
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);
202 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
204 auto phd = static_cast<PaneHistogramData *>(data);
207 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
214 histogram_set_channel(phd->histogram, channel);
215 bar_pane_histogram_update(phd);
218 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
220 auto phd = static_cast<PaneHistogramData *>(data);
223 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
230 histogram_set_mode(phd->histogram, logmode);
231 bar_pane_histogram_update(phd);
234 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
237 gint channel = histogram_get_channel(phd->histogram);
238 gint mode = histogram_get_mode(phd->histogram);
240 menu = popup_menu_short_lived();
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);
249 menu_item_add_divider(menu);
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);
257 static gboolean bar_pane_histogram_press_cb(GtkGesture *, gint, gdouble, gdouble, gpointer data)
259 auto phd = static_cast<PaneHistogramData *>(data);
262 menu = bar_pane_histogram_menu(phd);
263 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
269 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
271 PaneHistogramData *phd;
274 phd = g_new0(PaneHistogramData, 1);
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;
282 phd->pane.expanded = expanded;
284 phd->histogram = histogram_new();
286 histogram_set_channel(phd->histogram, histogram_channel);
287 histogram_set_mode(phd->histogram, histogram_mode);
289 phd->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
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);
296 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
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);
302 g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
303 G_CALLBACK(bar_pane_histogram_draw_cb), phd);
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);
311 gesture = gtk_gesture_click_new();
312 gtk_widget_add_controller(phd->drawing_area, GTK_EVENT_CONTROLLER(gesture));
314 gesture = gtk_gesture_multi_press_new(phd->drawing_area);
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);
319 gtk_widget_show(phd->widget);
321 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
326 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
328 gchar *title = nullptr;
329 gchar *id = g_strdup("histogram");
330 gboolean expanded = TRUE;
332 gint histogram_channel = HCHAN_RGB;
333 gint histogram_mode = 0;
336 while (*attribute_names)
338 const gchar *option = *attribute_names++;
339 const gchar *value = *attribute_values++;
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;
347 log_printf("unknown attribute %s = %s\n", option, value);
350 bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
351 ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
357 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
359 PaneHistogramData *phd;
361 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
364 gint histogram_channel = phd->histogram->histogram_channel;
365 gint histogram_mode = phd->histogram->histogram_mode;
367 while (*attribute_names)
369 const gchar *option = *attribute_names++;
370 const gchar *value = *attribute_values++;
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;
378 log_printf("unknown attribute %s = %s\n", option, value);
381 histogram_set_channel(phd->histogram, histogram_channel);
382 histogram_set_mode(phd->histogram, histogram_mode);
384 bar_update_expander(pane);
385 bar_pane_histogram_update(phd);
389 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */