]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
98b3a6167b727abfbaba15f2c512da3e6b840c04
[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 "FuncStatus.h"
20 #include "InsetGraphics.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 "MetricsInfo.h"
28 #include "ParagraphParameters.h"
29
30 #include "frontends/Application.h"
31
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/docstring_list.h"
35 #include "support/FileName.h"
36 #include "support/filetools.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39 #include "support/ExceptionMessage.h"
40
41 #include <sstream>
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47
48
49 InsetInfo::InsetInfo(Buffer const & buf, string const & name) 
50         : InsetText(buf), type_(UNKNOWN_INFO), name_(),
51           mouse_hover_(false)
52 {
53         setAutoBreakRows(true);
54         setDrawFrame(true);
55         setInfo(name);
56 }
57
58
59 Inset * InsetInfo::editXY(Cursor &, int, int)
60 {
61         return this;
62 }
63
64
65 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
66 {
67         InsetText::draw(pi, x, y); 
68 }
69
70
71 docstring InsetInfo::toolTip(BufferView const &, int, int) const
72 {
73         odocstringstream os;
74         os << _("Information regarding ")
75            << _(nameTranslator().find(type_))
76            << " " << from_utf8(name_);
77         return os.str();
78 }
79
80 namespace {
81
82 Translator<InsetInfo::info_type, string> const initTranslator()
83 {       
84         Translator<InsetInfo::info_type, string> 
85                 translator(InsetInfo::UNKNOWN_INFO, "unknown");
86
87         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
88         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
89         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
90         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
91         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
92         translator.addPair(InsetInfo::MENU_INFO, "menu");
93         translator.addPair(InsetInfo::ICON_INFO, "icon");
94         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
95
96         return translator;
97 }
98
99 } // namespace anon
100
101 Translator<InsetInfo::info_type, string> 
102         const & InsetInfo::nameTranslator() const
103 {
104         static Translator<info_type, string> const translator = initTranslator();
105         return translator;
106 }
107
108         
109
110 void InsetInfo::read(Lexer & lex)
111 {
112         string token;
113         while (lex.isOK()) {
114                 lex.next();
115                 token = lex.getString();
116                 if (token == "type") {
117                         lex.next();
118                         token = lex.getString();
119                         type_ = nameTranslator().find(token);
120                 } else if (token == "arg") {
121                         lex.next();
122                         name_ = lex.getString();
123                 } else if (token == "\\end_inset")
124                         break;
125         }       
126         if (token != "\\end_inset") {
127                 lex.printError("Missing \\end_inset at this point");
128                 throw ExceptionMessage(WarningException,
129                         _("Missing \\end_inset at this point."),
130                         from_utf8(token));
131         }
132         updateInfo();
133 }
134
135
136 void InsetInfo::write(ostream & os) const
137 {
138         os << "Info\ntype  \"" << nameTranslator().find(type_)
139            << "\"\narg   \"" << name_ << '\"';
140 }
141
142
143 bool InsetInfo::showInsetDialog(BufferView * bv) const
144 {
145         bv->showDialog("info");
146         return true;
147 }
148
149
150 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
151                 FuncStatus & flag) const
152 {
153         switch (cmd.action) {
154
155         case LFUN_INSET_MODIFY:
156                 flag.setEnabled(true);
157                 break;
158         //FIXME: do something.
159         /*
160         */
161
162         default:
163                 return false;
164         }
165         return true;
166 }
167
168
169 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
170 {
171         // FIXME: we should allow selection, copy etc...
172         switch (cmd.action) {
173         case LFUN_MOUSE_PRESS:
174         case LFUN_MOUSE_RELEASE:
175         case LFUN_MOUSE_MOTION:
176         case LFUN_MOUSE_DOUBLE:
177         case LFUN_MOUSE_TRIPLE:
178                 // do not dispatch to InsetText
179                 cur.dispatched();
180                 break;
181
182         default:
183                 InsetText::doDispatch(cur, cmd);
184                 break;
185         }
186 }
187
188
189 void InsetInfo::setInfo(string const & name)
190 {
191         if (name.empty())
192                 return;
193         // info_type name
194         string type;
195         name_ = trim(split(name, type, ' '));
196         type_ = nameTranslator().find(type);
197 }
198
199
200 void InsetInfo::updateInfo()
201 {
202         InsetText::clear();
203
204         BufferParams const & bp = buffer().params();    
205
206         switch (type_) {
207         case UNKNOWN_INFO:
208                 setText(_("Unknown Info: ") + from_utf8(name_),
209                         bp.getFont(), false);
210                 break;
211         case SHORTCUTS_INFO: {
212                 FuncRequest func = lyxaction.lookupFunc(name_);
213                 if (func.action != LFUN_UNKNOWN_ACTION)
214                         setText(theTopLevelKeymap().printBindings(func),
215                                 bp.getFont(), false);
216                 break;
217         }
218         case SHORTCUT_INFO: {
219                 FuncRequest func = lyxaction.lookupFunc(name_);
220                 if (func.action != LFUN_UNKNOWN_ACTION) {
221                         KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
222                         if (!bindings.empty())
223                                 setText(bindings.rbegin()->print(KeySequence::ForGui),
224                                         bp.getFont(), false);
225                 }
226                 break;
227         }
228         case LYXRC_INFO: {
229                 ostringstream oss;
230                 lyxrc.write(oss, true, name_);
231                 string result = oss.str();
232                 // remove leading \\name
233                 result = result.substr(name_.size() + 2);
234                 // remove \n and ""
235                 result = rtrim(result, "\n");
236                 result = trim(result, "\"");
237                 setText(from_utf8(result), bp.getFont(), false);
238                 break;
239         }
240         case PACKAGE_INFO:
241                 // check in packages.lst
242                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"),
243                         bp.getFont(), false);
244                 break;
245         case TEXTCLASS_INFO: {
246                 // name_ is the class name
247                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"),
248                 bp.getFont(), false);
249                 break;
250         }
251         case MENU_INFO: {
252                 docstring_list names;
253                 FuncRequest func = lyxaction.lookupFunc(name_);
254                 if (func.action == LFUN_UNKNOWN_ACTION) {
255                         setText(bformat(_("Unknown action %1$s"), from_utf8(name_)), bp.getFont(), false);
256                         break;
257                 }
258                 // iterate through the menubackend to find it
259                 if (!theApp()->searchMenu(func, names)) {
260                         setText(bformat(_("No menu entry for action %1$s"), from_utf8(name_)),
261                                 bp.getFont(), false);
262                         break;
263                 }
264                 // if find, return its path.
265                 InsetText::clear();
266                 Paragraph & info = paragraphs().front();
267                 unsigned int i = 0;
268                 while (!names.empty()) {
269                         // do not insert > for the top level menu item
270                         if (i != 0)
271                                 info.insertInset(0, new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
272                                         Change(Change::UNCHANGED));
273                         for (i = 0; i != names.back().length(); ++i)
274                                 info.insertChar(i, names.back()[i], bp.getFont(), false);
275                         names.pop_back();
276                 }
277                 break;
278         }
279         case ICON_INFO: {
280                 FuncRequest func = lyxaction.lookupFunc(name_);
281                 docstring icon_name = theApp()->iconName(func, true);
282                 //FIXME: We should use the icon directly instead of going through
283                 // FileName. The code below won't work if the icon is embedded in the
284                 // executable through the Qt resource system.
285                 FileName file(to_utf8(icon_name));
286                 if (!file.exists())
287                         break;
288                 InsetGraphicsParams igp;
289                 igp.filename = file;
290                 InsetGraphics * inset = new InsetGraphics(buffer());
291                 inset->setParams(igp);
292                 paragraphs().front().insertInset(0, inset, Change(Change::UNCHANGED));
293                 break;
294         }
295         case BUFFER_INFO: {
296                 if (name_ == "name")
297                         setText(from_utf8(buffer().fileName().onlyFileName()),
298                                 bp.getFont(), false);
299                 else if (name_ == "path")
300                         setText(from_utf8(buffer().filePath()), bp.getFont(), false);
301                 else if (name_ == "class")
302                         setText(from_utf8(bp.documentClass().name()), bp.getFont(), false);
303                 else
304                         setText(_("Unknown buffer info"), bp.getFont(), false);
305                 break;
306         }
307         }
308         // remove indent
309         paragraphs().begin()->params().noindent(true);
310 }
311
312
313 bool InsetInfo::setMouseHover(bool mouse_hover)
314 {
315         mouse_hover_ = mouse_hover;
316         return true;
317 }
318
319
320 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
321 {
322         return from_ascii("context-info");
323 }
324
325 } // namespace lyx