]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Fix bug http://bugzilla.lyx.org/show_bug.cgi?id=4910 by creating InsetText::addToToc().
[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         if (mouse_hover_) {
66                 odocstringstream os;
67                 os << _("Information regarding ")
68                    <<_(nameTranslator().find(type_))
69                    << " " << from_utf8(name_);
70                 pi.base.bv->message(os.str());
71         }
72 }
73
74
75 namespace {
76
77 Translator<InsetInfo::info_type, string> const initTranslator()
78 {       
79         Translator<InsetInfo::info_type, string> 
80                 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> 
95         const & InsetInfo::nameTranslator() const
96 {
97         static Translator<info_type, string> const translator = initTranslator();
98         return translator;
99 }
100
101         
102
103 void InsetInfo::read(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();
126 }
127
128
129 void InsetInfo::write(ostream & os) const
130 {
131         os << "Info\ntype  \"" << nameTranslator().find(type_)
132            << "\"\narg   \"" << name_ << '\"';
133 }
134
135
136 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
137 {
138         // FIXME: we should allow selection, copy etc...
139         switch (cmd.action) {
140         case LFUN_MOUSE_PRESS:
141         case LFUN_MOUSE_RELEASE:
142         case LFUN_MOUSE_MOTION:
143         case LFUN_MOUSE_DOUBLE:
144         case LFUN_MOUSE_TRIPLE:
145                 // do not dispatch to InsetText
146                 cur.dispatched();
147                 break;
148
149         default:
150                 InsetText::doDispatch(cur, cmd);
151                 break;
152         }
153 }
154
155
156 void InsetInfo::setInfo(string const & name)
157 {
158         if (name.empty())
159                 return;
160         // info_type name
161         string type;
162         name_ = trim(split(name, type, ' '));
163         type_ = nameTranslator().find(type);
164 }
165
166
167 void InsetInfo::updateInfo()
168 {
169         InsetText::clear();
170
171         BufferParams const & bp = buffer().params();    
172
173         switch (type_) {
174         case UNKNOWN_INFO:
175                 setText(_("Unknown Info: ") + from_utf8(name_),
176                         bp.getFont(), false);
177                 break;
178         case SHORTCUT_INFO: {
179                 FuncRequest func = lyxaction.lookupFunc(name_);
180                 if (func.action != LFUN_UNKNOWN_ACTION)
181                         setText(theTopLevelKeymap().printBindings(func),
182                                 bp.getFont(), false);
183                 break;
184         }
185         case LYXRC_INFO: {
186                 ostringstream oss;
187                 lyxrc.write(oss, true, name_);
188                 string result = oss.str();
189                 // remove leading \\name
190                 result = result.substr(name_.size() + 2);
191                 // remove \n and ""
192                 result = rtrim(result, "\n");
193                 result = trim(result, "\"");
194                 setText(from_utf8(result), bp.getFont(), false);
195                 break;
196         }
197         case PACKAGE_INFO:
198                 // check in packages.lst
199                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
200                         bp.getFont(), false);
201                 break;
202         case TEXTCLASS_INFO: {
203                 // name_ is the class name
204                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
205                 bp.getFont(), false);
206                 break;
207         }
208         case MENU_INFO: {
209                 docstring_list names;
210                 FuncRequest func = lyxaction.lookupFunc(name_);
211                 if (func.action == LFUN_UNKNOWN_ACTION) {
212                         setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
213                         break;
214                 }
215                 // iterate through the menubackend to find it
216                 if (!theApp()->searchMenu(func, names)) {
217                         setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
218                                 bp.getFont(), false);
219                         break;
220                 }
221                 // if find, return its path.
222                 InsetText::clear();
223                 Paragraph & info = paragraphs().front();
224                 unsigned int i = 0;
225                 while (!names.empty()) {
226                         // do not insert > for the top level menu item
227                         if (i != 0)
228                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
229                                         Change(Change::UNCHANGED));
230                         for (i = 0; i != names.back().length(); ++i)
231                                 info.insertChar(i, names.back()[i], bp.getFont(), false);
232                         names.pop_back();
233                 }
234                 break;
235         }
236         case BUFFER_INFO: {
237                 if (name_ == "name")
238                         setText(from_utf8(buffer().fileName().onlyFileName()),
239                                 bp.getFont(), false);
240                 else if (name_ == "path")
241                         setText(from_utf8(buffer().filePath()), bp.getFont(), false);
242                 else if (name_ == "class")
243                         setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
244                 else
245                         setText(_("Unknown buffer info"), bp.getFont(), false);
246                 break;
247         }
248         }
249         // remove indent
250         paragraphs().begin()->params().noindent(true);
251 }
252
253
254 bool InsetInfo::setMouseHover(bool mouse_hover)
255 {
256         mouse_hover_ = mouse_hover;
257         return true;
258 }
259
260 } // namespace lyx