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);
200 static void bar_pane_comment_close(GtkWidget *bar)
202 PaneCommentData *pcd;
204 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
207 gtk_widget_destroy(pcd->comment_view);
211 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
213 PaneCommentData *pcd = data;
215 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
217 file_data_unref(pcd->fd);
220 g_free(pcd->pane.id);
226 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
228 PaneCommentData *pcd;
230 GtkTextBuffer *buffer;
232 pcd = g_new0(PaneCommentData, 1);
234 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
235 pcd->pane.pane_event = bar_pane_comment_event;
236 pcd->pane.pane_write_config = bar_pane_comment_write_config;
237 pcd->pane.title = bar_pane_expander_title(title);
238 pcd->pane.id = g_strdup(id);
239 pcd->pane.type = PANE_COMMENT;
241 pcd->pane.expanded = expanded;
243 pcd->key = g_strdup(key);
244 pcd->height = height;
246 scrolled = gtk_scrolled_window_new(NULL, NULL);
248 pcd->widget = scrolled;
249 g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
250 g_signal_connect(G_OBJECT(pcd->widget), "destroy",
251 G_CALLBACK(bar_pane_comment_destroy), pcd);
253 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
254 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
255 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
257 gtk_widget_set_size_request(pcd->widget, -1, height);
258 gtk_widget_show(scrolled);
260 pcd->comment_view = gtk_text_view_new();
261 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
262 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
263 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
264 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
265 gtk_widget_show(pcd->comment_view);
267 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
268 g_signal_connect(G_OBJECT(buffer), "changed",
269 G_CALLBACK(bar_pane_comment_changed), pcd);
272 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
277 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
280 gchar *key = g_strdup(COMMENT_KEY);
281 gboolean expanded = TRUE;
283 gchar *id = g_strdup("comment");
286 while (*attribute_names)
288 const gchar *option = *attribute_names++;
289 const gchar *value = *attribute_values++;
291 if (READ_CHAR_FULL("title", title)) continue;
292 if (READ_CHAR_FULL("key", key)) continue;
293 if (READ_BOOL_FULL("expanded", expanded)) continue;
294 if (READ_INT_FULL("height", height)) continue;
295 if (READ_CHAR_FULL("id", id)) continue;
298 log_printf("unknown attribute %s = %s\n", option, value);
301 bar_pane_translate_title(PANE_COMMENT, id, &title);
302 ret = bar_pane_comment_new(id, title, key, expanded, height);
309 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
311 PaneCommentData *pcd;
313 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
318 while (*attribute_names)
320 const gchar *option = *attribute_names++;
321 const gchar *value = *attribute_values++;
323 if (READ_CHAR_FULL("title", title)) continue;
324 if (READ_CHAR_FULL("key", pcd->key)) continue;
325 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
326 if (READ_INT_FULL("height", pcd->height)) continue;
327 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
330 log_printf("unknown attribute %s = %s\n", option, value);
335 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
336 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
339 gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
340 bar_update_expander(pane);
341 bar_pane_comment_update(pcd);
344 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */