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 pos_type siz = par->size();
111 for (pos_type i = 0; i <= siz; ++i) {
112 par->setFont(i, font);
114 par->params().clear();
120 void InsetERT::write(Buffer const & buf, ostream & os) const
123 InsetCollapsable::write(buf, os);
127 void InsetERT::read(Buffer const & buf, Lexer & lex)
129 InsetCollapsable::read(buf, lex);
131 // Force default font
132 // This avoids paragraphs in buffer language that would have a
133 // foreign language after a document langauge change, and it ensures
134 // that all new text in ERT gets the "latex" language, since new text
135 // inherits the language from the last position of the existing text.
136 // As a side effect this makes us also robust against bugs in LyX
137 // that might lead to font changes in ERT in .lyx files.
138 resetParagraphsFont();
142 docstring const InsetERT::editMessage() const
144 return _("Opened ERT Inset");
148 int InsetERT::latex(Buffer const & buf, odocstream & os,
149 OutputParams const & op) const
151 return InsetCollapsable::latex(buf, os, op);
155 int InsetERT::plaintext(Buffer const &, odocstream &,
156 OutputParams const &) const
158 return 0; // do not output TeX code
162 int InsetERT::docbook(Buffer const &, odocstream & os,
163 OutputParams const &) const
165 // FIXME can we do the same thing here as for LaTeX?
166 ParagraphList::const_iterator par = paragraphs().begin();
167 ParagraphList::const_iterator end = paragraphs().end();
171 pos_type siz = par->size();
172 for (pos_type i = 0; i < siz; ++i)
173 os.put(par->getChar(i));
185 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
187 BufferParams const & bp = cur.buffer().params();
188 LayoutPtr const layout =
189 bp.getTextClass().defaultLayout();
190 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
191 switch (cmd.action) {
193 case LFUN_MOUSE_PRESS:
194 if (cmd.button() != mouse_button::button3)
195 InsetCollapsable::doDispatch(cur, cmd);
197 // This makes the cursor leave the
198 // inset when it collapses on mouse-3
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 // ERT contents has always latex_language
224 resetParagraphsFont();
228 // Force any new text to latex_language
229 // FIXME: This should only be necessary in init(), but
230 // new paragraphs that are created by pressing enter at the
231 // start of an existing paragraph get the buffer language
232 // and not latex_language, so we take this brute force
234 cur.current_font.fontInfo() = layout->font;
235 cur.real_current_font.fontInfo() = layout->font;
236 cur.current_font.setLanguage(latex_language);
237 cur.real_current_font.setLanguage(latex_language);
238 InsetCollapsable::doDispatch(cur, cmd);
244 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
245 FuncStatus & status) const
247 switch (cmd.action) {
248 case LFUN_CLIPBOARD_PASTE:
249 case LFUN_INSET_MODIFY:
251 case LFUN_PRIMARY_SELECTION_PASTE:
252 case LFUN_QUOTE_INSERT:
253 status.enabled(true);
256 // this one is difficult to get right. As a half-baked
257 // solution, we consider only the first action of the sequence
258 case LFUN_COMMAND_SEQUENCE: {
259 // argument contains ';'-terminated commands
260 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
261 FuncRequest func(lyxaction.lookupFunc(firstcmd));
262 func.origin = cmd.origin;
263 return getStatus(cur, func, status);
267 return InsetCollapsable::getStatus(cur, cmd, status);
272 void InsetERT::setButtonLabel()
275 if (decoration() == Classic)
276 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
278 setLabel(getNewLabel(_("ERT")));
282 bool InsetERT::insetAllowed(InsetCode /* code */) const
288 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
290 FontInfo tmpfont = mi.base.font;
291 getDrawFont(mi.base.font);
292 mi.base.font.realize(tmpfont);
293 InsetCollapsable::metrics(mi, dim);
294 mi.base.font = tmpfont;
298 void InsetERT::draw(PainterInfo & pi, int x, int y) const
300 FontInfo tmpfont = pi.base.font;
301 getDrawFont(pi.base.font);
302 pi.base.font.realize(tmpfont);
303 const_cast<InsetERT &>(*this).setButtonLabel();
304 InsetCollapsable::draw(pi, x, y);
305 pi.base.font = tmpfont;
309 bool InsetERT::showInsetDialog(BufferView * bv) const
311 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
316 void InsetERT::getDrawFont(FontInfo & font) const
319 font.realize(layout_.font);
323 string const InsetERTMailer::name_("ert");
325 InsetERTMailer::InsetERTMailer(InsetERT & inset)
330 string const InsetERTMailer::inset2string(Buffer const &) const
332 return params2string(inset_.status());
336 void InsetERTMailer::string2params(string const & in,
337 InsetCollapsable::CollapseStatus & status)
339 status = InsetCollapsable::Collapsed;
343 istringstream data(in);
350 return print_mailer_error("InsetERTMailer", in, 1, name_);
355 status = static_cast<InsetCollapsable::CollapseStatus>(s);
360 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
363 data << name_ << ' ' << status;