]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
Wrap most of the frontend code up inside namespace lyx::frontend.
[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 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 boost::signal0<void> & Dialogs::redrawGUI()
48 {
49         static BugfixSignal<boost::signal0<void> > thesignal;
50         return thesignal();
51 }
52
53
54 namespace {
55
56 BugfixSignal<boost::signal2<void, string const &, InsetBase*> > hideSignal;
57
58 }
59
60
61 void Dialogs::hide(string const & name, InsetBase* inset)
62 {
63         hideSignal()(name, inset);
64 }
65
66
67 Dialogs::Dialogs(LyXView & lyxview)
68         : lyxview_(lyxview)
69 {
70         // Connect signals
71         redrawGUI().connect(boost::bind(&Dialogs::redraw, this));
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         Dialog * dialog = find_or_build(name);
94         if (!dialog)
95                 return;
96
97         // FIXME! Should check that the dialog is NOT an inset dialog.
98         dialog->show(data);
99 }
100
101
102 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
103 {
104         Dialog * dialog = find_or_build(name);
105         if (!dialog)
106                 return;
107
108         // FIXME! Should check that the dialog IS an inset dialog.
109         dialog->show(data);
110         open_insets_[name] = inset;
111 }
112
113
114 bool Dialogs::visible(string const & name) const
115 {
116         std::map<string, DialogPtr>::const_iterator it =
117                 dialogs_.find(name);
118         if (it == dialogs_.end())
119                 return false;
120         return it->second.get()->isVisible();
121 }
122
123
124 void Dialogs::update(string const & name, string const & data)
125 {
126         std::map<string, DialogPtr>::const_iterator it =
127                 dialogs_.find(name);
128         if (it == dialogs_.end())
129                 return;
130
131         Dialog * const dialog = it->second.get();
132         if (dialog->isVisible())
133                 dialog->update(data);
134 }
135
136
137 void Dialogs::hideSlot(string const & name, InsetBase * inset)
138 {
139         std::map<string, DialogPtr>::const_iterator it =
140                 dialogs_.find(name);
141         if (it == dialogs_.end())
142                 return;
143
144         if (inset && inset != getOpenInset(name))
145                 return;
146
147         Dialog * const dialog = it->second.get();
148         if (dialog->isVisible())
149                 dialog->hide();
150         open_insets_[name] = 0;
151 }
152
153
154 void Dialogs::disconnect(string const & name)
155 {
156         if (!isValidName(name))
157                 return;
158
159         open_insets_[name] = 0;
160 }
161
162
163 InsetBase * Dialogs::getOpenInset(string const & name) const
164 {
165         if (!isValidName(name))
166                 return 0;
167
168         std::map<string, InsetBase *>::const_iterator it =
169                 open_insets_.find(name);
170         return it == open_insets_.end() ? 0 : it->second;
171 }
172
173
174 void Dialogs::hideAll() const
175 {
176         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
177         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
178
179         for(; it != end; ++it) {
180                 it->second->hide();
181         }
182 }
183
184
185 void Dialogs::hideBufferDependent() const
186 {
187         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
188         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
189
190         for(; it != end; ++it) {
191                 Dialog * dialog =  it->second.get();
192                 if (dialog->controller().isBufferDependent())
193                         dialog->hide();
194         }
195 }
196
197
198 void Dialogs::updateBufferDependent(bool switched) const
199 {
200         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
201         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
202
203         for(; it != end; ++it) {
204                 Dialog * dialog =  it->second.get();
205                 if (switched && dialog->controller().isBufferDependent()) {
206                         dialog->hide();
207                 } else {
208                         // A bit clunky, but the dialog will request
209                         // that the kernel provides it with the necessary
210                         // data.
211                         dialog->RestoreButton();
212                 }
213         }
214 }
215
216
217 void Dialogs::redraw() const
218 {
219         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
220         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
221
222         for(; it != end; ++it) {
223                 it->second->redraw();
224         }
225 }