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"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "InsetGraphics.h"
20 #include "InsetSpecialChar.h"
22 #include "LaTeXFeatures.h"
23 #include "LayoutFile.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/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
44 using namespace lyx::support;
50 typedef Translator<InsetInfo::info_type, string> NameTranslator;
52 NameTranslator const initTranslator()
54 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
56 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
57 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
58 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
59 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
60 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
61 translator.addPair(InsetInfo::MENU_INFO, "menu");
62 translator.addPair(InsetInfo::ICON_INFO, "icon");
63 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
68 /// The translator between the information type enum and corresponding string.
69 NameTranslator const & nameTranslator()
71 static NameTranslator const translator = initTranslator();
77 /////////////////////////////////////////////////////////////////////////
81 /////////////////////////////////////////////////////////////////////////
85 InsetInfo::InsetInfo(Buffer const & buf, string const & name)
86 : InsetCollapsable(buf, Collapsed), type_(UNKNOWN_INFO), name_()
88 setAutoBreakRows(true);
93 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
96 return InsetCollapsable::editXY(cur, x, y);
100 string InsetInfo::infoType() const
102 return nameTranslator().find(type_);
106 docstring InsetInfo::name() const
108 return from_ascii("Info:" + infoType());
112 docstring InsetInfo::toolTip(BufferView const &, int, int) const
115 os << _("Information regarding ")
116 << _(infoType()) << " " << from_utf8(name_);
121 void InsetInfo::read(Lexer & lex)
126 token = lex.getString();
127 if (token == "type") {
129 token = lex.getString();
130 type_ = nameTranslator().find(token);
131 } else if (token == "arg") {
133 name_ = lex.getString();
134 } else if (token == "\\end_inset")
137 if (token != "\\end_inset") {
138 lex.printError("Missing \\end_inset at this point");
139 throw ExceptionMessage(WarningException,
140 _("Missing \\end_inset at this point."),
143 setLayout(buffer().params());
148 void InsetInfo::write(ostream & os) const
150 os << "Info\ntype \"" << infoType()
151 << "\"\narg " << Lexer::quoteString(name_);
155 bool InsetInfo::validate(docstring const & arg) const
158 string const name = trim(split(to_utf8(arg), type, ' '));
159 switch (nameTranslator().find(type)) {
166 FuncRequest func = lyxaction.lookupFunc(name);
167 return func.action != LFUN_UNKNOWN_ACTION;
171 lyxrc.write(oss, true, name);
172 return !oss.str().empty();
178 return name == "name" || name == "path" || name == "class";
184 bool InsetInfo::showInsetDialog(BufferView * bv) const
186 bv->showDialog("info");
191 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
192 FuncStatus & flag) const
194 switch (cmd.action) {
195 case LFUN_MOUSE_PRESS:
196 case LFUN_MOUSE_RELEASE:
197 case LFUN_MOUSE_MOTION:
198 case LFUN_MOUSE_DOUBLE:
199 case LFUN_MOUSE_TRIPLE:
201 case LFUN_INSET_SETTINGS:
202 return InsetCollapsable::getStatus(cur, cmd, flag);
204 case LFUN_INSET_MODIFY:
205 flag.setEnabled(true);
215 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
217 // allow selection, copy but not cut, delete etc
218 switch (cmd.action) {
219 case LFUN_MOUSE_PRESS:
220 case LFUN_MOUSE_RELEASE:
221 case LFUN_MOUSE_MOTION:
222 case LFUN_MOUSE_DOUBLE:
223 case LFUN_MOUSE_TRIPLE:
225 case LFUN_INSET_SETTINGS:
226 InsetCollapsable::doDispatch(cur, cmd);
229 case LFUN_INSET_MODIFY:
230 setInfo(to_utf8(cmd.argument()));
240 void InsetInfo::setInfo(string const & name)
246 name_ = trim(split(name, type, ' '));
247 type_ = nameTranslator().find(type);
248 setLayout(buffer().params());
253 void InsetInfo::error(string const & err)
255 setText(bformat(_(err), from_utf8(name_)), Font(inherit_font), false);
259 void InsetInfo::setText(docstring const & str)
261 setText(str, Font(inherit_font), false);
265 void InsetInfo::updateInfo()
267 BufferParams const & bp = buffer().params();
271 error("Unknown Info: %1$s");
274 case SHORTCUTS_INFO: {
275 FuncRequest func = lyxaction.lookupFunc(name_);
276 if (func.action == LFUN_UNKNOWN_ACTION) {
277 error("Unknown action %1$s");
280 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
281 if (bindings.empty()) {
282 // It is impropriate to use error() for undefined shortcut
283 setText(_("undefined"));
286 if (type_ == SHORTCUT_INFO)
287 setText(bindings.rbegin()->print(KeySequence::Portable),
288 Font(getLayout().font()), false);
290 setText(theTopLevelKeymap().printBindings(func),
291 Font(getLayout().font()), false);
296 lyxrc.write(oss, true, name_);
297 string result = oss.str();
298 // remove leading \\name
299 result = result.substr(name_.size() + 2);
301 result = rtrim(result, "\n");
302 result = trim(result, "\"");
303 setText(from_utf8(result));
307 // check in packages.lst
308 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
310 case TEXTCLASS_INFO: {
311 // name_ is the class name
312 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
316 docstring_list names;
317 FuncRequest func = lyxaction.lookupFunc(name_);
318 if (func.action == LFUN_UNKNOWN_ACTION) {
319 error("Unknown action %1$s");
322 // iterate through the menubackend to find it
323 if (!theApp()->searchMenu(func, names)) {
324 error("No menu entry for action %1$s");
327 // if found, return its path.
329 Paragraph & par = paragraphs().front();
330 Font const f = Font(getLayout().font());
332 //fu.fontInfo().setUnderbar(FONT_ON);
333 docstring_list::const_iterator beg = names.begin();
334 docstring_list::const_iterator end = names.end();
335 for (docstring_list::const_iterator it = beg ;
337 // do not insert > for the top level menu item
339 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
340 Change(Change::UNCHANGED));
341 //FIXME: add proper underlines here. This
342 // involves rewriting searchMenu used above to
343 // return a vector of menus. If we do not do
344 // that, we might as well use below
345 // Paragraph::insert on each string (JMarc)
346 for (size_type i = 0; i != it->length(); ++i)
347 par.insertChar(par.size(), (*it)[i],
348 f, Change(Change::UNCHANGED));
353 FuncRequest func = lyxaction.lookupFunc(name_);
354 docstring icon_name = theApp()->iconName(func, true);
355 //FIXME: We should use the icon directly instead of
356 // going through FileName. The code below won't work
357 // if the icon is embedded in the executable through
358 // the Qt resource system.
359 FileName file(to_utf8(icon_name));
362 InsetGraphics * inset = new InsetGraphics(buffer());
363 InsetGraphicsParams igp;
365 inset->setParams(igp);
367 paragraphs().front().insertInset(0, inset,
368 Change(Change::UNCHANGED));
373 setText(from_utf8(buffer().fileName().onlyFileName()));
374 else if (name_ == "path")
375 setText(from_utf8(buffer().filePath()));
376 else if (name_ == "class")
377 setText(from_utf8(bp.documentClass().name()));
379 setText(_("Unknown buffer info"));
386 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
388 return from_ascii("context-info");