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"
46 using std::istringstream;
48 using std::ostringstream;
55 setLabelFont(layout_.labelfont);
56 text_.current_font.setLanguage(latex_language);
57 text_.real_current_font.setLanguage(latex_language);
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62 : InsetCollapsable(bp, status)
69 InsetERT::InsetERT(InsetERT const & in)
70 : InsetCollapsable(in)
76 Inset * InsetERT::clone() const
78 return new InsetERT(*this);
83 InsetERT::InsetERT(BufferParams const & bp,
84 Language const *, string const & contents, CollapseStatus status)
85 : InsetCollapsable(bp, status)
87 Font font(Font::ALL_INHERIT, latex_language);
88 paragraphs().begin()->insert(0, contents, font);
90 // the init has to be after the initialization of the paragraph
91 // because of the label settings (draw_label for ert insets).
99 InsetERTMailer(*this).hideDialog();
103 void InsetERT::write(Buffer const & buf, ostream & os) const
106 InsetCollapsable::write(buf, os);
110 void InsetERT::read(Buffer const & buf, Lexer & lex)
112 InsetCollapsable::read(buf, lex);
114 // Force default font
115 // This avoids paragraphs in buffer language that would have a
116 // foreign language after a document langauge change, and it ensures
117 // that all new text in ERT gets the "latex" language, since new text
118 // inherits the language from the last position of the existing text.
119 // As a side effect this makes us also robust against bugs in LyX
120 // that might lead to font changes in ERT in .lyx files.
121 Font font(Font::ALL_INHERIT, latex_language);
122 ParagraphList::iterator par = paragraphs().begin();
123 ParagraphList::iterator const end = paragraphs().end();
125 pos_type siz = par->size();
126 for (pos_type i = 0; i <= siz; ++i) {
127 par->setFont(i, font);
134 docstring const InsetERT::editMessage() const
136 return _("Opened ERT Inset");
140 int InsetERT::latex(Buffer const &, odocstream & os,
141 OutputParams const &) const
143 ParagraphList::const_iterator par = paragraphs().begin();
144 ParagraphList::const_iterator end = paragraphs().end();
148 pos_type siz = par->size();
149 for (pos_type i = 0; i < siz; ++i) {
150 // ignore all struck out text
151 if (par->isDeleted(i))
154 os.put(par->getChar(i));
167 int InsetERT::plaintext(Buffer const &, odocstream &,
168 OutputParams const &) const
170 return 0; // do not output TeX code
174 int InsetERT::docbook(Buffer const &, odocstream & os,
175 OutputParams const &) const
177 ParagraphList::const_iterator par = paragraphs().begin();
178 ParagraphList::const_iterator end = paragraphs().end();
182 pos_type siz = par->size();
183 for (pos_type i = 0; i < siz; ++i)
184 os.put(par->getChar(i));
196 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
198 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
199 switch (cmd.action) {
201 case LFUN_QUOTE_INSERT: {
202 // We need to bypass the fancy quotes in Text
203 FuncRequest f(LFUN_SELF_INSERT, "\"");
207 case LFUN_INSET_MODIFY: {
208 InsetCollapsable::CollapseStatus st;
209 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
214 case LFUN_CLIPBOARD_PASTE:
215 case LFUN_PRIMARY_SELECTION_PASTE: {
216 InsetCollapsable::doDispatch(cur, cmd);
218 // Since we can only store plain text, we must reset all
220 // FIXME: Change only the pasted paragraphs
222 BufferParams const & bp = cur.buffer().params();
223 LayoutPtr const layout =
224 bp.getTextClass().defaultLayout();
225 Font font = layout->font;
226 // ERT contents has always latex_language
227 font.setLanguage(latex_language);
228 ParagraphList::iterator const end = paragraphs().end();
229 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 // Force any new text to latex_language
243 // FIXME: This should only be necessary in init(), but
244 // new paragraphs that are created by pressing enter at the
245 // start of an existing paragraph get the buffer language
246 // and not latex_language, so we take this brute force
248 text_.current_font.setLanguage(latex_language);
249 text_.real_current_font.setLanguage(latex_language);
251 InsetCollapsable::doDispatch(cur, cmd);
257 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & status) const
260 switch (cmd.action) {
262 case LFUN_ACCENT_ACUTE:
263 case LFUN_ACCENT_BREVE:
264 case LFUN_ACCENT_CARON:
265 case LFUN_ACCENT_CEDILLA:
266 case LFUN_ACCENT_CIRCLE:
267 case LFUN_ACCENT_CIRCUMFLEX:
268 case LFUN_ACCENT_DOT:
269 case LFUN_ACCENT_GRAVE:
270 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
271 case LFUN_ACCENT_MACRON:
272 case LFUN_ACCENT_OGONEK:
273 case LFUN_ACCENT_SPECIAL_CARON:
274 case LFUN_ACCENT_TIE:
275 case LFUN_ACCENT_TILDE:
276 case LFUN_ACCENT_UMLAUT:
277 case LFUN_ACCENT_UNDERBAR:
278 case LFUN_ACCENT_UNDERDOT:
280 case LFUN_BREAK_LINE:
281 case LFUN_CAPTION_INSERT:
282 case LFUN_DEPTH_DECREMENT:
283 case LFUN_DEPTH_INCREMENT:
284 case LFUN_DOTS_INSERT:
285 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
286 case LFUN_ENVIRONMENT_INSERT:
287 case LFUN_ERT_INSERT:
288 case LFUN_FILE_INSERT:
289 case LFUN_FLOAT_INSERT:
290 case LFUN_FLOAT_WIDE_INSERT:
291 case LFUN_WRAP_INSERT:
294 case LFUN_FONT_DEFAULT:
296 case LFUN_FONT_FREE_APPLY:
297 case LFUN_FONT_FREE_UPDATE:
299 case LFUN_FONT_ROMAN:
304 case LFUN_FONT_STATE:
305 case LFUN_FONT_UNDERLINE:
306 case LFUN_FOOTNOTE_INSERT:
307 case LFUN_HFILL_INSERT:
308 case LFUN_HTML_INSERT:
309 case LFUN_HYPHENATION_POINT_INSERT:
310 case LFUN_LIGATURE_BREAK_INSERT:
311 case LFUN_INDEX_INSERT:
312 case LFUN_INDEX_PRINT:
313 case LFUN_LABEL_INSERT:
314 case LFUN_OPTIONAL_INSERT:
315 case LFUN_BIBITEM_INSERT:
316 case LFUN_LINE_INSERT:
317 case LFUN_PAGEBREAK_INSERT:
318 case LFUN_CLEARPAGE_INSERT:
319 case LFUN_CLEARDOUBLEPAGE_INSERT:
322 case LFUN_LAYOUT_PARAGRAPH:
323 case LFUN_LAYOUT_TABULAR:
324 case LFUN_MARGINALNOTE_INSERT:
325 case LFUN_MATH_DISPLAY:
326 case LFUN_MATH_INSERT:
327 case LFUN_MATH_MATRIX:
330 case LFUN_MENU_SEPARATOR_INSERT:
331 case LFUN_BRANCH_INSERT:
332 case LFUN_CHARSTYLE_INSERT:
333 case LFUN_NOTE_INSERT:
334 case LFUN_BOX_INSERT:
336 case LFUN_PARAGRAPH_SPACING:
337 case LFUN_LABEL_GOTO:
338 case LFUN_REFERENCE_NEXT:
339 case LFUN_SPACE_INSERT:
340 case LFUN_SERVER_GOTO_FILE_ROW:
341 case LFUN_SERVER_NOTIFY:
342 case LFUN_SERVER_SET_XY:
343 case LFUN_TABULAR_INSERT:
344 case LFUN_TOC_INSERT:
345 case LFUN_URL_INSERT:
346 case LFUN_FLOAT_LIST:
347 case LFUN_INSET_INSERT:
348 case LFUN_PARAGRAPH_PARAMS:
349 case LFUN_PARAGRAPH_PARAMS_APPLY:
350 case LFUN_PARAGRAPH_UPDATE:
351 case LFUN_NOMENCL_INSERT:
352 case LFUN_NOMENCL_PRINT:
354 status.enabled(false);
357 case LFUN_QUOTE_INSERT:
358 case LFUN_INSET_MODIFY:
360 case LFUN_CLIPBOARD_PASTE:
361 case LFUN_PRIMARY_SELECTION_PASTE:
362 status.enabled(true);
365 // this one is difficult to get right. As a half-baked
366 // solution, we consider only the first action of the sequence
367 case LFUN_COMMAND_SEQUENCE: {
368 // argument contains ';'-terminated commands
369 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
370 FuncRequest func(lyxaction.lookupFunc(firstcmd));
371 func.origin = cmd.origin;
372 return getStatus(cur, func, status);
376 return InsetCollapsable::getStatus(cur, cmd, status);
381 void InsetERT::setButtonLabel()
384 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
388 bool InsetERT::insetAllowed(Inset::Code /* code */) const
394 bool InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
396 Font tmpfont = mi.base.font;
397 getDrawFont(mi.base.font);
398 mi.base.font.realize(tmpfont);
399 InsetCollapsable::metrics(mi, dim);
400 mi.base.font = tmpfont;
401 bool const changed = dim_ != dim;
407 void InsetERT::draw(PainterInfo & pi, int x, int y) const
409 Font tmpfont = pi.base.font;
410 getDrawFont(pi.base.font);
411 pi.base.font.realize(tmpfont);
412 const_cast<InsetERT &>(*this).setButtonLabel();
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(Font & font) const
427 font = Font(Font::ALL_INHERIT, latex_language);
428 font.realize(layout_.font);
432 string const InsetERTMailer::name_("ert");
434 InsetERTMailer::InsetERTMailer(InsetERT & inset)
439 string const InsetERTMailer::inset2string(Buffer const &) const
441 return params2string(inset_.status());
445 void InsetERTMailer::string2params(string const & in,
446 InsetCollapsable::CollapseStatus & status)
448 status = InsetCollapsable::Collapsed;
452 istringstream data(in);
459 return print_mailer_error("InsetERTMailer", in, 1, name_);
464 status = static_cast<InsetCollapsable::CollapseStatus>(s);
469 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
472 data << name_ << ' ' << status;