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 "TextClass.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "Paragraph.h"
34 #include "frontends/alert.h"
47 using std::istringstream;
49 using std::ostringstream;
56 Font font(Font::ALL_SANE);
59 font.setColor(Color::latex);
61 text_.current_font.setLanguage(latex_language);
62 text_.real_current_font.setLanguage(latex_language);
66 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
67 : InsetCollapsable(bp, status)
73 InsetERT::InsetERT(InsetERT const & in)
74 : InsetCollapsable(in)
80 auto_ptr<Inset> InsetERT::doClone() const
82 return auto_ptr<Inset>(new InsetERT(*this));
87 InsetERT::InsetERT(BufferParams const & bp,
88 Language const *, string const & contents, CollapseStatus status)
89 : InsetCollapsable(bp, status)
91 Font font(Font::ALL_INHERIT, latex_language);
92 paragraphs().begin()->insert(0, contents, font);
94 // the init has to be after the initialization of the paragraph
95 // because of the label settings (draw_label for ert insets).
101 InsetERT::~InsetERT()
103 InsetERTMailer(*this).hideDialog();
107 void InsetERT::write(Buffer const & buf, ostream & os) const
110 InsetCollapsable::write(buf, os);
114 void InsetERT::read(Buffer const & buf, Lexer & lex)
116 InsetCollapsable::read(buf, lex);
118 // Force default font
119 // This avoids paragraphs in buffer language that would have a
120 // foreign language after a document langauge change, and it ensures
121 // that all new text in ERT gets the "latex" language, since new text
122 // inherits the language from the last position of the existing text.
123 // As a side effect this makes us also robust against bugs in LyX
124 // that might lead to font changes in ERT in .lyx files.
125 Font font(Font::ALL_INHERIT, latex_language);
126 ParagraphList::iterator par = paragraphs().begin();
127 ParagraphList::iterator const end = paragraphs().end();
129 pos_type siz = par->size();
130 for (pos_type i = 0; i <= siz; ++i) {
131 par->setFont(i, font);
138 docstring const InsetERT::editMessage() const
140 return _("Opened ERT Inset");
144 int InsetERT::latex(Buffer const &, odocstream & os,
145 OutputParams const &) const
147 ParagraphList::const_iterator par = paragraphs().begin();
148 ParagraphList::const_iterator end = paragraphs().end();
152 pos_type siz = par->size();
153 for (pos_type i = 0; i < siz; ++i) {
154 // ignore all struck out text
155 if (par->isDeleted(i))
158 os.put(par->getChar(i));
171 int InsetERT::plaintext(Buffer const &, odocstream &,
172 OutputParams const &) const
174 return 0; // do not output TeX code
178 int InsetERT::docbook(Buffer const &, odocstream & os,
179 OutputParams const &) const
181 ParagraphList::const_iterator par = paragraphs().begin();
182 ParagraphList::const_iterator end = paragraphs().end();
186 pos_type siz = par->size();
187 for (pos_type i = 0; i < siz; ++i)
188 os.put(par->getChar(i));
200 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
202 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
203 switch (cmd.action) {
205 case LFUN_QUOTE_INSERT: {
206 // We need to bypass the fancy quotes in Text
207 FuncRequest f(LFUN_SELF_INSERT, "\"");
211 case LFUN_INSET_MODIFY: {
212 InsetCollapsable::CollapseStatus st;
213 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
218 case LFUN_CLIPBOARD_PASTE:
219 case LFUN_PRIMARY_SELECTION_PASTE: {
220 InsetCollapsable::doDispatch(cur, cmd);
222 // Since we can only store plain text, we must reset all
224 // FIXME: Change only the pasted paragraphs
226 BufferParams const & bp = cur.buffer().params();
227 Layout_ptr const layout =
228 bp.getTextClass().defaultLayout();
229 Font font = layout->font;
230 // ERT contents has always latex_language
231 font.setLanguage(latex_language);
232 ParagraphList::iterator const end = paragraphs().end();
233 for (ParagraphList::iterator par = paragraphs().begin();
235 // in case par had a manual label
236 par->setBeginOfBody();
237 pos_type const siz = par->size();
238 for (pos_type i = 0; i < siz; ++i) {
239 par->setFont(i, font);
241 par->params().clear();
246 // Force any new text to latex_language
247 // FIXME: This should only be necessary in init(), but
248 // new paragraphs that are created by pressing enter at the
249 // start of an existing paragraph get the buffer language
250 // and not latex_language, so we take this brute force
252 text_.current_font.setLanguage(latex_language);
253 text_.real_current_font.setLanguage(latex_language);
255 InsetCollapsable::doDispatch(cur, cmd);
261 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
262 FuncStatus & status) const
264 switch (cmd.action) {
266 case LFUN_ACCENT_ACUTE:
267 case LFUN_ACCENT_BREVE:
268 case LFUN_ACCENT_CARON:
269 case LFUN_ACCENT_CEDILLA:
270 case LFUN_ACCENT_CIRCLE:
271 case LFUN_ACCENT_CIRCUMFLEX:
272 case LFUN_ACCENT_DOT:
273 case LFUN_ACCENT_GRAVE:
274 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
275 case LFUN_ACCENT_MACRON:
276 case LFUN_ACCENT_OGONEK:
277 case LFUN_ACCENT_SPECIAL_CARON:
278 case LFUN_ACCENT_TIE:
279 case LFUN_ACCENT_TILDE:
280 case LFUN_ACCENT_UMLAUT:
281 case LFUN_ACCENT_UNDERBAR:
282 case LFUN_ACCENT_UNDERDOT:
284 case LFUN_BREAK_LINE:
285 case LFUN_CAPTION_INSERT:
286 case LFUN_DEPTH_DECREMENT:
287 case LFUN_DEPTH_INCREMENT:
288 case LFUN_DOTS_INSERT:
289 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
290 case LFUN_ENVIRONMENT_INSERT:
291 case LFUN_ERT_INSERT:
292 case LFUN_FILE_INSERT:
293 case LFUN_FLOAT_INSERT:
294 case LFUN_FLOAT_WIDE_INSERT:
295 case LFUN_WRAP_INSERT:
298 case LFUN_FONT_DEFAULT:
300 case LFUN_FONT_FREE_APPLY:
301 case LFUN_FONT_FREE_UPDATE:
303 case LFUN_FONT_ROMAN:
308 case LFUN_FONT_STATE:
309 case LFUN_FONT_UNDERLINE:
310 case LFUN_FOOTNOTE_INSERT:
311 case LFUN_HFILL_INSERT:
312 case LFUN_HTML_INSERT:
313 case LFUN_HYPHENATION_POINT_INSERT:
314 case LFUN_LIGATURE_BREAK_INSERT:
315 case LFUN_INDEX_INSERT:
316 case LFUN_INDEX_PRINT:
317 case LFUN_LABEL_INSERT:
318 case LFUN_OPTIONAL_INSERT:
319 case LFUN_BIBITEM_INSERT:
320 case LFUN_LINE_INSERT:
321 case LFUN_PAGEBREAK_INSERT:
322 case LFUN_CLEARPAGE_INSERT:
323 case LFUN_CLEARDOUBLEPAGE_INSERT:
326 case LFUN_LAYOUT_PARAGRAPH:
327 case LFUN_LAYOUT_TABULAR:
328 case LFUN_MARGINALNOTE_INSERT:
329 case LFUN_MATH_DISPLAY:
330 case LFUN_MATH_INSERT:
331 case LFUN_MATH_MATRIX:
334 case LFUN_MENU_SEPARATOR_INSERT:
335 case LFUN_BRANCH_INSERT:
336 case LFUN_CHARSTYLE_INSERT:
337 case LFUN_NOTE_INSERT:
338 case LFUN_BOX_INSERT:
340 case LFUN_PARAGRAPH_SPACING:
341 case LFUN_LABEL_GOTO:
342 case LFUN_REFERENCE_NEXT:
343 case LFUN_SPACE_INSERT:
344 case LFUN_SERVER_GOTO_FILE_ROW:
345 case LFUN_SERVER_NOTIFY:
346 case LFUN_SERVER_SET_XY:
347 case LFUN_TABULAR_INSERT:
348 case LFUN_TOC_INSERT:
349 case LFUN_URL_INSERT:
350 case LFUN_FLOAT_LIST:
351 case LFUN_INSET_INSERT:
352 case LFUN_PARAGRAPH_PARAMS:
353 case LFUN_PARAGRAPH_PARAMS_APPLY:
354 case LFUN_PARAGRAPH_UPDATE:
355 case LFUN_NOMENCL_INSERT:
356 case LFUN_NOMENCL_PRINT:
358 status.enabled(false);
361 case LFUN_QUOTE_INSERT:
362 case LFUN_INSET_MODIFY:
364 case LFUN_CLIPBOARD_PASTE:
365 case LFUN_PRIMARY_SELECTION_PASTE:
366 status.enabled(true);
369 // this one is difficult to get right. As a half-baked
370 // solution, we consider only the first action of the sequence
371 case LFUN_COMMAND_SEQUENCE: {
372 // argument contains ';'-terminated commands
373 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
374 FuncRequest func(lyxaction.lookupFunc(firstcmd));
375 func.origin = cmd.origin;
376 return getStatus(cur, func, status);
380 return InsetCollapsable::getStatus(cur, cmd, status);
385 void InsetERT::setButtonLabel()
388 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
392 bool InsetERT::insetAllowed(Inset::Code /* code */) const
398 bool InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
400 Font tmpfont = mi.base.font;
401 getDrawFont(mi.base.font);
402 mi.base.font.realize(tmpfont);
403 InsetCollapsable::metrics(mi, dim);
404 mi.base.font = tmpfont;
405 bool const changed = dim_ != dim;
411 void InsetERT::draw(PainterInfo & pi, int x, int y) const
413 Font tmpfont = pi.base.font;
414 getDrawFont(pi.base.font);
415 pi.base.font.realize(tmpfont);
416 InsetCollapsable::draw(pi, x, y);
417 pi.base.font = tmpfont;
421 bool InsetERT::showInsetDialog(BufferView * bv) const
423 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
428 void InsetERT::getDrawFont(Font & font) const
430 font = Font(Font::ALL_INHERIT, latex_language);
431 font.setFamily(Font::TYPEWRITER_FAMILY);
432 font.setColor(Color::latex);
436 string const InsetERTMailer::name_("ert");
438 InsetERTMailer::InsetERTMailer(InsetERT & inset)
443 string const InsetERTMailer::inset2string(Buffer const &) const
445 return params2string(inset_.status());
449 void InsetERTMailer::string2params(string const & in,
450 InsetCollapsable::CollapseStatus & status)
452 status = InsetCollapsable::Collapsed;
456 istringstream data(in);
463 return print_mailer_error("InsetERTMailer", in, 1, name_);
468 status = static_cast<InsetCollapsable::CollapseStatus>(s);
473 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
476 data << name_ << ' ' << status;