]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
Overhaul the branches code.
[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/tostr.h"
32
33 namespace external = lyx::external;
34
35 using lyx::support::MakeAbsPath;
36 using lyx::support::readBB_from_PSFile;
37
38 using std::vector;
39 using std::string;
40
41
42 ControlExternal::ControlExternal(Dialog & parent)
43         : Dialog::Controller(parent),
44           bb_changed_(false)
45 {}
46
47
48 bool ControlExternal::initialiseParams(string const & data)
49 {
50         params_.reset(new InsetExternalParams);
51         InsetExternalMailer::string2params(data, kernel().buffer(), *params_);
52         return true;
53 }
54
55
56 void ControlExternal::clearParams()
57 {
58         params_.reset();
59 }
60
61
62 void ControlExternal::dispatchParams()
63 {
64         string const lfun = InsetExternalMailer::params2string(params(),
65                                                                kernel().buffer());
66
67         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
68 }
69
70
71 void ControlExternal::setParams(InsetExternalParams const & p)
72 {
73         BOOST_ASSERT(params_.get());
74         *params_ = p;
75 }
76
77
78 InsetExternalParams const & ControlExternal::params() const
79 {
80         BOOST_ASSERT(params_.get());
81         return *params_;
82 }
83
84
85 void ControlExternal::editExternal()
86 {
87         BOOST_ASSERT(params_.get());
88
89         dialog().view().apply();
90         string const lfun =
91                 InsetExternalMailer::params2string(params(), kernel().buffer());
92         kernel().dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
93 }
94
95
96 vector<string> const ControlExternal::getTemplates() const
97 {
98         vector<string> result;
99
100         external::TemplateManager::Templates::const_iterator i1, i2;
101         i1 = external::TemplateManager::get().getTemplates().begin();
102         i2 = external::TemplateManager::get().getTemplates().end();
103
104         for (; i1 != i2; ++i1) {
105                 result.push_back(i1->second.lyxName);
106         }
107         return result;
108 }
109
110
111 int ControlExternal::getTemplateNumber(string const & name) const
112 {
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)
118                         return i;
119         }
120
121         // we can get here if a LyX document has a template not installed
122         // on this machine.
123         return -1;
124 }
125
126
127 external::Template ControlExternal::getTemplate(int i) const
128 {
129         external::TemplateManager::Templates::const_iterator i1
130                 = external::TemplateManager::get().getTemplates().begin();
131
132         std::advance(i1, i);
133
134         return i1->second;
135 }
136
137
138 string const ControlExternal::Browse(string const & input) const
139 {
140         string const title =  _("Select external file");
141
142         string const bufpath = kernel().bufferFilepath();
143
144         /// Determine the template file extension
145         string pattern = "*";
146         external::Template const * const et_ptr =
147                 external::getTemplatePtr(params());
148         if (et_ptr)
149                 pattern = et_ptr->fileRegExp;
150
151         std::pair<string, string> dir1(N_("Documents|#o#O"),
152                                   string(lyxrc.document_path));
153
154         return browseRelFile(input, bufpath, title, pattern, false, dir1);
155 }
156
157
158 string const ControlExternal::readBB(string const & file)
159 {
160         string const abs_file =
161                 MakeAbsPath(file, kernel().bufferFilepath());
162
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);
166         if (!bb.empty())
167                 return bb;
168
169         // we don't, so ask the Graphics Cache if it has loaded the file
170         int width = 0;
171         int height = 0;
172
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();
176
177                 if (image) {
178                         width  = image->getWidth();
179                         height = image->getHeight();
180                 }
181         }
182
183         return ("0 0 " + tostr(width) + ' ' + tostr(height));
184 }
185
186
187 namespace {
188
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
201 };
202
203 lyx::size_type const origins_array_size =
204 sizeof(origins_array) / sizeof(origins_array[0]);
205
206 vector<external::RotationDataType> const
207 origins(origins_array, origins_array + origins_array_size);
208
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[] = {
212         N_("Default"),
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")
216 };
217
218 } // namespace anon
219
220 namespace lyx {
221 namespace external {
222
223 vector<RotationDataType> const & all_origins()
224 {
225         return origins;
226 }
227
228 string const origin_gui_str(size_type i)
229 {
230         return origin_gui_strs[i];
231 }
232
233 } // namespace external
234 } // namespace lyx