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