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 "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "InsetGraphics.h"
20 #include "InsetSpecialChar.h"
22 #include "LaTeXFeatures.h"
23 #include "LayoutFile.h"
24 #include "LyXAction.h"
28 #include "ParagraphParameters.h"
30 #include "frontends/Application.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/docstring_list.h"
35 #include "support/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
44 using namespace lyx::support;
50 typedef Translator<InsetInfo::info_type, string> NameTranslator;
52 NameTranslator const initTranslator()
54 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
56 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
57 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
58 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
59 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
60 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
61 translator.addPair(InsetInfo::MENU_INFO, "menu");
62 translator.addPair(InsetInfo::ICON_INFO, "icon");
63 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
64 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
69 /// The translator between the information type enum and corresponding string.
70 NameTranslator const & nameTranslator()
72 static NameTranslator const translator = initTranslator();
78 /////////////////////////////////////////////////////////////////////////
82 /////////////////////////////////////////////////////////////////////////
86 InsetInfo::InsetInfo(Buffer * buf, string const & name)
87 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
89 setAutoBreakRows(true);
95 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
97 // do not allow the cursor to be set in this Inset
98 return Inset::editXY(cur, x, y);
102 string InsetInfo::infoType() const
104 return nameTranslator().find(type_);
108 docstring InsetInfo::name() const
110 return from_ascii("Info:" + infoType());
114 docstring InsetInfo::toolTip(BufferView const &, int, int) const
116 return bformat(_("Information regarding %1$s '%2$s'"),
117 _(infoType()), from_utf8(name_));
121 void InsetInfo::read(Lexer & lex)
126 token = lex.getString();
127 if (token == "type") {
129 token = lex.getString();
130 type_ = nameTranslator().find(token);
131 } else if (token == "arg") {
133 name_ = lex.getString();
134 } else if (token == "\\end_inset")
137 if (token != "\\end_inset") {
138 lex.printError("Missing \\end_inset at this point");
139 throw ExceptionMessage(WarningException,
140 _("Missing \\end_inset at this point."),
147 void InsetInfo::write(ostream & os) const
149 os << "Info\ntype \"" << infoType()
150 << "\"\narg " << Lexer::quoteString(name_);
154 bool InsetInfo::validateModifyArgument(docstring const & arg) const
157 string const name = trim(split(to_utf8(arg), type, ' '));
158 switch (nameTranslator().find(type)) {
165 FuncRequest func = lyxaction.lookupFunc(name);
166 return func.action() != LFUN_UNKNOWN_ACTION;
170 lyxrc.write(oss, true, name);
171 return !oss.str().empty();
177 return name == "name" || name == "path" || name == "class" ||
178 name == "vcs-revision" || name == "vcs-tree-revision" ||
179 name == "vcs-author" || name == "vcs-date" || name == "vcs-time";
181 return name == "version";
187 bool InsetInfo::showInsetDialog(BufferView * bv) const
189 bv->showDialog("info");
194 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
195 FuncStatus & flag) const
197 switch (cmd.action()) {
198 case LFUN_INSET_SETTINGS:
199 return InsetCollapsable::getStatus(cur, cmd, flag);
201 case LFUN_INSET_DIALOG_UPDATE:
202 flag.setEnabled(true);
205 case LFUN_INSET_MODIFY:
206 if (validateModifyArgument(cmd.argument())) {
207 flag.setEnabled(true);
218 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
220 // allow selection, copy but not cut, delete etc
221 switch (cmd.action()) {
222 case LFUN_INSET_SETTINGS:
223 InsetCollapsable::doDispatch(cur, cmd);
226 case LFUN_INSET_MODIFY:
227 setInfo(to_utf8(cmd.argument()));
236 void InsetInfo::setInfo(string const & name)
242 name_ = trim(split(name, type, ' '));
243 type_ = nameTranslator().find(type);
248 void InsetInfo::error(string const & err)
250 setText(bformat(_(err), from_utf8(name_)),
251 Font(inherit_font, buffer().params().language), false);
255 void InsetInfo::setText(docstring const & str)
257 setText(str, Font(inherit_font, buffer().params().language), false);
261 void InsetInfo::updateInfo()
263 BufferParams const & bp = buffer().params();
267 error("Unknown Info: %1$s");
270 case SHORTCUTS_INFO: {
271 FuncRequest const func = lyxaction.lookupFunc(name_);
272 if (func.action() == LFUN_UNKNOWN_ACTION) {
273 error("Unknown action %1$s");
276 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
277 if (bindings.empty()) {
278 // It is impropriate to use error() for undefined shortcut
279 setText(_("undefined"));
282 if (type_ == SHORTCUT_INFO)
283 setText(bindings.begin()->print(KeySequence::Portable));
285 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
290 lyxrc.write(oss, true, name_);
291 string result = oss.str();
292 // remove leading \\name
293 result = result.substr(name_.size() + 2);
295 result = rtrim(result, "\n");
296 result = trim(result, "\"");
297 setText(from_utf8(result));
301 // check in packages.lst
302 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
304 case TEXTCLASS_INFO: {
305 // name_ is the class name
306 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
310 docstring_list names;
311 FuncRequest const func = lyxaction.lookupFunc(name_);
312 if (func.action() == LFUN_UNKNOWN_ACTION) {
313 error("Unknown action %1$s");
316 // iterate through the menubackend to find it
317 if (!theApp()->searchMenu(func, names)) {
318 error("No menu entry for action %1$s");
321 // if found, return its path.
323 Paragraph & par = paragraphs().front();
324 Font const f(inherit_font, buffer().params().language);
326 //fu.fontInfo().setUnderbar(FONT_ON);
327 docstring_list::const_iterator beg = names.begin();
328 docstring_list::const_iterator end = names.end();
329 for (docstring_list::const_iterator it = beg ;
331 // do not insert > for the top level menu item
333 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
334 Change(Change::UNCHANGED));
335 //FIXME: add proper underlines here. This
336 // involves rewriting searchMenu used above to
337 // return a vector of menus. If we do not do
338 // that, we might as well use below
339 // Paragraph::insert on each string (JMarc)
340 for (size_type i = 0; i != it->length(); ++i)
341 par.insertChar(par.size(), (*it)[i],
342 f, Change(Change::UNCHANGED));
347 FuncRequest func = lyxaction.lookupFunc(name_);
348 docstring icon_name = theApp()->iconName(func, true);
349 //FIXME: We should use the icon directly instead of
350 // going through FileName. The code below won't work
351 // if the icon is embedded in the executable through
352 // the Qt resource system.
353 FileName file(to_utf8(icon_name));
356 InsetGraphics * inset = new InsetGraphics(buffer_);
357 InsetGraphicsParams igp;
359 inset->setParams(igp);
361 paragraphs().front().insertInset(0, inset,
362 Change(Change::UNCHANGED));
366 if (name_ == "name") {
367 setText(from_utf8(buffer().fileName().onlyFileName()));
370 if (name_ == "path") {
371 setText(from_utf8(buffer().filePath()));
374 if (name_ == "class") {
375 setText(from_utf8(bp.documentClass().name()));
379 // everything that follows is for version control.
380 // nothing that isn't version control should go below this line.
381 if (!buffer().lyxvc().inUse()) {
382 setText(_("No version control"));
385 LyXVC::RevisionInfo itype = LyXVC::Unknown;
386 if (name_ == "vcs-revision")
388 else if (name_ == "vcs-tree-revision")
390 else if (name_ == "vcs-author")
391 itype = LyXVC::Author;
392 else if (name_ == "vcs-time")
394 else if (name_ == "vcs-date")
396 string binfo = buffer().lyxvc().revisionInfo(itype);
398 setText(bformat(_("[[%1$s unknown]]"), from_utf8(name_)));
400 setText(from_utf8(binfo));
404 if (name_ == "version")
405 setText(from_ascii(PACKAGE_VERSION));
411 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
413 return from_ascii("context-info");