]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
2218ef660d50bd5435d59f41adbe33ad4f0c5f9d
[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> translator(InsetInfo::UNKNOWN_INFO, "unknown");
81
82         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
83         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
84         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
85         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
86         translator.addPair(InsetInfo::MENU_INFO, "menu");
87         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
88
89         return translator;
90 }
91
92 } // namespace anon
93
94 Translator<InsetInfo::info_type, 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 & buf, 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(buf);
126 }
127
128
129 void InsetInfo::write(Buffer const &, 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 }
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::textclass_type> pp =
206                         textclasslist.numberOfClass(name_);
207                 setText(pp.first ? _("yes") : _("no"),
208                         bp.getFont(), false);
209                 break;
210         }
211         case MENU_INFO: {
212                 stack<docstring> names;
213                 FuncRequest func = lyxaction.lookupFunc(name_);
214                 if (func.action == LFUN_UNKNOWN_ACTION) {
215                         setText(_("No menu entry for "), bp.getFont(), false);
216                         break;
217                 }
218                 // iterate through the menubackend to find it
219                 Menu menu = theApp()->menuBackend().getMenubar();
220                 if (!menu.searchFunc(func, names)) {
221                         setText(_("No menu entry for "), bp.getFont(), false);
222                         break;
223                 }
224                 // if find, return its path.
225                 InsetText::clear();
226                 Paragraph & info = paragraphs().front();
227                 unsigned int i = 0;
228                 while (!names.empty()) {
229                         // do not insert > for the top level menu item
230                         if (i != 0)
231                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
232                                         Change(Change::UNCHANGED));
233                         for (i = 0; i < names.top().length(); ++i)
234                                 info.insertChar(i, names.top()[i], bp.getFont(), false);
235                         names.pop();
236                 }
237                 break;
238         }
239         case BUFFER_INFO: {
240                 if (name_ == "name")
241                         setText(from_utf8(buf.fileName().onlyFileName()), bp.getFont(), false);
242                 else if (name_ == "path")
243                         setText(from_utf8(buf.filePath()), bp.getFont(), false);
244                 else if (name_ == "class")
245                         setText(from_utf8(bp.getTextClass().name()), bp.getFont(), false);
246                 else
247                         setText(_("Unknown buffer info"), bp.getFont(), false);
248                 break;
249         }
250         }
251         // remove indent
252         paragraphs().begin()->params().noindent(true);
253 }
254
255
256 bool InsetInfo::setMouseHover(bool mouse_hover)
257 {
258         mouse_hover_ = mouse_hover;
259         return true;
260 }
261
262 } // namespace lyx