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"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "support/debug.h"
21 #include "FuncRequest.h"
22 #include "support/gettext.h"
23 #include "InsetSpecialChar.h"
25 #include "LaTeXFeatures.h"
26 #include "LyXAction.h"
29 #include "MenuBackend.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "TextClassList.h"
34 #include "support/docstream.h"
35 #include "support/FileName.h"
36 #include "support/lstrings.h"
37 #include "support/ExceptionMessage.h"
40 using namespace lyx::support;
45 InsetInfo::InsetInfo(BufferParams const & bp, string const & name)
46 : InsetText(bp), type_(UNKNOWN_INFO), name_(),
49 setAutoBreakRows(true);
55 Inset * InsetInfo::editXY(Cursor &, int, int)
61 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
63 InsetText::draw(pi, x, y);
66 os << _("Information regarding ")
67 <<_(nameTranslator().find(type_))
68 << _(" ") << from_utf8(name_);
69 pi.base.bv->message(os.str());
76 Translator<InsetInfo::info_type, string> const initTranslator()
78 Translator<InsetInfo::info_type, string> translator(InsetInfo::UNKNOWN_INFO, "unknown");
80 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
81 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
82 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
83 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
84 translator.addPair(InsetInfo::MENU_INFO, "menu");
85 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
92 Translator<InsetInfo::info_type, string> const & InsetInfo::nameTranslator() const
94 static Translator<info_type, string> const translator =
101 void InsetInfo::read(Buffer const & buf, Lexer & lex)
106 token = lex.getString();
107 if (token == "type") {
109 token = lex.getString();
110 type_ = nameTranslator().find(token);
111 } else if (token == "arg") {
113 name_ = lex.getString();
114 } else if (token == "\\end_inset")
117 if (token != "\\end_inset") {
118 lex.printError("Missing \\end_inset at this point");
119 throw ExceptionMessage(WarningException,
120 _("Missing \\end_inset at this point."),
127 void InsetInfo::write(Buffer const &, ostream & os) const
129 os << "Info\ntype \""
130 << nameTranslator().find(type_)
131 << "\"\narg \"" << name_ << '\"';
135 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
137 // FIXME: we should allow selection, copy etc...
138 switch (cmd.action) {
139 case LFUN_MOUSE_PRESS:
140 case LFUN_MOUSE_RELEASE:
141 case LFUN_MOUSE_MOTION:
142 case LFUN_MOUSE_DOUBLE:
143 case LFUN_MOUSE_TRIPLE:
144 // do not dispatch to InsetText
149 InsetText::doDispatch(cur, cmd);
155 void InsetInfo::setInfo(string const & name)
161 name_ = trim(split(name, type, ' '));
162 type_ = nameTranslator().find(type);
166 void InsetInfo::updateInfo(Buffer const & buf)
170 BufferParams const & bp = buf.params();
174 setText(_("Unknown Info: ") + from_utf8(name_),
175 bp.getFont(), false);
177 case SHORTCUT_INFO: {
178 FuncRequest func = lyxaction.lookupFunc(name_);
179 if (func.action != LFUN_UNKNOWN_ACTION)
180 setText(theTopLevelKeymap().printBindings(func),
181 bp.getFont(), false);
186 lyxrc.write(oss, true, name_);
187 string result = oss.str();
188 // remove leading \\name
189 result = result.substr(name_.size() + 2);
191 result = rtrim(result, "\n");
192 result = trim(result, "\"");
193 setText(from_utf8(result), bp.getFont(), false);
197 // check in packages.lst
198 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
199 bp.getFont(), false);
201 case TEXTCLASS_INFO: {
202 // name_ is the class name
203 pair<bool, lyx::textclass_type> pp =
204 textclasslist.numberOfClass(name_);
205 setText(pp.first ? _("yes") : _("no"),
206 bp.getFont(), false);
210 stack<docstring> names;
211 FuncRequest func = lyxaction.lookupFunc(name_);
212 if (func.action == LFUN_UNKNOWN_ACTION) {
213 setText(_("No menu entry for "), bp.getFont(), false);
216 // iterate through the menubackend to find it
217 Menu menu = menubackend.getMenubar();
218 if (!menu.searchFunc(func, names)) {
219 setText(_("No menu entry for "), bp.getFont(), false);
222 // if find, return its path.
224 Paragraph & info = paragraphs().front();
226 while (!names.empty()) {
227 // do not insert > for the top level menu item
229 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
230 Change(Change::UNCHANGED));
231 for (i = 0; i < names.top().length(); ++i)
232 info.insertChar(i, names.top()[i], bp.getFont(), false);
239 setText(from_utf8(buf.fileName().onlyFileName()), bp.getFont(), false);
240 else if (name_ == "path")
241 setText(from_utf8(buf.filePath()), bp.getFont(), false);
242 else if (name_ == "class")
243 setText(from_utf8(bp.getTextClass().name()), bp.getFont(), false);
245 setText(_("Unknown buffer info"), bp.getFont(), false);
250 paragraphs().begin()->params().noindent(true);
254 bool InsetInfo::setMouseHover(bool mouse_hover)
256 mouse_hover_ = mouse_hover;