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"
33 #include "frontends/Application.h"
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
42 using namespace lyx::support;
46 InsetERT::InsetERT(Buffer const & buf, CollapseStatus status)
47 : InsetCollapsable(buf, status)
54 theApp()->hideDialogs("ert", this);
58 void InsetERT::write(ostream & os) const
61 InsetCollapsable::write(os);
65 docstring InsetERT::editMessage() const
67 return _("Opened ERT Inset");
71 int InsetERT::latex(odocstream & os, OutputParams const & op) const
73 return InsetCollapsable::latex(os, op);
77 int InsetERT::plaintext(odocstream &, OutputParams const &) const
79 return 0; // do not output TeX code
83 int InsetERT::docbook(odocstream & os, OutputParams const &) const
85 // FIXME can we do the same thing here as for LaTeX?
86 ParagraphList::const_iterator par = paragraphs().begin();
87 ParagraphList::const_iterator end = paragraphs().end();
91 pos_type siz = par->size();
92 for (pos_type i = 0; i < siz; ++i)
93 os.put(par->getChar(i));
105 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
107 BufferParams const & bp = cur.buffer().params();
108 Layout const layout = bp.documentClass().emptyLayout();
109 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
110 switch (cmd.action) {
111 case LFUN_QUOTE_INSERT: {
112 // We need to bypass the fancy quotes in Text
113 FuncRequest f(LFUN_SELF_INSERT, "\"");
117 case LFUN_INSET_MODIFY: {
118 InsetCollapsable::CollapseStatus st;
119 InsetERT::string2params(to_utf8(cmd.argument()), st);
124 // Force any new text to latex_language
125 // FIXME: This should not be necessary but
126 // new paragraphs that are created by pressing enter at the
127 // start of an existing paragraph get the buffer language
128 // and not latex_language, so we take this brute force
130 cur.current_font.fontInfo() = layout.font;
131 cur.real_current_font.fontInfo() = layout.font;
132 InsetCollapsable::doDispatch(cur, cmd);
138 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
139 FuncStatus & status) const
141 switch (cmd.action) {
142 case LFUN_CLIPBOARD_PASTE:
143 case LFUN_INSET_MODIFY:
145 case LFUN_PRIMARY_SELECTION_PASTE:
146 case LFUN_QUOTE_INSERT:
147 status.enabled(true);
150 // this one is difficult to get right. As a half-baked
151 // solution, we consider only the first action of the sequence
152 case LFUN_COMMAND_SEQUENCE: {
153 // argument contains ';'-terminated commands
154 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
155 FuncRequest func(lyxaction.lookupFunc(firstcmd));
156 func.origin = cmd.origin;
157 return getStatus(cur, func, status);
161 return InsetCollapsable::getStatus(cur, cmd, status);
166 void InsetERT::setButtonLabel()
168 if (decoration() == InsetLayout::Classic)
169 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
171 setLabel(getNewLabel(_("ERT")));
175 bool InsetERT::insetAllowed(InsetCode /* code */) const
181 void InsetERT::draw(PainterInfo & pi, int x, int y) const
183 const_cast<InsetERT &>(*this).setButtonLabel();
184 InsetCollapsable::draw(pi, x, y);
188 bool InsetERT::showInsetDialog(BufferView * bv) const
190 bv->showDialog("ert", params2string(status()),
191 const_cast<InsetERT *>(this));
196 void InsetERT::string2params(string const & in, CollapseStatus & status)
198 status = InsetCollapsable::Collapsed;
202 istringstream data(in);
209 LYXERR0("InsetERT::string2params(" << in << ")\n"
210 "Expected arg 1 to be \"ert\"\n");
217 status = static_cast<InsetCollapsable::CollapseStatus>(s);
221 string InsetERT::params2string(CollapseStatus status)
224 data << "ert" << ' ' << status;