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 : InsetCollapsible(buf), initialized_(false),
96 type_(UNKNOWN_INFO), name_()
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."),
154 void InsetInfo::write(ostream & os) const
156 os << "Info\ntype \"" << infoType()
157 << "\"\narg " << Lexer::quoteString(name_);
161 bool InsetInfo::validateModifyArgument(docstring const & arg) const
164 string const name = trim(split(to_utf8(arg), type, ' '));
166 switch (nameTranslator().find(type)) {
173 FuncRequest func = lyxaction.lookupFunc(name);
174 return func.action() != LFUN_UNKNOWN_ACTION;
178 FuncCode const action = lyxaction.lookupFunc(name).action();
179 if (action == LFUN_UNKNOWN_ACTION) {
180 string dir = "images";
181 return !imageLibFileSearch(dir, name, "svgz,png").empty();
188 lyxrc.write(oss, true, name);
189 return !oss.str().empty();
197 if (name == "name" || name == "path" || name == "class")
199 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
200 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
201 return buffer().lyxvc().inUse();
205 return name == "version";
212 bool InsetInfo::showInsetDialog(BufferView * bv) const
214 bv->showDialog("info");
219 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
220 FuncStatus & flag) const
222 switch (cmd.action()) {
223 case LFUN_INSET_SETTINGS:
224 return InsetCollapsible::getStatus(cur, cmd, flag);
226 case LFUN_INSET_DIALOG_UPDATE:
227 case LFUN_INSET_COPY_AS:
228 flag.setEnabled(true);
231 case LFUN_INSET_MODIFY:
232 if (validateModifyArgument(cmd.argument())) {
233 flag.setEnabled(true);
244 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
246 switch (cmd.action()) {
247 case LFUN_INSET_MODIFY:
249 setInfo(to_utf8(cmd.argument()));
250 initialized_ = false;
253 case LFUN_INSET_COPY_AS: {
254 cap::clearSelection();
256 copy.pushBackward(*this);
260 copy.pit() = copy.lastpit();
261 copy.pos() = copy.lastpos();
263 cap::copySelection(copy);
268 InsetCollapsible::doDispatch(cur, cmd);
274 void InsetInfo::setInfo(string const & name)
280 name_ = trim(split(name, type, ' '));
281 type_ = nameTranslator().find(type);
285 void InsetInfo::error(string const & err)
287 setText(bformat(_(err), from_utf8(name_)),
288 Font(inherit_font, buffer().params().language), false);
292 void InsetInfo::setText(docstring const & str)
294 setText(str, Font(inherit_font, buffer().params().language), false);
298 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
299 BufferParams const & bp = buffer().params();
303 error("Unknown Info: %1$s");
304 initialized_ = false;
307 case SHORTCUTS_INFO: {
308 // only need to do this once.
311 // and we will not keep trying if we fail
313 FuncRequest const func = lyxaction.lookupFunc(name_);
314 if (func.action() == LFUN_UNKNOWN_ACTION) {
315 error("Unknown action %1$s");
318 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
319 if (bindings.empty()) {
320 // It is impropriate to use error() for undefined shortcut
321 setText(_("undefined"));
324 if (type_ == SHORTCUT_INFO)
325 setText(bindings.begin()->print(KeySequence::Portable));
327 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
331 // this information could actually change, if the preferences are changed,
332 // so we will recalculate each time through.
335 setText(_("undefined"));
338 // FIXME this uses the serialization mechanism to get the info
339 // we want, which i guess works but is a bit strange.
340 lyxrc.write(oss, true, name_);
341 string result = oss.str();
342 if (result.size() < 2) {
343 setText(_("undefined"));
346 string::size_type loc = result.rfind("\n", result.size() - 2);
347 loc = loc == string::npos ? 0 : loc + 1;
348 if (result.size() < loc + name_.size() + 1
349 || result.substr(loc + 1, name_.size()) != name_) {
350 setText(_("undefined"));
353 // remove leading comments and \\name and space
354 result = result.substr(loc + name_.size() + 2);
357 result = rtrim(result, "\n");
358 result = trim(result, "\"");
359 setText(from_utf8(result));
363 // only need to do this once.
366 // check in packages.lst
367 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
371 case TEXTCLASS_INFO: {
372 // only need to do this once.
375 // name_ is the class name
376 LayoutFileList const & list = LayoutFileList::get();
377 bool available = false;
378 if (list.haveClass(name_))
379 available = list[name_].isTeXClassAvailable();
380 setText(available ? _("yes") : _("no"));
385 // only need to do this once.
388 // and we will not keep trying if we fail
390 docstring_list names;
391 FuncRequest const func = lyxaction.lookupFunc(name_);
392 if (func.action() == LFUN_UNKNOWN_ACTION) {
393 error("Unknown action %1$s");
396 // iterate through the menubackend to find it
398 error("Can't determine menu entry for action %1$s in batch mode");
401 if (!theApp()->searchMenu(func, names)) {
402 error("No menu entry for action %1$s");
405 // if found, return its path.
407 Paragraph & par = paragraphs().front();
408 Font const f(inherit_font, buffer().params().language);
410 //fu.fontInfo().setUnderbar(FONT_ON);
411 docstring_list::const_iterator beg = names.begin();
412 docstring_list::const_iterator end = names.end();
413 for (docstring_list::const_iterator it = beg ;
415 // do not insert > for the top level menu item
417 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
418 f, Change(Change::UNCHANGED));
419 //FIXME: add proper underlines here. This
420 // involves rewriting searchMenu used above to
421 // return a vector of menus. If we do not do
422 // that, we might as well use below
423 // Paragraph::insert on each string (JMarc)
424 for (size_type i = 0; i != it->length(); ++i)
425 par.insertChar(par.size(), (*it)[i],
426 f, Change(Change::UNCHANGED));
431 // only need to do this once.
434 // and we will not keep trying if we fail
436 FuncRequest func = lyxaction.lookupFunc(name_);
437 docstring icon_name = frontend::Application::iconName(func, true);
438 // FIXME: We should use the icon directly instead of
439 // going through FileName. The code below won't work
440 // if the icon is embedded in the executable through
441 // the Qt resource system.
442 // This is only a negligible performance problem:
443 // If the installed icon differs from the resource icon the
444 // installed one is preferred anyway, and all icons that are
445 // embedded in the resources are installed as well.
446 FileName file(to_utf8(icon_name));
447 if (file.onlyFileNameWithoutExt() == "unknown") {
448 string dir = "images";
449 FileName file2(imageLibFileSearch(dir, name_, "svgz,png"));
455 int percent_scale = 100;
457 // Compute the scale factor for the icon such that its
458 // width on screen is equal to 1em in pixels.
459 // The scale factor is rounded to the integer nearest
460 // to the float value of the ratio 100*iconsize/imgsize.
461 int imgsize = QImage(toqstr(file.absFileName())).width();
463 int iconsize = Length(1, Length::EM).inPixels(1);
464 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
467 InsetGraphics * inset = new InsetGraphics(buffer_);
468 InsetGraphicsParams igp;
470 igp.lyxscale = percent_scale;
471 igp.scale = string();
472 igp.width = Length(1, Length::EM);
473 inset->setParams(igp);
475 Font const f(inherit_font, buffer().params().language);
476 paragraphs().front().insertInset(0, inset, f,
477 Change(Change::UNCHANGED));
481 // this could all change, so we will recalculate each time
482 if (name_ == "name") {
483 setText(from_utf8(buffer().fileName().onlyFileName()));
486 if (name_ == "path") {
487 setText(from_utf8(os::latex_path(buffer().filePath())));
490 if (name_ == "class") {
491 setText(from_utf8(bp.documentClass().name()));
495 ////////////////////////////////////////////////////////////////
496 // everything that follows is for version control.
497 // nothing that isn't version control should go below this line.
499 // this information could change, in principle, so we will
500 // recalculate each time through
501 if (!buffer().lyxvc().inUse()) {
502 setText(_("No version control"));
505 LyXVC::RevisionInfo itype = LyXVC::Unknown;
506 if (name_ == "vcs-revision")
508 else if (name_ == "vcs-tree-revision")
510 else if (name_ == "vcs-author")
511 itype = LyXVC::Author;
512 else if (name_ == "vcs-time")
514 else if (name_ == "vcs-date")
516 string binfo = buffer().lyxvc().revisionInfo(itype);
518 setText(from_ascii(name_) + " unknown");
520 setText(from_utf8(binfo));
524 // only need to do this once.
527 if (name_ == "version")
528 setText(from_ascii(lyx_version));
532 InsetCollapsible::updateBuffer(it, utype);
536 string InsetInfo::contextMenu(BufferView const &, int, int) const
538 //FIXME: We override the implementation of InsetCollapsible,
539 //because this inset is not a collapsible inset.
540 return contextMenuName();
544 string InsetInfo::contextMenuName() const
546 return "context-info";