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