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