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