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