Code

r16406@tres: ted | 2007-08-29 20:14:55 -0700
[inkscape.git] / src / extension / prefdialog.cpp
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
36     
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();
73     
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     return;
113 PrefDialog::~PrefDialog ( )
115     if (_param_preview != NULL) {
116         delete _param_preview;
117     }
118     if (_param_pinned != NULL) {
119         delete _param_pinned;
120     }
122     return;
125 /** \brief  Runs the dialog
126     \return The response to the dialog
128     This function overrides the run function in the GTKmm dialog
129     class, but basically it only calls it.  This function only
130     handles the \c Gtk::RESPONSE_HELP return, and in that case it
131     brings up the help window.  All other return values are returned
132     to the calling function.
133 */
134 int
135 PrefDialog::run (void) {
136     int resp = Gtk::RESPONSE_HELP;
137     while (resp == Gtk::RESPONSE_HELP) {
138         resp = Gtk::Dialog::run();
139         if (resp == Gtk::RESPONSE_HELP) {
140             /*
141             if (_helpDialog == NULL) {
142                 _helpDialog = new HelpDialog(_help);
143             }
144             */
145         }
146     }
147     return resp;
150 void
151 PrefDialog::setPreviewState (Glib::ustring state) {
155 void
156 PrefDialog::preview_toggle (void) {
157     if(_param_preview->get_bool(NULL, NULL) && !_param_pinned->get_bool(NULL, NULL)) {
158         _exEnv->livePreview(true);
159     } else {
160         _exEnv->livePreview(false);
161     }
164 void
165 PrefDialog::pinned_toggle (void) {
166     if (_param_pinned->get_bool(NULL, NULL)) {
167         _button_preview->set_sensitive(false);
168         preview_toggle();
169         set_modal(false);
171         _button_ok->set_label(Gtk::Stock::EXECUTE.id);
172         _button_cancel->set_label(Gtk::Stock::CLOSE.id);
174         if (_exEnv != NULL) {
175             _exEnv->shutdown(_createdExEnv);
176             _exEnv = NULL;
177         }
178     } else {
179         _button_preview->set_sensitive(true);
180         set_modal(true);
182         _button_ok->set_label(Gtk::Stock::OK.id);
183         _button_cancel->set_label(Gtk::Stock::CANCEL.id);
185         if (_exEnv == NULL) {
186             _exEnv = new ExecutionEnv(_effect, SP_ACTIVE_DESKTOP, NULL, _signal_param_change, this);
187             _createdExEnv = true;
188             preview_toggle();
189             _exEnv->run();
190         }
191     }
194 void
195 PrefDialog::on_response (int signal) {
196     //printf("Got signal %d\n", signal);
197     if (!_param_pinned->get_bool(NULL, NULL)) {
198         // Not my job if we're not pinned
199         // It's the execution environment's job
200         return;
201     }
203     if (signal == Gtk::RESPONSE_OK) {
204         _effect->effect(SP_ACTIVE_DESKTOP);
205     } else {
206         this->hide();
207         delete _signal_param_change; // This is not in the destructor because
208                                      // it is the only situation that we need
209                                      // to delete it in
210         delete this;
211     }
213     this->hide();
214     delete this;
217 #include "internal/clear-n_.h"
219 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>";
220 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>";
222 }; }; /* namespace Inkscape, Extension */
224 /*
225   Local Variables:
226   mode:c++
227   c-file-style:"stroustrup"
228   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
229   indent-tabs-mode:nil
230   fill-column:99
231   End:
232 */
233 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :