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