]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.cpp
This patch revert part of the code changed in revision 18825. This is needed because...
[lyx.git] / src / frontends / Dialogs.cpp
1 /**
2  * \file frontends/Dialogs.cpp
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 "callback.h"
18
19 #include "controllers/Dialog.h"
20
21 #include <boost/signal.hpp>
22 #include <boost/bind.hpp>
23
24
25 namespace lyx {
26
27
28 using std::string;
29 using lyx::frontend::Dialog;
30
31
32 // Note that static boost signals break some compilers, so this wrapper
33 // initialises the signal dynamically when it is first invoked.
34 template<typename Signal>
35 class BugfixSignal {
36 public:
37         Signal & operator()() { return thesignal(); }
38         Signal const & operator()() const { return thesignal(); }
39
40 private:
41         Signal & thesignal() const
42         {
43                 if (!signal_.get())
44                         signal_.reset(new Signal);
45                 return *signal_;
46         }
47
48         mutable boost::scoped_ptr<Signal> signal_;
49 };
50
51
52 namespace {
53
54 BugfixSignal<boost::signal<void(string const &, Inset*)> > hideSignal;
55
56 }
57
58
59 void Dialogs::hide(string const & name, Inset* inset)
60 {
61         // Don't send the signal if we are quitting, because on MSVC it is
62         // destructed before the cut stack in CutAndPaste.cpp, and this method
63         // is called from some inset destructor if the cut stack is not empty
64         // on exit.
65         if (!quitting)
66                 hideSignal()(name, inset);
67 }
68
69
70 Dialogs::Dialogs(LyXView & lyxview)
71         : lyxview_(lyxview), in_show_(false)
72 {
73         // Connect signals
74         connection_ = hideSignal().connect(boost::bind(&Dialogs::hideSlot, this, _1, _2));
75 }
76
77 Dialogs::~Dialogs() 
78 {
79         connection_.disconnect();
80 }
81
82 Dialog * Dialogs::find_or_build(string const & name)
83 {
84         if (!isValidName(name))
85                 return 0;
86
87         std::map<string, DialogPtr>::iterator it =
88                 dialogs_.find(name);
89
90         if (it != dialogs_.end())
91                 return it->second.get();
92
93         dialogs_[name] = build(name);
94         return dialogs_[name].get();
95 }
96
97
98 void Dialogs::show(string const & name, string const & data)
99 {
100         if (in_show_) {
101                 return;
102         }
103         in_show_ = true;
104         Dialog * dialog = find_or_build(name);
105         if (dialog) {
106                 // FIXME! Should check that the dialog is NOT an inset dialog.
107                 dialog->show(data);
108         }
109         in_show_ = false;
110 }
111
112
113 void Dialogs::show(string const & name, string const & data, Inset * inset)
114 {
115         if (in_show_) {
116                 return;
117         }
118         in_show_ = true;
119         Dialog * dialog = find_or_build(name);
120         if (dialog) {
121                 // FIXME! Should check that the dialog IS an inset dialog.
122                 dialog->show(data);
123                 open_insets_[name] = inset;
124         }
125         in_show_ = false;
126 }
127
128
129 bool Dialogs::visible(string const & name) const
130 {
131         std::map<string, DialogPtr>::const_iterator it =
132                 dialogs_.find(name);
133         if (it == dialogs_.end())
134                 return false;
135         return it->second.get()->isVisible();
136 }
137
138
139 void Dialogs::update(string const & name, string const & data)
140 {
141         std::map<string, DialogPtr>::const_iterator it =
142                 dialogs_.find(name);
143         if (it == dialogs_.end())
144                 return;
145
146         Dialog * const dialog = it->second.get();
147         if (dialog->isVisible())
148                 dialog->update(data);
149 }
150
151
152 void Dialogs::hideSlot(string const & name, Inset * inset)
153 {
154         std::map<string, DialogPtr>::const_iterator it =
155                 dialogs_.find(name);
156         if (it == dialogs_.end())
157                 return;
158
159         if (inset && inset != getOpenInset(name))
160                 return;
161
162         Dialog * const dialog = it->second.get();
163         if (dialog->isVisible())
164                 dialog->hide();
165         open_insets_[name] = 0;
166 }
167
168
169 void Dialogs::disconnect(string const & name)
170 {
171         if (!isValidName(name))
172                 return;
173
174         if (open_insets_.find(name) != open_insets_.end())
175                 open_insets_[name] = 0;
176 }
177
178
179 Inset * Dialogs::getOpenInset(string const & name) const
180 {
181         if (!isValidName(name))
182                 return 0;
183
184         std::map<string, Inset *>::const_iterator it =
185                 open_insets_.find(name);
186         return it == open_insets_.end() ? 0 : it->second;
187 }
188
189
190 void Dialogs::hideAll() const
191 {
192         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
193         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
194
195         for(; it != end; ++it) {
196                 it->second->hide();
197         }
198 }
199
200
201 void Dialogs::hideBufferDependent() const
202 {
203         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
204         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
205
206         for(; it != end; ++it) {
207                 Dialog * dialog =  it->second.get();
208                 if (dialog->controller().isBufferDependent())
209                         dialog->hide();
210         }
211 }
212
213
214 void Dialogs::updateBufferDependent(bool switched) const
215 {
216         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
217         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
218
219         for(; it != end; ++it) {
220                 Dialog * dialog =  it->second.get();
221                 if (switched && dialog->controller().isBufferDependent()) {
222                         if (dialog->isVisible() && dialog->controller().initialiseParams(""))
223                                 dialog->view().update();
224                         else
225                                 dialog->hide();
226                 } else {
227                         // A bit clunky, but the dialog will request
228                         // that the kernel provides it with the necessary
229                         // data.
230                         dialog->RestoreButton();
231                 }
232         }
233 }
234
235
236 void Dialogs::redraw() const
237 {
238         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
239         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
240
241         for(; it != end; ++it) {
242                 it->second->redraw();
243         }
244 }
245
246
247 void Dialogs::checkStatus()
248 {
249         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
250         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
251
252         for(; it != end; ++it) {
253                 Dialog * const dialog = it->second.get();
254                 if (dialog->isVisible())
255                         dialog->checkStatus();
256         }
257 }
258
259
260 } // namespace lyx