]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
InsetInfo: add InsetInfo (core)
[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 "BufferParams.h"
18 #include "BufferView.h"
19 #include "debug.h"
20 #include "FuncRequest.h"
21 #include "gettext.h"
22 #include "InsetSpecialChar.h"
23 #include "KeyMap.h"
24 #include "LaTeXFeatures.h"
25 #include "LyXAction.h"
26 #include "Lexer.h"
27 #include "MetricsInfo.h"
28 #include "ParagraphParameters.h"
29 #include "TextClassList.h"
30 #include "support/lstrings.h"
31 #include "support/ExceptionMessage.h"
32
33 using std::pair;
34 using std::string;
35 using std::ostream;
36 using std::ostringstream;
37 using std::stack;
38
39 namespace lyx {
40
41 using support::prefixIs;
42 using support::trim;
43 using support::split;
44 using support::rtrim;
45 using support::ExceptionMessage;
46 using support::WarningException;
47
48 InsetInfo::InsetInfo(BufferParams const & bp, string const & name) 
49         : InsetText(bp), bp_(bp), type_(UNKNOWN_INFO), name_(),
50           mouse_hover_(false)
51 {
52         setAutoBreakRows(true);
53         setDrawFrame(true);
54         setInfo(name);
55 }
56
57
58 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
59 {
60         return this;
61 }
62
63
64 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
65 {
66         InsetText::draw(pi, x, y); 
67         if (mouse_hover_) {
68                 odocstringstream os;
69                 os << _("Information regarding ")
70                    <<_(nameTranslator().find(type_))
71                    << _(" ") << from_utf8(name_);
72                 pi.base.bv->message(os.str());
73         }
74 }
75
76
77 namespace {
78
79 Translator<InsetInfo::info_type, string> const initTranslator()
80 {       
81         Translator<InsetInfo::info_type, string> translator(InsetInfo::UNKNOWN_INFO, "unknown");
82
83         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
84         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
85         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
86
87         return translator;
88 }
89
90 } // namespace anon
91
92 Translator<InsetInfo::info_type, std::string> const & InsetInfo::nameTranslator() const
93 {
94         static Translator<info_type, string> const translator =
95                 initTranslator();
96         return translator;
97 }
98
99         
100
101 void InsetInfo::read(Buffer const & buf, Lexer & lex)
102 {
103         string token;
104         while (lex.isOK()) {
105                 lex.next();
106                 token = lex.getString();
107                 if (token == "type") {
108                         lex.next();
109                         token = lex.getString();
110                         type_ = nameTranslator().find(token);
111                 } else if (token == "arg") {
112                         lex.next();
113                         name_ = lex.getString();
114                 } else if (token == "\\end_inset")
115                         break;
116         }       
117         if (token != "\\end_inset") {
118                 lex.printError("Missing \\end_inset at this point");
119                 throw ExceptionMessage(WarningException,
120                         _("Missing \\end_inset at this point."),
121                         from_utf8(token));
122         }
123         updateInfo();
124 }
125
126
127 void InsetInfo::write(Buffer const & buf, std::ostream & os) const
128 {
129         os << "Info\ntype  \""
130            << 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         updateInfo();
164 }
165
166
167 void InsetInfo::updateInfo()
168 {
169         InsetText::clear();
170
171         switch (type_) {
172         case UNKNOWN_INFO:
173                 setText(_("Unknown Info: ") + from_utf8(name_),
174                         bp_.getFont(), false);
175                 break;
176         case SHORTCUT_INFO: {
177                 FuncRequest func = lyxaction.lookupFunc(name_);
178                 if (func.action != LFUN_UNKNOWN_ACTION)
179                         setText(theTopLevelKeymap().printbindings(func),
180                                 bp_.getFont(), false);
181                 break;
182         }
183         case PACKAGE_INFO:
184                 // check in packages.lst
185                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
186                         bp_.getFont(), false);
187                 break;
188         case TEXTCLASS_INFO: {
189                 // name_ is the class name
190                 pair<bool, lyx::textclass_type> pp =
191                         textclasslist.numberOfClass(name_);
192                 setText(pp.first ? _("yes") : _("no"),
193                         bp_.getFont(), false);
194                 break;
195         }
196         }
197         // remove indent
198         paragraphs().begin()->params().noindent(true);
199 }
200
201
202 bool InsetInfo::setMouseHover(bool mouse_hover)
203 {
204         mouse_hover_ = mouse_hover;
205         return true;
206 }
207
208 } // namespace lyx