1 /*
2 * Authors:
3 * Ted Gould <ted@gould.cx>
4 *
5 * Copyright (C) 2005-2007 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 "preferences.h"
23 #include "effect.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, ExecutionEnv * exEnv, Effect * effect, sigc::signal<void> * changeSignal) :
42 Gtk::Dialog::Dialog(_(name.c_str()), true, true),
43 _help(help),
44 _name(name),
45 _exEnv(exEnv),
46 _createdExEnv(false),
47 _button_ok(NULL),
48 _button_cancel(NULL),
49 _button_preview(NULL),
50 _button_pinned(NULL),
51 _param_preview(NULL),
52 _param_pinned(NULL),
53 _effect(effect),
54 _signal_param_change(changeSignal)
55 {
56 Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
57 hbox->pack_start(*controls, true, true, 6);
58 hbox->show();
59 this->get_vbox()->pack_start(*hbox, true, true, 6);
61 /*
62 Gtk::Button * help_button = add_button(Gtk::Stock::HELP, Gtk::RESPONSE_HELP);
63 if (_help == NULL)
64 help_button->set_sensitive(false);
65 */
66 _button_cancel = add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
67 _button_cancel->set_use_stock(true);
69 _button_ok = add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
70 _button_ok->set_use_stock(true);
71 set_default_response(Gtk::RESPONSE_OK);
72 _button_ok->grab_focus();
74 // If we're working with an effect that can be live and
75 // the dialog can be pinned, put those options in too
76 if (_exEnv != NULL) {
77 if (_param_preview == NULL) {
78 XML::Document * doc = sp_repr_read_mem(live_param_xml, strlen(live_param_xml), NULL);
79 _param_preview = Parameter::make(doc->root(), _exEnv->_effect);
80 }
81 if (_param_pinned == NULL) {
82 XML::Document * doc = sp_repr_read_mem(pinned_param_xml, strlen(pinned_param_xml), NULL);
83 _param_pinned = Parameter::make(doc->root(), _exEnv->_effect);
84 }
86 Gtk::HSeparator * sep = Gtk::manage(new Gtk::HSeparator());
87 sep->show();
88 this->get_vbox()->pack_start(*sep, true, true, 4);
90 hbox = Gtk::manage(new Gtk::HBox());
91 _button_preview = _param_preview->get_widget(NULL, NULL, &_signal_preview);
92 _button_preview->show();
93 _button_pinned = _param_pinned->get_widget(NULL, NULL, &_signal_pinned);
94 _button_pinned->show();
95 hbox->pack_start(*_button_preview, true, true,6);
96 hbox->pack_start(*_button_pinned, true, true,6);
97 hbox->show();
98 this->get_vbox()->pack_start(*hbox, true, true, 6);
100 preview_toggle();
101 pinned_toggle();
102 _signal_preview.connect(sigc::mem_fun(this, &PrefDialog::preview_toggle));
103 _signal_pinned.connect(sigc::mem_fun(this, &PrefDialog::pinned_toggle));
105 }
107 GtkWidget *dlg = GTK_WIDGET(gobj());
108 sp_transientize(dlg);
110 if (_effect != NULL) {
111 _effect->set_pref_dialog(this);
112 }
114 return;
115 }
117 PrefDialog::~PrefDialog ( )
118 {
119 if (_effect != NULL) {
120 _effect->set_pref_dialog(NULL);
121 }
122 if (_param_preview != NULL) {
123 delete _param_preview;
124 }
125 if (_param_pinned != NULL) {
126 delete _param_pinned;
127 }
129 return;
130 }
132 /** \brief Runs the dialog
133 \return The response to the dialog
135 This function overrides the run function in the GTKmm dialog
136 class, but basically it only calls it. This function only
137 handles the \c Gtk::RESPONSE_HELP return, and in that case it
138 brings up the help window. All other return values are returned
139 to the calling function.
140 */
141 int
142 PrefDialog::run (void) {
143 int resp = Gtk::RESPONSE_HELP;
144 while (resp == Gtk::RESPONSE_HELP) {
145 resp = Gtk::Dialog::run();
146 if (resp == Gtk::RESPONSE_HELP) {
147 /*
148 if (_helpDialog == NULL) {
149 _helpDialog = new HelpDialog(_help);
150 }
151 */
152 }
153 }
154 return resp;
155 }
157 void
158 PrefDialog::setPreviewState (Glib::ustring state) {
160 }
162 void
163 PrefDialog::preview_toggle (void) {
164 if(_param_preview->get_bool(NULL, NULL) && !_param_pinned->get_bool(NULL, NULL)) {
165 _exEnv->livePreview(true);
166 } else {
167 _exEnv->livePreview(false);
168 }
169 }
171 void
172 PrefDialog::pinned_toggle (void) {
173 if (_param_pinned->get_bool(NULL, NULL)) {
174 _button_preview->set_sensitive(false);
175 preview_toggle();
176 set_modal(false);
178 _button_ok->set_label(Gtk::Stock::EXECUTE.id);
179 _button_cancel->set_label(Gtk::Stock::CLOSE.id);
181 if (_exEnv != NULL) {
182 _exEnv->shutdown(_createdExEnv);
183 _exEnv = NULL;
184 }
185 } else {
186 _button_preview->set_sensitive(true);
187 set_modal(true);
189 _button_ok->set_label(Gtk::Stock::OK.id);
190 _button_cancel->set_label(Gtk::Stock::CANCEL.id);
192 if (_exEnv == NULL) {
193 _exEnv = new ExecutionEnv(_effect, SP_ACTIVE_DESKTOP, NULL, _signal_param_change, this);
194 _createdExEnv = true;
195 preview_toggle();
196 _exEnv->run();
197 }
198 }
199 }
201 void
202 PrefDialog::on_response (int signal) {
203 //printf("Got signal %d\n", signal);
204 if (!_param_pinned->get_bool(NULL, NULL)) {
205 // Not my job if we're not pinned
206 // It's the execution environment's job
207 return;
208 }
210 if (signal == Gtk::RESPONSE_OK) {
211 _effect->effect(SP_ACTIVE_DESKTOP);
212 } else {
213 this->hide();
214 delete _signal_param_change; // This is not in the destructor because
215 // it is the only situation that we need
216 // to delete it in
217 delete this;
218 }
220 this->hide();
221 delete this;
222 }
224 #include "internal/clear-n_.h"
226 const char * PrefDialog::pinned_param_xml = "<param name=\"__pinned__\" type=\"boolean\" gui-text=\"" N_("Pin Dialog") "\" gui-description=\"" N_("Toggles whether the dialog stays for multiple executions or disappears after one") "\" scope=\"user\">false</param>";
227 const char * PrefDialog::live_param_xml = "<param name=\"__live_effect__\" type=\"boolean\" gui-text=\"" N_("Live Preview") "\" gui-description=\"" N_("Controls whether the effect settings are rendered live on canvas") "\" scope=\"user\">false</param>";
229 }; }; /* namespace Inkscape, Extension */
231 /*
232 Local Variables:
233 mode:c++
234 c-file-style:"stroustrup"
235 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
236 indent-tabs-mode:nil
237 fill-column:99
238 End:
239 */
240 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :