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 & buf, odocstream & os,
143 OutputParams const & op) const
145 return InsetCollapsable::latex(buf, os, op);
149 int InsetERT::plaintext(Buffer const &, odocstream &,
150 OutputParams const &) const
152 return 0; // do not output TeX code
156 int InsetERT::docbook(Buffer const &, odocstream & os,
157 OutputParams const &) const
159 // FIXME can we do the same thing here as for LaTeX?
160 ParagraphList::const_iterator par = paragraphs().begin();
161 ParagraphList::const_iterator end = paragraphs().end();
165 pos_type siz = par->size();
166 for (pos_type i = 0; i < siz; ++i)
167 os.put(par->getChar(i));
179 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
181 BufferParams const & bp = cur.buffer().params();
182 LayoutPtr const layout =
183 bp.getTextClass().defaultLayout();
184 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
185 switch (cmd.action) {
187 case LFUN_MOUSE_PRESS:
188 if (cmd.button() != mouse_button::button3)
189 InsetCollapsable::doDispatch(cur, cmd);
191 // This makes the cursor leave the
192 // inset when it collapses on mouse-3
196 case LFUN_QUOTE_INSERT: {
197 // We need to bypass the fancy quotes in Text
198 FuncRequest f(LFUN_SELF_INSERT, "\"");
202 case LFUN_INSET_MODIFY: {
203 InsetCollapsable::CollapseStatus st;
204 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
209 case LFUN_CLIPBOARD_PASTE:
210 case LFUN_PRIMARY_SELECTION_PASTE: {
211 InsetCollapsable::doDispatch(cur, cmd);
213 // Since we can only store plain text, we must reset all
215 // FIXME: Change only the pasted paragraphs
217 Font font = layout->font;
218 // ERT contents has always latex_language
219 font.setLanguage(latex_language);
220 ParagraphList::iterator const end = paragraphs().end();
221 for (ParagraphList::iterator par = paragraphs().begin();
223 // in case par had a manual label
224 par->setBeginOfBody();
225 pos_type const siz = par->size();
226 for (pos_type i = 0; i < siz; ++i) {
227 par->setFont(i, font);
229 par->params().clear();
234 // Force any new text to latex_language
235 // FIXME: This should only be necessary in init(), but
236 // new paragraphs that are created by pressing enter at the
237 // start of an existing paragraph get the buffer language
238 // and not latex_language, so we take this brute force
240 cur.current_font = layout->font;
241 cur.real_current_font = layout->font;
242 cur.current_font.setLanguage(latex_language);
243 cur.real_current_font.setLanguage(latex_language);
244 InsetCollapsable::doDispatch(cur, cmd);
250 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
251 FuncStatus & status) const
253 switch (cmd.action) {
255 case LFUN_ACCENT_ACUTE:
256 case LFUN_ACCENT_BREVE:
257 case LFUN_ACCENT_CARON:
258 case LFUN_ACCENT_CEDILLA:
259 case LFUN_ACCENT_CIRCLE:
260 case LFUN_ACCENT_CIRCUMFLEX:
261 case LFUN_ACCENT_DOT:
262 case LFUN_ACCENT_GRAVE:
263 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
264 case LFUN_ACCENT_MACRON:
265 case LFUN_ACCENT_OGONEK:
266 case LFUN_ACCENT_SPECIAL_CARON:
267 case LFUN_ACCENT_TIE:
268 case LFUN_ACCENT_TILDE:
269 case LFUN_ACCENT_UMLAUT:
270 case LFUN_ACCENT_UNDERBAR:
271 case LFUN_ACCENT_UNDERDOT:
273 case LFUN_BIBITEM_INSERT:
274 case LFUN_BOX_INSERT:
275 case LFUN_BRANCH_INSERT:
276 case LFUN_BREAK_LINE:
277 case LFUN_CAPTION_INSERT:
278 case LFUN_CLEARPAGE_INSERT:
279 case LFUN_CLEARDOUBLEPAGE_INSERT:
280 case LFUN_DEPTH_DECREMENT:
281 case LFUN_DEPTH_INCREMENT:
282 case LFUN_DOTS_INSERT:
283 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
284 case LFUN_ENVIRONMENT_INSERT:
285 case LFUN_ERT_INSERT:
286 case LFUN_FILE_INSERT:
287 case LFUN_FLEX_INSERT:
288 case LFUN_FLOAT_INSERT:
289 case LFUN_FLOAT_LIST:
290 case LFUN_FLOAT_WIDE_INSERT:
292 case LFUN_FONT_TYPEWRITER:
293 case LFUN_FONT_DEFAULT:
295 case LFUN_FONT_FREE_APPLY:
296 case LFUN_FONT_FREE_UPDATE:
298 case LFUN_FONT_ROMAN:
303 case LFUN_FONT_STATE:
304 case LFUN_FONT_UNDERLINE:
305 case LFUN_FOOTNOTE_INSERT:
306 case LFUN_HFILL_INSERT:
307 case LFUN_HYPERLINK_INSERT:
308 case LFUN_HYPHENATION_POINT_INSERT:
309 case LFUN_INDEX_INSERT:
310 case LFUN_INDEX_PRINT:
311 case LFUN_INSET_INSERT:
312 case LFUN_LABEL_GOTO:
313 case LFUN_LABEL_INSERT:
314 case LFUN_LIGATURE_BREAK_INSERT:
315 case LFUN_LINE_INSERT:
316 case LFUN_PAGEBREAK_INSERT:
319 case LFUN_LAYOUT_PARAGRAPH:
320 case LFUN_LAYOUT_TABULAR:
321 case LFUN_MARGINALNOTE_INSERT:
322 case LFUN_MATH_DISPLAY:
323 case LFUN_MATH_INSERT:
324 case LFUN_MATH_MATRIX:
327 case LFUN_MENU_SEPARATOR_INSERT:
329 case LFUN_NOMENCL_INSERT:
330 case LFUN_NOMENCL_PRINT:
331 case LFUN_NOTE_INSERT:
333 case LFUN_OPTIONAL_INSERT:
334 case LFUN_PARAGRAPH_PARAMS:
335 case LFUN_PARAGRAPH_PARAMS_APPLY:
336 case LFUN_PARAGRAPH_SPACING:
337 case LFUN_PARAGRAPH_UPDATE:
338 case LFUN_REFERENCE_NEXT:
339 case LFUN_SERVER_GOTO_FILE_ROW:
340 case LFUN_SERVER_NOTIFY:
341 case LFUN_SERVER_SET_XY:
342 case LFUN_SPACE_INSERT:
343 case LFUN_TABULAR_INSERT:
344 case LFUN_TOC_INSERT:
345 case LFUN_WRAP_INSERT:
346 status.enabled(false);
349 case LFUN_CLIPBOARD_PASTE:
350 case LFUN_INSET_MODIFY:
352 case LFUN_PRIMARY_SELECTION_PASTE:
353 case LFUN_QUOTE_INSERT:
354 status.enabled(true);
357 // this one is difficult to get right. As a half-baked
358 // solution, we consider only the first action of the sequence
359 case LFUN_COMMAND_SEQUENCE: {
360 // argument contains ';'-terminated commands
361 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
362 FuncRequest func(lyxaction.lookupFunc(firstcmd));
363 func.origin = cmd.origin;
364 return getStatus(cur, func, status);
368 return InsetCollapsable::getStatus(cur, cmd, status);
373 void InsetERT::setButtonLabel()
376 if (decoration() == Classic)
377 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
379 setLabel(getNewLabel(_("ERT")));
383 bool InsetERT::insetAllowed(InsetCode /* code */) const
389 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
391 Font tmpfont = mi.base.font;
392 getDrawFont(mi.base.font);
393 mi.base.font.realize(tmpfont);
394 InsetCollapsable::metrics(mi, dim);
395 mi.base.font = tmpfont;
399 void InsetERT::draw(PainterInfo & pi, int x, int y) const
401 Font tmpfont = pi.base.font;
402 getDrawFont(pi.base.font);
403 pi.base.font.realize(tmpfont);
404 const_cast<InsetERT &>(*this).setButtonLabel();
405 InsetCollapsable::draw(pi, x, y);
406 pi.base.font = tmpfont;
410 bool InsetERT::showInsetDialog(BufferView * bv) const
412 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
417 void InsetERT::getDrawFont(Font & font) const
419 font = Font(Font::ALL_INHERIT, latex_language);
420 font.realize(layout_.font);
424 string const InsetERTMailer::name_("ert");
426 InsetERTMailer::InsetERTMailer(InsetERT & inset)
431 string const InsetERTMailer::inset2string(Buffer const &) const
433 return params2string(inset_.status());
437 void InsetERTMailer::string2params(string const & in,
438 InsetCollapsable::CollapseStatus & status)
440 status = InsetCollapsable::Collapsed;
444 istringstream data(in);
451 return print_mailer_error("InsetERTMailer", in, 1, name_);
456 status = static_cast<InsetCollapsable::CollapseStatus>(s);
461 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
464 data << name_ << ' ' << status;