Debian Codegraph @

src/af/xap/gtk/xap_UnixDlg_ClipArt.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiWord
4 * Copyright (C) 2006 Rob Staudinger <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA.
20 */
21
22 #include <stdlib.h>
23 #include <time.h>
24
25 #include "ut_string.h"
26 #include "ut_assert.h"
27 #include "ut_debugmsg.h"
28
29 #include "xap_UnixDialogHelper.h"
30 #include "xap_Gtk2Compat.h"
31
32 #include "xap_App.h"
33 #include "xap_UnixApp.h"
34 #include "xap_Frame.h"
35
36 #include "xap_Strings.h"
37 #include "xap_Dialog_Id.h"
38 #include "xap_Dlg_ClipArt.h"
39 #include "xap_UnixDlg_ClipArt.h"
40
41 enum {
42 COL_PATH,
43 COL_DISPLAY_NAME,
44 COL_PIXBUF,
45 NUM_COLS
46 };
47
48 static gint clipartCount = 0;
49
50 /**
51 * Create list store for the icon view.
52 */
53 static GtkListStore *
54 create_store ()
55 {
56 GtkListStore *store;
57
58 store = gtk_list_store_new (NUM_COLS,
59 G_TYPE_STRING,
60 G_TYPE_STRING,
61 GDK_TYPE_PIXBUF);
62
63 return store;
64 }
65
66 /**
67 * Fill list store.
68 */
69 static gboolean
70 fill_store (XAP_UnixDialog_ClipArt *self)
71 {
72 gboolean ret = self->fillStore();
73 if (!ret) {
74 GtkWidget *dlg = self->getDialog ();
75 const XAP_StringSet *pSS = XAP_App::getApp()->getStringSet ();
76 std::string s;
77 pSS->getValueUTF8(XAP_STRING_ID_DLG_CLIPART_Error, s);
78
79 GtkWidget *err = gtk_message_dialog_new (GTK_WINDOW (dlg), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", s.c_str());
80 gtk_dialog_run (GTK_DIALOG (err));
81 gtk_widget_destroy (err); err = NULL;
82
83 gtk_dialog_response(GTK_DIALOG(dlg), GTK_RESPONSE_CANCEL);
84 }
85 return FALSE;
86 }
87
88 /**
89 * Clipart clicked handler.
90 */
91 static void
92 item_activated (GtkIconView * /*icon_view*/,
93 GtkTreePath * /*tree_path*/,
94 XAP_UnixDialog_ClipArt *self)
95 {
96 self->onItemActivated();
97 }
98
99 /**
100 *
101 */
102 XAP_Dialog * XAP_UnixDialog_ClipArt::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id id)
103 {
104 XAP_UnixDialog_ClipArt * p = new XAP_UnixDialog_ClipArt(pFactory,id);
105 return p;
106 }
107
108 /**
109 *
110 */
111 XAP_UnixDialog_ClipArt::XAP_UnixDialog_ClipArt(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id)
112 : XAP_Dialog_ClipArt(pDlgFactory, id)
113 {}
114
115 /**
116 *
117 */
118 XAP_UnixDialog_ClipArt::~XAP_UnixDialog_ClipArt()
119 {
120 this->dir_path = NULL;
121 this->progress = NULL;
122 this->icon_view = NULL;
123 this->store = NULL;
124 }
125
126 static
127 void _free_path(GtkTreePath *path, gpointer)
128 {
129 gtk_tree_path_free(path);
130 }
131 /**
132 *
133 */
134 void XAP_UnixDialog_ClipArt::runModal(XAP_Frame * pFrame)
135 {
136 GtkWidget *scroll;
137 GList *list;
138 GError *error;
139
140 std::string s;
141 const XAP_StringSet *pSS = m_pApp->getStringSet ();
142
143 UT_ASSERT(pFrame);
144
145 this->dlg = abiDialogNew ("clipart dialog", TRUE, pSS->getValue (XAP_STRING_ID_DLG_CLIPART_Title));
146 #if !GTK_CHECK_VERSION(3,0,0)
147 gtk_dialog_set_has_separator(GTK_DIALOG(this->dlg), FALSE);
148 #endif
149 gtk_window_set_default_size (GTK_WINDOW (this->dlg), 640, 480);
150 abiAddStockButton(GTK_DIALOG(this->dlg), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
151 abiAddStockButton(GTK_DIALOG(this->dlg), GTK_STOCK_OK, GTK_RESPONSE_OK);
152 connectFocus(GTK_WIDGET(this->dlg), pFrame);
153
154 GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
155 gtk_box_pack_start(GTK_BOX (gtk_dialog_get_content_area(GTK_DIALOG(this->dlg))), vbox, TRUE,TRUE,0);
156
157 pSS->getValueUTF8(XAP_STRING_ID_DLG_CLIPART_Loading, s);
158 this->progress = gtk_progress_bar_new ();
159 gtk_progress_bar_set_text (GTK_PROGRESS_BAR (this->progress), s.c_str());
160 gtk_box_pack_start (GTK_BOX (vbox), this->progress, FALSE, FALSE, 0);
161
162 scroll = gtk_scrolled_window_new (NULL, NULL);
163 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), GTK_SHADOW_ETCHED_IN);
164 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
165 GTK_POLICY_AUTOMATIC,
166 GTK_POLICY_AUTOMATIC);
167 gtk_box_pack_start (GTK_BOX (vbox), scroll, TRUE, TRUE, 0);
168
169 this->store = create_store ();
170
171 this->icon_view = gtk_icon_view_new ();
172 gtk_icon_view_set_text_column (GTK_ICON_VIEW (this->icon_view), COL_DISPLAY_NAME);
173 gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (this->icon_view), COL_PIXBUF);
174 gtk_icon_view_set_column_spacing (GTK_ICON_VIEW (this->icon_view), 0);
175 gtk_icon_view_set_row_spacing (GTK_ICON_VIEW (this->icon_view), 0);
176 gtk_icon_view_set_columns (GTK_ICON_VIEW (this->icon_view), -1);
177 gtk_container_add (GTK_CONTAINER (scroll), this->icon_view);
178 g_signal_connect (this->icon_view, "item_activated", G_CALLBACK (item_activated), (gpointer) this);
179 gtk_icon_view_set_model (GTK_ICON_VIEW (this->icon_view),
180 GTK_TREE_MODEL (this->store));
181 g_object_unref (G_OBJECT (this->store));
182
183 gtk_widget_show_all (this->dlg);
184
185 /* Dom says we just use that dir for now and hope for someone to build an openclipart client */
186 this->dir_path = getInitialDir ();
187 g_idle_add ((GSourceFunc) fill_store, this);
188
189 switch (abiRunModalDialog(GTK_DIALOG(this->dlg), pFrame, this, GTK_RESPONSE_CANCEL, false)) {
190 case GTK_RESPONSE_OK:
191 list = gtk_icon_view_get_selected_items (GTK_ICON_VIEW (this->icon_view));
192 if (list && list->data) {
193 gchar *graphic = NULL;
194 gchar *graphicUri = NULL;
195 GtkTreePath *treePath;
196 GtkTreeIter iter;
197 treePath = (GtkTreePath *) list->data;
198 gtk_tree_model_get_iter (GTK_TREE_MODEL (this->store), &iter, treePath);
199 gtk_tree_model_get (GTK_TREE_MODEL (this->store), &iter, COL_PATH, &graphic, -1);
200 if (graphic) {
201 error = NULL;
202 graphicUri = g_filename_to_uri (graphic, NULL, &error);
203 setGraphicName (graphicUri);
204 g_free (graphic);
205 g_free (graphicUri);
206 setAnswer (XAP_Dialog_ClipArt::a_OK);
207 }
208 else {
209 setAnswer (XAP_Dialog_ClipArt::a_CANCEL);
210 }
211 g_list_foreach (list, (GFunc)_free_path, NULL);
212 g_list_free (list);
213 }
214 break;
215 default:
216 break;
217 }
218
219 abiDestroyWidget(this->dlg);
220 }
221
222 /**
223 * Fill list store updating progress bar as we go.
224 */
225 gboolean XAP_UnixDialog_ClipArt::fillStore()
226 {
227 GDir *dir;
228 const gchar *name;
229 GdkPixbuf *pixbuf;
230 GError *error;
231 GtkTreeIter iter;
232 gint _count;
233
234 if (!g_file_test (this->dir_path, G_FILE_TEST_IS_DIR)) {
235 return FALSE;
236 }
237
238 error = NULL;
239 dir = g_dir_open (this->dir_path, 0, &error);
240 if (error) {
241 g_warning ("%s", error->message);
242 g_error_free (error);
243 return FALSE;
244 }
245
246 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (this->progress), 0.);
247 _count = 0;
248 name = g_dir_read_name (dir);
249 while (name != NULL) {
250
251 gchar *file_path, *display_name;
252
253 /* We ignore hidden files that start with a '.' */
254 if (name[0] == '.') {
255 goto next;
256 }
257
258 file_path = g_build_filename (this->dir_path, name, NULL);
259 if (g_file_test (file_path, G_FILE_TEST_IS_DIR)) {
260 goto next;
261 }
262
263 display_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
264 error = NULL;
265 pixbuf = gdk_pixbuf_new_from_file_at_size (file_path, 48, 48, &error);
266 if (error) {
267 g_warning ("%s", error->message);
268 g_error_free (error);
269 goto next;
270 }
271
272 gtk_list_store_append (this->store, &iter);
273 gtk_list_store_set (this->store, &iter,
274 COL_PATH, file_path,
275 COL_DISPLAY_NAME, display_name,
276 COL_PIXBUF, pixbuf,
277 -1);
278 g_free (file_path); file_path = NULL;
279 g_free (display_name); display_name = NULL;
280 g_object_unref (G_OBJECT (pixbuf)); pixbuf = NULL;
281
282 if (clipartCount) {
283 gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (this->progress),
284 _count / clipartCount * 100.);
285 }
286 else {
287 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (this->progress));
288 }
289 _count++;
290 if (_count % 10 == 0) {
291 gtk_main_iteration_do (FALSE);
292 }
293 next:
294 name = g_dir_read_name (dir);
295 }
296 clipartCount = _count;
297
298 gtk_widget_hide (this->progress);
299
300 return TRUE;
301 }
302
303 /**
304 * Clipart clicked handler.
305 */
306 void XAP_UnixDialog_ClipArt::onItemActivated()
307 {
308 gtk_dialog_response(GTK_DIALOG(this->dlg), GTK_RESPONSE_OK);
309 }