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"
14 #include "LayoutFile.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "FuncRequest.h"
19 #include "InsetSpecialChar.h"
21 #include "LaTeXFeatures.h"
22 #include "LyXAction.h"
25 #include "MetricsInfo.h"
26 #include "ParagraphParameters.h"
28 #include "frontends/Application.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/docstring_list.h"
33 #include "support/FileName.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/ExceptionMessage.h"
41 using namespace lyx::support;
46 InsetInfo::InsetInfo(Buffer const & buf, string const & name)
47 : InsetText(buf), type_(UNKNOWN_INFO), name_(),
50 setAutoBreakRows(true);
56 Inset * InsetInfo::editXY(Cursor &, int, int)
62 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
64 InsetText::draw(pi, x, y);
68 docstring InsetInfo::toolTip(BufferView const &, int, int) const
71 os << _("Information regarding ")
72 << _(nameTranslator().find(type_))
73 << " " << from_utf8(name_);
79 Translator<InsetInfo::info_type, string> const initTranslator()
81 Translator<InsetInfo::info_type, string>
82 translator(InsetInfo::UNKNOWN_INFO, "unknown");
84 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
85 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
86 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
87 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
88 translator.addPair(InsetInfo::MENU_INFO, "menu");
89 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
96 Translator<InsetInfo::info_type, string>
97 const & InsetInfo::nameTranslator() const
99 static Translator<info_type, string> const translator = initTranslator();
105 void InsetInfo::read(Lexer & lex)
110 token = lex.getString();
111 if (token == "type") {
113 token = lex.getString();
114 type_ = nameTranslator().find(token);
115 } else if (token == "arg") {
117 name_ = lex.getString();
118 } else if (token == "\\end_inset")
121 if (token != "\\end_inset") {
122 lex.printError("Missing \\end_inset at this point");
123 throw ExceptionMessage(WarningException,
124 _("Missing \\end_inset at this point."),
131 void InsetInfo::write(ostream & os) const
133 os << "Info\ntype \"" << nameTranslator().find(type_)
134 << "\"\narg \"" << name_ << '\"';
138 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
140 // FIXME: we should allow selection, copy etc...
141 switch (cmd.action) {
142 case LFUN_MOUSE_PRESS:
143 case LFUN_MOUSE_RELEASE:
144 case LFUN_MOUSE_MOTION:
145 case LFUN_MOUSE_DOUBLE:
146 case LFUN_MOUSE_TRIPLE:
147 // do not dispatch to InsetText
152 InsetText::doDispatch(cur, cmd);
158 void InsetInfo::setInfo(string const & name)
164 name_ = trim(split(name, type, ' '));
165 type_ = nameTranslator().find(type);
169 void InsetInfo::updateInfo()
173 BufferParams const & bp = buffer().params();
177 setText(_("Unknown Info: ") + from_utf8(name_),
178 bp.getFont(), false);
180 case SHORTCUT_INFO: {
181 FuncRequest func = lyxaction.lookupFunc(name_);
182 if (func.action != LFUN_UNKNOWN_ACTION)
183 setText(theTopLevelKeymap().printBindings(func),
184 bp.getFont(), false);
189 lyxrc.write(oss, true, name_);
190 string result = oss.str();
191 // remove leading \\name
192 result = result.substr(name_.size() + 2);
194 result = rtrim(result, "\n");
195 result = trim(result, "\"");
196 setText(from_utf8(result), bp.getFont(), false);
200 // check in packages.lst
201 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
202 bp.getFont(), false);
204 case TEXTCLASS_INFO: {
205 // name_ is the class name
206 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
207 bp.getFont(), false);
211 docstring_list names;
212 FuncRequest func = lyxaction.lookupFunc(name_);
213 if (func.action == LFUN_UNKNOWN_ACTION) {
214 setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
217 // iterate through the menubackend to find it
218 if (!theApp()->searchMenu(func, names)) {
219 setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
220 bp.getFont(), false);
223 // if find, return its path.
225 Paragraph & info = paragraphs().front();
227 while (!names.empty()) {
228 // do not insert > for the top level menu item
230 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
231 Change(Change::UNCHANGED));
232 for (i = 0; i != names.back().length(); ++i)
233 info.insertChar(i, names.back()[i], bp.getFont(), false);
240 setText(from_utf8(buffer().fileName().onlyFileName()),
241 bp.getFont(), false);
242 else if (name_ == "path")
243 setText(from_utf8(buffer().filePath()), bp.getFont(), false);
244 else if (name_ == "class")
245 setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
247 setText(_("Unknown buffer info"), bp.getFont(), false);
252 paragraphs().begin()->params().noindent(true);
256 bool InsetInfo::setMouseHover(bool mouse_hover)
258 mouse_hover_ = mouse_hover;