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"
20 #include "support/debug.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "support/gettext.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"
36 #include "support/lstrings.h"
41 using namespace lyx::support;
46 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
47 : InsetCollapsable(bp, status)
51 InsetERT::InsetERT(InsetERT const & in)
52 : InsetCollapsable(in)
56 Inset * InsetERT::clone() const
58 return new InsetERT(*this);
64 InsetERTMailer(*this).hideDialog();
68 void InsetERT::write(Buffer const & buf, ostream & os) const
71 InsetCollapsable::write(buf, os);
75 docstring const InsetERT::editMessage() const
77 return _("Opened ERT Inset");
81 int InsetERT::latex(Buffer const & buf, odocstream & os,
82 OutputParams const & op) const
84 return InsetCollapsable::latex(buf, os, op);
88 int InsetERT::plaintext(Buffer const &, odocstream &,
89 OutputParams const &) const
91 return 0; // do not output TeX code
95 int InsetERT::docbook(Buffer const &, odocstream & os,
96 OutputParams const &) const
98 // FIXME can we do the same thing here as for LaTeX?
99 ParagraphList::const_iterator par = paragraphs().begin();
100 ParagraphList::const_iterator end = paragraphs().end();
104 pos_type siz = par->size();
105 for (pos_type i = 0; i < siz; ++i)
106 os.put(par->getChar(i));
118 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
120 BufferParams const & bp = cur.buffer().params();
121 LayoutPtr const layout =
122 bp.getTextClass().defaultLayout();
123 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
124 switch (cmd.action) {
126 case LFUN_MOUSE_PRESS:
127 if (cmd.button() != mouse_button::button3)
128 InsetCollapsable::doDispatch(cur, cmd);
130 // This makes the cursor leave the
131 // inset when it collapses on mouse-3
135 case LFUN_QUOTE_INSERT: {
136 // We need to bypass the fancy quotes in Text
137 FuncRequest f(LFUN_SELF_INSERT, "\"");
141 case LFUN_INSET_MODIFY: {
142 InsetCollapsable::CollapseStatus st;
143 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
148 // Force any new text to latex_language
149 // FIXME: This should not be necessary but
150 // new paragraphs that are created by pressing enter at the
151 // start of an existing paragraph get the buffer language
152 // and not latex_language, so we take this brute force
154 cur.current_font.fontInfo() = layout->font;
155 cur.real_current_font.fontInfo() = layout->font;
156 InsetCollapsable::doDispatch(cur, cmd);
162 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
163 FuncStatus & status) const
165 switch (cmd.action) {
166 case LFUN_CLIPBOARD_PASTE:
167 case LFUN_INSET_MODIFY:
169 case LFUN_PRIMARY_SELECTION_PASTE:
170 case LFUN_QUOTE_INSERT:
171 status.enabled(true);
174 // this one is difficult to get right. As a half-baked
175 // solution, we consider only the first action of the sequence
176 case LFUN_COMMAND_SEQUENCE: {
177 // argument contains ';'-terminated commands
178 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
179 FuncRequest func(lyxaction.lookupFunc(firstcmd));
180 func.origin = cmd.origin;
181 return getStatus(cur, func, status);
185 return InsetCollapsable::getStatus(cur, cmd, status);
190 void InsetERT::setButtonLabel()
192 if (decoration() == Classic)
193 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
195 setLabel(getNewLabel(_("ERT")));
199 bool InsetERT::insetAllowed(InsetCode /* code */) const
205 void InsetERT::draw(PainterInfo & pi, int x, int y) const
207 const_cast<InsetERT &>(*this).setButtonLabel();
208 InsetCollapsable::draw(pi, x, y);
212 bool InsetERT::showInsetDialog(BufferView * bv) const
214 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
219 string const InsetERTMailer::name_("ert");
221 InsetERTMailer::InsetERTMailer(InsetERT & inset)
226 string const InsetERTMailer::inset2string(Buffer const &) const
228 return params2string(inset_.status());
232 void InsetERTMailer::string2params(string const & in,
233 InsetCollapsable::CollapseStatus & status)
235 status = InsetCollapsable::Collapsed;
239 istringstream data(in);
246 return print_mailer_error("InsetERTMailer", in, 1, name_);
251 status = static_cast<InsetCollapsable::CollapseStatus>(s);
256 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
259 data << name_ << ' ' << status;