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), type_(UNKNOWN_INFO), name_()
88 setAutoBreakRows(true);
94 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
97 return InsetCollapsable::editXY(cur, x, y);
101 string InsetInfo::infoType() const
103 return nameTranslator().find(type_);
107 docstring InsetInfo::name() const
109 return from_ascii("Info:" + infoType());
113 docstring InsetInfo::toolTip(BufferView const &, int, int) const
116 os << _("Information regarding ")
117 << _(infoType()) << " " << from_utf8(name_);
122 void InsetInfo::read(Lexer & lex)
127 token = lex.getString();
128 if (token == "type") {
130 token = lex.getString();
131 type_ = nameTranslator().find(token);
132 } else if (token == "arg") {
134 name_ = lex.getString();
135 } else if (token == "\\end_inset")
138 if (token != "\\end_inset") {
139 lex.printError("Missing \\end_inset at this point");
140 throw ExceptionMessage(WarningException,
141 _("Missing \\end_inset at this point."),
144 setLayout(buffer().params());
149 void InsetInfo::write(ostream & os) const
151 os << "Info\ntype \"" << infoType()
152 << "\"\narg " << Lexer::quoteString(name_);
156 bool InsetInfo::validate(docstring const & arg) const
159 string const name = trim(split(to_utf8(arg), type, ' '));
160 switch (nameTranslator().find(type)) {
167 FuncRequest func = lyxaction.lookupFunc(name);
168 return func.action != LFUN_UNKNOWN_ACTION;
172 lyxrc.write(oss, true, name);
173 return !oss.str().empty();
179 return name == "name" || name == "path" || name == "class";
185 bool InsetInfo::showInsetDialog(BufferView * bv) const
187 bv->showDialog("info");
192 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
193 FuncStatus & flag) const
195 switch (cmd.action) {
196 case LFUN_MOUSE_PRESS:
197 case LFUN_MOUSE_RELEASE:
198 case LFUN_MOUSE_MOTION:
199 case LFUN_MOUSE_DOUBLE:
200 case LFUN_MOUSE_TRIPLE:
202 case LFUN_INSET_SETTINGS:
203 return InsetCollapsable::getStatus(cur, cmd, flag);
205 case LFUN_INSET_MODIFY:
206 flag.setEnabled(true);
216 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
218 // allow selection, copy but not cut, delete etc
219 switch (cmd.action) {
220 case LFUN_MOUSE_PRESS:
221 case LFUN_MOUSE_RELEASE:
222 case LFUN_MOUSE_MOTION:
223 case LFUN_MOUSE_DOUBLE:
224 case LFUN_MOUSE_TRIPLE:
226 case LFUN_INSET_SETTINGS:
227 InsetCollapsable::doDispatch(cur, cmd);
230 case LFUN_INSET_MODIFY:
231 setInfo(to_utf8(cmd.argument()));
241 void InsetInfo::setInfo(string const & name)
247 name_ = trim(split(name, type, ' '));
248 type_ = nameTranslator().find(type);
249 setLayout(buffer().params());
254 void InsetInfo::error(string const & err)
256 setText(bformat(_(err), from_utf8(name_)), Font(inherit_font), false);
260 void InsetInfo::setText(docstring const & str)
262 setText(str, Font(inherit_font), false);
266 void InsetInfo::updateInfo()
268 BufferParams const & bp = buffer().params();
272 error("Unknown Info: %1$s");
275 case SHORTCUTS_INFO: {
276 FuncRequest func = lyxaction.lookupFunc(name_);
277 if (func.action == LFUN_UNKNOWN_ACTION) {
278 error("Unknown action %1$s");
281 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
282 if (bindings.empty()) {
283 // It is impropriate to use error() for undefined shortcut
284 setText(_("undefined"));
287 if (type_ == SHORTCUT_INFO)
288 setText(bindings.rbegin()->print(KeySequence::Portable),
289 Font(getLayout().font()), false);
291 setText(theTopLevelKeymap().printBindings(func),
292 Font(getLayout().font()), false);
297 lyxrc.write(oss, true, name_);
298 string result = oss.str();
299 // remove leading \\name
300 result = result.substr(name_.size() + 2);
302 result = rtrim(result, "\n");
303 result = trim(result, "\"");
304 setText(from_utf8(result));
308 // check in packages.lst
309 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
311 case TEXTCLASS_INFO: {
312 // name_ is the class name
313 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
317 docstring_list names;
318 FuncRequest func = lyxaction.lookupFunc(name_);
319 if (func.action == LFUN_UNKNOWN_ACTION) {
320 error("Unknown action %1$s");
323 // iterate through the menubackend to find it
324 if (!theApp()->searchMenu(func, names)) {
325 error("No menu entry for action %1$s");
328 // if found, return its path.
330 Paragraph & par = paragraphs().front();
331 Font const f = Font(getLayout().font());
333 //fu.fontInfo().setUnderbar(FONT_ON);
334 docstring_list::const_iterator beg = names.begin();
335 docstring_list::const_iterator end = names.end();
336 for (docstring_list::const_iterator it = beg ;
338 // do not insert > for the top level menu item
340 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
341 Change(Change::UNCHANGED));
342 //FIXME: add proper underlines here. This
343 // involves rewriting searchMenu used above to
344 // return a vector of menus. If we do not do
345 // that, we might as well use below
346 // Paragraph::insert on each string (JMarc)
347 for (size_type i = 0; i != it->length(); ++i)
348 par.insertChar(par.size(), (*it)[i],
349 f, Change(Change::UNCHANGED));
354 FuncRequest func = lyxaction.lookupFunc(name_);
355 docstring icon_name = theApp()->iconName(func, true);
356 //FIXME: We should use the icon directly instead of
357 // going through FileName. The code below won't work
358 // if the icon is embedded in the executable through
359 // the Qt resource system.
360 FileName file(to_utf8(icon_name));
363 InsetGraphics * inset = new InsetGraphics(buffer());
364 InsetGraphicsParams igp;
366 inset->setParams(igp);
368 paragraphs().front().insertInset(0, inset,
369 Change(Change::UNCHANGED));
374 setText(from_utf8(buffer().fileName().onlyFileName()));
375 else if (name_ == "path")
376 setText(from_utf8(buffer().filePath()));
377 else if (name_ == "class")
378 setText(from_utf8(bp.documentClass().name()));
380 setText(_("Unknown buffer info"));
387 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
389 return from_ascii("context-info");