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 // FIXME: what to do with those?
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 Inset * InsetERT::clone() const
79 return 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_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::resetParagraphsFont()
106 Font font(inherit_font, latex_language);
107 ParagraphList::iterator par = paragraphs().begin();
108 ParagraphList::iterator const end = paragraphs().end();
110 par->resetFonts(font);
111 par->params().clear();
117 void InsetERT::write(Buffer const & buf, ostream & os) const
120 InsetCollapsable::write(buf, os);
124 void InsetERT::read(Buffer const & buf, Lexer & lex)
126 InsetCollapsable::read(buf, lex);
128 // Force default font
129 // This avoids paragraphs in buffer language that would have a
130 // foreign language after a document langauge change, and it ensures
131 // that all new text in ERT gets the "latex" language, since new text
132 // inherits the language from the last position of the existing text.
133 // As a side effect this makes us also robust against bugs in LyX
134 // that might lead to font changes in ERT in .lyx files.
135 resetParagraphsFont();
139 docstring const InsetERT::editMessage() const
141 return _("Opened ERT Inset");
145 int InsetERT::latex(Buffer const & buf, odocstream & os,
146 OutputParams const & op) const
148 return InsetCollapsable::latex(buf, os, op);
152 int InsetERT::plaintext(Buffer const &, odocstream &,
153 OutputParams const &) const
155 return 0; // do not output TeX code
159 int InsetERT::docbook(Buffer const &, odocstream & os,
160 OutputParams const &) const
162 // FIXME can we do the same thing here as for LaTeX?
163 ParagraphList::const_iterator par = paragraphs().begin();
164 ParagraphList::const_iterator end = paragraphs().end();
168 pos_type siz = par->size();
169 for (pos_type i = 0; i < siz; ++i)
170 os.put(par->getChar(i));
182 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
184 BufferParams const & bp = cur.buffer().params();
185 LayoutPtr const layout =
186 bp.getTextClass().defaultLayout();
187 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
188 switch (cmd.action) {
190 case LFUN_MOUSE_PRESS:
191 if (cmd.button() != mouse_button::button3)
192 InsetCollapsable::doDispatch(cur, cmd);
194 // This makes the cursor leave the
195 // inset when it collapses on mouse-3
199 case LFUN_QUOTE_INSERT: {
200 // We need to bypass the fancy quotes in Text
201 FuncRequest f(LFUN_SELF_INSERT, "\"");
205 case LFUN_INSET_MODIFY: {
206 InsetCollapsable::CollapseStatus st;
207 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
212 case LFUN_CLIPBOARD_PASTE:
213 case LFUN_PRIMARY_SELECTION_PASTE: {
214 InsetCollapsable::doDispatch(cur, cmd);
216 // Since we can only store plain text, we must reset all
218 // FIXME: Change only the pasted paragraphs
220 // ERT contents has always latex_language
221 resetParagraphsFont();
225 // Force any new text to latex_language
226 // FIXME: This should only be necessary in init(), but
227 // new paragraphs that are created by pressing enter at the
228 // start of an existing paragraph get the buffer language
229 // and not latex_language, so we take this brute force
231 cur.current_font.fontInfo() = layout->font;
232 cur.real_current_font.fontInfo() = layout->font;
233 cur.current_font.setLanguage(latex_language);
234 cur.real_current_font.setLanguage(latex_language);
235 InsetCollapsable::doDispatch(cur, cmd);
241 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
242 FuncStatus & status) const
244 switch (cmd.action) {
245 case LFUN_CLIPBOARD_PASTE:
246 case LFUN_INSET_MODIFY:
248 case LFUN_PRIMARY_SELECTION_PASTE:
249 case LFUN_QUOTE_INSERT:
250 status.enabled(true);
253 // this one is difficult to get right. As a half-baked
254 // solution, we consider only the first action of the sequence
255 case LFUN_COMMAND_SEQUENCE: {
256 // argument contains ';'-terminated commands
257 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
258 FuncRequest func(lyxaction.lookupFunc(firstcmd));
259 func.origin = cmd.origin;
260 return getStatus(cur, func, status);
264 return InsetCollapsable::getStatus(cur, cmd, status);
269 void InsetERT::setButtonLabel()
272 if (decoration() == Classic)
273 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
275 setLabel(getNewLabel(_("ERT")));
279 bool InsetERT::insetAllowed(InsetCode /* code */) const
285 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
287 FontInfo tmpfont = mi.base.font;
288 getDrawFont(mi.base.font);
289 mi.base.font.realize(tmpfont);
290 InsetCollapsable::metrics(mi, dim);
291 mi.base.font = tmpfont;
295 void InsetERT::draw(PainterInfo & pi, int x, int y) const
297 FontInfo tmpfont = pi.base.font;
298 getDrawFont(pi.base.font);
299 pi.base.font.realize(tmpfont);
300 const_cast<InsetERT &>(*this).setButtonLabel();
301 InsetCollapsable::draw(pi, x, y);
302 pi.base.font = tmpfont;
306 bool InsetERT::showInsetDialog(BufferView * bv) const
308 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
313 void InsetERT::getDrawFont(FontInfo & font) const
316 font.realize(layout_.font);
320 string const InsetERTMailer::name_("ert");
322 InsetERTMailer::InsetERTMailer(InsetERT & inset)
327 string const InsetERTMailer::inset2string(Buffer const &) const
329 return params2string(inset_.status());
333 void InsetERTMailer::string2params(string const & in,
334 InsetCollapsable::CollapseStatus & status)
336 status = InsetCollapsable::Collapsed;
340 istringstream data(in);
347 return print_mailer_error("InsetERTMailer", in, 1, name_);
352 status = static_cast<InsetCollapsable::CollapseStatus>(s);
357 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
360 data << name_ << ' ' << status;