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 "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
23 #include "LaTeXFeatures.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/FileName.h"
36 #include "support/filetools.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/ExceptionMessage.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(string const & argument) const
157 bool InsetInfo::showInsetDialog(BufferView * bv) const
159 bv->showDialog("info");
164 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
165 FuncStatus & flag) const
167 switch (cmd.action) {
169 case LFUN_INSET_MODIFY:
170 flag.setEnabled(true);
172 //FIXME: do something.
183 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
185 // allow selection, copy but not cut, delete etc
186 switch (cmd.action) {
187 case LFUN_MOUSE_PRESS:
188 case LFUN_MOUSE_RELEASE:
189 case LFUN_MOUSE_MOTION:
190 case LFUN_MOUSE_DOUBLE:
191 case LFUN_MOUSE_TRIPLE:
193 case LFUN_INSET_SETTINGS:
194 InsetText::doDispatch(cur, cmd);
203 void InsetInfo::setInfo(string const & name)
209 name_ = trim(split(name, type, ' '));
210 type_ = nameTranslator().find(type);
214 void InsetInfo::updateInfo()
218 BufferParams const & bp = buffer().params();
222 setText(_("Unknown Info: ") + from_utf8(name_),
223 bp.getFont(), false);
225 case SHORTCUTS_INFO: {
226 FuncRequest func = lyxaction.lookupFunc(name_);
227 if (func.action != LFUN_UNKNOWN_ACTION)
228 setText(theTopLevelKeymap().printBindings(func),
229 bp.getFont(), false);
232 case SHORTCUT_INFO: {
233 FuncRequest func = lyxaction.lookupFunc(name_);
234 if (func.action != LFUN_UNKNOWN_ACTION) {
235 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
236 if (!bindings.empty())
237 setText(bindings.rbegin()->print(KeySequence::ForGui),
238 bp.getFont(), false);
244 lyxrc.write(oss, true, name_);
245 string result = oss.str();
246 // remove leading \\name
247 result = result.substr(name_.size() + 2);
249 result = rtrim(result, "\n");
250 result = trim(result, "\"");
251 setText(from_utf8(result), bp.getFont(), false);
255 // check in packages.lst
256 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
257 bp.getFont(), false);
259 case TEXTCLASS_INFO: {
260 // name_ is the class name
261 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
262 bp.getFont(), false);
266 docstring_list names;
267 FuncRequest func = lyxaction.lookupFunc(name_);
268 if (func.action == LFUN_UNKNOWN_ACTION) {
269 setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
272 // iterate through the menubackend to find it
273 if (!theApp()->searchMenu(func, names)) {
274 setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
275 bp.getFont(), false);
278 // if find, return its path.
280 Paragraph & info = paragraphs().front();
282 while (!names.empty()) {
283 // do not insert > for the top level menu item
285 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
286 Change(Change::UNCHANGED));
287 for (i = 0; i != names.back().length(); ++i)
288 info.insertChar(i, names.back()[i], bp.getFont(), false);
294 FuncRequest func = lyxaction.lookupFunc(name_);
295 docstring icon_name = theApp()->iconName(func, true);
296 //FIXME: We should use the icon directly instead of going through
297 // FileName. The code below won't work if the icon is embedded in the
298 // executable through the Qt resource system.
299 FileName file(to_utf8(icon_name));
302 InsetGraphicsParams igp;
304 InsetGraphics * inset = new InsetGraphics(buffer());
305 inset->setParams(igp);
306 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
311 setText(from_utf8(buffer().fileName().onlyFileName()),
312 bp.getFont(), false);
313 else if (name_ == "path")
314 setText(from_utf8(buffer().filePath()), bp.getFont(), false);
315 else if (name_ == "class")
316 setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
318 setText(_("Unknown buffer info"), bp.getFont(), false);
323 paragraphs().begin()->params().noindent(true);
327 bool InsetInfo::setMouseHover(bool mouse_hover)
329 mouse_hover_ = mouse_hover;
334 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
336 return from_ascii("context-info");