]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialogs.cpp
4be491f48dd0b182764ff9f077fea86dbb57cf17
[lyx.git] / src / frontends / qt4 / Dialogs.cpp
1 /**
2  * \file qt4/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
11 #include <config.h>
12
13 #include "Dialogs.h"
14 #include "Dialog.h"
15
16 #include <boost/assert.hpp>
17
18 using std::string;
19
20 namespace lyx {
21
22 extern bool quitting;
23
24 namespace frontend {
25
26 Dialogs::Dialogs(LyXView & lyxview)
27         : lyxview_(lyxview), in_show_(false)
28 {}
29
30
31 Dialog * Dialogs::find_or_build(string const & name)
32 {
33         if (!isValidName(name))
34                 return 0;
35
36         std::map<string, DialogPtr>::iterator it =
37                 dialogs_.find(name);
38
39         if (it != dialogs_.end())
40                 return it->second.get();
41
42         dialogs_[name].reset(build(name));
43         return dialogs_[name].get();
44 }
45
46
47 void Dialogs::show(string const & name, string const & data, Inset * inset)
48 {
49         if (in_show_)
50                 return;
51
52         in_show_ = true;
53         Dialog * dialog = find_or_build(name);
54         if (dialog) {
55                 dialog->showData(data);
56                 if (inset)
57                         open_insets_[name] = inset;
58         }
59         in_show_ = false;
60 }
61
62
63 bool Dialogs::visible(string const & name) const
64 {
65         std::map<string, DialogPtr>::const_iterator it = dialogs_.find(name);
66         if (it == dialogs_.end())
67                 return false;
68         return it->second.get()->isVisibleView();
69 }
70
71
72 void Dialogs::update(string const & name, string const & data)
73 {
74         std::map<string, DialogPtr>::const_iterator it = dialogs_.find(name);
75         if (it == dialogs_.end())
76                 return;
77
78         Dialog * const dialog = it->second.get();
79         if (dialog->isVisibleView())
80                 dialog->updateData(data);
81 }
82
83
84 void Dialogs::hide(string const & name, Inset* inset)
85 {
86         // Don't send the signal if we are quitting, because on MSVC it is
87         // destructed before the cut stack in CutAndPaste.cpp, and this method
88         // is called from some inset destructor if the cut stack is not empty
89         // on exit.
90         if (quitting)
91                 return;
92
93         std::map<string, DialogPtr>::const_iterator it =
94                 dialogs_.find(name);
95         if (it == dialogs_.end())
96                 return;
97
98         if (inset && inset != getOpenInset(name))
99                 return;
100
101         Dialog * const dialog = it->second.get();
102         if (dialog->isVisibleView())
103                 dialog->hide();
104         open_insets_[name] = 0;
105 }
106
107
108 void Dialogs::disconnect(string const & name)
109 {
110         if (!isValidName(name))
111                 return;
112
113         if (open_insets_.find(name) != open_insets_.end())
114                 open_insets_[name] = 0;
115 }
116
117
118 Inset * Dialogs::getOpenInset(string const & name) const
119 {
120         if (!isValidName(name))
121                 return 0;
122
123         std::map<string, Inset *>::const_iterator it =
124                 open_insets_.find(name);
125         return it == open_insets_.end() ? 0 : it->second;
126 }
127
128
129 void Dialogs::hideAll() const
130 {
131         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
132         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
133
134         for(; it != end; ++it)
135                 it->second->hide();
136 }
137
138
139 void Dialogs::hideBufferDependent() const
140 {
141         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
142         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
143
144         for(; it != end; ++it) {
145                 Dialog * dialog = it->second.get();
146                 if (dialog->isBufferDependent())
147                         dialog->hide();
148         }
149 }
150
151
152 void Dialogs::updateBufferDependent(bool switched) const
153 {
154         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
155         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
156
157         for(; it != end; ++it) {
158                 Dialog * dialog = it->second.get();
159                 if (switched && dialog->isBufferDependent()) {
160                         if (dialog->isVisibleView() && dialog->initialiseParams(""))
161                                 dialog->updateView();
162                         else
163                                 dialog->hide();
164                 } else {
165                         // A bit clunky, but the dialog will request
166                         // that the kernel provides it with the necessary
167                         // data.
168                         dialog->slotRestore();
169                 }
170         }
171 }
172
173
174 void Dialogs::redraw() const
175 {
176         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
177         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
178
179         for(; it != end; ++it)
180                 it->second->redraw();
181 }
182
183
184 void Dialogs::checkStatus()
185 {
186         std::map<string, DialogPtr>::const_iterator it  = dialogs_.begin();
187         std::map<string, DialogPtr>::const_iterator end = dialogs_.end();
188
189         for(; it != end; ++it) {
190                 Dialog * const dialog = it->second.get();
191                 if (dialog && dialog->isVisibleView())
192                         dialog->checkStatus();
193         }
194 }
195
196
197 namespace {
198
199 // This list should be kept in sync with the list of insets in
200 // src/insets/Inset.cpp.  I.e., if a dialog goes with an inset, the
201 // dialog should have the same name as the inset.
202
203 char const * const dialognames[] = {
204 "aboutlyx", "bibitem", "bibtex", "box", "branch", "changes", "character",
205 "citation", "document", "embedding", "errorlist", "ert", "external", "file",
206 "findreplace", "float", "graphics", "include", "index", "nomenclature", "label", "log",
207 "mathdelimiter", "mathmatrix", "note", "paragraph",
208 "prefs", "print", "ref", "sendto", "spellchecker","tabular", "tabularcreate",
209
210 #ifdef HAVE_LIBAIKSAURUS
211 "thesaurus",
212 #endif
213
214 "texinfo", "toc", "href", "view-source", "vspace", "wrap", "listings" };
215
216 char const * const * const end_dialognames =
217         dialognames + (sizeof(dialognames) / sizeof(char *));
218
219 class cmpCStr {
220 public:
221         cmpCStr(char const * name) : name_(name) {}
222         bool operator()(char const * other) {
223                 return strcmp(other, name_) == 0;
224         }
225 private:
226         char const * name_;
227 };
228
229
230 } // namespace anon
231
232 // will be replaced by a proper factory...
233 Dialog * createGuiAbout(LyXView & lv);
234 Dialog * createGuiBibitem(LyXView & lv);
235 Dialog * createGuiBibtex(LyXView & lv);
236 Dialog * createGuiBox(LyXView & lv);
237 Dialog * createGuiBranch(LyXView & lv);
238 Dialog * createGuiChanges(LyXView & lv);
239 Dialog * createGuiCharacter(LyXView & lv);
240 Dialog * createGuiCitation(LyXView & lv);
241 Dialog * createGuiDelimiter(LyXView & lv);
242 Dialog * createGuiDocument(LyXView & lv);
243 Dialog * createGuiErrorList(LyXView & lv);
244 Dialog * createGuiERT(LyXView & lv);
245 Dialog * createGuiExternal(LyXView & lv);
246 Dialog * createGuiFloat(LyXView & lv);
247 Dialog * createGuiGraphics(LyXView & lv);
248 Dialog * createGuiInclude(LyXView & lv);
249 Dialog * createGuiIndex(LyXView & lv);
250 Dialog * createGuiLabel(LyXView & lv);
251 Dialog * createGuiListings(LyXView & lv);
252 Dialog * createGuiLog(LyXView & lv);
253 Dialog * createGuiMathMatrix(LyXView & lv);
254 Dialog * createGuiNomenclature(LyXView & lv);
255 Dialog * createGuiNote(LyXView & lv);
256 Dialog * createGuiParagraph(LyXView & lv);
257 Dialog * createGuiPreferences(LyXView & lv);
258 Dialog * createGuiPrint(LyXView & lv);
259 Dialog * createGuiRef(LyXView & lv);
260 Dialog * createGuiSearch(LyXView & lv);
261 Dialog * createGuiSendTo(LyXView & lv);
262 Dialog * createGuiShowFile(LyXView & lv);
263 Dialog * createGuiSpellchecker(LyXView & lv);
264 Dialog * createGuiTabularCreate(LyXView & lv);
265 Dialog * createGuiTabular(LyXView & lv);
266 Dialog * createGuiTexInfo(LyXView & lv);
267 Dialog * createGuiToc(LyXView & lv);
268 Dialog * createGuiThesaurus(LyXView & lv);
269 Dialog * createGuiHyperlink(LyXView & lv);
270 Dialog * createGuiVSpace(LyXView & lv);
271 Dialog * createGuiViewSource(LyXView & lv);
272 Dialog * createGuiWrap(LyXView & lv);
273
274
275 bool Dialogs::isValidName(string const & name) const
276 {
277         return std::find_if(dialognames, end_dialognames,
278                             cmpCStr(name.c_str())) != end_dialognames;
279 }
280
281
282 Dialog * Dialogs::build(string const & name)
283 {
284         BOOST_ASSERT(isValidName(name));
285
286         if (name == "aboutlyx")
287                 return createGuiAbout(lyxview_);
288         if (name == "bibitem")
289                 return createGuiBibitem(lyxview_);
290         if (name == "bibtex")
291                 return createGuiBibtex(lyxview_);
292         if (name == "box")
293                 return createGuiBox(lyxview_);
294         if (name == "branch")
295                 return createGuiBranch(lyxview_);
296         if (name == "changes")
297                 return createGuiChanges(lyxview_);
298         if (name == "character")
299                 return createGuiCharacter(lyxview_);
300         if (name == "citation")
301                 return createGuiCitation(lyxview_);
302         if (name == "document")
303                 return createGuiDocument(lyxview_);
304         if (name == "errorlist")
305                 return createGuiErrorList(lyxview_);
306         if (name == "ert")
307                 return createGuiERT(lyxview_);
308         if (name == "external")
309                 return createGuiExternal(lyxview_);
310         if (name == "file")
311                 return createGuiShowFile(lyxview_);
312         if (name == "findreplace")
313                 return createGuiSearch(lyxview_);
314         if (name == "float")
315                 return createGuiFloat(lyxview_);
316         if (name == "graphics")
317                 return createGuiGraphics(lyxview_);
318         if (name == "include")
319                 return createGuiInclude(lyxview_);
320         if (name == "index")
321                 return createGuiIndex(lyxview_);
322         if (name == "nomenclature")
323                 return createGuiNomenclature(lyxview_);
324         if (name == "label")
325                 return createGuiLabel(lyxview_);
326         if (name == "log")
327                 return createGuiLog(lyxview_);
328         if (name == "view-source")
329                 return createGuiViewSource(lyxview_);
330         if (name == "mathdelimiter")
331                 return createGuiDelimiter(lyxview_);
332         if (name == "mathmatrix")
333                 return createGuiMathMatrix(lyxview_);
334         if (name == "note")
335                 return createGuiNote(lyxview_);
336         if (name == "paragraph")
337                 return createGuiParagraph(lyxview_);
338         if (name == "prefs")
339                 return createGuiPreferences(lyxview_);
340         if (name == "print")
341                 return createGuiPrint(lyxview_);
342         if (name == "ref")
343                 return createGuiRef(lyxview_);
344         if (name == "sendto")
345                 return createGuiSendTo(lyxview_);
346         if (name == "spellchecker")
347                 return createGuiSpellchecker(lyxview_);
348         if (name == "tabular")
349                 return createGuiTabular(lyxview_);
350         if (name == "tabularcreate")
351                 return createGuiTabularCreate(lyxview_);
352         if (name == "texinfo")
353                 return createGuiTexInfo(lyxview_);
354 #ifdef HAVE_LIBAIKSAURUS
355         if (name == "thesaurus")
356                 return createGuiThesaurus(lyxview_);
357 #endif
358         if (name == "toc")
359                 return createGuiToc(lyxview_);
360         if (name == "href")
361                 return createGuiHyperlink(lyxview_);
362         if (name == "vspace")
363                 return createGuiVSpace(lyxview_);
364         if (name == "wrap")
365                 return createGuiWrap(lyxview_);
366         if (name == "listings")
367                 return createGuiListings(lyxview_);
368
369         return 0;
370 }
371
372
373 /// Are the tooltips on or off?
374 bool Dialogs::tooltipsEnabled()
375 {
376         return false;
377 }
378
379 } // namespace frontend
380 } // namespace lyx