]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
Handle Qt-style file filters like
[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::vector;
41 using std::string;
42
43
44 ControlExternal::ControlExternal(Dialog & parent)
45         : Dialog::Controller(parent),
46           bb_changed_(false)
47 {}
48
49
50 bool ControlExternal::initialiseParams(string const & data)
51 {
52         params_.reset(new InsetExternalParams);
53         InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
54         return true;
55 }
56
57
58 void ControlExternal::clearParams()
59 {
60         params_.reset();
61 }
62
63
64 void ControlExternal::dispatchParams()
65 {
66         string const lfun = InsetExternalMailer::params2string(params(),
67                                                                kernel().buffer());
68
69         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
70 }
71
72
73 void ControlExternal::setParams(InsetExternalParams const & p)
74 {
75         BOOST_ASSERT(params_.get());
76         *params_ = p;
77 }
78
79
80 InsetExternalParams const & ControlExternal::params() const
81 {
82         BOOST_ASSERT(params_.get());
83         return *params_;
84 }
85
86
87 void ControlExternal::editExternal()
88 {
89         BOOST_ASSERT(params_.get());
90
91         dialog().view().apply();
92         string const lfun =
93                 InsetExternalMailer::params2string(params(), kernel().buffer());
94         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
95 }
96
97
98 vector<string> const ControlExternal::getTemplates() const
99 {
100         vector<string> result;
101
102         external::TemplateManager::Templates::const_iterator i1, i2;
103         i1 = external::TemplateManager::get().getTemplates().begin();
104         i2 = external::TemplateManager::get().getTemplates().end();
105
106         for (; i1 != i2; ++i1) {
107                 result.push_back(i1->second.lyxName);
108         }
109         return result;
110 }
111
112
113 int ControlExternal::getTemplateNumber(string const & name) const
114 {
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)
120                         return i;
121         }
122
123         // we can get here if a LyX document has a template not installed
124         // on this machine.
125         return -1;
126 }
127
128
129 external::Template ControlExternal::getTemplate(int i) const
130 {
131         external::TemplateManager::Templates::const_iterator i1
132                 = external::TemplateManager::get().getTemplates().begin();
133
134         std::advance(i1, i);
135
136         return i1->second;
137 }
138
139
140 string const ControlExternal::browse(string const & input,
141                                      string const & template_name) const
142 {
143         string const title =  _("Select external file");
144
145         string const bufpath = kernel().bufferFilepath();
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         FileFilterList const filter = et_ptr ?
154                 FileFilterList(et_ptr->fileRegExp) :
155                 FileFilterList();
156
157         std::pair<string, string> dir1(N_("Documents|#o#O"),
158                                        string(lyxrc.document_path));
159
160         return browseRelFile(input, bufpath, title, filter, false, dir1);
161 }
162
163
164 string const ControlExternal::readBB(string const & file)
165 {
166         string const abs_file =
167                 MakeAbsPath(file, kernel().bufferFilepath());
168
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);
172         if (!bb.empty())
173                 return bb;
174
175         // we don't, so ask the Graphics Cache if it has loaded the file
176         int width = 0;
177         int height = 0;
178
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();
182
183                 if (image) {
184                         width  = image->getWidth();
185                         height = image->getHeight();
186                 }
187         }
188
189         return ("0 0 " + tostr(width) + ' ' + tostr(height));
190 }
191
192
193 namespace {
194
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
207 };
208
209 lyx::size_type const origins_array_size =
210 sizeof(origins_array) / sizeof(origins_array[0]);
211
212 vector<external::RotationDataType> const
213 origins(origins_array, origins_array + origins_array_size);
214
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[] = {
218         N_("Default"),
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")
222 };
223
224 } // namespace anon
225
226 namespace lyx {
227 namespace external {
228
229 vector<RotationDataType> const & all_origins()
230 {
231         return origins;
232 }
233
234 string const origin_gui_str(size_type i)
235 {
236         return origin_gui_strs[i];
237 }
238
239 } // namespace external
240 } // namespace lyx