]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Cleanup InsetTabular::getStatus
[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 "MetricsInfo.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
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         switch (nameTranslator().find(type)) {
159         case UNKNOWN_INFO:
160                 return false;
161         case SHORTCUT_INFO:
162         case SHORTCUTS_INFO:
163         case MENU_INFO:
164         case ICON_INFO: {
165                 FuncRequest func = lyxaction.lookupFunc(name);
166                 return func.action != LFUN_UNKNOWN_ACTION;
167         }
168         case LYXRC_INFO: {
169                 ostringstream oss;
170                 lyxrc.write(oss, true, name);
171                 return !oss.str().empty();
172         }
173         case PACKAGE_INFO:
174         case TEXTCLASS_INFO:
175                 return true;
176         case BUFFER_INFO:
177                 return name == "name" || name == "path" || name == "class" ||
178                        name == "vcs-revision" || name == "vcs-tree-revision" ||
179                        name == "vcs-author" || name == "vcs-date" || name == "vcs-time";
180         }
181         return false;
182 }
183
184
185 bool InsetInfo::showInsetDialog(BufferView * bv) const
186 {
187         bv->showDialog("info");
188         return true;
189 }
190
191
192 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
193                 FuncStatus & flag) const
194 {
195         switch (cmd.action) {
196         case LFUN_INSET_SETTINGS:
197                 return InsetCollapsable::getStatus(cur, cmd, flag);
198                 
199         case LFUN_INSET_DIALOG_UPDATE:
200                 flag.setEnabled(true);
201                 return true;
202                 
203         case LFUN_INSET_MODIFY:
204                 if (validateModifyArgument(cmd.argument())) {
205                         flag.setEnabled(true);
206                         return true;
207                 }
208                 //fall back
209                 
210         default:
211                 return false;
212         }
213 }
214
215
216 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
217 {
218         // allow selection, copy but not cut, delete etc
219         switch (cmd.action) {
220         case LFUN_INSET_SETTINGS:
221                 InsetCollapsable::doDispatch(cur, cmd);
222                 break;
223
224         case LFUN_INSET_MODIFY:
225                 setInfo(to_utf8(cmd.argument()));
226                 break;
227
228         default:
229                 break;
230         }
231 }
232
233
234 void InsetInfo::setInfo(string const & name)
235 {
236         if (name.empty())
237                 return;
238         // info_type name
239         string type;
240         name_ = trim(split(name, type, ' '));
241         type_ = nameTranslator().find(type);
242         updateInfo();
243 }
244
245
246 void InsetInfo::error(string const & err)
247 {
248         setText(bformat(_(err), from_utf8(name_)), 
249                 Font(inherit_font, buffer().params().language), false);
250 }
251
252
253 void InsetInfo::setText(docstring const & str)
254 {
255         setText(str, Font(inherit_font, buffer().params().language), false);
256 }
257
258
259 void InsetInfo::updateInfo()
260 {
261         BufferParams const & bp = buffer().params();    
262
263         switch (type_) {
264         case UNKNOWN_INFO:
265                 error("Unknown Info: %1$s");
266                 break;
267         case SHORTCUT_INFO:
268         case SHORTCUTS_INFO: {
269                 FuncRequest func = lyxaction.lookupFunc(name_);
270                 if (func.action == LFUN_UNKNOWN_ACTION) {
271                         error("Unknown action %1$s");
272                         break;
273                 }
274                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
275                 if (bindings.empty()) {
276                         // It is impropriate to use error() for undefined shortcut
277                         setText(_("undefined"));
278                         break;
279                 }
280                 if (type_ == SHORTCUT_INFO)
281                         setText(bindings.begin()->print(KeySequence::Portable));
282                 else
283                         setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
284                 break;
285         }
286         case LYXRC_INFO: {
287                 ostringstream oss;
288                 lyxrc.write(oss, true, name_);
289                 string result = oss.str();
290                 // remove leading \\name
291                 result = result.substr(name_.size() + 2);
292                 // remove \n and ""
293                 result = rtrim(result, "\n");
294                 result = trim(result, "\"");
295                 setText(from_utf8(result));
296                 break;
297         }
298         case PACKAGE_INFO:
299                 // check in packages.lst
300                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
301                 break;
302         case TEXTCLASS_INFO: {
303                 // name_ is the class name
304                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
305                 break;
306         }
307         case MENU_INFO: {
308                 docstring_list names;
309                 FuncRequest func = lyxaction.lookupFunc(name_);
310                 if (func.action == LFUN_UNKNOWN_ACTION) {
311                         error("Unknown action %1$s");
312                         break;
313                 }
314                 // iterate through the menubackend to find it
315                 if (!theApp()->searchMenu(func, names)) {
316                         error("No menu entry for action %1$s");
317                         break;
318                 }
319                 // if found, return its path.
320                 clear();
321                 Paragraph & par = paragraphs().front();
322                 Font const f(inherit_font, buffer().params().language);
323                 //Font fu = f;
324                 //fu.fontInfo().setUnderbar(FONT_ON);
325                 docstring_list::const_iterator beg = names.begin();
326                 docstring_list::const_iterator end = names.end();
327                 for (docstring_list::const_iterator it = beg ; 
328                      it != end ; ++it) {
329                         // do not insert > for the top level menu item
330                         if (it != beg)
331                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
332                                                 Change(Change::UNCHANGED));
333                         //FIXME: add proper underlines here. This
334                         // involves rewriting searchMenu used above to
335                         // return a vector of menus. If we do not do
336                         // that, we might as well use below
337                         // Paragraph::insert on each string (JMarc)
338                         for (size_type i = 0; i != it->length(); ++i)
339                                 par.insertChar(par.size(), (*it)[i], 
340                                                f, Change(Change::UNCHANGED));
341                 }
342                 break;
343         }
344         case ICON_INFO: {
345                 FuncRequest func = lyxaction.lookupFunc(name_);
346                 docstring icon_name = theApp()->iconName(func, true);
347                 //FIXME: We should use the icon directly instead of
348                 // going through FileName. The code below won't work
349                 // if the icon is embedded in the executable through
350                 // the Qt resource system.
351                 FileName file(to_utf8(icon_name));
352                 if (!file.exists())
353                         break;
354                 InsetGraphics * inset = new InsetGraphics(buffer_);
355                 InsetGraphicsParams igp;
356                 igp.filename = file;
357                 inset->setParams(igp);
358                 clear();
359                 paragraphs().front().insertInset(0, inset, 
360                                                  Change(Change::UNCHANGED));
361                 break;
362         }
363         case BUFFER_INFO: {
364                 if (name_ == "name")
365                         setText(from_utf8(buffer().fileName().onlyFileName()));
366                 else if (name_ == "path")
367                         setText(from_utf8(buffer().filePath()));
368                 else if (name_ == "class")
369                         setText(from_utf8(bp.documentClass().name()));
370                 else if (name_ == "vcs-revision" && buffer().lyxvc().inUse() &&
371                          !buffer().lyxvc().revisionInfo(LyXVC::File).empty())
372                         setText(from_utf8(buffer().lyxvc().revisionInfo(LyXVC::File)));
373                 else if (name_ == "vcs-tree-revision" && buffer().lyxvc().inUse() &&
374                          !buffer().lyxvc().revisionInfo(LyXVC::Tree).empty())
375                         setText(from_utf8(buffer().lyxvc().revisionInfo(LyXVC::Tree)));
376                 else if (name_ == "vcs-author" && buffer().lyxvc().inUse() &&
377                          !buffer().lyxvc().revisionInfo(LyXVC::Author).empty())
378                         setText(from_utf8(buffer().lyxvc().revisionInfo(LyXVC::Author)));
379                 else if (name_ == "vcs-time" && buffer().lyxvc().inUse() &&
380                          !buffer().lyxvc().revisionInfo(LyXVC::Time).empty())
381                         setText(from_utf8(buffer().lyxvc().revisionInfo(LyXVC::Time)));
382                 else if (name_ == "vcs-date" && buffer().lyxvc().inUse() &&
383                          !buffer().lyxvc().revisionInfo(LyXVC::Date).empty())
384                         setText(from_utf8(buffer().lyxvc().revisionInfo(LyXVC::Date)));
385                 else
386                         setText(_("Unknown buffer info"));
387                 break;
388         }
389         }
390 }
391
392
393 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
394 {
395         return from_ascii("context-info");
396 }
397
398
399 } // namespace lyx