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"
46 using std::istringstream;
48 using std::ostringstream;
55 // FIXME: what to do with those?
56 //text_.current_font.setLanguage(latex_language);
57 //text_.real_current_font.setLanguage(latex_language);
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62 : InsetCollapsable(bp, status)
69 InsetERT::InsetERT(InsetERT const & in)
70 : InsetCollapsable(in)
76 Inset * InsetERT::clone() const
78 return new InsetERT(*this);
83 InsetERT::InsetERT(BufferParams const & bp,
84 Language const *, string const & contents, CollapseStatus status)
85 : InsetCollapsable(bp, status)
87 Font font(FONT_INHERIT, latex_language);
88 paragraphs().begin()->insert(0, contents, font);
90 // the init has to be after the initialization of the paragraph
91 // because of the label settings (draw_label for ert insets).
99 InsetERTMailer(*this).hideDialog();
103 void InsetERT::write(Buffer const & buf, ostream & os) const
106 InsetCollapsable::write(buf, os);
110 docstring const InsetERT::editMessage() const
112 return _("Opened ERT Inset");
116 int InsetERT::latex(Buffer const & buf, odocstream & os,
117 OutputParams const & op) const
119 return InsetCollapsable::latex(buf, os, op);
123 int InsetERT::plaintext(Buffer const &, odocstream &,
124 OutputParams const &) const
126 return 0; // do not output TeX code
130 int InsetERT::docbook(Buffer const &, odocstream & os,
131 OutputParams const &) const
133 // FIXME can we do the same thing here as for LaTeX?
134 ParagraphList::const_iterator par = paragraphs().begin();
135 ParagraphList::const_iterator end = paragraphs().end();
139 pos_type siz = par->size();
140 for (pos_type i = 0; i < siz; ++i)
141 os.put(par->getChar(i));
153 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
155 BufferParams const & bp = cur.buffer().params();
156 LayoutPtr const layout =
157 bp.getTextClass().defaultLayout();
158 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
159 switch (cmd.action) {
161 case LFUN_MOUSE_PRESS:
162 if (cmd.button() != mouse_button::button3)
163 InsetCollapsable::doDispatch(cur, cmd);
165 // This makes the cursor leave the
166 // inset when it collapses on mouse-3
170 case LFUN_QUOTE_INSERT: {
171 // We need to bypass the fancy quotes in Text
172 FuncRequest f(LFUN_SELF_INSERT, "\"");
176 case LFUN_INSET_MODIFY: {
177 InsetCollapsable::CollapseStatus st;
178 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
183 // Force any new text to latex_language
184 // FIXME: This should only be necessary in init(), but
185 // new paragraphs that are created by pressing enter at the
186 // start of an existing paragraph get the buffer language
187 // and not latex_language, so we take this brute force
189 cur.current_font.fontInfo() = layout->font;
190 cur.real_current_font.fontInfo() = layout->font;
191 InsetCollapsable::doDispatch(cur, cmd);
197 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
198 FuncStatus & status) const
200 switch (cmd.action) {
201 case LFUN_CLIPBOARD_PASTE:
202 case LFUN_INSET_MODIFY:
204 case LFUN_PRIMARY_SELECTION_PASTE:
205 case LFUN_QUOTE_INSERT:
206 status.enabled(true);
209 // this one is difficult to get right. As a half-baked
210 // solution, we consider only the first action of the sequence
211 case LFUN_COMMAND_SEQUENCE: {
212 // argument contains ';'-terminated commands
213 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
214 FuncRequest func(lyxaction.lookupFunc(firstcmd));
215 func.origin = cmd.origin;
216 return getStatus(cur, func, status);
220 return InsetCollapsable::getStatus(cur, cmd, status);
225 void InsetERT::setButtonLabel()
228 if (decoration() == Classic)
229 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
231 setLabel(getNewLabel(_("ERT")));
235 bool InsetERT::insetAllowed(InsetCode /* code */) const
241 void InsetERT::draw(PainterInfo & pi, int x, int y) const
243 const_cast<InsetERT &>(*this).setButtonLabel();
244 InsetCollapsable::draw(pi, x, y);
248 bool InsetERT::showInsetDialog(BufferView * bv) const
250 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
255 string const InsetERTMailer::name_("ert");
257 InsetERTMailer::InsetERTMailer(InsetERT & inset)
262 string const InsetERTMailer::inset2string(Buffer const &) const
264 return params2string(inset_.status());
268 void InsetERTMailer::string2params(string const & in,
269 InsetCollapsable::CollapseStatus & status)
271 status = InsetCollapsable::Collapsed;
275 istringstream data(in);
282 return print_mailer_error("InsetERTMailer", in, 1, name_);
287 status = static_cast<InsetCollapsable::CollapseStatus>(s);
292 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
295 data << name_ << ' ' << status;