]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
namespace grfx -> lyx::graphics
[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         // FIXME! Should check that the dialog is NOT an inset dialog.
99         dialog->show(data);
100 }
101
102
103 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
104 {
105         Dialog * dialog = find(name);
106         if (!dialog)
107                 return;
108
109         // FIXME! Should check that the dialog IS an inset dialog.
110         dialog->show(data);
111         open_insets_[name] = inset;
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         Dialog * dialog = find(name);
128         if (!dialog)
129                 return;
130
131         if (dialog->isVisible())
132                 dialog->update(data);
133 }
134
135
136 void Dialogs::hideSlot(string const & name, InsetBase * inset)
137 {
138         Dialog * dialog = find(name);
139         if (!dialog)
140                 return;
141
142         if (inset && inset != getOpenInset(name))
143                 return;
144
145         if (dialog->isVisible())
146                 dialog->hide();
147         open_insets_[name] = 0;
148 }
149
150
151 void Dialogs::disconnect(string const & name)
152 {
153         if (!isValidName(name))
154                 return;
155
156         open_insets_[name] = 0;
157 }
158
159
160 InsetBase * Dialogs::getOpenInset(string const & name) const
161 {
162         if (!isValidName(name))
163                 return 0;
164
165         std::map<string, InsetBase *>::const_iterator it =
166                 open_insets_.find(name);
167         return it == open_insets_.end() ? 0 : it->second;
168 }
169
170
171 void Dialogs::hideAll() const
172 {
173         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
174         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
175
176         for(; it != end; ++it) {
177                 it->second->hide();
178         }
179         hideAllSignal();
180 }
181
182
183 void Dialogs::hideBufferDependent() 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                 Dialog * dialog =  it->second.get();
190                 if (dialog->controller().isBufferDependent())
191                         dialog->hide();
192         }
193         hideBufferDependentSignal();
194 }
195
196
197 void Dialogs::updateBufferDependent(bool switched) const
198 {
199         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
200         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
201
202         for(; it != end; ++it) {
203                 Dialog * dialog =  it->second.get();
204                 if (switched && dialog->controller().isBufferDependent()) {
205                         dialog->hide();
206                 } else {
207                         // A bit clunky, but the dialog will request
208                         // that the kernel provides it with the necessary
209                         // data.
210                         dialog->RestoreButton();
211                 }
212         }
213         updateBufferDependentSignal(switched);
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 }