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"
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "FuncRequest.h"
22 #include "InsetSpecialChar.h"
24 #include "LaTeXFeatures.h"
25 #include "LyXAction.h"
28 #include "MenuBackend.h"
29 #include "MetricsInfo.h"
30 #include "ParagraphParameters.h"
31 #include "TextClassList.h"
32 #include "support/lstrings.h"
33 #include "support/ExceptionMessage.h"
38 using std::ostringstream;
43 using support::prefixIs;
47 using support::ExceptionMessage;
48 using support::WarningException;
50 InsetInfo::InsetInfo(BufferParams const & bp, string const & name)
51 : InsetText(bp), bp_(bp), type_(UNKNOWN_INFO), name_(),
54 setAutoBreakRows(true);
60 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
66 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
68 InsetText::draw(pi, x, y);
71 os << _("Information regarding ")
72 <<_(nameTranslator().find(type_))
73 << _(" ") << from_utf8(name_);
74 pi.base.bv->message(os.str());
81 Translator<InsetInfo::info_type, string> const initTranslator()
83 Translator<InsetInfo::info_type, string> translator(InsetInfo::UNKNOWN_INFO, "unknown");
85 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
86 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
87 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
88 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
89 translator.addPair(InsetInfo::MENU_INFO, "menu");
96 Translator<InsetInfo::info_type, std::string> const & InsetInfo::nameTranslator() const
98 static Translator<info_type, string> const translator =
105 void InsetInfo::read(Buffer const & buf, 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(Buffer const & buf, std::ostream & os) const
133 os << "Info\ntype \""
134 << nameTranslator().find(type_)
135 << "\"\narg \"" << name_ << '\"';
139 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
141 // FIXME: we should allow selection, copy etc...
142 switch (cmd.action) {
143 case LFUN_MOUSE_PRESS:
144 case LFUN_MOUSE_RELEASE:
145 case LFUN_MOUSE_MOTION:
146 case LFUN_MOUSE_DOUBLE:
147 case LFUN_MOUSE_TRIPLE:
148 // do not dispatch to InsetText
153 InsetText::doDispatch(cur, cmd);
159 void InsetInfo::setInfo(string const & name)
165 name_ = trim(split(name, type, ' '));
166 type_ = nameTranslator().find(type);
171 void InsetInfo::updateInfo()
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 pair<bool, lyx::textclass_type> pp =
207 textclasslist.numberOfClass(name_);
208 setText(pp.first ? _("yes") : _("no"),
209 bp_.getFont(), false);
213 stack<docstring> names;
214 FuncRequest func = lyxaction.lookupFunc(name_);
215 if (func.action == LFUN_UNKNOWN_ACTION) {
216 setText(_("No menu entry for "), bp_.getFont(), false);
219 // iterate through the menubackend to find it
220 Menu menu = menubackend.getMenubar();
221 if (!menu.searchFunc(func, names)) {
222 setText(_("No menu entry for "), bp_.getFont(), false);
225 // if find, return its path.
227 Paragraph & info = paragraphs().front();
229 while (!names.empty()) {
230 // do not insert > for the top level menu item
232 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
233 Change(Change::UNCHANGED));
234 for (i = 0; i < names.top().length(); ++i)
235 info.insertChar(i, names.top()[i], bp_.getFont(), false);
242 paragraphs().begin()->params().noindent(true);
246 bool InsetInfo::setMouseHover(bool mouse_hover)
248 mouse_hover_ = mouse_hover;