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