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