]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
The external inset uses the converters. Graphical display within LyX.
[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         InsetExternal inset;
74         inset.setParams(*params_, kernel().buffer()->filePath());
75         inset.cache(kernel().bufferview());
76         inset.editExternal();
77 }
78
79
80 vector<string> const ControlExternal::getTemplates() const
81 {
82         vector<string> result;
83
84         ExternalTemplateManager::Templates::const_iterator i1, i2;
85         i1 = ExternalTemplateManager::get().getTemplates().begin();
86         i2 = ExternalTemplateManager::get().getTemplates().end();
87
88         for (; i1 != i2; ++i1) {
89                 result.push_back(i1->second.lyxName);
90         }
91         return result;
92 }
93
94
95 int ControlExternal::getTemplateNumber(string const & name) const
96 {
97         ExternalTemplateManager::Templates::const_iterator i1, i2;
98         i1 = ExternalTemplateManager::get().getTemplates().begin();
99         i2 = ExternalTemplateManager::get().getTemplates().end();
100         for (int i = 0; i1 != i2; ++i1, ++i) {
101                 if (i1->second.lyxName == name)
102                         return i;
103         }
104
105         // we can get here if a LyX document has a template not installed
106         // on this machine.
107         return -1;
108 }
109
110
111 ExternalTemplate ControlExternal::getTemplate(int i) const
112 {
113         ExternalTemplateManager::Templates::const_iterator i1
114                 = ExternalTemplateManager::get().getTemplates().begin();
115
116         std::advance(i1,  i);
117
118         return i1->second;
119 }
120
121
122 string const ControlExternal::Browse(string const & input) const
123 {
124         string const title =  _("Select external file");
125
126         string const bufpath = kernel().buffer()->filePath();
127
128         /// Determine the template file extension
129         ExternalTemplate const & et = params().templ;
130         string pattern = et.fileRegExp;
131         if (pattern.empty())
132                 pattern = "*";
133
134         // FIXME: a temporary hack until the FileDialog interface is updated
135         pattern += '|';
136
137         std::pair<string, string> dir1(N_("Documents|#o#O"),
138                                   string(lyxrc.document_path));
139
140         return browseRelFile(input, bufpath, title, pattern, false, dir1);
141 }