Code

moving trunk for module inkscape
[inkscape.git] / src / extension / effect.cpp
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         _menu_node = sp_repr_new("verb");
34         _menu_node->setAttribute("verb-id", this->get_id(), false);
35         _effects_list->parent()->appendChild(_menu_node);
36         Inkscape::GC::release(_menu_node);
37     } /*else {
38         printf("Effect %s not added\n", get_name());
39     }*/
41     return;
42 }
44 Effect::~Effect (void)
45 {
46     if (get_last_effect() == this)
47         set_last_effect(NULL);
48     return;
49 }
51 bool
52 Effect::check (void)
53 {
54     if (!Extension::check()) {
55         /** \todo  Check to see if parent has this as its only child,
56                    if so, delete it too */
57         if (_menu_node != NULL)
58             sp_repr_unparent(_menu_node);
59         _menu_node = NULL;
60         return false;
61     }
62     return true;
63 }
65 bool
66 Effect::prefs (Inkscape::UI::View::View * doc)
67 {
68     if (!loaded())
69         set_state(Extension::STATE_LOADED);
70     if (!loaded()) return false;
72     Gtk::Widget * controls;
73     controls = imp->prefs_effect(this, doc);
74     if (controls == NULL) {
75         // std::cout << "No preferences for Effect" << std::endl;
76         return true;
77     }
79     PrefDialog * dialog = new PrefDialog(this->get_name(), controls);
80     int response = dialog->run();
81     dialog->hide();
83     delete dialog;
85     if (response == Gtk::RESPONSE_OK) return true;
87     return false;
88 }
90 /**
91     \brief  The function that 'does' the effect itself
92     \param  doc  The Inkscape::UI::View::View to do the effect on
94     This function first insures that the extension is loaded, and if not,
95     loads it.  It then calls the implemention to do the actual work.  It
96     also resets the last effect pointer to be this effect.  Finally, it
97     executes a \c sp_document_done to commit the changes to the undo
98     stack.
99 */
100 void
101 Effect::effect (Inkscape::UI::View::View * doc)
103     if (!loaded())
104         set_state(Extension::STATE_LOADED);
105     if (!loaded()) return;
107     set_last_effect(this);
108     imp->effect(this, doc);
110     sp_document_done(doc->doc());
112     return;
115 void
116 Effect::set_last_effect (Effect * in_effect)
118     if (in_effect == NULL) {
119         Inkscape::Verb::get(SP_VERB_EFFECT_LAST)->sensitive(NULL, false);
120         Inkscape::Verb::get(SP_VERB_EFFECT_LAST_PREF)->sensitive(NULL, false);
121     } else if (_last_effect == NULL) {
122         Inkscape::Verb::get(SP_VERB_EFFECT_LAST)->sensitive(NULL, true);
123         Inkscape::Verb::get(SP_VERB_EFFECT_LAST_PREF)->sensitive(NULL, true);
124     }
126     _last_effect = in_effect;
127     return;
130 #define  EFFECTS_LIST  "effects-list"
132 bool
133 Effect::find_effects_list (Inkscape::XML::Node * menustruct)
135     if (menustruct == NULL) return false;
136     for (Inkscape::XML::Node * child = menustruct;
137             child != NULL;
138             child = child->next()) {
139         if (!strcmp(child->name(), EFFECTS_LIST)) {
140             _effects_list = menustruct;
141             return true;
142         }
143         Inkscape::XML::Node * firstchild = child->firstChild();
144         if (firstchild != NULL)
145             if (find_effects_list(firstchild))
146                 return true;
147     }
148     return false;
151 /** \brief  Create an action for a \c EffectVerb
152     \param  view  Which view the action should be created for
153     \return The built action.
155     Calls \c make_action_helper with the \c vector.
156 */
157 SPAction *
158 Effect::EffectVerb::make_action (Inkscape::UI::View::View * view)
160     return make_action_helper(view, &vector, static_cast<void *>(_effect));
163 /** \brief  Decode the verb code and take appropriate action */
164 void
165 Effect::EffectVerb::perform (SPAction *action, void * data, void *pdata)
167     Inkscape::UI::View::View * current_view = sp_action_get_view(action);
168 //  SPDocument * current_document = current_view->doc;
169     Effect * effect = reinterpret_cast<Effect *>(data);
171     if (effect == NULL) return;
172     if (current_view == NULL) return;
174     // std::cout << "Executing: " << effect->get_name() << std::endl;
175     if (effect->prefs(current_view))
176         effect->effect(current_view);
178     return;
181 /**
182  * Action vector to define functions called if a staticly defined file verb
183  * is called.
184  */
185 SPActionEventVector Effect::EffectVerb::vector =
186             {{NULL},Effect::EffectVerb::perform, NULL, NULL, NULL};
189 } }  /* namespace Inkscape, Extension */
191 /*
192   Local Variables:
193   mode:c++
194   c-file-style:"stroustrup"
195   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
196   indent-tabs-mode:nil
197   fill-column:99
198   End:
199 */
200 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :