]> git.lyx.org Git - lyx.git/blob - src/frontends/Dialogs.cpp
next one
[lyx.git] / src / frontends / Dialogs.cpp
1 /**
2  * \file frontends/Dialogs.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Common to all frontends' Dialogs
11  */
12
13 #include <config.h>
14
15 #include "Dialogs.h"
16
17 #include "controllers/Dialog.h"
18
19 #include <boost/signal.hpp>
20 #include <boost/bind.hpp>
21
22 using std::string;
23
24 namespace lyx {
25
26 extern bool quitting;
27
28 namespace frontend {
29
30 Dialogs::Dialogs(LyXView & lyxview)
31         : lyxview_(lyxview), in_show_(false)
32 {}
33
34
35 Dialog * Dialogs::find_or_build(string const & name)
36 {
37         if (!isValidName(name))
38                 return 0;
39
40         std::map<string, DialogPtr>::iterator it =
41                 dialogs_.find(name);
42
43         if (it != dialogs_.end())
44                 return it->second.get();
45
46         dialogs_[name].reset(build(name));
47         return dialogs_[name].get();
48 }
49
50
51 void Dialogs::show(string const & name, string const & data, Inset * inset)
52 {
53         if (in_show_)
54                 return;
55
56         in_show_ = true;
57         Dialog * dialog = find_or_build(name);
58         if (dialog) {
59                 dialog->showData(data);
60                 if (inset)
61                         open_insets_[name] = inset;
62         }
63         in_show_ = false;
64 }
65
66
67 bool Dialogs::visible(string const & name) const
68 {
69         std::map<string, DialogPtr>::const_iterator it =
70                 dialogs_.find(name);
71         if (it == dialogs_.end())
72                 return false;
73         return it->second.get()->isVisibleView();
74 }
75
76
77 void Dialogs::update(string const & name, string const & data)
78 {
79         std::map<string, DialogPtr>::const_iterator it =
80                 dialogs_.find(name);
81         if (it == dialogs_.end())
82                 return;
83
84         Dialog * const dialog = it->second.get();
85         if (dialog->isVisibleView())
86                 dialog->updateData(data);
87 }
88
89
90 void Dialogs::hide(string const & name, Inset* inset)
91 {
92         // Don't send the signal if we are quitting, because on MSVC it is
93         // destructed before the cut stack in CutAndPaste.cpp, and this method
94         // is called from some inset destructor if the cut stack is not empty
95         // on exit.
96         if (quitting)
97                 return;
98
99         std::map<string, DialogPtr>::const_iterator it =
100                 dialogs_.find(name);
101         if (it == dialogs_.end())
102                 return;
103
104         if (inset && inset != getOpenInset(name))
105                 return;
106
107         Dialog * const dialog = it->second.get();
108         if (dialog->isVisibleView())
109                 dialog->hide();
110         open_insets_[name] = 0;
111 }
112
113
114 void Dialogs::disconnect(string const & name)
115 {
116         if (!isValidName(name))
117                 return;
118
119         if (open_insets_.find(name) != open_insets_.end())
120                 open_insets_[name] = 0;
121 }
122
123
124 Inset * Dialogs::getOpenInset(string const & name) const
125 {
126         if (!isValidName(name))
127                 return 0;
128
129         std::map<string, Inset *>::const_iterator it =
130                 open_insets_.find(name);
131         return it == open_insets_.end() ? 0 : it->second;
132 }
133
134
135 void Dialogs::hideAll() const
136 {
137         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
138         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
139
140         for(; it != end; ++it)
141                 it->second->hide();
142 }
143
144
145 void Dialogs::hideBufferDependent() const
146 {
147         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
148         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
149
150         for(; it != end; ++it) {
151                 Dialog * dialog = it->second.get();
152                 if (dialog->controller().isBufferDependent())
153                         dialog->hide();
154         }
155 }
156
157
158 void Dialogs::updateBufferDependent(bool switched) const
159 {
160         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
161         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
162
163         for(; it != end; ++it) {
164                 Dialog * dialog = it->second.get();
165                 if (switched && dialog->controller().isBufferDependent()) {
166                         if (dialog->isVisibleView() && dialog->controller().initialiseParams(""))
167                                 dialog->updateView();
168                         else
169                                 dialog->hide();
170                 } else {
171                         // A bit clunky, but the dialog will request
172                         // that the kernel provides it with the necessary
173                         // data.
174                         dialog->slotRestore();
175                 }
176         }
177 }
178
179
180 void Dialogs::redraw() const
181 {
182         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
183         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
184
185         for(; it != end; ++it)
186                 it->second->redraw();
187 }
188
189
190 void Dialogs::checkStatus()
191 {
192         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
193         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
194
195         for(; it != end; ++it) {
196                 Dialog * const dialog = it->second.get();
197                 if (dialog && dialog->isVisibleView())
198                         dialog->checkStatus();
199         }
200 }
201
202 } // namespace frontend
203 } // namespace lyx