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