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 docstring const InsetERT::editMessage() const
126 return _("Opened ERT Inset");
130 int InsetERT::latex(Buffer const & buf, odocstream & os,
131 OutputParams const & op) const
133 return InsetCollapsable::latex(buf, os, op);
137 int InsetERT::plaintext(Buffer const &, odocstream &,
138 OutputParams const &) const
140 return 0; // do not output TeX code
144 int InsetERT::docbook(Buffer const &, odocstream & os,
145 OutputParams const &) const
147 // FIXME can we do the same thing here as for LaTeX?
148 ParagraphList::const_iterator par = paragraphs().begin();
149 ParagraphList::const_iterator end = paragraphs().end();
153 pos_type siz = par->size();
154 for (pos_type i = 0; i < siz; ++i)
155 os.put(par->getChar(i));
167 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
169 BufferParams const & bp = cur.buffer().params();
170 LayoutPtr const layout =
171 bp.getTextClass().defaultLayout();
172 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
173 switch (cmd.action) {
175 case LFUN_MOUSE_PRESS:
176 if (cmd.button() != mouse_button::button3)
177 InsetCollapsable::doDispatch(cur, cmd);
179 // This makes the cursor leave the
180 // inset when it collapses on mouse-3
184 case LFUN_QUOTE_INSERT: {
185 // We need to bypass the fancy quotes in Text
186 FuncRequest f(LFUN_SELF_INSERT, "\"");
190 case LFUN_INSET_MODIFY: {
191 InsetCollapsable::CollapseStatus st;
192 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
197 // Force any new text to latex_language
198 // FIXME: This should only be necessary in init(), but
199 // new paragraphs that are created by pressing enter at the
200 // start of an existing paragraph get the buffer language
201 // and not latex_language, so we take this brute force
203 cur.current_font.fontInfo() = layout->font;
204 cur.real_current_font.fontInfo() = layout->font;
205 InsetCollapsable::doDispatch(cur, cmd);
211 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
212 FuncStatus & status) const
214 switch (cmd.action) {
215 case LFUN_CLIPBOARD_PASTE:
216 case LFUN_INSET_MODIFY:
218 case LFUN_PRIMARY_SELECTION_PASTE:
219 case LFUN_QUOTE_INSERT:
220 status.enabled(true);
223 // this one is difficult to get right. As a half-baked
224 // solution, we consider only the first action of the sequence
225 case LFUN_COMMAND_SEQUENCE: {
226 // argument contains ';'-terminated commands
227 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
228 FuncRequest func(lyxaction.lookupFunc(firstcmd));
229 func.origin = cmd.origin;
230 return getStatus(cur, func, status);
234 return InsetCollapsable::getStatus(cur, cmd, status);
239 void InsetERT::setButtonLabel()
242 if (decoration() == Classic)
243 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
245 setLabel(getNewLabel(_("ERT")));
249 bool InsetERT::insetAllowed(InsetCode /* code */) const
255 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
257 FontInfo tmpfont = mi.base.font;
258 getDrawFont(mi.base.font);
259 mi.base.font.realize(tmpfont);
260 InsetCollapsable::metrics(mi, dim);
261 mi.base.font = tmpfont;
265 void InsetERT::draw(PainterInfo & pi, int x, int y) const
267 FontInfo tmpfont = pi.base.font;
268 getDrawFont(pi.base.font);
269 pi.base.font.realize(tmpfont);
270 const_cast<InsetERT &>(*this).setButtonLabel();
271 InsetCollapsable::draw(pi, x, y);
272 pi.base.font = tmpfont;
276 bool InsetERT::showInsetDialog(BufferView * bv) const
278 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
283 void InsetERT::getDrawFont(FontInfo & font) const
286 font.realize(layout_.font);
290 string const InsetERTMailer::name_("ert");
292 InsetERTMailer::InsetERTMailer(InsetERT & inset)
297 string const InsetERTMailer::inset2string(Buffer const &) const
299 return params2string(inset_.status());
303 void InsetERTMailer::string2params(string const & in,
304 InsetCollapsable::CollapseStatus & status)
306 status = InsetCollapsable::Collapsed;
310 istringstream data(in);
317 return print_mailer_error("InsetERTMailer", in, 1, name_);
322 status = static_cast<InsetCollapsable::CollapseStatus>(s);
327 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
330 data << name_ << ' ' << status;