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