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