4 * Copyright (C) 2008 - 2012 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 gchar *orig_comment = NULL;
64 gchar *comment_not_null;
65 GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
67 orig_comment = text_widget_text_pull(pcd->comment_view);
68 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
69 comment_not_null = (comment) ? comment : "";
71 if (strcmp(orig_comment, comment_not_null) != 0)
73 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
74 gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
75 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
80 gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
83 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
87 gchar *comment = NULL;
89 comment = text_widget_text_pull(pcd->comment_view);
91 list = layout_selection_list(pcd->pane.lw);
92 list = file_data_process_groups_in_selection(list, FALSE, NULL);
97 FileData *fd = work->data;
99 if (fd == pcd->fd) continue;
103 metadata_append_string(fd, pcd->key, comment);
107 metadata_write_string(fd, pcd->key, comment);
115 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
117 PaneCommentData *pcd = data;
119 bar_pane_comment_set_selection(pcd, TRUE);
122 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
124 PaneCommentData *pcd = data;
126 bar_pane_comment_set_selection(pcd, FALSE);
130 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
132 PaneCommentData *pcd;
134 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
137 file_data_unref(pcd->fd);
138 pcd->fd = file_data_ref(fd);
140 bar_pane_comment_update(pcd);
143 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
145 PaneCommentData *pcd;
147 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
148 if (!pcd) return FALSE;
150 if (gtk_widget_has_focus(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
155 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
157 PaneCommentData *pcd;
159 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
162 WRITE_NL(); WRITE_STRING("<pane_comment ");
163 write_char_option(outstr, indent, "id", pcd->pane.id);
164 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
165 WRITE_BOOL(pcd->pane, expanded);
166 WRITE_CHAR(*pcd, key);
167 WRITE_INT(*pcd, height);
171 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
173 PaneCommentData *pcd = data;
174 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
176 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
178 bar_pane_comment_update(pcd);
182 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
184 PaneCommentData *pcd = data;
186 bar_pane_comment_write(pcd);
190 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
192 PaneCommentData *pcd = data;
194 menu_item_add_divider(GTK_WIDGET(menu));
195 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
196 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);
199 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
201 PaneCommentData *pcd = data;
203 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
205 file_data_unref(pcd->fd);
208 g_free(pcd->pane.id);
214 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
216 PaneCommentData *pcd;
218 GtkTextBuffer *buffer;
220 pcd = g_new0(PaneCommentData, 1);
222 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
223 pcd->pane.pane_event = bar_pane_comment_event;
224 pcd->pane.pane_write_config = bar_pane_comment_write_config;
225 pcd->pane.title = bar_pane_expander_title(title);
226 pcd->pane.id = g_strdup(id);
227 pcd->pane.type = PANE_COMMENT;
229 pcd->pane.expanded = expanded;
231 pcd->key = g_strdup(key);
232 pcd->height = height;
234 scrolled = gtk_scrolled_window_new(NULL, NULL);
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);
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);
245 gtk_widget_set_size_request(pcd->widget, -1, height);
246 gtk_widget_show(scrolled);
248 pcd->comment_view = gtk_text_view_new();
249 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
250 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
251 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
252 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
253 gtk_widget_show(pcd->comment_view);
255 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
256 g_signal_connect(G_OBJECT(buffer), "changed",
257 G_CALLBACK(bar_pane_comment_changed), pcd);
260 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
265 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
268 gchar *key = g_strdup(COMMENT_KEY);
269 gboolean expanded = TRUE;
271 gchar *id = g_strdup("comment");
274 while (*attribute_names)
276 const gchar *option = *attribute_names++;
277 const gchar *value = *attribute_values++;
279 if (READ_CHAR_FULL("title", title)) continue;
280 if (READ_CHAR_FULL("key", key)) continue;
281 if (READ_BOOL_FULL("expanded", expanded)) continue;
282 if (READ_INT_FULL("height", height)) continue;
283 if (READ_CHAR_FULL("id", id)) continue;
286 log_printf("unknown attribute %s = %s\n", option, value);
289 bar_pane_translate_title(PANE_COMMENT, id, &title);
290 ret = bar_pane_comment_new(id, title, key, expanded, height);
297 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
299 PaneCommentData *pcd;
301 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
306 while (*attribute_names)
308 const gchar *option = *attribute_names++;
309 const gchar *value = *attribute_values++;
311 if (READ_CHAR_FULL("title", title)) continue;
312 if (READ_CHAR_FULL("key", pcd->key)) continue;
313 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
314 if (READ_INT_FULL("height", pcd->height)) continue;
315 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
318 log_printf("unknown attribute %s = %s\n", option, value);
323 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
324 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
327 gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
328 bar_update_expander(pane);
329 bar_pane_comment_update(pcd);
332 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */