4 * Copyright (C) 2008 - 2012 The Geeqie Team
6 * Author: Vladimir Nadvornik
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "bar_histogram.h"
23 #include "histogram.h"
27 *-------------------------------------------------------------------
28 * keyword / comment utils
29 *-------------------------------------------------------------------
34 typedef struct _PaneHistogramData PaneHistogramData;
35 struct _PaneHistogramData
39 GtkWidget *drawing_area;
42 gint histogram_height;
46 guint idle_id; /* event source id */
49 static gboolean bar_pane_histogram_update_cb(gpointer data);
52 static void bar_pane_histogram_update(PaneHistogramData *phd)
54 if (phd->pixbuf) g_object_unref(phd->pixbuf);
57 gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
59 if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
61 /* histmap_get is relatively expensive, run it only when we really need it
62 and with lower priority than pixbuf_renderer
63 FIXME: this does not work for fullscreen*/
64 #if GTK_CHECK_VERSION(2,20,0)
65 if (gtk_widget_is_drawable(phd->drawing_area))
67 if (GTK_WIDGET_DRAWABLE(phd->drawing_area))
72 phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
77 phd->need_update = TRUE;
81 static gboolean bar_pane_histogram_update_cb(gpointer data)
83 const HistMap *histmap;
84 PaneHistogramData *phd = data;
87 phd->need_update = FALSE;
89 gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
91 if (phd->fd == NULL) return FALSE;
92 histmap = histmap_get(phd->fd);
96 histmap_start_idle(phd->fd);
100 phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
101 gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
102 histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
108 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
110 PaneHistogramData *phd;
112 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
115 file_data_unref(phd->fd);
116 phd->fd = file_data_ref(fd);
118 bar_pane_histogram_update(phd);
121 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
123 PaneHistogramData *phd;
125 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
128 WRITE_NL(); WRITE_STRING("<pane_histogram ");
129 write_char_option(outstr, indent, "id", phd->pane.id);
130 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(phd->pane.title)));
131 WRITE_BOOL(phd->pane, expanded);
132 WRITE_INT(*phd->histogram, histogram_channel);
133 WRITE_INT(*phd->histogram, histogram_mode);
137 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
139 PaneHistogramData *phd = data;
140 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
142 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
143 bar_pane_histogram_update(phd);
147 #if GTK_CHECK_VERSION(3,0,0)
148 static gboolean bar_pane_histogram_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
150 PaneHistogramData *phd = 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);
167 static gboolean bar_pane_histogram_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
169 PaneHistogramData *phd = data;
170 if (!phd) return TRUE;
172 if (phd->need_update)
174 bar_pane_histogram_update(phd);
177 if (!phd->pixbuf) return TRUE;
179 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
180 gdk_cairo_set_source_pixbuf (cr, phd->pixbuf, 0, 0);
188 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
190 PaneHistogramData *phd = data;
192 phd->histogram_width = allocation->width;
193 phd->histogram_height = allocation->height;
194 bar_pane_histogram_update(phd);
198 static void bar_pane_histogram_close(GtkWidget *pane)
200 PaneHistogramData *phd;
202 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
205 gtk_widget_destroy(phd->widget);
209 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
211 PaneHistogramData *phd = data;
213 if (phd->idle_id) g_source_remove(phd->idle_id);
214 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
216 file_data_unref(phd->fd);
217 histogram_free(phd->histogram);
218 if (phd->pixbuf) g_object_unref(phd->pixbuf);
219 g_free(phd->pane.id);
224 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
226 PaneHistogramData *phd = data;
229 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
233 channel = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
234 if (channel == histogram_get_channel(phd->histogram)) return;
236 histogram_set_channel(phd->histogram, channel);
237 bar_pane_histogram_update(phd);
240 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
242 PaneHistogramData *phd = data;
245 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
249 logmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
250 if (logmode == histogram_get_mode(phd->histogram)) return;
252 histogram_set_mode(phd->histogram, logmode);
253 bar_pane_histogram_update(phd);
256 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
259 gint channel = histogram_get_channel(phd->histogram);
260 gint mode = histogram_get_mode(phd->histogram);
262 menu = popup_menu_short_lived();
264 /* use the same strings as in layout_util.c */
265 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);
266 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);
267 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);
268 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);
269 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);
271 menu_item_add_divider(menu);
273 menu_item_add_radio(menu, _("Li_near Histogram"), GINT_TO_POINTER(0), (mode == 0), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
274 menu_item_add_radio(menu, _("L_og Histogram"), GINT_TO_POINTER(1), (mode == 1), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
279 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
281 PaneHistogramData *phd = data;
283 if (bevent->button == MOUSE_BUTTON_RIGHT)
287 menu = bar_pane_histogram_menu(phd);
288 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
296 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
298 PaneHistogramData *phd;
300 phd = g_new0(PaneHistogramData, 1);
302 phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
303 phd->pane.pane_write_config = bar_pane_histogram_write_config;
304 phd->pane.title = bar_pane_expander_title(title);
305 phd->pane.id = g_strdup(id);
306 phd->pane.type = PANE_HISTOGRAM;
308 phd->pane.expanded = expanded;
310 phd->histogram = histogram_new();
312 histogram_set_channel(phd->histogram, histogram_channel);
313 histogram_set_mode(phd->histogram, histogram_mode);
315 phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
317 g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
318 g_signal_connect(G_OBJECT(phd->widget), "destroy",
319 G_CALLBACK(bar_pane_histogram_destroy), phd);
322 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
324 phd->drawing_area = gtk_drawing_area_new();
325 g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
326 G_CALLBACK(bar_pane_histogram_size_cb), phd);
328 #if GTK_CHECK_VERSION(3,0,0)
329 g_signal_connect(G_OBJECT(phd->drawing_area), "draw",
330 G_CALLBACK(bar_pane_histogram_draw_cb), phd);
332 g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",
333 G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
336 gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
337 gtk_widget_show(phd->drawing_area);
338 gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
340 g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
342 gtk_widget_show(phd->widget);
344 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
349 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
352 gchar *id = g_strdup("histogram");
353 gboolean expanded = TRUE;
355 gint histogram_channel = HCHAN_RGB;
356 gint histogram_mode = 0;
359 while (*attribute_names)
361 const gchar *option = *attribute_names++;
362 const gchar *value = *attribute_values++;
364 if (READ_CHAR_FULL("id", id)) continue;
365 if (READ_CHAR_FULL("title", title)) continue;
366 if (READ_BOOL_FULL("expanded", expanded)) continue;
367 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
368 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
370 log_printf("unknown attribute %s = %s\n", option, value);
373 bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
374 ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
380 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
382 PaneHistogramData *phd;
384 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
387 gint histogram_channel = phd->histogram->histogram_channel;
388 gint histogram_mode = phd->histogram->histogram_mode;
390 while (*attribute_names)
392 const gchar *option = *attribute_names++;
393 const gchar *value = *attribute_values++;
395 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
396 // if (READ_CHAR_FULL("pane.title", title)) continue;
397 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
398 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
399 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
402 log_printf("unknown attribute %s = %s\n", option, value);
405 histogram_set_channel(phd->histogram, histogram_channel);
406 histogram_set_mode(phd->histogram, histogram_mode);
408 bar_update_expander(pane);
409 bar_pane_histogram_update(phd);
413 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */