2 * \file ControlGraphics.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
14 #include "ControlGraphics.h"
16 #include "helper_funcs.h"
19 #include "BufferView.h"
20 #include "funcrequest.h"
24 #include "graphics/GraphicsCache.h"
25 #include "graphics/GraphicsCacheItem.h"
26 #include "graphics/GraphicsImage.h"
28 #include "insets/insetgraphics.h"
29 #include "insets/insetgraphicsParams.h"
31 #include "support/tostr.h"
32 #include "support/filetools.h"
33 #include "support/FileInfo.h"
35 using namespace lyx::support;
41 // We need these in the file browser.
42 extern string system_lyxdir;
43 extern string user_lyxdir;
46 ControlGraphics::ControlGraphics(Dialog & parent)
47 : Dialog::Controller(parent)
51 bool ControlGraphics::initialiseParams(string const & data)
53 string const bufpath = kernel().buffer()->filePath();
54 InsetGraphicsParams params;
55 InsetGraphicsMailer::string2params(data, bufpath, params);
56 params_.reset(new InsetGraphicsParams(params));
57 // make relative for good UI
58 params_->filename = MakeRelPath(params_->filename, bufpath);
63 void ControlGraphics::clearParams()
69 void ControlGraphics::dispatchParams()
71 string const buffer_path = kernel().buffer()->filePath();
72 InsetGraphicsParams tmp_params(params());
73 // core requires absolute path during runtime
74 tmp_params.filename = MakeAbsPath(tmp_params.filename, buffer_path);
76 InsetGraphicsMailer::params2string(tmp_params, buffer_path);
77 kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
81 string const ControlGraphics::Browse(string const & in_name)
83 string const title = _("Select graphics file");
85 // Does user clipart directory exist?
86 string clipdir = AddName (user_lyxdir, "clipart");
87 FileInfo fileInfo(clipdir);
88 if (!(fileInfo.isOK() && fileInfo.isDir()))
89 // No - bail out to system clipart directory
90 clipdir = AddName (system_lyxdir, "clipart");
91 pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
92 pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
93 // Show the file browser dialog
94 return browseRelFile(in_name, kernel().buffer()->filePath(),
95 title, "*.*", false, dir1, dir2);
99 string const ControlGraphics::readBB(string const & file)
101 string const abs_file =
102 MakeAbsPath(file, kernel().buffer()->filePath());
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);
110 // we don't, so ask the Graphics Cache if it has loaded the file
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();
119 width = image->getWidth();
120 height = image->getHeight();
124 return ("0 0 " + tostr(width) + ' ' + tostr(height));
128 bool ControlGraphics::isFilenameValid(string const & fname) const
130 // It may be that the filename is relative.
131 string const name = MakeAbsPath(fname, kernel().buffer()->filePath());
132 return IsFileReadable(name);
140 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
141 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
143 // These are the strings that are stored in the LyX file and which
144 // correspond to the LaTeX identifiers shown in the comments at the
146 char const * const rorigin_lyx_strs[] = {
147 // the LaTeX default is leftBaseline
149 "leftTop", "leftBottom", "leftBaseline", // lt lb lB
150 "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
151 "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
153 // These are the strings, corresponding to the above, that the GUI should
154 // use. Note that they can/should be translated.
155 char const * const rorigin_gui_strs[] = {
157 N_("Top left"), N_("Bottom left"), N_("Left baseline"),
158 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
159 N_("Top right"), N_("Bottom right"), N_("Right baseline") };
161 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
166 vector<string> const getBBUnits()
168 return vector<string> (bb_units, bb_units + bb_size);
172 vector<RotationOriginPair> getRotationOriginData()
174 static vector<RotationOriginPair> data;
178 data.resize(rorigin_size);
179 for (lyx::size_type i = 0; i < rorigin_size; ++i) {
180 data[i] = make_pair(_(rorigin_gui_strs[i]),
181 rorigin_lyx_strs[i]);