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::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 & buf, odocstream & os,
142 OutputParams const & op) const
144 return InsetCollapsable::latex(buf, os, op);
148 int InsetERT::plaintext(Buffer const &, odocstream &,
149 OutputParams const &) const
151 return 0; // do not output TeX code
155 int InsetERT::docbook(Buffer const &, odocstream & os,
156 OutputParams const &) const
158 // FIXME can we do the same thing here as for LaTeX?
159 ParagraphList::const_iterator par = paragraphs().begin();
160 ParagraphList::const_iterator end = paragraphs().end();
164 pos_type siz = par->size();
165 for (pos_type i = 0; i < siz; ++i)
166 os.put(par->getChar(i));
178 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
180 BufferParams const & bp = cur.buffer().params();
181 LayoutPtr const layout =
182 bp.getTextClass().defaultLayout();
183 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
184 switch (cmd.action) {
186 case LFUN_MOUSE_PRESS:
187 if (cmd.button() != mouse_button::button3)
188 InsetCollapsable::doDispatch(cur, cmd);
190 // This makes the cursor leave the
191 // inset when it collapses on mouse-3
195 case LFUN_QUOTE_INSERT: {
196 // We need to bypass the fancy quotes in Text
197 FuncRequest f(LFUN_SELF_INSERT, "\"");
201 case LFUN_INSET_MODIFY: {
202 InsetCollapsable::CollapseStatus st;
203 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
208 case LFUN_CLIPBOARD_PASTE:
209 case LFUN_PRIMARY_SELECTION_PASTE: {
210 InsetCollapsable::doDispatch(cur, cmd);
212 // Since we can only store plain text, we must reset all
214 // FIXME: Change only the pasted paragraphs
216 Font font = layout->font;
217 // ERT contents has always latex_language
218 font.setLanguage(latex_language);
219 ParagraphList::iterator const end = paragraphs().end();
220 for (ParagraphList::iterator par = paragraphs().begin();
222 // in case par had a manual label
223 par->setBeginOfBody();
224 pos_type const siz = par->size();
225 for (pos_type i = 0; i < siz; ++i) {
226 par->setFont(i, font);
228 par->params().clear();
233 // Force any new text to latex_language
234 // FIXME: This should only be necessary in init(), but
235 // new paragraphs that are created by pressing enter at the
236 // start of an existing paragraph get the buffer language
237 // and not latex_language, so we take this brute force
239 cur.current_font = layout->font;
240 cur.real_current_font = layout->font;
241 cur.current_font.setLanguage(latex_language);
242 cur.real_current_font.setLanguage(latex_language);
243 InsetCollapsable::doDispatch(cur, cmd);
249 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
250 FuncStatus & status) const
252 switch (cmd.action) {
253 case LFUN_CLIPBOARD_PASTE:
254 case LFUN_INSET_MODIFY:
256 case LFUN_PRIMARY_SELECTION_PASTE:
257 case LFUN_QUOTE_INSERT:
258 status.enabled(true);
261 // this one is difficult to get right. As a half-baked
262 // solution, we consider only the first action of the sequence
263 case LFUN_COMMAND_SEQUENCE: {
264 // argument contains ';'-terminated commands
265 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
266 FuncRequest func(lyxaction.lookupFunc(firstcmd));
267 func.origin = cmd.origin;
268 return getStatus(cur, func, status);
272 return InsetCollapsable::getStatus(cur, cmd, status);
277 void InsetERT::setButtonLabel()
280 if (decoration() == Classic)
281 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
283 setLabel(getNewLabel(_("ERT")));
287 bool InsetERT::insetAllowed(InsetCode /* code */) const
293 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
295 Font tmpfont = mi.base.font;
296 getDrawFont(mi.base.font);
297 mi.base.font.realize(tmpfont);
298 InsetCollapsable::metrics(mi, dim);
299 mi.base.font = tmpfont;
303 void InsetERT::draw(PainterInfo & pi, int x, int y) const
305 Font tmpfont = pi.base.font;
306 getDrawFont(pi.base.font);
307 pi.base.font.realize(tmpfont);
308 const_cast<InsetERT &>(*this).setButtonLabel();
309 InsetCollapsable::draw(pi, x, y);
310 pi.base.font = tmpfont;
314 bool InsetERT::showInsetDialog(BufferView * bv) const
316 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
321 void InsetERT::getDrawFont(Font & font) const
323 font = Font(Font::ALL_INHERIT, latex_language);
324 font.realize(layout_.font);
328 string const InsetERTMailer::name_("ert");
330 InsetERTMailer::InsetERTMailer(InsetERT & inset)
335 string const InsetERTMailer::inset2string(Buffer const &) const
337 return params2string(inset_.status());
341 void InsetERTMailer::string2params(string const & in,
342 InsetCollapsable::CollapseStatus & status)
344 status = InsetCollapsable::Collapsed;
348 istringstream data(in);
355 return print_mailer_error("InsetERTMailer", in, 1, name_);
360 status = static_cast<InsetCollapsable::CollapseStatus>(s);
365 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
368 data << name_ << ' ' << status;