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 #include "ui/dialog/tile.h"
35 #include "ui/dialog/tracedialog.h"
36 #include "ui/dialog/transformation.h"
37 #include "ui/dialog/undo-history.h"
38 #include "ui/dialog/panel-dialog.h"
39 #include "ui/dialog/layers.h"
40 #include "ui/dialog/icon-preview.h"
41 #include "ui/dialog/floating-behavior.h"
42 #include "ui/dialog/dock-behavior.h"
43 #include "preferences.h"
45 #ifdef ENABLE_SVG_FONTS
46 #include "ui/dialog/svg-fonts-dialog.h"
47 #endif // ENABLE_SVG_FONTS
49 namespace Inkscape {
50 namespace UI {
51 namespace Dialog {
53 namespace {
55 using namespace Behavior;
57 template <typename T, typename B>
58 inline Dialog *create() { return PanelDialog<B>::template create<T>(); }
60 }
62 /**
63 * This class is provided as a container for Inkscape's various
64 * dialogs. This allows Inkscape::Application to treat the various
65 * dialogs it invokes, as abstractions.
66 *
67 * DialogManager is essentially a cache of dialogs. It lets us
68 * initialize dialogs lazily - instead of constructing them during
69 * application startup, they're constructed the first time they're
70 * actually invoked by Inkscape::Application. The constructed
71 * dialog is held here after that, so future invokations of the
72 * dialog don't need to get re-constructed each time. The memory for
73 * the dialogs are then reclaimed when the DialogManager is destroyed.
74 *
75 * In addition, DialogManager also serves as a signal manager for
76 * dialogs. It provides a set of signals that can be sent to all
77 * dialogs for doing things such as hiding/unhiding them, etc.
78 * DialogManager ensures that every dialog it handles will listen
79 * to these signals.
80 *
81 */
82 DialogManager::DialogManager() {
84 using namespace Behavior;
85 using namespace Inkscape::UI::Dialogs; // temporary
87 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
88 int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
90 if (dialogs_type == FLOATING) {
92 registerFactory("AlignAndDistribute", &create<AlignAndDistribute, FloatingBehavior>);
93 registerFactory("DocumentMetadata", &create<DocumentMetadata, FloatingBehavior>);
94 registerFactory("DocumentProperties", &create<DocumentProperties, FloatingBehavior>);
95 registerFactory("ExtensionEditor", &create<ExtensionEditor, FloatingBehavior>);
96 registerFactory("FillAndStroke", &create<FillAndStroke, FloatingBehavior>);
97 registerFactory("FilterEffectsDialog", &create<FilterEffectsDialog, FloatingBehavior>);
98 registerFactory("Find", &create<Find, FloatingBehavior>);
99 registerFactory("IconPreviewPanel", &create<IconPreviewPanel, FloatingBehavior>);
100 registerFactory("InkscapePreferences", &create<InkscapePreferences, FloatingBehavior>);
101 registerFactory("LayersPanel", &create<LayersPanel, FloatingBehavior>);
102 registerFactory("LivePathEffect", &create<LivePathEffectEditor, FloatingBehavior>);
103 registerFactory("Memory", &create<Memory, FloatingBehavior>);
104 registerFactory("Messages", &create<Messages, FloatingBehavior>);
105 registerFactory("Script", &create<ScriptDialog, FloatingBehavior>);
106 #ifdef ENABLE_SVG_FONTS
107 registerFactory("SvgFontsDialog", &create<SvgFontsDialog, FloatingBehavior>);
108 #endif
109 registerFactory("Swatches", &create<SwatchesPanel, FloatingBehavior>);
110 registerFactory("TileDialog", &create<TileDialog, FloatingBehavior>);
111 registerFactory("Trace", &create<TraceDialog, FloatingBehavior>);
112 registerFactory("Transformation", &create<Transformation, FloatingBehavior>);
113 registerFactory("UndoHistory", &create<UndoHistory, FloatingBehavior>);
114 registerFactory("InputDevices", &create<InputDialog, FloatingBehavior>);
116 } else {
118 registerFactory("AlignAndDistribute", &create<AlignAndDistribute, DockBehavior>);
119 registerFactory("DocumentMetadata", &create<DocumentMetadata, DockBehavior>);
120 registerFactory("DocumentProperties", &create<DocumentProperties, DockBehavior>);
121 registerFactory("ExtensionEditor", &create<ExtensionEditor, DockBehavior>);
122 registerFactory("FillAndStroke", &create<FillAndStroke, DockBehavior>);
123 registerFactory("FilterEffectsDialog", &create<FilterEffectsDialog, DockBehavior>);
124 registerFactory("Find", &create<Find, DockBehavior>);
125 registerFactory("IconPreviewPanel", &create<IconPreviewPanel, DockBehavior>);
126 registerFactory("InkscapePreferences", &create<InkscapePreferences, DockBehavior>);
127 registerFactory("LayersPanel", &create<LayersPanel, DockBehavior>);
128 registerFactory("LivePathEffect", &create<LivePathEffectEditor, DockBehavior>);
129 registerFactory("Memory", &create<Memory, DockBehavior>);
130 registerFactory("Messages", &create<Messages, DockBehavior>);
131 registerFactory("Script", &create<ScriptDialog, DockBehavior>);
132 #ifdef ENABLE_SVG_FONTS
133 registerFactory("SvgFontsDialog", &create<SvgFontsDialog, DockBehavior>);
134 #endif
135 registerFactory("Swatches", &create<SwatchesPanel, DockBehavior>);
136 registerFactory("TileDialog", &create<TileDialog, DockBehavior>);
137 registerFactory("Trace", &create<TraceDialog, DockBehavior>);
138 registerFactory("Transformation", &create<Transformation, DockBehavior>);
139 registerFactory("UndoHistory", &create<UndoHistory, DockBehavior>);
140 registerFactory("InputDevices", &create<InputDialog, DockBehavior>);
142 }
143 }
145 DialogManager::~DialogManager() {
146 // TODO: Disconnect the signals
147 // TODO: Do we need to explicitly delete the dialogs?
148 // Appears to cause a segfault if we do
149 }
152 DialogManager &DialogManager::getInstance()
153 {
154 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
155 int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
157 /* Use singleton behavior for floating dialogs */
158 if (dialogs_type == FLOATING) {
159 static DialogManager *instance = 0;
161 if (!instance)
162 instance = new DialogManager();
163 return *instance;
164 }
166 return *new DialogManager();
167 }
169 /**
170 * Registers a dialog factory function used to create the named dialog.
171 */
172 void DialogManager::registerFactory(gchar const *name,
173 DialogManager::DialogFactory factory)
174 {
175 registerFactory(g_quark_from_string(name), factory);
176 }
178 /**
179 * Registers a dialog factory function used to create the named dialog.
180 */
181 void DialogManager::registerFactory(GQuark name,
182 DialogManager::DialogFactory factory)
183 {
184 _factory_map[name] = factory;
185 }
187 /**
188 * Fetches the named dialog, creating it if it has not already been
189 * created (assuming a factory has been registered for it).
190 */
191 Dialog *DialogManager::getDialog(gchar const *name) {
192 return getDialog(g_quark_from_string(name));
193 }
195 /**
196 * Fetches the named dialog, creating it if it has not already been
197 * created (assuming a factory has been registered for it).
198 */
199 Dialog *DialogManager::getDialog(GQuark name) {
200 DialogMap::iterator dialog_found;
201 dialog_found = _dialog_map.find(name);
203 Dialog *dialog=NULL;
204 if ( dialog_found != _dialog_map.end() ) {
205 dialog = dialog_found->second;
206 } else {
207 FactoryMap::iterator factory_found;
208 factory_found = _factory_map.find(name);
210 if ( factory_found != _factory_map.end() ) {
211 dialog = factory_found->second();
212 _dialog_map[name] = dialog;
213 }
214 }
216 return dialog;
217 }
219 /**
220 * Shows the named dialog, creating it if necessary.
221 */
222 void DialogManager::showDialog(gchar const *name) {
223 showDialog(g_quark_from_string(name));
224 }
226 /**
227 * Shows the named dialog, creating it if necessary.
228 */
229 void DialogManager::showDialog(GQuark name) {
230 Dialog *dialog=getDialog(name);
231 if (dialog) {
232 dialog->present();
233 }
234 }
236 } // namespace Dialog
237 } // namespace UI
238 } // namespace Inkscape
240 /*
241 Local Variables:
242 mode:c++
243 c-file-style:"stroustrup"
244 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
245 indent-tabs-mode:nil
246 fill-column:99
247 End:
248 */
249 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :