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"
21 #include "converter.h"
25 #include "graphics/GraphicsCache.h"
26 #include "graphics/GraphicsCacheItem.h"
27 #include "graphics/GraphicsImage.h"
29 #include "insets/insetgraphics.h"
30 #include "insets/insetgraphicsParams.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
34 #include "support/FileInfo.h"
40 // We need these in the file browser.
41 extern string system_lyxdir;
42 extern string user_lyxdir;
45 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
46 : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
50 InsetGraphicsParams const ControlGraphics::getParams(string const &)
52 return InsetGraphicsParams();
56 InsetGraphicsParams const
57 ControlGraphics::getParams(InsetGraphics const & inset)
59 return inset.params();
63 void ControlGraphics::applyParamsToInset()
65 // Set the parameters in the inset, it also returns true if the new
66 // parameters are different from what was in the inset already.
67 bool changed = inset()->setParams(params(), buffer()->filePath());
69 // Tell LyX we've got a change, and mark the document dirty,
71 bufferview()->updateInset(inset(), changed);
75 void ControlGraphics::applyParamsNoInset()
79 string const ControlGraphics::Browse(string const & in_name)
81 string const title = _("Select graphics file");
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(&lv_, in_name, buffer()->filePath(),
93 title, "*.*", false, dir1, dir2);
97 string const ControlGraphics::readBB(string const & file)
99 string const abs_file = MakeAbsPath(file, buffer()->filePath());
101 // try to get it from the file, if possible. Zipped files are
102 // unzipped in the readBB_from_PSFile-Function
103 string const bb = readBB_from_PSFile(abs_file);
107 // we don't, so ask the Graphics Cache if it has loaded the file
111 grfx::Cache & gc = grfx::Cache::get();
112 if (gc.inCache(abs_file)) {
113 grfx::Image const * image = gc.item(abs_file)->image();
116 width = image->getWidth();
117 height = image->getHeight();
121 return ("0 0 " + tostr(width) + ' ' + tostr(height));
125 bool ControlGraphics::isFilenameValid(string const & fname) const
127 // It may be that the filename is relative.
128 string const name = MakeAbsPath(fname, buffer()->filePath());
129 return IsFileReadable(name);
137 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
138 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
140 // These are the strings that are stored in the LyX file and which
141 // correspond to the LaTeX identifiers shown in the comments at the
143 char const * const rorigin_lyx_strs[] = {
144 // the LaTeX default is leftBaseline
146 "leftTop", "leftBottom", "leftBaseline", // lt lb lB
147 "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
148 "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
150 // These are the strings, corresponding to the above, that the GUI should
151 // use. Note that they can/should be translated.
152 char const * const rorigin_gui_strs[] = {
154 N_("Top left"), N_("Bottom left"), N_("Left baseline"),
155 N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
156 N_("Top right"), N_("Bottom right"), N_("Right baseline") };
158 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
163 vector<string> const getBBUnits()
165 return vector<string> (bb_units, bb_units + bb_size);
169 vector<RotationOriginPair> getRotationOriginData()
171 static vector<RotationOriginPair> data;
175 data.resize(rorigin_size);
176 for (lyx::size_type i = 0; i < rorigin_size; ++i) {
177 data[i] = make_pair(_(rorigin_gui_strs[i]),
178 rorigin_lyx_strs[i]);