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);
66 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
67 : InsetCollapsable(bp, status)
73 InsetERT::InsetERT(InsetERT const & in)
74 : InsetCollapsable(in)
80 auto_ptr<InsetBase> InsetERT::doClone() const
82 return auto_ptr<InsetBase>(new InsetERT(*this));
86 InsetERT::InsetERT(BufferParams const & bp,
87 Language const *, string const & contents, CollapseStatus status)
88 : InsetCollapsable(bp, status)
90 //LyXFont font(LyXFont::ALL_INHERIT, lang);
93 string::const_iterator cit = contents.begin();
94 string::const_iterator end = contents.end();
96 for (; cit != end; ++cit)
97 paragraphs().begin()->insertChar(pos++, *cit, font);
99 // the init has to be after the initialization of the paragraph
100 // because of the label settings (draw_label for ert insets).
105 InsetERT::~InsetERT()
107 InsetERTMailer(*this).hideDialog();
111 void InsetERT::write(Buffer const & buf, ostream & os) const
114 InsetCollapsable::write(buf, os);
118 string const InsetERT::editMessage() const
120 return _("Opened ERT Inset");
124 int InsetERT::latex(Buffer const &, ostream & os,
125 OutputParams const &) const
127 ParagraphList::const_iterator par = paragraphs().begin();
128 ParagraphList::const_iterator end = paragraphs().end();
132 pos_type siz = par->size();
133 for (pos_type i = 0; i < siz; ++i) {
134 // ignore all struck out text
135 if (isDeletedText(*par, i))
138 os << par->getChar(i);
151 int InsetERT::plaintext(Buffer const &, ostream &,
152 OutputParams const & /*runparams*/) const
158 int InsetERT::linuxdoc(Buffer const &, ostream & os,
159 OutputParams const &) const
161 ParagraphList::const_iterator par = paragraphs().begin();
162 ParagraphList::const_iterator end = paragraphs().end();
166 pos_type siz = par->size();
167 for (pos_type i = 0; i < siz; ++i)
168 os << par->getChar(i);
180 int InsetERT::docbook(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 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
204 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
205 switch (cmd.action) {
207 case LFUN_INSET_MODIFY: {
208 InsetCollapsable::CollapseStatus st;
209 InsetERTMailer::string2params(cmd.argument, st);
211 if (status() == Collapsed)
212 cur.leaveInset(*this);
216 case LFUN_PASTESELECTION: {
217 InsetCollapsable::doDispatch(cur, cmd);
219 // Since we can only store plain text, we must reset all
221 // FIXME: Change only the pasted paragraphs
223 BufferParams const & bp = cur.buffer().params();
224 LyXLayout_ptr const layout =
225 bp.getLyXTextClass().defaultLayout();
226 LyXFont font = layout->font;
227 // We need to set the language for non-english documents
228 font.setLanguage(bp.language);
229 ParagraphList::iterator const end = paragraphs().end();
230 for (ParagraphList::iterator par = paragraphs().begin();
233 // in case par had a manual label
234 par->setBeginOfBody();
235 pos_type const siz = par->size();
236 for (pos_type i = 0; i < siz; ++i) {
237 par->setFont(i, font);
239 par->params().clear();
244 InsetCollapsable::doDispatch(cur, cmd);
250 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
251 FuncStatus & status) const
253 switch (cmd.action) {
260 case LFUN_CIRCUMFLEX:
263 case LFUN_HUNG_UMLAUT:
266 case LFUN_SPECIAL_CARON:
274 case LFUN_INSET_CAPTION:
276 case LFUN_DEPTH_PLUS:
278 case LFUN_END_OF_SENTENCE:
279 case LFUN_ENVIRONMENT_INSERT:
281 case LFUN_FILE_INSERT:
282 case LFUN_INSET_FLOAT:
283 case LFUN_INSET_WIDE_FLOAT:
284 case LFUN_INSET_WRAP:
289 case LFUN_FREEFONT_APPLY:
290 case LFUN_FREEFONT_UPDATE:
297 case LFUN_FONT_STATE:
299 case LFUN_INSET_FOOTNOTE:
302 case LFUN_HYPHENATION:
303 case LFUN_LIGATURE_BREAK:
304 case LFUN_INDEX_INSERT:
305 case LFUN_INDEX_PRINT:
306 case LFUN_INSERT_LABEL:
307 case LFUN_INSET_OPTARG:
308 case LFUN_INSERT_BIBITEM:
309 case LFUN_INSERT_LINE:
310 case LFUN_INSERT_PAGEBREAK:
313 case LFUN_LAYOUT_PARAGRAPH:
314 case LFUN_LAYOUT_TABULAR:
315 case LFUN_INSET_MARGINAL:
316 case LFUN_MATH_DISPLAY:
317 case LFUN_INSERT_MATH:
318 case LFUN_INSERT_MATRIX:
320 case LFUN_MENU_OPEN_BY_NAME:
321 case LFUN_MENU_SEPARATOR:
322 case LFUN_INSERT_BRANCH:
323 case LFUN_INSERT_CHARSTYLE:
324 case LFUN_INSERT_NOTE:
325 case LFUN_INSERT_BOX:
327 case LFUN_PARAGRAPH_SPACING:
330 case LFUN_REFERENCE_GOTO:
331 case LFUN_SPACE_INSERT:
332 case LFUN_GOTOFILEROW:
335 case LFUN_TABULAR_INSERT:
336 case LFUN_TOC_INSERT:
338 case LFUN_FLOAT_LIST:
339 case LFUN_INSET_INSERT:
340 case LFUN_PARAGRAPH_APPLY:
341 case LFUN_PARAGRAPH_UPDATE:
343 status.enabled(false);
346 case LFUN_INSET_MODIFY:
348 case LFUN_PASTESELECTION:
349 status.enabled(true);
352 // this one is difficult to get right. As a half-baked
353 // solution, we consider only the first action of the sequence
354 case LFUN_SEQUENCE: {
355 // argument contains ';'-terminated commands
356 string const firstcmd = token(cmd.argument, ';', 0);
357 FuncRequest func(lyxaction.lookupFunc(firstcmd));
358 func.origin = cmd.origin;
359 return getStatus(cur, func, status);
363 return InsetCollapsable::getStatus(cur, cmd, status);
368 void InsetERT::setButtonLabel()
370 setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
374 bool InsetERT::insetAllowed(InsetBase::Code /* code */) const
380 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
382 LyXFont tmpfont = mi.base.font;
383 getDrawFont(mi.base.font);
384 mi.base.font.realize(tmpfont);
385 InsetCollapsable::metrics(mi, dim);
386 mi.base.font = tmpfont;
391 void InsetERT::draw(PainterInfo & pi, int x, int y) const
393 LyXFont tmpfont = pi.base.font;
394 getDrawFont(pi.base.font);
395 // I don't understand why the above .realize isn't needed, or
396 // even wanted, here. It just works. -- MV 10.04.2005
397 InsetCollapsable::draw(pi, x, y);
398 pi.base.font = tmpfont;
402 bool InsetERT::showInsetDialog(BufferView * bv) const
404 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
409 void InsetERT::getDrawFont(LyXFont & font) const
411 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
412 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
413 font.setColor(LColor::latex);
417 string const InsetERTMailer::name_("ert");
419 InsetERTMailer::InsetERTMailer(InsetERT & inset)
424 string const InsetERTMailer::inset2string(Buffer const &) const
426 return params2string(inset_.status());
430 void InsetERTMailer::string2params(string const & in,
431 InsetCollapsable::CollapseStatus & status)
433 status = InsetCollapsable::Collapsed;
437 istringstream data(in);
444 return print_mailer_error("InsetERTMailer", in, 1, name_);
449 status = static_cast<InsetCollapsable::CollapseStatus>(s);
454 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
457 data << name_ << ' ' << status;