simplified config writing
[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 #include "rcfile.h"
24
25 /*
26  *-------------------------------------------------------------------
27  * keyword / comment utils
28  *-------------------------------------------------------------------
29  */
30
31
32
33 typedef struct _PaneHistogramData PaneHistogramData;
34 struct _PaneHistogramData
35 {
36         PaneData pane;
37         GtkWidget *widget;
38         GtkWidget *drawing_area;
39         Histogram *histogram;
40         gint histogram_width;
41         gint histogram_height;
42         GdkPixbuf *pixbuf;
43         FileData *fd;
44 };
45
46
47 static void bar_pane_histogram_update(PaneHistogramData *phd)
48 {
49         const HistMap *histmap;
50         if (phd->pixbuf) g_object_unref(phd->pixbuf);
51         phd->pixbuf = NULL;
52
53         if (!phd->histogram_width || !phd->histogram_height || !phd->fd) return;
54
55         gtk_widget_queue_draw_area(GTK_WIDGET(phd->drawing_area), 0, 0, phd->histogram_width, phd->histogram_height);
56         
57         histmap = histmap_get(phd->fd);
58         
59         if (!histmap) return;
60         
61         phd->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, phd->histogram_width, phd->histogram_height);
62         gdk_pixbuf_fill(phd->pixbuf, 0xffffffff);
63         histogram_draw(phd->histogram, histmap, phd->pixbuf, 0, 0, phd->histogram_width, phd->histogram_height);
64 }
65
66
67 static void bar_pane_histogram_set_fd(GtkWidget *pane, FileData *fd)
68 {
69         PaneHistogramData *phd;
70
71         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
72         if (!phd) return;
73
74         file_data_unref(phd->fd);
75         phd->fd = file_data_ref(fd);
76
77         bar_pane_histogram_update(phd);
78 }
79
80 static void bar_pane_histogram_write_config(GtkWidget *pane, GString *outstr, gint indent)
81 {
82         PaneHistogramData *phd;
83
84         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
85         if (!phd) return;
86
87         WRITE_STRING("<pane_histogram\n");
88         indent++;
89         WRITE_CHAR(*phd, pane.title);
90         WRITE_BOOL(*phd, pane.expanded);
91         indent--;
92         WRITE_STRING("/>\n");
93 }
94
95
96 static void bar_pane_histogram_notify_cb(FileData *fd, NotifyType type, gpointer data)
97 {
98         PaneHistogramData *phd = data;
99         if (fd == phd->fd) bar_pane_histogram_update(phd);
100 }
101
102 static gboolean bar_pane_histogram_expose_event_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)
103 {
104         PaneHistogramData *phd = data;
105         if (!phd || !phd->pixbuf) return TRUE;
106         
107         gdk_draw_pixbuf(widget->window,
108                         widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
109                         phd->pixbuf,
110                         0, 0,
111                         0, 0,
112                         -1, -1,
113                         GDK_RGB_DITHER_NORMAL, 0, 0);
114         return TRUE;
115 }
116
117 static void bar_pane_histogram_size_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
118 {
119         PaneHistogramData *phd = data;
120
121         phd->histogram_width = allocation->width;
122         phd->histogram_height = allocation->height;
123         bar_pane_histogram_update(phd);
124 }
125
126 static void bar_pane_histogram_close(GtkWidget *pane)
127 {
128         PaneHistogramData *phd;
129
130         phd = g_object_get_data(G_OBJECT(pane), "pane_data");
131         if (!phd) return;
132
133         gtk_widget_destroy(phd->widget);
134 }
135
136 static void bar_pane_histogram_destroy(GtkWidget *widget, gpointer data)
137 {
138         PaneHistogramData *phd = data;
139
140         file_data_unregister_notify_func(bar_pane_histogram_notify_cb, phd);
141
142         file_data_unref(phd->fd);
143         g_free(phd->pane.title);
144         histogram_free(phd->histogram);
145         if (phd->pixbuf) g_object_unref(phd->pixbuf);
146
147         g_free(phd);
148 }
149
150
151 GtkWidget *bar_pane_histogram_new(const gchar *title, gint height, gint expanded)
152 {
153         PaneHistogramData *phd;
154
155         phd = g_new0(PaneHistogramData, 1);
156         
157         phd->pane.pane_set_fd = bar_pane_histogram_set_fd;
158         phd->pane.pane_write_config = bar_pane_histogram_write_config;
159         phd->pane.title = g_strdup(title);
160         phd->pane.expanded = expanded;
161         
162         phd->histogram = histogram_new();
163         
164         phd->widget = gtk_vbox_new(FALSE, PREF_PAD_GAP);
165
166         g_object_set_data(G_OBJECT(phd->widget), "pane_data", phd);
167         g_signal_connect(G_OBJECT(phd->widget), "destroy",
168                          G_CALLBACK(bar_pane_histogram_destroy), phd);
169         
170
171         gtk_widget_set_size_request(GTK_WIDGET(phd->widget), -1, height);
172
173         phd->drawing_area = gtk_drawing_area_new();
174         g_signal_connect_after(G_OBJECT(phd->drawing_area), "size_allocate",
175                                G_CALLBACK(bar_pane_histogram_size_cb), phd);
176
177         g_signal_connect(G_OBJECT(phd->drawing_area), "expose_event",  
178                          G_CALLBACK(bar_pane_histogram_expose_event_cb), phd);
179                          
180         gtk_box_pack_start(GTK_BOX(phd->widget), phd->drawing_area, TRUE, TRUE, 0);
181         gtk_widget_show(phd->drawing_area);
182
183
184         gtk_widget_show(phd->widget);
185
186         file_data_register_notify_func(bar_pane_histogram_notify_cb, phd, NOTIFY_PRIORITY_LOW);
187
188         return phd->widget;
189 }
190
191 GtkWidget *bar_pane_histogram_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
192 {
193         gchar *title = g_strdup(_("NoName"));
194         gboolean expanded = TRUE;
195         gint height = 80;
196
197         while (*attribute_names)
198                 {
199                 const gchar *option = *attribute_names++;
200                 const gchar *value = *attribute_values++;
201
202                 READ_CHAR_FULL("pane.title", title);
203                 READ_BOOL_FULL("pane.expanded", expanded);
204                 
205
206                 DEBUG_1("unknown attribute %s = %s", option, value);
207                 }
208         
209         return bar_pane_histogram_new(title, height, expanded);
210 }
211
212
213 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */