]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
CoordBranch merge
[lyx.git] / src / insets / insetert.C
1 /**
2  * \file insetert.C
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 "debug.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
23 #include "gettext.h"
24 #include "language.h"
25 #include "LColor.h"
26 #include "lyxlex.h"
27 #include "metricsinfo.h"
28 #include "paragraph.h"
29
30 #include "frontends/Alert.h"
31 #include "frontends/LyXView.h"
32
33 #include <sstream>
34
35 using lyx::pos_type;
36
37 using std::endl;
38 using std::min;
39
40 using std::auto_ptr;
41 using std::istringstream;
42 using std::ostream;
43 using std::ostringstream;
44 using std::string;
45
46
47 void InsetERT::init()
48 {
49         setButtonLabel();
50
51         LyXFont font(LyXFont::ALL_SANE);
52         font.decSize();
53         font.decSize();
54         font.setColor(LColor::latex);
55         setLabelFont(font);
56
57         setInsetName("ERT");
58 }
59
60
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62         : InsetCollapsable(bp, status)
63 {
64         init();
65 }
66
67
68 InsetERT::InsetERT(InsetERT const & in)
69         : InsetCollapsable(in)
70 {
71         init();
72 }
73
74
75 auto_ptr<InsetBase> InsetERT::doClone() const
76 {
77         return auto_ptr<InsetBase>(new InsetERT(*this));
78 }
79
80
81 InsetERT::InsetERT(BufferParams const & bp,
82                    Language const *, string const & contents, CollapseStatus status)
83         : InsetCollapsable(bp, status)
84 {
85         //LyXFont font(LyXFont::ALL_INHERIT, lang);
86         LyXFont font;
87         getDrawFont(font);
88         string::const_iterator cit = contents.begin();
89         string::const_iterator end = contents.end();
90         pos_type pos = 0;
91         for (; cit != end; ++cit)
92                 paragraphs().begin()->insertChar(pos++, *cit, font);
93
94         // the init has to be after the initialization of the paragraph
95         // because of the label settings (draw_label for ert insets).
96         init();
97 }
98
99
100 InsetERT::~InsetERT()
101 {
102         InsetERTMailer(*this).hideDialog();
103 }
104
105
106 void InsetERT::write(Buffer const & buf, ostream & os) const
107 {
108         os << "ERT" << "\n";
109         InsetCollapsable::write(buf, os);
110 }
111
112
113 string const InsetERT::editMessage() const
114 {
115         return _("Opened ERT Inset");
116 }
117
118
119 int InsetERT::latex(Buffer const &, ostream & os,
120                     OutputParams const &) const
121 {
122         ParagraphList::const_iterator par = paragraphs().begin();
123         ParagraphList::const_iterator end = paragraphs().end();
124
125         int lines = 0;
126         while (par != end) {
127                 pos_type siz = par->size();
128                 for (pos_type i = 0; i < siz; ++i) {
129                         // ignore all struck out text
130                         if (isDeletedText(*par, i))
131                                 continue;
132
133                         if (par->isNewline(i)) {
134                                 os << '\n';
135                                 ++lines;
136                         } else {
137                                 os << par->getChar(i);
138                         }
139                 }
140                 ++par;
141                 if (par != end) {
142                         os << "\n";
143                         ++lines;
144                 }
145         }
146
147         return lines;
148 }
149
150
151 int InsetERT::plaintext(Buffer const &, ostream &,
152                     OutputParams const & /*runparams*/) const
153 {
154         return 0;
155 }
156
157
158 int InsetERT::linuxdoc(Buffer const &, ostream & os,
159                        OutputParams const &) const
160 {
161         ParagraphList::const_iterator par = paragraphs().begin();
162         ParagraphList::const_iterator end = paragraphs().end();
163
164         int lines = 0;
165         while (par != end) {
166                 pos_type siz = par->size();
167                 for (pos_type i = 0; i < siz; ++i) {
168                         if (par->isNewline(i)) {
169                                 os << '\n';
170                                 ++lines;
171                         } else {
172                                 os << par->getChar(i);
173                         }
174                 }
175                 ++par;
176                 if (par != end) {
177                         os << "\n";
178                         lines ++;
179                 }
180         }
181
182         return lines;
183 }
184
185
186 int InsetERT::docbook(Buffer const &, ostream & os,
187                       OutputParams const &) const
188 {
189         ParagraphList::const_iterator par = paragraphs().begin();
190         ParagraphList::const_iterator end = paragraphs().end();
191
192         int lines = 0;
193         while (par != end) {
194                 pos_type siz = par->size();
195                 for (pos_type i = 0; i < siz; ++i) {
196                         if (par->isNewline(i)) {
197                                 os << '\n';
198                                 ++lines;
199                         } else {
200                                 os << par->getChar(i);
201                         }
202                 }
203                 ++par;
204                 if (par != end) {
205                         os << "\n";
206                         ++lines;
207                 }
208         }
209
210         return lines;
211 }
212
213
214 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
215 {
216         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
217         switch (cmd.action) {
218
219         case LFUN_INSET_MODIFY: {
220                 InsetCollapsable::CollapseStatus st;
221                 InsetERTMailer::string2params(cmd.argument, st);
222                 setStatus(st);
223                 break;
224         }
225
226         default:
227                 InsetCollapsable::doDispatch(cur, cmd);
228                 break;
229         }
230 }
231
232
233 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
234         FuncStatus & status) const
235 {
236         switch (cmd.action) {
237                 // suppress these
238                 case LFUN_LAYOUT:
239                 case LFUN_BOLD:
240                 case LFUN_CODE:
241                 case LFUN_DEFAULT:
242                 case LFUN_EMPH:
243                 case LFUN_FREEFONT_APPLY:
244                 case LFUN_FREEFONT_UPDATE:
245                 case LFUN_NOUN:
246                 case LFUN_ROMAN:
247                 case LFUN_SANS:
248                 case LFUN_FRAK:
249                 case LFUN_ITAL:
250                 case LFUN_FONT_SIZE:
251                 case LFUN_FONT_STATE:
252                 case LFUN_UNDERLINE:
253                         status.enabled(false);
254                         return true;
255
256                 default:
257                         return InsetCollapsable::getStatus(cur, cmd, status);
258                 }
259 }
260
261
262 void InsetERT::setButtonLabel()
263 {
264         setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
265 }
266
267
268 bool InsetERT::insetAllowed(InsetBase::Code code) const
269 {
270         return code == InsetBase::NEWLINE_CODE;
271 }
272
273
274 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
275 {
276         LyXFont tmpfont = mi.base.font;
277         getDrawFont(mi.base.font);
278         InsetCollapsable::metrics(mi, dim);
279         mi.base.font = tmpfont;
280         dim_ = dim;
281 }
282
283
284 void InsetERT::draw(PainterInfo & pi, int x, int y) const
285 {
286         LyXFont tmpfont = pi.base.font;
287         getDrawFont(pi.base.font);
288         InsetCollapsable::draw(pi, x, y);
289         pi.base.font = tmpfont;
290 }
291
292
293 bool InsetERT::showInsetDialog(BufferView * bv) const
294 {
295         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
296         return true;
297 }
298
299
300 void InsetERT::getDrawFont(LyXFont & font) const
301 {
302         font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
303         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
304         font.setColor(LColor::latex);
305 }
306
307
308 string const InsetERTMailer::name_("ert");
309
310 InsetERTMailer::InsetERTMailer(InsetERT & inset)
311         : inset_(inset)
312 {}
313
314
315 string const InsetERTMailer::inset2string(Buffer const &) const
316 {
317         return params2string(inset_.status());
318 }
319
320
321 void InsetERTMailer::string2params(string const & in,
322                                    InsetCollapsable::CollapseStatus & status)
323 {
324         status = InsetCollapsable::Collapsed;
325         if (in.empty())
326                 return;
327
328         istringstream data(in);
329         LyXLex lex(0,0);
330         lex.setStream(data);
331
332         string name;
333         lex >> name;
334         if (name != name_)
335                 return print_mailer_error("InsetERTMailer", in, 1, name_);
336
337         int s;
338         lex >> s;
339         if (lex)
340                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
341 }
342
343
344 string const
345 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
346 {
347         ostringstream data;
348         data << name_ << ' ' << status;
349         return data.str();
350 }