]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
5 new lfuns, move all apply code out of ControlDocument and into the core.
[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
17 #include "funcrequest.h"
18 #include "gettext.h"
19 #include "helper_funcs.h"
20 #include "lyxrc.h"
21
22 #include "graphics/GraphicsCache.h"
23 #include "graphics/GraphicsCacheItem.h"
24 #include "graphics/GraphicsImage.h"
25
26 #include "insets/insetexternal.h"
27 #include "insets/ExternalSupport.h"
28 #include "insets/ExternalTemplate.h"
29
30 #include "support/filetools.h"
31 #include "support/globbing.h"
32 #include "support/tostr.h"
33
34 namespace external = lyx::external;
35
36 using lyx::support::FileFilterList;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::readBB_from_PSFile;
39
40 using std::advance;
41 using std::vector;
42 using std::string;
43
44
45 ControlExternal::ControlExternal(Dialog & parent)
46         : Dialog::Controller(parent),
47           bb_changed_(false)
48 {}
49
50
51 bool ControlExternal::initialiseParams(string const & data)
52 {
53         params_.reset(new InsetExternalParams);
54         InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
55         return true;
56 }
57
58
59 void ControlExternal::clearParams()
60 {
61         params_.reset();
62 }
63
64
65 void ControlExternal::dispatchParams()
66 {
67         string const lfun = InsetExternalMailer::params2string(params(),
68                                                                kernel().buffer());
69
70         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
71 }
72
73
74 void ControlExternal::setParams(InsetExternalParams const & p)
75 {
76         BOOST_ASSERT(params_.get());
77         *params_ = p;
78 }
79
80
81 InsetExternalParams const & ControlExternal::params() const
82 {
83         BOOST_ASSERT(params_.get());
84         return *params_;
85 }
86
87
88 void ControlExternal::editExternal()
89 {
90         BOOST_ASSERT(params_.get());
91
92         dialog().view().apply();
93         string const lfun =
94                 InsetExternalMailer::params2string(params(), kernel().buffer());
95         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
96 }
97
98
99 vector<string> const ControlExternal::getTemplates() const
100 {
101         vector<string> result;
102
103         external::TemplateManager::Templates::const_iterator i1, i2;
104         i1 = external::TemplateManager::get().getTemplates().begin();
105         i2 = external::TemplateManager::get().getTemplates().end();
106
107         for (; i1 != i2; ++i1) {
108                 result.push_back(i1->second.lyxName);
109         }
110         return result;
111 }
112
113
114 int ControlExternal::getTemplateNumber(string const & name) const
115 {
116         external::TemplateManager::Templates::const_iterator i1, i2;
117         i1 = external::TemplateManager::get().getTemplates().begin();
118         i2 = external::TemplateManager::get().getTemplates().end();
119         for (int i = 0; i1 != i2; ++i1, ++i) {
120                 if (i1->second.lyxName == name)
121                         return i;
122         }
123
124         // we can get here if a LyX document has a template not installed
125         // on this machine.
126         return -1;
127 }
128
129
130 external::Template ControlExternal::getTemplate(int i) const
131 {
132         external::TemplateManager::Templates::const_iterator i1
133                 = external::TemplateManager::get().getTemplates().begin();
134
135         advance(i1, i);
136
137         return i1->second;
138 }
139
140
141 string const ControlExternal::browse(string const & input,
142                                      string const & template_name) const
143 {
144         string const title =  _("Select external file");
145
146         string const bufpath = kernel().bufferFilepath();
147
148         /// Determine the template file extension
149         external::TemplateManager const & etm =
150                 external::TemplateManager::get();
151         external::Template const * const et_ptr =
152                 etm.getTemplateByName(template_name);
153
154         FileFilterList const filter = et_ptr ?
155                 FileFilterList(et_ptr->fileRegExp) :
156                 FileFilterList();
157
158         std::pair<string, string> dir1(N_("Documents|#o#O"),
159                                        string(lyxrc.document_path));
160
161         return browseRelFile(input, bufpath, title, filter, false, dir1);
162 }
163
164
165 string const ControlExternal::readBB(string const & file)
166 {
167         string const abs_file =
168                 MakeAbsPath(file, kernel().bufferFilepath());
169
170         // try to get it from the file, if possible. Zipped files are
171         // unzipped in the readBB_from_PSFile-Function
172         string const bb = readBB_from_PSFile(abs_file);
173         if (!bb.empty())
174                 return bb;
175
176         // we don't, so ask the Graphics Cache if it has loaded the file
177         int width = 0;
178         int height = 0;
179
180         lyx::graphics::Cache & gc = lyx::graphics::Cache::get();
181         if (gc.inCache(abs_file)) {
182                 lyx::graphics::Image const * image = gc.item(abs_file)->image();
183
184                 if (image) {
185                         width  = image->getWidth();
186                         height = image->getHeight();
187                 }
188         }
189
190         return ("0 0 " + tostr(width) + ' ' + tostr(height));
191 }
192
193
194 namespace {
195
196 external::RotationDataType origins_array[] = {
197         external::RotationData::DEFAULT,
198         external::RotationData::TOPLEFT,
199         external::RotationData::BOTTOMLEFT,
200         external::RotationData::BASELINELEFT,
201         external::RotationData::CENTER,
202         external::RotationData::TOPCENTER,
203         external::RotationData::BOTTOMCENTER,
204         external::RotationData::BASELINECENTER,
205         external::RotationData::TOPRIGHT,
206         external::RotationData::BOTTOMRIGHT,
207         external::RotationData::BASELINERIGHT
208 };
209
210 lyx::size_type const origins_array_size =
211 sizeof(origins_array) / sizeof(origins_array[0]);
212
213 vector<external::RotationDataType> const
214 origins(origins_array, origins_array + origins_array_size);
215
216 // These are the strings, corresponding to the above, that the GUI should
217 // use. Note that they can/should be translated.
218 char const * const origin_gui_strs[] = {
219         N_("Default"),
220         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
221         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
222         N_("Top right"), N_("Bottom right"), N_("Baseline right")
223 };
224
225 } // namespace anon
226
227 namespace lyx {
228 namespace external {
229
230 vector<RotationDataType> const & all_origins()
231 {
232         return origins;
233 }
234
235 string const origin_gui_str(size_type i)
236 {
237         return origin_gui_strs[i];
238 }
239
240 } // namespace external
241 } // namespace lyx