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