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"
40 using lyx::support::token;
46 using std::istringstream;
48 using std::ostringstream;
56 LyXFont font(LyXFont::ALL_SANE);
59 font.setColor(LColor::latex);
61 text_.current_font.setLanguage(latex_language);
62 text_.real_current_font.setLanguage(latex_language);
68 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
69 : InsetCollapsable(bp, status)
75 InsetERT::InsetERT(InsetERT const & in)
76 : InsetCollapsable(in)
82 auto_ptr<InsetBase> InsetERT::doClone() const
84 return auto_ptr<InsetBase>(new InsetERT(*this));
89 InsetERT::InsetERT(BufferParams const & bp,
90 Language const *, string const & contents, CollapseStatus status)
91 : InsetCollapsable(bp, status)
93 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
94 paragraphs().begin()->insert(0, contents, font);
96 // the init has to be after the initialization of the paragraph
97 // because of the label settings (draw_label for ert insets).
103 InsetERT::~InsetERT()
105 InsetERTMailer(*this).hideDialog();
109 void InsetERT::write(Buffer const & buf, ostream & os) const
112 InsetCollapsable::write(buf, os);
116 void InsetERT::read(Buffer const & buf, LyXLex & lex)
118 InsetCollapsable::read(buf, lex);
120 // Force default font
121 // This avoids paragraphs in buffer language that would have a
122 // foreign language after a document langauge change, and it ensures
123 // that all new text in ERT gets the "latex" language, since new text
124 // inherits the language from the last position of the existing text.
125 // As a side effect this makes us also robust against bugs in LyX
126 // that might lead to font changes in ERT in .lyx files.
127 LyXFont font(LyXFont::ALL_INHERIT, latex_language);
128 ParagraphList::iterator par = paragraphs().begin();
129 ParagraphList::iterator const end = paragraphs().end();
131 pos_type siz = par->size();
132 for (pos_type i = 0; i <= siz; ++i) {
133 par->setFont(i, font);
140 string const InsetERT::editMessage() const
142 return _("Opened ERT Inset");
146 int InsetERT::latex(Buffer const &, ostream & os,
147 OutputParams const &) const
149 ParagraphList::const_iterator par = paragraphs().begin();
150 ParagraphList::const_iterator end = paragraphs().end();
154 pos_type siz = par->size();
155 for (pos_type i = 0; i < siz; ++i) {
156 // ignore all struck out text
157 if (isDeletedText(*par, i))
160 os << par->getChar(i);
173 int InsetERT::plaintext(Buffer const &, ostream &,
174 OutputParams const & /*runparams*/) const
180 int InsetERT::docbook(Buffer const &, ostream & os,
181 OutputParams const &) const
183 ParagraphList::const_iterator par = paragraphs().begin();
184 ParagraphList::const_iterator end = paragraphs().end();
188 pos_type siz = par->size();
189 for (pos_type i = 0; i < siz; ++i)
190 os << par->getChar(i);
202 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
204 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
205 switch (cmd.action) {
207 case LFUN_QUOTE_INSERT: {
208 // We need to bypass the fancy quotes in LyXText
209 FuncRequest f(LFUN_SELF_INSERT, "\"");
213 case LFUN_INSET_MODIFY: {
214 InsetCollapsable::CollapseStatus st;
215 InsetERTMailer::string2params(lyx::to_utf8(cmd.argument()), st);
220 case LFUN_CLIPBOARD_PASTE:
221 case LFUN_PRIMARY_SELECTION_PASTE: {
222 InsetCollapsable::doDispatch(cur, cmd);
224 // Since we can only store plain text, we must reset all
226 forceParagraphsToDefault(cur);
227 // FIXME: Change only the pasted paragraphs
229 BufferParams const & bp = cur.buffer().params();
230 LyXLayout_ptr const layout =
231 bp.getLyXTextClass().defaultLayout();
232 LyXFont font = layout->font;
233 // ERT contents has always latex_language
234 font.setLanguage(latex_language);
235 ParagraphList::iterator const end = paragraphs().end();
236 for (ParagraphList::iterator par = paragraphs().begin();
238 // in case par had a manual label
239 par->setBeginOfBody();
240 pos_type const siz = par->size();
241 for (pos_type i = 0; i < siz; ++i) {
242 par->setFont(i, font);
244 par->params().clear();
249 // Force any new text to latex_language
250 // FIXME: This should only be necessary in init(), but
251 // new paragraphs that are created by pressing enter at the
252 // start of an existing paragraph get the buffer language
253 // and not latex_language, so we take this brute force
255 text_.current_font.setLanguage(latex_language);
256 text_.real_current_font.setLanguage(latex_language);
258 InsetCollapsable::doDispatch(cur, cmd);
264 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
265 FuncStatus & status) const
267 switch (cmd.action) {
269 case LFUN_ACCENT_ACUTE:
270 case LFUN_ACCENT_BREVE:
271 case LFUN_ACCENT_CARON:
272 case LFUN_ACCENT_CEDILLA:
273 case LFUN_ACCENT_CIRCLE:
274 case LFUN_ACCENT_CIRCUMFLEX:
275 case LFUN_ACCENT_DOT:
276 case LFUN_ACCENT_GRAVE:
277 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
278 case LFUN_ACCENT_MACRON:
279 case LFUN_ACCENT_OGONEK:
280 case LFUN_ACCENT_SPECIAL_CARON:
281 case LFUN_ACCENT_TIE:
282 case LFUN_ACCENT_TILDE:
283 case LFUN_ACCENT_UMLAUT:
284 case LFUN_ACCENT_UNDERBAR:
285 case LFUN_ACCENT_UNDERDOT:
287 case LFUN_BREAK_LINE:
288 case LFUN_CAPTION_INSERT:
289 case LFUN_DEPTH_DECREMENT:
290 case LFUN_DEPTH_INCREMENT:
291 case LFUN_DOTS_INSERT:
292 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
293 case LFUN_ENVIRONMENT_INSERT:
294 case LFUN_ERT_INSERT:
295 case LFUN_FILE_INSERT:
296 case LFUN_FLOAT_INSERT:
297 case LFUN_FLOAT_WIDE_INSERT:
298 case LFUN_WRAP_INSERT:
301 case LFUN_FONT_DEFAULT:
303 case LFUN_FONT_FREE_APPLY:
304 case LFUN_FONT_FREE_UPDATE:
306 case LFUN_FONT_ROMAN:
311 case LFUN_FONT_STATE:
312 case LFUN_FONT_UNDERLINE:
313 case LFUN_FOOTNOTE_INSERT:
314 case LFUN_HFILL_INSERT:
315 case LFUN_HTML_INSERT:
316 case LFUN_HYPHENATION_POINT_INSERT:
317 case LFUN_LIGATURE_BREAK_INSERT:
318 case LFUN_INDEX_INSERT:
319 case LFUN_INDEX_PRINT:
320 case LFUN_LABEL_INSERT:
321 case LFUN_OPTIONAL_INSERT:
322 case LFUN_BIBITEM_INSERT:
323 case LFUN_LINE_INSERT:
324 case LFUN_PAGEBREAK_INSERT:
327 case LFUN_LAYOUT_PARAGRAPH:
328 case LFUN_LAYOUT_TABULAR:
329 case LFUN_MARGINALNOTE_INSERT:
330 case LFUN_MATH_DISPLAY:
331 case LFUN_MATH_INSERT:
332 case LFUN_MATH_MATRIX:
335 case LFUN_MENU_SEPARATOR_INSERT:
336 case LFUN_BRANCH_INSERT:
337 case LFUN_CHARSTYLE_INSERT:
338 case LFUN_NOTE_INSERT:
339 case LFUN_BOX_INSERT:
341 case LFUN_PARAGRAPH_SPACING:
342 case LFUN_LABEL_GOTO:
343 case LFUN_REFERENCE_NEXT:
344 case LFUN_SPACE_INSERT:
345 case LFUN_SERVER_GOTO_FILE_ROW:
346 case LFUN_SERVER_NOTIFY:
347 case LFUN_SERVER_SET_XY:
348 case LFUN_TABULAR_INSERT:
349 case LFUN_TOC_INSERT:
350 case LFUN_URL_INSERT:
351 case LFUN_FLOAT_LIST:
352 case LFUN_INSET_INSERT:
353 case LFUN_PARAGRAPH_PARAMS_APPLY:
354 case LFUN_PARAGRAPH_UPDATE:
356 status.enabled(false);
359 case LFUN_QUOTE_INSERT:
360 case LFUN_INSET_MODIFY:
362 case LFUN_CLIPBOARD_PASTE:
363 case LFUN_PRIMARY_SELECTION_PASTE:
364 status.enabled(true);
367 // this one is difficult to get right. As a half-baked
368 // solution, we consider only the first action of the sequence
369 case LFUN_COMMAND_SEQUENCE: {
370 // argument contains ';'-terminated commands
371 string const firstcmd = token(lyx::to_utf8(cmd.argument()), ';', 0);
372 FuncRequest func(lyxaction.lookupFunc(firstcmd));
373 func.origin = cmd.origin;
374 return getStatus(cur, func, status);
378 return InsetCollapsable::getStatus(cur, cmd, status);
383 void InsetERT::setButtonLabel()
385 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
389 bool InsetERT::insetAllowed(InsetBase::Code /* code */) const
395 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
397 LyXFont tmpfont = mi.base.font;
398 getDrawFont(mi.base.font);
399 mi.base.font.realize(tmpfont);
400 InsetCollapsable::metrics(mi, dim);
401 mi.base.font = tmpfont;
406 void InsetERT::draw(PainterInfo & pi, int x, int y) const
408 LyXFont tmpfont = pi.base.font;
409 getDrawFont(pi.base.font);
410 pi.base.font.realize(tmpfont);
411 InsetCollapsable::draw(pi, x, y);
412 pi.base.font = tmpfont;
416 bool InsetERT::showInsetDialog(BufferView * bv) const
418 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
423 void InsetERT::getDrawFont(LyXFont & font) const
425 font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
426 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
427 font.setColor(LColor::latex);
431 string const InsetERTMailer::name_("ert");
433 InsetERTMailer::InsetERTMailer(InsetERT & inset)
438 string const InsetERTMailer::inset2string(Buffer const &) const
440 return params2string(inset_.status());
444 void InsetERTMailer::string2params(string const & in,
445 InsetCollapsable::CollapseStatus & status)
447 status = InsetCollapsable::Collapsed;
451 istringstream data(in);
458 return print_mailer_error("InsetERTMailer", in, 1, name_);
463 status = static_cast<InsetCollapsable::CollapseStatus>(s);
468 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
471 data << name_ << ' ' << status;