Fix up event source ids type: gint -> guint.
[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         guint idle_id; /* event source 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         gtk_label_set_text(GTK_LABEL(phd->pane.title), histogram_label(phd->histogram));
58
59         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
60
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))
65                 {
66                 if (!phd->idle_id)
67                         {
68                         phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
69                         }
70                 }
71         else
72                 {
73                 phd->need_update = TRUE;
74                 }
75 }
76
77 static gboolean bar_pane_histogram_update_cb(gpointer data)
78 {
79         const HistMap *histmap;
80         PaneHistogramData *phd = data;
81
82         phd->idle_id = 0;
83         phd->need_update = FALSE;
84         
85         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
86         
87         if (phd->fd == NULL) return FALSE;
88         histmap = histmap_get(phd->fd);
89         
90         if (!histmap) 
91                 {
92                 histmap_start_idle(phd->fd);
93                 return FALSE;
94                 }
95         
96         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
97         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
98         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
99
100         return FALSE;
101 }
102
103
104 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
105 {
106         PaneHistogramData *phd;
107
108         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
109         if (!phd) return;
110
111         file_data_unref(phd->fd);
112         phd->fd = file_data_ref(fd);
113
114         bar_pane_histogram_update(phd);
115 }
116
117 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
118 {
119         PaneHistogramData *phd;
120
121         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
122         if (!phd) return;
123
124         WRITE_NL(); WRITE_STRING("<pane_histogram ");
125         write_char_option(outstr, indent, "id", phd->pane.id);
126         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(phd->pane.title)));
127         WRITE_BOOL(phd->pane, expanded);
128         WRITE_INT(*phd->histogram, histogram_channel);
129         WRITE_INT(*phd->histogram, histogram_mode);
130         WRITE_STRING("/>");
131 }
132
133 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
134 {
135         PaneHistogramData *phd = data;
136         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd) 
137                 {
138                 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
139                 bar_pane_histogram_update(phd);
140                 }
141 }
142
143 static gboolean bar_pane_histogram_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
144 {
145         PaneHistogramData *phd = data;
146         if (!phd) return TRUE;
147         
148         if (phd->need_update)
149                 {
150                 bar_pane_histogram_update(phd);
151                 }
152         
153         if (!phd->pixbuf) return TRUE;
154         
155         gdk_draw_pixbuf(widget->window,
156                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
157                         phd->pixbuf,
158                         0, 0,
159                         0, 0,
160                         -1, -1,
161                         GDK_RGB_DITHER_NORMAL, 0, 0);
162         return TRUE;
163 }
164
165 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
166 {
167         PaneHistogramData *phd = data;
168
169         phd->histogram_width = allocation->width;
170         phd->histogram_height = allocation->height;
171         bar_pane_histogram_update(phd);
172 }
173
174 static void bar_pane_histogram_close(GtkWidget *pane)
175 {
176         PaneHistogramData *phd;
177
178         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
179         if (!phd) return;
180
181         gtk_widget_destroy(phd->widget);
182 }
183
184 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
185 {
186         PaneHistogramData *phd = data;
187         
188         if (phd->idle_id) g_source_remove(phd->idle_id);
189         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
190
191         file_data_unref(phd->fd);
192         histogram_free(phd->histogram);
193         if (phd->pixbuf) g_object_unref(phd->pixbuf);
194         g_free(phd->pane.id);
195
196         g_free(phd);
197 }
198
199 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
200 {
201         PaneHistogramData *phd;
202         gint channel;
203
204         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
205
206         phd = submenu_item_get_data(widget);
207
208         if (!phd) return;
209
210         channel = GPOINTER_TO_INT(data);
211         if (channel == histogram_get_channel(phd->histogram)) return;
212
213         histogram_set_channel(phd->histogram, channel);
214         bar_pane_histogram_update(phd);
215 }
216
217 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
218 {
219         PaneHistogramData *phd;
220         gint logmode;
221         
222         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
223
224         phd = submenu_item_get_data(widget);
225
226         if (!phd) return;
227
228         logmode = GPOINTER_TO_INT(data);
229         if (logmode == histogram_get_mode(phd->histogram)) return;
230
231         histogram_set_mode(phd->histogram, logmode);
232         bar_pane_histogram_update(phd);
233 }
234
235 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
236                                         const gchar *label,
237                                         GCallback func, gint value,
238                                         gboolean show_current, gint current_value)
239 {
240         GtkWidget *item;
241
242         if (show_current)
243                 {
244                 item = menu_item_add_radio(menu, parent,
245                                            label, (value == current_value),
246                                            func, GINT_TO_POINTER((gint)value));
247                 }
248         else
249                 {
250                 item = menu_item_add(menu, label,
251                                      func, GINT_TO_POINTER((gint)value));
252                 }
253
254         return item;
255 }
256
257 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
258                                            gboolean show_current, gint current_value)
259 {
260         GtkWidget *submenu;
261         GtkWidget *parent;
262
263         submenu = gtk_menu_new();
264         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
265
266         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
267         bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
268         bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
269         bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
270         bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
271
272         if (menu)
273                 {
274                 GtkWidget *item;
275
276                 item = menu_item_add(menu, _("Channels"), NULL, NULL);
277                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
278                 return item;
279                 }
280
281         return submenu;
282 }
283 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
284                                            gboolean show_current, gint current_value)
285 {
286         GtkWidget *submenu;
287         GtkWidget *parent;
288
289         submenu = gtk_menu_new();
290         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
291
292         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
293         bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
294
295         if (menu)
296                 {
297                 GtkWidget *item;
298
299                 item = menu_item_add(menu, _("Mode"), NULL, NULL);
300                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
301                 return item;
302                 }
303
304         return submenu;
305 }
306
307
308 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
309 {
310         GtkWidget *menu;
311         static gboolean show_current = TRUE;
312
313         menu = popup_menu_short_lived();
314         bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
315                                         show_current, histogram_get_channel(phd->histogram));
316         bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
317                                        show_current, histogram_get_mode(phd->histogram));
318
319         return menu;
320 }
321
322 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
323 {
324         PaneHistogramData *phd = data;
325
326         if (bevent->button == MOUSE_BUTTON_RIGHT)
327                 {
328                 GtkWidget *menu;
329
330                 menu = bar_pane_histogram_menu(phd);
331                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
332                 return TRUE;
333         }
334
335         return FALSE;
336 }
337
338
339 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
340 {
341         PaneHistogramData *phd;
342
343         phd = g_new0(PaneHistogramData, 1);
344         
345         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
346         phd->pane.pane_write_config = bar_pane_histogram_write_config;
347         phd->pane.title = bar_pane_expander_title(title);
348         phd->pane.id = g_strdup(id);
349         phd->pane.type = PANE_HISTOGRAM;
350
351         phd->pane.expanded = expanded;
352         
353         phd->histogram = histogram_new();
354
355         histogram_set_channel(phd->histogram, histogram_channel);
356         histogram_set_mode(phd->histogram, histogram_mode);
357
358         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
359
360         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
361         g_signal_connect(G_OBJECT(phd->widget), "destroy",
362                          G_CALLBACK(bar_pane_histogram_destroy), phd);
363         
364
365         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
366
367         phd->drawing_area = gtk_drawing_area_new();
368         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
369                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
370
371         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
372                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
373                          
374         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
375         gtk_widget_show(phd->drawing_area);
376         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
377
378         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
379
380         gtk_widget_show(phd->widget);
381
382         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
383
384         return phd->widget;
385 }
386
387 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
388 {
389         gchar *title = NULL;
390         gchar *id = g_strdup("histogram");
391         gboolean expanded = TRUE;
392         gint height = 80;
393         gint histogram_channel = HCHAN_RGB;
394         gint histogram_mode = 0;
395         GtkWidget *ret;
396
397         while (*attribute_names)
398                 {
399                 const gchar *option = *attribute_names++;
400                 const gchar *value = *attribute_values++;
401
402                 if (READ_CHAR_FULL("id", id)) continue;
403                 if (READ_CHAR_FULL("title", title)) continue;
404                 if (READ_BOOL_FULL("expanded", expanded)) continue;
405                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
406                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
407
408                 log_printf("unknown attribute %s = %s\n", option, value);
409                 }
410         
411         bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
412         ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
413         g_free(title);
414         g_free(id);
415         return ret;
416 }
417
418 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
419 {
420         PaneHistogramData *phd;
421
422         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
423         if (!phd) return;
424
425         gint histogram_channel = phd->histogram->histogram_channel;
426         gint histogram_mode = phd->histogram->histogram_mode;
427
428         while (*attribute_names)
429                 {
430                 const gchar *option = *attribute_names++;
431                 const gchar *value = *attribute_values++;
432
433                 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
434 //              if (READ_CHAR_FULL("pane.title", title)) continue;
435                 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
436                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
437                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
438                 
439
440                 log_printf("unknown attribute %s = %s\n", option, value);
441                 }
442         
443         histogram_set_channel(phd->histogram, histogram_channel);
444         histogram_set_mode(phd->histogram, histogram_mode);
445
446         bar_update_expander(pane);
447         bar_pane_histogram_update(phd);
448 }
449
450
451 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */