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 "ui/dialog/print-colors-preview-dialog.h"
44 #include "preferences.h"
46 #ifdef ENABLE_SVG_FONTS
47 #include "ui/dialog/svg-fonts-dialog.h"
48 #endif // ENABLE_SVG_FONTS
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) {
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("PrintColorsPreviewDialog", &create<PrintColorsPreviewDialog, 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("PrintColorsPreviewDialog", &create<PrintColorsPreviewDialog, DockBehavior>);
133 registerFactory("Script", &create<ScriptDialog, DockBehavior>);
134 #ifdef ENABLE_SVG_FONTS
135 registerFactory("SvgFontsDialog", &create<SvgFontsDialog, DockBehavior>);
136 #endif
137 registerFactory("Swatches", &create<SwatchesPanel, DockBehavior>);
138 registerFactory("TileDialog", &create<TileDialog, DockBehavior>);
139 registerFactory("Trace", &create<TraceDialog, DockBehavior>);
140 registerFactory("Transformation", &create<Transformation, DockBehavior>);
141 registerFactory("UndoHistory", &create<UndoHistory, DockBehavior>);
142 registerFactory("InputDevices", &create<InputDialog, DockBehavior>);
144 }
145 }
147 DialogManager::~DialogManager() {
148 // TODO: Disconnect the signals
149 // TODO: Do we need to explicitly delete the dialogs?
150 // Appears to cause a segfault if we do
151 }
154 DialogManager &DialogManager::getInstance()
155 {
156 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
157 int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
159 /* Use singleton behavior for floating dialogs */
160 if (dialogs_type == FLOATING) {
161 static DialogManager *instance = 0;
163 if (!instance)
164 instance = new DialogManager();
165 return *instance;
166 }
168 return *new DialogManager();
169 }
171 /**
172 * Registers a dialog factory function used to create the named dialog.
173 */
174 void DialogManager::registerFactory(gchar const *name,
175 DialogManager::DialogFactory factory)
176 {
177 registerFactory(g_quark_from_string(name), factory);
178 }
180 /**
181 * Registers a dialog factory function used to create the named dialog.
182 */
183 void DialogManager::registerFactory(GQuark name,
184 DialogManager::DialogFactory factory)
185 {
186 _factory_map[name] = factory;
187 }
189 /**
190 * Fetches the named dialog, creating it if it has not already been
191 * created (assuming a factory has been registered for it).
192 */
193 Dialog *DialogManager::getDialog(gchar const *name) {
194 return getDialog(g_quark_from_string(name));
195 }
197 /**
198 * Fetches the named dialog, creating it if it has not already been
199 * created (assuming a factory has been registered for it).
200 */
201 Dialog *DialogManager::getDialog(GQuark name) {
202 DialogMap::iterator dialog_found;
203 dialog_found = _dialog_map.find(name);
205 Dialog *dialog=NULL;
206 if ( dialog_found != _dialog_map.end() ) {
207 dialog = dialog_found->second;
208 } else {
209 FactoryMap::iterator factory_found;
210 factory_found = _factory_map.find(name);
212 if ( factory_found != _factory_map.end() ) {
213 dialog = factory_found->second();
214 _dialog_map[name] = dialog;
215 }
216 }
218 return dialog;
219 }
221 /**
222 * Shows the named dialog, creating it if necessary.
223 */
224 void DialogManager::showDialog(gchar const *name) {
225 showDialog(g_quark_from_string(name));
226 }
228 /**
229 * Shows the named dialog, creating it if necessary.
230 */
231 void DialogManager::showDialog(GQuark name) {
232 Dialog *dialog=getDialog(name);
233 if (dialog) {
234 dialog->present();
235 }
236 }
238 } // namespace Dialog
239 } // namespace UI
240 } // namespace Inkscape
242 /*
243 Local Variables:
244 mode:c++
245 c-file-style:"stroustrup"
246 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
247 indent-tabs-mode:nil
248 fill-column:99
249 End:
250 */
251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :