4 * Copyright (C) 2008 - 2009 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);
89 FileData *fd = work->data;
94 metadata_append_string(fd, pcd->key, comment);
98 metadata_write_string(fd, pcd->key, comment);
106 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
108 PaneCommentData *pcd = data;
110 bar_pane_comment_set_selection(pcd, TRUE);
113 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
115 PaneCommentData *pcd = data;
117 bar_pane_comment_set_selection(pcd, FALSE);
121 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
123 PaneCommentData *pcd;
125 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
128 file_data_unref(pcd->fd);
129 pcd->fd = file_data_ref(fd);
131 bar_pane_comment_update(pcd);
134 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
136 PaneCommentData *pcd;
138 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
139 if (!pcd) return FALSE;
141 if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
146 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
148 PaneCommentData *pcd;
150 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
153 WRITE_NL(); WRITE_STRING("<pane_comment ");
154 write_char_option(outstr, indent, "id", pcd->pane.id);
155 write_char_option(outstr, indent, "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);
162 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
164 PaneCommentData *pcd = data;
165 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
167 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
169 bar_pane_comment_update(pcd);
173 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
175 PaneCommentData *pcd = data;
177 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
178 bar_pane_comment_write(pcd);
179 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
183 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
185 PaneCommentData *pcd = data;
187 menu_item_add_divider(GTK_WIDGET(menu));
188 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
189 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);
193 static void bar_pane_comment_close(GtkWidget *bar)
195 PaneCommentData *pcd;
197 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
200 gtk_widget_destroy(pcd->comment_view);
204 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
206 PaneCommentData *pcd = data;
208 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
210 file_data_unref(pcd->fd);
213 g_free(pcd->pane.id);
219 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
221 PaneCommentData *pcd;
223 GtkTextBuffer *buffer;
225 pcd = g_new0(PaneCommentData, 1);
227 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
228 pcd->pane.pane_event = bar_pane_comment_event;
229 pcd->pane.pane_write_config = bar_pane_comment_write_config;
230 pcd->pane.title = bar_pane_expander_title(title);
231 pcd->pane.id = g_strdup(id);
232 pcd->pane.type = PANE_COMMENT;
234 pcd->pane.expanded = expanded;
236 pcd->key = g_strdup(key);
237 pcd->height = height;
239 scrolled = gtk_scrolled_window_new(NULL, NULL);
241 pcd->widget = scrolled;
242 g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
243 g_signal_connect(G_OBJECT(pcd->widget), "destroy",
244 G_CALLBACK(bar_pane_comment_destroy), pcd);
246 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
247 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
248 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
250 gtk_widget_set_size_request(pcd->widget, -1, height);
251 gtk_widget_show(scrolled);
253 pcd->comment_view = gtk_text_view_new();
254 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
255 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
256 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
257 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
258 gtk_widget_show(pcd->comment_view);
260 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
261 g_signal_connect(G_OBJECT(buffer), "changed",
262 G_CALLBACK(bar_pane_comment_changed), pcd);
265 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
270 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
273 gchar *key = g_strdup(COMMENT_KEY);
274 gboolean expanded = TRUE;
276 gchar *id = g_strdup("comment");
279 while (*attribute_names)
281 const gchar *option = *attribute_names++;
282 const gchar *value = *attribute_values++;
284 if (READ_CHAR_FULL("title", title)) continue;
285 if (READ_CHAR_FULL("key", key)) continue;
286 if (READ_BOOL_FULL("expanded", expanded)) continue;
287 if (READ_INT_FULL("height", height)) continue;
288 if (READ_CHAR_FULL("id", id)) continue;
291 log_printf("unknown attribute %s = %s\n", option, value);
294 bar_pane_translate_title(PANE_COMMENT, id, &title);
295 ret = bar_pane_comment_new(id, title, key, expanded, height);
302 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
304 PaneCommentData *pcd;
306 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
311 while (*attribute_names)
313 const gchar *option = *attribute_names++;
314 const gchar *value = *attribute_values++;
316 if (READ_CHAR_FULL("title", title)) continue;
317 if (READ_CHAR_FULL("key", pcd->key)) continue;
318 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
319 if (READ_INT_FULL("height", pcd->height)) continue;
320 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
323 log_printf("unknown attribute %s = %s\n", option, value);
328 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
329 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
332 gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
333 bar_update_expander(pane);
334 bar_pane_comment_update(pcd);
337 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */