730adba30e079ba5a5e1170673aa4b319a91375d
[geeqie.git] / src / bar-comment.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "bar-comment.h"
23
24 #include <config.h>
25
26 #include "bar.h"
27 #include "compat.h"
28 #include "debug.h"
29 #include "intl.h"
30 #include "main-defines.h"
31 #include "metadata.h"
32 #include "filedata.h"
33 #include "ui-menu.h"
34 #include "ui-misc.h"
35 #include "rcfile.h"
36 #include "layout.h"
37
38 #ifdef HAVE_SPELL
39 #include <gspell/gspell.h>
40 #endif
41
42 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
43
44 /*
45  *-------------------------------------------------------------------
46  * keyword / comment utils
47  *-------------------------------------------------------------------
48  */
49
50
51
52 struct PaneCommentData
53 {
54         PaneData pane;
55         GtkWidget *widget;
56         GtkWidget *comment_view;
57         FileData *fd;
58         gchar *key;
59         gint height;
60 #ifdef HAVE_SPELL
61         GspellTextView *gspell_view;
62 #endif
63 };
64
65
66 static void bar_pane_comment_write(PaneCommentData *pcd)
67 {
68         gchar *comment;
69
70         if (!pcd->fd) return;
71
72         comment = text_widget_text_pull(pcd->comment_view);
73
74         metadata_write_string(pcd->fd, pcd->key, comment);
75         g_free(comment);
76 }
77
78
79 static void bar_pane_comment_update(PaneCommentData *pcd)
80 {
81         gchar *comment = nullptr;
82         gchar *orig_comment = nullptr;
83         const gchar *comment_not_null;
84         gshort rating;
85         GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
86
87         orig_comment = text_widget_text_pull(pcd->comment_view);
88         if (g_strcmp0(pcd->key, "Xmp.xmp.Rating") == 0)
89                 {
90                 rating = metadata_read_int(pcd->fd, pcd->key, 0);
91                 comment = g_strdup_printf("%d", rating);
92                 }
93         else
94                 {
95                 comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
96                 }
97         comment_not_null = (comment) ? comment : "";
98
99         if (strcmp(orig_comment, comment_not_null) != 0)
100                 {
101                 g_signal_handlers_block_by_func(comment_buffer, (gpointer)bar_pane_comment_changed, pcd);
102                 gtk_text_buffer_set_text(comment_buffer, comment_not_null, -1);
103                 g_signal_handlers_unblock_by_func(comment_buffer, (gpointer)bar_pane_comment_changed, pcd);
104                 }
105         g_free(comment);
106         g_free(orig_comment);
107
108         gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != nullptr));
109 }
110
111 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
112 {
113         GList *list = nullptr;
114         GList *work;
115         gchar *comment = nullptr;
116
117         comment = text_widget_text_pull(pcd->comment_view);
118
119         list = layout_selection_list(pcd->pane.lw);
120         list = file_data_process_groups_in_selection(list, FALSE, nullptr);
121
122         work = list;
123         while (work)
124                 {
125                 auto fd = static_cast<FileData *>(work->data);
126                 work = work->next;
127                 if (fd == pcd->fd) continue;
128
129                 if (append)
130                         {
131                         metadata_append_string(fd, pcd->key, comment);
132                         }
133                 else
134                         {
135                         metadata_write_string(fd, pcd->key, comment);
136                         }
137                 }
138
139         filelist_free(list);
140         g_free(comment);
141 }
142
143 static void bar_pane_comment_sel_add_cb(GtkWidget *, gpointer data)
144 {
145         auto pcd = static_cast<PaneCommentData *>(data);
146
147         bar_pane_comment_set_selection(pcd, TRUE);
148 }
149
150 static void bar_pane_comment_sel_replace_cb(GtkWidget *, gpointer data)
151 {
152         auto pcd = static_cast<PaneCommentData *>(data);
153
154         bar_pane_comment_set_selection(pcd, FALSE);
155 }
156
157
158 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
159 {
160         PaneCommentData *pcd;
161
162         pcd = static_cast<PaneCommentData *>(g_object_get_data(G_OBJECT(bar), "pane_data"));
163         if (!pcd) return;
164
165         file_data_unref(pcd->fd);
166         pcd->fd = file_data_ref(fd);
167
168         bar_pane_comment_update(pcd);
169 }
170
171 static gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
172 {
173         PaneCommentData *pcd;
174
175         pcd = static_cast<PaneCommentData *>(g_object_get_data(G_OBJECT(bar), "pane_data"));
176         if (!pcd) return FALSE;
177
178         if (gtk_widget_has_focus(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
179
180         return FALSE;
181 }
182
183 static void bar_pane_comment_write_config(GtkWidget *pane, GString *outstr, gint indent)
184 {
185         PaneCommentData *pcd;
186         gint w;
187         gint h;
188
189         pcd = static_cast<PaneCommentData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
190         if (!pcd) return;
191
192         gtk_widget_get_size_request(GTK_WIDGET(pane), &w, &h);
193
194         if (!g_strcmp0(pcd->pane.id, "title"))
195                 {
196                 pcd->height = h;
197                 }
198         if (!g_strcmp0(pcd->pane.id, "comment"))
199                 {
200                 pcd->height = h;
201                 }
202         if (!g_strcmp0(pcd->pane.id, "rating"))
203                 {
204                 pcd->height = h;
205                 }
206         if (!g_strcmp0(pcd->pane.id, "headline"))
207                 {
208                 pcd->height = h;
209                 }
210
211         WRITE_NL(); WRITE_STRING("<pane_comment ");
212         write_char_option(outstr, indent, "id", pcd->pane.id);
213         write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(pcd->pane.title)));
214         WRITE_BOOL(pcd->pane, expanded);
215         WRITE_CHAR(*pcd, key);
216         WRITE_INT(*pcd, height);
217         WRITE_STRING("/>");
218 }
219
220 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
221 {
222         auto pcd = static_cast<PaneCommentData *>(data);
223         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_METADATA)) && fd == pcd->fd)
224                 {
225                 DEBUG_1("Notify pane_comment: %s %04x", fd->path, type);
226
227                 bar_pane_comment_update(pcd);
228                 }
229 }
230
231 static void bar_pane_comment_changed(GtkTextBuffer *, gpointer data)
232 {
233         auto pcd = static_cast<PaneCommentData *>(data);
234
235         bar_pane_comment_write(pcd);
236 }
237
238
239 static void bar_pane_comment_populate_popup(GtkTextView *, GtkMenu *menu, gpointer data)
240 {
241         auto pcd = static_cast<PaneCommentData *>(data);
242
243         menu_item_add_divider(GTK_WIDGET(menu));
244         menu_item_add_icon(GTK_WIDGET(menu), _("Add text to selected files"), GQ_ICON_ADD, G_CALLBACK(bar_pane_comment_sel_add_cb), pcd);
245         menu_item_add_icon(GTK_WIDGET(menu), _("Replace existing text in selected files"), GQ_ICON_REPLACE, G_CALLBACK(bar_pane_comment_sel_replace_cb), data);
246 }
247
248 static void bar_pane_comment_destroy(GtkWidget *, gpointer data)
249 {
250         auto pcd = static_cast<PaneCommentData *>(data);
251
252         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
253 #ifdef HAVE_SPELL
254         g_object_unref(pcd->gspell_view);
255 #endif
256         file_data_unref(pcd->fd);
257         g_free(pcd->key);
258
259         g_free(pcd->pane.id);
260
261         g_free(pcd);
262 }
263
264
265 static GtkWidget *bar_pane_comment_new(const gchar *id, const gchar *title, const gchar *key, gboolean expanded, gint height)
266 {
267         PaneCommentData *pcd;
268         GtkWidget *scrolled;
269         GtkTextBuffer *buffer;
270
271         pcd = g_new0(PaneCommentData, 1);
272
273         pcd->pane.pane_set_fd = bar_pane_comment_set_fd;
274         pcd->pane.pane_event = bar_pane_comment_event;
275         pcd->pane.pane_write_config = bar_pane_comment_write_config;
276         pcd->pane.title = bar_pane_expander_title(title);
277         pcd->pane.id = g_strdup(id);
278         pcd->pane.type = PANE_COMMENT;
279 #ifdef HAVE_SPELL
280         pcd->gspell_view = nullptr;
281 #endif
282         pcd->pane.expanded = expanded;
283
284         pcd->key = g_strdup(key);
285         pcd->height = height;
286
287         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
288
289         pcd->widget = scrolled;
290         g_object_set_data(G_OBJECT(pcd->widget), "pane_data", pcd);
291         g_signal_connect(G_OBJECT(pcd->widget), "destroy",
292                          G_CALLBACK(bar_pane_comment_destroy), pcd);
293
294         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
295         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
296                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
297
298         gtk_widget_set_size_request(pcd->widget, -1, height);
299         gtk_widget_show(scrolled);
300
301         pcd->comment_view = gtk_text_view_new();
302         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(pcd->comment_view), GTK_WRAP_WORD);
303         gq_gtk_container_add(GTK_WIDGET(scrolled), pcd->comment_view);
304         g_signal_connect(G_OBJECT(pcd->comment_view), "populate-popup",
305                          G_CALLBACK(bar_pane_comment_populate_popup), pcd);
306         gtk_widget_show(pcd->comment_view);
307
308 #ifdef HAVE_SPELL
309         if (g_strcmp0(key, "Xmp.xmp.Rating") != 0)
310                 {
311                 if (options->metadata.check_spelling)
312                         {
313                         pcd->gspell_view = gspell_text_view_get_from_gtk_text_view(GTK_TEXT_VIEW(pcd->comment_view));
314                         gspell_text_view_basic_setup(pcd->gspell_view);
315                         }
316         }
317 #endif
318
319         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
320         g_signal_connect(G_OBJECT(buffer), "changed",
321                          G_CALLBACK(bar_pane_comment_changed), pcd);
322
323
324         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
325
326         return pcd->widget;
327 }
328
329 GtkWidget *bar_pane_comment_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
330 {
331         gchar *title = nullptr;
332         gchar *key = g_strdup(COMMENT_KEY);
333         gboolean expanded = TRUE;
334         gint height = 50;
335         gchar *id = g_strdup("comment");
336         GtkWidget *ret;
337
338         while (*attribute_names)
339                 {
340                 const gchar *option = *attribute_names++;
341                 const gchar *value = *attribute_values++;
342
343                 if (READ_CHAR_FULL("title", title)) continue;
344                 if (READ_CHAR_FULL("key", key)) continue;
345                 if (READ_BOOL_FULL("expanded", expanded)) continue;
346                 if (READ_INT_FULL("height", height)) continue;
347                 if (READ_CHAR_FULL("id", id)) continue;
348
349
350                 log_printf("unknown attribute %s = %s\n", option, value);
351                 }
352
353         if (!g_strcmp0(id, "title"))
354                 {
355                 options->info_title.height = height;
356                 }
357         if (!g_strcmp0(id, "comment"))
358                 {
359                 options->info_comment.height = height;
360                 }
361         if (!g_strcmp0(id, "rating"))
362                 {
363                 options->info_rating.height = height;
364                 }
365         if (!g_strcmp0(id, "headline"))
366                 {
367                 options->info_headline.height = height;
368                 }
369
370         bar_pane_translate_title(PANE_COMMENT, id, &title);
371         ret = bar_pane_comment_new(id, title, key, expanded, height);
372         g_free(title);
373         g_free(key);
374         g_free(id);
375         return ret;
376 }
377
378 void bar_pane_comment_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
379 {
380         PaneCommentData *pcd;
381
382         pcd = static_cast<PaneCommentData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
383         if (!pcd) return;
384
385         gchar *title = nullptr;
386
387         while (*attribute_names)
388                 {
389                 const gchar *option = *attribute_names++;
390                 const gchar *value = *attribute_values++;
391
392                 if (READ_CHAR_FULL("title", title)) continue;
393                 if (READ_CHAR_FULL("key", pcd->key)) continue;
394                 if (READ_BOOL_FULL("expanded", pcd->pane.expanded)) continue;
395                 if (READ_INT_FULL("height", pcd->height)) continue;
396                 if (READ_CHAR_FULL("id", pcd->pane.id)) continue;
397
398
399                 log_printf("unknown attribute %s = %s\n", option, value);
400                 }
401
402         if (title)
403                 {
404                 bar_pane_translate_title(PANE_COMMENT, pcd->pane.id, &title);
405                 gtk_label_set_text(GTK_LABEL(pcd->pane.title), title);
406                 g_free(title);
407                 }
408         gtk_widget_set_size_request(pcd->widget, -1, pcd->height);
409         bar_update_expander(pane);
410         bar_pane_comment_update(pcd);
411 }
412
413 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */