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