]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / frontends / controllers / ControlGraphics.cpp
1 /**
2  * \file ControlGraphics.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Herbert Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlGraphics.h"
15
16 #include "frontend_helpers.h"
17
18 #include "FuncRequest.h"
19 #include "gettext.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/InsetGraphics.h"
27
28 #include "support/convert.h"
29 #include "support/FileFilterList.h"
30 #include "support/filetools.h"
31 #include "support/Package.h"
32 #include "support/types.h"
33
34 #include <boost/filesystem/operations.hpp>
35
36 using std::make_pair;
37 using std::string;
38 using std::pair;
39 using std::vector;
40
41 namespace fs = boost::filesystem;
42
43 namespace lyx {
44
45 using support::addName;
46 using support::FileFilterList;
47 using support::FileName;
48 using support::isFileReadable;
49 using support::makeAbsPath;
50 using support::package;
51 using support::readBB_from_PSFile;
52
53 namespace frontend {
54
55
56 ControlGraphics::ControlGraphics(Dialog & parent)
57         : Dialog::Controller(parent)
58 {}
59
60
61 bool ControlGraphics::initialiseParams(string const & data)
62 {
63         InsetGraphicsParams params;
64         InsetGraphicsMailer::string2params(data, kernel().buffer(), params);
65         params_.reset(new InsetGraphicsParams(params));
66         return true;
67 }
68
69
70 void ControlGraphics::clearParams()
71 {
72         params_.reset();
73 }
74
75
76 void ControlGraphics::dispatchParams()
77 {
78         InsetGraphicsParams tmp_params(params());
79         string const lfun =
80                 InsetGraphicsMailer::params2string(tmp_params, kernel().buffer());
81         kernel().dispatch(FuncRequest(getLfun(), lfun));
82 }
83
84
85 docstring const ControlGraphics::browse(docstring const & in_name) const
86 {
87         docstring const title = _("Select graphics file");
88
89         // Does user clipart directory exist?
90         string clipdir = addName(package().user_support().absFilename(), "clipart");
91         string const encoded_clipdir = FileName(clipdir).toFilesystemEncoding();
92         if (!(fs::exists(encoded_clipdir) && fs::is_directory(encoded_clipdir)))
93                 // No - bail out to system clipart directory
94                 clipdir = addName(package().system_support().absFilename(), "clipart");
95         pair<docstring, docstring> dir1(_("Clipart|#C#c"), from_utf8(clipdir));
96         pair<docstring, docstring> dir2(_("Documents|#o#O"), from_utf8(lyxrc.document_path));
97         // Show the file browser dialog
98         return browseRelFile(in_name, from_utf8(kernel().bufferFilepath()),
99                              title,
100                              FileFilterList(),
101                              false, dir1, dir2);
102 }
103
104
105 string const ControlGraphics::readBB(string const & file)
106 {
107         FileName const abs_file(makeAbsPath(file, kernel().bufferFilepath()));
108
109         // try to get it from the file, if possible. Zipped files are
110         // unzipped in the readBB_from_PSFile-Function
111         string const bb = readBB_from_PSFile(abs_file);
112         if (!bb.empty())
113                 return bb;
114
115         // we don't, so ask the Graphics Cache if it has loaded the file
116         int width = 0;
117         int height = 0;
118
119         graphics::Cache & gc = graphics::Cache::get();
120         if (gc.inCache(abs_file)) {
121                 graphics::Image const * image = gc.item(abs_file)->image();
122
123                 if (image) {
124                         width  = image->getWidth();
125                         height = image->getHeight();
126                 }
127         }
128
129         return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
130 }
131
132
133 bool ControlGraphics::isFilenameValid(string const & fname) const
134 {
135         // It may be that the filename is relative.
136         FileName const name(makeAbsPath(fname, kernel().bufferFilepath()));
137         return isFileReadable(name);
138 }
139
140
141 void ControlGraphics::editGraphics()
142 {
143         BOOST_ASSERT(params_.get());
144
145         dialog().view().apply();
146         string const lfun =
147                 InsetGraphicsMailer::params2string(params(), kernel().buffer());
148         kernel().dispatch(FuncRequest(LFUN_GRAPHICS_EDIT, lfun));
149 }
150
151
152 namespace {
153
154 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
155 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
156
157 // These are the strings that are stored in the LyX file and which
158 // correspond to the LaTeX identifiers shown in the comments at the
159 // end of each line.
160 char const * const rorigin_lyx_strs[] = {
161         // the LaTeX default is leftBaseline
162         "",
163         "leftTop",  "leftBottom", "leftBaseline", // lt lb lB
164         "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
165         "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
166
167 // These are the strings, corresponding to the above, that the GUI should
168 // use. Note that they can/should be translated.
169 char const * const rorigin_gui_strs[] = {
170         N_("Default"),
171         N_("Top left"), N_("Bottom left"), N_("Baseline left"),
172         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
173         N_("Top right"), N_("Bottom right"), N_("Baseline right") };
174
175 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
176
177 } // namespace anon
178
179
180 vector<string> const getBBUnits()
181 {
182         return vector<string>(bb_units, bb_units + bb_size);
183 }
184
185
186 vector<RotationOriginPair> getRotationOriginData()
187 {
188         static vector<RotationOriginPair> data;
189         if (!data.empty())
190                 return data;
191
192         data.resize(rorigin_size);
193         for (size_type i = 0; i < rorigin_size; ++i) {
194                 data[i] = make_pair(_(rorigin_gui_strs[i]),
195                                     rorigin_lyx_strs[i]);
196         }
197
198         return data;
199 }
200
201 } // namespace frontend
202 } // namespace lyx