]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
31f787741ef1361cb7db9ce490b897a618bf746c
[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 "LayoutFile.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "FuncRequest.h"
19 #include "InsetSpecialChar.h"
20 #include "KeyMap.h"
21 #include "LaTeXFeatures.h"
22 #include "LyXAction.h"
23 #include "LyXRC.h"
24 #include "Lexer.h"
25 #include "MetricsInfo.h"
26 #include "ParagraphParameters.h"
27
28 #include "frontends/Application.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/docstring_list.h"
33 #include "support/FileName.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/ExceptionMessage.h"
37
38 #include <sstream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45
46 InsetInfo::InsetInfo(Buffer const & buf, string const & name) 
47         : InsetText(buf), type_(UNKNOWN_INFO), name_(),
48           mouse_hover_(false)
49 {
50         setAutoBreakRows(true);
51         setDrawFrame(true);
52         setInfo(name);
53 }
54
55
56 Inset * InsetInfo::editXY(Cursor &, int, int)
57 {
58         return this;
59 }
60
61
62 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
63 {
64         InsetText::draw(pi, x, y); 
65 }
66
67
68 docstring InsetInfo::toolTip(BufferView const & bv, int x, int y) const
69 {
70         odocstringstream os;
71         os << _("Information regarding ")
72            << _(nameTranslator().find(type_))
73            << " " << from_utf8(name_);
74         return os.str();
75 }
76
77 namespace {
78
79 Translator<InsetInfo::info_type, string> const initTranslator()
80 {       
81         Translator<InsetInfo::info_type, string> 
82                 translator(InsetInfo::UNKNOWN_INFO, "unknown");
83
84         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
85         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
86         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
87         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
88         translator.addPair(InsetInfo::MENU_INFO, "menu");
89         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
90
91         return translator;
92 }
93
94 } // namespace anon
95
96 Translator<InsetInfo::info_type, string> 
97         const & InsetInfo::nameTranslator() const
98 {
99         static Translator<info_type, string> const translator = initTranslator();
100         return translator;
101 }
102
103         
104
105 void InsetInfo::read(Lexer & lex)
106 {
107         string token;
108         while (lex.isOK()) {
109                 lex.next();
110                 token = lex.getString();
111                 if (token == "type") {
112                         lex.next();
113                         token = lex.getString();
114                         type_ = nameTranslator().find(token);
115                 } else if (token == "arg") {
116                         lex.next();
117                         name_ = lex.getString();
118                 } else if (token == "\\end_inset")
119                         break;
120         }       
121         if (token != "\\end_inset") {
122                 lex.printError("Missing \\end_inset at this point");
123                 throw ExceptionMessage(WarningException,
124                         _("Missing \\end_inset at this point."),
125                         from_utf8(token));
126         }
127         updateInfo();
128 }
129
130
131 void InsetInfo::write(ostream & os) const
132 {
133         os << "Info\ntype  \"" << nameTranslator().find(type_)
134            << "\"\narg   \"" << name_ << '\"';
135 }
136
137
138 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
139 {
140         // FIXME: we should allow selection, copy etc...
141         switch (cmd.action) {
142         case LFUN_MOUSE_PRESS:
143         case LFUN_MOUSE_RELEASE:
144         case LFUN_MOUSE_MOTION:
145         case LFUN_MOUSE_DOUBLE:
146         case LFUN_MOUSE_TRIPLE:
147                 // do not dispatch to InsetText
148                 cur.dispatched();
149                 break;
150
151         default:
152                 InsetText::doDispatch(cur, cmd);
153                 break;
154         }
155 }
156
157
158 void InsetInfo::setInfo(string const & name)
159 {
160         if (name.empty())
161                 return;
162         // info_type name
163         string type;
164         name_ = trim(split(name, type, ' '));
165         type_ = nameTranslator().find(type);
166 }
167
168
169 void InsetInfo::updateInfo()
170 {
171         InsetText::clear();
172
173         BufferParams const & bp = buffer().params();    
174
175         switch (type_) {
176         case UNKNOWN_INFO:
177                 setText(_("Unknown Info: ") + from_utf8(name_),
178                         bp.getFont(), false);
179                 break;
180         case SHORTCUT_INFO: {
181                 FuncRequest func = lyxaction.lookupFunc(name_);
182                 if (func.action != LFUN_UNKNOWN_ACTION)
183                         setText(theTopLevelKeymap().printBindings(func),
184                                 bp.getFont(), false);
185                 break;
186         }
187         case LYXRC_INFO: {
188                 ostringstream oss;
189                 lyxrc.write(oss, true, name_);
190                 string result = oss.str();
191                 // remove leading \\name
192                 result = result.substr(name_.size() + 2);
193                 // remove \n and ""
194                 result = rtrim(result, "\n");
195                 result = trim(result, "\"");
196                 setText(from_utf8(result), bp.getFont(), false);
197                 break;
198         }
199         case PACKAGE_INFO:
200                 // check in packages.lst
201                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
202                         bp.getFont(), false);
203                 break;
204         case TEXTCLASS_INFO: {
205                 // name_ is the class name
206                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
207                 bp.getFont(), false);
208                 break;
209         }
210         case MENU_INFO: {
211                 docstring_list names;
212                 FuncRequest func = lyxaction.lookupFunc(name_);
213                 if (func.action == LFUN_UNKNOWN_ACTION) {
214                         setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
215                         break;
216                 }
217                 // iterate through the menubackend to find it
218                 if (!theApp()->searchMenu(func, names)) {
219                         setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
220                                 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.back().length(); ++i)
233                                 info.insertChar(i, names.back()[i], bp.getFont(), false);
234                         names.pop_back();
235                 }
236                 break;
237         }
238         case BUFFER_INFO: {
239                 if (name_ == "name")
240                         setText(from_utf8(buffer().fileName().onlyFileName()),
241                                 bp.getFont(), false);
242                 else if (name_ == "path")
243                         setText(from_utf8(buffer().filePath()), bp.getFont(), false);
244                 else if (name_ == "class")
245                         setText(from_utf8(bp.documentClass().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