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)
68 InsetERT::InsetERT(InsetERT const & in)
69 : InsetCollapsable(in)
75 Inset * InsetERT::clone() const
77 return new InsetERT(*this);
82 InsetERT::InsetERT(BufferParams const & bp,
83 Language const *, string const & contents, CollapseStatus status)
84 : InsetCollapsable(bp, status)
86 Font font(FONT_INHERIT, latex_language);
87 paragraphs().begin()->insert(0, contents, font);
89 // the init has to be after the initialization of the paragraph
90 // because of the label settings (draw_label for ert insets).
98 InsetERTMailer(*this).hideDialog();
102 void InsetERT::write(Buffer const & buf, ostream & os) const
105 InsetCollapsable::write(buf, os);
109 docstring const InsetERT::editMessage() const
111 return _("Opened ERT Inset");
115 int InsetERT::latex(Buffer const & buf, odocstream & os,
116 OutputParams const & op) const
118 return InsetCollapsable::latex(buf, os, op);
122 int InsetERT::plaintext(Buffer const &, odocstream &,
123 OutputParams const &) const
125 return 0; // do not output TeX code
129 int InsetERT::docbook(Buffer const &, odocstream & os,
130 OutputParams const &) const
132 // FIXME can we do the same thing here as for LaTeX?
133 ParagraphList::const_iterator par = paragraphs().begin();
134 ParagraphList::const_iterator end = paragraphs().end();
138 pos_type siz = par->size();
139 for (pos_type i = 0; i < siz; ++i)
140 os.put(par->getChar(i));
152 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
154 BufferParams const & bp = cur.buffer().params();
155 LayoutPtr const layout =
156 bp.getTextClass().defaultLayout();
157 //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
158 switch (cmd.action) {
160 case LFUN_MOUSE_PRESS:
161 if (cmd.button() != mouse_button::button3)
162 InsetCollapsable::doDispatch(cur, cmd);
164 // This makes the cursor leave the
165 // inset when it collapses on mouse-3
169 case LFUN_QUOTE_INSERT: {
170 // We need to bypass the fancy quotes in Text
171 FuncRequest f(LFUN_SELF_INSERT, "\"");
175 case LFUN_INSET_MODIFY: {
176 InsetCollapsable::CollapseStatus st;
177 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
182 // Force any new text to latex_language
183 // FIXME: This should only be necessary in init(), but
184 // new paragraphs that are created by pressing enter at the
185 // start of an existing paragraph get the buffer language
186 // and not latex_language, so we take this brute force
188 cur.current_font.fontInfo() = layout->font;
189 cur.real_current_font.fontInfo() = layout->font;
190 InsetCollapsable::doDispatch(cur, cmd);
196 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
197 FuncStatus & status) const
199 switch (cmd.action) {
200 case LFUN_CLIPBOARD_PASTE:
201 case LFUN_INSET_MODIFY:
203 case LFUN_PRIMARY_SELECTION_PASTE:
204 case LFUN_QUOTE_INSERT:
205 status.enabled(true);
208 // this one is difficult to get right. As a half-baked
209 // solution, we consider only the first action of the sequence
210 case LFUN_COMMAND_SEQUENCE: {
211 // argument contains ';'-terminated commands
212 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
213 FuncRequest func(lyxaction.lookupFunc(firstcmd));
214 func.origin = cmd.origin;
215 return getStatus(cur, func, status);
219 return InsetCollapsable::getStatus(cur, cmd, status);
224 void InsetERT::setButtonLabel()
227 if (decoration() == Classic)
228 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
230 setLabel(getNewLabel(_("ERT")));
234 bool InsetERT::insetAllowed(InsetCode /* code */) const
240 void InsetERT::draw(PainterInfo & pi, int x, int y) const
242 const_cast<InsetERT &>(*this).setButtonLabel();
243 InsetCollapsable::draw(pi, x, y);
247 bool InsetERT::showInsetDialog(BufferView * bv) const
249 InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
254 string const InsetERTMailer::name_("ert");
256 InsetERTMailer::InsetERTMailer(InsetERT & inset)
261 string const InsetERTMailer::inset2string(Buffer const &) const
263 return params2string(inset_.status());
267 void InsetERTMailer::string2params(string const & in,
268 InsetCollapsable::CollapseStatus & status)
270 status = InsetCollapsable::Collapsed;
274 istringstream data(in);
281 return print_mailer_error("InsetERTMailer", in, 1, name_);
286 status = static_cast<InsetCollapsable::CollapseStatus>(s);
291 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
294 data << name_ << ' ' << status;