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