4 * Copyright (C) 2008 - 2010 The Geeqie Team
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!
15 #include "bar_comment.h"
25 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
28 *-------------------------------------------------------------------
29 * keyword / comment utils
30 *-------------------------------------------------------------------
35 typedef struct _PaneCommentData PaneCommentData;
36 struct _PaneCommentData
40 GtkWidget *comment_view;
47 static void bar_pane_comment_write(PaneCommentData *pcd)
53 comment = text_widget_text_pull(pcd->comment_view);
55 metadata_write_string(pcd->fd, pcd->key, comment);
60 static void bar_pane_comment_update(PaneCommentData *pcd)
62 gchar *comment = NULL;
63 GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
65 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
67 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
68 gtk_text_buffer_set_text(comment_buffer,
69 (comment) ? comment : "", -1);
72 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
74 gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
77 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
81 gchar *comment = NULL;
83 comment = text_widget_text_pull(pcd->comment_view);
85 list = layout_selection_list(pcd->pane.lw);
86 list = file_data_process_groups_in_selection(list, FALSE, NULL);
91 FileData *fd = work->data;
93 if (fd == pcd->fd) continue;
97 metadata_append_string(fd, pcd->key, comment);
101 metadata_write_string(fd, pcd->key, comment);
109 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
111 PaneCommentData *pcd = data;
113 bar_pane_comment_set_selection(pcd, TRUE);
116 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
118 PaneCommentData *pcd = data;
120 bar_pane_comment_set_selection(pcd, FALSE);
124 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
126 PaneCommentData *pcd;
128 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
131 file_data_unref(pcd->fd);
132 pcd->fd = file_data_ref(fd);
134 bar_pane_comment_update(pcd);
137 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
139 PaneCommentData *pcd;
141 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
142 if (!pcd) return FALSE;
144 if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
149 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
151 PaneCommentData *pcd;
153 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
156 WRITE_NL(); WRITE_STRING("<pane_comment ");
157 write_char_option(outstr, indent, "id", pcd->pane.id);
158 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
159 WRITE_BOOL(pcd->pane, expanded);
160 WRITE_CHAR(*pcd, key);
161 WRITE_INT(*pcd, height);
165 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
167 PaneCommentData *pcd = data;
168 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
170 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
172 bar_pane_comment_update(pcd);
176 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
178 PaneCommentData *pcd = data;
180 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
181 bar_pane_comment_write(pcd);
182 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
186 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
188 PaneCommentData *pcd = data;
190 menu_item_add_divider(GTK_WIDGET(menu));
191 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
192 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);
196 static void bar_pane_comment_close(GtkWidget *bar)
198 PaneCommentData *pcd;
200 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
203 gtk_widget_destroy(pcd->comment_view);
207 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
209 PaneCommentData *pcd = data;
211 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
213 file_data_unref(pcd->fd);
216 g_free(pcd->pane.id);
222 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
224 PaneCommentData *pcd;
226 GtkTextBuffer *buffer;
228 pcd = g_new0(PaneCommentData, 1);
230 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
231 pcd->pane.pane_event = bar_pane_comment_event;
232 pcd->pane.pane_write_config = bar_pane_comment_write_config;
233 pcd->pane.title = bar_pane_expander_title(title);
234 pcd->pane.id = g_strdup(id);
235 pcd->pane.type = PANE_COMMENT;
237 pcd->pane.expanded = expanded;
239 pcd->key = g_strdup(key);
240 pcd->height = height;
242 scrolled = gtk_scrolled_window_new(NULL, NULL);
244 pcd->widget = scrolled;
245 g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
246 g_signal_connect(G_OBJECT(pcd->widget), "destroy",
247 G_CALLBACK(bar_pane_comment_destroy), pcd);
249 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
250 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
251 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
253 gtk_widget_set_size_request(pcd->widget, -1, height);
254 gtk_widget_show(scrolled);
256 pcd->comment_view = gtk_text_view_new();
257 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
258 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
259 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
260 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
261 gtk_widget_show(pcd->comment_view);
263 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
264 g_signal_connect(G_OBJECT(buffer), "changed",
265 G_CALLBACK(bar_pane_comment_changed), pcd);
268 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
273 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
276 gchar *key = g_strdup(COMMENT_KEY);
277 gboolean expanded = TRUE;
279 gchar *id = g_strdup("comment");
282 while (*attribute_names)
284 const gchar *option = *attribute_names++;
285 const gchar *value = *attribute_values++;
287 if (READ_CHAR_FULL("title", title)) continue;
288 if (READ_CHAR_FULL("key", key)) continue;
289 if (READ_BOOL_FULL("expanded", expanded)) continue;
290 if (READ_INT_FULL("height", height)) continue;
291 if (READ_CHAR_FULL("id", id)) continue;
294 log_printf("unknown attribute %s = %s\n", option, value);
297 bar_pane_translate_title(PANE_COMMENT, id, &title);
298 ret = bar_pane_comment_new(id, title, key, expanded, height);
305 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
307 PaneCommentData *pcd;
309 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
314 while (*attribute_names)
316 const gchar *option = *attribute_names++;
317 const gchar *value = *attribute_values++;
319 if (READ_CHAR_FULL("title", title)) continue;
320 if (READ_CHAR_FULL("key", pcd->key)) continue;
321 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
322 if (READ_INT_FULL("height", pcd->height)) continue;
323 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
326 log_printf("unknown attribute %s = %s\n", option, value);
331 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
332 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
335 gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
336 bar_update_expander(pane);
337 bar_pane_comment_update(pcd);
340 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */