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
115 return bformat(_("Information regarding %1$s '%2$s'"),
116 _(infoType()), from_utf8(name_));
120 void InsetInfo::read(Lexer & lex)
125 token = lex.getString();
126 if (token == "type") {
128 token = lex.getString();
129 type_ = nameTranslator().find(token);
130 } else if (token == "arg") {
132 name_ = lex.getString();
133 } else if (token == "\\end_inset")
136 if (token != "\\end_inset") {
137 lex.printError("Missing \\end_inset at this point");
138 throw ExceptionMessage(WarningException,
139 _("Missing \\end_inset at this point."),
142 setLayout(buffer().params());
147 void InsetInfo::write(ostream & os) const
149 os << "Info\ntype \"" << infoType()
150 << "\"\narg " << Lexer::quoteString(name_);
154 bool InsetInfo::validate(docstring const & arg) const
157 string const name = trim(split(to_utf8(arg), type, ' '));
158 switch (nameTranslator().find(type)) {
165 FuncRequest func = lyxaction.lookupFunc(name);
166 return func.action != LFUN_UNKNOWN_ACTION;
170 lyxrc.write(oss, true, name);
171 return !oss.str().empty();
177 return name == "name" || name == "path" || name == "class";
183 bool InsetInfo::showInsetDialog(BufferView * bv) const
185 bv->showDialog("info");
190 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & flag) const
193 switch (cmd.action) {
194 case LFUN_MOUSE_PRESS:
195 case LFUN_MOUSE_RELEASE:
196 case LFUN_MOUSE_MOTION:
197 case LFUN_MOUSE_DOUBLE:
198 case LFUN_MOUSE_TRIPLE:
200 return InsetCollapsable::getStatus(cur, cmd, flag);
202 case LFUN_INSET_DIALOG_UPDATE:
203 case LFUN_INSET_SETTINGS:
204 flag.setEnabled(true);
207 case LFUN_INSET_MODIFY:
208 flag.setEnabled(true);
218 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
220 // allow selection, copy but not cut, delete etc
221 switch (cmd.action) {
222 case LFUN_MOUSE_PRESS:
223 case LFUN_MOUSE_RELEASE:
224 case LFUN_MOUSE_MOTION:
225 case LFUN_MOUSE_DOUBLE:
226 case LFUN_MOUSE_TRIPLE:
228 case LFUN_INSET_SETTINGS:
229 InsetCollapsable::doDispatch(cur, cmd);
232 case LFUN_INSET_MODIFY:
233 setInfo(to_utf8(cmd.argument()));
243 void InsetInfo::setInfo(string const & name)
249 name_ = trim(split(name, type, ' '));
250 type_ = nameTranslator().find(type);
251 setLayout(buffer().params());
256 void InsetInfo::error(string const & err)
258 setText(bformat(_(err), from_utf8(name_)),
259 Font(inherit_font, buffer().params().language), false);
263 void InsetInfo::setText(docstring const & str)
265 setText(str, Font(inherit_font, buffer().params().language), false);
269 void InsetInfo::updateInfo()
271 BufferParams const & bp = buffer().params();
275 error("Unknown Info: %1$s");
278 case SHORTCUTS_INFO: {
279 FuncRequest func = lyxaction.lookupFunc(name_);
280 if (func.action == LFUN_UNKNOWN_ACTION) {
281 error("Unknown action %1$s");
284 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
285 if (bindings.empty()) {
286 // It is impropriate to use error() for undefined shortcut
287 setText(_("undefined"));
290 if (type_ == SHORTCUT_INFO)
291 setText(bindings.begin()->print(KeySequence::Portable));
293 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
298 lyxrc.write(oss, true, name_);
299 string result = oss.str();
300 // remove leading \\name
301 result = result.substr(name_.size() + 2);
303 result = rtrim(result, "\n");
304 result = trim(result, "\"");
305 setText(from_utf8(result));
309 // check in packages.lst
310 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
312 case TEXTCLASS_INFO: {
313 // name_ is the class name
314 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
318 docstring_list names;
319 FuncRequest func = lyxaction.lookupFunc(name_);
320 if (func.action == LFUN_UNKNOWN_ACTION) {
321 error("Unknown action %1$s");
324 // iterate through the menubackend to find it
325 if (!theApp()->searchMenu(func, names)) {
326 error("No menu entry for action %1$s");
329 // if found, return its path.
331 Paragraph & par = paragraphs().front();
332 Font const f(inherit_font, buffer().params().language);
334 //fu.fontInfo().setUnderbar(FONT_ON);
335 docstring_list::const_iterator beg = names.begin();
336 docstring_list::const_iterator end = names.end();
337 for (docstring_list::const_iterator it = beg ;
339 // do not insert > for the top level menu item
341 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
342 Change(Change::UNCHANGED));
343 //FIXME: add proper underlines here. This
344 // involves rewriting searchMenu used above to
345 // return a vector of menus. If we do not do
346 // that, we might as well use below
347 // Paragraph::insert on each string (JMarc)
348 for (size_type i = 0; i != it->length(); ++i)
349 par.insertChar(par.size(), (*it)[i],
350 f, Change(Change::UNCHANGED));
355 FuncRequest func = lyxaction.lookupFunc(name_);
356 docstring icon_name = theApp()->iconName(func, true);
357 //FIXME: We should use the icon directly instead of
358 // going through FileName. The code below won't work
359 // if the icon is embedded in the executable through
360 // the Qt resource system.
361 FileName file(to_utf8(icon_name));
364 InsetGraphics * inset = new InsetGraphics(buffer());
365 InsetGraphicsParams igp;
367 inset->setParams(igp);
369 paragraphs().front().insertInset(0, inset,
370 Change(Change::UNCHANGED));
375 setText(from_utf8(buffer().fileName().onlyFileName()));
376 else if (name_ == "path")
377 setText(from_utf8(buffer().filePath()));
378 else if (name_ == "class")
379 setText(from_utf8(bp.documentClass().name()));
381 setText(_("Unknown buffer info"));
388 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
390 return from_ascii("context-info");