Code

ff31c91c4910178b544ec84608ff7f76fa59037e
[inkscape.git] / src / ui / dialog / dialog-manager.cpp
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/glyphs.h"
29 #include "ui/dialog/inkscape-preferences.h"
30 #include "ui/dialog/input.h"
31 #include "ui/dialog/livepatheffect-editor.h"
32 #include "ui/dialog/memory.h"
33 #include "ui/dialog/messages.h"
34 #include "ui/dialog/scriptdialog.h"
35 #include "ui/dialog/tile.h"
36 #include "ui/dialog/tracedialog.h"
37 #include "ui/dialog/transformation.h"
38 #include "ui/dialog/undo-history.h"
39 #include "ui/dialog/panel-dialog.h"
40 #include "ui/dialog/layers.h"
41 #include "ui/dialog/icon-preview.h"
42 #include "ui/dialog/floating-behavior.h"
43 #include "ui/dialog/dock-behavior.h"
44 //#include "ui/dialog/print-colors-preview-dialog.h"
45 #include "preferences.h"
47 #ifdef ENABLE_SVG_FONTS
48 #include "ui/dialog/svg-fonts-dialog.h"
49 #endif // ENABLE_SVG_FONTS
51 namespace Inkscape {
52 namespace UI {
53 namespace Dialog {
55 namespace {
57 using namespace Behavior;
59 template <typename T, typename B>
60 inline Dialog *create() { return PanelDialog<B>::template create<T>(); }
62 }
64 /**
65  *  This class is provided as a container for Inkscape's various
66  *  dialogs.  This allows Inkscape::Application to treat the various
67  *  dialogs it invokes, as abstractions.
68  *
69  *  DialogManager is essentially a cache of dialogs.  It lets us
70  *  initialize dialogs lazily - instead of constructing them during
71  *  application startup, they're constructed the first time they're
72  *  actually invoked by Inkscape::Application.  The constructed
73  *  dialog is held here after that, so future invokations of the
74  *  dialog don't need to get re-constructed each time.  The memory for
75  *  the dialogs are then reclaimed when the DialogManager is destroyed.
76  *
77  *  In addition, DialogManager also serves as a signal manager for
78  *  dialogs.  It provides a set of signals that can be sent to all
79  *  dialogs for doing things such as hiding/unhiding them, etc.
80  *  DialogManager ensures that every dialog it handles will listen
81  *  to these signals.
82  *
83  */
84 DialogManager::DialogManager() {
86     using namespace Behavior;
87     using namespace Inkscape::UI::Dialogs; // temporary
89     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
90     int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
92     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("Glyphs",              &create<GlyphsPanel,          FloatingBehavior>);
101         registerFactory("IconPreviewPanel",    &create<IconPreviewPanel,     FloatingBehavior>);
102         registerFactory("InkscapePreferences", &create<InkscapePreferences,  FloatingBehavior>);
103         registerFactory("LayersPanel",         &create<LayersPanel,          FloatingBehavior>);
104         registerFactory("LivePathEffect",      &create<LivePathEffectEditor, FloatingBehavior>);
105         registerFactory("Memory",              &create<Memory,               FloatingBehavior>);
106         registerFactory("Messages",            &create<Messages,             FloatingBehavior>);
107 //        registerFactory("PrintColorsPreviewDialog",      &create<PrintColorsPreviewDialog,       FloatingBehavior>);
108         registerFactory("Script",              &create<ScriptDialog,         FloatingBehavior>);
109 #ifdef ENABLE_SVG_FONTS
110         registerFactory("SvgFontsDialog",      &create<SvgFontsDialog,       FloatingBehavior>);
111 #endif
112         registerFactory("Swatches",            &create<SwatchesPanel,        FloatingBehavior>);
113         registerFactory("TileDialog",          &create<TileDialog,           FloatingBehavior>);
114         registerFactory("Trace",               &create<TraceDialog,          FloatingBehavior>);
115         registerFactory("Transformation",      &create<Transformation,       FloatingBehavior>);
116         registerFactory("UndoHistory",         &create<UndoHistory,          FloatingBehavior>);
117         registerFactory("InputDevices",        &create<InputDialog,          FloatingBehavior>);
119     } else {
121         registerFactory("AlignAndDistribute",  &create<AlignAndDistribute,   DockBehavior>);
122         registerFactory("DocumentMetadata",    &create<DocumentMetadata,     DockBehavior>);
123         registerFactory("DocumentProperties",  &create<DocumentProperties,   DockBehavior>);
124         registerFactory("ExtensionEditor",     &create<ExtensionEditor,      DockBehavior>);
125         registerFactory("FillAndStroke",       &create<FillAndStroke,        DockBehavior>);
126         registerFactory("FilterEffectsDialog", &create<FilterEffectsDialog,  DockBehavior>);
127         registerFactory("Find",                &create<Find,                 DockBehavior>);
128         registerFactory("Glyphs",              &create<GlyphsPanel,          DockBehavior>);
129         registerFactory("IconPreviewPanel",    &create<IconPreviewPanel,     DockBehavior>);
130         registerFactory("InkscapePreferences", &create<InkscapePreferences,  DockBehavior>);
131         registerFactory("LayersPanel",         &create<LayersPanel,          DockBehavior>);
132         registerFactory("LivePathEffect",      &create<LivePathEffectEditor, DockBehavior>);
133         registerFactory("Memory",              &create<Memory,               DockBehavior>);
134         registerFactory("Messages",            &create<Messages,             DockBehavior>);
135 //        registerFactory("PrintColorsPreviewDialog",      &create<PrintColorsPreviewDialog,       DockBehavior>);
136         registerFactory("Script",              &create<ScriptDialog,         DockBehavior>);
137 #ifdef ENABLE_SVG_FONTS
138         registerFactory("SvgFontsDialog",      &create<SvgFontsDialog,       DockBehavior>);
139 #endif
140         registerFactory("Swatches",            &create<SwatchesPanel,        DockBehavior>);
141         registerFactory("TileDialog",          &create<TileDialog,           DockBehavior>);
142         registerFactory("Trace",               &create<TraceDialog,          DockBehavior>);
143         registerFactory("Transformation",      &create<Transformation,       DockBehavior>);
144         registerFactory("UndoHistory",         &create<UndoHistory,          DockBehavior>);
145         registerFactory("InputDevices",        &create<InputDialog,          DockBehavior>);
147     }
150 DialogManager::~DialogManager() {
151     // TODO:  Disconnect the signals
152     // TODO:  Do we need to explicitly delete the dialogs?
153     //        Appears to cause a segfault if we do
157 DialogManager &DialogManager::getInstance()
159     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
160     int dialogs_type = prefs->getIntLimited("/options/dialogtype/value", DOCK, 0, 1);
162     /* Use singleton behavior for floating dialogs */
163     if (dialogs_type == FLOATING) {
164         static DialogManager *instance = 0;
166         if (!instance)
167             instance = new DialogManager();
168         return *instance;
169     }
171     return *new DialogManager();
174 /**
175  * Registers a dialog factory function used to create the named dialog.
176  */
177 void DialogManager::registerFactory(gchar const *name,
178                                     DialogManager::DialogFactory factory)
180     registerFactory(g_quark_from_string(name), factory);
183 /**
184  * Registers a dialog factory function used to create the named dialog.
185  */
186 void DialogManager::registerFactory(GQuark name,
187                                     DialogManager::DialogFactory factory)
189     _factory_map[name] = factory;
192 /**
193  * Fetches the named dialog, creating it if it has not already been
194  * created (assuming a factory has been registered for it).
195  */
196 Dialog *DialogManager::getDialog(gchar const *name) {
197     return getDialog(g_quark_from_string(name));
200 /**
201  * Fetches the named dialog, creating it if it has not already been
202  * created (assuming a factory has been registered for it).
203  */
204 Dialog *DialogManager::getDialog(GQuark name) {
205     DialogMap::iterator dialog_found;
206     dialog_found = _dialog_map.find(name);
208     Dialog *dialog=NULL;
209     if ( dialog_found != _dialog_map.end() ) {
210         dialog = dialog_found->second;
211     } else {
212         FactoryMap::iterator factory_found;
213         factory_found = _factory_map.find(name);
215         if ( factory_found != _factory_map.end() ) {
216             dialog = factory_found->second();
217             _dialog_map[name] = dialog;
218         }
219     }
221     return dialog;
224 /**
225  * Shows the named dialog, creating it if necessary.
226  */
227 void DialogManager::showDialog(gchar const *name) {
228     showDialog(g_quark_from_string(name));
231 /**
232  * Shows the named dialog, creating it if necessary.
233  */
234 void DialogManager::showDialog(GQuark name) {
235     Dialog *dialog=getDialog(name);
236     if (dialog) {
237         dialog->present();
238     }
241 } // namespace Dialog
242 } // namespace UI
243 } // namespace Inkscape
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:fileencoding=utf-8:textwidth=99 :