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