]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
change "support/std_sstream.h" to <sstream>
[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 <sstream>
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                 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::const_iterator par = paragraphs().begin();
120         ParagraphList::const_iterator end = 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::const_iterator par = paragraphs().begin();
159         ParagraphList::const_iterator end = 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::const_iterator par = paragraphs().begin();
187         ParagraphList::const_iterator end = 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 void InsetERT::priv_dispatch(LCursor & cur, FuncRequest & cmd)
212 {
213         //lyxerr << "\nInsetERT::priv_dispatch (begin): cmd: " << cmd << endl;
214         switch (cmd.action) {
215
216         case LFUN_INSET_MODIFY: {
217                 InsetCollapsable::CollapseStatus st;
218                 InsetERTMailer::string2params(cmd.argument, st);
219                 setStatus(st);
220                 break;
221         }
222
223         // suppress these
224         case LFUN_LAYOUT:
225         case LFUN_BOLD:
226         case LFUN_CODE:
227         case LFUN_DEFAULT:
228         case LFUN_EMPH:
229         case LFUN_FREEFONT_APPLY:
230         case LFUN_FREEFONT_UPDATE:
231         case LFUN_NOUN:
232         case LFUN_ROMAN:
233         case LFUN_SANS:
234         case LFUN_FRAK:
235         case LFUN_ITAL:
236         case LFUN_FONT_SIZE:
237         case LFUN_FONT_STATE:
238         case LFUN_UNDERLINE:
239                 break;
240
241         default:
242                 InsetCollapsable::priv_dispatch(cur, cmd);
243                 break;
244         }
245 }
246
247
248 void InsetERT::setButtonLabel()
249 {
250         setLabel(status() == Collapsed ? getNewLabel(_("ERT")) : _("ERT"));
251 }
252
253
254 bool InsetERT::insetAllowed(InsetOld::Code code) const
255 {
256         return code == InsetOld::NEWLINE_CODE;
257 }
258
259
260 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
261 {
262         LyXFont tmpfont = mi.base.font;
263         getDrawFont(mi.base.font);
264         InsetCollapsable::metrics(mi, dim);
265         mi.base.font = tmpfont;
266         dim_ = dim;
267 }
268
269
270 void InsetERT::draw(PainterInfo & pi, int x, int y) const
271 {
272         LyXFont tmpfont = pi.base.font;
273         getDrawFont(pi.base.font);
274         InsetCollapsable::draw(pi, x, y);
275         pi.base.font = tmpfont;
276 }
277
278
279 bool InsetERT::showInsetDialog(BufferView * bv) const
280 {
281         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
282         return true;
283 }
284
285
286 void InsetERT::getDrawFont(LyXFont & font) const
287 {
288         font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
289         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
290         font.setColor(LColor::latex);
291 }
292
293
294 string const InsetERTMailer::name_("ert");
295
296 InsetERTMailer::InsetERTMailer(InsetERT & inset)
297         : inset_(inset)
298 {}
299
300
301 string const InsetERTMailer::inset2string(Buffer const &) const
302 {
303         return params2string(inset_.status());
304 }
305
306
307 void InsetERTMailer::string2params(string const & in,
308                                    InsetCollapsable::CollapseStatus & status)
309 {
310         status = InsetCollapsable::Collapsed;
311         if (in.empty())
312                 return;
313
314         istringstream data(in);
315         LyXLex lex(0,0);
316         lex.setStream(data);
317
318         string name;
319         lex >> name;
320         if (name != name_)
321                 return print_mailer_error("InsetERTMailer", in, 1, name_);
322
323         int s;
324         lex >> s;
325         if (lex)
326                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
327 }
328
329
330 string const
331 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
332 {
333         ostringstream data;
334         data << name_ << ' ' << status;
335         return data.str();
336 }