]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Change the type of the context menu name to string
[lyx.git] / src / insets / InsetInfo.cpp
1 /**
2  * \file InsetInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetInfo.h"
13
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
22 #include "KeyMap.h"
23 #include "LaTeXFeatures.h"
24 #include "LayoutFile.h"
25 #include "LyXAction.h"
26 #include "LyXRC.h"
27 #include "LyXVC.h"
28 #include "Lexer.h"
29 #include "ParagraphParameters.h"
30 #include "version.h"
31
32 #include "frontends/Application.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/docstring_list.h"
37 #include "support/ExceptionMessage.h"
38 #include "support/FileName.h"
39 #include "support/filetools.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42 #include "support/Translator.h"
43
44 #include <sstream>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50
51 namespace {
52
53 typedef Translator<InsetInfo::info_type, string> NameTranslator;
54
55 NameTranslator const initTranslator()
56 {       
57         NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
58
59         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
60         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
61         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
62         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
63         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
64         translator.addPair(InsetInfo::MENU_INFO, "menu");
65         translator.addPair(InsetInfo::ICON_INFO, "icon");
66         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
67         translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
68
69         return translator;
70 }
71
72 /// The translator between the information type enum and corresponding string.
73 NameTranslator const & nameTranslator()
74 {
75         static NameTranslator const translator = initTranslator();
76         return translator;
77 }
78
79 } // namespace anon
80
81 /////////////////////////////////////////////////////////////////////////
82 //
83 // InsetInfo
84 //
85 /////////////////////////////////////////////////////////////////////////
86
87
88         
89 InsetInfo::InsetInfo(Buffer * buf, string const & name) 
90         : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
91 {
92         setAutoBreakRows(true);
93         setInfo(name);
94         status_ = Collapsed;
95 }
96
97
98 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
99 {
100         // do not allow the cursor to be set in this Inset
101         return Inset::editXY(cur, x, y);
102 }
103
104
105 string InsetInfo::infoType() const
106 {
107         return nameTranslator().find(type_);
108 }
109
110
111 docstring InsetInfo::layoutName() const
112 {
113         return from_ascii("Info:" + infoType());
114 }
115
116
117 docstring InsetInfo::toolTip(BufferView const &, int, int) const
118 {
119         return bformat(_("Information regarding %1$s '%2$s'"),
120                         _(infoType()), from_utf8(name_));
121 }
122
123
124 void InsetInfo::read(Lexer & lex)
125 {
126         string token;
127         while (lex.isOK()) {
128                 lex.next();
129                 token = lex.getString();
130                 if (token == "type") {
131                         lex.next();
132                         token = lex.getString();
133                         type_ = nameTranslator().find(token);
134                 } else if (token == "arg") {
135                         lex.next(true);
136                         name_ = lex.getString();
137                 } else if (token == "\\end_inset")
138                         break;
139         }       
140         if (token != "\\end_inset") {
141                 lex.printError("Missing \\end_inset at this point");
142                 throw ExceptionMessage(WarningException,
143                         _("Missing \\end_inset at this point."),
144                         from_utf8(token));
145         }
146         updateInfo();
147 }
148
149
150 void InsetInfo::write(ostream & os) const
151 {
152         os << "Info\ntype  \"" << infoType() 
153            << "\"\narg   " << Lexer::quoteString(name_);
154 }
155
156
157 bool InsetInfo::validateModifyArgument(docstring const & arg) const
158 {
159         string type;
160         string const name = trim(split(to_utf8(arg), type, ' '));
161
162         switch (nameTranslator().find(type)) {
163         case UNKNOWN_INFO:
164                 return false;
165
166         case SHORTCUT_INFO:
167         case SHORTCUTS_INFO:
168         case MENU_INFO:
169         case ICON_INFO: {
170                 FuncRequest func = lyxaction.lookupFunc(name);
171                 return func.action() != LFUN_UNKNOWN_ACTION;
172         }
173
174         case LYXRC_INFO: {
175                 ostringstream oss;
176                 lyxrc.write(oss, true, name);
177                 return !oss.str().empty();
178         }
179
180         case PACKAGE_INFO:
181         case TEXTCLASS_INFO:
182                 return true;
183
184         case BUFFER_INFO:
185                 if (name == "name" || name == "path" || name == "class")
186                         return true;
187                 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
188                        name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
189                         return buffer().lyxvc().inUse();
190                 return false;
191
192         case LYX_INFO:
193                 return name == "version";
194         }
195
196         return false;
197 }
198
199
200 bool InsetInfo::showInsetDialog(BufferView * bv) const
201 {
202         bv->showDialog("info");
203         return true;
204 }
205
206
207 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
208                 FuncStatus & flag) const
209 {
210         switch (cmd.action()) {
211         case LFUN_INSET_SETTINGS:
212                 return InsetCollapsable::getStatus(cur, cmd, flag);
213
214         case LFUN_INSET_DIALOG_UPDATE:
215         case LFUN_INSET_COPY_AS:
216                 flag.setEnabled(true);
217                 return true;
218
219         case LFUN_INSET_MODIFY:
220                 if (validateModifyArgument(cmd.argument())) {
221                         flag.setEnabled(true);
222                         return true;
223                 }
224                 //fall through
225
226         default:
227                 return false;
228         }
229 }
230
231
232 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
233 {
234         switch (cmd.action()) {
235         case LFUN_INSET_MODIFY:
236                 cur.recordUndo();
237                 setInfo(to_utf8(cmd.argument()));
238                 break;
239
240         case LFUN_INSET_COPY_AS: {
241                 cap::clearSelection();
242                 Cursor copy(cur);
243                 copy.pushBackward(*this);
244                 copy.pit() = 0;
245                 copy.pos() = 0;
246                 copy.resetAnchor();
247                 copy.pit() = copy.lastpit();
248                 copy.pos() = copy.lastpos();
249                 copy.setSelection();
250                 cap::copySelection(copy);
251                 break;
252         }
253
254         default:
255                 InsetCollapsable::doDispatch(cur, cmd);
256                 break;
257         }
258 }
259
260
261 void InsetInfo::setInfo(string const & name)
262 {
263         if (name.empty())
264                 return;
265         // info_type name
266         string type;
267         name_ = trim(split(name, type, ' '));
268         type_ = nameTranslator().find(type);
269         updateInfo();
270 }
271
272
273 void InsetInfo::error(string const & err)
274 {
275         setText(bformat(_(err), from_utf8(name_)), 
276                 Font(inherit_font, buffer().params().language), false);
277 }
278
279
280 void InsetInfo::setText(docstring const & str)
281 {
282         setText(str, Font(inherit_font, buffer().params().language), false);
283 }
284
285
286 void InsetInfo::updateInfo()
287 {
288         BufferParams const & bp = buffer().params();    
289
290         switch (type_) {
291         case UNKNOWN_INFO:
292                 error("Unknown Info: %1$s");
293                 break;
294         case SHORTCUT_INFO:
295         case SHORTCUTS_INFO: {
296                 FuncRequest const func = lyxaction.lookupFunc(name_);
297                 if (func.action() == LFUN_UNKNOWN_ACTION) {
298                         error("Unknown action %1$s");
299                         break;
300                 }
301                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
302                 if (bindings.empty()) {
303                         // It is impropriate to use error() for undefined shortcut
304                         setText(_("undefined"));
305                         break;
306                 }
307                 if (type_ == SHORTCUT_INFO)
308                         setText(bindings.begin()->print(KeySequence::Portable));
309                 else
310                         setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
311                 break;
312         }
313         case LYXRC_INFO: {
314                 ostringstream oss;
315                 if (name_.empty()) {
316                         setText(_("undefined"));
317                         break;
318                 }
319                 lyxrc.write(oss, true, name_);
320                 string result = oss.str();
321                 if (result.size() < 2) {
322                         setText(_("undefined"));
323                         break;
324                 }
325                 string::size_type loc = result.rfind("\n", result.size() - 2);
326                 loc = loc == string::npos ? 0 : loc + 1;
327                 if (result.size() < loc + name_.size() + 1
328                           || result.substr(loc + 1, name_.size()) != name_) {
329                         setText(_("undefined"));
330                         break;
331                 }
332                 // remove leading comments and \\name and space
333                 result = result.substr(loc + name_.size() + 2);
334                 
335                 // remove \n and ""
336                 result = rtrim(result, "\n");
337                 result = trim(result, "\"");
338                 setText(from_utf8(result));
339                 break;
340         }
341         case PACKAGE_INFO:
342                 // check in packages.lst
343                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
344                 break;
345
346         case TEXTCLASS_INFO: {
347                 // name_ is the class name
348                 LayoutFileList const & list = LayoutFileList::get();
349                 bool available = false;
350                 if (list.haveClass(name_))
351                         available = list[name_].isTeXClassAvailable();
352                 setText(available ? _("yes") : _("no"));
353                 break;
354         }
355         case MENU_INFO: {
356                 docstring_list names;
357                 FuncRequest const func = lyxaction.lookupFunc(name_);
358                 if (func.action() == LFUN_UNKNOWN_ACTION) {
359                         error("Unknown action %1$s");
360                         break;
361                 }
362                 // iterate through the menubackend to find it
363                 if (!theApp()) {
364                         error("Can't determine menu entry for action %1$s in batch mode");
365                         break;
366                 }
367                 if (!theApp()->searchMenu(func, names)) {
368                         error("No menu entry for action %1$s");
369                         break;
370                 }
371                 // if found, return its path.
372                 clear();
373                 Paragraph & par = paragraphs().front();
374                 Font const f(inherit_font, buffer().params().language);
375                 //Font fu = f;
376                 //fu.fontInfo().setUnderbar(FONT_ON);
377                 docstring_list::const_iterator beg = names.begin();
378                 docstring_list::const_iterator end = names.end();
379                 for (docstring_list::const_iterator it = beg ; 
380                      it != end ; ++it) {
381                         // do not insert > for the top level menu item
382                         if (it != beg)
383                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
384                                                 Change(Change::UNCHANGED));
385                         //FIXME: add proper underlines here. This
386                         // involves rewriting searchMenu used above to
387                         // return a vector of menus. If we do not do
388                         // that, we might as well use below
389                         // Paragraph::insert on each string (JMarc)
390                         for (size_type i = 0; i != it->length(); ++i)
391                                 par.insertChar(par.size(), (*it)[i], 
392                                                f, Change(Change::UNCHANGED));
393                 }
394                 break;
395         }
396         case ICON_INFO: {
397                 FuncRequest func = lyxaction.lookupFunc(name_);
398                 docstring icon_name = frontend::Application::iconName(func, true);
399                 //FIXME: We should use the icon directly instead of
400                 // going through FileName. The code below won't work
401                 // if the icon is embedded in the executable through
402                 // the Qt resource system.
403                 // This is only a negligible performance problem:
404                 // If the installed icon differs from the resource icon the
405                 // installed one is preferred anyway, and all icons that are
406                 // embedded in the resources are installed as well.
407                 FileName file(to_utf8(icon_name));
408                 if (!file.exists())
409                         break;
410                 InsetGraphics * inset = new InsetGraphics(buffer_);
411                 InsetGraphicsParams igp;
412                 igp.filename = file;
413                 inset->setParams(igp);
414                 clear();
415                 paragraphs().front().insertInset(0, inset, 
416                                                  Change(Change::UNCHANGED));
417                 break;
418         }
419         case BUFFER_INFO: {
420                 if (name_ == "name") {
421                         setText(from_utf8(buffer().fileName().onlyFileName()));
422                         break;
423                 }
424                 if (name_ == "path") {
425                         setText(from_utf8(os::latex_path(buffer().filePath())));
426                         break;
427                 }
428                 if (name_ == "class") {
429                         setText(from_utf8(bp.documentClass().name()));
430                         break;
431                 }
432                 
433                 // everything that follows is for version control.
434                 // nothing that isn't version control should go below this line.
435                 if (!buffer().lyxvc().inUse()) {
436                         setText(_("No version control"));
437                         break;
438                 }
439                 LyXVC::RevisionInfo itype = LyXVC::Unknown;
440                 if (name_ == "vcs-revision")
441                         itype = LyXVC::File;
442                 else if (name_ == "vcs-tree-revision")
443                         itype = LyXVC::Tree;
444                 else if (name_ == "vcs-author")
445                         itype = LyXVC::Author;
446                 else if (name_ == "vcs-time")
447                         itype = LyXVC::Time;
448                 else if (name_ == "vcs-date")
449                         itype = LyXVC::Date;
450                 string binfo = buffer().lyxvc().revisionInfo(itype);
451                 if (binfo.empty())
452                         setText(from_ascii(name_) + " unknown");
453                 else
454                         setText(from_utf8(binfo));
455                 break;
456         }
457         case LYX_INFO:
458                 if (name_ == "version")
459                         setText(from_ascii(lyx_version));
460                 break;
461         }
462 }
463
464
465 string InsetInfo::contextMenu(BufferView const &, int, int) const
466 {
467         //FIXME: We override the implementation of InsetCollapsable,
468         //because this inset is not a collapsable inset.
469         return contextMenuName();
470 }
471
472
473 string InsetInfo::contextMenuName() const
474 {
475         return "context-info";
476 }
477
478
479 } // namespace lyx