]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Move disabling LFUNS from ERT to Collapsable
[features.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "Language.h"
26 #include "Layout.h"
27 #include "Color.h"
28 #include "LyXAction.h"
29 #include "Lexer.h"
30 #include "TextClass.h"
31 #include "MetricsInfo.h"
32 #include "ParagraphParameters.h"
33 #include "Paragraph.h"
34
35 #include "frontends/alert.h"
36
37 #include <sstream>
38
39
40 namespace lyx {
41
42 using support::token;
43
44 using std::endl;
45 using std::min;
46
47 using std::istringstream;
48 using std::ostream;
49 using std::ostringstream;
50 using std::string;
51
52
53 void InsetERT::init()
54 {
55         setButtonLabel();
56         setLabelFont(layout_.labelfont);
57         // FIXME: what to do with those?
58         //text_.current_font.setLanguage(latex_language);
59         //text_.real_current_font.setLanguage(latex_language);
60 }
61
62
63 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
64         : InsetCollapsable(bp, status)
65 {
66         setLayout(bp);
67         init();
68 }
69
70
71 InsetERT::InsetERT(InsetERT const & in)
72         : InsetCollapsable(in)
73 {
74         init();
75 }
76
77
78 Inset * InsetERT::clone() const
79 {
80         return new InsetERT(*this);
81 }
82
83
84 #if 0
85 InsetERT::InsetERT(BufferParams const & bp,
86                    Language const *, string const & contents, CollapseStatus status)
87         : InsetCollapsable(bp, status)
88 {
89         Font font(Font::ALL_INHERIT, latex_language);
90         paragraphs().begin()->insert(0, contents, font);
91
92         // the init has to be after the initialization of the paragraph
93         // because of the label settings (draw_label for ert insets).
94         init();
95 }
96 #endif
97
98
99 InsetERT::~InsetERT()
100 {
101         InsetERTMailer(*this).hideDialog();
102 }
103
104
105 void InsetERT::write(Buffer const & buf, ostream & os) const
106 {
107         os << "ERT" << "\n";
108         InsetCollapsable::write(buf, os);
109 }
110
111
112 void InsetERT::read(Buffer const & buf, Lexer & lex)
113 {
114         InsetCollapsable::read(buf, lex);
115
116         // Force default font
117         // This avoids paragraphs in buffer language that would have a
118         // foreign language after a document langauge change, and it ensures
119         // that all new text in ERT gets the "latex" language, since new text
120         // inherits the language from the last position of the existing text.
121         // As a side effect this makes us also robust against bugs in LyX
122         // that might lead to font changes in ERT in .lyx files.
123         Font font(Font::ALL_INHERIT, latex_language);
124         ParagraphList::iterator par = paragraphs().begin();
125         ParagraphList::iterator const end = paragraphs().end();
126         while (par != end) {
127                 pos_type siz = par->size();
128                 for (pos_type i = 0; i <= siz; ++i) {
129                         par->setFont(i, font);
130                 }
131                 ++par;
132         }
133 }
134
135
136 docstring const InsetERT::editMessage() const
137 {
138         return _("Opened ERT Inset");
139 }
140
141
142 int InsetERT::latex(Buffer const & buf, odocstream & os,
143                     OutputParams const & op) const
144 {
145         return InsetCollapsable::latex(buf, os, op);
146 }
147
148
149 int InsetERT::plaintext(Buffer const &, odocstream &,
150                         OutputParams const &) const
151 {
152         return 0; // do not output TeX code
153 }
154
155
156 int InsetERT::docbook(Buffer const &, odocstream & os,
157                       OutputParams const &) const
158 {
159         // FIXME can we do the same thing here as for LaTeX?
160         ParagraphList::const_iterator par = paragraphs().begin();
161         ParagraphList::const_iterator end = paragraphs().end();
162
163         int lines = 0;
164         while (par != end) {
165                 pos_type siz = par->size();
166                 for (pos_type i = 0; i < siz; ++i)
167                         os.put(par->getChar(i));
168                 ++par;
169                 if (par != end) {
170                         os << "\n";
171                         ++lines;
172                 }
173         }
174
175         return lines;
176 }
177
178
179 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
180 {
181         BufferParams const & bp = cur.buffer().params();
182         LayoutPtr const layout =
183                         bp.getTextClass().defaultLayout();
184         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
185         switch (cmd.action) {
186
187         case LFUN_MOUSE_PRESS:
188                 if (cmd.button() != mouse_button::button3)
189                         InsetCollapsable::doDispatch(cur, cmd);
190                 else
191                         // This makes the cursor leave the
192                         // inset when it collapses on mouse-3
193                         cur.undispatched();
194                 break;
195
196         case LFUN_QUOTE_INSERT: {
197                 // We need to bypass the fancy quotes in Text
198                 FuncRequest f(LFUN_SELF_INSERT, "\"");
199                 dispatch(cur, f);
200                 break;
201         }
202         case LFUN_INSET_MODIFY: {
203                 InsetCollapsable::CollapseStatus st;
204                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
205                 setStatus(cur, st);
206                 break;
207         }
208         case LFUN_PASTE:
209         case LFUN_CLIPBOARD_PASTE:
210         case LFUN_PRIMARY_SELECTION_PASTE: {
211                 InsetCollapsable::doDispatch(cur, cmd);
212
213                 // Since we can only store plain text, we must reset all
214                 // attributes.
215                 // FIXME: Change only the pasted paragraphs
216
217                 Font font = layout->font;
218                 // ERT contents has always latex_language
219                 font.setLanguage(latex_language);
220                 ParagraphList::iterator const end = paragraphs().end();
221                 for (ParagraphList::iterator par = paragraphs().begin();
222                      par != end; ++par) {
223                         // in case par had a manual label
224                         par->setBeginOfBody();
225                         pos_type const siz = par->size();
226                         for (pos_type i = 0; i < siz; ++i) {
227                                 par->setFont(i, font);
228                         }
229                         par->params().clear();
230                 }
231                 break;
232         }
233         default:
234                 // Force any new text to latex_language
235                 // FIXME: This should only be necessary in init(), but
236                 // new paragraphs that are created by pressing enter at the
237                 // start of an existing paragraph get the buffer language
238                 // and not latex_language, so we take this brute force
239                 // approach.
240                 cur.current_font = layout->font;
241                 cur.real_current_font = layout->font;
242                 cur.current_font.setLanguage(latex_language);
243                 cur.real_current_font.setLanguage(latex_language);
244                 InsetCollapsable::doDispatch(cur, cmd);
245                 break;
246         }
247 }
248
249
250 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
251         FuncStatus & status) const
252 {
253         switch (cmd.action) {
254                 case LFUN_CLIPBOARD_PASTE:
255                 case LFUN_INSET_MODIFY:
256                 case LFUN_PASTE:
257                 case LFUN_PRIMARY_SELECTION_PASTE:
258                 case LFUN_QUOTE_INSERT:
259                         status.enabled(true);
260                         return true;
261
262                 // this one is difficult to get right. As a half-baked
263                 // solution, we consider only the first action of the sequence
264                 case LFUN_COMMAND_SEQUENCE: {
265                         // argument contains ';'-terminated commands
266                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
267                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
268                         func.origin = cmd.origin;
269                         return getStatus(cur, func, status);
270                 }
271
272                 default:
273                         return InsetCollapsable::getStatus(cur, cmd, status);
274         }
275 }
276
277
278 void InsetERT::setButtonLabel()
279 {
280         // FIXME UNICODE
281         if (decoration() == Classic)
282                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
283         else
284                 setLabel(getNewLabel(_("ERT")));
285 }
286
287
288 bool InsetERT::insetAllowed(InsetCode /* code */) const
289 {
290         return false;
291 }
292
293
294 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
295 {
296         Font tmpfont = mi.base.font;
297         getDrawFont(mi.base.font);
298         mi.base.font.realize(tmpfont);
299         InsetCollapsable::metrics(mi, dim);
300         mi.base.font = tmpfont;
301 }
302
303
304 void InsetERT::draw(PainterInfo & pi, int x, int y) const
305 {
306         Font tmpfont = pi.base.font;
307         getDrawFont(pi.base.font);
308         pi.base.font.realize(tmpfont);
309         const_cast<InsetERT &>(*this).setButtonLabel();
310         InsetCollapsable::draw(pi, x, y);
311         pi.base.font = tmpfont;
312 }
313
314
315 bool InsetERT::showInsetDialog(BufferView * bv) const
316 {
317         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
318         return true;
319 }
320
321
322 void InsetERT::getDrawFont(Font & font) const
323 {
324         font = Font(Font::ALL_INHERIT, latex_language);
325         font.realize(layout_.font);
326 }
327
328
329 string const InsetERTMailer::name_("ert");
330
331 InsetERTMailer::InsetERTMailer(InsetERT & inset)
332         : inset_(inset)
333 {}
334
335
336 string const InsetERTMailer::inset2string(Buffer const &) const
337 {
338         return params2string(inset_.status());
339 }
340
341
342 void InsetERTMailer::string2params(string const & in,
343                                    InsetCollapsable::CollapseStatus & status)
344 {
345         status = InsetCollapsable::Collapsed;
346         if (in.empty())
347                 return;
348
349         istringstream data(in);
350         Lexer lex(0,0);
351         lex.setStream(data);
352
353         string name;
354         lex >> name;
355         if (name != name_)
356                 return print_mailer_error("InsetERTMailer", in, 1, name_);
357
358         int s;
359         lex >> s;
360         if (lex)
361                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
362 }
363
364
365 string const
366 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
367 {
368         ostringstream data;
369         data << name_ << ' ' << status;
370         return data.str();
371 }
372
373
374 } // namespace lyx