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