]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
61fb735f6f25b0e1c4ab21397816810c58abce4d
[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 "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "InsetGraphics.h"
20 #include "InsetSpecialChar.h"
21 #include "KeyMap.h"
22 #include "LaTeXFeatures.h"
23 #include "LayoutFile.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/ExceptionMessage.h"
36 #include "support/FileName.h"
37 #include "support/filetools.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include <sstream>
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47
48 namespace {
49
50 typedef Translator<InsetInfo::info_type, string> NameTranslator;
51
52 NameTranslator const initTranslator()
53 {       
54         NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
55
56         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
57         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
58         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
59         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
60         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
61         translator.addPair(InsetInfo::MENU_INFO, "menu");
62         translator.addPair(InsetInfo::ICON_INFO, "icon");
63         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
64
65         return translator;
66 }
67
68 /// The translator between the information type enum and corresponding string.
69 NameTranslator const & nameTranslator()
70 {
71         static NameTranslator const translator = initTranslator();
72         return translator;
73 }
74
75 } // namespace anon
76
77 /////////////////////////////////////////////////////////////////////////
78 //
79 // InsetInfo
80 //
81 /////////////////////////////////////////////////////////////////////////
82
83
84         
85 InsetInfo::InsetInfo(Buffer const & buf, string const & name) 
86         : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
87 {
88         setAutoBreakRows(true);
89         setInfo(name);
90         status_ = Collapsed;
91 }
92
93
94 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
95 {
96         cur.push(*this);
97         return InsetCollapsable::editXY(cur, x, y);
98 }
99
100
101 string InsetInfo::infoType() const
102 {
103         return nameTranslator().find(type_);
104 }
105
106
107 docstring InsetInfo::name() const 
108 {
109         return from_ascii("Info:" + infoType());
110 }
111
112
113 docstring InsetInfo::toolTip(BufferView const &, int, int) const
114 {
115         return bformat(_("Information regarding %1$s '%2$s'"),
116                         _(infoType()), from_utf8(name_));
117 }
118
119
120 void InsetInfo::read(Lexer & lex)
121 {
122         string token;
123         while (lex.isOK()) {
124                 lex.next();
125                 token = lex.getString();
126                 if (token == "type") {
127                         lex.next();
128                         token = lex.getString();
129                         type_ = nameTranslator().find(token);
130                 } else if (token == "arg") {
131                         lex.next(true);
132                         name_ = lex.getString();
133                 } else if (token == "\\end_inset")
134                         break;
135         }       
136         if (token != "\\end_inset") {
137                 lex.printError("Missing \\end_inset at this point");
138                 throw ExceptionMessage(WarningException,
139                         _("Missing \\end_inset at this point."),
140                         from_utf8(token));
141         }
142         setLayout(buffer().params());
143         updateInfo();
144 }
145
146
147 void InsetInfo::write(ostream & os) const
148 {
149         os << "Info\ntype  \"" << infoType() 
150            << "\"\narg   " << Lexer::quoteString(name_);
151 }
152
153
154 bool InsetInfo::validate(docstring const & arg) const
155 {
156         string type;
157         string const name = trim(split(to_utf8(arg), type, ' '));
158         switch (nameTranslator().find(type)) {
159         case UNKNOWN_INFO:
160                 return false;
161         case SHORTCUT_INFO:
162         case SHORTCUTS_INFO:
163         case MENU_INFO:
164         case ICON_INFO: {
165                 FuncRequest func = lyxaction.lookupFunc(name);
166                 return func.action != LFUN_UNKNOWN_ACTION;
167         }
168         case LYXRC_INFO: {
169                 ostringstream oss;
170                 lyxrc.write(oss, true, name);
171                 return !oss.str().empty();
172         }
173         case PACKAGE_INFO:
174         case TEXTCLASS_INFO:
175                 return true;
176         case BUFFER_INFO:
177                 return name == "name" || name == "path" || name == "class";
178         }
179         return false;
180 }
181
182
183 bool InsetInfo::showInsetDialog(BufferView * bv) const
184 {
185         bv->showDialog("info");
186         return true;
187 }
188
189
190 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
191                 FuncStatus & flag) const
192 {
193         switch (cmd.action) {
194         case LFUN_MOUSE_PRESS:
195         case LFUN_MOUSE_RELEASE:
196         case LFUN_MOUSE_MOTION:
197         case LFUN_MOUSE_DOUBLE:
198         case LFUN_MOUSE_TRIPLE:
199         case LFUN_COPY:
200                 return InsetCollapsable::getStatus(cur, cmd, flag);
201
202         case LFUN_INSET_DIALOG_UPDATE:
203         case LFUN_INSET_SETTINGS:
204                 flag.setEnabled(true);
205                 return true;
206
207         case LFUN_INSET_MODIFY:
208                 flag.setEnabled(true);
209                 break;
210
211         default:
212                 return false;
213         }
214         return true;
215 }
216
217
218 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
219 {
220         // allow selection, copy but not cut, delete etc
221         switch (cmd.action) {
222         case LFUN_MOUSE_PRESS:
223         case LFUN_MOUSE_RELEASE:
224         case LFUN_MOUSE_MOTION:
225         case LFUN_MOUSE_DOUBLE:
226         case LFUN_MOUSE_TRIPLE:
227         case LFUN_COPY:
228         case LFUN_INSET_SETTINGS:
229                 InsetCollapsable::doDispatch(cur, cmd);
230                 break;
231
232         case LFUN_INSET_MODIFY:
233                 setInfo(to_utf8(cmd.argument()));
234                 cur.pos() = 0;
235                 break;
236
237         default:
238                 break;
239         }
240 }
241
242
243 void InsetInfo::setInfo(string const & name)
244 {
245         if (name.empty())
246                 return;
247         // info_type name
248         string type;
249         name_ = trim(split(name, type, ' '));
250         type_ = nameTranslator().find(type);
251         setLayout(buffer().params());
252         updateInfo();
253 }
254
255
256 void InsetInfo::error(string const & err)
257 {
258         setText(bformat(_(err), from_utf8(name_)), 
259                 Font(inherit_font, buffer().params().language), false);
260 }
261
262
263 void InsetInfo::setText(docstring const & str)
264 {
265         setText(str, Font(inherit_font, buffer().params().language), false);
266 }
267
268
269 void InsetInfo::updateInfo()
270 {
271         BufferParams const & bp = buffer().params();    
272
273         switch (type_) {
274         case UNKNOWN_INFO:
275                 error("Unknown Info: %1$s");
276                 break;
277         case SHORTCUT_INFO:
278         case SHORTCUTS_INFO: {
279                 FuncRequest func = lyxaction.lookupFunc(name_);
280                 if (func.action == LFUN_UNKNOWN_ACTION) {
281                         error("Unknown action %1$s");
282                         break;
283                 }
284                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
285                 if (bindings.empty()) {
286                         // It is impropriate to use error() for undefined shortcut
287                         setText(_("undefined"));
288                         break;
289                 }
290                 if (type_ == SHORTCUT_INFO)
291                         setText(bindings.begin()->print(KeySequence::Portable));
292                 else
293                         setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
294                 break;
295         }
296         case LYXRC_INFO: {
297                 ostringstream oss;
298                 lyxrc.write(oss, true, name_);
299                 string result = oss.str();
300                 // remove leading \\name
301                 result = result.substr(name_.size() + 2);
302                 // remove \n and ""
303                 result = rtrim(result, "\n");
304                 result = trim(result, "\"");
305                 setText(from_utf8(result));
306                 break;
307         }
308         case PACKAGE_INFO:
309                 // check in packages.lst
310                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
311                 break;
312         case TEXTCLASS_INFO: {
313                 // name_ is the class name
314                 setText(LayoutFileList::get().haveClass(name_) ? _("yes") : _("no"));
315                 break;
316         }
317         case MENU_INFO: {
318                 docstring_list names;
319                 FuncRequest func = lyxaction.lookupFunc(name_);
320                 if (func.action == LFUN_UNKNOWN_ACTION) {
321                         error("Unknown action %1$s");
322                         break;
323                 }
324                 // iterate through the menubackend to find it
325                 if (!theApp()->searchMenu(func, names)) {
326                         error("No menu entry for action %1$s");
327                         break;
328                 }
329                 // if found, return its path.
330                 clear();
331                 Paragraph & par = paragraphs().front();
332                 Font const f(inherit_font, buffer().params().language);
333                 //Font fu = f;
334                 //fu.fontInfo().setUnderbar(FONT_ON);
335                 docstring_list::const_iterator beg = names.begin();
336                 docstring_list::const_iterator end = names.end();
337                 for (docstring_list::const_iterator it = beg ; 
338                      it != end ; ++it) {
339                         // do not insert > for the top level menu item
340                         if (it != beg)
341                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
342                                                 Change(Change::UNCHANGED));
343                         //FIXME: add proper underlines here. This
344                         // involves rewriting searchMenu used above to
345                         // return a vector of menus. If we do not do
346                         // that, we might as well use below
347                         // Paragraph::insert on each string (JMarc)
348                         for (size_type i = 0; i != it->length(); ++i)
349                                 par.insertChar(par.size(), (*it)[i], 
350                                                f, Change(Change::UNCHANGED));
351                 }
352                 break;
353         }
354         case ICON_INFO: {
355                 FuncRequest func = lyxaction.lookupFunc(name_);
356                 docstring icon_name = theApp()->iconName(func, true);
357                 //FIXME: We should use the icon directly instead of
358                 // going through FileName. The code below won't work
359                 // if the icon is embedded in the executable through
360                 // the Qt resource system.
361                 FileName file(to_utf8(icon_name));
362                 if (!file.exists())
363                         break;
364                 InsetGraphics * inset = new InsetGraphics(buffer());
365                 InsetGraphicsParams igp;
366                 igp.filename = file;
367                 inset->setParams(igp);
368                 clear();
369                 paragraphs().front().insertInset(0, inset, 
370                                                  Change(Change::UNCHANGED));
371                 break;
372         }
373         case BUFFER_INFO: {
374                 if (name_ == "name")
375                         setText(from_utf8(buffer().fileName().onlyFileName()));
376                 else if (name_ == "path")
377                         setText(from_utf8(buffer().filePath()));
378                 else if (name_ == "class")
379                         setText(from_utf8(bp.documentClass().name()));
380                 else
381                         setText(_("Unknown buffer info"));
382                 break;
383         }
384         }
385 }
386
387
388 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
389 {
390         return from_ascii("context-info");
391 }
392
393
394 } // namespace lyx