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