]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
(temporarily) add a Buffer * Inset::buffer_ membert
[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 static Translator<InsetInfo::info_type, string> initTranslator()
77 {       
78         Translator<InsetInfo::info_type, string>
79                 translator(InsetInfo::UNKNOWN_INFO, "unknown");
80
81         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
82         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
83         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
84         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
85         translator.addPair(InsetInfo::MENU_INFO, "menu");
86         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
87
88         return translator;
89 }
90
91 Translator<InsetInfo::info_type, string> const & InsetInfo::nameTranslator() const
92 {
93         static Translator<info_type, string> const translator = initTranslator();
94         return translator;
95 }
96
97
98 void InsetInfo::read(Buffer const & buf, 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(buf);
121 }
122
123
124 void InsetInfo::write(Buffer const &, ostream & os) const
125 {
126         os << "Info\ntype  \"" << nameTranslator().find(type_)
127            << "\"\narg   \"" << name_ << '\"';
128 }
129
130
131 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
132 {
133         // FIXME: we should allow selection, copy etc...
134         switch (cmd.action) {
135         case LFUN_MOUSE_PRESS:
136         case LFUN_MOUSE_RELEASE:
137         case LFUN_MOUSE_MOTION:
138         case LFUN_MOUSE_DOUBLE:
139         case LFUN_MOUSE_TRIPLE:
140                 // do not dispatch to InsetText
141                 cur.dispatched();
142                 break;
143
144         default:
145                 InsetText::doDispatch(cur, cmd);
146                 break;
147         }
148 }
149
150
151 void InsetInfo::setInfo(string const & name)
152 {
153         if (name.empty())
154                 return;
155         // info_type name
156         string type;
157         name_ = trim(split(name, type, ' '));
158         type_ = nameTranslator().find(type);
159 }
160
161
162 void InsetInfo::updateInfo(Buffer const & buf)
163 {
164         InsetText::clear();
165
166         BufferParams const & bp = buf.params(); 
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, TextClassIndex> pp = textclasslist.numberOfClass(name_);
200                 setText(pp.first ? _("yes") : _("no"),
201                         bp.getFont(), false);
202                 break;
203         }
204         case MENU_INFO: {
205                 stack<docstring> names;
206                 FuncRequest func = lyxaction.lookupFunc(name_);
207                 if (func.action == LFUN_UNKNOWN_ACTION) {
208                         setText(_("No menu entry for "), bp.getFont(), false);
209                         break;
210                 }
211                 // iterate through the menubackend to find it
212                 Menu menu = theApp()->menuBackend().getMenubar();
213                 if (!menu.searchFunc(func, names)) {
214                         setText(_("No menu entry for "), bp.getFont(), false);
215                         break;
216                 }
217                 // if find, return its path.
218                 InsetText::clear();
219                 Paragraph & info = paragraphs().front();
220                 unsigned int i = 0;
221                 while (!names.empty()) {
222                         // do not insert > for the top level menu item
223                         if (i != 0)
224                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
225                                         Change(Change::UNCHANGED));
226                         for (i = 0; i < names.top().length(); ++i)
227                                 info.insertChar(i, names.top()[i], bp.getFont(), false);
228                         names.pop();
229                 }
230                 break;
231         }
232         case BUFFER_INFO: {
233                 if (name_ == "name")
234                         setText(from_utf8(buf.fileName().onlyFileName()), bp.getFont(), false);
235                 else if (name_ == "path")
236                         setText(from_utf8(buf.filePath()), bp.getFont(), false);
237                 else if (name_ == "class")
238                         setText(from_utf8(bp.textClass().name()), bp.getFont(), false);
239                 else
240                         setText(_("Unknown buffer info"), bp.getFont(), false);
241                 break;
242         }
243         }
244         // remove indent
245         paragraphs().begin()->params().noindent(true);
246 }
247
248
249 bool InsetInfo::setMouseHover(bool mouse_hover)
250 {
251         mouse_hover_ = mouse_hover;
252         return true;
253 }
254
255 } // namespace lyx