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 "InsetGraphics.h"
20 #include "InsetSpecialChar.h"
22 #include "LaTeXFeatures.h"
23 #include "LyXAction.h"
26 #include "MetricsInfo.h"
27 #include "ParagraphParameters.h"
29 #include "frontends/Application.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/docstring_list.h"
34 #include "support/FileName.h"
35 #include "support/filetools.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/ExceptionMessage.h"
43 using namespace lyx::support;
48 InsetInfo::InsetInfo(Buffer const & buf, string const & name)
49 : InsetText(buf), type_(UNKNOWN_INFO), name_(),
52 setAutoBreakRows(true);
58 Inset * InsetInfo::editXY(Cursor &, int, int)
64 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
66 InsetText::draw(pi, x, y);
70 docstring InsetInfo::toolTip(BufferView const &, int, int) const
73 os << _("Information regarding ")
74 << _(nameTranslator().find(type_))
75 << " " << from_utf8(name_);
81 Translator<InsetInfo::info_type, string> const initTranslator()
83 Translator<InsetInfo::info_type, string>
84 translator(InsetInfo::UNKNOWN_INFO, "unknown");
86 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
87 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
88 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
89 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
90 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
91 translator.addPair(InsetInfo::MENU_INFO, "menu");
92 translator.addPair(InsetInfo::ICON_INFO, "icon");
93 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
100 Translator<InsetInfo::info_type, string>
101 const & InsetInfo::nameTranslator() const
103 static Translator<info_type, string> const translator = initTranslator();
109 void InsetInfo::read(Lexer & lex)
114 token = lex.getString();
115 if (token == "type") {
117 token = lex.getString();
118 type_ = nameTranslator().find(token);
119 } else if (token == "arg") {
121 name_ = lex.getString();
122 } else if (token == "\\end_inset")
125 if (token != "\\end_inset") {
126 lex.printError("Missing \\end_inset at this point");
127 throw ExceptionMessage(WarningException,
128 _("Missing \\end_inset at this point."),
135 void InsetInfo::write(ostream & os) const
137 os << "Info\ntype \"" << nameTranslator().find(type_)
138 << "\"\narg \"" << name_ << '\"';
142 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
144 // FIXME: we should allow selection, copy etc...
145 switch (cmd.action) {
146 case LFUN_MOUSE_PRESS:
147 case LFUN_MOUSE_RELEASE:
148 case LFUN_MOUSE_MOTION:
149 case LFUN_MOUSE_DOUBLE:
150 case LFUN_MOUSE_TRIPLE:
151 // do not dispatch to InsetText
156 InsetText::doDispatch(cur, cmd);
162 void InsetInfo::setInfo(string const & name)
168 name_ = trim(split(name, type, ' '));
169 type_ = nameTranslator().find(type);
173 void InsetInfo::updateInfo()
177 BufferParams const & bp = buffer().params();
181 setText(_("Unknown Info: ") + from_utf8(name_),
182 bp.getFont(), false);
184 case SHORTCUTS_INFO: {
185 FuncRequest func = lyxaction.lookupFunc(name_);
186 if (func.action != LFUN_UNKNOWN_ACTION)
187 setText(theTopLevelKeymap().printBindings(func),
188 bp.getFont(), false);
191 case SHORTCUT_INFO: {
192 FuncRequest func = lyxaction.lookupFunc(name_);
193 if (func.action != LFUN_UNKNOWN_ACTION) {
194 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
195 if (!bindings.empty())
196 setText(bindings.rbegin()->print(KeySequence::ForGui),
197 bp.getFont(), false);
203 lyxrc.write(oss, true, name_);
204 string result = oss.str();
205 // remove leading \\name
206 result = result.substr(name_.size() + 2);
208 result = rtrim(result, "\n");
209 result = trim(result, "\"");
210 setText(from_utf8(result), bp.getFont(), false);
214 // check in packages.lst
215 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
216 bp.getFont(), false);
218 case TEXTCLASS_INFO: {
219 // name_ is the class name
220 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
221 bp.getFont(), false);
225 docstring_list names;
226 FuncRequest func = lyxaction.lookupFunc(name_);
227 if (func.action == LFUN_UNKNOWN_ACTION) {
228 setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
231 // iterate through the menubackend to find it
232 if (!theApp()->searchMenu(func, names)) {
233 setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
234 bp.getFont(), false);
237 // if find, return its path.
239 Paragraph & info = paragraphs().front();
241 while (!names.empty()) {
242 // do not insert > for the top level menu item
244 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
245 Change(Change::UNCHANGED));
246 for (i = 0; i != names.back().length(); ++i)
247 info.insertChar(i, names.back()[i], bp.getFont(), false);
253 FuncRequest func = lyxaction.lookupFunc(name_);
254 docstring icon_name = theApp()->iconName(func, true);
255 //FIXME: We should use the icon directly instead of going through
256 // FileName. The code below won't work if the icon is embedded in the
257 // executable through the Qt resource system.
258 FileName file(to_utf8(icon_name));
261 InsetGraphicsParams igp;
263 InsetGraphics * inset = new InsetGraphics(buffer());
264 inset->setParams(igp);
265 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
270 setText(from_utf8(buffer().fileName().onlyFileName()),
271 bp.getFont(), false);
272 else if (name_ == "path")
273 setText(from_utf8(buffer().filePath()), bp.getFont(), false);
274 else if (name_ == "class")
275 setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
277 setText(_("Unknown buffer info"), bp.getFont(), false);
282 paragraphs().begin()->params().noindent(true);
286 bool InsetInfo::setMouseHover(bool mouse_hover)
288 mouse_hover_ = mouse_hover;