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(Buffer const & buf, CollapseStatus status)
47 : InsetCollapsable(buf, status)
53 InsetERTMailer(*this).hideDialog();
57 void InsetERT::write(ostream & os) const
60 InsetCollapsable::write(os);
64 docstring InsetERT::editMessage() const
66 return _("Opened ERT Inset");
70 int InsetERT::latex(odocstream & os, OutputParams const & op) const
72 return InsetCollapsable::latex(os, op);
76 int InsetERT::plaintext(odocstream &, OutputParams const &) const
78 return 0; // do not output TeX code
82 int InsetERT::docbook(odocstream & os, OutputParams const &) const
84 // FIXME can we do the same thing here as for LaTeX?
85 ParagraphList::const_iterator par = paragraphs().begin();
86 ParagraphList::const_iterator end = paragraphs().end();
90 pos_type siz = par->size();
91 for (pos_type i = 0; i < siz; ++i)
92 os.put(par->getChar(i));
104 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
106 BufferParams const & bp = cur.buffer().params();
107 LayoutPtr const layout = bp.documentClass().emptyLayout();
108 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
109 switch (cmd.action) {
111 case LFUN_MOUSE_PRESS:
112 if (cmd.button() != mouse_button::button3)
113 InsetCollapsable::doDispatch(cur, cmd);
115 // This makes the cursor leave the
116 // inset when it collapses on mouse-3
120 case LFUN_QUOTE_INSERT: {
121 // We need to bypass the fancy quotes in Text
122 FuncRequest f(LFUN_SELF_INSERT, "\"");
126 case LFUN_INSET_MODIFY: {
127 InsetCollapsable::CollapseStatus st;
128 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
133 // Force any new text to latex_language
134 // FIXME: This should not be necessary but
135 // new paragraphs that are created by pressing enter at the
136 // start of an existing paragraph get the buffer language
137 // and not latex_language, so we take this brute force
139 cur.current_font.fontInfo() = layout->font;
140 cur.real_current_font.fontInfo() = layout->font;
141 InsetCollapsable::doDispatch(cur, cmd);
147 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
148 FuncStatus & status) const
150 switch (cmd.action) {
151 case LFUN_CLIPBOARD_PASTE:
152 case LFUN_INSET_MODIFY:
154 case LFUN_PRIMARY_SELECTION_PASTE:
155 case LFUN_QUOTE_INSERT:
156 status.enabled(true);
159 // this one is difficult to get right. As a half-baked
160 // solution, we consider only the first action of the sequence
161 case LFUN_COMMAND_SEQUENCE: {
162 // argument contains ';'-terminated commands
163 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
164 FuncRequest func(lyxaction.lookupFunc(firstcmd));
165 func.origin = cmd.origin;
166 return getStatus(cur, func, status);
170 return InsetCollapsable::getStatus(cur, cmd, status);
175 void InsetERT::setButtonLabel()
177 if (decoration() == InsetLayout::Classic)
178 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
180 setLabel(getNewLabel(_("ERT")));
184 bool InsetERT::insetAllowed(InsetCode /* code */) const
190 void InsetERT::draw(PainterInfo & pi, int x, int y) const
192 const_cast<InsetERT &>(*this).setButtonLabel();
193 InsetCollapsable::draw(pi, x, y);
197 bool InsetERT::showInsetDialog(BufferView * bv) const
199 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
204 string const InsetERTMailer::name_("ert");
206 InsetERTMailer::InsetERTMailer(InsetERT & inset)
211 string const InsetERTMailer::inset2string(Buffer const &) const
213 return params2string(inset_.status());
217 void InsetERTMailer::string2params(string const & in,
218 InsetCollapsable::CollapseStatus & status)
220 status = InsetCollapsable::Collapsed;
224 istringstream data(in);
231 return print_mailer_error("InsetERTMailer", in, 1, name_);
236 status = static_cast<InsetCollapsable::CollapseStatus>(s);
241 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
244 data << name_ << ' ' << status;