3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
12 #include "InsetInfo.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
23 #include "LaTeXFeatures.h"
24 #include "LayoutFile.h"
26 #include "LyXAction.h"
30 #include "ParagraphParameters.h"
33 #include "frontends/Application.h"
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/docstring_list.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/FileName.h"
41 #include "support/filetools.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/qstring_helpers.h"
45 #include "support/Translator.h"
49 #include <QtGui/QImage>
52 using namespace lyx::support;
58 typedef Translator<InsetInfo::info_type, string> NameTranslator;
60 NameTranslator const initTranslator()
62 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
64 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
65 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
66 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
67 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
68 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
69 translator.addPair(InsetInfo::MENU_INFO, "menu");
70 translator.addPair(InsetInfo::ICON_INFO, "icon");
71 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
72 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
77 /// The translator between the information type enum and corresponding string.
78 NameTranslator const & nameTranslator()
80 static NameTranslator const translator = initTranslator();
86 /////////////////////////////////////////////////////////////////////////
90 /////////////////////////////////////////////////////////////////////////
94 InsetInfo::InsetInfo(Buffer * buf, string const & name)
95 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
97 setAutoBreakRows(true);
103 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
105 // do not allow the cursor to be set in this Inset
106 return Inset::editXY(cur, x, y);
110 string InsetInfo::infoType() const
112 return nameTranslator().find(type_);
116 docstring InsetInfo::layoutName() const
118 return from_ascii("Info:" + infoType());
122 docstring InsetInfo::toolTip(BufferView const &, int, int) const
124 return bformat(_("Information regarding %1$s '%2$s'"),
125 _(infoType()), from_utf8(name_));
129 void InsetInfo::read(Lexer & lex)
134 token = lex.getString();
135 if (token == "type") {
137 token = lex.getString();
138 type_ = nameTranslator().find(token);
139 } else if (token == "arg") {
141 name_ = lex.getString();
142 } else if (token == "\\end_inset")
145 if (token != "\\end_inset") {
146 lex.printError("Missing \\end_inset at this point");
147 throw ExceptionMessage(WarningException,
148 _("Missing \\end_inset at this point."),
155 void InsetInfo::write(ostream & os) const
157 os << "Info\ntype \"" << infoType()
158 << "\"\narg " << Lexer::quoteString(name_);
162 bool InsetInfo::validateModifyArgument(docstring const & arg) const
165 string const name = trim(split(to_utf8(arg), type, ' '));
167 switch (nameTranslator().find(type)) {
175 FuncRequest func = lyxaction.lookupFunc(name);
176 return func.action() != LFUN_UNKNOWN_ACTION;
181 lyxrc.write(oss, true, name);
182 return !oss.str().empty();
190 if (name == "name" || name == "path" || name == "class")
192 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
193 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
194 return buffer().lyxvc().inUse();
198 return name == "version";
205 bool InsetInfo::showInsetDialog(BufferView * bv) const
207 bv->showDialog("info");
212 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
213 FuncStatus & flag) const
215 switch (cmd.action()) {
216 case LFUN_INSET_SETTINGS:
217 return InsetCollapsable::getStatus(cur, cmd, flag);
219 case LFUN_INSET_DIALOG_UPDATE:
220 case LFUN_INSET_COPY_AS:
221 flag.setEnabled(true);
224 case LFUN_INSET_MODIFY:
225 if (validateModifyArgument(cmd.argument())) {
226 flag.setEnabled(true);
237 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
239 switch (cmd.action()) {
240 case LFUN_INSET_MODIFY:
242 setInfo(to_utf8(cmd.argument()));
245 case LFUN_INSET_COPY_AS: {
246 cap::clearSelection();
248 copy.pushBackward(*this);
252 copy.pit() = copy.lastpit();
253 copy.pos() = copy.lastpos();
255 cap::copySelection(copy);
260 InsetCollapsable::doDispatch(cur, cmd);
266 void InsetInfo::setInfo(string const & name)
272 name_ = trim(split(name, type, ' '));
273 type_ = nameTranslator().find(type);
278 void InsetInfo::error(string const & err)
280 setText(bformat(_(err), from_utf8(name_)),
281 Font(inherit_font, buffer().params().language), false);
285 void InsetInfo::setText(docstring const & str)
287 setText(str, Font(inherit_font, buffer().params().language), false);
291 void InsetInfo::updateInfo()
293 BufferParams const & bp = buffer().params();
297 error("Unknown Info: %1$s");
300 case SHORTCUTS_INFO: {
301 FuncRequest const func = lyxaction.lookupFunc(name_);
302 if (func.action() == LFUN_UNKNOWN_ACTION) {
303 error("Unknown action %1$s");
306 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
307 if (bindings.empty()) {
308 // It is impropriate to use error() for undefined shortcut
309 setText(_("undefined"));
312 if (type_ == SHORTCUT_INFO)
313 setText(bindings.begin()->print(KeySequence::Portable));
315 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
321 setText(_("undefined"));
324 lyxrc.write(oss, true, name_);
325 string result = oss.str();
326 if (result.size() < 2) {
327 setText(_("undefined"));
330 string::size_type loc = result.rfind("\n", result.size() - 2);
331 loc = loc == string::npos ? 0 : loc + 1;
332 if (result.size() < loc + name_.size() + 1
333 || result.substr(loc + 1, name_.size()) != name_) {
334 setText(_("undefined"));
337 // remove leading comments and \\name and space
338 result = result.substr(loc + name_.size() + 2);
341 result = rtrim(result, "\n");
342 result = trim(result, "\"");
343 setText(from_utf8(result));
347 // check in packages.lst
348 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
351 case TEXTCLASS_INFO: {
352 // name_ is the class name
353 LayoutFileList const & list = LayoutFileList::get();
354 bool available = false;
355 if (list.haveClass(name_))
356 available = list[name_].isTeXClassAvailable();
357 setText(available ? _("yes") : _("no"));
361 docstring_list names;
362 FuncRequest const func = lyxaction.lookupFunc(name_);
363 if (func.action() == LFUN_UNKNOWN_ACTION) {
364 error("Unknown action %1$s");
367 // iterate through the menubackend to find it
369 error("Can't determine menu entry for action %1$s in batch mode");
372 if (!theApp()->searchMenu(func, names)) {
373 error("No menu entry for action %1$s");
376 // if found, return its path.
378 Paragraph & par = paragraphs().front();
379 Font const f(inherit_font, buffer().params().language);
381 //fu.fontInfo().setUnderbar(FONT_ON);
382 docstring_list::const_iterator beg = names.begin();
383 docstring_list::const_iterator end = names.end();
384 for (docstring_list::const_iterator it = beg ;
386 // do not insert > for the top level menu item
388 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
389 f, Change(Change::UNCHANGED));
390 //FIXME: add proper underlines here. This
391 // involves rewriting searchMenu used above to
392 // return a vector of menus. If we do not do
393 // that, we might as well use below
394 // Paragraph::insert on each string (JMarc)
395 for (size_type i = 0; i != it->length(); ++i)
396 par.insertChar(par.size(), (*it)[i],
397 f, Change(Change::UNCHANGED));
402 FuncRequest func = lyxaction.lookupFunc(name_);
403 docstring icon_name = frontend::Application::iconName(func, true);
404 // FIXME: We should use the icon directly instead of
405 // going through FileName. The code below won't work
406 // if the icon is embedded in the executable through
407 // the Qt resource system.
408 // This is only a negligible performance problem:
409 // If the installed icon differs from the resource icon the
410 // installed one is preferred anyway, and all icons that are
411 // embedded in the resources are installed as well.
412 FileName file(to_utf8(icon_name));
413 if (file.onlyFileNameWithoutExt() == "unknown") {
414 string dir = "images";
415 FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
421 int percent_scale = 100;
423 // Compute the scale factor for the icon such that its
424 // width on screen is equal to 1em in pixels.
425 // The scale factor is rounded to the integer nearest
426 // to the float value of the ratio 100*iconsize/imgsize.
427 int imgsize = QImage(toqstr(file.absFileName())).width();
429 int iconsize = Length(1, Length::EM).inPixels(1);
430 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
433 InsetGraphics * inset = new InsetGraphics(buffer_);
434 InsetGraphicsParams igp;
436 igp.lyxscale = percent_scale;
437 igp.scale = string();
438 igp.width = Length(1, Length::EM);
439 inset->setParams(igp);
441 Font const f(inherit_font, buffer().params().language);
442 paragraphs().front().insertInset(0, inset, f,
443 Change(Change::UNCHANGED));
447 if (name_ == "name") {
448 setText(from_utf8(buffer().fileName().onlyFileName()));
451 if (name_ == "path") {
452 setText(from_utf8(os::latex_path(buffer().filePath())));
455 if (name_ == "class") {
456 setText(from_utf8(bp.documentClass().name()));
460 // everything that follows is for version control.
461 // nothing that isn't version control should go below this line.
462 if (!buffer().lyxvc().inUse()) {
463 setText(_("No version control"));
466 LyXVC::RevisionInfo itype = LyXVC::Unknown;
467 if (name_ == "vcs-revision")
469 else if (name_ == "vcs-tree-revision")
471 else if (name_ == "vcs-author")
472 itype = LyXVC::Author;
473 else if (name_ == "vcs-time")
475 else if (name_ == "vcs-date")
477 string binfo = buffer().lyxvc().revisionInfo(itype);
479 setText(from_ascii(name_) + " unknown");
481 setText(from_utf8(binfo));
485 if (name_ == "version")
486 setText(from_ascii(lyx_version));
492 string InsetInfo::contextMenu(BufferView const &, int, int) const
494 //FIXME: We override the implementation of InsetCollapsable,
495 //because this inset is not a collapsable inset.
496 return contextMenuName();
500 string InsetInfo::contextMenuName() const
502 return "context-info";