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"
30 #include "histogram.h"
37 *-------------------------------------------------------------------
38 * keyword / comment utils
39 *-------------------------------------------------------------------
44 struct PaneHistogramData
48 GtkWidget *drawing_area;
51 gint histogram_height;
55 guint idle_id; /* event source id */
58 static gboolean bar_pane_histogram_update_cb(gpointer data);
61 static void bar_pane_histogram_update(PaneHistogramData *phd)
63 if (phd->pixbuf) g_object_unref(phd->pixbuf);
64 phd->pixbuf = nullptr;
66 gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
68 if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
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))
77 phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, nullptr);
82 phd->need_update = TRUE;
86 static gboolean bar_pane_histogram_update_cb(gpointer data)
88 const HistMap *histmap;
89 auto phd = static_cast<PaneHistogramData *>(data);
92 phd->need_update = FALSE;
94 gq_gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
96 if (phd->fd == nullptr) return G_SOURCE_REMOVE;
97 histmap = histmap_get(phd->fd);
101 histmap_start_idle(phd->fd);
102 return G_SOURCE_REMOVE;
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);
109 return G_SOURCE_REMOVE;
113 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
115 PaneHistogramData *phd;
117 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
120 file_data_unref(phd->fd);
121 phd->fd = file_data_ref(fd);
123 bar_pane_histogram_update(phd);
126 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
128 PaneHistogramData *phd;
130 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
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);
142 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
144 auto phd = static_cast<PaneHistogramData *>(data);
145 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
147 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
148 bar_pane_histogram_update(phd);
152 static gboolean bar_pane_histogram_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
154 auto phd = static_cast<PaneHistogramData *>(data);
155 if (!phd) return TRUE;
157 if (phd->need_update)
159 bar_pane_histogram_update(phd);
162 if (!phd->pixbuf) return TRUE;
164 gdk_cairo_set_source_pixbuf(cr, phd->pixbuf, 0, 0);
170 static void bar_pane_histogram_size_cb(GtkWidget *, GtkAllocation *allocation, gpointer data)
172 auto phd = static_cast<PaneHistogramData *>(data);
174 phd->histogram_width = allocation->width;
175 phd->histogram_height = allocation->height;
176 bar_pane_histogram_update(phd);
179 static void bar_pane_histogram_destroy(GtkWidget *, gpointer data)
181 auto phd = static_cast<PaneHistogramData *>(data);
183 if (phd->idle_id) g_source_remove(phd->idle_id);
184 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
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);
194 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
196 auto phd = static_cast<PaneHistogramData *>(data);
199 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
206 histogram_set_channel(phd->histogram, channel);
207 bar_pane_histogram_update(phd);
210 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
212 auto phd = static_cast<PaneHistogramData *>(data);
215 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
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;
222 histogram_set_mode(phd->histogram, logmode);
223 bar_pane_histogram_update(phd);
226 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
229 gint channel = histogram_get_channel(phd->histogram);
230 gint mode = histogram_get_mode(phd->histogram);
232 menu = popup_menu_short_lived();
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);
241 menu_item_add_divider(menu);
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);
249 static gboolean bar_pane_histogram_press_cb(GtkGesture *, gint, gdouble, gdouble, gpointer data)
251 auto phd = static_cast<PaneHistogramData *>(data);
254 menu = bar_pane_histogram_menu(phd);
255 gtk_menu_popup_at_pointer(GTK_MENU(menu), nullptr);
261 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
263 PaneHistogramData *phd;
266 phd = g_new0(PaneHistogramData, 1);
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;
274 phd->pane.expanded = expanded;
276 phd->histogram = histogram_new();
278 histogram_set_channel(phd->histogram, histogram_channel);
279 histogram_set_mode(phd->histogram, histogram_mode);
281 phd->widget = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
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);
288 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
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);
294 g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
295 G_CALLBACK(bar_pane_histogram_draw_cb), phd);
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);
303 gesture = gtk_gesture_click_new();
304 gtk_widget_add_controller(phd->drawing_area, GTK_EVENT_CONTROLLER(gesture));
306 gesture = gtk_gesture_multi_press_new(phd->drawing_area);
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);
311 gtk_widget_show(phd->widget);
313 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
318 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
320 gchar *title = nullptr;
321 gchar *id = g_strdup("histogram");
322 gboolean expanded = TRUE;
324 gint histogram_channel = HCHAN_RGB;
325 gint histogram_mode = 0;
328 while (*attribute_names)
330 const gchar *option = *attribute_names++;
331 const gchar *value = *attribute_values++;
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;
339 log_printf("unknown attribute %s = %s\n", option, value);
342 bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
343 ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
349 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
351 PaneHistogramData *phd;
353 phd = static_cast<PaneHistogramData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
356 gint histogram_channel = phd->histogram->histogram_channel;
357 gint histogram_mode = phd->histogram->histogram_mode;
359 while (*attribute_names)
361 const gchar *option = *attribute_names++;
362 const gchar *value = *attribute_values++;
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;
370 log_printf("unknown attribute %s = %s\n", option, value);
373 histogram_set_channel(phd->histogram, histogram_channel);
374 histogram_set_mode(phd->histogram, histogram_mode);
376 bar_update_expander(pane);
377 bar_pane_histogram_update(phd);
381 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */