]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.C
Port the About LyX dialog to the Dialog scheme.
[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 "controllers/ButtonControllerBase.h"
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 Dialogs::Dialogs(LyXView & lyxview)
48         : lyxview_(lyxview)
49 {
50         // Connect signals
51         redrawGUI().connect(boost::bind(&Dialogs::redraw, this));
52
53         // All this is slated to go
54         init_pimpl();
55         // reduce the number of connections needed in
56         // dialogs by a simple connection here.
57         hideAllSignal.connect(hideBufferDependentSignal);
58 }
59
60
61 Dialog * Dialogs::find(string const & name)
62 {
63         if (!isValidName(name))
64                 return 0;
65
66         std::map<string, DialogPtr>::iterator it =
67                 dialogs_.find(name);
68
69         if (it == dialogs_.end()) {
70                 dialogs_[name] = DialogPtr(build(name));
71                 return dialogs_[name].get();
72         }
73
74         return it->second.get();
75 }
76
77
78 void Dialogs::show(string const & name)
79 {
80         Dialog * dialog = find(name);
81         if (!dialog)
82                 return;
83
84         dialog->show();
85 }
86
87
88 void Dialogs::show(string const & name, string const & data, InsetBase * inset)
89 {
90         Dialog * dialog = find(name);
91         if (!dialog)
92                 return;
93
94         dialog->show(data);
95         open_insets_[name] = inset;
96 }
97
98
99 void Dialogs::update(string const & name, string const & data)
100 {
101         Dialog * dialog = find(name);
102         if (!dialog)
103                 return;
104
105         if (dialog->isVisible())
106                 dialog->update(data);
107 }
108
109
110 void Dialogs::hide(string const & name)
111 {
112         Dialog * dialog = find(name);
113         if (!dialog)
114                 return;
115
116         if (dialog->isVisible())
117                 dialog->hide();
118         open_insets_[name] = 0;
119 }
120
121
122 void Dialogs::disconnect(string const & name)
123 {
124         if (!isValidName(name))
125                 return;
126
127         open_insets_[name] = 0;
128 }
129
130
131 InsetBase * Dialogs::getOpenInset(string const & name) const
132 {
133         if (!isValidName(name))
134                 return 0;
135
136         std::map<string, InsetBase *>::const_iterator it =
137                 open_insets_.find(name);
138         return it == open_insets_.end() ? 0 : it->second;
139 }
140
141
142 void Dialogs::hideAll() const
143 {
144         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
145         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
146
147         for(; it != end; ++it) {
148                 it->second->hide();
149         }
150         hideAllSignal();
151 }
152
153
154 void Dialogs::hideBufferDependent() const
155 {
156         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
157         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
158
159         for(; it != end; ++it) {
160                 Dialog * dialog =  it->second.get();
161                 if (dialog->controller().isBufferDependent())
162                         dialog->hide();
163         }
164         hideBufferDependentSignal();
165 }
166
167
168 void Dialogs::updateBufferDependent(bool switched) const
169 {
170         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
171         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
172
173         for(; it != end; ++it) {
174                 Dialog * dialog =  it->second.get();
175                 if (switched && dialog->controller().isBufferDependent()) {
176                         dialog->hide();
177                 } else {
178                         // A bit clunky, but the dialog will request
179                         // that the kernel provides it with the necessary
180                         // data.
181                         dialog->RestoreButton();
182                 }
183         }
184         updateBufferDependentSignal(switched);
185 }
186
187
188 void Dialogs::redraw() const
189 {
190         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
191         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
192
193         for(; it != end; ++it) {
194                 it->second->redraw();
195         }
196 }