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