2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "bar_comment.h"
33 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
36 *-------------------------------------------------------------------
37 * keyword / comment utils
38 *-------------------------------------------------------------------
43 typedef struct _PaneCommentData PaneCommentData;
44 struct _PaneCommentData
48 GtkWidget *comment_view;
55 static void bar_pane_comment_write(PaneCommentData *pcd)
61 comment = text_widget_text_pull(pcd->comment_view);
63 metadata_write_string(pcd->fd, pcd->key, comment);
68 static void bar_pane_comment_update(PaneCommentData *pcd)
70 gchar *comment = NULL;
71 gchar *orig_comment = NULL;
72 gchar *comment_not_null;
73 GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
75 orig_comment = text_widget_text_pull(pcd->comment_view);
76 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
77 comment_not_null = (comment) ? comment : "";
79 if (strcmp(orig_comment, comment_not_null) != 0)
81 g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
82 gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
83 g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
88 gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
91 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
95 gchar *comment = NULL;
97 comment = text_widget_text_pull(pcd->comment_view);
99 list = layout_selection_list(pcd->pane.lw);
100 list = file_data_process_groups_in_selection(list, FALSE, NULL);
105 FileData *fd = work->data;
107 if (fd == pcd->fd) continue;
111 metadata_append_string(fd, pcd->key, comment);
115 metadata_write_string(fd, pcd->key, comment);
123 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
125 PaneCommentData *pcd = data;
127 bar_pane_comment_set_selection(pcd, TRUE);
130 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
132 PaneCommentData *pcd = data;
134 bar_pane_comment_set_selection(pcd, FALSE);
138 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
140 PaneCommentData *pcd;
142 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
145 file_data_unref(pcd->fd);
146 pcd->fd = file_data_ref(fd);
148 bar_pane_comment_update(pcd);
151 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
153 PaneCommentData *pcd;
155 pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
156 if (!pcd) return FALSE;
158 if (gtk_widget_has_focus(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
163 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
165 PaneCommentData *pcd;
167 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
170 if (!g_strcmp0(pcd->pane.id, "title"))
172 pcd->height = options->info_title.height;
174 if (!g_strcmp0(pcd->pane.id, "comment"))
176 pcd->height = options->info_comment.height;
179 WRITE_NL(); WRITE_STRING("<pane_comment ");
180 write_char_option(outstr, indent, "id", pcd->pane.id);
181 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
182 WRITE_BOOL(pcd->pane, expanded);
183 WRITE_CHAR(*pcd, key);
184 WRITE_INT(*pcd, height);
188 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
190 PaneCommentData *pcd = data;
191 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
193 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
195 bar_pane_comment_update(pcd);
199 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
201 PaneCommentData *pcd = data;
203 bar_pane_comment_write(pcd);
207 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
209 PaneCommentData *pcd = data;
211 menu_item_add_divider(GTK_WIDGET(menu));
212 menu_item_add_stock(GTK_WIDGET(menu), _("Add text to selected files"), GTK_STOCK_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
213 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);
216 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
218 PaneCommentData *pcd = data;
220 file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
222 file_data_unref(pcd->fd);
225 g_free(pcd->pane.id);
231 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
233 PaneCommentData *pcd;
235 GtkTextBuffer *buffer;
237 pcd = g_new0(PaneCommentData, 1);
239 pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
240 pcd->pane.pane_event = bar_pane_comment_event;
241 pcd->pane.pane_write_config = bar_pane_comment_write_config;
242 pcd->pane.title = bar_pane_expander_title(title);
243 pcd->pane.id = g_strdup(id);
244 pcd->pane.type = PANE_COMMENT;
246 pcd->pane.expanded = expanded;
248 pcd->key = g_strdup(key);
249 pcd->height = height;
251 scrolled = gtk_scrolled_window_new(NULL, NULL);
253 pcd->widget = scrolled;
254 g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
255 g_signal_connect(G_OBJECT(pcd->widget), "destroy",
256 G_CALLBACK(bar_pane_comment_destroy), pcd);
258 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
259 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
260 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
262 gtk_widget_set_size_request(pcd->widget, -1, height);
263 gtk_widget_show(scrolled);
265 pcd->comment_view = gtk_text_view_new();
266 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
267 gtk_container_add(GTK_CONTAINER(scrolled), pcd->comment_view);
268 g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
269 G_CALLBACK(bar_pane_comment_populate_popup), pcd);
270 gtk_widget_show(pcd->comment_view);
272 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
273 g_signal_connect(G_OBJECT(buffer), "changed",
274 G_CALLBACK(bar_pane_comment_changed), pcd);
277 file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
282 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
285 gchar *key = g_strdup(COMMENT_KEY);
286 gboolean expanded = TRUE;
288 gchar *id = g_strdup("comment");
291 while (*attribute_names)
293 const gchar *option = *attribute_names++;
294 const gchar *value = *attribute_values++;
296 if (READ_CHAR_FULL("title", title)) continue;
297 if (READ_CHAR_FULL("key", key)) continue;
298 if (READ_BOOL_FULL("expanded", expanded)) continue;
299 if (READ_INT_FULL("height", height)) continue;
300 if (READ_CHAR_FULL("id", id)) continue;
303 log_printf("unknown attribute %s = %s\n", option, value);
306 if (!g_strcmp0(id, "title"))
308 options->info_title.height = height;
310 if (!g_strcmp0(id, "comment"))
312 options->info_comment.height = height;
315 bar_pane_translate_title(PANE_COMMENT, id, &title);
316 ret = bar_pane_comment_new(id, title, key, expanded, height);
323 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
325 PaneCommentData *pcd;
327 pcd = g_object_get_data(G_OBJECT(pane), "pane_data");
332 while (*attribute_names)
334 const gchar *option = *attribute_names++;
335 const gchar *value = *attribute_values++;
337 if (READ_CHAR_FULL("title", title)) continue;
338 if (READ_CHAR_FULL("key", pcd->key)) continue;
339 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
340 if (READ_INT_FULL("height", pcd->height)) continue;
341 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
344 log_printf("unknown attribute %s = %s\n", option, value);
349 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
350 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
353 gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
354 bar_update_expander(pane);
355 bar_pane_comment_update(pcd);
358 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */