]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
The file dialog browser doesn't need a LyXView...
[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
16 #include "ControlExternal.h"
17 #include "buffer.h"
18 #include "BufferView.h"
19 #include "gettext.h"
20 #include "helper_funcs.h"
21 #include "lyxrc.h"
22 #include "support/filetools.h"
23
24 #include <vector>
25
26 using std::vector;
27
28
29 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
30         : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
31 {}
32
33
34 InsetExternal::Params const ControlExternal::getParams(string const &)
35 {
36         return InsetExternal::Params();
37 }
38
39
40 InsetExternal::Params const
41 ControlExternal::getParams(InsetExternal const & inset)
42 {
43         return inset.params();
44 }
45
46
47 void ControlExternal::applyParamsToInset()
48 {
49         inset()->setFromParams(params());
50         bufferview()->updateInset(inset(), true);
51 }
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
70 void ControlExternal::viewExternal()
71 {
72         view().apply();
73
74         boost::scoped_ptr<InsetExternal> ie;
75         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
76         ie->setFromParams(params());
77
78         ie->viewExternal();
79 }
80
81
82 void ControlExternal::updateExternal()
83 {
84         view().apply();
85
86         boost::scoped_ptr<InsetExternal> ie;
87         ie.reset(static_cast<InsetExternal *>(inset()->clone(*buffer())));
88         ie->setFromParams(params());
89
90         ie->updateExternal();
91 }
92
93
94 vector<string> const ControlExternal::getTemplates() const
95 {
96         vector<string> result;
97
98         ExternalTemplateManager::Templates::const_iterator i1, i2;
99         i1 = ExternalTemplateManager::get().getTemplates().begin();
100         i2 = ExternalTemplateManager::get().getTemplates().end();
101
102         for (; i1 != i2; ++i1) {
103                 result.push_back(i1->second.lyxName);
104         }
105         return result;
106 }
107
108
109 int ControlExternal::getTemplateNumber(string const & name) const
110 {
111         ExternalTemplateManager::Templates::const_iterator i1, i2;
112         i1 = ExternalTemplateManager::get().getTemplates().begin();
113         i2 = ExternalTemplateManager::get().getTemplates().end();
114         for (int i = 0; i1 != i2; ++i1, ++i) {
115                 if (i1->second.lyxName == name)
116                         return 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                 = ExternalTemplateManager::get().getTemplates().begin();
129
130         std::advance(i1,  i);
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(input, bufpath, title, pattern, false, dir1);
155 }