]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
prefs/tabular MVC work
[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
32 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
33         : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
34 {}
35
36
37 InsetExternal::Params const ControlExternal::getParams(string const &)
38 {
39         return InsetExternal::Params();
40 }
41
42
43 InsetExternal::Params const
44 ControlExternal::getParams(InsetExternal const & inset)
45 {
46         return inset.params();
47 }
48
49
50 void ControlExternal::applyParamsToInset()
51 {
52         inset()->setFromParams(params());
53         bufferview()->updateInset(inset(), true);
54 }
55
56
57 void ControlExternal::editExternal()
58 {
59         // fill the local, controller's copy of the Params struct with
60         // the contents of the dialog's fields.
61         view().apply();
62
63         // Create a local copy of the inset and initialise it with this
64         // params struct.
65         boost::scoped_ptr<InsetExternal> ie;
66         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
67         ie->setFromParams(params());
68
69         ie->editExternal();
70 }
71
72
73 void ControlExternal::viewExternal()
74 {
75         view().apply();
76
77         boost::scoped_ptr<InsetExternal> ie;
78         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
79         ie->setFromParams(params());
80
81         ie->viewExternal();
82 }
83
84
85 void ControlExternal::updateExternal()
86 {
87         view().apply();
88
89         boost::scoped_ptr<InsetExternal> ie;
90         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
91         ie->setFromParams(params());
92
93         ie->updateExternal();
94 }
95
96
97 vector<string> const ControlExternal::getTemplates() const
98 {
99         vector<string> result;
100
101         ExternalTemplateManager::Templates::const_iterator i1, i2;
102         i1 = ExternalTemplateManager::get().getTemplates().begin();
103         i2 = ExternalTemplateManager::get().getTemplates().end();
104
105         for (; i1 != i2; ++i1) {
106                 result.push_back(i1->second.lyxName);
107         }
108         return result;
109 }
110
111
112 int ControlExternal::getTemplateNumber(string const & name) const
113 {
114         ExternalTemplateManager::Templates::const_iterator i1, i2;
115         i1 = ExternalTemplateManager::get().getTemplates().begin();
116         i2 = ExternalTemplateManager::get().getTemplates().end();
117         for (int i = 0; i1 != i2; ++i1, ++i) {
118                 if (i1->second.lyxName == name)
119                         return i;
120         }
121
122         // we can get here if a LyX document has a template not installed
123         // on this machine.
124         return -1;
125 }
126
127
128 ExternalTemplate ControlExternal::getTemplate(int i) const
129 {
130         ExternalTemplateManager::Templates::const_iterator i1
131                 = ExternalTemplateManager::get().getTemplates().begin();
132
133         std::advance(i1,  i);
134
135         return i1->second;
136 }
137
138
139 string const ControlExternal::Browse(string const & input) const
140 {
141         string const title =  _("Select external file");
142
143         string const bufpath = buffer()->filePath();
144
145         /// Determine the template file extension
146         ExternalTemplate const & et = params().templ;
147         string pattern = et.fileRegExp;
148         if (pattern.empty())
149                 pattern = "*";
150
151         // FIXME: a temporary hack until the FileDialog interface is updated
152         pattern += "|";
153
154         std::pair<string, string> dir1(N_("Documents|#o#O"),
155                                   string(lyxrc.document_path));
156
157         return browseRelFile(&lv_, input, bufpath, title, pattern, dir1);
158 }