]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
5168447a4112d1cd8726d0e1ddc78f5109e1e80c
[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 "FuncRequest.h"
21 #include "InsetSpecialChar.h"
22 #include "KeyMap.h"
23 #include "LaTeXFeatures.h"
24 #include "LyXAction.h"
25 #include "LyXRC.h"
26 #include "Lexer.h"
27 #include "MenuBackend.h"
28 #include "MetricsInfo.h"
29 #include "ParagraphParameters.h"
30 #include "TextClassList.h"
31
32 #include "frontends/Application.h"
33
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/FileName.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/ExceptionMessage.h"
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46
47 InsetInfo::InsetInfo(BufferParams const & bp, string const & name) 
48         : InsetText(bp), type_(UNKNOWN_INFO), name_(),
49           mouse_hover_(false)
50 {
51         setAutoBreakRows(true);
52         setDrawFrame(true);
53         setInfo(name);
54 }
55
56
57 Inset * InsetInfo::editXY(Cursor &, int, int)
58 {
59         return this;
60 }
61
62
63 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
64 {
65         InsetText::draw(pi, x, y); 
66         if (mouse_hover_) {
67                 odocstringstream os;
68                 os << _("Information regarding ")
69                    <<_(nameTranslator().find(type_))
70                    << _(" ") << from_utf8(name_);
71                 pi.base.bv->message(os.str());
72         }
73 }
74
75
76 namespace {
77
78 Translator<InsetInfo::info_type, string> const initTranslator()
79 {       
80         Translator<InsetInfo::info_type, string> 
81                 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         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
89
90         return translator;
91 }
92
93 } // namespace anon
94
95 Translator<InsetInfo::info_type, string> 
96         const & InsetInfo::nameTranslator() const
97 {
98         static Translator<info_type, string> const translator = initTranslator();
99         return translator;
100 }
101
102         
103
104 void InsetInfo::read(Buffer const & buf, Lexer & lex)
105 {
106         string token;
107         while (lex.isOK()) {
108                 lex.next();
109                 token = lex.getString();
110                 if (token == "type") {
111                         lex.next();
112                         token = lex.getString();
113                         type_ = nameTranslator().find(token);
114                 } else if (token == "arg") {
115                         lex.next();
116                         name_ = lex.getString();
117                 } else if (token == "\\end_inset")
118                         break;
119         }       
120         if (token != "\\end_inset") {
121                 lex.printError("Missing \\end_inset at this point");
122                 throw ExceptionMessage(WarningException,
123                         _("Missing \\end_inset at this point."),
124                         from_utf8(token));
125         }
126         updateInfo(buf);
127 }
128
129
130 void InsetInfo::write(Buffer const &, ostream & os) const
131 {
132         os << "Info\ntype  \"" << 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 }
166
167
168 void InsetInfo::updateInfo(Buffer const & buf)
169 {
170         InsetText::clear();
171
172         BufferParams const & bp = buf.params(); 
173
174         switch (type_) {
175         case UNKNOWN_INFO:
176                 setText(_("Unknown Info: ") + from_utf8(name_),
177                         bp.getFont(), false);
178                 break;
179         case SHORTCUT_INFO: {
180                 FuncRequest func = lyxaction.lookupFunc(name_);
181                 if (func.action != LFUN_UNKNOWN_ACTION)
182                         setText(theTopLevelKeymap().printBindings(func),
183                                 bp.getFont(), false);
184                 break;
185         }
186         case LYXRC_INFO: {
187                 ostringstream oss;
188                 lyxrc.write(oss, true, name_);
189                 string result = oss.str();
190                 // remove leading \\name
191                 result = result.substr(name_.size() + 2);
192                 // remove \n and ""
193                 result = rtrim(result, "\n");
194                 result = trim(result, "\"");
195                 setText(from_utf8(result), bp.getFont(), false);
196                 break;
197         }
198         case PACKAGE_INFO:
199                 // check in packages.lst
200                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
201                         bp.getFont(), false);
202                 break;
203         case TEXTCLASS_INFO: {
204                 // name_ is the class name
205                 pair<bool, lyx::BaseClassIndex> pp = 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 = theApp()->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         case BUFFER_INFO: {
239                 if (name_ == "name")
240                         setText(from_utf8(buf.fileName().onlyFileName()), bp.getFont(), false);
241                 else if (name_ == "path")
242                         setText(from_utf8(buf.filePath()), bp.getFont(), false);
243                 else if (name_ == "class")
244                         setText(from_utf8(bp.textClass().name()), bp.getFont(), false);
245                 else
246                         setText(_("Unknown buffer info"), bp.getFont(), false);
247                 break;
248         }
249         }
250         // remove indent
251         paragraphs().begin()->params().noindent(true);
252 }
253
254
255 bool InsetInfo::setMouseHover(bool mouse_hover)
256 {
257         mouse_hover_ = mouse_hover;
258         return true;
259 }
260
261 } // namespace lyx