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