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