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