]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
2f98b603da5d2befe3762339ca229e17089948b4
[lyx.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "Language.h"
26 #include "Layout.h"
27 #include "LyXAction.h"
28 #include "Lexer.h"
29 #include "TextClass.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "Paragraph.h"
33
34 #include "frontends/alert.h"
35
36 #include "support/lstrings.h"
37
38 #include <sstream>
39
40
41 namespace lyx {
42
43 using support::token;
44
45 using std::endl;
46 using std::min;
47
48 using std::istringstream;
49 using std::ostream;
50 using std::ostringstream;
51 using std::string;
52
53
54 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
55         : InsetCollapsable(bp, status)
56 {}
57
58
59 InsetERT::InsetERT(InsetERT const & in)
60         : InsetCollapsable(in)
61 {}
62
63
64 Inset * InsetERT::clone() const
65 {
66         return new InsetERT(*this);
67 }
68
69
70 InsetERT::~InsetERT()
71 {
72         InsetERTMailer(*this).hideDialog();
73 }
74
75
76 void InsetERT::write(Buffer const & buf, ostream & os) const
77 {
78         os << "ERT" << "\n";
79         InsetCollapsable::write(buf, os);
80 }
81
82
83 docstring const InsetERT::editMessage() const
84 {
85         return _("Opened ERT Inset");
86 }
87
88
89 int InsetERT::latex(Buffer const & buf, odocstream & os,
90                     OutputParams const & op) const
91 {
92         return InsetCollapsable::latex(buf, os, op);
93 }
94
95
96 int InsetERT::plaintext(Buffer const &, odocstream &,
97                         OutputParams const &) const
98 {
99         return 0; // do not output TeX code
100 }
101
102
103 int InsetERT::docbook(Buffer const &, odocstream & os,
104                       OutputParams const &) const
105 {
106         // FIXME can we do the same thing here as for LaTeX?
107         ParagraphList::const_iterator par = paragraphs().begin();
108         ParagraphList::const_iterator end = paragraphs().end();
109
110         int lines = 0;
111         while (par != end) {
112                 pos_type siz = par->size();
113                 for (pos_type i = 0; i < siz; ++i)
114                         os.put(par->getChar(i));
115                 ++par;
116                 if (par != end) {
117                         os << "\n";
118                         ++lines;
119                 }
120         }
121
122         return lines;
123 }
124
125
126 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
127 {
128         BufferParams const & bp = cur.buffer().params();
129         LayoutPtr const layout =
130                         bp.getTextClass().defaultLayout();
131         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
132         switch (cmd.action) {
133
134         case LFUN_MOUSE_PRESS:
135                 if (cmd.button() != mouse_button::button3)
136                         InsetCollapsable::doDispatch(cur, cmd);
137                 else
138                         // This makes the cursor leave the
139                         // inset when it collapses on mouse-3
140                         cur.undispatched();
141                 break;
142
143         case LFUN_QUOTE_INSERT: {
144                 // We need to bypass the fancy quotes in Text
145                 FuncRequest f(LFUN_SELF_INSERT, "\"");
146                 dispatch(cur, f);
147                 break;
148         }
149         case LFUN_INSET_MODIFY: {
150                 InsetCollapsable::CollapseStatus st;
151                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
152                 setStatus(cur, st);
153                 break;
154         }
155         default:
156                 // Force any new text to latex_language
157                 // FIXME: This should not be necessary but
158                 // new paragraphs that are created by pressing enter at the
159                 // start of an existing paragraph get the buffer language
160                 // and not latex_language, so we take this brute force
161                 // approach.
162                 cur.current_font.fontInfo() = layout->font;
163                 cur.real_current_font.fontInfo() = layout->font;
164                 InsetCollapsable::doDispatch(cur, cmd);
165                 break;
166         }
167 }
168
169
170 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
171         FuncStatus & status) const
172 {
173         switch (cmd.action) {
174                 case LFUN_CLIPBOARD_PASTE:
175                 case LFUN_INSET_MODIFY:
176                 case LFUN_PASTE:
177                 case LFUN_PRIMARY_SELECTION_PASTE:
178                 case LFUN_QUOTE_INSERT:
179                         status.enabled(true);
180                         return true;
181
182                 // this one is difficult to get right. As a half-baked
183                 // solution, we consider only the first action of the sequence
184                 case LFUN_COMMAND_SEQUENCE: {
185                         // argument contains ';'-terminated commands
186                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
187                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
188                         func.origin = cmd.origin;
189                         return getStatus(cur, func, status);
190                 }
191
192                 default:
193                         return InsetCollapsable::getStatus(cur, cmd, status);
194         }
195 }
196
197
198 void InsetERT::setButtonLabel()
199 {
200         if (decoration() == Classic)
201                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
202         else
203                 setLabel(getNewLabel(_("ERT")));
204 }
205
206
207 bool InsetERT::insetAllowed(InsetCode /* code */) const
208 {
209         return false;
210 }
211
212
213 void InsetERT::draw(PainterInfo & pi, int x, int y) const
214 {
215         const_cast<InsetERT &>(*this).setButtonLabel();
216         InsetCollapsable::draw(pi, x, y);
217 }
218
219
220 bool InsetERT::showInsetDialog(BufferView * bv) const
221 {
222         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
223         return true;
224 }
225
226
227 string const InsetERTMailer::name_("ert");
228
229 InsetERTMailer::InsetERTMailer(InsetERT & inset)
230         : inset_(inset)
231 {}
232
233
234 string const InsetERTMailer::inset2string(Buffer const &) const
235 {
236         return params2string(inset_.status());
237 }
238
239
240 void InsetERTMailer::string2params(string const & in,
241                                    InsetCollapsable::CollapseStatus & status)
242 {
243         status = InsetCollapsable::Collapsed;
244         if (in.empty())
245                 return;
246
247         istringstream data(in);
248         Lexer lex(0,0);
249         lex.setStream(data);
250
251         string name;
252         lex >> name;
253         if (name != name_)
254                 return print_mailer_error("InsetERTMailer", in, 1, name_);
255
256         int s;
257         lex >> s;
258         if (lex)
259                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
260 }
261
262
263 string const
264 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
265 {
266         ostringstream data;
267         data << name_ << ' ' << status;
268         return data.str();
269 }
270
271
272 } // namespace lyx