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 "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
25 #include "LyXAction.h"
27 #include "TextClass.h"
28 #include "MetricsInfo.h"
29 #include "ParagraphParameters.h"
30 #include "Paragraph.h"
32 #include "frontends/alert.h"
34 #include "support/debug.h"
35 #include "support/gettext.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(ostream & os) const
71 InsetCollapsable::write(os);
75 docstring InsetERT::editMessage() const
77 return _("Opened ERT Inset");
81 int InsetERT::latex(odocstream & os, OutputParams const & op) const
83 return InsetCollapsable::latex(os, op);
87 int InsetERT::plaintext(odocstream &, OutputParams const &) const
89 return 0; // do not output TeX code
93 int InsetERT::docbook(odocstream & os, OutputParams const &) const
95 // FIXME can we do the same thing here as for LaTeX?
96 ParagraphList::const_iterator par = paragraphs().begin();
97 ParagraphList::const_iterator end = paragraphs().end();
101 pos_type siz = par->size();
102 for (pos_type i = 0; i < siz; ++i)
103 os.put(par->getChar(i));
115 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
117 BufferParams const & bp = cur.buffer().params();
118 LayoutPtr const layout =
119 bp.textClass().emptyLayout();
120 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
121 switch (cmd.action) {
123 case LFUN_MOUSE_PRESS:
124 if (cmd.button() != mouse_button::button3)
125 InsetCollapsable::doDispatch(cur, cmd);
127 // This makes the cursor leave the
128 // inset when it collapses on mouse-3
132 case LFUN_QUOTE_INSERT: {
133 // We need to bypass the fancy quotes in Text
134 FuncRequest f(LFUN_SELF_INSERT, "\"");
138 case LFUN_INSET_MODIFY: {
139 InsetCollapsable::CollapseStatus st;
140 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
145 // Force any new text to latex_language
146 // FIXME: This should not be necessary but
147 // new paragraphs that are created by pressing enter at the
148 // start of an existing paragraph get the buffer language
149 // and not latex_language, so we take this brute force
151 cur.current_font.fontInfo() = layout->font;
152 cur.real_current_font.fontInfo() = layout->font;
153 InsetCollapsable::doDispatch(cur, cmd);
159 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
160 FuncStatus & status) const
162 switch (cmd.action) {
163 case LFUN_CLIPBOARD_PASTE:
164 case LFUN_INSET_MODIFY:
166 case LFUN_PRIMARY_SELECTION_PASTE:
167 case LFUN_QUOTE_INSERT:
168 status.enabled(true);
171 // this one is difficult to get right. As a half-baked
172 // solution, we consider only the first action of the sequence
173 case LFUN_COMMAND_SEQUENCE: {
174 // argument contains ';'-terminated commands
175 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
176 FuncRequest func(lyxaction.lookupFunc(firstcmd));
177 func.origin = cmd.origin;
178 return getStatus(cur, func, status);
182 return InsetCollapsable::getStatus(cur, cmd, status);
187 void InsetERT::setButtonLabel()
189 if (decoration() == InsetLayout::Classic)
190 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
192 setLabel(getNewLabel(_("ERT")));
196 bool InsetERT::insetAllowed(InsetCode /* code */) const
202 void InsetERT::draw(PainterInfo & pi, int x, int y) const
204 const_cast<InsetERT &>(*this).setButtonLabel();
205 InsetCollapsable::draw(pi, x, y);
209 bool InsetERT::showInsetDialog(BufferView * bv) const
211 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
216 string const InsetERTMailer::name_("ert");
218 InsetERTMailer::InsetERTMailer(InsetERT & inset)
223 string const InsetERTMailer::inset2string(Buffer const &) const
225 return params2string(inset_.status());
229 void InsetERTMailer::string2params(string const & in,
230 InsetCollapsable::CollapseStatus & status)
232 status = InsetCollapsable::Collapsed;
236 istringstream data(in);
243 return print_mailer_error("InsetERTMailer", in, 1, name_);
248 status = static_cast<InsetCollapsable::CollapseStatus>(s);
253 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
256 data << name_ << ' ' << status;