Code

eebd323d6a54ed6ff252c5fd4295ad68de1e7fa2
[inkscape.git] / src / ui / dialog / panel-dialog.h
1 /**
2  * \brief A panel holding dialog
3  *
4  * Authors:
5  *   Gustav Broberg <broberg@kth.se>
6  *
7  * Copyright (C) 2007 Authors
8  *
9  * Released under GNU GPL.  Read the file 'COPYING' for more information.
10  */
12 #ifndef INKSCAPE_PANEL_DIALOG_H
13 #define INKSCAPE_PANEL_DIALOG_H
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <gtkmm/stock.h>
21 #include "verbs.h"
22 #include "dialog.h"
23 #include "dialogs/swatches.h"
24 #include "ui/dialog/floating-behavior.h"
25 #include "ui/dialog/dock-behavior.h"
26 #include "prefs-utils.h"
28 namespace Inkscape {
29 namespace UI {
30 namespace Dialog {
32 class PanelDialogBase {
33 public:
34     PanelDialogBase(Panel &panel, char const */*prefs_path*/, int const /*verb_num*/,
35                     Glib::ustring const &/*apply_label*/) :
36       _panel (panel) { }
38     virtual void present() = 0;
39     virtual ~PanelDialogBase() {}
41     virtual Panel &getPanel() { return _panel; }
43 protected:
44     static void handle_deactivate_desktop(Inkscape::Application *application, SPDesktop *desktop, void *data) {
45         g_return_if_fail(data != NULL);
46         static_cast<PanelDialogBase *>(data)->_propagateDesktopDeactivated(application, desktop);
47     }
49     static void _handle_activate_desktop(Inkscape::Application *application, SPDesktop *desktop, void *data) {
50         g_return_if_fail(data != NULL);
51         static_cast<PanelDialogBase *>(data)->_propagateDesktopActivated(application, desktop);
52     }
54     inline virtual void _propagateDocumentReplaced(SPDesktop* desktop, SPDocument *document);
55     inline virtual void _propagateDesktopActivated(Inkscape::Application *, SPDesktop *);
56     inline virtual void _propagateDesktopDeactivated(Inkscape::Application *, SPDesktop *);
58     Panel &_panel;
59     sigc::connection _document_replaced_connection;
60 };
62 template <typename Behavior>
63 class PanelDialog : public PanelDialogBase, public Inkscape::UI::Dialog::Dialog {
65 public:
66     PanelDialog(Panel &contents, char const *prefs_path, int const verb_num,
67                 Glib::ustring const &apply_label);
69     virtual ~PanelDialog() {}
71     template <typename T>
72     static PanelDialog<Behavior> *create();
74     inline virtual void present();
76 private:
77     inline void _presentDialog();
79     PanelDialog();  // no constructor without params
80     PanelDialog(PanelDialog<Behavior> const &d);                      // no copy
81     PanelDialog<Behavior>& operator=(PanelDialog<Behavior> const &d); // no assign
82 };
85 template <>
86 class PanelDialog<Behavior::FloatingBehavior> :
87         public PanelDialogBase, public Inkscape::UI::Dialog::Dialog {
89 public:
90     inline PanelDialog(Panel &contents, char const *prefs_path, int const verb_num,
91                        Glib::ustring const &apply_label);
93     virtual ~PanelDialog() {}
95     template <typename T>
96     static PanelDialog<Behavior::FloatingBehavior> *create();
98     inline virtual void present();
100 private:
101     PanelDialog();  // no constructor without params
102     PanelDialog(PanelDialog<Behavior::FloatingBehavior> const &d); // no copy
103     PanelDialog<Behavior::FloatingBehavior>&
104     operator=(PanelDialog<Behavior::FloatingBehavior> const &d);   // no assign
105 };
109 void
110 PanelDialogBase::_propagateDocumentReplaced(SPDesktop *desktop, SPDocument *document)
112     _panel.signalDocumentReplaced().emit(desktop, document);
115 void
116 PanelDialogBase::_propagateDesktopActivated(Inkscape::Application *application, SPDesktop *desktop)
118     _document_replaced_connection =
119         desktop->connectDocumentReplaced(sigc::mem_fun(*this, &PanelDialogBase::_propagateDocumentReplaced));
120     _panel.signalActivateDesktop().emit(application, desktop);
123 void
124 PanelDialogBase::_propagateDesktopDeactivated(Inkscape::Application *application, SPDesktop *desktop)
126     _document_replaced_connection.disconnect();
127     _panel.signalDeactiveDesktop().emit(application, desktop);
131 template <typename B>
132 PanelDialog<B>::PanelDialog(Panel &panel, char const *prefs_path, int const verb_num,
133                             Glib::ustring const &apply_label) :
134     PanelDialogBase(panel, prefs_path, verb_num, apply_label),
135     Dialog(&B::create, prefs_path, verb_num, apply_label)
137     Gtk::VBox *vbox = get_vbox();
138     _panel.signalResponse().connect(sigc::mem_fun(*this, &PanelDialog::_handleResponse));
139     _panel.signalPresent().connect(sigc::mem_fun(*this, &PanelDialog::_presentDialog));
141     vbox->pack_start(_panel, true, true, 0);
143     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
145     _propagateDesktopActivated(INKSCAPE, desktop);
147     _document_replaced_connection =
148         desktop->connectDocumentReplaced(sigc::mem_fun(*this, &PanelDialog::_propagateDocumentReplaced));
150     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || !apply_label.empty()) {
151         // TODO: make the order of buttons obey the global preference
152         if (!apply_label.empty()) {
153             panel.addResponseButton(apply_label, Gtk::RESPONSE_APPLY);
154             panel.setDefaultResponse(Gtk::RESPONSE_APPLY);
155         }
156         panel.addResponseButton(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
157     }
159     show_all_children();
162 template <typename B> template <typename P>
163 PanelDialog<B> *
164 PanelDialog<B>::create()
166     Panel &panel = P::getInstance();
167     return new PanelDialog<B>(panel, panel.getPrefsPath(), panel.getVerb(), panel.getApplyLabel());
170 template <typename B>
171 void
172 PanelDialog<B>::present()
174     _panel.present(); 
177 template <typename B>
178 void
179 PanelDialog<B>::_presentDialog()
181     Dialog::present(); 
184 PanelDialog<Behavior::FloatingBehavior>::PanelDialog(Panel &panel, char const *prefs_path,
185                                                      int const verb_num, Glib::ustring const &apply_label) :
186     PanelDialogBase(panel, prefs_path, verb_num, apply_label),
187     Dialog(&Behavior::FloatingBehavior::create, prefs_path, verb_num, apply_label)
189     Gtk::VBox *vbox = get_vbox();
190     _panel.signalResponse().connect(sigc::mem_fun(*this, &PanelDialog::_handleResponse));
192     vbox->pack_start(_panel, true, true, 0);
194     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
196     _propagateDesktopActivated(INKSCAPE, desktop);
198     _document_replaced_connection =
199         desktop->connectDocumentReplaced(sigc::mem_fun(*this, &PanelDialog::_propagateDocumentReplaced));
201     if (prefs_get_int_attribute ("dialogs", "showclose", 0) || !apply_label.empty()) {
202         // TODO: make the order of buttons obey the global preference
203         if (!apply_label.empty()) {
204             panel.addResponseButton(apply_label, Gtk::RESPONSE_APPLY);
205             panel.setDefaultResponse(Gtk::RESPONSE_APPLY);
206         }
207         panel.addResponseButton(Gtk::Stock::CLOSE, Gtk::RESPONSE_CLOSE);
208     }
210     show_all_children();
213 void
214 PanelDialog<Behavior::FloatingBehavior>::present()
215
216     Dialog::present();
217     _panel.present();
220 /**
221  * Specialize factory method for panel dialogs with floating behavior in order to make them work as
222  * singletons, i.e. allow them track the current active desktop.
223  */
224 template <typename P>
225 PanelDialog<Behavior::FloatingBehavior> *
226 PanelDialog<Behavior::FloatingBehavior>::create()
228     Panel &panel = P::getInstance();
229     PanelDialog<Behavior::FloatingBehavior> *instance =
230         new PanelDialog<Behavior::FloatingBehavior>(panel, panel.getPrefsPath(),
231                                                     panel.getVerb(), panel.getApplyLabel());
233     g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop", G_CALLBACK(_handle_activate_desktop), instance);
234     g_signal_connect(G_OBJECT(INKSCAPE), "deactivate_desktop", G_CALLBACK(handle_deactivate_desktop), instance);
236     return instance;
239 } // namespace Dialog
240 } // namespace UI
241 } // namespace Inkscape
243 #endif //INKSCAPE_PANEL_DIALOG_H
245 /*
246   Local Variables:
247   mode:c++
248   c-file-style:"stroustrup"
249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250   indent-tabs-mode:nil
251   fill-column:99
252   End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :