give the panes more control over expander title
[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_option(outstr, indent, "pane.title", gtk_label_get_text(GTK_LABEL(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         histogram_free(phd->histogram);
184         if (phd->pixbuf) g_object_unref(phd->pixbuf);
185
186         g_free(phd);
187 }
188
189 static void bar_pane_histogram_popup_channels_cb(GtkWidget *widget, gpointer data)
190 {
191         PaneHistogramData *phd;
192         gint channel;
193
194         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
195
196         phd = submenu_item_get_data(widget);
197
198         if (!phd) return;
199
200         channel = GPOINTER_TO_INT(data);
201         if (channel == histogram_get_channel(phd->histogram)) return;
202
203         histogram_set_channel(phd->histogram, channel);
204         bar_pane_histogram_update(phd);
205 }
206
207 static void bar_pane_histogram_popup_logmode_cb(GtkWidget *widget, gpointer data)
208 {
209         PaneHistogramData *phd;
210         gint logmode;
211         
212         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
213
214         phd = submenu_item_get_data(widget);
215
216         if (!phd) return;
217
218         logmode = GPOINTER_TO_INT(data);
219         if (logmode == histogram_get_mode(phd->histogram)) return;
220
221         histogram_set_mode(phd->histogram, logmode);
222         bar_pane_histogram_update(phd);
223 }
224
225 static GtkWidget *bar_pane_histogram_add_radio(GtkWidget *menu, GtkWidget *parent,
226                                         const gchar *label,
227                                         GCallback func, gint value,
228                                         gboolean show_current, gint current_value)
229 {
230         GtkWidget *item;
231
232         if (show_current)
233                 {
234                 item = menu_item_add_radio(menu, parent,
235                                            label, (value == current_value),
236                                            func, GINT_TO_POINTER((gint)value));
237                 }
238         else
239                 {
240                 item = menu_item_add(menu, label,
241                                      func, GINT_TO_POINTER((gint)value));
242                 }
243
244         return item;
245 }
246
247 GtkWidget *bar_pane_histogram_add_channels(GtkWidget *menu, GCallback func, gpointer data,
248                                            gboolean show_current, gint current_value)
249 {
250         GtkWidget *submenu;
251         GtkWidget *parent;
252
253         submenu = gtk_menu_new();
254         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
255
256         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Red"), func, HCHAN_R, show_current, current_value);
257         bar_pane_histogram_add_radio(submenu, parent, _("_Green"), func, HCHAN_G, show_current, current_value);
258         bar_pane_histogram_add_radio(submenu, parent, _("_Blue"),func, HCHAN_B, show_current, current_value);
259         bar_pane_histogram_add_radio(submenu, parent, _("_RGB"),func, HCHAN_RGB, show_current, current_value);
260         bar_pane_histogram_add_radio(submenu, parent, _("_Value"),func, HCHAN_MAX, show_current, current_value);
261
262         if (menu)
263                 {
264                 GtkWidget *item;
265
266                 item = menu_item_add(menu, _("Channels"), NULL, NULL);
267                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
268                 return item;
269                 }
270
271         return submenu;
272 }
273 GtkWidget *bar_pane_histogram_add_logmode(GtkWidget *menu, GCallback func, gpointer data,
274                                            gboolean show_current, gint current_value)
275 {
276         GtkWidget *submenu;
277         GtkWidget *parent;
278
279         submenu = gtk_menu_new();
280         g_object_set_data(G_OBJECT(submenu), "submenu_data", data);
281
282         parent = bar_pane_histogram_add_radio(submenu, NULL, _("_Linear"), func, 0, show_current, current_value);
283         bar_pane_histogram_add_radio(submenu, parent, _("Lo_garithmical"), func, 1, show_current, current_value);
284
285         if (menu)
286                 {
287                 GtkWidget *item;
288
289                 item = menu_item_add(menu, _("Mode"), NULL, NULL);
290                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
291                 return item;
292                 }
293
294         return submenu;
295 }
296
297
298 static GtkWidget *bar_pane_histogram_menu(PaneHistogramData *phd)
299 {
300         GtkWidget *menu;
301         static gboolean show_current = TRUE;
302
303         menu = popup_menu_short_lived();
304         bar_pane_histogram_add_channels(menu, G_CALLBACK(bar_pane_histogram_popup_channels_cb), phd,
305                                         show_current, histogram_get_channel(phd->histogram));
306         bar_pane_histogram_add_logmode(menu, G_CALLBACK(bar_pane_histogram_popup_logmode_cb), phd,
307                                        show_current, histogram_get_mode(phd->histogram));
308
309         return menu;
310 }
311
312 static gboolean bar_pane_histogram_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
313 {
314         PaneHistogramData *phd = data;
315
316         if (bevent->button == MOUSE_BUTTON_RIGHT)
317                 {
318                 GtkWidget *menu;
319
320                 menu = bar_pane_histogram_menu(phd);
321                 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
322                 return TRUE;
323         }
324
325         return FALSE;
326 }
327
328
329 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gboolean expanded, gint histogram_channel, gint histogram_mode)
330 {
331         PaneHistogramData *phd;
332
333         phd = g_new0(PaneHistogramData, 1);
334         
335         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
336         phd->pane.pane_write_config = bar_pane_histogram_write_config;
337         phd->pane.title = gtk_label_new(title);
338         phd->pane.expanded = expanded;
339         phd->idle_id = -1;
340         
341         phd->histogram = histogram_new();
342
343         histogram_set_channel(phd->histogram, histogram_channel);
344         histogram_set_mode(phd->histogram, histogram_mode);
345
346         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
347
348         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
349         g_signal_connect(G_OBJECT(phd->widget), "destroy",
350                          G_CALLBACK(bar_pane_histogram_destroy), phd);
351         
352
353         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
354
355         phd->drawing_area = gtk_drawing_area_new();
356         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
357                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
358
359         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
360                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
361                          
362         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
363         gtk_widget_show(phd->drawing_area);
364         gtk_widget_add_events(phd->drawing_area, GDK_BUTTON_PRESS_MASK);
365
366         g_signal_connect(G_OBJECT(phd->drawing_area), "button_press_event", G_CALLBACK(bar_pane_histogram_press_cb), phd);
367
368 #if GTK_CHECK_VERSION(2,12,0)
369         gtk_widget_set_tooltip_text(phd->drawing_area, histogram_label(phd->histogram));
370 #endif
371         gtk_widget_show(phd->widget);
372
373         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
374
375         return phd->widget;
376 }
377
378 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
379 {
380         gchar *title = g_strdup(_("NoName"));
381         gboolean expanded = TRUE;
382         gint height = 80;
383         gint histogram_channel = HCHAN_RGB;
384         gint histogram_mode = 0;
385
386         while (*attribute_names)
387                 {
388                 const gchar *option = *attribute_names++;
389                 const gchar *value = *attribute_values++;
390
391                 if (READ_CHAR_FULL("pane.title", title)) continue;
392                 if (READ_BOOL_FULL("pane.expanded", expanded)) continue;
393                 if (READ_INT_FULL("histogram_channel", histogram_channel)) continue;
394                 if (READ_INT_FULL("histogram_mode", histogram_mode)) continue;
395
396                 
397                 DEBUG_1("unknown attribute %s = %s", option, value);
398                 }
399         
400         return bar_pane_histogram_new(title, height, expanded, histogram_channel, histogram_mode);
401 }
402
403
404 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */