Code

Node tool: special case node duplication for endnodes - select new endnode
[inkscape.git] / src / extension / prefdialog.cpp
1 /*
2  * Authors:
3  *   Ted Gould <ted@gould.cx>
4  *
5  * Copyright (C) 2005-2008 Authors
6  *
7  * Released under GNU GPL, read the file 'COPYING' for more information
8  */
10 #include <gtkmm/stock.h>
11 #include <gtkmm/checkbutton.h>
12 #include <gtkmm/separator.h>
13 #include <glibmm/i18n.h>
15 #include "../dialogs/dialog-events.h"
16 #include "xml/repr.h"
18 // Used to get SP_ACTIVE_DESKTOP
19 #include "inkscape.h"
20 #include "desktop.h"
22 #include "effect.h"
23 #include "implementation/implementation.h"
25 #include "prefdialog.h"
28 namespace Inkscape {
29 namespace Extension {
32 /** \brief  Creates a new preference dialog for extension preferences
33     \param  name  Name of the Extension who's dialog this is
34     \param  help  The help string for the extension (NULL if none)
35     \param  controls  The extension specific widgets in the dialog
37     This function initializes the dialog with the name of the extension
38     in the title.  It adds a few buttons and sets up handlers for
39     them.  It also places the passed in widgets into the dialog.
40 */
41 PrefDialog::PrefDialog (Glib::ustring name, gchar const * help, Gtk::Widget * controls, Effect * effect) :
42     Gtk::Dialog::Dialog(_(name.c_str()), true, true),
43     _help(help),
44     _name(name),
45     _button_ok(NULL),
46     _button_cancel(NULL),
47     _button_preview(NULL),
48     _param_preview(NULL),
49     _effect(effect),
50     _exEnv(NULL)
51 {
52     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
53     if (controls == NULL) {
54         if (_effect == NULL) {
55             std::cout << "AH!!!  No controls and no effect!!!" << std::endl;
56             return;
57         }
58         controls = _effect->get_imp()->prefs_effect(_effect, SP_ACTIVE_DESKTOP, &_signal_param_change, NULL);
59         _signal_param_change.connect(sigc::mem_fun(this, &PrefDialog::param_change));
60     }
62     hbox->pack_start(*controls, true, true, 6);
63     hbox->show();
64     this->get_vbox()->pack_start(*hbox, true, true, 6);
66     /*
67     Gtk::Button * help_button = add_button(Gtk::Stock::HELP, Gtk::RESPONSE_HELP);
68     if (_help == NULL)
69         help_button->set_sensitive(false);
70     */
71     _button_cancel = add_button(_effect == NULL ? Gtk::Stock::CANCEL : Gtk::Stock::CLOSE, Gtk::RESPONSE_CANCEL);
72     _button_cancel->set_use_stock(true);
74     _button_ok = add_button(_effect == NULL ? Gtk::Stock::OK : Gtk::Stock::APPLY, Gtk::RESPONSE_OK);
75     _button_ok->set_use_stock(true);
76     set_default_response(Gtk::RESPONSE_OK);
77     _button_ok->grab_focus();
79     if (_effect != NULL && !_effect->no_live_preview) {
80         if (_param_preview == NULL) {
81             XML::Document * doc = sp_repr_read_mem(live_param_xml, strlen(live_param_xml), NULL);
82             _param_preview = Parameter::make(doc->root(), _effect);
83         }
85         Gtk::HSeparator * sep = Gtk::manage(new Gtk::HSeparator());
86         sep->show();
87         this->get_vbox()->pack_start(*sep, true, true, 4);
89         hbox = Gtk::manage(new Gtk::HBox());
90         _button_preview = _param_preview->get_widget(NULL, NULL, &_signal_preview);
91         _button_preview->show();
92         hbox->pack_start(*_button_preview, true, true,6);
93         hbox->show();
94         this->get_vbox()->pack_start(*hbox, true, true, 6);
96         Gtk::HBox * hbox = dynamic_cast<Gtk::HBox *>(_button_preview);
97         if (hbox != NULL) {
98             Gtk::Widget * back = hbox->children().back().get_widget();
99             Gtk::CheckButton * cb = dynamic_cast<Gtk::CheckButton *>(back);
100             _checkbox_preview = cb;
101         }
103         preview_toggle();
104         _signal_preview.connect(sigc::mem_fun(this, &PrefDialog::preview_toggle));
105     }
107     GtkWidget *dlg = GTK_WIDGET(gobj());
108     sp_transientize(dlg);
110     return;
113 PrefDialog::~PrefDialog ( )
115     if (_param_preview != NULL) {
116         delete _param_preview;
117         _param_preview = NULL;
118     }
120     if (_exEnv != NULL) {
121         _exEnv->cancel();
122         delete _exEnv;
123         _exEnv = NULL;
124     }
126     if (_effect != NULL) {
127         _effect->set_pref_dialog(NULL);
128     }
130     return;
133 #if 0
134 /** \brief  Runs the dialog
135     \return The response to the dialog
137     This function overrides the run function in the GTKmm dialog
138     class, but basically it only calls it.  This function only
139     handles the \c Gtk::RESPONSE_HELP return, and in that case it
140     brings up the help window.  All other return values are returned
141     to the calling function.
142 */
143 int
144 PrefDialog::run (void) {
145     int resp = Gtk::RESPONSE_HELP;
146     while (resp == Gtk::RESPONSE_HELP) {
147         resp = Gtk::Dialog::run();
148         if (resp == Gtk::RESPONSE_HELP) {
149             /*
150             if (_helpDialog == NULL) {
151                 _helpDialog = new HelpDialog(_help);
152             }
153             */
154         }
155     }
156     return resp;
158 #endif
160 void
161 PrefDialog::preview_toggle (void) {
162     if(_param_preview->get_bool(NULL, NULL)) {
163         set_modal(true);
164         if (_exEnv == NULL) {
165             _exEnv = new ExecutionEnv(_effect, SP_ACTIVE_DESKTOP, NULL, false, false);
166             _exEnv->run();
167         }
168     } else {
169         set_modal(false);
170         if (_exEnv != NULL) {
171             _exEnv->cancel();
172             _exEnv->undo();
173             delete _exEnv;
174             _exEnv = NULL;
175         }
176     }
179 void
180 PrefDialog::param_change (void) {
181     if (_exEnv != NULL) {
182         _timersig.disconnect();
183         _timersig = Glib::signal_timeout().connect(sigc::mem_fun(this, &PrefDialog::param_timer_expire),
184                                                    250, /* ms */
185                                                    Glib::PRIORITY_DEFAULT_IDLE);
186     }
188     return;
191 bool
192 PrefDialog::param_timer_expire (void) {
193     if (_exEnv != NULL) {
194         _exEnv->cancel();
195         _exEnv->undo();
196         _exEnv->run();
197     }
199     return false;
202 void
203 PrefDialog::on_response (int signal) {
204     if (signal == Gtk::RESPONSE_OK) {
205         if (_exEnv == NULL) {
206                         if (_effect != NULL) {
207                                 _effect->effect(SP_ACTIVE_DESKTOP);
208                         } else {
209                                 // Shutdown run()
210                                 return;
211                         }
212         } else {
213             if (_exEnv->wait()) {
214                 _exEnv->commit();
215             } else {
216                 _exEnv->undo();
217             }
218             delete _exEnv;
219             _exEnv = NULL;
220         }
221     }
223     if (_param_preview != NULL) {
224         _checkbox_preview->set_active(false);
225     }
227     if ((signal == Gtk::RESPONSE_CANCEL || signal == Gtk::RESPONSE_DELETE_EVENT) && _effect != NULL) {
228         delete this;
229     }
231     return;
234 #include "internal/clear-n_.h"
236 const char * PrefDialog::live_param_xml = "<param name=\"__live_effect__\" type=\"boolean\" gui-text=\"" N_("Live preview") "\" gui-description=\"" N_("Is the effect previewed live on canvas?") "\" scope=\"user\">false</param>";
238 }; }; /* namespace Inkscape, Extension */
240 /*
241   Local Variables:
242   mode:c++
243   c-file-style:"stroustrup"
244   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
245   indent-tabs-mode:nil
246   fill-column:99
247   End:
248 */
249 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :