1 /*
2 * Authors:
3 * Ted Gould <ted@gould.cx>
4 *
5 * Copyright (C) 2002-2005 Authors
6 *
7 * Released under GNU GPL, read the file 'COPYING' for more information
8 */
10 #include "inkscape-private.h"
11 #include "helper/action.h"
12 #include "document.h"
13 #include "prefdialog.h"
14 #include "implementation/implementation.h"
15 #include "effect.h"
16 #include "ui/view/view.h"
18 /* Inkscape::Extension::Effect */
20 namespace Inkscape {
21 namespace Extension {
23 Effect * Effect::_last_effect = NULL;
24 Inkscape::XML::Node * Effect::_effects_list = NULL;
26 Effect::Effect (Inkscape::XML::Node * in_repr, Implementation::Implementation * in_imp)
27 : Extension(in_repr, in_imp), _verb(get_id(), get_name(), NULL, NULL, this), _menu_node(NULL)
28 {
29 if (_effects_list == NULL)
30 find_effects_list(inkscape_get_menus(INKSCAPE));
32 if (_effects_list != NULL) {
33 unsigned start_pos = _effects_list->position();
35 _menu_node = sp_repr_new("verb");
36 _menu_node->setAttribute("verb-id", this->get_id(), false);
37 _effects_list->parent()->appendChild(_menu_node);
39 _menu_node->setPosition(start_pos + 1);
41 Inkscape::GC::release(_menu_node);
42 } /*else {
43 printf("Effect %s not added\n", get_name());
44 }*/
46 return;
47 }
49 Effect::~Effect (void)
50 {
51 if (get_last_effect() == this)
52 set_last_effect(NULL);
53 return;
54 }
56 bool
57 Effect::check (void)
58 {
59 if (!Extension::check()) {
60 /** \todo Check to see if parent has this as its only child,
61 if so, delete it too */
62 if (_menu_node != NULL)
63 sp_repr_unparent(_menu_node);
64 _menu_node = NULL;
65 return false;
66 }
67 return true;
68 }
70 bool
71 Effect::prefs (Inkscape::UI::View::View * doc)
72 {
73 if (!loaded())
74 set_state(Extension::STATE_LOADED);
75 if (!loaded()) return false;
77 Gtk::Widget * controls;
78 controls = imp->prefs_effect(this, doc);
79 if (controls == NULL) {
80 // std::cout << "No preferences for Effect" << std::endl;
81 return true;
82 }
84 PrefDialog * dialog = new PrefDialog(this->get_name(), controls);
85 int response = dialog->run();
86 dialog->hide();
88 delete dialog;
90 if (response == Gtk::RESPONSE_OK) return true;
92 return false;
93 }
95 /**
96 \brief The function that 'does' the effect itself
97 \param doc The Inkscape::UI::View::View to do the effect on
99 This function first insures that the extension is loaded, and if not,
100 loads it. It then calls the implemention to do the actual work. It
101 also resets the last effect pointer to be this effect. Finally, it
102 executes a \c sp_document_done to commit the changes to the undo
103 stack.
104 */
105 void
106 Effect::effect (Inkscape::UI::View::View * doc)
107 {
108 if (!loaded())
109 set_state(Extension::STATE_LOADED);
110 if (!loaded()) return;
112 set_last_effect(this);
113 imp->effect(this, doc);
115 sp_document_done(doc->doc());
117 return;
118 }
120 void
121 Effect::set_last_effect (Effect * in_effect)
122 {
123 if (in_effect == NULL) {
124 Inkscape::Verb::get(SP_VERB_EFFECT_LAST)->sensitive(NULL, false);
125 Inkscape::Verb::get(SP_VERB_EFFECT_LAST_PREF)->sensitive(NULL, false);
126 } else if (_last_effect == NULL) {
127 Inkscape::Verb::get(SP_VERB_EFFECT_LAST)->sensitive(NULL, true);
128 Inkscape::Verb::get(SP_VERB_EFFECT_LAST_PREF)->sensitive(NULL, true);
129 }
131 _last_effect = in_effect;
132 return;
133 }
135 #define EFFECTS_LIST "effects-list"
137 bool
138 Effect::find_effects_list (Inkscape::XML::Node * menustruct)
139 {
140 if (menustruct == NULL) return false;
141 for (Inkscape::XML::Node * child = menustruct;
142 child != NULL;
143 child = child->next()) {
144 if (!strcmp(child->name(), EFFECTS_LIST)) {
145 _effects_list = child;
146 return true;
147 }
148 Inkscape::XML::Node * firstchild = child->firstChild();
149 if (firstchild != NULL)
150 if (find_effects_list(firstchild))
151 return true;
152 }
153 return false;
154 }
156 /** \brief Create an action for a \c EffectVerb
157 \param view Which view the action should be created for
158 \return The built action.
160 Calls \c make_action_helper with the \c vector.
161 */
162 SPAction *
163 Effect::EffectVerb::make_action (Inkscape::UI::View::View * view)
164 {
165 return make_action_helper(view, &vector, static_cast<void *>(_effect));
166 }
168 /** \brief Decode the verb code and take appropriate action */
169 void
170 Effect::EffectVerb::perform (SPAction *action, void * data, void *pdata)
171 {
172 Inkscape::UI::View::View * current_view = sp_action_get_view(action);
173 // SPDocument * current_document = current_view->doc;
174 Effect * effect = reinterpret_cast<Effect *>(data);
176 if (effect == NULL) return;
177 if (current_view == NULL) return;
179 // std::cout << "Executing: " << effect->get_name() << std::endl;
180 if (effect->prefs(current_view))
181 effect->effect(current_view);
183 return;
184 }
186 /**
187 * Action vector to define functions called if a staticly defined file verb
188 * is called.
189 */
190 SPActionEventVector Effect::EffectVerb::vector =
191 {{NULL},Effect::EffectVerb::perform, NULL, NULL, NULL};
194 } } /* namespace Inkscape, Extension */
196 /*
197 Local Variables:
198 mode:c++
199 c-file-style:"stroustrup"
200 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
201 indent-tabs-mode:nil
202 fill-column:99
203 End:
204 */
205 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :