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_comment.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;
48 static void bar_pane_histogram_update(PaneHistogramData *phd)
50 const HistMap *histmap;
51 if (phd->pixbuf) g_object_unref(phd->pixbuf);
54 if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
56 gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
58 histmap = histmap_get(phd->fd);
62 phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
63 gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
64 histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
68 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
70 PaneHistogramData *phd;
72 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
75 file_data_unref(phd->fd);
76 phd->fd = file_data_ref(fd);
78 bar_pane_histogram_update(phd);
81 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
83 PaneHistogramData *phd;
85 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
88 WRITE_STRING("<pane_histogram\n");
90 WRITE_CHAR(*phd, pane.title);
91 WRITE_BOOL(*phd, pane.expanded);
97 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
99 PaneHistogramData *phd = data;
100 if (fd == phd->fd) bar_pane_histogram_update(phd);
103 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
105 PaneHistogramData *phd = data;
106 if (!phd || !phd->pixbuf) return TRUE;
108 gdk_draw_pixbuf(widget->window,
109 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
114 GDK_RGB_DITHER_NORMAL, 0, 0);
118 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
120 PaneHistogramData *phd = data;
122 phd->histogram_width = allocation->width;
123 phd->histogram_height = allocation->height;
124 bar_pane_histogram_update(phd);
127 static void bar_pane_histogram_close(GtkWidget *pane)
129 PaneHistogramData *phd;
131 phd = g_object_get_data(G_OBJECT(pane), "pane_data");
134 gtk_widget_destroy(phd->widget);
137 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
139 PaneHistogramData *phd = data;
141 file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
143 file_data_unref(phd->fd);
144 g_free(phd->pane.title);
145 histogram_free(phd->histogram);
146 if (phd->pixbuf) g_object_unref(phd->pixbuf);
151 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
153 PaneHistogramData *phd;
156 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
158 phd = submenu_item_get_data(widget);
162 channel = GPOINTER_TO_INT(data);
163 if (channel == histogram_get_channel(phd->histogram)) return;
165 histogram_set_channel(phd->histogram, channel);
166 bar_pane_histogram_update(phd);
169 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
171 PaneHistogramData *phd;
174 if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
176 phd = submenu_item_get_data(widget);
180 logmode = GPOINTER_TO_INT(data);
181 if (logmode == histogram_get_mode(phd->histogram)) return;
183 histogram_set_mode(phd->histogram, logmode);
184 bar_pane_histogram_update(phd);
187 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
189 GCallback func, gint value,
190 gboolean show_current, gint current_value)
196 item = menu_item_add_radio(menu, parent,
197 label, (value == current_value),
198 func, GINT_TO_POINTER((gint)value));
202 item = menu_item_add(menu, label,
203 func, GINT_TO_POINTER((gint)value));
209 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
210 gboolean show_current, gint current_value)
215 submenu = gtk_menu_new();
216 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
218 parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
219 bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
220 bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
221 bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
222 bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
228 item = menu_item_add(menu, _("Channels"), NULL, NULL);
229 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
235 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
236 gboolean show_current, gint current_value)
241 submenu = gtk_menu_new();
242 g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
244 parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
245 bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
251 item = menu_item_add(menu, _("Mode"), NULL, NULL);
252 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
260 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
263 static gboolean show_current = TRUE;
265 menu = popup_menu_short_lived();
266 bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
267 show_current, histogram_get_channel(phd->histogram));
268 bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
269 show_current, histogram_get_mode(phd->histogram));
274 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
276 PaneHistogramData *phd = data;
278 if (bevent->button == MOUSE_BUTTON_RIGHT)
282 menu = bar_pane_histogram_menu(phd);
283 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
291 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gint expanded)
293 PaneHistogramData *phd;
295 phd = g_new0(PaneHistogramData, 1);
297 phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
298 phd->pane.pane_write_config = bar_pane_histogram_write_config;
299 phd->pane.title = g_strdup(title);
300 phd->pane.expanded = expanded;
302 phd->histogram = histogram_new();
304 histogram_set_channel(phd->histogram, HCHAN_RGB);
305 histogram_set_mode(phd->histogram, 0);
307 phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
309 g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
310 g_signal_connect(G_OBJECT(phd->widget), "destroy",
311 G_CALLBACK(bar_pane_histogram_destroy), phd);
314 gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
316 phd->drawing_area = gtk_drawing_area_new();
317 g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
318 G_CALLBACK(bar_pane_histogram_size_cb), phd);
320 g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",
321 G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
323 gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
324 gtk_widget_show(phd->drawing_area);
325 gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
327 g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
329 gtk_widget_show(phd->widget);
331 file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
336 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
338 gchar *title = g_strdup(_("NoName"));
339 gboolean expanded = TRUE;
342 while (*attribute_names)
344 const gchar *option = *attribute_names++;
345 const gchar *value = *attribute_values++;
347 if (READ_CHAR_FULL("pane.title", title)) continue;
348 if (READ_BOOL_FULL("pane.expanded", expanded)) continue;
351 DEBUG_1("unknown attribute %s = %s", option, value);
354 return bar_pane_histogram_new(title, height, expanded);
358 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */