]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
Hold on to your hats.
[lyx.git] / src / frontends / Dialogs.C
1 /**
2  * \file frontends/Dialogs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  * \author Angus Leeming
6  *
7  * Full author contact details are available in file CREDITS
8  *
9  * Common to all frontends' Dialogs
10  */
11
12 #include <config.h>
13
14 #include "Dialogs.h"
15 #include "controllers/Dialog.h"
16 #include "controllers/ButtonControllerBase.h"
17 #include <boost/bind.hpp>
18
19
20 // Note that static boost signals break some compilers, so this wrapper
21 // initialises the signal dynamically when it is first invoked.
22 template<typename Signal>
23 class BugfixSignal {
24 public:
25         Signal & operator()() { return thesignal(); }
26         Signal const & operator()() const { return thesignal(); }
27
28 private:
29         Signal & thesignal() const
30         {
31                 if (!signal_.get())
32                         signal_.reset(new Signal);
33                 return *signal_;
34         }
35
36         mutable boost::scoped_ptr<Signal> signal_;
37 };
38
39
40 boost::signal0<void> & Dialogs::redrawGUI()
41 {
42         static BugfixSignal<boost::signal0<void> > thesignal;
43         return thesignal();
44 }
45
46
47 Dialogs::Dialogs(LyXView & lyxview)
48         : lyxview_(lyxview)
49 {
50         // Connect signals
51         redrawGUI().connect(boost::bind(&Dialogs::redraw, this));
52
53         // All this is slated to go
54         init_pimpl();
55         // reduce the number of connections needed in
56         // dialogs by a simple connection here.
57         hideAllSignal.connect(hideBufferDependentSignal);
58 }
59
60
61 Dialog * Dialogs::find(string const & name)
62 {
63         if (!isValidName(name))
64                 return 0;
65
66         std::map<string, DialogPtr>::iterator it =
67                 dialogs_.find(name);
68
69         if (it == dialogs_.end()) {
70                 dialogs_[name] = DialogPtr(build(name));
71                 return dialogs_[name].get();
72         }
73
74         return it->second.get();
75 }
76
77
78 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
79 {
80         Dialog * dialog = find(name);
81         if (!dialog)
82                 return;
83
84         dialog->show(data);
85         open_insets_[name] = inset;
86 }
87
88
89 void Dialogs::update(string const & name, string const & data)
90 {
91         Dialog * dialog = find(name);
92         if (!dialog)
93                 return;
94
95         if (dialog->isVisible())
96                 dialog->update(data);
97 }
98
99
100 void Dialogs::hide(string const & name)
101 {
102         Dialog * dialog = find(name);
103         if (!dialog)
104                 return;
105
106         if (dialog->isVisible())
107                 dialog->hide();
108         open_insets_[name] = 0;
109 }
110
111
112 void Dialogs::disconnect(string const & name)
113 {
114         if (!isValidName(name))
115                 return;
116
117         open_insets_[name] = 0;
118 }
119
120
121 InsetBase * Dialogs::getOpenInset(string const & name) const
122 {
123         if (!isValidName(name))
124                 return 0;
125
126         std::map<string, InsetBase *>::const_iterator it =
127                 open_insets_.find(name);
128         return it == open_insets_.end() ? 0 : it->second;
129 }
130
131
132 void Dialogs::hideAll() const
133 {
134         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
135         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
136
137         for(; it != end; ++it) {
138                 it->second->hide();
139         }
140         hideAllSignal();
141 }
142
143
144 void Dialogs::hideBufferDependent() const
145 {
146         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
147         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
148
149         for(; it != end; ++it) {
150                 Dialog * dialog =  it->second.get();
151                 if (dialog->controller().isBufferDependent())
152                         dialog->hide();
153         }
154         hideBufferDependentSignal();
155 }
156
157
158 void Dialogs::updateBufferDependent(bool switched) const
159 {
160         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
161         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
162
163         for(; it != end; ++it) {
164                 Dialog * dialog =  it->second.get();
165                 if (switched && dialog->controller().isBufferDependent()) {
166                         dialog->hide();
167                 } else {
168                         // A bit clunky, but the dialog will request
169                         // that the kernel provides it with the necessary
170                         // data.
171                         dialog->RestoreButton();
172                 }
173         }
174         updateBufferDependentSignal(switched);
175 }
176
177
178 void Dialogs::redraw() const
179 {
180         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
181         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
182
183         for(; it != end; ++it) {
184                 it->second->redraw();
185         }
186 }