]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
271539b4b6096460f98359f9c3fdfa6ad812bfd6
[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 "InsetGraphics.h"
20 #include "InsetSpecialChar.h"
21 #include "KeyMap.h"
22 #include "LaTeXFeatures.h"
23 #include "LyXAction.h"
24 #include "LyXRC.h"
25 #include "Lexer.h"
26 #include "MetricsInfo.h"
27 #include "ParagraphParameters.h"
28
29 #include "frontends/Application.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/docstring_list.h"
34 #include "support/FileName.h"
35 #include "support/filetools.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38 #include "support/ExceptionMessage.h"
39
40 #include <sstream>
41
42 using namespace std;
43 using namespace lyx::support;
44
45 namespace lyx {
46
47
48 InsetInfo::InsetInfo(Buffer const & buf, string const & name) 
49         : InsetText(buf), type_(UNKNOWN_INFO), name_(),
50           mouse_hover_(false)
51 {
52         setAutoBreakRows(true);
53         setDrawFrame(true);
54         setInfo(name);
55 }
56
57
58 Inset * InsetInfo::editXY(Cursor &, int, int)
59 {
60         return this;
61 }
62
63
64 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
65 {
66         InsetText::draw(pi, x, y); 
67 }
68
69
70 docstring InsetInfo::toolTip(BufferView const &, int, int) const
71 {
72         odocstringstream os;
73         os << _("Information regarding ")
74            << _(nameTranslator().find(type_))
75            << " " << from_utf8(name_);
76         return os.str();
77 }
78
79 namespace {
80
81 Translator<InsetInfo::info_type, string> const initTranslator()
82 {       
83         Translator<InsetInfo::info_type, string> 
84                 translator(InsetInfo::UNKNOWN_INFO, "unknown");
85
86         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
87         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
88         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
89         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
90         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
91         translator.addPair(InsetInfo::MENU_INFO, "menu");
92         translator.addPair(InsetInfo::ICON_INFO, "icon");
93         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
94
95         return translator;
96 }
97
98 } // namespace anon
99
100 Translator<InsetInfo::info_type, string> 
101         const & InsetInfo::nameTranslator() const
102 {
103         static Translator<info_type, string> const translator = initTranslator();
104         return translator;
105 }
106
107         
108
109 void InsetInfo::read(Lexer & lex)
110 {
111         string token;
112         while (lex.isOK()) {
113                 lex.next();
114                 token = lex.getString();
115                 if (token == "type") {
116                         lex.next();
117                         token = lex.getString();
118                         type_ = nameTranslator().find(token);
119                 } else if (token == "arg") {
120                         lex.next();
121                         name_ = lex.getString();
122                 } else if (token == "\\end_inset")
123                         break;
124         }       
125         if (token != "\\end_inset") {
126                 lex.printError("Missing \\end_inset at this point");
127                 throw ExceptionMessage(WarningException,
128                         _("Missing \\end_inset at this point."),
129                         from_utf8(token));
130         }
131         updateInfo();
132 }
133
134
135 void InsetInfo::write(ostream & os) const
136 {
137         os << "Info\ntype  \"" << nameTranslator().find(type_)
138            << "\"\narg   \"" << name_ << '\"';
139 }
140
141
142 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
143 {
144         // FIXME: we should allow selection, copy etc...
145         switch (cmd.action) {
146         case LFUN_MOUSE_PRESS:
147         case LFUN_MOUSE_RELEASE:
148         case LFUN_MOUSE_MOTION:
149         case LFUN_MOUSE_DOUBLE:
150         case LFUN_MOUSE_TRIPLE:
151                 // do not dispatch to InsetText
152                 cur.dispatched();
153                 break;
154
155         default:
156                 InsetText::doDispatch(cur, cmd);
157                 break;
158         }
159 }
160
161
162 void InsetInfo::setInfo(string const & name)
163 {
164         if (name.empty())
165                 return;
166         // info_type name
167         string type;
168         name_ = trim(split(name, type, ' '));
169         type_ = nameTranslator().find(type);
170 }
171
172
173 void InsetInfo::updateInfo()
174 {
175         InsetText::clear();
176
177         BufferParams const & bp = buffer().params();    
178
179         switch (type_) {
180         case UNKNOWN_INFO:
181                 setText(_("Unknown Info: ") + from_utf8(name_),
182                         bp.getFont(), false);
183                 break;
184         case SHORTCUTS_INFO: {
185                 FuncRequest func = lyxaction.lookupFunc(name_);
186                 if (func.action != LFUN_UNKNOWN_ACTION)
187                         setText(theTopLevelKeymap().printBindings(func),
188                                 bp.getFont(), false);
189                 break;
190         }
191         case SHORTCUT_INFO: {
192                 FuncRequest func = lyxaction.lookupFunc(name_);
193                 if (func.action != LFUN_UNKNOWN_ACTION) {
194                         KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
195                         if (!bindings.empty())
196                                 setText(bindings.rbegin()->print(KeySequence::ForGui),
197                                         bp.getFont(), false);
198                 }
199                 break;
200         }
201         case LYXRC_INFO: {
202                 ostringstream oss;
203                 lyxrc.write(oss, true, name_);
204                 string result = oss.str();
205                 // remove leading \\name
206                 result = result.substr(name_.size() + 2);
207                 // remove \n and ""
208                 result = rtrim(result, "\n");
209                 result = trim(result, "\"");
210                 setText(from_utf8(result), bp.getFont(), false);
211                 break;
212         }
213         case PACKAGE_INFO:
214                 // check in packages.lst
215                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
216                         bp.getFont(), false);
217                 break;
218         case TEXTCLASS_INFO: {
219                 // name_ is the class name
220                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
221                 bp.getFont(), false);
222                 break;
223         }
224         case MENU_INFO: {
225                 docstring_list names;
226                 FuncRequest func = lyxaction.lookupFunc(name_);
227                 if (func.action == LFUN_UNKNOWN_ACTION) {
228                         setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
229                         break;
230                 }
231                 // iterate through the menubackend to find it
232                 if (!theApp()->searchMenu(func, names)) {
233                         setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
234                                 bp.getFont(), false);
235                         break;
236                 }
237                 // if find, return its path.
238                 InsetText::clear();
239                 Paragraph & info = paragraphs().front();
240                 unsigned int i = 0;
241                 while (!names.empty()) {
242                         // do not insert > for the top level menu item
243                         if (i != 0)
244                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
245                                         Change(Change::UNCHANGED));
246                         for (i = 0; i != names.back().length(); ++i)
247                                 info.insertChar(i, names.back()[i], bp.getFont(), false);
248                         names.pop_back();
249                 }
250                 break;
251         }
252         case ICON_INFO: {
253                 FuncRequest func = lyxaction.lookupFunc(name_);
254                 docstring icon_name = theApp()->iconName(func, true);
255                 //FIXME: We should use the icon directly instead of going through
256                 // FileName. The code below won't work if the icon is embedded in the
257                 // executable through the Qt resource system.
258                 FileName file(to_utf8(icon_name));
259                 if (!file.exists())
260                         break;
261                 InsetGraphicsParams igp;
262                 igp.filename = file;
263                 InsetGraphics * inset = new InsetGraphics(buffer());
264                 inset->setParams(igp);
265                 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
266                 break;
267         }
268         case BUFFER_INFO: {
269                 if (name_ == "name")
270                         setText(from_utf8(buffer().fileName().onlyFileName()),
271                                 bp.getFont(), false);
272                 else if (name_ == "path")
273                         setText(from_utf8(buffer().filePath()), bp.getFont(), false);
274                 else if (name_ == "class")
275                         setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
276                 else
277                         setText(_("Unknown buffer info"), bp.getFont(), false);
278                 break;
279         }
280         }
281         // remove indent
282         paragraphs().begin()->params().noindent(true);
283 }
284
285
286 bool InsetInfo::setMouseHover(bool mouse_hover)
287 {
288         mouse_hover_ = mouse_hover;
289         return true;
290 }
291
292 } // namespace lyx