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"
27 #include "MetricsInfo.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;
49 InsetInfo::InsetInfo(Buffer const & buf, string const & name)
50 : InsetText(buf), type_(UNKNOWN_INFO), name_(),
53 setAutoBreakRows(true);
59 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
62 return InsetText::editXY(cur, x, y);
66 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
68 InsetText::draw(pi, x, y);
72 string InsetInfo::infoType() const
74 return nameTranslator().find(type_);
78 docstring InsetInfo::toolTip(BufferView const &, int, int) const
81 os << _("Information regarding ")
82 << _(nameTranslator().find(type_))
83 << " " << from_utf8(name_);
89 Translator<InsetInfo::info_type, string> const initTranslator()
91 Translator<InsetInfo::info_type, string>
92 translator(InsetInfo::UNKNOWN_INFO, "unknown");
94 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
95 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
96 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
97 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
98 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
99 translator.addPair(InsetInfo::MENU_INFO, "menu");
100 translator.addPair(InsetInfo::ICON_INFO, "icon");
101 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
108 Translator<InsetInfo::info_type, string>
109 const & InsetInfo::nameTranslator() const
111 static Translator<info_type, string> const translator = initTranslator();
117 void InsetInfo::read(Lexer & lex)
122 token = lex.getString();
123 if (token == "type") {
125 token = lex.getString();
126 type_ = nameTranslator().find(token);
127 } else if (token == "arg") {
129 name_ = lex.getString();
130 } else if (token == "\\end_inset")
133 if (token != "\\end_inset") {
134 lex.printError("Missing \\end_inset at this point");
135 throw ExceptionMessage(WarningException,
136 _("Missing \\end_inset at this point."),
143 void InsetInfo::write(ostream & os) const
145 os << "Info\ntype \"" << nameTranslator().find(type_)
146 << "\"\narg \"" << name_ << '\"';
150 bool InsetInfo::validate(docstring const & arg) const
155 name = trim(split(to_utf8(arg), type, ' '));
156 switch (nameTranslator().find(type)) {
163 FuncRequest func = lyxaction.lookupFunc(name);
164 return func.action != LFUN_UNKNOWN_ACTION;
168 lyxrc.write(oss, true, name);
169 return !oss.str().empty();
175 return name_ == "name" || name_ == "path" || name_ == "class";
181 bool InsetInfo::showInsetDialog(BufferView * bv) const
183 bv->showDialog("info");
188 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
189 FuncStatus & flag) const
191 switch (cmd.action) {
192 case LFUN_MOUSE_PRESS:
193 case LFUN_MOUSE_RELEASE:
194 case LFUN_MOUSE_MOTION:
195 case LFUN_MOUSE_DOUBLE:
196 case LFUN_MOUSE_TRIPLE:
198 case LFUN_INSET_SETTINGS:
199 return InsetText::getStatus(cur, cmd, flag);
201 case LFUN_INSET_MODIFY:
202 flag.setEnabled(true);
212 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
214 // allow selection, copy but not cut, delete etc
215 switch (cmd.action) {
216 case LFUN_MOUSE_PRESS:
217 case LFUN_MOUSE_RELEASE:
218 case LFUN_MOUSE_MOTION:
219 case LFUN_MOUSE_DOUBLE:
220 case LFUN_MOUSE_TRIPLE:
222 case LFUN_INSET_SETTINGS:
223 InsetText::doDispatch(cur, cmd);
226 case LFUN_INSET_MODIFY:
227 setInfo(to_utf8(cmd.argument()));
237 void InsetInfo::setInfo(string const & name)
243 name_ = trim(split(name, type, ' '));
244 type_ = nameTranslator().find(type);
249 void InsetInfo::error(string const & err)
251 InsetText::setText(bformat(_(err), from_utf8(name_)), buffer().params().getFont(),
256 void InsetInfo::setText(docstring const & str)
258 InsetText::setText(str, buffer().params().getFont(), false);
262 void InsetInfo::updateInfo()
266 BufferParams const & bp = buffer().params();
270 error("Unknown Info: %1$s");
273 case SHORTCUTS_INFO: {
274 FuncRequest func = lyxaction.lookupFunc(name_);
275 if (func.action == LFUN_UNKNOWN_ACTION) {
276 error("Unknown action %1$s");
279 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
280 if (bindings.empty()) {
281 // It is impropriate to use error() for undefined shortcut
282 setText(_("undefined"));
285 if (type_ == SHORTCUT_INFO)
286 setText(bindings.rbegin()->print(KeySequence::Portable));
288 setText(theTopLevelKeymap().printBindings(func));
293 lyxrc.write(oss, true, name_);
294 string result = oss.str();
295 // remove leading \\name
296 result = result.substr(name_.size() + 2);
298 result = rtrim(result, "\n");
299 result = trim(result, "\"");
300 setText(from_utf8(result));
304 // check in packages.lst
305 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
307 case TEXTCLASS_INFO: {
308 // name_ is the class name
309 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
313 docstring_list names;
314 FuncRequest func = lyxaction.lookupFunc(name_);
315 if (func.action == LFUN_UNKNOWN_ACTION) {
316 error("Unknown action %1$s");
319 // iterate through the menubackend to find it
320 if (!theApp()->searchMenu(func, names)) {
321 error("No menu entry for action %1$s");
324 // if find, return its path.
326 Paragraph & info = paragraphs().front();
328 while (!names.empty()) {
329 // do not insert > for the top level menu item
331 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
332 Change(Change::UNCHANGED));
333 for (i = 0; i != names.back().length(); ++i)
334 info.insertChar(i, names.back()[i], bp.getFont(), false);
340 FuncRequest func = lyxaction.lookupFunc(name_);
341 docstring icon_name = theApp()->iconName(func, true);
342 //FIXME: We should use the icon directly instead of going through
343 // FileName. The code below won't work if the icon is embedded in the
344 // executable through the Qt resource system.
345 FileName file(to_utf8(icon_name));
348 InsetGraphicsParams igp;
350 InsetGraphics * inset = new InsetGraphics(buffer());
351 inset->setParams(igp);
352 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
357 setText(from_utf8(buffer().fileName().onlyFileName()));
358 else if (name_ == "path")
359 setText(from_utf8(buffer().filePath()));
360 else if (name_ == "class")
361 setText(from_utf8(bp.documentClass().name()));
363 setText(_("Unknown buffer info"));
368 paragraphs().begin()->params().noindent(true);
372 bool InsetInfo::setMouseHover(bool mouse_hover)
374 mouse_hover_ = mouse_hover;
379 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
381 return from_ascii("context-info");