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