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"
28 #include "LyXAction.h"
30 #include "TextClass.h"
31 #include "MetricsInfo.h"
32 #include "ParagraphParameters.h"
33 #include "Paragraph.h"
35 #include "frontends/alert.h"
47 using std::istringstream;
49 using std::ostringstream;
56 setLabelFont(layout_.labelfont);
57 // FIXME: what to do with those?
58 //text_.current_font.setLanguage(latex_language);
59 //text_.real_current_font.setLanguage(latex_language);
63 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
64 : InsetCollapsable(bp, status)
71 InsetERT::InsetERT(InsetERT const & in)
72 : InsetCollapsable(in)
78 Inset * InsetERT::clone() const
80 return new InsetERT(*this);
85 InsetERT::InsetERT(BufferParams const & bp,
86 Language const *, string const & contents, CollapseStatus status)
87 : InsetCollapsable(bp, status)
89 Font font(Font::ALL_INHERIT, latex_language);
90 paragraphs().begin()->insert(0, contents, font);
92 // the init has to be after the initialization of the paragraph
93 // because of the label settings (draw_label for ert insets).
101 InsetERTMailer(*this).hideDialog();
105 void InsetERT::write(Buffer const & buf, ostream & os) const
108 InsetCollapsable::write(buf, os);
112 void InsetERT::read(Buffer const & buf, Lexer & lex)
114 InsetCollapsable::read(buf, lex);
116 // Force default font
117 // This avoids paragraphs in buffer language that would have a
118 // foreign language after a document langauge change, and it ensures
119 // that all new text in ERT gets the "latex" language, since new text
120 // inherits the language from the last position of the existing text.
121 // As a side effect this makes us also robust against bugs in LyX
122 // that might lead to font changes in ERT in .lyx files.
123 Font font(Font::ALL_INHERIT, latex_language);
124 ParagraphList::iterator par = paragraphs().begin();
125 ParagraphList::iterator const end = paragraphs().end();
127 pos_type siz = par->size();
128 for (pos_type i = 0; i <= siz; ++i) {
129 par->setFont(i, font);
136 docstring const InsetERT::editMessage() const
138 return _("Opened ERT Inset");
142 int InsetERT::latex(Buffer const &, odocstream & os,
143 OutputParams const &) const
145 ParagraphList::const_iterator par = paragraphs().begin();
146 ParagraphList::const_iterator end = paragraphs().end();
150 pos_type siz = par->size();
151 for (pos_type i = 0; i < siz; ++i) {
152 // ignore all struck out text
153 if (par->isDeleted(i))
156 os.put(par->getChar(i));
169 int InsetERT::plaintext(Buffer const &, odocstream &,
170 OutputParams const &) const
172 return 0; // do not output TeX code
176 int InsetERT::docbook(Buffer const &, odocstream & os,
177 OutputParams const &) const
179 ParagraphList::const_iterator par = paragraphs().begin();
180 ParagraphList::const_iterator end = paragraphs().end();
184 pos_type siz = par->size();
185 for (pos_type i = 0; i < siz; ++i)
186 os.put(par->getChar(i));
198 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
200 BufferParams const & bp = cur.buffer().params();
201 LayoutPtr const layout =
202 bp.getTextClass().defaultLayout();
203 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
204 switch (cmd.action) {
206 case LFUN_MOUSE_PRESS:
207 if (cmd.button() != mouse_button::button3)
208 InsetCollapsable::doDispatch(cur, cmd);
210 // This makes the cursor leave the
211 // inset when it collapses on mouse-3
215 case LFUN_QUOTE_INSERT: {
216 // We need to bypass the fancy quotes in Text
217 FuncRequest f(LFUN_SELF_INSERT, "\"");
221 case LFUN_INSET_MODIFY: {
222 InsetCollapsable::CollapseStatus st;
223 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
228 case LFUN_CLIPBOARD_PASTE:
229 case LFUN_PRIMARY_SELECTION_PASTE: {
230 InsetCollapsable::doDispatch(cur, cmd);
232 // Since we can only store plain text, we must reset all
234 // FIXME: Change only the pasted paragraphs
236 Font font = layout->font;
237 // ERT contents has always latex_language
238 font.setLanguage(latex_language);
239 ParagraphList::iterator const end = paragraphs().end();
240 for (ParagraphList::iterator par = paragraphs().begin();
242 // in case par had a manual label
243 par->setBeginOfBody();
244 pos_type const siz = par->size();
245 for (pos_type i = 0; i < siz; ++i) {
246 par->setFont(i, font);
248 par->params().clear();
253 // Force any new text to latex_language
254 // FIXME: This should only be necessary in init(), but
255 // new paragraphs that are created by pressing enter at the
256 // start of an existing paragraph get the buffer language
257 // and not latex_language, so we take this brute force
259 cur.current_font = layout->font;
260 cur.real_current_font = layout->font;
261 cur.current_font.setLanguage(latex_language);
262 cur.real_current_font.setLanguage(latex_language);
263 InsetCollapsable::doDispatch(cur, cmd);
269 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
270 FuncStatus & status) const
272 switch (cmd.action) {
274 case LFUN_ACCENT_ACUTE:
275 case LFUN_ACCENT_BREVE:
276 case LFUN_ACCENT_CARON:
277 case LFUN_ACCENT_CEDILLA:
278 case LFUN_ACCENT_CIRCLE:
279 case LFUN_ACCENT_CIRCUMFLEX:
280 case LFUN_ACCENT_DOT:
281 case LFUN_ACCENT_GRAVE:
282 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
283 case LFUN_ACCENT_MACRON:
284 case LFUN_ACCENT_OGONEK:
285 case LFUN_ACCENT_SPECIAL_CARON:
286 case LFUN_ACCENT_TIE:
287 case LFUN_ACCENT_TILDE:
288 case LFUN_ACCENT_UMLAUT:
289 case LFUN_ACCENT_UNDERBAR:
290 case LFUN_ACCENT_UNDERDOT:
292 case LFUN_BREAK_LINE:
293 case LFUN_CAPTION_INSERT:
294 case LFUN_DEPTH_DECREMENT:
295 case LFUN_DEPTH_INCREMENT:
296 case LFUN_DOTS_INSERT:
297 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
298 case LFUN_ENVIRONMENT_INSERT:
299 case LFUN_ERT_INSERT:
300 case LFUN_FILE_INSERT:
301 case LFUN_FLOAT_INSERT:
302 case LFUN_FLOAT_WIDE_INSERT:
303 case LFUN_WRAP_INSERT:
305 case LFUN_FONT_TYPEWRITER:
306 case LFUN_FONT_DEFAULT:
308 case LFUN_FONT_FREE_APPLY:
309 case LFUN_FONT_FREE_UPDATE:
311 case LFUN_FONT_ROMAN:
316 case LFUN_FONT_STATE:
317 case LFUN_FONT_UNDERLINE:
318 case LFUN_FOOTNOTE_INSERT:
319 case LFUN_HFILL_INSERT:
320 case LFUN_HTML_INSERT:
321 case LFUN_HYPHENATION_POINT_INSERT:
322 case LFUN_LIGATURE_BREAK_INSERT:
323 case LFUN_INDEX_INSERT:
324 case LFUN_INDEX_PRINT:
325 case LFUN_LABEL_INSERT:
326 case LFUN_OPTIONAL_INSERT:
327 case LFUN_BIBITEM_INSERT:
328 case LFUN_LINE_INSERT:
329 case LFUN_PAGEBREAK_INSERT:
330 case LFUN_CLEARPAGE_INSERT:
331 case LFUN_CLEARDOUBLEPAGE_INSERT:
334 case LFUN_LAYOUT_PARAGRAPH:
335 case LFUN_LAYOUT_TABULAR:
336 case LFUN_MARGINALNOTE_INSERT:
337 case LFUN_MATH_DISPLAY:
338 case LFUN_MATH_INSERT:
339 case LFUN_MATH_MATRIX:
342 case LFUN_MENU_SEPARATOR_INSERT:
343 case LFUN_BRANCH_INSERT:
344 case LFUN_FLEX_INSERT:
345 case LFUN_NOTE_INSERT:
346 case LFUN_BOX_INSERT:
348 case LFUN_PARAGRAPH_SPACING:
349 case LFUN_LABEL_GOTO:
350 case LFUN_REFERENCE_NEXT:
351 case LFUN_SPACE_INSERT:
352 case LFUN_SERVER_GOTO_FILE_ROW:
353 case LFUN_SERVER_NOTIFY:
354 case LFUN_SERVER_SET_XY:
355 case LFUN_TABULAR_INSERT:
356 case LFUN_TOC_INSERT:
357 case LFUN_URL_INSERT:
358 case LFUN_FLOAT_LIST:
359 case LFUN_INSET_INSERT:
360 case LFUN_PARAGRAPH_PARAMS:
361 case LFUN_PARAGRAPH_PARAMS_APPLY:
362 case LFUN_PARAGRAPH_UPDATE:
363 case LFUN_NOMENCL_INSERT:
364 case LFUN_NOMENCL_PRINT:
366 status.enabled(false);
369 case LFUN_QUOTE_INSERT:
370 case LFUN_INSET_MODIFY:
372 case LFUN_CLIPBOARD_PASTE:
373 case LFUN_PRIMARY_SELECTION_PASTE:
374 status.enabled(true);
377 // this one is difficult to get right. As a half-baked
378 // solution, we consider only the first action of the sequence
379 case LFUN_COMMAND_SEQUENCE: {
380 // argument contains ';'-terminated commands
381 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
382 FuncRequest func(lyxaction.lookupFunc(firstcmd));
383 func.origin = cmd.origin;
384 return getStatus(cur, func, status);
388 return InsetCollapsable::getStatus(cur, cmd, status);
393 void InsetERT::setButtonLabel()
396 if (decoration() == Classic)
397 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
399 setLabel(getNewLabel(_("ERT")));
403 bool InsetERT::insetAllowed(Inset::Code /* code */) const
409 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
411 Font tmpfont = mi.base.font;
412 getDrawFont(mi.base.font);
413 mi.base.font.realize(tmpfont);
414 InsetCollapsable::metrics(mi, dim);
415 mi.base.font = tmpfont;
419 void InsetERT::draw(PainterInfo & pi, int x, int y) const
421 Font tmpfont = pi.base.font;
422 getDrawFont(pi.base.font);
423 pi.base.font.realize(tmpfont);
424 const_cast<InsetERT &>(*this).setButtonLabel();
425 InsetCollapsable::draw(pi, x, y);
426 pi.base.font = tmpfont;
430 bool InsetERT::showInsetDialog(BufferView * bv) const
432 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
437 void InsetERT::getDrawFont(Font & font) const
439 font = Font(Font::ALL_INHERIT, latex_language);
440 font.realize(layout_.font);
444 string const InsetERTMailer::name_("ert");
446 InsetERTMailer::InsetERTMailer(InsetERT & inset)
451 string const InsetERTMailer::inset2string(Buffer const &) const
453 return params2string(inset_.status());
457 void InsetERTMailer::string2params(string const & in,
458 InsetCollapsable::CollapseStatus & status)
460 status = InsetCollapsable::Collapsed;
464 istringstream data(in);
471 return print_mailer_error("InsetERTMailer", in, 1, name_);
476 status = static_cast<InsetCollapsable::CollapseStatus>(s);
481 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
484 data << name_ << ' ' << status;