config file format changed to XML
[geeqie.git] / src / bar_comment.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
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 "rcfile.h"
23
24 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
25
26 /*
27  *-------------------------------------------------------------------
28  * keyword / comment utils
29  *-------------------------------------------------------------------
30  */
31
32
33
34 typedef struct _PaneCommentData PaneCommentData;
35 struct _PaneCommentData
36 {
37         PaneData pane;
38         GtkWidget *widget;
39         GtkWidget *comment_view;
40         FileData *fd;
41         gchar *key;
42         gint height;
43 };
44
45
46 static void bar_pane_comment_write(PaneCommentData *pcd)
47 {
48         gchar *comment;
49
50         if (!pcd->fd) return;
51
52         comment = text_widget_text_pull(pcd->comment_view);
53
54         metadata_write_string(pcd->fd, pcd->key, comment);
55         g_free(comment);
56 }
57
58
59 static void bar_pane_comment_update(PaneCommentData *pcd)
60 {
61         gchar *comment = NULL;
62         GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
63
64         g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
65
66         comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
67         gtk_text_buffer_set_text(comment_buffer,
68                                  (comment) ? comment : "", -1);
69         g_free(comment);
70         
71         g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
72
73         gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
74 }
75
76 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
77 {
78         GList *list = NULL;
79         GList *work;
80         gchar *comment = NULL;
81
82         if (!pcd->pane.list_func) return;
83
84         comment = text_widget_text_pull(pcd->comment_view);
85
86         list = pcd->pane.list_func(pcd->pane.list_data);
87         work = list;
88         while (work)
89                 {
90                 FileData *fd = work->data;
91                 work = work->next;
92
93                 if (append)
94                         {
95                         metadata_append_string(fd, pcd->key, comment);
96                         }
97                 else
98                         {
99                         metadata_write_string(fd, pcd->key, comment);
100                         }
101                 }
102
103         filelist_free(list);
104         g_free(comment);
105 }
106
107 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
108 {
109         PaneCommentData *pcd = data;
110
111         bar_pane_comment_set_selection(pcd, TRUE);
112 }
113
114 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
115 {
116         PaneCommentData *pcd = data;
117
118         bar_pane_comment_set_selection(pcd, FALSE);
119 }
120
121
122 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
123 {
124         PaneCommentData *pcd;
125
126         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
127         if (!pcd) return;
128
129         file_data_unref(pcd->fd);
130         pcd->fd = file_data_ref(fd);
131
132         bar_pane_comment_update(pcd);
133 }
134
135 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
136 {
137         PaneCommentData *pcd;
138
139         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
140         if (!pcd) return FALSE;
141
142         if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
143
144         return FALSE;
145 }
146
147 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
148 {
149         PaneCommentData *pcd;
150
151         pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
152         if (!pcd) return;
153
154         write_indent(outstr, indent);
155         g_string_append_printf(outstr, "<pane_comment\n");
156         indent++;
157         WRITE_CHAR(*pcd, pane.title);
158         WRITE_BOOL(*pcd, pane.expanded);
159         WRITE_CHAR(*pcd, key);
160         WRITE_INT(*pcd, height); 
161         indent--;
162         write_indent(outstr, indent);
163         g_string_append_printf(outstr, "/>\n");
164 }
165
166 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
167 {
168         PaneCommentData *pcd = data;
169         if (fd == pcd->fd) bar_pane_comment_update(pcd);
170 }
171
172 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
173 {
174         PaneCommentData *pcd = data;
175
176         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
177         bar_pane_comment_write(pcd);
178         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
179 }
180
181
182 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
183 {
184         PaneCommentData *pcd = data;
185
186         menu_item_add_divider(GTK_WIDGET(menu));
187         menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
188         menu_item_add_stock(GTK_WIDGET(menu), _("Replace existing text in selected files"), GTK_STOCK_CONVERT, G_CALLBACK(bar_pane_comment_sel_replace_cb), data);
189 }
190
191
192 static void bar_pane_comment_close(GtkWidget *bar)
193 {
194         PaneCommentData *pcd;
195
196         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
197         if (!pcd) return;
198
199         gtk_widget_destroy(pcd->comment_view);
200 }
201
202 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
203 {
204         PaneCommentData *pcd = data;
205
206         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
207
208         file_data_unref(pcd->fd);
209         g_free(pcd->pane.title);
210         g_free(pcd->key);
211         
212
213         g_free(pcd);
214 }
215
216
217 GtkWidget *bar_pane_comment_new(const gchar *title, const gchar *key, gboolean expanded, gint height)
218 {
219         PaneCommentData *pcd;
220         GtkWidget *scrolled;
221         GtkTextBuffer *buffer;
222
223         pcd = g_new0(PaneCommentData, 1);
224         
225         pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
226         pcd->pane.pane_event = bar_pane_comment_event;
227         pcd->pane.pane_write_config = bar_pane_comment_write_config;
228         pcd->pane.title = g_strdup(title);
229         pcd->pane.expanded = expanded;
230         
231         pcd->key = g_strdup(key);
232         pcd->height = height;
233
234         scrolled = gtk_scrolled_window_new(NULL, NULL);
235         
236         pcd->widget = scrolled;
237         g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
238         g_signal_connect(G_OBJECT(pcd->widget), "destroy",
239                          G_CALLBACK(bar_pane_comment_destroy), pcd);
240         
241         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
242         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
243                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
244
245         gtk_widget_set_size_request(scrolled, -1, height);
246         gtk_widget_show(scrolled);
247
248         pcd->comment_view = gtk_text_view_new();
249         gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
250         g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
251                          G_CALLBACK(bar_pane_comment_populate_popup), pcd);
252         gtk_widget_show(pcd->comment_view);
253
254         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
255         g_signal_connect(G_OBJECT(buffer), "changed",
256                          G_CALLBACK(bar_pane_comment_changed), pcd);
257
258
259         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
260
261         return pcd->widget;
262 }
263
264 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
265 {
266         gchar *title = g_strdup(_("NoName"));
267         gchar *key = g_strdup(COMMENT_KEY);
268         gboolean expanded = TRUE;
269         gint height = 50;
270
271         while (*attribute_names)
272                 {
273                 const gchar *option = *attribute_names++;
274                 const gchar *value = *attribute_values++;
275
276                 READ_CHAR_FULL("pane.title", title);
277                 READ_CHAR_FULL("key", key);
278                 READ_BOOL_FULL("pane.expanded", expanded);
279                 READ_INT_FULL("height", height);
280                 
281
282                 DEBUG_1("unknown attribute %s = %s", option, value);
283                 }
284         
285         return bar_pane_comment_new(title, key, expanded, height);
286 }
287
288 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */