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