added histogram pane
[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_comment.h"
16
17 #include "bar.h"
18 #include "metadata.h"
19 #include "filedata.h"
20 #include "ui_menu.h"
21 #include "ui_misc.h"
22 #include "histogram.h"
23
24 /*
25  *-------------------------------------------------------------------
26  * keyword / comment utils
27  *-------------------------------------------------------------------
28  */
29
30
31
32 typedef struct _PaneHistogramData PaneHistogramData;
33 struct _PaneHistogramData
34 {
35         PaneData pane;
36         GtkWidget *widget;
37         GtkWidget *drawing_area;
38         Histogram *histogram;
39         gint histogram_width;
40         gint histogram_height;
41         GdkPixbuf *pixbuf;
42         FileData *fd;
43 };
44
45
46 static void bar_pane_histogram_update(PaneHistogramData *phd)
47 {
48         const HistMap *histmap;
49         if (phd->pixbuf) g_object_unref(phd->pixbuf);
50         phd->pixbuf = NULL;
51
52         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
53
54         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
55         
56         histmap = histmap_get(phd->fd);
57         
58         if (!histmap) return;
59         
60         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
61         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
62         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
63 }
64
65
66 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
67 {
68         PaneHistogramData *phd;
69
70         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
71         if (!phd) return;
72
73         file_data_unref(phd->fd);
74         phd->fd = file_data_ref(fd);
75
76         bar_pane_histogram_update(phd);
77 }
78
79 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
80 {
81         PaneHistogramData *phd = data;
82         if (fd == phd->fd) bar_pane_histogram_update(phd);
83 }
84
85 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
86 {
87         PaneHistogramData *phd = data;
88         if (!phd || !phd->pixbuf) return TRUE;
89         
90         gdk_draw_pixbuf(widget->window,
91                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
92                         phd->pixbuf,
93                         0, 0,
94                         0, 0,
95                         -1, -1,
96                         GDK_RGB_DITHER_NORMAL, 0, 0);
97         return TRUE;
98 }
99
100 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
101 {
102         PaneHistogramData *phd = data;
103
104         phd->histogram_width = allocation->width;
105         phd->histogram_height = allocation->height;
106         bar_pane_histogram_update(phd);
107 }
108
109 static void bar_pane_histogram_close(GtkWidget *pane)
110 {
111         PaneHistogramData *phd;
112
113         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
114         if (!phd) return;
115
116         gtk_widget_destroy(phd->widget);
117 }
118
119 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
120 {
121         PaneHistogramData *phd = data;
122
123         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
124
125         file_data_unref(phd->fd);
126         g_free(phd->pane.title);
127         histogram_free(phd->histogram);
128         if (phd->pixbuf) g_object_unref(phd->pixbuf);
129
130         g_free(phd);
131 }
132
133
134 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height)
135 {
136         PaneHistogramData *phd;
137
138         phd = g_new0(PaneHistogramData, 1);
139         
140         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
141         phd->pane.title = g_strdup(title);
142         
143         phd->histogram = histogram_new();
144         
145         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
146
147         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
148         g_signal_connect(G_OBJECT(phd->widget), "destroy",
149                          G_CALLBACK(bar_pane_histogram_destroy), phd);
150         
151
152         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
153
154         phd->drawing_area = gtk_drawing_area_new();
155         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
156                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
157
158         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
159                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
160                          
161         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
162         gtk_widget_show(phd->drawing_area);
163
164
165         gtk_widget_show(phd->widget);
166
167         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
168
169         return phd->widget;
170 }
171
172 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */