]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
fix View Source for literate documents
[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 "Lexer.h"
27 #include "MetricsInfo.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 const & buf, string const & name) 
86         : InsetCollapsable(buf, Collapsed), type_(UNKNOWN_INFO), name_()
87 {
88         setAutoBreakRows(true);
89         setInfo(name);
90 }
91
92
93 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
94 {
95         cur.push(*this);
96         return InsetCollapsable::editXY(cur, x, y);
97 }
98
99
100 string InsetInfo::infoType() const
101 {
102         return nameTranslator().find(type_);
103 }
104
105
106 docstring InsetInfo::name() const 
107 {
108         return from_ascii("Info:" + infoType());
109 }
110
111
112 docstring InsetInfo::toolTip(BufferView const &, int, int) const
113 {
114         odocstringstream os;
115         os << _("Information regarding ")
116            << _(infoType()) << " " << from_utf8(name_);
117         return os.str();
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();
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         setLayout(buffer().params());
144         updateInfo();
145 }
146
147
148 void InsetInfo::write(ostream & os) const
149 {
150         os << "Info\ntype  \"" << infoType() << "\"\narg   \"" << name_ << '\"';
151 }
152
153
154 bool InsetInfo::validate(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         }
179         return false;
180 }
181
182
183 bool InsetInfo::showInsetDialog(BufferView * bv) const
184 {
185         bv->showDialog("info");
186         return true;
187 }
188
189
190 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
191                 FuncStatus & flag) const
192 {
193         switch (cmd.action) {
194         case LFUN_MOUSE_PRESS:
195         case LFUN_MOUSE_RELEASE:
196         case LFUN_MOUSE_MOTION:
197         case LFUN_MOUSE_DOUBLE:
198         case LFUN_MOUSE_TRIPLE:
199         case LFUN_COPY:
200         case LFUN_INSET_SETTINGS:
201                 return InsetCollapsable::getStatus(cur, cmd, flag);
202
203         case LFUN_INSET_MODIFY:
204                 flag.setEnabled(true);
205                 break;
206
207         default:
208                 return false;
209         }
210         return true;
211 }
212
213
214 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
215 {
216         // allow selection, copy but not cut, delete etc
217         switch (cmd.action) {
218         case LFUN_MOUSE_PRESS:
219         case LFUN_MOUSE_RELEASE:
220         case LFUN_MOUSE_MOTION:
221         case LFUN_MOUSE_DOUBLE:
222         case LFUN_MOUSE_TRIPLE:
223         case LFUN_COPY:
224         case LFUN_INSET_SETTINGS:
225                 InsetCollapsable::doDispatch(cur, cmd);
226                 break;
227
228         case LFUN_INSET_MODIFY:
229                 setInfo(to_utf8(cmd.argument()));
230                 cur.pos() = 0;
231                 break;
232
233         default:
234                 break;
235         }
236 }
237
238
239 void InsetInfo::setInfo(string const & name)
240 {
241         if (name.empty())
242                 return;
243         // info_type name
244         string type;
245         name_ = trim(split(name, type, ' '));
246         type_ = nameTranslator().find(type);
247         setLayout(buffer().params());
248         updateInfo();
249 }
250
251
252 void InsetInfo::error(string const & err)
253 {
254         setText(bformat(_(err), from_utf8(name_)), Font(inherit_font), false);
255 }
256
257
258 void InsetInfo::setText(docstring const & str)
259 {
260         setText(str, Font(inherit_font), false);
261 }
262
263
264 void InsetInfo::updateInfo()
265 {
266         BufferParams const & bp = buffer().params();    
267
268         switch (type_) {
269         case UNKNOWN_INFO:
270                 error("Unknown Info: %1$s");
271                 break;
272         case SHORTCUT_INFO:
273         case SHORTCUTS_INFO: {
274                 FuncRequest func = lyxaction.lookupFunc(name_);
275                 if (func.action == LFUN_UNKNOWN_ACTION) {
276                         error("Unknown action %1$s");
277                         break;
278                 }
279                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
280                 if (bindings.empty()) {
281                         // It is impropriate to use error() for undefined shortcut
282                         setText(_("undefined"));
283                         break;
284                 }
285                 if (type_ == SHORTCUT_INFO)
286                         setText(bindings.rbegin()->print(KeySequence::Portable),
287                                 Font(getLayout().font()), false);
288                 else
289                         setText(theTopLevelKeymap().printBindings(func), 
290                                 Font(getLayout().font()), false);
291                 break;
292         }
293         case LYXRC_INFO: {
294                 ostringstream oss;
295                 lyxrc.write(oss, true, name_);
296                 string result = oss.str();
297                 // remove leading \\name
298                 result = result.substr(name_.size() + 2);
299                 // remove \n and ""
300                 result = rtrim(result, "\n");
301                 result = trim(result, "\"");
302                 setText(from_utf8(result));
303                 break;
304         }
305         case PACKAGE_INFO:
306                 // check in packages.lst
307                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
308                 break;
309         case TEXTCLASS_INFO: {
310                 // name_ is the class name
311                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
312                 break;
313         }
314         case MENU_INFO: {
315                 docstring_list names;
316                 FuncRequest func = lyxaction.lookupFunc(name_);
317                 if (func.action == LFUN_UNKNOWN_ACTION) {
318                         error("Unknown action %1$s");
319                         break;
320                 }
321                 // iterate through the menubackend to find it
322                 if (!theApp()->searchMenu(func, names)) {
323                         error("No menu entry for action %1$s");
324                         break;
325                 }
326                 // if found, return its path.
327                 clear();
328                 Paragraph & par = paragraphs().front();
329                 Font const f = Font(getLayout().font());
330                 //Font fu = f;
331                 //fu.fontInfo().setUnderbar(FONT_ON);
332                 docstring_list::const_iterator beg = names.begin();
333                 docstring_list::const_iterator end = names.end();
334                 for (docstring_list::const_iterator it = beg ; 
335                      it != end ; ++it) {
336                         // do not insert > for the top level menu item
337                         if (it != beg)
338                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
339                                                 Change(Change::UNCHANGED));
340                         //FIXME: add proper underlines here. This
341                         // involves rewriting searchMenu used above to
342                         // return a vector of menus. If we do not do
343                         // that, we might as well use below
344                         // Paragraph::insert on each string (JMarc)
345                         for (size_type i = 0; i != it->length(); ++i)
346                                 par.insertChar(par.size(), (*it)[i], 
347                                                f, Change(Change::UNCHANGED));
348                 }
349                 break;
350         }
351         case ICON_INFO: {
352                 FuncRequest func = lyxaction.lookupFunc(name_);
353                 docstring icon_name = theApp()->iconName(func, true);
354                 //FIXME: We should use the icon directly instead of
355                 // going through FileName. The code below won't work
356                 // if the icon is embedded in the executable through
357                 // the Qt resource system.
358                 FileName file(to_utf8(icon_name));
359                 if (!file.exists())
360                         break;
361                 InsetGraphics * inset = new InsetGraphics(buffer());
362                 InsetGraphicsParams igp;
363                 igp.filename = file;
364                 inset->setParams(igp);
365                 clear();
366                 paragraphs().front().insertInset(0, inset, 
367                                                  Change(Change::UNCHANGED));
368                 break;
369         }
370         case BUFFER_INFO: {
371                 if (name_ == "name")
372                         setText(from_utf8(buffer().fileName().onlyFileName()));
373                 else if (name_ == "path")
374                         setText(from_utf8(buffer().filePath()));
375                 else if (name_ == "class")
376                         setText(from_utf8(bp.documentClass().name()));
377                 else
378                         setText(_("Unknown buffer info"));
379                 break;
380         }
381         }
382 }
383
384
385 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
386 {
387         return from_ascii("context-info");
388 }
389
390
391 } // namespace lyx