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