fixed bar_histogram headers
[geeqie.git] / src / bar_histogram.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: Vladimir Nadvornik
7  *
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!
11  */
12
13
14 #include "main.h"
15 #include "bar_histogram.h"
16
17 #include "bar.h"
18 #include "metadata.h"
19 #include "filedata.h"
20 #include "menu.h"
21 #include "ui_menu.h"
22 #include "ui_misc.h"
23 #include "histogram.h"
24 #include "rcfile.h"
25
26 /*
27  *-------------------------------------------------------------------
28  * keyword / comment utils
29  *-------------------------------------------------------------------
30  */
31
32
33
34 typedef struct _PaneHistogramData PaneHistogramData;
35 struct _PaneHistogramData
36 {
37         PaneData pane;
38         GtkWidget *widget;
39         GtkWidget *drawing_area;
40         Histogram *histogram;
41         gint histogram_width;
42         gint histogram_height;
43         GdkPixbuf *pixbuf;
44         FileData *fd;
45         gboolean need_update;
46         gint idle_id;
47 };
48
49 static gboolean bar_pane_histogram_update_cb(gpointer data);
50
51
52 static void bar_pane_histogram_update(PaneHistogramData *phd)
53 {
54         if (phd->pixbuf) g_object_unref(phd->pixbuf);
55         phd->pixbuf = NULL;
56
57         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
58
59         /* histmap_get is relatively expensive, run it only when we really need it
60            and with lower priority than pixbuf_renderer 
61            FIXME: this does not work for fullscreen*/
62         if (GTK_WIDGET_DRAWABLE(phd->drawing_area))
63                 {
64                 if (phd->idle_id == -1) phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
65                 }
66         else
67                 {
68                 phd->need_update = TRUE;
69                 }
70 }
71
72 static gboolean bar_pane_histogram_update_cb(gpointer data)
73 {
74         const HistMap *histmap;
75         PaneHistogramData *phd = data;
76
77         phd->idle_id = -1;
78         phd->need_update = FALSE;
79         
80         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
81         
82         histmap = histmap_get(phd->fd);
83         
84         if (!histmap) return FALSE;
85         
86         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
87         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
88         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
89
90 #if GTK_CHECK_VERSION(2,12,0)
91         gtk_widget_set_tooltip_text(phd->drawing_area, histogram_label(phd->histogram));
92 #endif
93         return FALSE;
94 }
95
96
97 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
98 {
99         PaneHistogramData *phd;
100
101         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
102         if (!phd) return;
103
104         file_data_unref(phd->fd);
105         phd->fd = file_data_ref(fd);
106
107         bar_pane_histogram_update(phd);
108 }
109
110 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
111 {
112         PaneHistogramData *phd;
113
114         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
115         if (!phd) return;
116
117         WRITE_STRING("<pane_histogram\n");
118         indent++;
119         WRITE_CHAR(*phd, pane.title);
120         WRITE_BOOL(*phd, pane.expanded);
121         WRITE_INT(*phd->histogram, histogram_channel);
122         WRITE_INT(*phd->histogram, histogram_mode);
123         indent--;
124         WRITE_STRING("/>\n");
125 }
126
127
128 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
129 {
130         PaneHistogramData *phd = data;
131         if (fd == phd->fd) bar_pane_histogram_update(phd);
132 }
133
134 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
135 {
136         PaneHistogramData *phd = data;
137         if (!phd) return TRUE;
138         
139         if (phd->need_update)
140                 {
141                 bar_pane_histogram_update(phd);
142                 }
143         
144         if (!phd->pixbuf) return TRUE;
145         
146         gdk_draw_pixbuf(widget->window,
147                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
148                         phd->pixbuf,
149                         0, 0,
150                         0, 0,
151                         -1, -1,
152                         GDK_RGB_DITHER_NORMAL, 0, 0);
153         return TRUE;
154 }
155
156 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
157 {
158         PaneHistogramData *phd = data;
159
160         phd->histogram_width = allocation->width;
161         phd->histogram_height = allocation->height;
162         bar_pane_histogram_update(phd);
163 }
164
165 static void bar_pane_histogram_close(GtkWidget *pane)
166 {
167         PaneHistogramData *phd;
168
169         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
170         if (!phd) return;
171
172         gtk_widget_destroy(phd->widget);
173 }
174
175 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
176 {
177         PaneHistogramData *phd = data;
178         
179         g_source_remove(phd->idle_id);
180         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
181
182         file_data_unref(phd->fd);
183         g_free(phd->pane.title);
184         histogram_free(phd->histogram);
185         if (phd->pixbuf) g_object_unref(phd->pixbuf);
186
187         g_free(phd);
188 }
189
190 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
191 {
192         PaneHistogramData *phd;
193         gint channel;
194
195         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
196
197         phd = submenu_item_get_data(widget);
198
199         if (!phd) return;
200
201         channel = GPOINTER_TO_INT(data);
202         if (channel == histogram_get_channel(phd->histogram)) return;
203
204         histogram_set_channel(phd->histogram, channel);
205         bar_pane_histogram_update(phd);
206 }
207
208 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
209 {
210         PaneHistogramData *phd;
211         gint logmode;
212         
213         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
214
215         phd = submenu_item_get_data(widget);
216
217         if (!phd) return;
218
219         logmode = GPOINTER_TO_INT(data);
220         if (logmode == histogram_get_mode(phd->histogram)) return;
221
222         histogram_set_mode(phd->histogram, logmode);
223         bar_pane_histogram_update(phd);
224 }
225
226 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
227                                         const gchar *label,
228                                         GCallback func, gint value,
229                                         gboolean show_current, gint current_value)
230 {
231         GtkWidget *item;
232
233         if (show_current)
234                 {
235                 item = menu_item_add_radio(menu, parent,
236                                            label, (value == current_value),
237                                            func, GINT_TO_POINTER((gint)value));
238                 }
239         else
240                 {
241                 item = menu_item_add(menu, label,
242                                      func, GINT_TO_POINTER((gint)value));
243                 }
244
245         return item;
246 }
247
248 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
249                                            gboolean show_current, gint current_value)
250 {
251         GtkWidget *submenu;
252         GtkWidget *parent;
253
254         submenu = gtk_menu_new();
255         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
256
257         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
258         bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
259         bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
260         bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
261         bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
262
263         if (menu)
264                 {
265                 GtkWidget *item;
266
267                 item = menu_item_add(menu, _("Channels"), NULL, NULL);
268                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
269                 return item;
270                 }
271
272         return submenu;
273 }
274 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
275                                            gboolean show_current, gint current_value)
276 {
277         GtkWidget *submenu;
278         GtkWidget *parent;
279
280         submenu = gtk_menu_new();
281         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
282
283         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
284         bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
285
286         if (menu)
287                 {
288                 GtkWidget *item;
289
290                 item = menu_item_add(menu, _("Mode"), NULL, NULL);
291                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
292                 return item;
293                 }
294
295         return submenu;
296 }
297
298
299 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
300 {
301         GtkWidget *menu;
302         static gboolean show_current = TRUE;
303
304         menu = popup_menu_short_lived();
305         bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
306                                         show_current, histogram_get_channel(phd->histogram));
307         bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
308                                        show_current, histogram_get_mode(phd->histogram));
309
310         return menu;
311 }
312
313 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
314 {
315         PaneHistogramData *phd = data;
316
317         if (bevent->button == MOUSE_BUTTON_RIGHT)
318                 {
319                 GtkWidget *menu;
320
321                 menu = bar_pane_histogram_menu(phd);
322                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
323                 return TRUE;
324         }
325
326         return FALSE;
327 }
328
329
330 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
331 {
332         PaneHistogramData *phd;
333
334         phd = g_new0(PaneHistogramData, 1);
335         
336         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
337         phd->pane.pane_write_config = bar_pane_histogram_write_config;
338         phd->pane.title = g_strdup(title);
339         phd->pane.expanded = expanded;
340         phd->idle_id = -1;
341         
342         phd->histogram = histogram_new();
343
344         histogram_set_channel(phd->histogram, histogram_channel);
345         histogram_set_mode(phd->histogram, histogram_mode);
346
347         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
348
349         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
350         g_signal_connect(G_OBJECT(phd->widget), "destroy",
351                          G_CALLBACK(bar_pane_histogram_destroy), phd);
352         
353
354         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
355
356         phd->drawing_area = gtk_drawing_area_new();
357         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
358                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
359
360         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
361                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
362                          
363         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
364         gtk_widget_show(phd->drawing_area);
365         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
366
367         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
368
369 #if GTK_CHECK_VERSION(2,12,0)
370         gtk_widget_set_tooltip_text(phd->drawing_area, histogram_label(phd->histogram));
371 #endif
372         gtk_widget_show(phd->widget);
373
374         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
375
376         return phd->widget;
377 }
378
379 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
380 {
381         gchar *title = g_strdup(_("NoName"));
382         gboolean expanded = TRUE;
383         gint height = 80;
384         gint histogram_channel = HCHAN_RGB;
385         gint histogram_mode = 0;
386
387         while (*attribute_names)
388                 {
389                 const gchar *option = *attribute_names++;
390                 const gchar *value = *attribute_values++;
391
392                 if (READ_CHAR_FULL("pane.title", title)) continue;
393                 if (READ_BOOL_FULL("pane.expanded", expanded)) continue;
394                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
395                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
396
397                 
398                 DEBUG_1("unknown attribute %s = %s", option, value);
399                 }
400         
401         return bar_pane_histogram_new(title, height, expanded, histogram_channel, histogram_mode);
402 }
403
404
405 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */