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;
52 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
53 : InsetCollapsable(bp, status)
57 InsetERT::InsetERT(InsetERT const & in)
58 : InsetCollapsable(in)
62 Inset * InsetERT::clone() const
64 return new InsetERT(*this);
70 InsetERTMailer(*this).hideDialog();
74 void InsetERT::write(Buffer const & buf, ostream & os) const
77 InsetCollapsable::write(buf, os);
81 docstring const InsetERT::editMessage() const
83 return _("Opened ERT Inset");
87 int InsetERT::latex(Buffer const & buf, odocstream & os,
88 OutputParams const & op) const
90 return InsetCollapsable::latex(buf, os, op);
94 int InsetERT::plaintext(Buffer const &, odocstream &,
95 OutputParams const &) const
97 return 0; // do not output TeX code
101 int InsetERT::docbook(Buffer const &, odocstream & os,
102 OutputParams const &) const
104 // FIXME can we do the same thing here as for LaTeX?
105 ParagraphList::const_iterator par = paragraphs().begin();
106 ParagraphList::const_iterator end = paragraphs().end();
110 pos_type siz = par->size();
111 for (pos_type i = 0; i < siz; ++i)
112 os.put(par->getChar(i));
124 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
126 BufferParams const & bp = cur.buffer().params();
127 LayoutPtr const layout =
128 bp.getTextClass().defaultLayout();
129 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
130 switch (cmd.action) {
132 case LFUN_MOUSE_PRESS:
133 if (cmd.button() != mouse_button::button3)
134 InsetCollapsable::doDispatch(cur, cmd);
136 // This makes the cursor leave the
137 // inset when it collapses on mouse-3
141 case LFUN_QUOTE_INSERT: {
142 // We need to bypass the fancy quotes in Text
143 FuncRequest f(LFUN_SELF_INSERT, "\"");
147 case LFUN_INSET_MODIFY: {
148 InsetCollapsable::CollapseStatus st;
149 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
154 // Force any new text to latex_language
155 // FIXME: This should not be necessary but
156 // new paragraphs that are created by pressing enter at the
157 // start of an existing paragraph get the buffer language
158 // and not latex_language, so we take this brute force
160 cur.current_font.fontInfo() = layout->font;
161 cur.real_current_font.fontInfo() = layout->font;
162 InsetCollapsable::doDispatch(cur, cmd);
168 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
169 FuncStatus & status) const
171 switch (cmd.action) {
172 case LFUN_CLIPBOARD_PASTE:
173 case LFUN_INSET_MODIFY:
175 case LFUN_PRIMARY_SELECTION_PASTE:
176 case LFUN_QUOTE_INSERT:
177 status.enabled(true);
180 // this one is difficult to get right. As a half-baked
181 // solution, we consider only the first action of the sequence
182 case LFUN_COMMAND_SEQUENCE: {
183 // argument contains ';'-terminated commands
184 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
185 FuncRequest func(lyxaction.lookupFunc(firstcmd));
186 func.origin = cmd.origin;
187 return getStatus(cur, func, status);
191 return InsetCollapsable::getStatus(cur, cmd, status);
196 void InsetERT::setButtonLabel()
199 if (decoration() == Classic)
200 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
202 setLabel(getNewLabel(_("ERT")));
206 bool InsetERT::insetAllowed(InsetCode /* code */) const
212 void InsetERT::draw(PainterInfo & pi, int x, int y) const
214 const_cast<InsetERT &>(*this).setButtonLabel();
215 InsetCollapsable::draw(pi, x, y);
219 bool InsetERT::showInsetDialog(BufferView * bv) const
221 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
226 string const InsetERTMailer::name_("ert");
228 InsetERTMailer::InsetERTMailer(InsetERT & inset)
233 string const InsetERTMailer::inset2string(Buffer const &) const
235 return params2string(inset_.status());
239 void InsetERTMailer::string2params(string const & in,
240 InsetCollapsable::CollapseStatus & status)
242 status = InsetCollapsable::Collapsed;
246 istringstream data(in);
253 return print_mailer_error("InsetERTMailer", in, 1, name_);
258 status = static_cast<InsetCollapsable::CollapseStatus>(s);
263 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
266 data << name_ << ' ' << status;