]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
2efedc6f2e726757758626b658503fb06f5aca9a
[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 "LayoutFile.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
22 #include "KeyMap.h"
23 #include "LaTeXFeatures.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/FileName.h"
36 #include "support/filetools.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/ExceptionMessage.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 & argument) const
151 {
152         // FIXME!
153         return true;
154 }
155
156
157 bool InsetInfo::showInsetDialog(BufferView * bv) const
158 {
159         bv->showDialog("info");
160         return true;
161 }
162
163
164 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
165                 FuncStatus & flag) const
166 {
167         switch (cmd.action) {
168         case LFUN_MOUSE_PRESS:
169         case LFUN_MOUSE_RELEASE:
170         case LFUN_MOUSE_MOTION:
171         case LFUN_MOUSE_DOUBLE:
172         case LFUN_MOUSE_TRIPLE:
173         case LFUN_COPY:
174         case LFUN_INSET_SETTINGS:
175                 return InsetText::getStatus(cur, cmd, flag);
176
177         case LFUN_INSET_MODIFY:
178                 flag.setEnabled(true);
179                 break;
180
181         default:
182                 return false;
183         }
184         return true;
185 }
186
187
188 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
189 {
190         // allow selection, copy but not cut, delete etc
191         switch (cmd.action) {
192         case LFUN_MOUSE_PRESS:
193         case LFUN_MOUSE_RELEASE:
194         case LFUN_MOUSE_MOTION:
195         case LFUN_MOUSE_DOUBLE:
196         case LFUN_MOUSE_TRIPLE:
197         case LFUN_COPY:
198         case LFUN_INSET_SETTINGS:
199                 InsetText::doDispatch(cur, cmd);
200                 break;
201
202         case LFUN_INSET_MODIFY:
203                 setInfo(to_utf8(cmd.argument()));
204                 cur.pos() = 0;
205                 break;
206
207         default:
208                 break;
209         }
210 }
211
212
213 void InsetInfo::setInfo(string const & name)
214 {
215         if (name.empty())
216                 return;
217         // info_type name
218         string type;
219         name_ = trim(split(name, type, ' '));
220         type_ = nameTranslator().find(type);
221         updateInfo();
222 }
223
224
225 void InsetInfo::updateInfo()
226 {
227         InsetText::clear();
228
229         BufferParams const & bp = buffer().params();    
230
231         switch (type_) {
232         case UNKNOWN_INFO:
233                 setText(_("Unknown Info: ") + from_utf8(name_),
234                         bp.getFont(), false);
235                 break;
236         case SHORTCUTS_INFO: {
237                 FuncRequest func = lyxaction.lookupFunc(name_);
238                 if (func.action != LFUN_UNKNOWN_ACTION)
239                         setText(theTopLevelKeymap().printBindings(func),
240                                 bp.getFont(), false);
241                 break;
242         }
243         case SHORTCUT_INFO: {
244                 FuncRequest func = lyxaction.lookupFunc(name_);
245                 if (func.action != LFUN_UNKNOWN_ACTION) {
246                         KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
247                         if (!bindings.empty())
248                                 setText(bindings.rbegin()->print(KeySequence::ForGui),
249                                         bp.getFont(), false);
250                 }
251                 break;
252         }
253         case LYXRC_INFO: {
254                 ostringstream oss;
255                 lyxrc.write(oss, true, name_);
256                 string result = oss.str();
257                 // remove leading \\name
258                 result = result.substr(name_.size() + 2);
259                 // remove \n and ""
260                 result = rtrim(result, "\n");
261                 result = trim(result, "\"");
262                 setText(from_utf8(result), bp.getFont(), false);
263                 break;
264         }
265         case PACKAGE_INFO:
266                 // check in packages.lst
267                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
268                         bp.getFont(), false);
269                 break;
270         case TEXTCLASS_INFO: {
271                 // name_ is the class name
272                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
273                 bp.getFont(), false);
274                 break;
275         }
276         case MENU_INFO: {
277                 docstring_list names;
278                 FuncRequest func = lyxaction.lookupFunc(name_);
279                 if (func.action == LFUN_UNKNOWN_ACTION) {
280                         setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
281                         break;
282                 }
283                 // iterate through the menubackend to find it
284                 if (!theApp()->searchMenu(func, names)) {
285                         setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
286                                 bp.getFont(), false);
287                         break;
288                 }
289                 // if find, return its path.
290                 InsetText::clear();
291                 Paragraph & info = paragraphs().front();
292                 unsigned int i = 0;
293                 while (!names.empty()) {
294                         // do not insert > for the top level menu item
295                         if (i != 0)
296                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
297                                         Change(Change::UNCHANGED));
298                         for (i = 0; i != names.back().length(); ++i)
299                                 info.insertChar(i, names.back()[i], bp.getFont(), false);
300                         names.pop_back();
301                 }
302                 break;
303         }
304         case ICON_INFO: {
305                 FuncRequest func = lyxaction.lookupFunc(name_);
306                 docstring icon_name = theApp()->iconName(func, true);
307                 //FIXME: We should use the icon directly instead of going through
308                 // FileName. The code below won't work if the icon is embedded in the
309                 // executable through the Qt resource system.
310                 FileName file(to_utf8(icon_name));
311                 if (!file.exists())
312                         break;
313                 InsetGraphicsParams igp;
314                 igp.filename = file;
315                 InsetGraphics * inset = new InsetGraphics(buffer());
316                 inset->setParams(igp);
317                 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
318                 break;
319         }
320         case BUFFER_INFO: {
321                 if (name_ == "name")
322                         setText(from_utf8(buffer().fileName().onlyFileName()),
323                                 bp.getFont(), false);
324                 else if (name_ == "path")
325                         setText(from_utf8(buffer().filePath()), bp.getFont(), false);
326                 else if (name_ == "class")
327                         setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
328                 else
329                         setText(_("Unknown buffer info"), bp.getFont(), false);
330                 break;
331         }
332         }
333         // remove indent
334         paragraphs().begin()->params().noindent(true);
335 }
336
337
338 bool InsetInfo::setMouseHover(bool mouse_hover)
339 {
340         mouse_hover_ = mouse_hover;
341         return true;
342 }
343
344
345 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
346 {
347         return from_ascii("context-info");
348 }
349
350 } // namespace lyx