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