Fix up few compilation warnings.
[geeqie.git] / src / bar_comment.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
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!
11  */
12
13
14 #include "main.h"
15 #include "bar_comment.h"
16
17 #include "bar.h"
18 #include "metadata.h"
19 #include "filedata.h"
20 #include "ui_menu.h"
21 #include "ui_misc.h"
22
23 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data);
24
25 /*
26  *-------------------------------------------------------------------
27  * keyword / comment utils
28  *-------------------------------------------------------------------
29  */
30
31
32
33 typedef struct _PaneCommentData PaneCommentData;
34 struct _PaneCommentData
35 {
36         PaneData pane;
37         GtkWidget *widget;
38         GtkWidget *comment_view;
39         FileData *fd;
40         gchar *key;
41 };
42
43
44 static void bar_pane_comment_write(PaneCommentData *pcd)
45 {
46         gchar *comment;
47
48         if (!pcd->fd) return;
49
50         comment = text_widget_text_pull(pcd->comment_view);
51
52         metadata_write_string(pcd->fd, pcd->key, comment);
53         g_free(comment);
54 }
55
56
57 static void bar_pane_comment_update(PaneCommentData *pcd)
58 {
59         gchar *comment = NULL;
60         GtkTextBuffer *comment_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(pcd->comment_view));
61
62         g_signal_handlers_block_by_func(comment_buffer, bar_pane_comment_changed, pcd);
63
64         comment = metadata_read_string(pcd->fd, pcd->key, METADATA_PLAIN);
65         gtk_text_buffer_set_text(comment_buffer,
66                                  (comment) ? comment : "", -1);
67         g_free(comment);
68         
69         g_signal_handlers_unblock_by_func(comment_buffer, bar_pane_comment_changed, pcd);
70
71         gtk_widget_set_sensitive(pcd->comment_view, (pcd->fd != NULL));
72 }
73
74 static void bar_pane_comment_set_selection(PaneCommentData *pcd, gboolean append)
75 {
76         GList *list = NULL;
77         GList *work;
78         gchar *comment = NULL;
79
80         if (!pcd->pane.list_func) return;
81
82         comment = text_widget_text_pull(pcd->comment_view);
83
84         list = pcd->pane.list_func(pcd->pane.list_data);
85         work = list;
86         while (work)
87                 {
88                 FileData *fd = work->data;
89                 work = work->next;
90
91                 if (append)
92                         {
93                         metadata_append_string(fd, pcd->key, comment);
94                         }
95                 else
96                         {
97                         metadata_write_string(fd, pcd->key, comment);
98                         }
99                 }
100
101         filelist_free(list);
102         g_free(comment);
103 }
104
105 static void bar_pane_comment_sel_add_cb(GtkWidget *button, gpointer data)
106 {
107         PaneCommentData *pcd = data;
108
109         bar_pane_comment_set_selection(pcd, TRUE);
110 }
111
112 static void bar_pane_comment_sel_replace_cb(GtkWidget *button, gpointer data)
113 {
114         PaneCommentData *pcd = data;
115
116         bar_pane_comment_set_selection(pcd, FALSE);
117 }
118
119
120 static void bar_pane_comment_set_fd(GtkWidget *bar, FileData *fd)
121 {
122         PaneCommentData *pcd;
123
124         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
125         if (!pcd) return;
126
127         file_data_unref(pcd->fd);
128         pcd->fd = file_data_ref(fd);
129
130         bar_pane_comment_update(pcd);
131 }
132
133 gint bar_pane_comment_event(GtkWidget *bar, GdkEvent *event)
134 {
135         PaneCommentData *pcd;
136
137         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
138         if (!pcd) return FALSE;
139
140         if (GTK_WIDGET_HAS_FOCUS(pcd->comment_view)) return gtk_widget_event(pcd->comment_view, event);
141
142         return FALSE;
143 }
144
145 static void bar_pane_comment_notify_cb(FileData *fd, NotifyType type, gpointer data)
146 {
147         PaneCommentData *pcd = data;
148         if (fd == pcd->fd) bar_pane_comment_update(pcd);
149 }
150
151 static void bar_pane_comment_changed(GtkTextBuffer *buffer, gpointer data)
152 {
153         PaneCommentData *pcd = data;
154
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);
158 }
159
160
161 static void bar_pane_comment_populate_popup(GtkTextView *textview, GtkMenu *menu, gpointer data)
162 {
163         PaneCommentData *pcd = data;
164
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);
168 }
169
170
171 static void bar_pane_comment_close(GtkWidget *bar)
172 {
173         PaneCommentData *pcd;
174
175         pcd = g_object_get_data(G_OBJECT(bar), "pane_data");
176         if (!pcd) return;
177
178         gtk_widget_destroy(pcd->comment_view);
179 }
180
181 static void bar_pane_comment_destroy(GtkWidget *widget, gpointer data)
182 {
183         PaneCommentData *pcd = data;
184
185         file_data_unregister_notify_func(bar_pane_comment_notify_cb, pcd);
186
187         file_data_unref(pcd->fd);
188         g_free(pcd->pane.title);
189         g_free(pcd->key);
190         
191
192         g_free(pcd);
193 }
194
195
196 GtkWidget *bar_pane_comment_new(const gchar *title, const gchar *key, gint height)
197 {
198         PaneCommentData *pcd;
199         GtkWidget *scrolled;
200         GtkTextBuffer *buffer;
201
202         pcd = g_new0(PaneCommentData, 1);
203         
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);
207         
208         pcd->key = g_strdup(key);
209
210         scrolled = gtk_scrolled_window_new(NULL, NULL);
211         
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);
216         
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);
220
221         gtk_widget_set_size_request(scrolled, -1, height);
222         gtk_widget_show(scrolled);
223
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);
229
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);
233
234
235         file_data_register_notify_func(bar_pane_comment_notify_cb, pcd, NOTIFY_PRIORITY_LOW);
236
237         return pcd->widget;
238 }
239
240 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */