]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
add Dialogs::visible to avoid the madness of filling unexistent dialogs
[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  * \author Angus Leeming
6  *
7  * Full author contact details are available in file CREDITS
8  *
9  * Common to all frontends' Dialogs
10  */
11
12 #include <config.h>
13
14 #include "Dialogs.h"
15 #include "controllers/Dialog.h"
16 #include <boost/signals/signal2.hpp>
17 #include <boost/bind.hpp>
18
19
20 // Note that static boost signals break some compilers, so this wrapper
21 // initialises the signal dynamically when it is first invoked.
22 template<typename Signal>
23 class BugfixSignal {
24 public:
25         Signal & operator()() { return thesignal(); }
26         Signal const & operator()() const { return thesignal(); }
27
28 private:
29         Signal & thesignal() const
30         {
31                 if (!signal_.get())
32                         signal_.reset(new Signal);
33                 return *signal_;
34         }
35
36         mutable boost::scoped_ptr<Signal> signal_;
37 };
38
39
40 boost::signal0<void> & Dialogs::redrawGUI()
41 {
42         static BugfixSignal<boost::signal0<void> > thesignal;
43         return thesignal();
44 }
45
46
47 namespace {
48
49 BugfixSignal<boost::signal2<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)
62 {
63         // Connect signals
64         redrawGUI().connect(boost::bind(&Dialogs::redraw, this));
65         hideSignal().connect(boost::bind(&Dialogs::hideSlot, this, _1, _2));
66
67         // All this is slated to go
68         init_pimpl();
69         // reduce the number of connections needed in
70         // dialogs by a simple connection here.
71         hideAllSignal.connect(hideBufferDependentSignal);
72 }
73
74
75 Dialog * Dialogs::find(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                 dialogs_[name] = DialogPtr(build(name));
85                 return dialogs_[name].get();
86         }
87
88         return it->second.get();
89 }
90
91
92 void Dialogs::show(string const & name, string const & data)
93 {
94         Dialog * dialog = find(name);
95         if (!dialog)
96                 return;
97
98         dialog->show(data);
99 }
100
101
102 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
103 {
104         Dialog * dialog = find(name);
105         if (!dialog)
106                 return;
107
108         dialog->show(data);
109         open_insets_[name] = inset;
110 }
111
112
113 bool Dialogs::visible(string const & name) const
114 {
115         std::map<string, DialogPtr>::const_iterator it =
116                 dialogs_.find(name);
117         if (it == dialogs_.end())
118                 return false;
119         return it->second.get()->isVisible();
120 }
121
122
123 void Dialogs::update(string const & name, string const & data)
124 {
125         Dialog * dialog = find(name);
126         if (!dialog)
127                 return;
128
129         if (dialog->isVisible())
130                 dialog->update(data);
131 }
132
133
134 void Dialogs::hideSlot(string const & name, InsetBase * inset)
135 {
136         Dialog * dialog = find(name);
137         if (!dialog)
138                 return;
139
140         if (inset && inset != getOpenInset(name))
141                 return;
142
143         if (dialog->isVisible())
144                 dialog->hide();
145         open_insets_[name] = 0;
146 }
147
148
149 void Dialogs::disconnect(string const & name)
150 {
151         if (!isValidName(name))
152                 return;
153
154         open_insets_[name] = 0;
155 }
156
157
158 InsetBase * Dialogs::getOpenInset(string const & name) const
159 {
160         if (!isValidName(name))
161                 return 0;
162
163         std::map<string, InsetBase *>::const_iterator it =
164                 open_insets_.find(name);
165         return it == open_insets_.end() ? 0 : it->second;
166 }
167
168
169 void Dialogs::hideAll() const
170 {
171         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
172         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
173
174         for(; it != end; ++it) {
175                 it->second->hide();
176         }
177         hideAllSignal();
178 }
179
180
181 void Dialogs::hideBufferDependent() const
182 {
183         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
184         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
185
186         for(; it != end; ++it) {
187                 Dialog * dialog =  it->second.get();
188                 if (dialog->controller().isBufferDependent())
189                         dialog->hide();
190         }
191         hideBufferDependentSignal();
192 }
193
194
195 void Dialogs::updateBufferDependent(bool switched) const
196 {
197         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
198         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
199
200         for(; it != end; ++it) {
201                 Dialog * dialog =  it->second.get();
202                 if (switched && dialog->controller().isBufferDependent()) {
203                         dialog->hide();
204                 } else {
205                         // A bit clunky, but the dialog will request
206                         // that the kernel provides it with the necessary
207                         // data.
208                         dialog->RestoreButton();
209                 }
210         }
211         updateBufferDependentSignal(switched);
212 }
213
214
215 void Dialogs::redraw() const
216 {
217         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
218         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
219
220         for(; it != end; ++it) {
221                 it->second->redraw();
222         }
223 }