]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Final touch on minimalistic branches
[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 "Color.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         text_.current_font.setLanguage(latex_language);
57         text_.real_current_font.setLanguage(latex_language);
58 }
59
60
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62         : InsetCollapsable(bp, status)
63 {
64         setLayout(bp);
65         init();
66 }
67
68
69 InsetERT::InsetERT(InsetERT const & in)
70         : InsetCollapsable(in)
71 {
72         init();
73 }
74
75
76 Inset * InsetERT::clone() const
77 {
78         return new InsetERT(*this);
79 }
80
81
82 #if 0
83 InsetERT::InsetERT(BufferParams const & bp,
84                    Language const *, string const & contents, CollapseStatus status)
85         : InsetCollapsable(bp, status)
86 {
87         Font font(Font::ALL_INHERIT, latex_language);
88         paragraphs().begin()->insert(0, contents, font);
89
90         // the init has to be after the initialization of the paragraph
91         // because of the label settings (draw_label for ert insets).
92         init();
93 }
94 #endif
95
96
97 InsetERT::~InsetERT()
98 {
99         InsetERTMailer(*this).hideDialog();
100 }
101
102
103 void InsetERT::write(Buffer const & buf, ostream & os) const
104 {
105         os << "ERT" << "\n";
106         InsetCollapsable::write(buf, os);
107 }
108
109
110 void InsetERT::read(Buffer const & buf, Lexer & lex)
111 {
112         InsetCollapsable::read(buf, lex);
113
114         // Force default font
115         // This avoids paragraphs in buffer language that would have a
116         // foreign language after a document langauge change, and it ensures
117         // that all new text in ERT gets the "latex" language, since new text
118         // inherits the language from the last position of the existing text.
119         // As a side effect this makes us also robust against bugs in LyX
120         // that might lead to font changes in ERT in .lyx files.
121         Font font(Font::ALL_INHERIT, latex_language);
122         ParagraphList::iterator par = paragraphs().begin();
123         ParagraphList::iterator const end = paragraphs().end();
124         while (par != end) {
125                 pos_type siz = par->size();
126                 for (pos_type i = 0; i <= siz; ++i) {
127                         par->setFont(i, font);
128                 }
129                 ++par;
130         }
131 }
132
133
134 docstring const InsetERT::editMessage() const
135 {
136         return _("Opened ERT Inset");
137 }
138
139
140 int InsetERT::latex(Buffer const &, odocstream & os,
141                     OutputParams const &) const
142 {
143         ParagraphList::const_iterator par = paragraphs().begin();
144         ParagraphList::const_iterator end = paragraphs().end();
145
146         int lines = 0;
147         while (par != end) {
148                 pos_type siz = par->size();
149                 for (pos_type i = 0; i < siz; ++i) {
150                         // ignore all struck out text
151                         if (par->isDeleted(i))
152                                 continue;
153
154                         os.put(par->getChar(i));
155                 }
156                 ++par;
157                 if (par != end) {
158                         os << "\n";
159                         ++lines;
160                 }
161         }
162
163         return lines;
164 }
165
166
167 int InsetERT::plaintext(Buffer const &, odocstream &,
168                         OutputParams const &) const
169 {
170         return 0; // do not output TeX code
171 }
172
173
174 int InsetERT::docbook(Buffer const &, odocstream & os,
175                       OutputParams const &) const
176 {
177         ParagraphList::const_iterator par = paragraphs().begin();
178         ParagraphList::const_iterator end = paragraphs().end();
179
180         int lines = 0;
181         while (par != end) {
182                 pos_type siz = par->size();
183                 for (pos_type i = 0; i < siz; ++i)
184                         os.put(par->getChar(i));
185                 ++par;
186                 if (par != end) {
187                         os << "\n";
188                         ++lines;
189                 }
190         }
191
192         return lines;
193 }
194
195
196 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
197 {
198         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
199         switch (cmd.action) {
200
201         case LFUN_MOUSE_PRESS:
202                 if (cmd.button() != mouse_button::button3)
203                         InsetCollapsable::doDispatch(cur, cmd);
204                 else
205                         // This makes the cursor leave the
206                         // inset when it collapses on mouse-3
207                         cur.undispatched();
208                 break;
209
210         case LFUN_QUOTE_INSERT: {
211                 // We need to bypass the fancy quotes in Text
212                 FuncRequest f(LFUN_SELF_INSERT, "\"");
213                 dispatch(cur, f);
214                 break;
215         }
216         case LFUN_INSET_MODIFY: {
217                 InsetCollapsable::CollapseStatus st;
218                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
219                 setStatus(cur, st);
220                 break;
221         }
222         case LFUN_PASTE:
223         case LFUN_CLIPBOARD_PASTE:
224         case LFUN_PRIMARY_SELECTION_PASTE: {
225                 InsetCollapsable::doDispatch(cur, cmd);
226
227                 // Since we can only store plain text, we must reset all
228                 // attributes.
229                 // FIXME: Change only the pasted paragraphs
230
231                 BufferParams const & bp = cur.buffer().params();
232                 LayoutPtr const layout =
233                         bp.getTextClass().defaultLayout();
234                 Font font = layout->font;
235                 // ERT contents has always latex_language
236                 font.setLanguage(latex_language);
237                 ParagraphList::iterator const end = paragraphs().end();
238                 for (ParagraphList::iterator par = paragraphs().begin();
239                      par != end; ++par) {
240                         // in case par had a manual label
241                         par->setBeginOfBody();
242                         pos_type const siz = par->size();
243                         for (pos_type i = 0; i < siz; ++i) {
244                                 par->setFont(i, font);
245                         }
246                         par->params().clear();
247                 }
248                 break;
249         }
250         default:
251                 // Force any new text to latex_language
252                 // FIXME: This should only be necessary in init(), but
253                 // new paragraphs that are created by pressing enter at the
254                 // start of an existing paragraph get the buffer language
255                 // and not latex_language, so we take this brute force
256                 // approach.
257                 text_.current_font.setLanguage(latex_language);
258                 text_.real_current_font.setLanguage(latex_language);
259
260                 InsetCollapsable::doDispatch(cur, cmd);
261                 break;
262         }
263 }
264
265
266 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
267         FuncStatus & status) const
268 {
269         switch (cmd.action) {
270                 // suppress these
271                 case LFUN_ACCENT_ACUTE:
272                 case LFUN_ACCENT_BREVE:
273                 case LFUN_ACCENT_CARON:
274                 case LFUN_ACCENT_CEDILLA:
275                 case LFUN_ACCENT_CIRCLE:
276                 case LFUN_ACCENT_CIRCUMFLEX:
277                 case LFUN_ACCENT_DOT:
278                 case LFUN_ACCENT_GRAVE:
279                 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
280                 case LFUN_ACCENT_MACRON:
281                 case LFUN_ACCENT_OGONEK:
282                 case LFUN_ACCENT_SPECIAL_CARON:
283                 case LFUN_ACCENT_TIE:
284                 case LFUN_ACCENT_TILDE:
285                 case LFUN_ACCENT_UMLAUT:
286                 case LFUN_ACCENT_UNDERBAR:
287                 case LFUN_ACCENT_UNDERDOT:
288                 case LFUN_APPENDIX:
289                 case LFUN_BREAK_LINE:
290                 case LFUN_CAPTION_INSERT:
291                 case LFUN_DEPTH_DECREMENT:
292                 case LFUN_DEPTH_INCREMENT:
293                 case LFUN_DOTS_INSERT:
294                 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
295                 case LFUN_ENVIRONMENT_INSERT:
296                 case LFUN_ERT_INSERT:
297                 case LFUN_FILE_INSERT:
298                 case LFUN_FLOAT_INSERT:
299                 case LFUN_FLOAT_WIDE_INSERT:
300                 case LFUN_WRAP_INSERT:
301                 case LFUN_FONT_BOLD:
302                 case LFUN_FONT_CODE:
303                 case LFUN_FONT_DEFAULT:
304                 case LFUN_FONT_EMPH:
305                 case LFUN_FONT_FREE_APPLY:
306                 case LFUN_FONT_FREE_UPDATE:
307                 case LFUN_FONT_NOUN:
308                 case LFUN_FONT_ROMAN:
309                 case LFUN_FONT_SANS:
310                 case LFUN_FONT_FRAK:
311                 case LFUN_FONT_ITAL:
312                 case LFUN_FONT_SIZE:
313                 case LFUN_FONT_STATE:
314                 case LFUN_FONT_UNDERLINE:
315                 case LFUN_FOOTNOTE_INSERT:
316                 case LFUN_HFILL_INSERT:
317                 case LFUN_HTML_INSERT:
318                 case LFUN_HYPHENATION_POINT_INSERT:
319                 case LFUN_LIGATURE_BREAK_INSERT:
320                 case LFUN_INDEX_INSERT:
321                 case LFUN_INDEX_PRINT:
322                 case LFUN_LABEL_INSERT:
323                 case LFUN_OPTIONAL_INSERT:
324                 case LFUN_BIBITEM_INSERT:
325                 case LFUN_LINE_INSERT:
326                 case LFUN_PAGEBREAK_INSERT:
327                 case LFUN_CLEARPAGE_INSERT:
328                 case LFUN_CLEARDOUBLEPAGE_INSERT:
329                 case LFUN_LANGUAGE:
330                 case LFUN_LAYOUT:
331                 case LFUN_LAYOUT_PARAGRAPH:
332                 case LFUN_LAYOUT_TABULAR:
333                 case LFUN_MARGINALNOTE_INSERT:
334                 case LFUN_MATH_DISPLAY:
335                 case LFUN_MATH_INSERT:
336                 case LFUN_MATH_MATRIX:
337                 case LFUN_MATH_MODE:
338                 case LFUN_MENU_OPEN:
339                 case LFUN_MENU_SEPARATOR_INSERT:
340                 case LFUN_BRANCH_INSERT:
341                 case LFUN_CHARSTYLE_INSERT:
342                 case LFUN_NOTE_INSERT:
343                 case LFUN_BOX_INSERT:
344                 case LFUN_NOTE_NEXT:
345                 case LFUN_PARAGRAPH_SPACING:
346                 case LFUN_LABEL_GOTO:
347                 case LFUN_REFERENCE_NEXT:
348                 case LFUN_SPACE_INSERT:
349                 case LFUN_SERVER_GOTO_FILE_ROW:
350                 case LFUN_SERVER_NOTIFY:
351                 case LFUN_SERVER_SET_XY:
352                 case LFUN_TABULAR_INSERT:
353                 case LFUN_TOC_INSERT:
354                 case LFUN_URL_INSERT:
355                 case LFUN_FLOAT_LIST:
356                 case LFUN_INSET_INSERT:
357                 case LFUN_PARAGRAPH_PARAMS:
358                 case LFUN_PARAGRAPH_PARAMS_APPLY:
359                 case LFUN_PARAGRAPH_UPDATE:
360                 case LFUN_NOMENCL_INSERT:
361                 case LFUN_NOMENCL_PRINT:
362                 case LFUN_NOACTION:
363                         status.enabled(false);
364                         return true;
365
366                 case LFUN_QUOTE_INSERT:
367                 case LFUN_INSET_MODIFY:
368                 case LFUN_PASTE:
369                 case LFUN_CLIPBOARD_PASTE:
370                 case LFUN_PRIMARY_SELECTION_PASTE:
371                         status.enabled(true);
372                         return true;
373
374                 // this one is difficult to get right. As a half-baked
375                 // solution, we consider only the first action of the sequence
376                 case LFUN_COMMAND_SEQUENCE: {
377                         // argument contains ';'-terminated commands
378                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
379                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
380                         func.origin = cmd.origin;
381                         return getStatus(cur, func, status);
382                 }
383
384                 default:
385                         return InsetCollapsable::getStatus(cur, cmd, status);
386         }
387 }
388
389
390 void InsetERT::setButtonLabel()
391 {
392         // FIXME UNICODE
393         if (decoration() == Classic)
394                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
395         else
396                 setLabel(getNewLabel(_("ERT")));
397 }
398
399
400 bool InsetERT::insetAllowed(Inset::Code /* code */) const
401 {
402         return false;
403 }
404
405
406 bool InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
407 {
408         Font tmpfont = mi.base.font;
409         getDrawFont(mi.base.font);
410         mi.base.font.realize(tmpfont);
411         InsetCollapsable::metrics(mi, dim);
412         mi.base.font = tmpfont;
413         bool const changed = dim_ != dim;
414         dim_ = dim;
415         return changed;
416 }
417
418
419 void InsetERT::draw(PainterInfo & pi, int x, int y) const
420 {
421         Font tmpfont = pi.base.font;
422         getDrawFont(pi.base.font);
423         pi.base.font.realize(tmpfont);
424         const_cast<InsetERT &>(*this).setButtonLabel();
425         InsetCollapsable::draw(pi, x, y);
426         pi.base.font = tmpfont;
427 }
428
429
430 bool InsetERT::showInsetDialog(BufferView * bv) const
431 {
432         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
433         return true;
434 }
435
436
437 void InsetERT::getDrawFont(Font & font) const
438 {
439         font = Font(Font::ALL_INHERIT, latex_language);
440         font.realize(layout_.font);
441 }
442
443
444 string const InsetERTMailer::name_("ert");
445
446 InsetERTMailer::InsetERTMailer(InsetERT & inset)
447         : inset_(inset)
448 {}
449
450
451 string const InsetERTMailer::inset2string(Buffer const &) const
452 {
453         return params2string(inset_.status());
454 }
455
456
457 void InsetERTMailer::string2params(string const & in,
458                                    InsetCollapsable::CollapseStatus & status)
459 {
460         status = InsetCollapsable::Collapsed;
461         if (in.empty())
462                 return;
463
464         istringstream data(in);
465         Lexer lex(0,0);
466         lex.setStream(data);
467
468         string name;
469         lex >> name;
470         if (name != name_)
471                 return print_mailer_error("InsetERTMailer", in, 1, name_);
472
473         int s;
474         lex >> s;
475         if (lex)
476                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
477 }
478
479
480 string const
481 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
482 {
483         ostringstream data;
484         data << name_ << ' ' << status;
485         return data.str();
486 }
487
488
489 } // namespace lyx