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 setLabelFont(layout_.labelfont);
57 text_.current_font.setLanguage(latex_language);
58 text_.real_current_font.setLanguage(latex_language);
62 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
63 : InsetCollapsable(bp, status)
70 InsetERT::InsetERT(InsetERT const & in)
71 : InsetCollapsable(in)
77 auto_ptr<Inset> InsetERT::doClone() const
79 return auto_ptr<Inset>(new InsetERT(*this));
84 InsetERT::InsetERT(BufferParams const & bp,
85 Language const *, string const & contents, CollapseStatus status)
86 : InsetCollapsable(bp, status)
88 Font font(Font::ALL_INHERIT, latex_language);
89 paragraphs().begin()->insert(0, contents, font);
91 // the init has to be after the initialization of the paragraph
92 // because of the label settings (draw_label for ert insets).
100 InsetERTMailer(*this).hideDialog();
104 void InsetERT::write(Buffer const & buf, ostream & os) const
107 InsetCollapsable::write(buf, os);
111 void InsetERT::read(Buffer const & buf, Lexer & lex)
113 InsetCollapsable::read(buf, lex);
115 // Force default font
116 // This avoids paragraphs in buffer language that would have a
117 // foreign language after a document langauge change, and it ensures
118 // that all new text in ERT gets the "latex" language, since new text
119 // inherits the language from the last position of the existing text.
120 // As a side effect this makes us also robust against bugs in LyX
121 // that might lead to font changes in ERT in .lyx files.
122 Font font(Font::ALL_INHERIT, latex_language);
123 ParagraphList::iterator par = paragraphs().begin();
124 ParagraphList::iterator const end = paragraphs().end();
126 pos_type siz = par->size();
127 for (pos_type i = 0; i <= siz; ++i) {
128 par->setFont(i, font);
135 docstring const InsetERT::editMessage() const
137 return _("Opened ERT Inset");
141 int InsetERT::latex(Buffer const &, odocstream & os,
142 OutputParams const &) const
144 ParagraphList::const_iterator par = paragraphs().begin();
145 ParagraphList::const_iterator end = paragraphs().end();
149 pos_type siz = par->size();
150 for (pos_type i = 0; i < siz; ++i) {
151 // ignore all struck out text
152 if (par->isDeleted(i))
155 os.put(par->getChar(i));
168 int InsetERT::plaintext(Buffer const &, odocstream &,
169 OutputParams const &) const
171 return 0; // do not output TeX code
175 int InsetERT::docbook(Buffer const &, odocstream & os,
176 OutputParams const &) const
178 ParagraphList::const_iterator par = paragraphs().begin();
179 ParagraphList::const_iterator end = paragraphs().end();
183 pos_type siz = par->size();
184 for (pos_type i = 0; i < siz; ++i)
185 os.put(par->getChar(i));
197 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
199 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
200 switch (cmd.action) {
202 case LFUN_QUOTE_INSERT: {
203 // We need to bypass the fancy quotes in Text
204 FuncRequest f(LFUN_SELF_INSERT, "\"");
208 case LFUN_INSET_MODIFY: {
209 InsetCollapsable::CollapseStatus st;
210 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
215 case LFUN_CLIPBOARD_PASTE:
216 case LFUN_PRIMARY_SELECTION_PASTE: {
217 InsetCollapsable::doDispatch(cur, cmd);
219 // Since we can only store plain text, we must reset all
221 // FIXME: Change only the pasted paragraphs
223 BufferParams const & bp = cur.buffer().params();
224 LayoutPtr const layout =
225 bp.getTextClass().defaultLayout();
226 Font font = layout->font;
227 // ERT contents has always latex_language
228 font.setLanguage(latex_language);
229 ParagraphList::iterator const end = paragraphs().end();
230 for (ParagraphList::iterator par = paragraphs().begin();
232 // in case par had a manual label
233 par->setBeginOfBody();
234 pos_type const siz = par->size();
235 for (pos_type i = 0; i < siz; ++i) {
236 par->setFont(i, font);
238 par->params().clear();
243 // Force any new text to latex_language
244 // FIXME: This should only be necessary in init(), but
245 // new paragraphs that are created by pressing enter at the
246 // start of an existing paragraph get the buffer language
247 // and not latex_language, so we take this brute force
249 text_.current_font.setLanguage(latex_language);
250 text_.real_current_font.setLanguage(latex_language);
252 InsetCollapsable::doDispatch(cur, cmd);
258 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 switch (cmd.action) {
263 case LFUN_ACCENT_ACUTE:
264 case LFUN_ACCENT_BREVE:
265 case LFUN_ACCENT_CARON:
266 case LFUN_ACCENT_CEDILLA:
267 case LFUN_ACCENT_CIRCLE:
268 case LFUN_ACCENT_CIRCUMFLEX:
269 case LFUN_ACCENT_DOT:
270 case LFUN_ACCENT_GRAVE:
271 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
272 case LFUN_ACCENT_MACRON:
273 case LFUN_ACCENT_OGONEK:
274 case LFUN_ACCENT_SPECIAL_CARON:
275 case LFUN_ACCENT_TIE:
276 case LFUN_ACCENT_TILDE:
277 case LFUN_ACCENT_UMLAUT:
278 case LFUN_ACCENT_UNDERBAR:
279 case LFUN_ACCENT_UNDERDOT:
281 case LFUN_BREAK_LINE:
282 case LFUN_CAPTION_INSERT:
283 case LFUN_DEPTH_DECREMENT:
284 case LFUN_DEPTH_INCREMENT:
285 case LFUN_DOTS_INSERT:
286 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
287 case LFUN_ENVIRONMENT_INSERT:
288 case LFUN_ERT_INSERT:
289 case LFUN_FILE_INSERT:
290 case LFUN_FLOAT_INSERT:
291 case LFUN_FLOAT_WIDE_INSERT:
292 case LFUN_WRAP_INSERT:
295 case LFUN_FONT_DEFAULT:
297 case LFUN_FONT_FREE_APPLY:
298 case LFUN_FONT_FREE_UPDATE:
300 case LFUN_FONT_ROMAN:
305 case LFUN_FONT_STATE:
306 case LFUN_FONT_UNDERLINE:
307 case LFUN_FOOTNOTE_INSERT:
308 case LFUN_HFILL_INSERT:
309 case LFUN_HTML_INSERT:
310 case LFUN_HYPHENATION_POINT_INSERT:
311 case LFUN_LIGATURE_BREAK_INSERT:
312 case LFUN_INDEX_INSERT:
313 case LFUN_INDEX_PRINT:
314 case LFUN_LABEL_INSERT:
315 case LFUN_OPTIONAL_INSERT:
316 case LFUN_BIBITEM_INSERT:
317 case LFUN_LINE_INSERT:
318 case LFUN_PAGEBREAK_INSERT:
319 case LFUN_CLEARPAGE_INSERT:
320 case LFUN_CLEARDOUBLEPAGE_INSERT:
323 case LFUN_LAYOUT_PARAGRAPH:
324 case LFUN_LAYOUT_TABULAR:
325 case LFUN_MARGINALNOTE_INSERT:
326 case LFUN_MATH_DISPLAY:
327 case LFUN_MATH_INSERT:
328 case LFUN_MATH_MATRIX:
331 case LFUN_MENU_SEPARATOR_INSERT:
332 case LFUN_BRANCH_INSERT:
333 case LFUN_CHARSTYLE_INSERT:
334 case LFUN_NOTE_INSERT:
335 case LFUN_BOX_INSERT:
337 case LFUN_PARAGRAPH_SPACING:
338 case LFUN_LABEL_GOTO:
339 case LFUN_REFERENCE_NEXT:
340 case LFUN_SPACE_INSERT:
341 case LFUN_SERVER_GOTO_FILE_ROW:
342 case LFUN_SERVER_NOTIFY:
343 case LFUN_SERVER_SET_XY:
344 case LFUN_TABULAR_INSERT:
345 case LFUN_TOC_INSERT:
346 case LFUN_URL_INSERT:
347 case LFUN_FLOAT_LIST:
348 case LFUN_INSET_INSERT:
349 case LFUN_PARAGRAPH_PARAMS:
350 case LFUN_PARAGRAPH_PARAMS_APPLY:
351 case LFUN_PARAGRAPH_UPDATE:
352 case LFUN_NOMENCL_INSERT:
353 case LFUN_NOMENCL_PRINT:
355 status.enabled(false);
358 case LFUN_QUOTE_INSERT:
359 case LFUN_INSET_MODIFY:
361 case LFUN_CLIPBOARD_PASTE:
362 case LFUN_PRIMARY_SELECTION_PASTE:
363 status.enabled(true);
366 // this one is difficult to get right. As a half-baked
367 // solution, we consider only the first action of the sequence
368 case LFUN_COMMAND_SEQUENCE: {
369 // argument contains ';'-terminated commands
370 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
371 FuncRequest func(lyxaction.lookupFunc(firstcmd));
372 func.origin = cmd.origin;
373 return getStatus(cur, func, status);
377 return InsetCollapsable::getStatus(cur, cmd, status);
382 void InsetERT::setButtonLabel()
385 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
389 bool InsetERT::insetAllowed(Inset::Code /* code */) const
395 bool InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
397 Font 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;
402 bool const changed = dim_ != dim;
408 void InsetERT::draw(PainterInfo & pi, int x, int y) const
410 Font tmpfont = pi.base.font;
411 getDrawFont(pi.base.font);
412 pi.base.font.realize(tmpfont);
413 const_cast<InsetERT &>(*this).setButtonLabel();
414 InsetCollapsable::draw(pi, x, y);
415 pi.base.font = tmpfont;
419 bool InsetERT::showInsetDialog(BufferView * bv) const
421 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
426 void InsetERT::getDrawFont(Font & font) const
428 font = Font(Font::ALL_INHERIT, latex_language);
429 font.realize(layout_.font);
433 string const InsetERTMailer::name_("ert");
435 InsetERTMailer::InsetERTMailer(InsetERT & inset)
440 string const InsetERTMailer::inset2string(Buffer const &) const
442 return params2string(inset_.status());
446 void InsetERTMailer::string2params(string const & in,
447 InsetCollapsable::CollapseStatus & status)
449 status = InsetCollapsable::Collapsed;
453 istringstream data(in);
460 return print_mailer_error("InsetERTMailer", in, 1, name_);
465 status = static_cast<InsetCollapsable::CollapseStatus>(s);
470 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
473 data << name_ << ' ' << status;