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