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), 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."),
156 void InsetInfo::write(ostream & os) const
158 os << "Info\ntype \"" << infoType()
159 << "\"\narg " << Lexer::quoteString(name_);
163 bool InsetInfo::validateModifyArgument(docstring const & arg) const
166 string const name = trim(split(to_utf8(arg), type, ' '));
168 switch (nameTranslator().find(type)) {
175 FuncRequest func = lyxaction.lookupFunc(name);
176 return func.action() != LFUN_UNKNOWN_ACTION;
180 FuncCode const action = lyxaction.lookupFunc(name).action();
181 if (action == LFUN_UNKNOWN_ACTION) {
182 string dir = "images";
183 return !imageLibFileSearch(dir, name, "svgz,png").empty();
190 lyxrc.write(oss, true, name);
191 return !oss.str().empty();
199 if (name == "name" || name == "path" || name == "class")
201 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
202 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
203 return buffer().lyxvc().inUse();
207 return name == "version";
214 bool InsetInfo::showInsetDialog(BufferView * bv) const
216 bv->showDialog("info");
221 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
222 FuncStatus & flag) const
224 switch (cmd.action()) {
225 case LFUN_INSET_SETTINGS:
226 return InsetCollapsible::getStatus(cur, cmd, flag);
228 case LFUN_INSET_DIALOG_UPDATE:
229 case LFUN_INSET_COPY_AS:
230 flag.setEnabled(true);
233 case LFUN_INSET_MODIFY:
234 if (validateModifyArgument(cmd.argument())) {
235 flag.setEnabled(true);
246 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
248 switch (cmd.action()) {
249 case LFUN_INSET_MODIFY:
251 setInfo(to_utf8(cmd.argument()));
254 case LFUN_INSET_COPY_AS: {
255 cap::clearSelection();
257 copy.pushBackward(*this);
261 copy.pit() = copy.lastpit();
262 copy.pos() = copy.lastpos();
264 cap::copySelection(copy);
269 InsetCollapsible::doDispatch(cur, cmd);
275 void InsetInfo::setInfo(string const & name)
281 name_ = trim(split(name, type, ' '));
282 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() || force_ltr_;
306 void InsetInfo::updateInfo()
308 BufferParams const & bp = buffer().params();
313 error("Unknown Info: %1$s");
316 case SHORTCUTS_INFO: {
317 FuncRequest const func = lyxaction.lookupFunc(name_);
318 if (func.action() == LFUN_UNKNOWN_ACTION) {
319 error("Unknown action %1$s");
322 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
323 if (bindings.empty()) {
324 // It is impropriate to use error() for undefined shortcut
325 setText(_("undefined"));
329 if (type_ == SHORTCUT_INFO)
330 sequence = bindings.begin()->print(KeySequence::ForGui);
332 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
333 // QKeySequence returns special characters for keys on the mac
334 // Since these are not included in many fonts, we
335 // re-translate them to textual names (see #10641)
336 odocstringstream ods;
338 string const lcode = bp.language->code();
339 for (size_t n = 0; n < sequence.size(); ++n) {
340 char_type const c = sequence[n];
343 gui = _("Return[[Key]]");
344 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
346 case 0x21b9://Tab both directions (Win)
347 gui = _("Tab[[Key]]");
348 ods << translateIfPossible(from_ascii("Tab[[Key]]"), lcode);
350 case 0x21de://Qt::Key_PageUp
352 ods << translateIfPossible(from_ascii("PgUp"), lcode);
354 case 0x21df://Qt::Key_PageDown
356 ods << translateIfPossible(from_ascii("PgDown"), lcode);
358 case 0x21e4://Qt::Key_Backtab
360 ods << translateIfPossible(from_ascii("Backtab"), lcode);
362 case 0x21e5://Qt::Key_Tab
364 ods << translateIfPossible(from_ascii("Tab"), lcode);
368 ods << translateIfPossible(from_ascii("Shift"), lcode);
370 case 0x21ea://Qt::Key_CapsLock
372 ods << translateIfPossible(from_ascii("CapsLock"), lcode);
374 case 0x2303://Control
375 gui = _("Control[[Key]]");
376 ods << translateIfPossible(from_ascii("Control[[Key]]"), lcode);
379 gui = _("Command[[Key]]");
380 ods << translateIfPossible(from_ascii("Command[[Key]]"), lcode);
382 case 0x2324://Qt::Key_Enter
383 gui = _("Return[[Key]]");
384 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
386 case 0x2325://Option key
387 gui = _("Option[[Key]]");
388 ods << translateIfPossible(from_ascii("Option[[Key]]"), lcode);
390 case 0x2326://Qt::Key_Delete
391 gui = _("Delete[[Key]]");
392 ods << translateIfPossible(from_ascii("Delete[[Key]]"), lcode);
394 case 0x232b://Qt::Key_Backspace
396 ods << translateIfPossible(from_ascii("Fn+Delete"), lcode);
398 case 0x238b://Qt::Key_Escape
400 ods << translateIfPossible(from_ascii("Esc"), lcode);
407 force_ltr_ = !bp.language->rightToLeft();
413 setText(_("undefined"));
416 lyxrc.write(oss, true, name_);
417 string result = oss.str();
418 if (result.size() < 2) {
419 setText(_("undefined"));
422 string::size_type loc = result.rfind("\n", result.size() - 2);
423 loc = loc == string::npos ? 0 : loc + 1;
424 if (result.size() < loc + name_.size() + 1
425 || result.substr(loc + 1, name_.size()) != name_) {
426 setText(_("undefined"));
429 // remove leading comments and \\name and space
430 result = result.substr(loc + name_.size() + 2);
433 result = rtrim(result, "\n");
434 result = trim(result, "\"");
435 setText(from_utf8(result));
439 // check in packages.lst
440 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
443 case TEXTCLASS_INFO: {
444 // name_ is the class name
445 LayoutFileList const & list = LayoutFileList::get();
446 bool available = false;
447 if (list.haveClass(name_))
448 available = list[name_].isTeXClassAvailable();
449 setText(available ? _("yes") : _("no"));
453 docstring_list names;
454 FuncRequest const func = lyxaction.lookupFunc(name_);
455 if (func.action() == LFUN_UNKNOWN_ACTION) {
456 error("Unknown action %1$s");
459 // iterate through the menubackend to find it
461 error("Can't determine menu entry for action %1$s in batch mode");
464 if (!theApp()->searchMenu(func, names)) {
465 error("No menu entry for action %1$s");
468 // if found, return its path.
470 Paragraph & par = paragraphs().front();
471 Font const f(inherit_font, buffer().params().language);
473 //fu.fontInfo().setUnderbar(FONT_ON);
474 docstring_list::const_iterator beg = names.begin();
475 docstring_list::const_iterator end = names.end();
476 for (docstring_list::const_iterator it = beg ;
478 // do not insert > for the top level menu item
480 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
481 f, Change(Change::UNCHANGED));
482 //FIXME: add proper underlines here. This
483 // involves rewriting searchMenu used above to
484 // return a vector of menus. If we do not do
485 // that, we might as well use below
486 // Paragraph::insert on each string (JMarc)
487 for (size_type i = 0; i != it->length(); ++i)
488 par.insertChar(par.size(), (*it)[i],
489 f, Change(Change::UNCHANGED));
494 FuncRequest func = lyxaction.lookupFunc(name_);
495 docstring icon_name = frontend::Application::iconName(func, true);
496 // FIXME: We should use the icon directly instead of
497 // going through FileName. The code below won't work
498 // if the icon is embedded in the executable through
499 // the Qt resource system.
500 // This is only a negligible performance problem:
501 // If the installed icon differs from the resource icon the
502 // installed one is preferred anyway, and all icons that are
503 // embedded in the resources are installed as well.
504 FileName file(to_utf8(icon_name));
505 if (file.onlyFileNameWithoutExt() == "unknown") {
506 string dir = "images";
507 FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
513 int percent_scale = 100;
515 // Compute the scale factor for the icon such that its
516 // width on screen is equal to 1em in pixels.
517 // The scale factor is rounded to the integer nearest
518 // to the float value of the ratio 100*iconsize/imgsize.
519 int imgsize = QImage(toqstr(file.absFileName())).width();
521 int iconsize = Length(1, Length::EM).inPixels(1);
522 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
525 InsetGraphics * inset = new InsetGraphics(buffer_);
526 InsetGraphicsParams igp;
528 igp.lyxscale = percent_scale;
529 igp.scale = string();
530 igp.width = Length(1, Length::EM);
531 inset->setParams(igp);
533 Font const f(inherit_font, buffer().params().language);
534 paragraphs().front().insertInset(0, inset, f,
535 Change(Change::UNCHANGED));
539 if (name_ == "name") {
540 setText(from_utf8(buffer().fileName().onlyFileName()));
543 if (name_ == "path") {
544 setText(from_utf8(os::latex_path(buffer().filePath())));
547 if (name_ == "class") {
548 setText(from_utf8(bp.documentClass().name()));
552 // everything that follows is for version control.
553 // nothing that isn't version control should go below this line.
554 if (!buffer().lyxvc().inUse()) {
555 setText(_("No version control"));
558 LyXVC::RevisionInfo itype = LyXVC::Unknown;
559 if (name_ == "vcs-revision")
561 else if (name_ == "vcs-tree-revision")
563 else if (name_ == "vcs-author")
564 itype = LyXVC::Author;
565 else if (name_ == "vcs-time")
567 else if (name_ == "vcs-date")
569 string binfo = buffer().lyxvc().revisionInfo(itype);
571 setText(from_ascii(name_) + " unknown");
573 setText(from_utf8(binfo));
577 if (name_ == "version")
578 setText(from_ascii(lyx_version));
584 string InsetInfo::contextMenu(BufferView const &, int, int) const
586 //FIXME: We override the implementation of InsetCollapsible,
587 //because this inset is not a collapsible inset.
588 return contextMenuName();
592 string InsetInfo::contextMenuName() const
594 return "context-info";