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"
41 using lyx::support::token;
47 using std::istringstream;
49 using std::ostringstream;
57 LyXFont font(LyXFont::ALL_SANE);
60 font.setColor(LColor::latex);
62 text_.current_font.setLanguage(latex_language);
63 text_.real_current_font.setLanguage(latex_language);
69 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
70 : InsetCollapsable(bp, status)
76 InsetERT::InsetERT(InsetERT const & in)
77 : InsetCollapsable(in)
83 auto_ptr<InsetBase> InsetERT::doClone() const
85 return auto_ptr<InsetBase>(new InsetERT(*this));
90 InsetERT::InsetERT(BufferParams const & bp,
91 Language const *, string const & contents, CollapseStatus status)
92 : InsetCollapsable(bp, status)
94 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
95 paragraphs().begin()->insert(0, contents, font);
97 // the init has to be after the initialization of the paragraph
98 // because of the label settings (draw_label for ert insets).
104 InsetERT::~InsetERT()
106 InsetERTMailer(*this).hideDialog();
110 void InsetERT::write(Buffer const & buf, ostream & os) const
113 InsetCollapsable::write(buf, os);
117 void InsetERT::read(Buffer const & buf, LyXLex & lex)
119 InsetCollapsable::read(buf, lex);
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();
132 pos_type siz = par->size();
133 for (pos_type i = 0; i <= siz; ++i) {
134 par->setFont(i, font);
141 docstring const InsetERT::editMessage() const
143 return _("Opened ERT Inset");
147 int InsetERT::latex(Buffer const &, ostream & os,
148 OutputParams const &) const
150 ParagraphList::const_iterator par = paragraphs().begin();
151 ParagraphList::const_iterator end = paragraphs().end();
155 pos_type siz = par->size();
156 for (pos_type i = 0; i < siz; ++i) {
157 // ignore all struck out text
158 if (isDeletedText(*par, i))
161 os << par->getChar(i);
174 int InsetERT::plaintext(Buffer const &, ostream &,
175 OutputParams const & /*runparams*/) const
181 int InsetERT::docbook(Buffer const &, ostream & os,
182 OutputParams const &) const
184 ParagraphList::const_iterator par = paragraphs().begin();
185 ParagraphList::const_iterator end = paragraphs().end();
189 pos_type siz = par->size();
190 for (pos_type i = 0; i < siz; ++i)
191 os << par->getChar(i);
203 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
205 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
206 switch (cmd.action) {
208 case LFUN_QUOTE_INSERT: {
209 // We need to bypass the fancy quotes in LyXText
210 FuncRequest f(LFUN_SELF_INSERT, "\"");
214 case LFUN_INSET_MODIFY: {
215 InsetCollapsable::CollapseStatus st;
216 InsetERTMailer::string2params(lyx::to_utf8(cmd.argument()), st);
221 case LFUN_CLIPBOARD_PASTE:
222 case LFUN_PRIMARY_SELECTION_PASTE: {
223 InsetCollapsable::doDispatch(cur, cmd);
225 // Since we can only store plain text, we must reset all
227 forceParagraphsToDefault(cur);
228 // FIXME: Change only the pasted paragraphs
230 BufferParams const & bp = cur.buffer().params();
231 LyXLayout_ptr const layout =
232 bp.getLyXTextClass().defaultLayout();
233 LyXFont font = layout->font;
234 // ERT contents has always latex_language
235 font.setLanguage(latex_language);
236 ParagraphList::iterator const end = paragraphs().end();
237 for (ParagraphList::iterator par = paragraphs().begin();
239 // in case par had a manual label
240 par->setBeginOfBody();
241 pos_type const siz = par->size();
242 for (pos_type i = 0; i < siz; ++i) {
243 par->setFont(i, font);
245 par->params().clear();
250 // Force any new text to latex_language
251 // FIXME: This should only be necessary in init(), but
252 // new paragraphs that are created by pressing enter at the
253 // start of an existing paragraph get the buffer language
254 // and not latex_language, so we take this brute force
256 text_.current_font.setLanguage(latex_language);
257 text_.real_current_font.setLanguage(latex_language);
259 InsetCollapsable::doDispatch(cur, cmd);
265 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
266 FuncStatus & status) const
268 switch (cmd.action) {
270 case LFUN_ACCENT_ACUTE:
271 case LFUN_ACCENT_BREVE:
272 case LFUN_ACCENT_CARON:
273 case LFUN_ACCENT_CEDILLA:
274 case LFUN_ACCENT_CIRCLE:
275 case LFUN_ACCENT_CIRCUMFLEX:
276 case LFUN_ACCENT_DOT:
277 case LFUN_ACCENT_GRAVE:
278 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
279 case LFUN_ACCENT_MACRON:
280 case LFUN_ACCENT_OGONEK:
281 case LFUN_ACCENT_SPECIAL_CARON:
282 case LFUN_ACCENT_TIE:
283 case LFUN_ACCENT_TILDE:
284 case LFUN_ACCENT_UMLAUT:
285 case LFUN_ACCENT_UNDERBAR:
286 case LFUN_ACCENT_UNDERDOT:
288 case LFUN_BREAK_LINE:
289 case LFUN_CAPTION_INSERT:
290 case LFUN_DEPTH_DECREMENT:
291 case LFUN_DEPTH_INCREMENT:
292 case LFUN_DOTS_INSERT:
293 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
294 case LFUN_ENVIRONMENT_INSERT:
295 case LFUN_ERT_INSERT:
296 case LFUN_FILE_INSERT:
297 case LFUN_FLOAT_INSERT:
298 case LFUN_FLOAT_WIDE_INSERT:
299 case LFUN_WRAP_INSERT:
302 case LFUN_FONT_DEFAULT:
304 case LFUN_FONT_FREE_APPLY:
305 case LFUN_FONT_FREE_UPDATE:
307 case LFUN_FONT_ROMAN:
312 case LFUN_FONT_STATE:
313 case LFUN_FONT_UNDERLINE:
314 case LFUN_FOOTNOTE_INSERT:
315 case LFUN_HFILL_INSERT:
316 case LFUN_HTML_INSERT:
317 case LFUN_HYPHENATION_POINT_INSERT:
318 case LFUN_LIGATURE_BREAK_INSERT:
319 case LFUN_INDEX_INSERT:
320 case LFUN_INDEX_PRINT:
321 case LFUN_LABEL_INSERT:
322 case LFUN_OPTIONAL_INSERT:
323 case LFUN_BIBITEM_INSERT:
324 case LFUN_LINE_INSERT:
325 case LFUN_PAGEBREAK_INSERT:
328 case LFUN_LAYOUT_PARAGRAPH:
329 case LFUN_LAYOUT_TABULAR:
330 case LFUN_MARGINALNOTE_INSERT:
331 case LFUN_MATH_DISPLAY:
332 case LFUN_MATH_INSERT:
333 case LFUN_MATH_MATRIX:
336 case LFUN_MENU_SEPARATOR_INSERT:
337 case LFUN_BRANCH_INSERT:
338 case LFUN_CHARSTYLE_INSERT:
339 case LFUN_NOTE_INSERT:
340 case LFUN_BOX_INSERT:
342 case LFUN_PARAGRAPH_SPACING:
343 case LFUN_LABEL_GOTO:
344 case LFUN_REFERENCE_NEXT:
345 case LFUN_SPACE_INSERT:
346 case LFUN_SERVER_GOTO_FILE_ROW:
347 case LFUN_SERVER_NOTIFY:
348 case LFUN_SERVER_SET_XY:
349 case LFUN_TABULAR_INSERT:
350 case LFUN_TOC_INSERT:
351 case LFUN_URL_INSERT:
352 case LFUN_FLOAT_LIST:
353 case LFUN_INSET_INSERT:
354 case LFUN_PARAGRAPH_PARAMS_APPLY:
355 case LFUN_PARAGRAPH_UPDATE:
357 status.enabled(false);
360 case LFUN_QUOTE_INSERT:
361 case LFUN_INSET_MODIFY:
363 case LFUN_CLIPBOARD_PASTE:
364 case LFUN_PRIMARY_SELECTION_PASTE:
365 status.enabled(true);
368 // this one is difficult to get right. As a half-baked
369 // solution, we consider only the first action of the sequence
370 case LFUN_COMMAND_SEQUENCE: {
371 // argument contains ';'-terminated commands
372 string const firstcmd = token(lyx::to_utf8(cmd.argument()), ';', 0);
373 FuncRequest func(lyxaction.lookupFunc(firstcmd));
374 func.origin = cmd.origin;
375 return getStatus(cur, func, status);
379 return InsetCollapsable::getStatus(cur, cmd, status);
384 void InsetERT::setButtonLabel()
387 setLabel(isOpen() ? lyx::to_utf8(_("ERT")) : getNewLabel(lyx::to_utf8(_("ERT"))));
391 bool InsetERT::insetAllowed(InsetBase::Code /* code */) const
397 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
399 LyXFont tmpfont = mi.base.font;
400 getDrawFont(mi.base.font);
401 mi.base.font.realize(tmpfont);
402 InsetCollapsable::metrics(mi, dim);
403 mi.base.font = tmpfont;
408 void InsetERT::draw(PainterInfo & pi, int x, int y) const
410 LyXFont tmpfont = pi.base.font;
411 getDrawFont(pi.base.font);
412 pi.base.font.realize(tmpfont);
413 InsetCollapsable::draw(pi, x, y);
414 pi.base.font = tmpfont;
418 bool InsetERT::showInsetDialog(BufferView * bv) const
420 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
425 void InsetERT::getDrawFont(LyXFont & font) const
427 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
428 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
429 font.setColor(LColor::latex);
433 string const InsetERTMailer::name_("ert");
435 InsetERTMailer::InsetERTMailer(InsetERT & inset)
440 string const InsetERTMailer::inset2string(Buffer const &) const
442 return params2string(inset_.status());
446 void InsetERTMailer::string2params(string const & in,
447 InsetCollapsable::CollapseStatus & status)
449 status = InsetCollapsable::Collapsed;
453 istringstream data(in);
460 return print_mailer_error("InsetERTMailer", in, 1, name_);
465 status = static_cast<InsetCollapsable::CollapseStatus>(s);
470 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
473 data << name_ << ' ' << status;