]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
This file is part of LyX, the document processor.
[lyx.git] / src / frontends / controllers / ControlExternal.C
1 /**
2  * \file ControlExternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "ControlExternal.h"
20 #include "buffer.h"
21 #include "BufferView.h"
22 #include "gettext.h"
23 #include "helper_funcs.h"
24 #include "lyxrc.h"
25 #include "support/filetools.h"
26
27 #include <vector>
28
29 using std::vector;
30
31 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
32         : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
33 {}
34
35
36 InsetExternal::Params const ControlExternal::getParams(string const &)
37 {
38         return InsetExternal::Params();
39 }
40
41 InsetExternal::Params const
42 ControlExternal::getParams(InsetExternal const & inset)
43 {
44         return inset.params();
45 }
46
47
48 void ControlExternal::applyParamsToInset()
49 {
50         inset()->setFromParams(params());
51         bufferview()->updateInset(inset(), true);
52 }
53
54 void ControlExternal::editExternal()
55 {
56         // fill the local, controller's copy of the Params struct with
57         // the contents of the dialog's fields.
58         view().apply();
59
60         // Create a local copy of the inset and initialise it with this
61         // params struct.
62         boost::scoped_ptr<InsetExternal> ie;
63         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
64         ie->setFromParams(params());
65
66         ie->editExternal();
67 }
68
69 void ControlExternal::viewExternal()
70 {
71         view().apply();
72
73         boost::scoped_ptr<InsetExternal> ie;
74         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
75         ie->setFromParams(params());
76
77         ie->viewExternal();
78 }
79
80 void ControlExternal::updateExternal()
81 {
82         view().apply();
83
84         boost::scoped_ptr<InsetExternal> ie;
85         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
86         ie->setFromParams(params());
87
88         ie->updateExternal();
89 }
90
91 vector<string> const ControlExternal::getTemplates() const
92 {
93         vector<string> result;
94
95         ExternalTemplateManager::Templates::const_iterator i1, i2;
96         i1 = ExternalTemplateManager::get().getTemplates().begin();
97         i2 = ExternalTemplateManager::get().getTemplates().end();
98
99         for (; i1 != i2; ++i1) {
100                 result.push_back(i1->second.lyxName);
101         }
102         return result;
103 }
104
105
106 int ControlExternal::getTemplateNumber(string const & name) const
107 {
108         int i = 0;
109
110         ExternalTemplateManager::Templates::const_iterator i1, i2;
111         i1 = ExternalTemplateManager::get().getTemplates().begin();
112         i2 = ExternalTemplateManager::get().getTemplates().end();
113         for (; i1 != i2; ++i1) {
114                 if (i1->second.lyxName == name)
115                         return i;
116                 ++i;
117         }
118
119         // we can get here if a LyX document has a template not installed
120         // on this machine.
121         return -1;
122 }
123
124
125 ExternalTemplate ControlExternal::getTemplate(int i) const
126 {
127         ExternalTemplateManager::Templates::const_iterator i1;
128         i1 = ExternalTemplateManager::get().getTemplates().begin();
129         for (int n = 1; n < i; ++n)
130                 ++i1;
131
132         return i1->second;
133 }
134
135
136 string const ControlExternal::Browse(string const & input) const
137 {
138         string const title =  _("Select external file");
139
140         string const bufpath = buffer()->filePath();
141
142         /// Determine the template file extension
143         ExternalTemplate const & et = params().templ;
144         string pattern = et.fileRegExp;
145         if (pattern.empty())
146                 pattern = "*";
147
148         // FIXME: a temporary hack until the FileDialog interface is updated
149         pattern += "|";
150
151         std::pair<string, string> dir1(N_("Documents|#o#O"),
152                                   string(lyxrc.document_path));
153
154         return browseRelFile(&lv_, input, bufpath, title, pattern, dir1);
155 }