1 /*
2 * Authors:
3 * Ted Gould <ted@gould.cx>
4 *
5 * Copyright (C) 2007 Authors
6 *
7 * Released under GNU GPL, read the file 'COPYING' for more information
8 */
10 #include <config.h>
12 #include "gtkmm/messagedialog.h"
14 #include "execution-env.h"
15 #include "prefdialog.h"
16 #include "implementation/implementation.h"
18 #include "selection.h"
19 #include "effect.h"
20 #include "document.h"
21 #include "ui/view/view.h"
22 #include "sp-namedview.h"
23 #include "desktop-handles.h"
25 #include "util/glib-list-iterators.h"
27 namespace Inkscape {
28 namespace Extension {
31 ExecutionEnv::ExecutionEnv (Effect * effect, Inkscape::UI::View::View * doc, Gtk::Widget * controls) :
32 _effect(effect),
33 _visibleDialog(NULL),
34 _prefsVisible(false),
35 _finished(false),
36 _humanWait(false),
37 _canceled(false),
38 _prefsChanged(false),
39 _doc(doc) {
41 SPDesktop *desktop = (SPDesktop *)_doc;
42 sp_namedview_document_from_window(desktop);
44 if (desktop != NULL) {
45 Inkscape::Util::GSListConstIterator<SPItem *> selected =
46 sp_desktop_selection(desktop)->itemList();
47 while ( selected != NULL ) {
48 Glib::ustring selected_id;
49 selected_id = SP_OBJECT_ID(*selected);
50 _selected.insert(_selected.end(), selected_id);
51 //std::cout << "Selected: " << selected_id << std::endl;
52 ++selected;
53 }
54 }
56 _mainloop = Glib::MainLoop::create(false);
58 if (controls != NULL) {
59 createPrefsDialog(controls);
60 } else {
61 createWorkingDialog();
62 }
64 return;
65 }
67 ExecutionEnv::~ExecutionEnv (void) {
68 if (_visibleDialog != NULL) {
69 delete _visibleDialog;
70 }
71 return;
72 }
74 void
75 ExecutionEnv::preferencesChange (void) {
76 //std::cout << "Preferences are a changin'" << std::endl;
77 _prefsChanged = true;
78 if (_humanWait) {
79 _mainloop->quit();
80 documentCancel();
81 _humanWait = false;
82 } else {
83 processingCancel();
84 documentCancel();
85 }
86 return;
87 }
89 void
90 ExecutionEnv::createPrefsDialog (Gtk::Widget * controls) {
91 if (_visibleDialog != NULL) {
92 delete _visibleDialog;
93 }
95 _visibleDialog = new PrefDialog(_effect->get_name(), _effect->get_help(), controls);
96 _visibleDialog->signal_response().connect(sigc::mem_fun(this, &ExecutionEnv::preferencesResponse));
97 _visibleDialog->show();
99 _prefsVisible = true;
100 return;
101 }
103 void
104 ExecutionEnv::createWorkingDialog (void) {
105 if (_visibleDialog != NULL) {
106 delete _visibleDialog;
107 }
109 gchar * dlgmessage = g_strdup_printf(_("'%s' working, please wait..."), _effect->get_name());
110 _visibleDialog = new Gtk::MessageDialog(dlgmessage,
111 false, // use markup
112 Gtk::MESSAGE_INFO,
113 Gtk::BUTTONS_CANCEL,
114 true); // modal
115 _visibleDialog->signal_response().connect(sigc::mem_fun(this, &ExecutionEnv::workingCanceled));
116 g_free(dlgmessage);
117 _visibleDialog->show();
119 _prefsVisible = false;
120 return;
121 }
123 void
124 ExecutionEnv::workingCanceled (const int resp) {
125 processingCancel();
126 documentCancel();
127 _finished = true;
128 return;
129 }
131 void
132 ExecutionEnv::preferencesResponse (const int resp) {
133 if (resp == Gtk::RESPONSE_OK) {
134 if (_humanWait) {
135 documentCommit();
136 _mainloop->quit();
137 _finished = true;
138 } else {
139 createWorkingDialog();
140 }
141 } else {
142 if (_humanWait) {
143 _mainloop->quit();
144 } else {
145 processingCancel();
146 }
147 documentCancel();
148 _finished = true;
149 }
150 return;
151 }
153 void
154 ExecutionEnv::processingComplete(void) {
155 //std::cout << "Processing Complete" << std::endl;
156 if (_prefsChanged) { return; } // do it all again
157 if (_prefsVisible) {
158 _humanWait = true;
159 } else {
160 documentCommit();
161 _finished = true;
162 }
163 return;
164 }
166 void
167 ExecutionEnv::processingCancel (void) {
168 _effect->get_imp()->cancelProcessing();
169 return;
170 }
172 void
173 ExecutionEnv::documentCancel (void) {
174 _canceled = true;
175 return;
176 }
178 void
179 ExecutionEnv::documentCommit (void) {
180 sp_document_done(_doc->doc(), SP_VERB_NONE, _(_effect->get_name()));
181 Effect::set_last_effect(_effect);
182 return;
183 }
185 void
186 ExecutionEnv::reselect (void) {
187 SPDocument * doc = _doc->doc();
189 SPDesktop *desktop = (SPDesktop *)_doc;
190 sp_namedview_document_from_window(desktop);
192 if (desktop == NULL) { return; }
194 Inkscape::Selection * selection = sp_desktop_selection(desktop);
196 for (std::list<Glib::ustring>::iterator i = _selected.begin(); i != _selected.end(); i++) {
197 selection->add(doc->getObjectById(i->c_str()));
198 }
200 return;
201 }
203 void
204 ExecutionEnv::run (void) {
205 while (!_finished) {
206 _canceled = false;
207 if (_humanWait) {
208 _mainloop->run();
209 } else {
210 _prefsChanged = false;
211 _effect->get_imp()->effect(_effect, _doc);
212 processingComplete();
213 }
214 if (_canceled) {
215 sp_document_cancel(_doc->doc());
216 reselect();
217 }
218 }
219 return;
220 }
224 } } /* namespace Inkscape, Extension */
228 /*
229 Local Variables:
230 mode:c++
231 c-file-style:"stroustrup"
232 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
233 indent-tabs-mode:nil
234 fill-column:99
235 End:
236 */
237 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :