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