]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
Rewrite the MailInset as suggested to Andr��.
[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/bind.hpp>
17
18
19 // Note that static boost signals break some compilers, so this wrapper
20 // initialises the signal dynamically when it is first invoked.
21 template<typename Signal>
22 class BugfixSignal {
23 public:
24         Signal & operator()() { return thesignal(); }
25         Signal const & operator()() const { return thesignal(); }
26
27 private:
28         Signal & thesignal() const
29         {
30                 if (!signal_.get())
31                         signal_.reset(new Signal);
32                 return *signal_;
33         }
34
35         mutable boost::scoped_ptr<Signal> signal_;
36 };
37
38
39 boost::signal0<void> & Dialogs::redrawGUI()
40 {
41         static BugfixSignal<boost::signal0<void> > thesignal;
42         return thesignal();
43 }
44
45
46 boost::signal2<void, string const &, InsetBase*> & Dialogs::hide()
47 {
48         static BugfixSignal<boost::signal2<void, string const &, InsetBase*> >
49                 thesignal;
50         return thesignal();
51 }
52
53
54 Dialogs::Dialogs(LyXView & lyxview)
55         : lyxview_(lyxview)
56 {
57         // Connect signals
58         redrawGUI().connect(boost::bind(&Dialogs::redraw, this));
59         hide().connect(boost::bind(&Dialogs::hideSlot, this, _1, _2));
60
61         // All this is slated to go
62         init_pimpl();
63         // reduce the number of connections needed in
64         // dialogs by a simple connection here.
65         hideAllSignal.connect(hideBufferDependentSignal);
66 }
67
68
69 Dialog * Dialogs::find(string const & name)
70 {
71         if (!isValidName(name))
72                 return 0;
73
74         std::map<string, DialogPtr>::iterator it =
75                 dialogs_.find(name);
76
77         if (it == dialogs_.end()) {
78                 dialogs_[name] = DialogPtr(build(name));
79                 return dialogs_[name].get();
80         }
81
82         return it->second.get();
83 }
84
85
86 void Dialogs::show(string const & name)
87 {
88         Dialog * dialog = find(name);
89         if (!dialog)
90                 return;
91
92         dialog->show();
93 }
94
95
96 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
97 {
98         Dialog * dialog = find(name);
99         if (!dialog)
100                 return;
101
102         dialog->show(data);
103         open_insets_[name] = inset;
104 }
105
106
107 void Dialogs::update(string const & name, string const & data)
108 {
109         Dialog * dialog = find(name);
110         if (!dialog)
111                 return;
112
113         if (dialog->isVisible())
114                 dialog->update(data);
115 }
116
117
118 void Dialogs::hideSlot(string const & name, InsetBase * inset)
119 {
120         Dialog * dialog = find(name);
121         if (!dialog)
122                 return;
123
124         if (inset && inset != getOpenInset(name))
125                 return;
126
127         if (dialog->isVisible())
128                 dialog->hide();
129         open_insets_[name] = 0;
130 }
131
132
133 void Dialogs::disconnect(string const & name)
134 {
135         if (!isValidName(name))
136                 return;
137
138         open_insets_[name] = 0;
139 }
140
141
142 InsetBase * Dialogs::getOpenInset(string const & name) const
143 {
144         if (!isValidName(name))
145                 return 0;
146
147         std::map<string, InsetBase *>::const_iterator it =
148                 open_insets_.find(name);
149         return it == open_insets_.end() ? 0 : it->second;
150 }
151
152
153 void Dialogs::hideAll() const
154 {
155         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
156         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
157
158         for(; it != end; ++it) {
159                 it->second->hide();
160         }
161         hideAllSignal();
162 }
163
164
165 void Dialogs::hideBufferDependent() const
166 {
167         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
168         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
169
170         for(; it != end; ++it) {
171                 Dialog * dialog =  it->second.get();
172                 if (dialog->controller().isBufferDependent())
173                         dialog->hide();
174         }
175         hideBufferDependentSignal();
176 }
177
178
179 void Dialogs::updateBufferDependent(bool switched) const
180 {
181         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
182         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
183
184         for(; it != end; ++it) {
185                 Dialog * dialog =  it->second.get();
186                 if (switched && dialog->controller().isBufferDependent()) {
187                         dialog->hide();
188                 } else {
189                         // A bit clunky, but the dialog will request
190                         // that the kernel provides it with the necessary
191                         // data.
192                         dialog->RestoreButton();
193                 }
194         }
195         updateBufferDependentSignal(switched);
196 }
197
198
199 void Dialogs::redraw() const
200 {
201         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
202         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
203
204         for(; it != end; ++it) {
205                 it->second->redraw();
206         }
207 }