summaryrefslogtreecommitdiffstats
path: root/src/ui/dialog/panel-dialog.h
blob: eb7d9f37e9355483cbd77967bb6f2fa54a48cc4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * @brief A panel holding dialog
 */
/* Authors:
 *   C 2007 Gustav Broberg <broberg@kth.se>
 *   C 2012 Kris De Gussem <Kris.DeGussem@gmail.com>
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef INKSCAPE_PANEL_DIALOG_H
#define INKSCAPE_PANEL_DIALOG_H

#include <glibmm/i18n.h>
#include <gtkmm/dialog.h>

#include "verbs.h"
#include "dialog.h"
#include "ui/dialog/swatches.h"
#include "ui/dialog/floating-behavior.h"
#include "ui/dialog/dock-behavior.h"
#include "inkscape.h"
#include "desktop.h"

namespace Inkscape {
namespace UI {
namespace Dialog {

/**
 * Auxiliary class for the link between UI::Dialog::PanelDialog and UI::Dialog::Dialog.
 *
 * PanelDialog handles signals emitted when a desktop changes, either changing to a
 * different desktop or a new document.
 */
class PanelDialogBase {
public:
    PanelDialogBase(UI::Widget::Panel &panel, char const */*prefs_path*/, int const /*verb_num*/) :
      _panel (panel) { }

    virtual void present() = 0;
    virtual ~PanelDialogBase() = default;

    virtual UI::Widget::Panel &getPanel() { return _panel; }

protected:

    inline virtual void _propagateDocumentReplaced(SPDesktop* desktop, SPDocument *document);
    inline virtual void _propagateDesktopActivated(SPDesktop *);
    inline virtual void _propagateDesktopDeactivated(SPDesktop *);

    UI::Widget::Panel &_panel;
    sigc::connection _document_replaced_connection;
};

/**
 * Bridges UI::Widget::Panel and UI::Dialog::Dialog.
 *
 * Where Dialog handles window behaviour, such as closing, position, etc, and where
 * Panel is the actual container for dialog child widgets (and from where the dialog
 * content is made), PanelDialog links these two classes together to create a
 * dockable and floatable dialog. The link with Dialog is made via PanelDialogBase.
 */
template <typename Behavior>
class PanelDialog : public PanelDialogBase, public Inkscape::UI::Dialog::Dialog {

public:
    /**
     * Constructor.
     * 
     * @param contents panel with the actual dialog content.
     * @param prefs_path characteristic path for loading/saving dialog position.
     * @param verb_num the dialog verb.
     */
    PanelDialog(UI::Widget::Panel &contents, char const *prefs_path, int const verb_num);

    ~PanelDialog() override = default;

    template <typename T>
    static PanelDialog<Behavior> *create();

    inline void present() override;

private:
    template <typename T>
    static PanelDialog<Behavior> *_create();

    inline void _presentDialog();

    PanelDialog() = delete;
    PanelDialog(PanelDialog<Behavior> const &d) = delete;                      // no copy
    PanelDialog<Behavior>& operator=(PanelDialog<Behavior> const &d) = delete; // no assign
};


void PanelDialogBase::_propagateDocumentReplaced(SPDesktop *desktop, SPDocument *document)
{
    _panel.signalDocumentReplaced().emit(desktop, document);
}

void PanelDialogBase::_propagateDesktopActivated(SPDesktop *desktop)
{
    _document_replaced_connection =
        desktop->connectDocumentReplaced(sigc::mem_fun(*this, &PanelDialogBase::_propagateDocumentReplaced));
    _panel.signalActivateDesktop().emit(desktop);
}

void PanelDialogBase::_propagateDesktopDeactivated(SPDesktop *desktop)
{
    _document_replaced_connection.disconnect();
    _panel.signalDeactiveDesktop().emit(desktop);
}


template <typename B>
PanelDialog<B>::PanelDialog(Widget::Panel &panel, char const *prefs_path, int const verb_num) :
    PanelDialogBase(panel, prefs_path, verb_num),
    Dialog(&B::create, prefs_path, verb_num)
{
    Gtk::Box *vbox = get_vbox();
    _panel.signalResponse().connect(sigc::mem_fun(*this, &PanelDialog::_handleResponse));
    _panel.signalPresent().connect(sigc::mem_fun(*this, &PanelDialog::_presentDialog));

    vbox->pack_start(_panel, true, true, 0);

    SPDesktop *desktop = SP_ACTIVE_DESKTOP;

    _propagateDesktopActivated(desktop);

    _document_replaced_connection =
        desktop->connectDocumentReplaced(sigc::mem_fun(*this, &PanelDialog::_propagateDocumentReplaced));

    show_all_children();
}

template <typename B> template <typename P>
PanelDialog<B> *PanelDialog<B>::create()
{
    return _create<P>();
}

template <typename B> template <typename P>
PanelDialog<B> *PanelDialog<B>::_create()
{
    UI::Widget::Panel &panel = P::getInstance();
    return new PanelDialog<B>(panel, panel.getPrefsPath(), panel.getVerb());
}

template <typename B>
void PanelDialog<B>::present()
{
    _panel.present();
}

template <typename B>
void PanelDialog<B>::_presentDialog()
{
    Dialog::present();
}

template <> inline
void PanelDialog<Behavior::FloatingBehavior>::present()
{
    Dialog::present();
    _panel.present();
}

template <> inline
void PanelDialog<Behavior::FloatingBehavior>::_presentDialog()
{
}

/**
 * Specialized factory method for panel dialogs with floating behavior in order to make them work as
 * singletons, i.e. allow them track the current active desktop.
 */
template <>
template <typename P>
PanelDialog<Behavior::FloatingBehavior> *PanelDialog<Behavior::FloatingBehavior>::create()
{
    auto instance = _create<P>();

    INKSCAPE.signal_activate_desktop.connect(
            sigc::mem_fun(*instance, &PanelDialog<Behavior::FloatingBehavior>::_propagateDesktopActivated)
    );
    INKSCAPE.signal_deactivate_desktop.connect(        
            sigc::mem_fun(*instance, &PanelDialog<Behavior::FloatingBehavior>::_propagateDesktopDeactivated)
    );

    return instance;
}

} // namespace Dialog
} // namespace UI
} // namespace Inkscape

#endif //INKSCAPE_PANEL_DIALOG_H

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :