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);
214 case LFUN_PASTESELECTION: {
215 InsetCollapsable::doDispatch(cur, cmd);
217 // Since we can only store plain text, we must reset all
219 // FIXME: Change only the pasted paragraphs
221 BufferParams const & bp = cur.buffer().params();
222 LyXLayout_ptr const layout =
223 bp.getLyXTextClass().defaultLayout();
224 LyXFont font = layout->font;
225 // We need to set the language for non-english documents
226 font.setLanguage(bp.language);
227 ParagraphList::iterator const end = paragraphs().end();
228 for (ParagraphList::iterator par = paragraphs().begin();
231 // in case par had a manual label
232 par->setBeginOfBody();
233 pos_type const siz = par->size();
234 for (pos_type i = 0; i < siz; ++i) {
235 par->setFont(i, font);
237 par->params().clear();
242 InsetCollapsable::doDispatch(cur, cmd);
248 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
249 FuncStatus & status) const
251 switch (cmd.action) {
258 case LFUN_CIRCUMFLEX:
261 case LFUN_HUNG_UMLAUT:
264 case LFUN_SPECIAL_CARON:
272 case LFUN_INSET_CAPTION:
274 case LFUN_DEPTH_PLUS:
276 case LFUN_END_OF_SENTENCE:
277 case LFUN_ENVIRONMENT_INSERT:
279 case LFUN_FILE_INSERT:
280 case LFUN_INSET_FLOAT:
281 case LFUN_INSET_WIDE_FLOAT:
282 case LFUN_INSET_WRAP:
287 case LFUN_FREEFONT_APPLY:
288 case LFUN_FREEFONT_UPDATE:
295 case LFUN_FONT_STATE:
297 case LFUN_INSET_FOOTNOTE:
300 case LFUN_HYPHENATION:
301 case LFUN_LIGATURE_BREAK:
302 case LFUN_INDEX_INSERT:
303 case LFUN_INDEX_PRINT:
304 case LFUN_INSERT_LABEL:
305 case LFUN_INSET_OPTARG:
306 case LFUN_INSERT_BIBITEM:
307 case LFUN_INSERT_LINE:
308 case LFUN_INSERT_PAGEBREAK:
311 case LFUN_LAYOUT_PARAGRAPH:
312 case LFUN_LAYOUT_TABULAR:
313 case LFUN_INSET_MARGINAL:
314 case LFUN_MATH_DISPLAY:
315 case LFUN_INSERT_MATH:
316 case LFUN_INSERT_MATRIX:
318 case LFUN_MENU_OPEN_BY_NAME:
319 case LFUN_MENU_SEPARATOR:
320 case LFUN_INSERT_BRANCH:
321 case LFUN_INSERT_CHARSTYLE:
322 case LFUN_INSERT_NOTE:
323 case LFUN_INSERT_BOX:
325 case LFUN_PARAGRAPH_SPACING:
327 case LFUN_LABEL_GOTO:
328 case LFUN_REFERENCE_GOTO:
329 case LFUN_SPACE_INSERT:
330 case LFUN_GOTOFILEROW:
333 case LFUN_TABULAR_INSERT:
334 case LFUN_TOC_INSERT:
336 case LFUN_FLOAT_LIST:
337 case LFUN_INSET_INSERT:
338 case LFUN_PARAGRAPH_APPLY:
339 case LFUN_PARAGRAPH_UPDATE:
341 status.enabled(false);
344 case LFUN_INSET_MODIFY:
346 case LFUN_PASTESELECTION:
347 status.enabled(true);
350 // this one is difficult to get right. As a half-baked
351 // solution, we consider only the first action of the sequence
352 case LFUN_SEQUENCE: {
353 // argument contains ';'-terminated commands
354 string const firstcmd = token(cmd.argument, ';', 0);
355 FuncRequest func(lyxaction.lookupFunc(firstcmd));
356 func.origin = cmd.origin;
357 return getStatus(cur, func, status);
361 return InsetCollapsable::getStatus(cur, cmd, status);
366 void InsetERT::setButtonLabel()
368 setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
372 bool InsetERT::insetAllowed(InsetBase::Code /* code */) const
378 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
380 LyXFont tmpfont = mi.base.font;
381 getDrawFont(mi.base.font);
382 mi.base.font.realize(tmpfont);
383 InsetCollapsable::metrics(mi, dim);
384 mi.base.font = tmpfont;
389 void InsetERT::draw(PainterInfo & pi, int x, int y) const
391 LyXFont tmpfont = pi.base.font;
392 getDrawFont(pi.base.font);
393 // I don't understand why the above .realize isn't needed, or
394 // even wanted, here. It just works. -- MV 10.04.2005
395 InsetCollapsable::draw(pi, x, y);
396 pi.base.font = tmpfont;
400 bool InsetERT::showInsetDialog(BufferView * bv) const
402 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
407 void InsetERT::getDrawFont(LyXFont & font) const
409 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
410 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
411 font.setColor(LColor::latex);
415 string const InsetERTMailer::name_("ert");
417 InsetERTMailer::InsetERTMailer(InsetERT & inset)
422 string const InsetERTMailer::inset2string(Buffer const &) const
424 return params2string(inset_.status());
428 void InsetERTMailer::string2params(string const & in,
429 InsetCollapsable::CollapseStatus & status)
431 status = InsetCollapsable::Collapsed;
435 istringstream data(in);
442 return print_mailer_error("InsetERTMailer", in, 1, name_);
447 status = static_cast<InsetCollapsable::CollapseStatus>(s);
452 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
455 data << name_ << ' ' << status;