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::write(Buffer const & buf, ostream & os) const
107 InsetCollapsable::write(buf, os);
111 docstring const InsetERT::editMessage() const
113 return _("Opened ERT Inset");
117 int InsetERT::latex(Buffer const & buf, odocstream & os,
118 OutputParams const & op) const
120 return InsetCollapsable::latex(buf, os, op);
124 int InsetERT::plaintext(Buffer const &, odocstream &,
125 OutputParams const &) const
127 return 0; // do not output TeX code
131 int InsetERT::docbook(Buffer const &, odocstream & os,
132 OutputParams const &) const
134 // FIXME can we do the same thing here as for LaTeX?
135 ParagraphList::const_iterator par = paragraphs().begin();
136 ParagraphList::const_iterator end = paragraphs().end();
140 pos_type siz = par->size();
141 for (pos_type i = 0; i < siz; ++i)
142 os.put(par->getChar(i));
154 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
156 BufferParams const & bp = cur.buffer().params();
157 LayoutPtr const layout =
158 bp.getTextClass().defaultLayout();
159 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
160 switch (cmd.action) {
162 case LFUN_MOUSE_PRESS:
163 if (cmd.button() != mouse_button::button3)
164 InsetCollapsable::doDispatch(cur, cmd);
166 // This makes the cursor leave the
167 // inset when it collapses on mouse-3
171 case LFUN_QUOTE_INSERT: {
172 // We need to bypass the fancy quotes in Text
173 FuncRequest f(LFUN_SELF_INSERT, "\"");
177 case LFUN_INSET_MODIFY: {
178 InsetCollapsable::CollapseStatus st;
179 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
184 // Force any new text to latex_language
185 // FIXME: This should only be necessary in init(), but
186 // new paragraphs that are created by pressing enter at the
187 // start of an existing paragraph get the buffer language
188 // and not latex_language, so we take this brute force
190 cur.current_font.fontInfo() = layout->font;
191 cur.real_current_font.fontInfo() = layout->font;
192 InsetCollapsable::doDispatch(cur, cmd);
198 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
199 FuncStatus & status) const
201 switch (cmd.action) {
202 case LFUN_CLIPBOARD_PASTE:
203 case LFUN_INSET_MODIFY:
205 case LFUN_PRIMARY_SELECTION_PASTE:
206 case LFUN_QUOTE_INSERT:
207 status.enabled(true);
210 // this one is difficult to get right. As a half-baked
211 // solution, we consider only the first action of the sequence
212 case LFUN_COMMAND_SEQUENCE: {
213 // argument contains ';'-terminated commands
214 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
215 FuncRequest func(lyxaction.lookupFunc(firstcmd));
216 func.origin = cmd.origin;
217 return getStatus(cur, func, status);
221 return InsetCollapsable::getStatus(cur, cmd, status);
226 void InsetERT::setButtonLabel()
229 if (decoration() == Classic)
230 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
232 setLabel(getNewLabel(_("ERT")));
236 bool InsetERT::insetAllowed(InsetCode /* code */) const
242 void InsetERT::draw(PainterInfo & pi, int x, int y) const
244 const_cast<InsetERT &>(*this).setButtonLabel();
245 InsetCollapsable::draw(pi, x, y);
249 bool InsetERT::showInsetDialog(BufferView * bv) const
251 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
256 string const InsetERTMailer::name_("ert");
258 InsetERTMailer::InsetERTMailer(InsetERT & inset)
263 string const InsetERTMailer::inset2string(Buffer const &) const
265 return params2string(inset_.status());
269 void InsetERTMailer::string2params(string const & in,
270 InsetCollapsable::CollapseStatus & status)
272 status = InsetCollapsable::Collapsed;
276 istringstream data(in);
283 return print_mailer_error("InsetERTMailer", in, 1, name_);
288 status = static_cast<InsetCollapsable::CollapseStatus>(s);
293 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
296 data << name_ << ' ' << status;