]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Further cleanup of InsetFlex, InsetCollapsable and InsetLayout:
[features.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         // FIXME UNICODE
199         if (decoration() == Classic)
200                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
201         else
202                 setLabel(getNewLabel(_("ERT")));
203 }
204
205
206 bool InsetERT::insetAllowed(InsetCode /* code */) const
207 {
208         return false;
209 }
210
211
212 void InsetERT::draw(PainterInfo & pi, int x, int y) const
213 {
214         const_cast<InsetERT &>(*this).setButtonLabel();
215         InsetCollapsable::draw(pi, x, y);
216 }
217
218
219 bool InsetERT::showInsetDialog(BufferView * bv) const
220 {
221         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
222         return true;
223 }
224
225
226 string const InsetERTMailer::name_("ert");
227
228 InsetERTMailer::InsetERTMailer(InsetERT & inset)
229         : inset_(inset)
230 {}
231
232
233 string const InsetERTMailer::inset2string(Buffer const &) const
234 {
235         return params2string(inset_.status());
236 }
237
238
239 void InsetERTMailer::string2params(string const & in,
240                                    InsetCollapsable::CollapseStatus & status)
241 {
242         status = InsetCollapsable::Collapsed;
243         if (in.empty())
244                 return;
245
246         istringstream data(in);
247         Lexer lex(0,0);
248         lex.setStream(data);
249
250         string name;
251         lex >> name;
252         if (name != name_)
253                 return print_mailer_error("InsetERTMailer", in, 1, name_);
254
255         int s;
256         lex >> s;
257         if (lex)
258                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
259 }
260
261
262 string const
263 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
264 {
265         ostringstream data;
266         data << name_ << ' ' << status;
267         return data.str();
268 }
269
270
271 } // namespace lyx