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"
23 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
26 *-------------------------------------------------------------------
27 * keyword / comment utils
28 *-------------------------------------------------------------------
33 typedef struct _PaneCommentData PaneCommentData;
34 struct _PaneCommentData
38 GtkWidget *comment_view;
44 static void bar_pane_comment_write(PaneCommentData *pcd)
50 comment = text_widget_text_pull(pcd->comment_view);
52 metadata_write_string(pcd->fd, pcd->key, comment);
57 static void bar_pane_comment_update(PaneCommentData *pcd)
59 gchar *comment = NULL;
60 GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
62 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
64 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
65 gtk_text_buffer_set_text(comment_buffer,
66 (comment) ? comment : "", -1);
69 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
71 gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
74 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
78 gchar *comment = NULL;
80 if (!pcd->pane.list_func) return;
82 comment = text_widget_text_pull(pcd->comment_view);
84 list = pcd->pane.list_func(pcd->pane.list_data);
88 FileData *fd = work->data;
93 metadata_append_string(fd, pcd->key, comment);
97 metadata_write_string(fd, pcd->key, comment);
105 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
107 PaneCommentData *pcd = data;
109 bar_pane_comment_set_selection(pcd, TRUE);
112 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
114 PaneCommentData *pcd = data;
116 bar_pane_comment_set_selection(pcd, FALSE);
120 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
122 PaneCommentData *pcd;
124 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
127 file_data_unref(pcd->fd);
128 pcd->fd = file_data_ref(fd);
130 bar_pane_comment_update(pcd);
133 gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
135 PaneCommentData *pcd;
137 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
138 if (!pcd) return FALSE;
140 if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
145 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
147 PaneCommentData *pcd = data;
148 if (fd == pcd->fd) bar_pane_comment_update(pcd);
151 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
153 PaneCommentData *pcd = data;
155 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
156 bar_pane_comment_write(pcd);
157 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
161 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
163 PaneCommentData *pcd = data;
165 menu_item_add_divider(GTK_WIDGET(menu));
166 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
167 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);
171 static void bar_pane_comment_close(GtkWidget *bar)
173 PaneCommentData *pcd;
175 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
178 gtk_widget_destroy(pcd->comment_view);
181 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
183 PaneCommentData *pcd = data;
185 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
187 file_data_unref(pcd->fd);
188 g_free(pcd->pane.title);
196 GtkWidget *bar_pane_comment_new(const gchar *title, const gchar *key, gint height)
198 PaneCommentData *pcd;
200 GtkTextBuffer *buffer;
202 pcd = g_new0(PaneCommentData, 1);
204 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
205 pcd->pane.pane_event = bar_pane_comment_event;
206 pcd->pane.title = g_strdup(title);
208 pcd->key = g_strdup(key);
210 scrolled = gtk_scrolled_window_new(NULL, NULL);
212 pcd->widget = scrolled;
213 g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
214 g_signal_connect(G_OBJECT(pcd->widget), "destroy",
215 G_CALLBACK(bar_pane_comment_destroy), pcd);
217 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
218 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
219 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
221 gtk_widget_set_size_request(scrolled, -1, height);
222 gtk_widget_show(scrolled);
224 pcd->comment_view = gtk_text_view_new();
225 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
226 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
227 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
228 gtk_widget_show(pcd->comment_view);
230 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
231 g_signal_connect(G_OBJECT(buffer), "changed",
232 G_CALLBACK(bar_pane_comment_changed), pcd);
235 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
240 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */