use accessor functions
[geeqie.git] / src / bar_histogram.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2012 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_CHECK_VERSION(2,20,0)
65         if (gtk_widget_is_drawable(phd->drawing_area))
66 #else
67         if (GTK_WIDGET_DRAWABLE(phd->drawing_area))
68 #endif
69                 {
70                 if (!phd->idle_id)
71                         {
72                         phd->idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, bar_pane_histogram_update_cb, phd, NULL);
73                         }
74                 }
75         else
76                 {
77                 phd->need_update = TRUE;
78                 }
79 }
80
81 static gboolean bar_pane_histogram_update_cb(gpointer data)
82 {
83         const HistMap *histmap;
84         PaneHistogramData *phd = data;
85
86         phd->idle_id = 0;
87         phd->need_update = FALSE;
88         
89         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
90         
91         if (phd->fd == NULL) return FALSE;
92         histmap = histmap_get(phd->fd);
93         
94         if (!histmap) 
95                 {
96                 histmap_start_idle(phd->fd);
97                 return FALSE;
98                 }
99         
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);
103
104         return FALSE;
105 }
106
107
108 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
109 {
110         PaneHistogramData *phd;
111
112         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
113         if (!phd) return;
114
115         file_data_unref(phd->fd);
116         phd->fd = file_data_ref(fd);
117
118         bar_pane_histogram_update(phd);
119 }
120
121 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
122 {
123         PaneHistogramData *phd;
124
125         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
126         if (!phd) return;
127
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);
134         WRITE_STRING("/>");
135 }
136
137 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
138 {
139         PaneHistogramData *phd = data;
140         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == phd->fd) 
141                 {
142                 DEBUG_1("Notify pane_histogram: %s %04x", fd->path, type);
143                 bar_pane_histogram_update(phd);
144                 }
145 }
146
147 static gboolean bar_pane_histogram_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
148 {
149         PaneHistogramData *phd = data;
150         if (!phd) return TRUE;
151         
152         if (phd->need_update)
153                 {
154                 bar_pane_histogram_update(phd);
155                 }
156         
157         if (!phd->pixbuf) return TRUE;
158         
159         cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
160         gdk_cairo_set_source_pixbuf (cr, phd->pixbuf, 0, 0);
161         cairo_paint (cr);
162         cairo_destroy (cr);
163
164         return TRUE;
165 }
166
167 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
168 {
169         PaneHistogramData *phd = data;
170
171         phd->histogram_width = allocation->width;
172         phd->histogram_height = allocation->height;
173         bar_pane_histogram_update(phd);
174 }
175
176 #if 0
177 static void bar_pane_histogram_close(GtkWidget *pane)
178 {
179         PaneHistogramData *phd;
180
181         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
182         if (!phd) return;
183
184         gtk_widget_destroy(phd->widget);
185 }
186 #endif
187
188 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
189 {
190         PaneHistogramData *phd = data;
191         
192         if (phd->idle_id) g_source_remove(phd->idle_id);
193         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
194
195         file_data_unref(phd->fd);
196         histogram_free(phd->histogram);
197         if (phd->pixbuf) g_object_unref(phd->pixbuf);
198         g_free(phd->pane.id);
199
200         g_free(phd);
201 }
202
203 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
204 {
205         PaneHistogramData *phd = data;
206         gint channel;
207
208         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
209
210         if (!phd) return;
211
212         channel = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
213         if (channel == histogram_get_channel(phd->histogram)) return;
214
215         histogram_set_channel(phd->histogram, channel);
216         bar_pane_histogram_update(phd);
217 }
218
219 static void bar_pane_histogram_popup_mode_cb(GtkWidget *widget, gpointer data)
220 {
221         PaneHistogramData *phd = data;
222         gint logmode;
223         
224         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
225
226         if (!phd) return;
227
228         logmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_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_menu(PaneHistogramData *phd)
236 {
237         GtkWidget *menu;
238         gint channel = histogram_get_channel(phd->histogram);
239         gint mode = histogram_get_mode(phd->histogram);
240
241         menu = popup_menu_short_lived();
242
243         /* use the same strings as in layout_util.c */
244         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);
245         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);
246         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);
247         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);
248         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);
249         
250         menu_item_add_divider(menu);
251         
252         menu_item_add_radio(menu, _("Li_near Histogram"), GINT_TO_POINTER(0), (mode == 0), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
253         menu_item_add_radio(menu, _("L_og Histogram"),    GINT_TO_POINTER(1), (mode == 1), G_CALLBACK(bar_pane_histogram_popup_mode_cb), phd);
254
255         return menu;
256 }
257
258 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
259 {
260         PaneHistogramData *phd = data;
261
262         if (bevent->button == MOUSE_BUTTON_RIGHT)
263                 {
264                 GtkWidget *menu;
265
266                 menu = bar_pane_histogram_menu(phd);
267                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
268                 return TRUE;
269         }
270
271         return FALSE;
272 }
273
274
275 static GtkWidget *bar_pane_histogram_new(const gchar *id, const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
276 {
277         PaneHistogramData *phd;
278
279         phd = g_new0(PaneHistogramData, 1);
280         
281         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
282         phd->pane.pane_write_config = bar_pane_histogram_write_config;
283         phd->pane.title = bar_pane_expander_title(title);
284         phd->pane.id = g_strdup(id);
285         phd->pane.type = PANE_HISTOGRAM;
286
287         phd->pane.expanded = expanded;
288         
289         phd->histogram = histogram_new();
290
291         histogram_set_channel(phd->histogram, histogram_channel);
292         histogram_set_mode(phd->histogram, histogram_mode);
293
294         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
295
296         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
297         g_signal_connect(G_OBJECT(phd->widget), "destroy",
298                          G_CALLBACK(bar_pane_histogram_destroy), phd);
299         
300
301         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
302
303         phd->drawing_area = gtk_drawing_area_new();
304         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
305                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
306
307         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
308                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
309                          
310         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
311         gtk_widget_show(phd->drawing_area);
312         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
313
314         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
315
316         gtk_widget_show(phd->widget);
317
318         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
319
320         return phd->widget;
321 }
322
323 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
324 {
325         gchar *title = NULL;
326         gchar *id = g_strdup("histogram");
327         gboolean expanded = TRUE;
328         gint height = 80;
329         gint histogram_channel = HCHAN_RGB;
330         gint histogram_mode = 0;
331         GtkWidget *ret;
332
333         while (*attribute_names)
334                 {
335                 const gchar *option = *attribute_names++;
336                 const gchar *value = *attribute_values++;
337
338                 if (READ_CHAR_FULL("id", id)) continue;
339                 if (READ_CHAR_FULL("title", title)) continue;
340                 if (READ_BOOL_FULL("expanded", expanded)) continue;
341                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
342                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
343
344                 log_printf("unknown attribute %s = %s\n", option, value);
345                 }
346         
347         bar_pane_translate_title(PANE_HISTOGRAM, id, &title);
348         ret = bar_pane_histogram_new(id, title, height, expanded, histogram_channel, histogram_mode);
349         g_free(title);
350         g_free(id);
351         return ret;
352 }
353
354 void bar_pane_histogram_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
355 {
356         PaneHistogramData *phd;
357
358         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
359         if (!phd) return;
360
361         gint histogram_channel = phd->histogram->histogram_channel;
362         gint histogram_mode = phd->histogram->histogram_mode;
363
364         while (*attribute_names)
365                 {
366                 const gchar *option = *attribute_names++;
367                 const gchar *value = *attribute_values++;
368
369                 if (READ_CHAR_FULL("id", phd->pane.id)) continue;
370 //              if (READ_CHAR_FULL("pane.title", title)) continue;
371                 if (READ_BOOL_FULL("expanded", phd->pane.expanded)) continue;
372                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
373                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
374                 
375
376                 log_printf("unknown attribute %s = %s\n", option, value);
377                 }
378         
379         histogram_set_channel(phd->histogram, histogram_channel);
380         histogram_set_mode(phd->histogram, histogram_mode);
381
382         bar_update_expander(pane);
383         bar_pane_histogram_update(phd);
384 }
385
386
387 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */