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;
45 ControlExternal::ControlExternal(Dialog & parent)
46 : Dialog::Controller(parent),
51 bool ControlExternal::initialiseParams(string const & data)
53 params_.reset(new InsetExternalParams);
54 InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
59 void ControlExternal::clearParams()
65 void ControlExternal::dispatchParams()
67 string const lfun = InsetExternalMailer::params2string(params(),
70 kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
74 void ControlExternal::setParams(InsetExternalParams const & p)
76 BOOST_ASSERT(params_.get());
81 InsetExternalParams const & ControlExternal::params() const
83 BOOST_ASSERT(params_.get());
88 void ControlExternal::editExternal()
90 BOOST_ASSERT(params_.get());
92 dialog().view().apply();
94 InsetExternalMailer::params2string(params(), kernel().buffer());
95 kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
99 vector<string> const ControlExternal::getTemplates() const
101 vector<string> result;
103 external::TemplateManager::Templates::const_iterator i1, i2;
104 i1 = external::TemplateManager::get().getTemplates().begin();
105 i2 = external::TemplateManager::get().getTemplates().end();
107 for (; i1 != i2; ++i1) {
108 result.push_back(i1->second.lyxName);
114 int ControlExternal::getTemplateNumber(string const & name) const
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)
124 // we can get here if a LyX document has a template not installed
130 external::Template ControlExternal::getTemplate(int i) const
132 external::TemplateManager::Templates::const_iterator i1
133 = external::TemplateManager::get().getTemplates().begin();
141 string const ControlExternal::browse(string const & input,
142 string const & template_name) const
144 string const title = _("Select external file");
146 string const bufpath = kernel().bufferFilepath();
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);
154 FileFilterList const filter = et_ptr ?
155 FileFilterList(et_ptr->fileRegExp) :
158 std::pair<string, string> dir1(N_("Documents|#o#O"),
159 string(lyxrc.document_path));
161 return browseRelFile(input, bufpath, title, filter, false, dir1);
165 string const ControlExternal::readBB(string const & file)
167 string const abs_file =
168 MakeAbsPath(file, kernel().bufferFilepath());
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);
176 // we don't, so ask the Graphics Cache if it has loaded the file
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();
185 width = image->getWidth();
186 height = image->getHeight();
190 return ("0 0 " + tostr(width) + ' ' + tostr(height));
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
210 lyx::size_type const origins_array_size =
211 sizeof(origins_array) / sizeof(origins_array[0]);
213 vector<external::RotationDataType> const
214 origins(origins_array, origins_array + origins_array_size);
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[] = {
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")
230 vector<RotationDataType> const & all_origins()
235 string const origin_gui_str(size_type i)
237 return origin_gui_strs[i];
240 } // namespace external