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