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
15 #include "ControlGraphics.h"
17 #include "helper_funcs.h"
20 #include "BufferView.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/lstrings.h"
32 #include "support/filetools.h"
33 #include "support/FileInfo.h"
39 // We need these in the file browser.
40 extern string system_lyxdir;
41 extern string user_lyxdir;
44 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
45 : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
49 InsetGraphicsParams const ControlGraphics::getParams(string const &)
51 return InsetGraphicsParams();
55 InsetGraphicsParams const
56 ControlGraphics::getParams(InsetGraphics const & inset)
58 return inset.params();
62 void ControlGraphics::applyParamsToInset()
64 // Set the parameters in the inset, it also returns true if the new
65 // parameters are different from what was in the inset already.
66 bool changed = inset()->setParams(params(), buffer()->filePath());
68 // Tell LyX we've got a change, and mark the document dirty,
70 bufferview()->updateInset(inset(), changed);
74 void ControlGraphics::applyParamsNoInset()
78 string const ControlGraphics::Browse(string const & in_name)
80 string const title = _("Select graphics file");
82 // Does user clipart directory exist?
83 string clipdir = AddName (user_lyxdir, "clipart");
84 FileInfo fileInfo(clipdir);
85 if (!(fileInfo.isOK() && fileInfo.isDir()))
86 // No - bail out to system clipart directory
87 clipdir = AddName (system_lyxdir, "clipart");
88 pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
89 pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
90 // Show the file browser dialog
91 return browseRelFile(in_name, buffer()->filePath(),
92 title, "*.*", false, dir1, dir2);
96 string const ControlGraphics::readBB(string const & file)
98 string const abs_file = MakeAbsPath(file, buffer()->filePath());
100 // try to get it from the file, if possible. Zipped files are
101 // unzipped in the readBB_from_PSFile-Function
102 string const bb = readBB_from_PSFile(abs_file);
106 // we don't, so ask the Graphics Cache if it has loaded the file
110 grfx::Cache & gc = grfx::Cache::get();
111 if (gc.inCache(abs_file)) {
112 grfx::Image const * image = gc.item(abs_file)->image();
115 width = image->getWidth();
116 height = image->getHeight();
120 return ("0 0 " + tostr(width) + ' ' + tostr(height));
124 bool ControlGraphics::isFilenameValid(string const & fname) const
126 // It may be that the filename is relative.
127 string const name = MakeAbsPath(fname, buffer()->filePath());
128 return IsFileReadable(name);
136 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
137 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
139 // These are the strings that are stored in the LyX file and which
140 // correspond to the LaTeX identifiers shown in the comments at the
142 char const * const rorigin_lyx_strs[] = {
143 // the LaTeX default is leftBaseline
145 "leftTop", "leftBottom", "leftBaseline", // lt lb lB
146 "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
147 "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
149 // These are the strings, corresponding to the above, that the GUI should
150 // use. Note that they can/should be translated.
151 char const * const rorigin_gui_strs[] = {
153 N_("Top left"), N_("Bottom left"), N_("Left baseline"),
154 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
155 N_("Top right"), N_("Bottom right"), N_("Right baseline") };
157 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
162 vector<string> const getBBUnits()
164 return vector<string> (bb_units, bb_units + bb_size);
168 vector<RotationOriginPair> getRotationOriginData()
170 static vector<RotationOriginPair> data;
174 data.resize(rorigin_size);
175 for (lyx::size_type i = 0; i < rorigin_size; ++i) {
176 data[i] = make_pair(_(rorigin_gui_strs[i]),
177 rorigin_lyx_strs[i]);