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