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/tostr.h"
33 namespace external = lyx::external;
35 using lyx::support::MakeAbsPath;
36 using lyx::support::readBB_from_PSFile;
42 ControlExternal::ControlExternal(Dialog & parent)
43 : Dialog::Controller(parent),
48 bool ControlExternal::initialiseParams(string const & data)
50 params_.reset(new InsetExternalParams);
51 InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
56 void ControlExternal::clearParams()
62 void ControlExternal::dispatchParams()
64 string const lfun = InsetExternalMailer::params2string(params(),
67 kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
71 void ControlExternal::setParams(InsetExternalParams const & p)
73 BOOST_ASSERT(params_.get());
78 InsetExternalParams const & ControlExternal::params() const
80 BOOST_ASSERT(params_.get());
85 void ControlExternal::editExternal()
87 BOOST_ASSERT(params_.get());
89 dialog().view().apply();
91 InsetExternalMailer::params2string(params(), kernel().buffer());
92 kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
96 vector<string> const ControlExternal::getTemplates() const
98 vector<string> result;
100 external::TemplateManager::Templates::const_iterator i1, i2;
101 i1 = external::TemplateManager::get().getTemplates().begin();
102 i2 = external::TemplateManager::get().getTemplates().end();
104 for (; i1 != i2; ++i1) {
105 result.push_back(i1->second.lyxName);
111 int ControlExternal::getTemplateNumber(string const & name) const
113 external::TemplateManager::Templates::const_iterator i1, i2;
114 i1 = external::TemplateManager::get().getTemplates().begin();
115 i2 = external::TemplateManager::get().getTemplates().end();
116 for (int i = 0; i1 != i2; ++i1, ++i) {
117 if (i1->second.lyxName == name)
121 // we can get here if a LyX document has a template not installed
127 external::Template ControlExternal::getTemplate(int i) const
129 external::TemplateManager::Templates::const_iterator i1
130 = external::TemplateManager::get().getTemplates().begin();
138 string const ControlExternal::Browse(string const & input) const
140 string const title = _("Select external file");
142 string const bufpath = kernel().bufferFilepath();
144 /// Determine the template file extension
145 string pattern = "*";
146 external::Template const * const et_ptr =
147 external::getTemplatePtr(params());
149 pattern = et_ptr->fileRegExp;
151 std::pair<string, string> dir1(N_("Documents|#o#O"),
152 string(lyxrc.document_path));
154 return browseRelFile(input, bufpath, title, pattern, false, dir1);
158 string const ControlExternal::readBB(string const & file)
160 string const abs_file =
161 MakeAbsPath(file, kernel().bufferFilepath());
163 // try to get it from the file, if possible. Zipped files are
164 // unzipped in the readBB_from_PSFile-Function
165 string const bb = readBB_from_PSFile(abs_file);
169 // we don't, so ask the Graphics Cache if it has loaded the file
173 lyx::graphics::Cache & gc = lyx::graphics::Cache::get();
174 if (gc.inCache(abs_file)) {
175 lyx::graphics::Image const * image = gc.item(abs_file)->image();
178 width = image->getWidth();
179 height = image->getHeight();
183 return ("0 0 " + tostr(width) + ' ' + tostr(height));
189 external::RotationDataType origins_array[] = {
190 external::RotationData::DEFAULT,
191 external::RotationData::TOPLEFT,
192 external::RotationData::BOTTOMLEFT,
193 external::RotationData::BASELINELEFT,
194 external::RotationData::CENTER,
195 external::RotationData::TOPCENTER,
196 external::RotationData::BOTTOMCENTER,
197 external::RotationData::BASELINECENTER,
198 external::RotationData::TOPRIGHT,
199 external::RotationData::BOTTOMRIGHT,
200 external::RotationData::BASELINERIGHT
203 lyx::size_type const origins_array_size =
204 sizeof(origins_array) / sizeof(origins_array[0]);
206 vector<external::RotationDataType> const
207 origins(origins_array, origins_array + origins_array_size);
209 // These are the strings, corresponding to the above, that the GUI should
210 // use. Note that they can/should be translated.
211 char const * const origin_gui_strs[] = {
213 N_("Top left"), N_("Bottom left"), N_("Baseline left"),
214 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
215 N_("Top right"), N_("Bottom right"), N_("Baseline right")
223 vector<RotationDataType> const & all_origins()
228 string const origin_gui_str(size_type i)
230 return origin_gui_strs[i];
233 } // namespace external