1 /** @file
2 * @brief Object for managing a set of dialogs, including their signals and
3 * construction/caching/destruction of them.
4 */
5 /* Authors:
6 * Bryce W. Harrington <bryce@bryceharrington.org>
7 * Jon Phillips <jon@rejon.org>
8 * Gustav Broberg <broberg@kth.se>
9 *
10 * Copyright (C) 2004-2007 Authors
11 *
12 * Released under GNU GPL. Read the file 'COPYING' for more information.
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include "ui/dialog/dialog-manager.h"
21 #include "ui/dialog/align-and-distribute.h"
22 #include "ui/dialog/document-metadata.h"
23 #include "ui/dialog/document-properties.h"
24 #include "ui/dialog/extension-editor.h"
25 #include "ui/dialog/fill-and-stroke.h"
26 #include "ui/dialog/filter-effects-dialog.h"
27 #include "ui/dialog/find.h"
28 #include "ui/dialog/inkscape-preferences.h"
29 #include "ui/dialog/input.h"
30 #include "ui/dialog/livepatheffect-editor.h"
31 #include "ui/dialog/memory.h"
32 #include "ui/dialog/messages.h"
33 #include "ui/dialog/scriptdialog.h"
34 #ifdef ENABLE_SVG_FONTS
35 #include "ui/dialog/svg-fonts-dialog.h"
36 #endif // ENABLE_SVG_FONTS
37 #include "ui/dialog/tile.h"
38 #include "ui/dialog/tracedialog.h"
39 #include "ui/dialog/transformation.h"
40 #include "ui/dialog/undo-history.h"
41 #include "ui/dialog/panel-dialog.h"
43 #include "dialogs/layers-panel.h"
44 #include "dialogs/iconpreview.h"
46 #include "ui/dialog/floating-behavior.h"
47 #include "ui/dialog/dock-behavior.h"
48 #include "preferences.h"
50 namespace Inkscape {
51 namespace UI {
52 namespace Dialog {
54 namespace {
56 using namespace Behavior;
58 template <typename T, typename B>
59 inline Dialog *create() { return PanelDialog<B>::template create<T>(); }
61 }
63 /**
64 * This class is provided as a container for Inkscape's various
65 * dialogs. This allows Inkscape::Application to treat the various
66 * dialogs it invokes, as abstractions.
67 *
68 * DialogManager is essentially a cache of dialogs. It lets us
69 * initialize dialogs lazily - instead of constructing them during
70 * application startup, they're constructed the first time they're
71 * actually invoked by Inkscape::Application. The constructed
72 * dialog is held here after that, so future invokations of the
73 * dialog don't need to get re-constructed each time. The memory for
74 * the dialogs are then reclaimed when the DialogManager is destroyed.
75 *
76 * In addition, DialogManager also serves as a signal manager for
77 * dialogs. It provides a set of signals that can be sent to all
78 * dialogs for doing things such as hiding/unhiding them, etc.
79 * DialogManager ensures that every dialog it handles will listen
80 * to these signals.
81 *
82 */
83 DialogManager::DialogManager() {
85 using namespace Behavior;
86 using namespace Inkscape::UI::Dialogs; // temporary
88 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
89 int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
91 if (dialogs_type == FLOATING) {
93 registerFactory("AlignAndDistribute", &create<AlignAndDistribute, FloatingBehavior>);
94 registerFactory("DocumentMetadata", &create<DocumentMetadata, FloatingBehavior>);
95 registerFactory("DocumentProperties", &create<DocumentProperties, FloatingBehavior>);
96 registerFactory("ExtensionEditor", &create<ExtensionEditor, FloatingBehavior>);
97 registerFactory("FillAndStroke", &create<FillAndStroke, FloatingBehavior>);
98 registerFactory("FilterEffectsDialog", &create<FilterEffectsDialog, FloatingBehavior>);
99 registerFactory("Find", &create<Find, FloatingBehavior>);
100 registerFactory("IconPreviewPanel", &create<IconPreviewPanel, FloatingBehavior>);
101 registerFactory("InkscapePreferences", &create<InkscapePreferences, FloatingBehavior>);
102 registerFactory("LayersPanel", &create<LayersPanel, FloatingBehavior>);
103 registerFactory("LivePathEffect", &create<LivePathEffectEditor, FloatingBehavior>);
104 registerFactory("Memory", &create<Memory, FloatingBehavior>);
105 registerFactory("Messages", &create<Messages, FloatingBehavior>);
106 registerFactory("Script", &create<ScriptDialog, FloatingBehavior>);
107 #ifdef ENABLE_SVG_FONTS
108 registerFactory("SvgFontsDialog", &create<SvgFontsDialog, FloatingBehavior>);
109 #endif
110 registerFactory("Swatches", &create<SwatchesPanel, FloatingBehavior>);
111 registerFactory("TileDialog", &create<TileDialog, FloatingBehavior>);
112 registerFactory("Trace", &create<TraceDialog, FloatingBehavior>);
113 registerFactory("Transformation", &create<Transformation, FloatingBehavior>);
114 registerFactory("UndoHistory", &create<UndoHistory, FloatingBehavior>);
115 registerFactory("InputDevices", &create<InputDialog, FloatingBehavior>);
117 } else {
119 registerFactory("AlignAndDistribute", &create<AlignAndDistribute, DockBehavior>);
120 registerFactory("DocumentMetadata", &create<DocumentMetadata, DockBehavior>);
121 registerFactory("DocumentProperties", &create<DocumentProperties, DockBehavior>);
122 registerFactory("ExtensionEditor", &create<ExtensionEditor, DockBehavior>);
123 registerFactory("FillAndStroke", &create<FillAndStroke, DockBehavior>);
124 registerFactory("FilterEffectsDialog", &create<FilterEffectsDialog, DockBehavior>);
125 registerFactory("Find", &create<Find, DockBehavior>);
126 registerFactory("IconPreviewPanel", &create<IconPreviewPanel, DockBehavior>);
127 registerFactory("InkscapePreferences", &create<InkscapePreferences, DockBehavior>);
128 registerFactory("LayersPanel", &create<LayersPanel, DockBehavior>);
129 registerFactory("LivePathEffect", &create<LivePathEffectEditor, DockBehavior>);
130 registerFactory("Memory", &create<Memory, DockBehavior>);
131 registerFactory("Messages", &create<Messages, DockBehavior>);
132 registerFactory("Script", &create<ScriptDialog, DockBehavior>);
133 #ifdef ENABLE_SVG_FONTS
134 registerFactory("SvgFontsDialog", &create<SvgFontsDialog, DockBehavior>);
135 #endif
136 registerFactory("Swatches", &create<SwatchesPanel, DockBehavior>);
137 registerFactory("TileDialog", &create<TileDialog, DockBehavior>);
138 registerFactory("Trace", &create<TraceDialog, DockBehavior>);
139 registerFactory("Transformation", &create<Transformation, DockBehavior>);
140 registerFactory("UndoHistory", &create<UndoHistory, DockBehavior>);
141 registerFactory("InputDevices", &create<InputDialog, DockBehavior>);
143 }
144 }
146 DialogManager::~DialogManager() {
147 // TODO: Disconnect the signals
148 // TODO: Do we need to explicitly delete the dialogs?
149 // Appears to cause a segfault if we do
150 }
153 DialogManager &DialogManager::getInstance()
154 {
155 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
156 int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
158 /* Use singleton behavior for floating dialogs */
159 if (dialogs_type == FLOATING) {
160 static DialogManager *instance = 0;
162 if (!instance)
163 instance = new DialogManager();
164 return *instance;
165 }
167 return *new DialogManager();
168 }
170 /**
171 * Registers a dialog factory function used to create the named dialog.
172 */
173 void DialogManager::registerFactory(gchar const *name,
174 DialogManager::DialogFactory factory)
175 {
176 registerFactory(g_quark_from_string(name), factory);
177 }
179 /**
180 * Registers a dialog factory function used to create the named dialog.
181 */
182 void DialogManager::registerFactory(GQuark name,
183 DialogManager::DialogFactory factory)
184 {
185 _factory_map[name] = factory;
186 }
188 /**
189 * Fetches the named dialog, creating it if it has not already been
190 * created (assuming a factory has been registered for it).
191 */
192 Dialog *DialogManager::getDialog(gchar const *name) {
193 return getDialog(g_quark_from_string(name));
194 }
196 /**
197 * Fetches the named dialog, creating it if it has not already been
198 * created (assuming a factory has been registered for it).
199 */
200 Dialog *DialogManager::getDialog(GQuark name) {
201 DialogMap::iterator dialog_found;
202 dialog_found = _dialog_map.find(name);
204 Dialog *dialog=NULL;
205 if ( dialog_found != _dialog_map.end() ) {
206 dialog = dialog_found->second;
207 } else {
208 FactoryMap::iterator factory_found;
209 factory_found = _factory_map.find(name);
211 if ( factory_found != _factory_map.end() ) {
212 dialog = factory_found->second();
213 _dialog_map[name] = dialog;
214 }
215 }
217 return dialog;
218 }
220 /**
221 * Shows the named dialog, creating it if necessary.
222 */
223 void DialogManager::showDialog(gchar const *name) {
224 showDialog(g_quark_from_string(name));
225 }
227 /**
228 * Shows the named dialog, creating it if necessary.
229 */
230 void DialogManager::showDialog(GQuark name) {
231 Dialog *dialog=getDialog(name);
232 if (dialog) {
233 dialog->present();
234 }
235 }
237 } // namespace Dialog
238 } // namespace UI
239 } // namespace Inkscape
241 /*
242 Local Variables:
243 mode:c++
244 c-file-style:"stroustrup"
245 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
246 indent-tabs-mode:nil
247 fill-column:99
248 End:
249 */
250 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :