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