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