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) {
254 case LFUN_CLIPBOARD_PASTE:
255 case LFUN_INSET_MODIFY:
257 case LFUN_PRIMARY_SELECTION_PASTE:
258 case LFUN_QUOTE_INSERT:
259 status.enabled(true);
262 // this one is difficult to get right. As a half-baked
263 // solution, we consider only the first action of the sequence
264 case LFUN_COMMAND_SEQUENCE: {
265 // argument contains ';'-terminated commands
266 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
267 FuncRequest func(lyxaction.lookupFunc(firstcmd));
268 func.origin = cmd.origin;
269 return getStatus(cur, func, status);
273 return InsetCollapsable::getStatus(cur, cmd, status);
278 void InsetERT::setButtonLabel()
281 if (decoration() == Classic)
282 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
284 setLabel(getNewLabel(_("ERT")));
288 bool InsetERT::insetAllowed(InsetCode /* code */) const
294 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
296 Font tmpfont = mi.base.font;
297 getDrawFont(mi.base.font);
298 mi.base.font.realize(tmpfont);
299 InsetCollapsable::metrics(mi, dim);
300 mi.base.font = tmpfont;
304 void InsetERT::draw(PainterInfo & pi, int x, int y) const
306 Font tmpfont = pi.base.font;
307 getDrawFont(pi.base.font);
308 pi.base.font.realize(tmpfont);
309 const_cast<InsetERT &>(*this).setButtonLabel();
310 InsetCollapsable::draw(pi, x, y);
311 pi.base.font = tmpfont;
315 bool InsetERT::showInsetDialog(BufferView * bv) const
317 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
322 void InsetERT::getDrawFont(Font & font) const
324 font = Font(Font::ALL_INHERIT, latex_language);
325 font.realize(layout_.font);
329 string const InsetERTMailer::name_("ert");
331 InsetERTMailer::InsetERTMailer(InsetERT & inset)
336 string const InsetERTMailer::inset2string(Buffer const &) const
338 return params2string(inset_.status());
342 void InsetERTMailer::string2params(string const & in,
343 InsetCollapsable::CollapseStatus & status)
345 status = InsetCollapsable::Collapsed;
349 istringstream data(in);
356 return print_mailer_error("InsetERTMailer", in, 1, name_);
361 status = static_cast<InsetCollapsable::CollapseStatus>(s);
366 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
369 data << name_ << ' ' << status;