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"
24 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
27 *-------------------------------------------------------------------
28 * keyword / comment utils
29 *-------------------------------------------------------------------
34 typedef struct _PaneCommentData PaneCommentData;
35 struct _PaneCommentData
39 GtkWidget *comment_view;
46 static void bar_pane_comment_write(PaneCommentData *pcd)
52 comment = text_widget_text_pull(pcd->comment_view);
54 metadata_write_string(pcd->fd, pcd->key, comment);
59 static void bar_pane_comment_update(PaneCommentData *pcd)
61 gchar *comment = NULL;
62 GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
64 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
66 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
67 gtk_text_buffer_set_text(comment_buffer,
68 (comment) ? comment : "", -1);
71 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
73 gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
76 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
80 gchar *comment = NULL;
82 if (!pcd->pane.list_func) return;
84 comment = text_widget_text_pull(pcd->comment_view);
86 list = pcd->pane.list_func(pcd->pane.list_data);
90 FileData *fd = work->data;
95 metadata_append_string(fd, pcd->key, comment);
99 metadata_write_string(fd, pcd->key, comment);
107 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
109 PaneCommentData *pcd = data;
111 bar_pane_comment_set_selection(pcd, TRUE);
114 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
116 PaneCommentData *pcd = data;
118 bar_pane_comment_set_selection(pcd, FALSE);
122 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
124 PaneCommentData *pcd;
126 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
129 file_data_unref(pcd->fd);
130 pcd->fd = file_data_ref(fd);
132 bar_pane_comment_update(pcd);
135 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
137 PaneCommentData *pcd;
139 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
140 if (!pcd) return FALSE;
142 if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
147 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
149 PaneCommentData *pcd;
151 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
154 WRITE_STRING("<pane_comment\n");
156 write_char_option(outstr, indent, "pane.title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
157 WRITE_BOOL(*pcd, pane.expanded);
158 WRITE_CHAR(*pcd, key);
159 WRITE_INT(*pcd, height);
161 WRITE_STRING("/>\n");
164 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
166 PaneCommentData *pcd = data;
167 if (fd == pcd->fd) bar_pane_comment_update(pcd);
170 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
172 PaneCommentData *pcd = data;
174 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
175 bar_pane_comment_write(pcd);
176 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
180 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
182 PaneCommentData *pcd = data;
184 menu_item_add_divider(GTK_WIDGET(menu));
185 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
186 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);
190 static void bar_pane_comment_close(GtkWidget *bar)
192 PaneCommentData *pcd;
194 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
197 gtk_widget_destroy(pcd->comment_view);
200 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
202 PaneCommentData *pcd = data;
204 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
206 file_data_unref(pcd->fd);
214 GtkWidget *bar_pane_comment_new(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 = gtk_label_new(title);
226 pcd->pane.expanded = expanded;
228 pcd->key = g_strdup(key);
229 pcd->height = height;
231 scrolled = gtk_scrolled_window_new(NULL, NULL);
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);
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);
242 gtk_widget_set_size_request(scrolled, -1, height);
243 gtk_widget_show(scrolled);
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);
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);
257 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
262 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
264 gchar *title = g_strdup(_("NoName"));
265 gchar *key = g_strdup(COMMENT_KEY);
266 gboolean expanded = TRUE;
269 while (*attribute_names)
271 const gchar *option = *attribute_names++;
272 const gchar *value = *attribute_values++;
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;
280 DEBUG_1("unknown attribute %s = %s", option, value);
283 return bar_pane_comment_new(title, key, expanded, height);
286 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */