Code

r16404@tres: ted | 2007-08-29 19:48:31 -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) :
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 {
55     Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox());
56     hbox->pack_start(*controls, true, true, 6);
57     hbox->show();
58     this->get_vbox()->pack_start(*hbox, true, true, 6);
60     /*
61     Gtk::Button * help_button = add_button(Gtk::Stock::HELP, Gtk::RESPONSE_HELP);
62     if (_help == NULL)
63         help_button->set_sensitive(false);
64     */
65     _button_cancel = add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
66     _button_cancel->set_use_stock(true);
68     _button_ok = add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
69     _button_ok->set_use_stock(true);
70     set_default_response(Gtk::RESPONSE_OK);
71     _button_ok->grab_focus();
72     
73     // If we're working with an effect that can be live and 
74     // the dialog can be pinned, put those options in too
75     if (_exEnv != NULL) {
76         if (_param_preview == NULL) {
77             XML::Document * doc = sp_repr_read_mem(live_param_xml, strlen(live_param_xml), NULL);
78             _param_preview = Parameter::make(doc->root(), _exEnv->_effect);
79         }
80         if (_param_pinned == NULL) {
81             XML::Document * doc = sp_repr_read_mem(pinned_param_xml, strlen(pinned_param_xml), NULL);
82             _param_pinned = Parameter::make(doc->root(), _exEnv->_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         _button_pinned  = _param_pinned->get_widget(NULL, NULL, &_signal_pinned);
93         _button_pinned->show();
94         hbox->pack_start(*_button_preview, true, true,6);
95         hbox->pack_start(*_button_pinned, true, true,6);
96         hbox->show();
97         this->get_vbox()->pack_start(*hbox, true, true, 6);
99         preview_toggle();
100         pinned_toggle();
101         _signal_preview.connect(sigc::mem_fun(this, &PrefDialog::preview_toggle));
102         _signal_pinned.connect(sigc::mem_fun(this, &PrefDialog::pinned_toggle));
104     }
106     GtkWidget *dlg = GTK_WIDGET(gobj());
107     sp_transientize(dlg);
109     return;
112 PrefDialog::~PrefDialog ( )
114     if (_param_preview != NULL) {
115         delete _param_preview;
116     }
117     if (_param_pinned != NULL) {
118         delete _param_pinned;
119     }
121     return;
124 /** \brief  Runs the dialog
125     \return The response to the dialog
127     This function overrides the run function in the GTKmm dialog
128     class, but basically it only calls it.  This function only
129     handles the \c Gtk::RESPONSE_HELP return, and in that case it
130     brings up the help window.  All other return values are returned
131     to the calling function.
132 */
133 int
134 PrefDialog::run (void) {
135     int resp = Gtk::RESPONSE_HELP;
136     while (resp == Gtk::RESPONSE_HELP) {
137         resp = Gtk::Dialog::run();
138         if (resp == Gtk::RESPONSE_HELP) {
139             /*
140             if (_helpDialog == NULL) {
141                 _helpDialog = new HelpDialog(_help);
142             }
143             */
144         }
145     }
146     return resp;
149 void
150 PrefDialog::setPreviewState (Glib::ustring state) {
154 void
155 PrefDialog::preview_toggle (void) {
156     if(_param_preview->get_bool(NULL, NULL) && !_param_pinned->get_bool(NULL, NULL)) {
157         _exEnv->livePreview(true);
158     } else {
159         _exEnv->livePreview(false);
160     }
163 void
164 PrefDialog::pinned_toggle (void) {
165     if (_param_pinned->get_bool(NULL, NULL)) {
166         _button_preview->set_sensitive(false);
167         preview_toggle();
168         set_modal(false);
170         _button_ok->set_label(Gtk::Stock::EXECUTE.id);
171         _button_cancel->set_label(Gtk::Stock::CLOSE.id);
173         if (_exEnv != NULL) {
174             _exEnv->shutdown(_createdExEnv);
175             _exEnv = NULL;
176         }
177     } else {
178         _button_preview->set_sensitive(true);
179         set_modal(true);
181         _button_ok->set_label(Gtk::Stock::OK.id);
182         _button_cancel->set_label(Gtk::Stock::CANCEL.id);
184         if (_exEnv == NULL) {
185             _exEnv = new ExecutionEnv(_effect, SP_ACTIVE_DESKTOP, NULL, this);
186             _createdExEnv = true;
187             preview_toggle();
188             _exEnv->run();
189         }
190     }
193 void
194 PrefDialog::on_response (int signal) {
195     if (!_param_pinned->get_bool(NULL, NULL)) {
196         // Not my job if we're not pinned
197         // It's the execution environment's job
198         return;
199     }
201     if (signal == Gtk::RESPONSE_OK) {
202         _effect->effect(SP_ACTIVE_DESKTOP);
203     }
205     this->hide();
206     delete this;
209 #include "internal/clear-n_.h"
211 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>";
212 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>";
214 }; }; /* namespace Inkscape, Extension */
216 /*
217   Local Variables:
218   mode:c++
219   c-file-style:"stroustrup"
220   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
221   indent-tabs-mode:nil
222   fill-column:99
223   End:
224 */
225 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :