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"
36 #include "support/lstrings.h"
48 using std::istringstream;
50 using std::ostringstream;
54 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
55 : InsetCollapsable(bp, status)
59 InsetERT::InsetERT(InsetERT const & in)
60 : InsetCollapsable(in)
64 Inset * InsetERT::clone() const
66 return new InsetERT(*this);
72 InsetERTMailer(*this).hideDialog();
76 void InsetERT::write(Buffer const & buf, ostream & os) const
79 InsetCollapsable::write(buf, os);
83 docstring const InsetERT::editMessage() const
85 return _("Opened ERT Inset");
89 int InsetERT::latex(Buffer const & buf, odocstream & os,
90 OutputParams const & op) const
92 return InsetCollapsable::latex(buf, os, op);
96 int InsetERT::plaintext(Buffer const &, odocstream &,
97 OutputParams const &) const
99 return 0; // do not output TeX code
103 int InsetERT::docbook(Buffer const &, odocstream & os,
104 OutputParams const &) const
106 // FIXME can we do the same thing here as for LaTeX?
107 ParagraphList::const_iterator par = paragraphs().begin();
108 ParagraphList::const_iterator end = paragraphs().end();
112 pos_type siz = par->size();
113 for (pos_type i = 0; i < siz; ++i)
114 os.put(par->getChar(i));
126 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
128 BufferParams const & bp = cur.buffer().params();
129 LayoutPtr const layout =
130 bp.getTextClass().defaultLayout();
131 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
132 switch (cmd.action) {
134 case LFUN_MOUSE_PRESS:
135 if (cmd.button() != mouse_button::button3)
136 InsetCollapsable::doDispatch(cur, cmd);
138 // This makes the cursor leave the
139 // inset when it collapses on mouse-3
143 case LFUN_QUOTE_INSERT: {
144 // We need to bypass the fancy quotes in Text
145 FuncRequest f(LFUN_SELF_INSERT, "\"");
149 case LFUN_INSET_MODIFY: {
150 InsetCollapsable::CollapseStatus st;
151 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
156 // Force any new text to latex_language
157 // FIXME: This should not be necessary but
158 // new paragraphs that are created by pressing enter at the
159 // start of an existing paragraph get the buffer language
160 // and not latex_language, so we take this brute force
162 cur.current_font.fontInfo() = layout->font;
163 cur.real_current_font.fontInfo() = layout->font;
164 InsetCollapsable::doDispatch(cur, cmd);
170 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
171 FuncStatus & status) const
173 switch (cmd.action) {
174 case LFUN_CLIPBOARD_PASTE:
175 case LFUN_INSET_MODIFY:
177 case LFUN_PRIMARY_SELECTION_PASTE:
178 case LFUN_QUOTE_INSERT:
179 status.enabled(true);
182 // this one is difficult to get right. As a half-baked
183 // solution, we consider only the first action of the sequence
184 case LFUN_COMMAND_SEQUENCE: {
185 // argument contains ';'-terminated commands
186 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
187 FuncRequest func(lyxaction.lookupFunc(firstcmd));
188 func.origin = cmd.origin;
189 return getStatus(cur, func, status);
193 return InsetCollapsable::getStatus(cur, cmd, status);
198 void InsetERT::setButtonLabel()
200 if (decoration() == Classic)
201 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
203 setLabel(getNewLabel(_("ERT")));
207 bool InsetERT::insetAllowed(InsetCode /* code */) const
213 void InsetERT::draw(PainterInfo & pi, int x, int y) const
215 const_cast<InsetERT &>(*this).setButtonLabel();
216 InsetCollapsable::draw(pi, x, y);
220 bool InsetERT::showInsetDialog(BufferView * bv) const
222 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
227 string const InsetERTMailer::name_("ert");
229 InsetERTMailer::InsetERTMailer(InsetERT & inset)
234 string const InsetERTMailer::inset2string(Buffer const &) const
236 return params2string(inset_.status());
240 void InsetERTMailer::string2params(string const & in,
241 InsetCollapsable::CollapseStatus & status)
243 status = InsetCollapsable::Collapsed;
247 istringstream data(in);
254 return print_mailer_error("InsetERTMailer", in, 1, name_);
259 status = static_cast<InsetCollapsable::CollapseStatus>(s);
264 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
267 data << name_ << ' ' << status;