3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
18 #include "BufferView.h"
21 #include "dispatchresult.h"
22 #include "funcrequest.h"
23 #include "FuncStatus.h"
27 #include "LyXAction.h"
29 #include "lyxtextclass.h"
30 #include "metricsinfo.h"
31 #include "ParagraphParameters.h"
32 #include "paragraph.h"
34 #include "frontends/Alert.h"
35 #include "frontends/LyXView.h"
40 using lyx::support::token;
46 using std::istringstream;
48 using std::ostringstream;
56 LyXFont font(LyXFont::ALL_SANE);
59 font.setColor(LColor::latex);
61 text_.current_font.setLanguage(latex_language);
62 text_.real_current_font.setLanguage(latex_language);
68 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
69 : InsetCollapsable(bp, status)
75 InsetERT::InsetERT(InsetERT const & in)
76 : InsetCollapsable(in)
82 auto_ptr<InsetBase> InsetERT::doClone() const
84 return auto_ptr<InsetBase>(new InsetERT(*this));
89 InsetERT::InsetERT(BufferParams const & bp,
90 Language const *, string const & contents, CollapseStatus status)
91 : InsetCollapsable(bp, status)
93 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
94 paragraphs().begin()->insert(0, contents, font);
96 // the init has to be after the initialization of the paragraph
97 // because of the label settings (draw_label for ert insets).
103 InsetERT::~InsetERT()
105 InsetERTMailer(*this).hideDialog();
109 void InsetERT::write(Buffer const & buf, ostream & os) const
112 InsetCollapsable::write(buf, os);
116 void InsetERT::read(Buffer const & buf, LyXLex & lex)
118 InsetCollapsable::read(buf, lex);
120 // Force default font
121 // This avoids paragraphs in buffer language that would have a
122 // foreign language after a document langauge change, and it ensures
123 // that all new text in ERT gets the "latex" language, since new text
124 // inherits the language from the last position of the existing text.
125 // As a side effect this makes us also robust against bugs in LyX
126 // that might lead to font changes in ERT in .lyx files.
127 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
128 ParagraphList::iterator par = paragraphs().begin();
129 ParagraphList::iterator const end = paragraphs().end();
131 pos_type siz = par->size();
132 for (pos_type i = 0; i <= siz; ++i) {
133 par->setFont(i, font);
140 string const InsetERT::editMessage() const
142 return _("Opened ERT Inset");
146 int InsetERT::latex(Buffer const &, ostream & os,
147 OutputParams const &) const
149 ParagraphList::const_iterator par = paragraphs().begin();
150 ParagraphList::const_iterator end = paragraphs().end();
154 pos_type siz = par->size();
155 for (pos_type i = 0; i < siz; ++i) {
156 // ignore all struck out text
157 if (isDeletedText(*par, i))
160 os << par->getChar(i);
173 int InsetERT::plaintext(Buffer const &, ostream &,
174 OutputParams const & /*runparams*/) const
180 int InsetERT::linuxdoc(Buffer const &, ostream & os,
181 OutputParams const &) const
183 ParagraphList::const_iterator par = paragraphs().begin();
184 ParagraphList::const_iterator end = paragraphs().end();
188 pos_type siz = par->size();
189 for (pos_type i = 0; i < siz; ++i)
190 os << par->getChar(i);
202 int InsetERT::docbook(Buffer const &, ostream & os,
203 OutputParams const &) const
205 ParagraphList::const_iterator par = paragraphs().begin();
206 ParagraphList::const_iterator end = paragraphs().end();
210 pos_type siz = par->size();
211 for (pos_type i = 0; i < siz; ++i)
212 os << par->getChar(i);
224 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
226 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
227 switch (cmd.action) {
229 case LFUN_INSET_MODIFY: {
230 InsetCollapsable::CollapseStatus st;
231 InsetERTMailer::string2params(cmd.argument, st);
236 case LFUN_PASTESELECTION: {
237 InsetCollapsable::doDispatch(cur, cmd);
239 // Since we can only store plain text, we must reset all
241 // FIXME: Change only the pasted paragraphs
243 BufferParams const & bp = cur.buffer().params();
244 LyXLayout_ptr const layout =
245 bp.getLyXTextClass().defaultLayout();
246 LyXFont font = layout->font;
247 // ERT contents has always latex_language
248 font.setLanguage(latex_language);
249 ParagraphList::iterator const end = paragraphs().end();
250 for (ParagraphList::iterator par = paragraphs().begin();
253 // in case par had a manual label
254 par->setBeginOfBody();
255 pos_type const siz = par->size();
256 for (pos_type i = 0; i < siz; ++i) {
257 par->setFont(i, font);
259 par->params().clear();
264 // Force any new text to latex_language
265 // FIXME: This should only be necessary in init(), but
266 // new paragraphs that are created by pressing enter at the
267 // start of an existing paragraph get the buffer language
268 // and not latex_language, so we take this brute force
270 text_.current_font.setLanguage(latex_language);
271 text_.real_current_font.setLanguage(latex_language);
273 InsetCollapsable::doDispatch(cur, cmd);
279 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
280 FuncStatus & status) const
282 switch (cmd.action) {
289 case LFUN_CIRCUMFLEX:
292 case LFUN_HUNG_UMLAUT:
295 case LFUN_SPECIAL_CARON:
303 case LFUN_INSET_CAPTION:
305 case LFUN_DEPTH_PLUS:
307 case LFUN_END_OF_SENTENCE:
308 case LFUN_ENVIRONMENT_INSERT:
310 case LFUN_FILE_INSERT:
311 case LFUN_INSET_FLOAT:
312 case LFUN_INSET_WIDE_FLOAT:
313 case LFUN_INSET_WRAP:
318 case LFUN_FREEFONT_APPLY:
319 case LFUN_FREEFONT_UPDATE:
326 case LFUN_FONT_STATE:
328 case LFUN_INSET_FOOTNOTE:
331 case LFUN_HYPHENATION:
332 case LFUN_LIGATURE_BREAK:
333 case LFUN_INDEX_INSERT:
334 case LFUN_INDEX_PRINT:
335 case LFUN_INSERT_LABEL:
336 case LFUN_INSET_OPTARG:
337 case LFUN_INSERT_BIBITEM:
338 case LFUN_INSERT_LINE:
339 case LFUN_INSERT_PAGEBREAK:
342 case LFUN_LAYOUT_PARAGRAPH:
343 case LFUN_LAYOUT_TABULAR:
344 case LFUN_INSET_MARGINAL:
345 case LFUN_MATH_DISPLAY:
346 case LFUN_INSERT_MATH:
347 case LFUN_INSERT_MATRIX:
349 case LFUN_MENU_OPEN_BY_NAME:
350 case LFUN_MENU_SEPARATOR:
351 case LFUN_INSERT_BRANCH:
352 case LFUN_INSERT_CHARSTYLE:
353 case LFUN_INSERT_NOTE:
354 case LFUN_INSERT_BOX:
356 case LFUN_PARAGRAPH_SPACING:
358 case LFUN_LABEL_GOTO:
359 case LFUN_REFERENCE_GOTO:
360 case LFUN_SPACE_INSERT:
361 case LFUN_GOTOFILEROW:
364 case LFUN_TABULAR_INSERT:
365 case LFUN_TOC_INSERT:
367 case LFUN_FLOAT_LIST:
368 case LFUN_INSET_INSERT:
369 case LFUN_PARAGRAPH_APPLY:
370 case LFUN_PARAGRAPH_UPDATE:
372 status.enabled(false);
375 case LFUN_INSET_MODIFY:
377 case LFUN_PASTESELECTION:
378 status.enabled(true);
381 // this one is difficult to get right. As a half-baked
382 // solution, we consider only the first action of the sequence
383 case LFUN_SEQUENCE: {
384 // argument contains ';'-terminated commands
385 string const firstcmd = token(cmd.argument, ';', 0);
386 FuncRequest func(lyxaction.lookupFunc(firstcmd));
387 func.origin = cmd.origin;
388 return getStatus(cur, func, status);
392 return InsetCollapsable::getStatus(cur, cmd, status);
397 void InsetERT::setButtonLabel()
399 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
403 bool InsetERT::insetAllowed(InsetBase::Code /* code */) const
409 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
411 LyXFont tmpfont = mi.base.font;
412 getDrawFont(mi.base.font);
413 mi.base.font.realize(tmpfont);
414 InsetCollapsable::metrics(mi, dim);
415 mi.base.font = tmpfont;
420 void InsetERT::draw(PainterInfo & pi, int x, int y) const
422 LyXFont tmpfont = pi.base.font;
423 getDrawFont(pi.base.font);
424 pi.base.font.realize(tmpfont);
425 InsetCollapsable::draw(pi, x, y);
426 pi.base.font = tmpfont;
430 bool InsetERT::showInsetDialog(BufferView * bv) const
432 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
437 void InsetERT::getDrawFont(LyXFont & font) const
439 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
440 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
441 font.setColor(LColor::latex);
445 string const InsetERTMailer::name_("ert");
447 InsetERTMailer::InsetERTMailer(InsetERT & inset)
452 string const InsetERTMailer::inset2string(Buffer const &) const
454 return params2string(inset_.status());
458 void InsetERTMailer::string2params(string const & in,
459 InsetCollapsable::CollapseStatus & status)
461 status = InsetCollapsable::Collapsed;
465 istringstream data(in);
472 return print_mailer_error("InsetERTMailer", in, 1, name_);
477 status = static_cast<InsetCollapsable::CollapseStatus>(s);
482 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
485 data << name_ << ' ' << status;