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