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"
47 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
48 : InsetCollapsable(bp, status)
52 InsetERT::InsetERT(InsetERT const & in)
53 : InsetCollapsable(in)
57 Inset * InsetERT::clone() const
59 return new InsetERT(*this);
65 InsetERTMailer(*this).hideDialog();
69 void InsetERT::write(Buffer const & buf, ostream & os) const
72 InsetCollapsable::write(buf, os);
76 docstring const InsetERT::editMessage() const
78 return _("Opened ERT Inset");
82 int InsetERT::latex(Buffer const & buf, odocstream & os,
83 OutputParams const & op) const
85 return InsetCollapsable::latex(buf, os, op);
89 int InsetERT::plaintext(Buffer const &, odocstream &,
90 OutputParams const &) const
92 return 0; // do not output TeX code
96 int InsetERT::docbook(Buffer const &, odocstream & os,
97 OutputParams const &) const
99 // FIXME can we do the same thing here as for LaTeX?
100 ParagraphList::const_iterator par = paragraphs().begin();
101 ParagraphList::const_iterator end = paragraphs().end();
105 pos_type siz = par->size();
106 for (pos_type i = 0; i < siz; ++i)
107 os.put(par->getChar(i));
119 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
121 BufferParams const & bp = cur.buffer().params();
122 LayoutPtr const layout =
123 bp.getTextClass().defaultLayout();
124 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
125 switch (cmd.action) {
127 case LFUN_MOUSE_PRESS:
128 if (cmd.button() != mouse_button::button3)
129 InsetCollapsable::doDispatch(cur, cmd);
131 // This makes the cursor leave the
132 // inset when it collapses on mouse-3
136 case LFUN_QUOTE_INSERT: {
137 // We need to bypass the fancy quotes in Text
138 FuncRequest f(LFUN_SELF_INSERT, "\"");
142 case LFUN_INSET_MODIFY: {
143 InsetCollapsable::CollapseStatus st;
144 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
149 // Force any new text to latex_language
150 // FIXME: This should not be necessary but
151 // new paragraphs that are created by pressing enter at the
152 // start of an existing paragraph get the buffer language
153 // and not latex_language, so we take this brute force
155 cur.current_font.fontInfo() = layout->font;
156 cur.real_current_font.fontInfo() = layout->font;
157 InsetCollapsable::doDispatch(cur, cmd);
163 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
164 FuncStatus & status) const
166 switch (cmd.action) {
167 case LFUN_CLIPBOARD_PASTE:
168 case LFUN_INSET_MODIFY:
170 case LFUN_PRIMARY_SELECTION_PASTE:
171 case LFUN_QUOTE_INSERT:
172 status.enabled(true);
175 // this one is difficult to get right. As a half-baked
176 // solution, we consider only the first action of the sequence
177 case LFUN_COMMAND_SEQUENCE: {
178 // argument contains ';'-terminated commands
179 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
180 FuncRequest func(lyxaction.lookupFunc(firstcmd));
181 func.origin = cmd.origin;
182 return getStatus(cur, func, status);
186 return InsetCollapsable::getStatus(cur, cmd, status);
191 void InsetERT::setButtonLabel()
193 if (decoration() == Classic)
194 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
196 setLabel(getNewLabel(_("ERT")));
200 bool InsetERT::insetAllowed(InsetCode /* code */) const
206 void InsetERT::draw(PainterInfo & pi, int x, int y) const
208 const_cast<InsetERT &>(*this).setButtonLabel();
209 InsetCollapsable::draw(pi, x, y);
213 bool InsetERT::showInsetDialog(BufferView * bv) const
215 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
220 string const InsetERTMailer::name_("ert");
222 InsetERTMailer::InsetERTMailer(InsetERT & inset)
227 string const InsetERTMailer::inset2string(Buffer const &) const
229 return params2string(inset_.status());
233 void InsetERTMailer::string2params(string const & in,
234 InsetCollapsable::CollapseStatus & status)
236 status = InsetCollapsable::Collapsed;
240 istringstream data(in);
247 return print_mailer_error("InsetERTMailer", in, 1, name_);
252 status = static_cast<InsetCollapsable::CollapseStatus>(s);
257 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
260 data << name_ << ' ' << status;