]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Implement ForceLtR; cleanup of collapsable insets
[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         setLabelFont(layout_.labelfont);
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::resetParagraphsFont()
105 {
106         Font font(inherit_font, latex_language);
107         ParagraphList::iterator par = paragraphs().begin();
108         ParagraphList::iterator const end = paragraphs().end();
109         while (par != end) {
110                 par->resetFonts(font);
111                 par->params().clear();
112                 ++par;
113         }
114 }
115
116
117 void InsetERT::write(Buffer const & buf, ostream & os) const
118 {
119         os << "ERT" << "\n";
120         InsetCollapsable::write(buf, os);
121 }
122
123
124 docstring const InsetERT::editMessage() const
125 {
126         return _("Opened ERT Inset");
127 }
128
129
130 int InsetERT::latex(Buffer const & buf, odocstream & os,
131                     OutputParams const & op) const
132 {
133         return InsetCollapsable::latex(buf, os, op);
134 }
135
136
137 int InsetERT::plaintext(Buffer const &, odocstream &,
138                         OutputParams const &) const
139 {
140         return 0; // do not output TeX code
141 }
142
143
144 int InsetERT::docbook(Buffer const &, odocstream & os,
145                       OutputParams const &) const
146 {
147         // FIXME can we do the same thing here as for LaTeX?
148         ParagraphList::const_iterator par = paragraphs().begin();
149         ParagraphList::const_iterator end = paragraphs().end();
150
151         int lines = 0;
152         while (par != end) {
153                 pos_type siz = par->size();
154                 for (pos_type i = 0; i < siz; ++i)
155                         os.put(par->getChar(i));
156                 ++par;
157                 if (par != end) {
158                         os << "\n";
159                         ++lines;
160                 }
161         }
162
163         return lines;
164 }
165
166
167 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
168 {
169         BufferParams const & bp = cur.buffer().params();
170         LayoutPtr const layout =
171                         bp.getTextClass().defaultLayout();
172         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
173         switch (cmd.action) {
174
175         case LFUN_MOUSE_PRESS:
176                 if (cmd.button() != mouse_button::button3)
177                         InsetCollapsable::doDispatch(cur, cmd);
178                 else
179                         // This makes the cursor leave the
180                         // inset when it collapses on mouse-3
181                         cur.undispatched();
182                 break;
183
184         case LFUN_QUOTE_INSERT: {
185                 // We need to bypass the fancy quotes in Text
186                 FuncRequest f(LFUN_SELF_INSERT, "\"");
187                 dispatch(cur, f);
188                 break;
189         }
190         case LFUN_INSET_MODIFY: {
191                 InsetCollapsable::CollapseStatus st;
192                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
193                 setStatus(cur, st);
194                 break;
195         }
196         default:
197                 // Force any new text to latex_language
198                 // FIXME: This should only be necessary in init(), but
199                 // new paragraphs that are created by pressing enter at the
200                 // start of an existing paragraph get the buffer language
201                 // and not latex_language, so we take this brute force
202                 // approach.
203                 cur.current_font.fontInfo() = layout->font;
204                 cur.real_current_font.fontInfo() = layout->font;
205                 InsetCollapsable::doDispatch(cur, cmd);
206                 break;
207         }
208 }
209
210
211 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
212         FuncStatus & status) const
213 {
214         switch (cmd.action) {
215                 case LFUN_CLIPBOARD_PASTE:
216                 case LFUN_INSET_MODIFY:
217                 case LFUN_PASTE:
218                 case LFUN_PRIMARY_SELECTION_PASTE:
219                 case LFUN_QUOTE_INSERT:
220                         status.enabled(true);
221                         return true;
222
223                 // this one is difficult to get right. As a half-baked
224                 // solution, we consider only the first action of the sequence
225                 case LFUN_COMMAND_SEQUENCE: {
226                         // argument contains ';'-terminated commands
227                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
228                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
229                         func.origin = cmd.origin;
230                         return getStatus(cur, func, status);
231                 }
232
233                 default:
234                         return InsetCollapsable::getStatus(cur, cmd, status);
235         }
236 }
237
238
239 void InsetERT::setButtonLabel()
240 {
241         // FIXME UNICODE
242         if (decoration() == Classic)
243                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
244         else
245                 setLabel(getNewLabel(_("ERT")));
246 }
247
248
249 bool InsetERT::insetAllowed(InsetCode /* code */) const
250 {
251         return false;
252 }
253
254
255 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
256 {
257         FontInfo tmpfont = mi.base.font;
258         getDrawFont(mi.base.font);
259         mi.base.font.realize(tmpfont);
260         InsetCollapsable::metrics(mi, dim);
261         mi.base.font = tmpfont;
262 }
263
264
265 void InsetERT::draw(PainterInfo & pi, int x, int y) const
266 {
267         FontInfo tmpfont = pi.base.font;
268         getDrawFont(pi.base.font);
269         pi.base.font.realize(tmpfont);
270         const_cast<InsetERT &>(*this).setButtonLabel();
271         InsetCollapsable::draw(pi, x, y);
272         pi.base.font = tmpfont;
273 }
274
275
276 bool InsetERT::showInsetDialog(BufferView * bv) const
277 {
278         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
279         return true;
280 }
281
282
283 void InsetERT::getDrawFont(FontInfo & font) const
284 {
285         font = inherit_font;
286         font.realize(layout_.font);
287 }
288
289
290 string const InsetERTMailer::name_("ert");
291
292 InsetERTMailer::InsetERTMailer(InsetERT & inset)
293         : inset_(inset)
294 {}
295
296
297 string const InsetERTMailer::inset2string(Buffer const &) const
298 {
299         return params2string(inset_.status());
300 }
301
302
303 void InsetERTMailer::string2params(string const & in,
304                                    InsetCollapsable::CollapseStatus & status)
305 {
306         status = InsetCollapsable::Collapsed;
307         if (in.empty())
308                 return;
309
310         istringstream data(in);
311         Lexer lex(0,0);
312         lex.setStream(data);
313
314         string name;
315         lex >> name;
316         if (name != name_)
317                 return print_mailer_error("InsetERTMailer", in, 1, name_);
318
319         int s;
320         lex >> s;
321         if (lex)
322                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
323 }
324
325
326 string const
327 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
328 {
329         ostringstream data;
330         data << name_ << ' ' << status;
331         return data.str();
332 }
333
334
335 } // namespace lyx