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"
25 #include "LayoutFile.h"
27 #include "LyXAction.h"
31 #include "ParagraphParameters.h"
34 #include "frontends/Application.h"
36 #include "support/convert.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/docstring_list.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/FileName.h"
42 #include "support/filetools.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
45 #include "support/qstring_helpers.h"
46 #include "support/Translator.h"
50 #include <QtGui/QImage>
53 using namespace lyx::support;
59 typedef Translator<InsetInfo::info_type, string> NameTranslator;
61 NameTranslator const initTranslator()
63 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
65 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
66 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
67 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
68 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
69 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
70 translator.addPair(InsetInfo::MENU_INFO, "menu");
71 translator.addPair(InsetInfo::ICON_INFO, "icon");
72 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
73 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
78 /// The translator between the information type enum and corresponding string.
79 NameTranslator const & nameTranslator()
81 static NameTranslator const translator = initTranslator();
87 /////////////////////////////////////////////////////////////////////////
91 /////////////////////////////////////////////////////////////////////////
95 InsetInfo::InsetInfo(Buffer * buf, string const & name)
96 : InsetCollapsible(buf), initialized_(false),
97 type_(UNKNOWN_INFO), name_()
104 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
106 // do not allow the cursor to be set in this Inset
107 return Inset::editXY(cur, x, y);
111 string InsetInfo::infoType() const
113 return nameTranslator().find(type_);
117 docstring InsetInfo::layoutName() const
119 return from_ascii("Info:" + infoType());
123 docstring InsetInfo::toolTip(BufferView const &, int, int) const
125 return bformat(_("Information regarding %1$s '%2$s'"),
126 _(infoType()), from_utf8(name_));
130 void InsetInfo::read(Lexer & lex)
135 token = lex.getString();
136 if (token == "type") {
138 token = lex.getString();
139 type_ = nameTranslator().find(token);
140 } else if (token == "arg") {
142 name_ = lex.getString();
143 } else if (token == "\\end_inset")
146 if (token != "\\end_inset") {
147 lex.printError("Missing \\end_inset at this point");
148 throw ExceptionMessage(WarningException,
149 _("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)) {
174 FuncRequest func = lyxaction.lookupFunc(name);
175 return func.action() != LFUN_UNKNOWN_ACTION;
179 FuncCode const action = lyxaction.lookupFunc(name).action();
180 if (action == LFUN_UNKNOWN_ACTION) {
181 string dir = "images";
182 return !imageLibFileSearch(dir, name, "svgz,png").empty();
189 lyxrc.write(oss, true, name);
190 return !oss.str().empty();
198 if (name == "name" || name == "path" || name == "class")
200 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
201 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
202 return buffer().lyxvc().inUse();
206 return name == "version";
213 bool InsetInfo::showInsetDialog(BufferView * bv) const
215 bv->showDialog("info");
220 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
221 FuncStatus & flag) const
223 switch (cmd.action()) {
224 case LFUN_INSET_SETTINGS:
225 return InsetCollapsible::getStatus(cur, cmd, flag);
227 case LFUN_INSET_DIALOG_UPDATE:
228 case LFUN_INSET_COPY_AS:
229 flag.setEnabled(true);
232 case LFUN_INSET_MODIFY:
233 if (validateModifyArgument(cmd.argument())) {
234 flag.setEnabled(true);
245 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
247 switch (cmd.action()) {
248 case LFUN_INSET_MODIFY:
250 setInfo(to_utf8(cmd.argument()));
251 cur.forceBufferUpdate();
252 initialized_ = false;
255 case LFUN_INSET_COPY_AS: {
256 cap::clearSelection();
258 copy.pushBackward(*this);
262 copy.pit() = copy.lastpit();
263 copy.pos() = copy.lastpos();
265 cap::copySelection(copy);
270 InsetCollapsible::doDispatch(cur, cmd);
276 void InsetInfo::setInfo(string const & name)
282 name_ = trim(split(name, type, ' '));
283 type_ = nameTranslator().find(type);
287 void InsetInfo::error(string const & err)
289 setText(bformat(_(err), from_utf8(name_)),
290 Font(inherit_font, buffer().params().language), false);
294 void InsetInfo::setText(docstring const & str)
296 setText(str, Font(inherit_font, buffer().params().language), false);
300 bool InsetInfo::forceLTR() const
302 return !buffer().params().language->rightToLeft();
306 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
307 // If the Buffer is a clone, then we neither need nor want to do any
308 // of what follows. We want, rather, just to inherit how things were
309 // in the original Buffer. This is especially important for VCS.
310 // Otherwise, we could in principle have different settings here
311 // than in the Buffer we were exporting.
312 if (buffer().isClone())
315 BufferParams const & bp = buffer().params();
319 error("Unknown Info: %1$s");
320 initialized_ = false;
323 case SHORTCUTS_INFO: {
324 // shortcuts can change, so we need to re-do this each time
325 FuncRequest const func = lyxaction.lookupFunc(name_);
326 if (func.action() == LFUN_UNKNOWN_ACTION) {
327 error("Unknown action %1$s");
330 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
331 if (bindings.empty()) {
332 // It is impropriate to use error() for undefined shortcut
333 setText(_("undefined"));
336 if (type_ == SHORTCUT_INFO)
337 setText(bindings.begin()->print(KeySequence::Portable));
339 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
343 // this information could change, if the preferences are changed,
344 // so we will recalculate each time through.
347 setText(_("undefined"));
350 // FIXME this uses the serialization mechanism to get the info
351 // we want, which i guess works but is a bit strange.
352 lyxrc.write(oss, true, name_);
353 string result = oss.str();
354 if (result.size() < 2) {
355 setText(_("undefined"));
358 string::size_type loc = result.rfind("\n", result.size() - 2);
359 loc = loc == string::npos ? 0 : loc + 1;
360 if (result.size() < loc + name_.size() + 1
361 || result.substr(loc + 1, name_.size()) != name_) {
362 setText(_("undefined"));
365 // remove leading comments and \\name and space
366 result = result.substr(loc + name_.size() + 2);
369 result = rtrim(result, "\n");
370 result = trim(result, "\"");
371 setText(from_utf8(result));
375 // only need to do this once.
378 // check in packages.lst
379 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
383 case TEXTCLASS_INFO: {
384 // the TextClass can change
385 LayoutFileList const & list = LayoutFileList::get();
386 bool available = false;
387 // name_ is the class name
388 if (list.haveClass(name_))
389 available = list[name_].isTeXClassAvailable();
390 setText(available ? _("yes") : _("no"));
394 // only need to do this once.
397 // and we will not keep trying if we fail
399 docstring_list names;
400 FuncRequest const func = lyxaction.lookupFunc(name_);
401 if (func.action() == LFUN_UNKNOWN_ACTION) {
402 error("Unknown action %1$s");
405 // iterate through the menubackend to find it
407 error("Can't determine menu entry for action %1$s in batch mode");
410 if (!theApp()->searchMenu(func, names)) {
411 error("No menu entry for action %1$s");
414 // if found, return its path.
416 Paragraph & par = paragraphs().front();
417 Font const f(inherit_font, buffer().params().language);
419 //fu.fontInfo().setUnderbar(FONT_ON);
420 for (docstring const & name : names) {
421 // do not insert > for the top level menu item
422 if (&name != &names.front())
423 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
424 f, Change(Change::UNCHANGED));
425 //FIXME: add proper underlines here. This
426 // involves rewriting searchMenu used above to
427 // return a vector of menus. If we do not do
428 // that, we might as well use below
429 // Paragraph::insert on each string (JMarc)
430 for (char_type c : name)
431 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
436 // only need to do this once.
439 // and we will not keep trying if we fail
441 FuncRequest func = lyxaction.lookupFunc(name_);
442 docstring icon_name = frontend::Application::iconName(func, true);
443 // FIXME: We should use the icon directly instead of
444 // going through FileName. The code below won't work
445 // if the icon is embedded in the executable through
446 // the Qt resource system.
447 // This is only a negligible performance problem:
448 // If the installed icon differs from the resource icon the
449 // installed one is preferred anyway, and all icons that are
450 // embedded in the resources are installed as well.
451 FileName file(to_utf8(icon_name));
452 if (file.onlyFileNameWithoutExt() == "unknown") {
453 string dir = "images";
454 FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
460 int percent_scale = 100;
462 // Compute the scale factor for the icon such that its
463 // width on screen is equal to 1em in pixels.
464 // The scale factor is rounded to the integer nearest
465 // to the float value of the ratio 100*iconsize/imgsize.
466 int imgsize = QImage(toqstr(file.absFileName())).width();
468 int iconsize = Length(1, Length::EM).inPixels(1);
469 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
472 InsetGraphics * inset = new InsetGraphics(buffer_);
473 InsetGraphicsParams igp;
475 igp.lyxscale = percent_scale;
476 igp.scale = string();
477 igp.width = Length(1, Length::EM);
478 inset->setParams(igp);
480 Font const f(inherit_font, buffer().params().language);
481 paragraphs().front().insertInset(0, inset, f,
482 Change(Change::UNCHANGED));
486 // this could all change, so we will recalculate each time
487 if (name_ == "name") {
488 setText(from_utf8(buffer().fileName().onlyFileName()));
491 if (name_ == "path") {
492 setText(from_utf8(os::latex_path(buffer().filePath())));
495 if (name_ == "class") {
496 setText(from_utf8(bp.documentClass().name()));
500 ////////////////////////////////////////////////////////////////
501 // everything that follows is for version control.
502 // nothing that isn't version control should go below this line.
504 // this information could change, in principle, so we will
505 // recalculate each time through
506 if (!buffer().lyxvc().inUse()) {
507 setText(_("No version control"));
510 LyXVC::RevisionInfo itype = LyXVC::Unknown;
511 if (name_ == "vcs-revision")
513 else if (name_ == "vcs-tree-revision")
515 else if (name_ == "vcs-author")
516 itype = LyXVC::Author;
517 else if (name_ == "vcs-time")
519 else if (name_ == "vcs-date")
521 string binfo = buffer().lyxvc().revisionInfo(itype);
523 setText(from_ascii(name_) + " unknown");
525 setText(from_utf8(binfo));
529 // only need to do this once.
532 if (name_ == "version")
533 setText(from_ascii(lyx_version));
537 InsetCollapsible::updateBuffer(it, utype);
541 string InsetInfo::contextMenu(BufferView const &, int, int) const
543 //FIXME: We override the implementation of InsetCollapsible,
544 //because this inset is not a collapsible inset.
545 return contextMenuName();
549 string InsetInfo::contextMenuName() const
551 return "context-info";