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