]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
LFUN_EXTERNAL_EDIT.
[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 #include "ControlExternal.h"
16 #include "buffer.h"
17 #include "funcrequest.h"
18 #include "gettext.h"
19 #include "helper_funcs.h"
20 #include "lyxrc.h"
21
22 #include "support/LAssert.h"
23 #include <vector>
24
25 using std::vector;
26
27
28 ControlExternal::ControlExternal(Dialog & parent)
29         : Dialog::Controller(parent)
30 {}
31
32
33 bool ControlExternal::initialiseParams(string const & data)
34 {
35         params_.reset(new InsetExternal::Params);
36         InsetExternalMailer::string2params(data, *params_);
37         return true;
38 }
39
40
41 void ControlExternal::clearParams()
42 {
43         params_.reset();
44 }
45
46
47 void ControlExternal::dispatchParams()
48 {
49         string const lfun = InsetExternalMailer::params2string(params());
50         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
51 }
52
53
54 void ControlExternal::setParams(InsetExternal::Params const & p)
55 {
56         lyx::Assert(params_.get());
57         *params_ = p;
58 }
59
60
61 InsetExternal::Params const & ControlExternal::params() const
62 {
63         lyx::Assert(params_.get());
64         return *params_;
65 }
66
67
68 void ControlExternal::editExternal()
69 {
70         lyx::Assert(params_.get());
71
72         dialog().view().apply();
73         string const lfun = InsetExternalMailer::params2string(params());
74         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
75 }
76
77
78 vector<string> const ControlExternal::getTemplates() const
79 {
80         vector<string> result;
81
82         ExternalTemplateManager::Templates::const_iterator i1, i2;
83         i1 = ExternalTemplateManager::get().getTemplates().begin();
84         i2 = ExternalTemplateManager::get().getTemplates().end();
85
86         for (; i1 != i2; ++i1) {
87                 result.push_back(i1->second.lyxName);
88         }
89         return result;
90 }
91
92
93 int ControlExternal::getTemplateNumber(string const & name) const
94 {
95         ExternalTemplateManager::Templates::const_iterator i1, i2;
96         i1 = ExternalTemplateManager::get().getTemplates().begin();
97         i2 = ExternalTemplateManager::get().getTemplates().end();
98         for (int i = 0; i1 != i2; ++i1, ++i) {
99                 if (i1->second.lyxName == name)
100                         return i;
101         }
102
103         // we can get here if a LyX document has a template not installed
104         // on this machine.
105         return -1;
106 }
107
108
109 ExternalTemplate ControlExternal::getTemplate(int i) const
110 {
111         ExternalTemplateManager::Templates::const_iterator i1
112                 = ExternalTemplateManager::get().getTemplates().begin();
113
114         std::advance(i1,  i);
115
116         return i1->second;
117 }
118
119
120 string const ControlExternal::Browse(string const & input) const
121 {
122         string const title =  _("Select external file");
123
124         string const bufpath = kernel().buffer()->filePath();
125
126         /// Determine the template file extension
127         ExternalTemplate const & et = params().templ;
128         string pattern = et.fileRegExp;
129         if (pattern.empty())
130                 pattern = "*";
131
132         // FIXME: a temporary hack until the FileDialog interface is updated
133         pattern += '|';
134
135         std::pair<string, string> dir1(N_("Documents|#o#O"),
136                                   string(lyxrc.document_path));
137
138         return browseRelFile(input, bufpath, title, pattern, false, dir1);
139 }