4 * Copyright (C) 2008 - 2009 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;
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_WIDGET_DRAWABLE(phd->drawing_area))
66 if (phd->idle_id == -1) phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
70 phd->need_update = TRUE;
74 static gboolean bar_pane_histogram_update_cb(gpointer data)
76 const HistMap *histmap;
77 PaneHistogramData *phd = data;
80 phd->need_update = FALSE;
82 gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
84 if (phd->fd == NULL) return FALSE;
85 histmap = histmap_get(phd->fd);
89 histmap_start_idle(phd->fd);
93 phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
94 gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
95 histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
101 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
103 PaneHistogramData *phd;
105 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
108 file_data_unref(phd->fd);
109 phd->fd = file_data_ref(fd);
111 bar_pane_histogram_update(phd);
114 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
116 PaneHistogramData *phd;
118 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
121 WRITE_NL(); WRITE_STRING("<pane_histogram ");
122 write_char_option(outstr, indent, "id", phd->pane.id);
123 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(phd->pane.title)));
124 WRITE_BOOL(phd->pane, expanded);
125 WRITE_INT(*phd->histogram, histogram_channel);
126 WRITE_INT(*phd->histogram, histogram_mode);
130 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
132 PaneHistogramData *phd = data;
133 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd)
135 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
136 bar_pane_histogram_update(phd);
140 static gboolean bar_pane_histogram_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
142 PaneHistogramData *phd = data;
143 if (!phd) return TRUE;
145 if (phd->need_update)
147 bar_pane_histogram_update(phd);
150 if (!phd->pixbuf) return TRUE;
152 gdk_draw_pixbuf(widget->window,
153 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
158 GDK_RGB_DITHER_NORMAL, 0, 0);
162 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
164 PaneHistogramData *phd = data;
166 phd->histogram_width = allocation->width;
167 phd->histogram_height = allocation->height;
168 bar_pane_histogram_update(phd);
171 static void bar_pane_histogram_close(GtkWidget *pane)
173 PaneHistogramData *phd;
175 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
178 gtk_widget_destroy(phd->widget);
181 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
183 PaneHistogramData *phd = data;
185 g_source_remove(phd->idle_id);
186 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
188 file_data_unref(phd->fd);
189 histogram_free(phd->histogram);
190 if (phd->pixbuf) g_object_unref(phd->pixbuf);
191 g_free(phd->pane.id);
196 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
198 PaneHistogramData *phd;
201 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
203 phd = submenu_item_get_data(widget);
207 channel = GPOINTER_TO_INT(data);
208 if (channel == histogram_get_channel(phd->histogram)) return;
210 histogram_set_channel(phd->histogram, channel);
211 bar_pane_histogram_update(phd);
214 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
216 PaneHistogramData *phd;
219 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
221 phd = submenu_item_get_data(widget);
225 logmode = GPOINTER_TO_INT(data);
226 if (logmode == histogram_get_mode(phd->histogram)) return;
228 histogram_set_mode(phd->histogram, logmode);
229 bar_pane_histogram_update(phd);
232 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
234 GCallback func, gint value,
235 gboolean show_current, gint current_value)
241 item = menu_item_add_radio(menu, parent,
242 label, (value == current_value),
243 func, GINT_TO_POINTER((gint)value));
247 item = menu_item_add(menu, label,
248 func, GINT_TO_POINTER((gint)value));
254 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
255 gboolean show_current, gint current_value)
260 submenu = gtk_menu_new();
261 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
263 parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
264 bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
265 bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
266 bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
267 bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
273 item = menu_item_add(menu, _("Channels"), NULL, NULL);
274 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
280 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
281 gboolean show_current, gint current_value)
286 submenu = gtk_menu_new();
287 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
289 parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
290 bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
296 item = menu_item_add(menu, _("Mode"), NULL, NULL);
297 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
305 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
308 static gboolean show_current = TRUE;
310 menu = popup_menu_short_lived();
311 bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
312 show_current, histogram_get_channel(phd->histogram));
313 bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
314 show_current, histogram_get_mode(phd->histogram));
319 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
321 PaneHistogramData *phd = data;
323 if (bevent->button == MOUSE_BUTTON_RIGHT)
327 menu = bar_pane_histogram_menu(phd);
328 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
336 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
338 PaneHistogramData *phd;
340 phd = g_new0(PaneHistogramData, 1);
342 phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
343 phd->pane.pane_write_config = bar_pane_histogram_write_config;
344 phd->pane.title = bar_pane_expander_title(title);
345 phd->pane.id = g_strdup(id);
346 phd->pane.type = PANE_HISTOGRAM;
348 phd->pane.expanded = expanded;
351 phd->histogram = histogram_new();
353 histogram_set_channel(phd->histogram, histogram_channel);
354 histogram_set_mode(phd->histogram, histogram_mode);
356 phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
358 g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
359 g_signal_connect(G_OBJECT(phd->widget), "destroy",
360 G_CALLBACK(bar_pane_histogram_destroy), phd);
363 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
365 phd->drawing_area = gtk_drawing_area_new();
366 g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
367 G_CALLBACK(bar_pane_histogram_size_cb), phd);
369 g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",
370 G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
372 gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
373 gtk_widget_show(phd->drawing_area);
374 gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
376 g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
378 gtk_widget_show(phd->widget);
380 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
385 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
388 gchar *id = g_strdup("histogram");
389 gboolean expanded = TRUE;
391 gint histogram_channel = HCHAN_RGB;
392 gint histogram_mode = 0;
395 while (*attribute_names)
397 const gchar *option = *attribute_names++;
398 const gchar *value = *attribute_values++;
400 if (READ_CHAR_FULL("id", id)) continue;
401 if (READ_CHAR_FULL("title", title)) continue;
402 if (READ_BOOL_FULL("expanded", expanded)) continue;
403 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
404 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
406 log_printf("unknown attribute %s = %s\n", option, value);
409 bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
410 ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
416 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
418 PaneHistogramData *phd;
420 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
423 gint histogram_channel = phd->histogram->histogram_channel;
424 gint histogram_mode = phd->histogram->histogram_mode;
426 while (*attribute_names)
428 const gchar *option = *attribute_names++;
429 const gchar *value = *attribute_values++;
431 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
432 // if (READ_CHAR_FULL("pane.title", title)) continue;
433 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
434 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
435 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
438 log_printf("unknown attribute %s = %s\n", option, value);
441 histogram_set_channel(phd->histogram, histogram_channel);
442 histogram_set_mode(phd->histogram, histogram_mode);
444 bar_update_expander(pane);
445 bar_pane_histogram_update(phd);
449 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */