2 * \file ControlExternal.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "ControlExternal.h"
17 #include "funcrequest.h"
19 #include "helper_funcs.h"
22 #include "graphics/GraphicsCache.h"
23 #include "graphics/GraphicsCacheItem.h"
24 #include "graphics/GraphicsImage.h"
26 #include "insets/insetexternal.h"
27 #include "insets/ExternalSupport.h"
28 #include "insets/ExternalTemplate.h"
30 #include "support/filetools.h"
31 #include "support/globbing.h"
32 #include "support/tostr.h"
34 namespace external = lyx::external;
36 using lyx::support::FileFilterList;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::readBB_from_PSFile;
44 ControlExternal::ControlExternal(Dialog & parent)
45 : Dialog::Controller(parent),
50 bool ControlExternal::initialiseParams(string const & data)
52 params_.reset(new InsetExternalParams);
53 InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
58 void ControlExternal::clearParams()
64 void ControlExternal::dispatchParams()
66 string const lfun = InsetExternalMailer::params2string(params(),
69 kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
73 void ControlExternal::setParams(InsetExternalParams const & p)
75 BOOST_ASSERT(params_.get());
80 InsetExternalParams const & ControlExternal::params() const
82 BOOST_ASSERT(params_.get());
87 void ControlExternal::editExternal()
89 BOOST_ASSERT(params_.get());
91 dialog().view().apply();
93 InsetExternalMailer::params2string(params(), kernel().buffer());
94 kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
98 vector<string> const ControlExternal::getTemplates() const
100 vector<string> result;
102 external::TemplateManager::Templates::const_iterator i1, i2;
103 i1 = external::TemplateManager::get().getTemplates().begin();
104 i2 = external::TemplateManager::get().getTemplates().end();
106 for (; i1 != i2; ++i1) {
107 result.push_back(i1->second.lyxName);
113 int ControlExternal::getTemplateNumber(string const & name) const
115 external::TemplateManager::Templates::const_iterator i1, i2;
116 i1 = external::TemplateManager::get().getTemplates().begin();
117 i2 = external::TemplateManager::get().getTemplates().end();
118 for (int i = 0; i1 != i2; ++i1, ++i) {
119 if (i1->second.lyxName == name)
123 // we can get here if a LyX document has a template not installed
129 external::Template ControlExternal::getTemplate(int i) const
131 external::TemplateManager::Templates::const_iterator i1
132 = external::TemplateManager::get().getTemplates().begin();
140 string const ControlExternal::browse(string const & input,
141 string const & template_name) const
143 string const title = _("Select external file");
145 string const bufpath = kernel().bufferFilepath();
147 /// Determine the template file extension
148 external::TemplateManager const & etm =
149 external::TemplateManager::get();
150 external::Template const * const et_ptr =
151 etm.getTemplateByName(template_name);
153 FileFilterList const filter = et_ptr ?
154 FileFilterList(et_ptr->fileRegExp) :
157 std::pair<string, string> dir1(N_("Documents|#o#O"),
158 string(lyxrc.document_path));
160 return browseRelFile(input, bufpath, title, filter, false, dir1);
164 string const ControlExternal::readBB(string const & file)
166 string const abs_file =
167 MakeAbsPath(file, kernel().bufferFilepath());
169 // try to get it from the file, if possible. Zipped files are
170 // unzipped in the readBB_from_PSFile-Function
171 string const bb = readBB_from_PSFile(abs_file);
175 // we don't, so ask the Graphics Cache if it has loaded the file
179 lyx::graphics::Cache & gc = lyx::graphics::Cache::get();
180 if (gc.inCache(abs_file)) {
181 lyx::graphics::Image const * image = gc.item(abs_file)->image();
184 width = image->getWidth();
185 height = image->getHeight();
189 return ("0 0 " + tostr(width) + ' ' + tostr(height));
195 external::RotationDataType origins_array[] = {
196 external::RotationData::DEFAULT,
197 external::RotationData::TOPLEFT,
198 external::RotationData::BOTTOMLEFT,
199 external::RotationData::BASELINELEFT,
200 external::RotationData::CENTER,
201 external::RotationData::TOPCENTER,
202 external::RotationData::BOTTOMCENTER,
203 external::RotationData::BASELINECENTER,
204 external::RotationData::TOPRIGHT,
205 external::RotationData::BOTTOMRIGHT,
206 external::RotationData::BASELINERIGHT
209 lyx::size_type const origins_array_size =
210 sizeof(origins_array) / sizeof(origins_array[0]);
212 vector<external::RotationDataType> const
213 origins(origins_array, origins_array + origins_array_size);
215 // These are the strings, corresponding to the above, that the GUI should
216 // use. Note that they can/should be translated.
217 char const * const origin_gui_strs[] = {
219 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
220 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
221 N_("Top right"), N_("Bottom right"), N_("Baseline right")
229 vector<RotationDataType> const & all_origins()
234 string const origin_gui_str(size_type i)
236 return origin_gui_strs[i];
239 } // namespace external