]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
The std::string mammoth path.
[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         hideSignal().connect(boost::bind(&Dialogs::hideSlot, this, _1, _2));
72
73         // All this is slated to go
74         init_pimpl();
75         // reduce the number of connections needed in
76         // dialogs by a simple connection here.
77         hideAllSignal.connect(hideBufferDependentSignal);
78 }
79
80
81 Dialog * Dialogs::find_or_build(string const & name)
82 {
83         if (!isValidName(name))
84                 return 0;
85
86         std::map<string, DialogPtr>::iterator it =
87                 dialogs_.find(name);
88
89         if (it != dialogs_.end())
90                 return it->second.get();
91
92         dialogs_[name] = DialogPtr(build(name));
93         return dialogs_[name].get();
94 }
95
96
97 void Dialogs::show(string const & name, string const & data)
98 {
99         Dialog * dialog = find_or_build(name);
100         if (!dialog)
101                 return;
102
103         // FIXME! Should check that the dialog is NOT an inset dialog.
104         dialog->show(data);
105 }
106
107
108 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
109 {
110         Dialog * dialog = find_or_build(name);
111         if (!dialog)
112                 return;
113
114         // FIXME! Should check that the dialog IS an inset dialog.
115         dialog->show(data);
116         open_insets_[name] = inset;
117 }
118
119
120 bool Dialogs::visible(string const & name) const
121 {
122         std::map<string, DialogPtr>::const_iterator it =
123                 dialogs_.find(name);
124         if (it == dialogs_.end())
125                 return false;
126         return it->second.get()->isVisible();
127 }
128
129
130 void Dialogs::update(string const & name, string const & data)
131 {
132         std::map<string, DialogPtr>::const_iterator it =
133                 dialogs_.find(name);
134         if (it == dialogs_.end())
135                 return;
136
137         Dialog * const dialog = it->second.get();
138         if (dialog->isVisible())
139                 dialog->update(data);
140 }
141
142
143 void Dialogs::hideSlot(string const & name, InsetBase * inset)
144 {
145         std::map<string, DialogPtr>::const_iterator it =
146                 dialogs_.find(name);
147         if (it == dialogs_.end())
148                 return;
149
150         if (inset && inset != getOpenInset(name))
151                 return;
152
153         Dialog * const dialog = it->second.get();
154         if (dialog->isVisible())
155                 dialog->hide();
156         open_insets_[name] = 0;
157 }
158
159
160 void Dialogs::disconnect(string const & name)
161 {
162         if (!isValidName(name))
163                 return;
164
165         open_insets_[name] = 0;
166 }
167
168
169 InsetBase * Dialogs::getOpenInset(string const & name) const
170 {
171         if (!isValidName(name))
172                 return 0;
173
174         std::map<string, InsetBase *>::const_iterator it =
175                 open_insets_.find(name);
176         return it == open_insets_.end() ? 0 : it->second;
177 }
178
179
180 void Dialogs::hideAll() const
181 {
182         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
183         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
184
185         for(; it != end; ++it) {
186                 it->second->hide();
187         }
188         hideAllSignal();
189 }
190
191
192 void Dialogs::hideBufferDependent() const
193 {
194         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
195         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
196
197         for(; it != end; ++it) {
198                 Dialog * dialog =  it->second.get();
199                 if (dialog->controller().isBufferDependent())
200                         dialog->hide();
201         }
202         hideBufferDependentSignal();
203 }
204
205
206 void Dialogs::updateBufferDependent(bool switched) const
207 {
208         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
209         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
210
211         for(; it != end; ++it) {
212                 Dialog * dialog =  it->second.get();
213                 if (switched && dialog->controller().isBufferDependent()) {
214                         dialog->hide();
215                 } else {
216                         // A bit clunky, but the dialog will request
217                         // that the kernel provides it with the necessary
218                         // data.
219                         dialog->RestoreButton();
220                 }
221         }
222         updateBufferDependentSignal(switched);
223 }
224
225
226 void Dialogs::redraw() const
227 {
228         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
229         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
230
231         for(; it != end; ++it) {
232                 it->second->redraw();
233         }
234 }