]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
pimpl not needed here
[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 void InsetERT::init()
53 {
54         setButtonLabel();
55         // FIXME: what to do with those?
56         //text_.current_font.setLanguage(latex_language);
57         //text_.real_current_font.setLanguage(latex_language);
58 }
59
60
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62         : InsetCollapsable(bp, status)
63 {
64         init();
65 }
66
67
68 InsetERT::InsetERT(InsetERT const & in)
69         : InsetCollapsable(in)
70 {
71         init();
72 }
73
74
75 Inset * InsetERT::clone() const
76 {
77         return new InsetERT(*this);
78 }
79
80
81 #if 0
82 InsetERT::InsetERT(BufferParams const & bp,
83                    Language const *, string const & contents, CollapseStatus status)
84         : InsetCollapsable(bp, status)
85 {
86         Font font(FONT_INHERIT, latex_language);
87         paragraphs().begin()->insert(0, contents, font);
88
89         // the init has to be after the initialization of the paragraph
90         // because of the label settings (draw_label for ert insets).
91         init();
92 }
93 #endif
94
95
96 InsetERT::~InsetERT()
97 {
98         InsetERTMailer(*this).hideDialog();
99 }
100
101
102 void InsetERT::write(Buffer const & buf, ostream & os) const
103 {
104         os << "ERT" << "\n";
105         InsetCollapsable::write(buf, os);
106 }
107
108
109 docstring const InsetERT::editMessage() const
110 {
111         return _("Opened ERT Inset");
112 }
113
114
115 int InsetERT::latex(Buffer const & buf, odocstream & os,
116                     OutputParams const & op) const
117 {
118         return InsetCollapsable::latex(buf, os, op);
119 }
120
121
122 int InsetERT::plaintext(Buffer const &, odocstream &,
123                         OutputParams const &) const
124 {
125         return 0; // do not output TeX code
126 }
127
128
129 int InsetERT::docbook(Buffer const &, odocstream & os,
130                       OutputParams const &) const
131 {
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();
135
136         int lines = 0;
137         while (par != end) {
138                 pos_type siz = par->size();
139                 for (pos_type i = 0; i < siz; ++i)
140                         os.put(par->getChar(i));
141                 ++par;
142                 if (par != end) {
143                         os << "\n";
144                         ++lines;
145                 }
146         }
147
148         return lines;
149 }
150
151
152 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
153 {
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) {
159
160         case LFUN_MOUSE_PRESS:
161                 if (cmd.button() != mouse_button::button3)
162                         InsetCollapsable::doDispatch(cur, cmd);
163                 else
164                         // This makes the cursor leave the
165                         // inset when it collapses on mouse-3
166                         cur.undispatched();
167                 break;
168
169         case LFUN_QUOTE_INSERT: {
170                 // We need to bypass the fancy quotes in Text
171                 FuncRequest f(LFUN_SELF_INSERT, "\"");
172                 dispatch(cur, f);
173                 break;
174         }
175         case LFUN_INSET_MODIFY: {
176                 InsetCollapsable::CollapseStatus st;
177                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
178                 setStatus(cur, st);
179                 break;
180         }
181         default:
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
187                 // approach.
188                 cur.current_font.fontInfo() = layout->font;
189                 cur.real_current_font.fontInfo() = layout->font;
190                 InsetCollapsable::doDispatch(cur, cmd);
191                 break;
192         }
193 }
194
195
196 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
197         FuncStatus & status) const
198 {
199         switch (cmd.action) {
200                 case LFUN_CLIPBOARD_PASTE:
201                 case LFUN_INSET_MODIFY:
202                 case LFUN_PASTE:
203                 case LFUN_PRIMARY_SELECTION_PASTE:
204                 case LFUN_QUOTE_INSERT:
205                         status.enabled(true);
206                         return true;
207
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);
216                 }
217
218                 default:
219                         return InsetCollapsable::getStatus(cur, cmd, status);
220         }
221 }
222
223
224 void InsetERT::setButtonLabel()
225 {
226         // FIXME UNICODE
227         if (decoration() == Classic)
228                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
229         else
230                 setLabel(getNewLabel(_("ERT")));
231 }
232
233
234 bool InsetERT::insetAllowed(InsetCode /* code */) const
235 {
236         return false;
237 }
238
239
240 void InsetERT::draw(PainterInfo & pi, int x, int y) const
241 {
242         const_cast<InsetERT &>(*this).setButtonLabel();
243         InsetCollapsable::draw(pi, x, y);
244 }
245
246
247 bool InsetERT::showInsetDialog(BufferView * bv) const
248 {
249         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
250         return true;
251 }
252
253
254 string const InsetERTMailer::name_("ert");
255
256 InsetERTMailer::InsetERTMailer(InsetERT & inset)
257         : inset_(inset)
258 {}
259
260
261 string const InsetERTMailer::inset2string(Buffer const &) const
262 {
263         return params2string(inset_.status());
264 }
265
266
267 void InsetERTMailer::string2params(string const & in,
268                                    InsetCollapsable::CollapseStatus & status)
269 {
270         status = InsetCollapsable::Collapsed;
271         if (in.empty())
272                 return;
273
274         istringstream data(in);
275         Lexer lex(0,0);
276         lex.setStream(data);
277
278         string name;
279         lex >> name;
280         if (name != name_)
281                 return print_mailer_error("InsetERTMailer", in, 1, name_);
282
283         int s;
284         lex >> s;
285         if (lex)
286                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
287 }
288
289
290 string const
291 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
292 {
293         ostringstream data;
294         data << name_ << ' ' << status;
295         return data.str();
296 }
297
298
299 } // namespace lyx