1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
10 * \file ControlGraphics.C
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 * \author Herbert Voss <voss@perce.de>
18 #pragma implementation
22 #include "ButtonControllerBase.h"
23 #include "ControlGraphics.h"
24 #include "ControlInset.tmpl"
26 #include "helper_funcs.h"
28 #include "converter.h"
30 #include "BufferView.h"
32 #include "frontends/LyXView.h"
36 #include "graphics/GraphicsCache.h"
37 #include "graphics/GraphicsConverter.h"
39 #include "insets/insetgraphics.h"
40 #include "insets/insetgraphicsParams.h"
42 #include "support/lstrings.h"
43 #include "support/filetools.h"
44 #include "support/FileInfo.h"
46 #include <boost/bind.hpp>
57 // FIXME: currently we need the second '|' to prevent mis-interpretation!
58 // All supported graphic formats with their file-extension and the
59 // gzip-ext for zipped (e)ps-files.
60 // string const grfx_pattern =
61 // "*.(agr|bmp|eps|epsi|fits|gif|jpg|obj|pdf|pbm|pgm|png|"
62 // "ppm|ps|tif|tiff|xbm|xpm|xwd|gz)|";
63 vector<string> const grfx_formats()
65 vector<string> native_formats = grfx::GCache::get().loadableFormats();
66 // We can load any format that can be loaded natively together with
67 // those that can be converted to one of these native formats.
68 vector<string> browsable_formats = native_formats;
70 grfx::GConverter const & gconverter = grfx::GConverter::get();
72 vector<string>::const_iterator to_end = native_formats.end();
74 Formats::const_iterator from_it = formats.begin();
75 Formats::const_iterator from_end = formats.end();
76 for (; from_it != from_end; ++from_it) {
77 string const from = from_it->name();
79 vector<string>::const_iterator to_it = native_formats.begin();
80 for (; to_it != to_end; ++to_it) {
81 if (gconverter.isReachable(from, *to_it)) {
82 browsable_formats.push_back(from);
88 browsable_formats.push_back("gz");
90 return browsable_formats;
94 string const xforms_pattern()
96 vector<string> const browsable_formats = grfx_formats();
98 "*.(" + getStringFromVector(browsable_formats, "|") +")|";
105 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
106 : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
108 d_.showGraphics = boost::bind(&ControlGraphics::showInset, this, _1);
112 InsetGraphicsParams const ControlGraphics::getParams(string const &)
114 return InsetGraphicsParams();
118 InsetGraphicsParams const
119 ControlGraphics::getParams(InsetGraphics const & inset)
121 return inset.params();
125 void ControlGraphics::applyParamsToInset()
127 // Set the parameters in the inset, it also returns true if the new
128 // parameters are different from what was in the inset already.
129 bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
130 // Tell LyX we've got a change, and mark the document dirty,
132 lv_.view()->updateInset(inset(), changed);
136 void ControlGraphics::applyParamsNoInset()
140 // We need these in the file browser.
141 extern string system_lyxdir;
142 extern string user_lyxdir;
145 string const ControlGraphics::Browse(string const & in_name)
147 string const title = _("Select graphics file");
149 // Does user clipart directory exist?
150 string clipdir = AddName (user_lyxdir, "clipart");
151 FileInfo fileInfo(clipdir);
152 if (!(fileInfo.isOK() && fileInfo.isDir()))
153 // No - bail out to system clipart directory
154 clipdir = AddName (system_lyxdir, "clipart");
155 pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
156 pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
157 // Show the file browser dialog
158 return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
159 title, ::xforms_pattern(), dir1, dir2);
163 string const ControlGraphics::readBB(string const & file)
165 string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
167 string const from = getExtFromContents(abs_file);
168 // Check if we have a Postscript file, then it's easy
169 if (contains(from, "ps"))
170 return readBB_from_PSFile(abs_file);
172 // we don't, so ask the Graphics Cache if it has loaded the file
173 grfx::GCache & gc = grfx::GCache::get();
175 tostr(gc.raw_width(abs_file)) + ' ' +
176 tostr(gc.raw_height(abs_file)));
180 bool ControlGraphics::isFilenameValid(string const & fname) const
182 // It may be that the filename is relative.
183 string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
184 return IsFileReadable(name);
192 // These are the strings that are stored in the LyX file and which
193 // correspond to the LaTeX identifiers shown in the comments at the
195 char const * const rorigin_lyx_strs[] = {
197 "leftTop", "leftBottom", "leftBaseline", // lt lb lB
198 "centerTop", "centerBottom", "centerBaseline", // ct cb cB
199 "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
201 // These are the strings, corresponding to the above, that the GUI should
202 // use. Note that they can/should be translated.
203 char const * const rorigin_gui_strs[] = {
205 N_("left top"), N_("left bottom"), N_("left baseline"),
206 N_("center top"), N_("center bottom"), N_("center baseline"),
207 N_("right top"), N_("right bottom"), N_("right baseline") };
209 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
213 vector<RotationOriginPair> getRotationOriginData()
215 static vector<RotationOriginPair> data;
219 data.resize(rorigin_size);
220 for (lyx::size_type i = 0; i < rorigin_size; ++i) {
221 data[i] = std::make_pair(_(rorigin_gui_strs[i]),
222 rorigin_lyx_strs[i]);