]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
cleanup after svn hang-up, #undef CursorShape. Should be compilable ganin now.
[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         if (open_insets_.find(name) != open_insets_.end())
161                 open_insets_[name] = 0;
162 }
163
164
165 InsetBase * Dialogs::getOpenInset(string const & name) const
166 {
167         if (!isValidName(name))
168                 return 0;
169
170         std::map<string, InsetBase *>::const_iterator it =
171                 open_insets_.find(name);
172         return it == open_insets_.end() ? 0 : it->second;
173 }
174
175
176 void Dialogs::hideAll() const
177 {
178         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
179         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
180
181         for(; it != end; ++it) {
182                 it->second->hide();
183         }
184 }
185
186
187 void Dialogs::hideBufferDependent() const
188 {
189         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
190         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
191
192         for(; it != end; ++it) {
193                 Dialog * dialog =  it->second.get();
194                 if (dialog->controller().isBufferDependent())
195                         dialog->hide();
196         }
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 }
217
218
219 void Dialogs::redraw() const
220 {
221         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
222         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
223
224         for(; it != end; ++it) {
225                 it->second->redraw();
226         }
227 }
228
229
230 void Dialogs::checkStatus()
231 {
232         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
233         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
234
235         for(; it != end; ++it) {
236                 Dialog * const dialog = it->second.get();
237                 if (dialog->isVisible())
238                         dialog->checkStatus();
239         }
240 }