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