]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
4a332d683720469fe1480f78f411bddc5508cf6a
[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 "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         // 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::write(Buffer const & buf, ostream & os) const
105 {
106         os << "ERT" << "\n";
107         InsetCollapsable::write(buf, os);
108 }
109
110
111 void InsetERT::read(Buffer const & buf, Lexer & lex)
112 {
113         InsetCollapsable::read(buf, lex);
114
115         // Force default font
116         // This avoids paragraphs in buffer language that would have a
117         // foreign language after a document langauge change, and it ensures
118         // that all new text in ERT gets the "latex" language, since new text
119         // inherits the language from the last position of the existing text.
120         // As a side effect this makes us also robust against bugs in LyX
121         // that might lead to font changes in ERT in .lyx files.
122         Font font(Font::ALL_INHERIT, latex_language);
123         ParagraphList::iterator par = paragraphs().begin();
124         ParagraphList::iterator const end = paragraphs().end();
125         while (par != end) {
126                 pos_type siz = par->size();
127                 for (pos_type i = 0; i <= siz; ++i) {
128                         par->setFont(i, font);
129                 }
130                 ++par;
131         }
132 }
133
134
135 docstring const InsetERT::editMessage() const
136 {
137         return _("Opened ERT Inset");
138 }
139
140
141 int InsetERT::latex(Buffer const &, odocstream & os,
142                     OutputParams const &) const
143 {
144         ParagraphList::const_iterator par = paragraphs().begin();
145         ParagraphList::const_iterator end = paragraphs().end();
146
147         int lines = 0;
148         while (par != end) {
149                 pos_type siz = par->size();
150                 for (pos_type i = 0; i < siz; ++i) {
151                         // ignore all struck out text
152                         if (par->isDeleted(i))
153                                 continue;
154
155                         os.put(par->getChar(i));
156                 }
157                 ++par;
158                 if (par != end) {
159                         os << "\n";
160                         ++lines;
161                 }
162         }
163
164         return lines;
165 }
166
167
168 int InsetERT::plaintext(Buffer const &, odocstream &,
169                         OutputParams const &) const
170 {
171         return 0; // do not output TeX code
172 }
173
174
175 int InsetERT::docbook(Buffer const &, odocstream & os,
176                       OutputParams const &) const
177 {
178         ParagraphList::const_iterator par = paragraphs().begin();
179         ParagraphList::const_iterator end = paragraphs().end();
180
181         int lines = 0;
182         while (par != end) {
183                 pos_type siz = par->size();
184                 for (pos_type i = 0; i < siz; ++i)
185                         os.put(par->getChar(i));
186                 ++par;
187                 if (par != end) {
188                         os << "\n";
189                         ++lines;
190                 }
191         }
192
193         return lines;
194 }
195
196
197 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
198 {
199         BufferParams const & bp = cur.buffer().params();
200         LayoutPtr const layout =
201                         bp.getTextClass().defaultLayout();
202         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
203         switch (cmd.action) {
204
205         case LFUN_MOUSE_PRESS:
206                 if (cmd.button() != mouse_button::button3)
207                         InsetCollapsable::doDispatch(cur, cmd);
208                 else
209                         // This makes the cursor leave the
210                         // inset when it collapses on mouse-3
211                         cur.undispatched();
212                 break;
213
214         case LFUN_QUOTE_INSERT: {
215                 // We need to bypass the fancy quotes in Text
216                 FuncRequest f(LFUN_SELF_INSERT, "\"");
217                 dispatch(cur, f);
218                 break;
219         }
220         case LFUN_INSET_MODIFY: {
221                 InsetCollapsable::CollapseStatus st;
222                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
223                 setStatus(cur, st);
224                 break;
225         }
226         case LFUN_PASTE:
227         case LFUN_CLIPBOARD_PASTE:
228         case LFUN_PRIMARY_SELECTION_PASTE: {
229                 InsetCollapsable::doDispatch(cur, cmd);
230
231                 // Since we can only store plain text, we must reset all
232                 // attributes.
233                 // FIXME: Change only the pasted paragraphs
234
235                 Font font = layout->font;
236                 // ERT contents has always latex_language
237                 font.setLanguage(latex_language);
238                 ParagraphList::iterator const end = paragraphs().end();
239                 for (ParagraphList::iterator par = paragraphs().begin();
240                      par != end; ++par) {
241                         // in case par had a manual label
242                         par->setBeginOfBody();
243                         pos_type const siz = par->size();
244                         for (pos_type i = 0; i < siz; ++i) {
245                                 par->setFont(i, font);
246                         }
247                         par->params().clear();
248                 }
249                 break;
250         }
251         default:
252                 // Force any new text to latex_language
253                 // FIXME: This should only be necessary in init(), but
254                 // new paragraphs that are created by pressing enter at the
255                 // start of an existing paragraph get the buffer language
256                 // and not latex_language, so we take this brute force
257                 // approach.
258                 cur.current_font = layout->font;
259                 cur.real_current_font = layout->font;
260                 cur.current_font.setLanguage(latex_language);
261                 cur.real_current_font.setLanguage(latex_language);
262                 InsetCollapsable::doDispatch(cur, cmd);
263                 break;
264         }
265 }
266
267
268 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
269         FuncStatus & status) const
270 {
271         switch (cmd.action) {
272                 // suppress these
273                 case LFUN_ACCENT_ACUTE:
274                 case LFUN_ACCENT_BREVE:
275                 case LFUN_ACCENT_CARON:
276                 case LFUN_ACCENT_CEDILLA:
277                 case LFUN_ACCENT_CIRCLE:
278                 case LFUN_ACCENT_CIRCUMFLEX:
279                 case LFUN_ACCENT_DOT:
280                 case LFUN_ACCENT_GRAVE:
281                 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
282                 case LFUN_ACCENT_MACRON:
283                 case LFUN_ACCENT_OGONEK:
284                 case LFUN_ACCENT_SPECIAL_CARON:
285                 case LFUN_ACCENT_TIE:
286                 case LFUN_ACCENT_TILDE:
287                 case LFUN_ACCENT_UMLAUT:
288                 case LFUN_ACCENT_UNDERBAR:
289                 case LFUN_ACCENT_UNDERDOT:
290                 case LFUN_APPENDIX:
291                 case LFUN_BREAK_LINE:
292                 case LFUN_CAPTION_INSERT:
293                 case LFUN_DEPTH_DECREMENT:
294                 case LFUN_DEPTH_INCREMENT:
295                 case LFUN_DOTS_INSERT:
296                 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
297                 case LFUN_ENVIRONMENT_INSERT:
298                 case LFUN_ERT_INSERT:
299                 case LFUN_FILE_INSERT:
300                 case LFUN_FLOAT_INSERT:
301                 case LFUN_FLOAT_WIDE_INSERT:
302                 case LFUN_WRAP_INSERT:
303                 case LFUN_FONT_BOLD:
304                 case LFUN_FONT_TYPEWRITER:
305                 case LFUN_FONT_DEFAULT:
306                 case LFUN_FONT_EMPH:
307                 case LFUN_FONT_FREE_APPLY:
308                 case LFUN_FONT_FREE_UPDATE:
309                 case LFUN_FONT_NOUN:
310                 case LFUN_FONT_ROMAN:
311                 case LFUN_FONT_SANS:
312                 case LFUN_FONT_FRAK:
313                 case LFUN_FONT_ITAL:
314                 case LFUN_FONT_SIZE:
315                 case LFUN_FONT_STATE:
316                 case LFUN_FONT_UNDERLINE:
317                 case LFUN_FOOTNOTE_INSERT:
318                 case LFUN_HFILL_INSERT:
319                 case LFUN_HTML_INSERT:
320                 case LFUN_HYPHENATION_POINT_INSERT:
321                 case LFUN_LIGATURE_BREAK_INSERT:
322                 case LFUN_INDEX_INSERT:
323                 case LFUN_INDEX_PRINT:
324                 case LFUN_LABEL_INSERT:
325                 case LFUN_OPTIONAL_INSERT:
326                 case LFUN_BIBITEM_INSERT:
327                 case LFUN_LINE_INSERT:
328                 case LFUN_PAGEBREAK_INSERT:
329                 case LFUN_CLEARPAGE_INSERT:
330                 case LFUN_CLEARDOUBLEPAGE_INSERT:
331                 case LFUN_LANGUAGE:
332                 case LFUN_LAYOUT:
333                 case LFUN_LAYOUT_PARAGRAPH:
334                 case LFUN_LAYOUT_TABULAR:
335                 case LFUN_MARGINALNOTE_INSERT:
336                 case LFUN_MATH_DISPLAY:
337                 case LFUN_MATH_INSERT:
338                 case LFUN_MATH_MATRIX:
339                 case LFUN_MATH_MODE:
340                 case LFUN_MENU_OPEN:
341                 case LFUN_MENU_SEPARATOR_INSERT:
342                 case LFUN_BRANCH_INSERT:
343                 case LFUN_FLEX_INSERT:
344                 case LFUN_NOTE_INSERT:
345                 case LFUN_BOX_INSERT:
346                 case LFUN_NOTE_NEXT:
347                 case LFUN_PARAGRAPH_SPACING:
348                 case LFUN_LABEL_GOTO:
349                 case LFUN_REFERENCE_NEXT:
350                 case LFUN_SPACE_INSERT:
351                 case LFUN_SERVER_GOTO_FILE_ROW:
352                 case LFUN_SERVER_NOTIFY:
353                 case LFUN_SERVER_SET_XY:
354                 case LFUN_TABULAR_INSERT:
355                 case LFUN_TOC_INSERT:
356                 case LFUN_URL_INSERT:
357                 case LFUN_FLOAT_LIST:
358                 case LFUN_INSET_INSERT:
359                 case LFUN_PARAGRAPH_PARAMS:
360                 case LFUN_PARAGRAPH_PARAMS_APPLY:
361                 case LFUN_PARAGRAPH_UPDATE:
362                 case LFUN_NOMENCL_INSERT:
363                 case LFUN_NOMENCL_PRINT:
364                 case LFUN_NOACTION:
365                         status.enabled(false);
366                         return true;
367
368                 case LFUN_QUOTE_INSERT:
369                 case LFUN_INSET_MODIFY:
370                 case LFUN_PASTE:
371                 case LFUN_CLIPBOARD_PASTE:
372                 case LFUN_PRIMARY_SELECTION_PASTE:
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(Inset::Code /* 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