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"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "InsetGraphics.h"
22 #include "InsetSpecialChar.h"
24 #include "LaTeXFeatures.h"
26 #include "LayoutFile.h"
28 #include "LyXAction.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
34 #include "ParagraphParameters.h"
37 #include "frontends/Application.h"
39 #include "support/convert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/docstring_list.h"
43 #include "support/ExceptionMessage.h"
44 #include "support/FileName.h"
45 #include "support/filetools.h"
46 #include "support/gettext.h"
47 #include "support/Messages.h"
48 #include "support/lstrings.h"
49 #include "support/qstring_helpers.h"
50 #include "support/Translator.h"
54 #include <QtGui/QImage>
57 using namespace lyx::support;
63 typedef Translator<InsetInfo::info_type, string> NameTranslator;
65 NameTranslator const initTranslator()
67 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
69 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
70 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
71 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
72 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
73 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
74 translator.addPair(InsetInfo::MENU_INFO, "menu");
75 translator.addPair(InsetInfo::ICON_INFO, "icon");
76 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
77 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
82 /// The translator between the information type enum and corresponding string.
83 NameTranslator const & nameTranslator()
85 static NameTranslator const translator = initTranslator();
91 /////////////////////////////////////////////////////////////////////////
95 /////////////////////////////////////////////////////////////////////////
99 InsetInfo::InsetInfo(Buffer * buf, string const & name)
100 : InsetCollapsible(buf), initialized_(false),
101 type_(UNKNOWN_INFO), name_(), force_ltr_(false)
108 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
110 // do not allow the cursor to be set in this Inset
111 return Inset::editXY(cur, x, y);
115 string InsetInfo::infoType() const
117 return nameTranslator().find(type_);
121 docstring InsetInfo::layoutName() const
123 return from_ascii("Info:" + infoType());
127 docstring InsetInfo::toolTip(BufferView const &, int, int) const
130 switch (nameTranslator().find(infoType())) {
132 result = _("Invalid information inset");
135 result = bformat(_("The keybard shortcut for the function '%1$s'"),
139 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
143 result = bformat(_("The menu location for the function '%1$s'"),
147 result = bformat(_("The toolbar icon for the function '%1$s'"),
151 result = bformat(_("The preference setting for the preference key '%1$s'"),
155 result = bformat(_("Availability of the LaTeX package '%1$s'"),
159 result = bformat(_("Availability of the LaTeX class '%1$s'"),
164 result = _("The name of this file");
165 else if (name_ == "path")
166 result = _("The path were this file is saved");
167 else if (name_ == "class")
168 result = _("The class this document uses");
169 else if (name_ == "vcs-revision")
170 result = _("Version control revision");
171 else if (name_ == "vcs-tree-revision")
172 result = _("Version control tree revision");
173 else if (name_ == "vcs-author")
174 result = _("Version control author");
175 else if (name_ == "vcs-date")
176 result = _("Version control date");
177 else if (name_ == "vcs-time")
178 result = _("Version control time");
181 result = _("The current LyX version");
189 void InsetInfo::read(Lexer & lex)
194 token = lex.getString();
195 if (token == "type") {
197 token = lex.getString();
198 type_ = nameTranslator().find(token);
199 } else if (token == "arg") {
201 name_ = lex.getString();
202 } else if (token == "\\end_inset")
205 if (token != "\\end_inset") {
206 lex.printError("Missing \\end_inset at this point");
207 throw ExceptionMessage(WarningException,
208 _("Missing \\end_inset at this point."),
214 void InsetInfo::write(ostream & os) const
216 os << "Info\ntype \"" << infoType()
217 << "\"\narg " << Lexer::quoteString(name_);
221 bool InsetInfo::validateModifyArgument(docstring const & arg) const
224 string const name = trim(split(to_utf8(arg), type, ' '));
226 switch (nameTranslator().find(type)) {
233 FuncRequest func = lyxaction.lookupFunc(name);
234 return func.action() != LFUN_UNKNOWN_ACTION;
238 FuncCode const action = lyxaction.lookupFunc(name).action();
239 if (action == LFUN_UNKNOWN_ACTION) {
240 string dir = "images";
241 return !imageLibFileSearch(dir, name, "svgz,png").empty();
248 lyxrc.write(oss, true, name);
249 return !oss.str().empty();
257 if (name == "name" || name == "path" || name == "class")
259 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
260 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
261 return buffer().lyxvc().inUse();
265 return name == "version";
272 bool InsetInfo::showInsetDialog(BufferView * bv) const
274 bv->showDialog("info");
279 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
280 FuncStatus & flag) const
282 switch (cmd.action()) {
283 case LFUN_INSET_SETTINGS:
284 return InsetCollapsible::getStatus(cur, cmd, flag);
286 case LFUN_INSET_DIALOG_UPDATE:
287 case LFUN_INSET_COPY_AS:
288 flag.setEnabled(true);
291 case LFUN_INSET_MODIFY:
292 if (validateModifyArgument(cmd.argument())) {
293 flag.setEnabled(true);
304 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
306 switch (cmd.action()) {
307 case LFUN_INSET_MODIFY:
309 setInfo(to_utf8(cmd.argument()));
310 cur.forceBufferUpdate();
311 initialized_ = false;
314 case LFUN_INSET_COPY_AS: {
315 cap::clearSelection();
317 copy.pushBackward(*this);
321 copy.pit() = copy.lastpit();
322 copy.pos() = copy.lastpos();
324 cap::copySelection(copy);
329 InsetCollapsible::doDispatch(cur, cmd);
335 void InsetInfo::setInfo(string const & name)
341 name_ = trim(split(name, type, ' '));
342 type_ = nameTranslator().find(type);
346 void InsetInfo::error(docstring const & err, Language const * lang)
348 setText(bformat(translateIfPossible(err, lang->code()), from_utf8(name_)),
349 Font(inherit_font, lang), false);
353 void InsetInfo::info(docstring const & err, Language const * lang)
355 setText(translateIfPossible(err, lang->code()),
356 Font(inherit_font, lang), false);
360 void InsetInfo::setText(docstring const & str, Language const * lang)
362 setText(str, Font(inherit_font, lang), false);
366 bool InsetInfo::forceLTR() const
372 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
373 // If the Buffer is a clone, then we neither need nor want to do any
374 // of what follows. We want, rather, just to inherit how things were
375 // in the original Buffer. This is especially important for VCS.
376 // Otherwise, we could in principle have different settings here
377 // than in the Buffer we were exporting.
378 if (buffer().isClone())
381 BufferParams const & bp = buffer().params();
382 Language const * lang = it.paragraph().getFontSettings(bp, it.pos()).language();
383 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
384 // Some info insets use the language of the GUI (if available)
385 Language const * guilang = tryguilang ? tryguilang : lang;
387 force_ltr_ = !lang->rightToLeft();
388 // This is just to get the string into the po files
392 gui = _("Unknown Info!");
393 info(from_ascii("Unknown Info!"), lang);
394 initialized_ = false;
397 case SHORTCUTS_INFO: {
398 // shortcuts can change, so we need to re-do this each time
399 FuncRequest const func = lyxaction.lookupFunc(name_);
400 if (func.action() == LFUN_UNKNOWN_ACTION) {
401 gui = _("Unknown action %1$s");
402 error(from_ascii("Unknown action %1$s"), lang);
405 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
406 if (bindings.empty()) {
407 gui = _("undefined");
408 info(from_ascii("undefined"), lang);
411 if (type_ == SHORTCUT_INFO)
412 setText(bindings.begin()->print(KeySequence::Portable), guilang);
414 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable), guilang);
415 force_ltr_ = !guilang->rightToLeft() && !lang->rightToLeft();
419 // this information could change, if the preferences are changed,
420 // so we will recalculate each time through.
423 gui = _("undefined");
424 info(from_ascii("undefined"), lang);
427 // FIXME this uses the serialization mechanism to get the info
428 // we want, which i guess works but is a bit strange.
429 lyxrc.write(oss, true, name_);
430 string result = oss.str();
431 if (result.size() < 2) {
432 gui = _("undefined");
433 info(from_ascii("undefined"), lang);
436 string::size_type loc = result.rfind("\n", result.size() - 2);
437 loc = loc == string::npos ? 0 : loc + 1;
438 if (result.size() < loc + name_.size() + 1
439 || result.substr(loc + 1, name_.size()) != name_) {
440 gui = _("undefined");
441 info(from_ascii("undefined"), lang);
444 // remove leading comments and \\name and space
445 result = result.substr(loc + name_.size() + 2);
448 result = rtrim(result, "\n");
449 result = trim(result, "\"");
450 setText(from_utf8(result), lang);
454 // only need to do this once.
457 // check in packages.lst
458 if (LaTeXFeatures::isAvailable(name_)) {
460 info(from_ascii("yes"), lang);
463 info(from_ascii("no"), lang);
468 case TEXTCLASS_INFO: {
469 // the TextClass can change
470 LayoutFileList const & list = LayoutFileList::get();
471 bool available = false;
472 // name_ is the class name
473 if (list.haveClass(name_))
474 available = list[name_].isTeXClassAvailable();
477 info(from_ascii("yes"), lang);
480 info(from_ascii("no"), lang);
485 // only need to do this once.
488 // and we will not keep trying if we fail
490 docstring_list names;
491 FuncRequest const func = lyxaction.lookupFunc(name_);
492 if (func.action() == LFUN_UNKNOWN_ACTION) {
493 gui = _("Unknown action %1$s");
494 error(from_ascii("Unknown action %1$s"), lang);
497 // iterate through the menubackend to find it
499 gui = _("Can't determine menu entry for action %1$s in batch mode");
500 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), lang);
503 if (!theApp()->searchMenu(func, names)) {
504 gui = _("No menu entry for action %1$s");
505 error(from_ascii("No menu entry for action %1$s"), lang);
508 // if found, return its path.
510 Paragraph & par = paragraphs().front();
511 Font const f(inherit_font, guilang);
512 force_ltr_ = !guilang->rightToLeft();
514 //fu.fontInfo().setUnderbar(FONT_ON);
515 for (docstring const & name : names) {
516 // do not insert > for the top level menu item
517 if (&name != &names.front())
518 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
519 f, Change(Change::UNCHANGED));
520 //FIXME: add proper underlines here. This
521 // involves rewriting searchMenu used above to
522 // return a vector of menus. If we do not do
523 // that, we might as well use below
524 // Paragraph::insert on each string (JMarc)
525 for (char_type c : name)
526 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
531 // only need to do this once.
534 // and we will not keep trying if we fail
536 FuncRequest func = lyxaction.lookupFunc(name_);
537 docstring icon_name = frontend::Application::iconName(func, true);
538 // FIXME: We should use the icon directly instead of
539 // going through FileName. The code below won't work
540 // if the icon is embedded in the executable through
541 // the Qt resource system.
542 // This is only a negligible performance problem:
543 // If the installed icon differs from the resource icon the
544 // installed one is preferred anyway, and all icons that are
545 // embedded in the resources are installed as well.
546 FileName file(to_utf8(icon_name));
547 if (file.onlyFileNameWithoutExt() == "unknown") {
548 string dir = "images";
549 FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
555 int percent_scale = 100;
557 // Compute the scale factor for the icon such that its
558 // width on screen is equal to 1em in pixels.
559 // The scale factor is rounded to the integer nearest
560 // to the float value of the ratio 100*iconsize/imgsize.
561 int imgsize = QImage(toqstr(file.absFileName())).width();
563 int iconsize = Length(1, Length::EM).inPixels(1);
564 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
567 InsetGraphics * inset = new InsetGraphics(buffer_);
568 InsetGraphicsParams igp;
570 igp.lyxscale = percent_scale;
571 igp.scale = string();
572 igp.width = Length(1, Length::EM);
573 inset->setParams(igp);
575 Font const f(inherit_font, lang);
576 paragraphs().front().insertInset(0, inset, f,
577 Change(Change::UNCHANGED));
581 // this could all change, so we will recalculate each time
582 if (name_ == "name") {
583 setText(from_utf8(buffer().fileName().onlyFileName()), lang);
586 if (name_ == "path") {
587 setText(from_utf8(os::latex_path(buffer().filePath())), lang);
590 if (name_ == "class") {
591 setText(from_utf8(bp.documentClass().name()), lang);
595 ////////////////////////////////////////////////////////////////
596 // everything that follows is for version control.
597 // nothing that isn't version control should go below this line.
599 // this information could change, in principle, so we will
600 // recalculate each time through
601 if (!buffer().lyxvc().inUse()) {
602 gui = _("No version control");
603 info(from_ascii("No version control"), lang);
606 LyXVC::RevisionInfo itype = LyXVC::Unknown;
607 if (name_ == "vcs-revision")
609 else if (name_ == "vcs-tree-revision")
611 else if (name_ == "vcs-author")
612 itype = LyXVC::Author;
613 else if (name_ == "vcs-time")
615 else if (name_ == "vcs-date")
617 string binfo = buffer().lyxvc().revisionInfo(itype);
619 gui = _("%1$s[[vcs data]] unknown");
620 error(from_ascii("%1$s[[vcs data]] unknown"), lang);
622 setText(from_utf8(binfo), lang);
626 // only need to do this once.
629 if (name_ == "version")
630 setText(from_ascii(lyx_version), lang);
634 // Just to do something with that string
635 LYXERR(Debug::INFO, "info inset text: " << gui);
636 InsetCollapsible::updateBuffer(it, utype);
640 string InsetInfo::contextMenu(BufferView const &, int, int) const
642 //FIXME: We override the implementation of InsetCollapsible,
643 //because this inset is not a collapsible inset.
644 return contextMenuName();
648 string InsetInfo::contextMenuName() const
650 return "context-info";