]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
rename priv_dispatch to doDispatch
[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 * l, string const & contents, CollapseStatus status)
83         : InsetCollapsable(bp, status)
84 {
85         LyXFont font(LyXFont::ALL_INHERIT, l);
86         string::const_iterator cit = contents.begin();
87         string::const_iterator end = contents.end();
88         pos_type pos = 0;
89         for (; cit != end; ++cit)
90                 paragraphs().begin()->insertChar(pos++, *cit, font);
91
92         // the init has to be after the initialization of the paragraph
93         // because of the label settings (draw_label for ert insets).
94         init();
95 }
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 string const InsetERT::editMessage() const
112 {
113         return _("Opened ERT Inset");
114 }
115
116
117 int InsetERT::latex(Buffer const &, ostream & os,
118                     OutputParams const &) const
119 {
120         ParagraphList::const_iterator par = paragraphs().begin();
121         ParagraphList::const_iterator end = paragraphs().end();
122
123         int lines = 0;
124         while (par != end) {
125                 pos_type siz = par->size();
126                 for (pos_type i = 0; i < siz; ++i) {
127                         // ignore all struck out text
128                         if (isDeletedText(*par, i))
129                                 continue;
130
131                         if (par->isNewline(i)) {
132                                 os << '\n';
133                                 ++lines;
134                         } else {
135                                 os << par->getChar(i);
136                         }
137                 }
138                 ++par;
139                 if (par != end) {
140                         os << "\n";
141                         ++lines;
142                 }
143         }
144
145         return lines;
146 }
147
148
149 int InsetERT::plaintext(Buffer const &, ostream &,
150                     OutputParams const & /*runparams*/) const
151 {
152         return 0;
153 }
154
155
156 int InsetERT::linuxdoc(Buffer const &, ostream & os,
157                        OutputParams const &) const
158 {
159         ParagraphList::const_iterator par = paragraphs().begin();
160         ParagraphList::const_iterator end = paragraphs().end();
161
162         int lines = 0;
163         while (par != end) {
164                 pos_type siz = par->size();
165                 for (pos_type i = 0; i < siz; ++i) {
166                         if (par->isNewline(i)) {
167                                 os << '\n';
168                                 ++lines;
169                         } else {
170                                 os << par->getChar(i);
171                         }
172                 }
173                 ++par;
174                 if (par != end) {
175                         os << "\n";
176                         lines ++;
177                 }
178         }
179
180         return lines;
181 }
182
183
184 int InsetERT::docbook(Buffer const &, ostream & os,
185                       OutputParams const &) const
186 {
187         ParagraphList::const_iterator par = paragraphs().begin();
188         ParagraphList::const_iterator end = paragraphs().end();
189
190         int lines = 0;
191         while (par != end) {
192                 pos_type siz = par->size();
193                 for (pos_type i = 0; i < siz; ++i) {
194                         if (par->isNewline(i)) {
195                                 os << '\n';
196                                 ++lines;
197                         } else {
198                                 os << par->getChar(i);
199                         }
200                 }
201                 ++par;
202                 if (par != end) {
203                         os << "\n";
204                         ++lines;
205                 }
206         }
207
208         return lines;
209 }
210
211
212 void InsetERT::doDispatch(LCursor & cur, FuncRequest & cmd)
213 {
214         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
215         switch (cmd.action) {
216
217         case LFUN_INSET_MODIFY: {
218                 InsetCollapsable::CollapseStatus st;
219                 InsetERTMailer::string2params(cmd.argument, st);
220                 setStatus(st);
221                 break;
222         }
223
224         default:
225                 InsetCollapsable::doDispatch(cur, cmd);
226                 break;
227         }
228 }
229
230
231 bool InsetERT::getStatus(LCursor & cur, FuncRequest const & cmd,
232         FuncStatus & status) const
233 {
234         switch (cmd.action) {
235                 // suppress these
236                 case LFUN_LAYOUT:
237                 case LFUN_BOLD:
238                 case LFUN_CODE:
239                 case LFUN_DEFAULT:
240                 case LFUN_EMPH:
241                 case LFUN_FREEFONT_APPLY:
242                 case LFUN_FREEFONT_UPDATE:
243                 case LFUN_NOUN:
244                 case LFUN_ROMAN:
245                 case LFUN_SANS:
246                 case LFUN_FRAK:
247                 case LFUN_ITAL:
248                 case LFUN_FONT_SIZE:
249                 case LFUN_FONT_STATE:
250                 case LFUN_UNDERLINE:
251                         status.enabled(false);
252                         return true;
253
254                 default:
255                         return InsetCollapsable::getStatus(cur, cmd, status);
256                 }
257 }
258
259
260 void InsetERT::setButtonLabel()
261 {
262         setLabel(status() == Collapsed ? getNewLabel(_("P-ERT")) : _("P-ERT"));
263 }
264
265
266 bool InsetERT::insetAllowed(InsetOld::Code code) const
267 {
268         return code == InsetOld::NEWLINE_CODE;
269 }
270
271
272 void InsetERT::metrics(MetricsInfo & mi, Dimension & dim) const
273 {
274         LyXFont tmpfont = mi.base.font;
275         getDrawFont(mi.base.font);
276         InsetCollapsable::metrics(mi, dim);
277         mi.base.font = tmpfont;
278         dim_ = dim;
279 }
280
281
282 void InsetERT::draw(PainterInfo & pi, int x, int y) const
283 {
284         LyXFont tmpfont = pi.base.font;
285         getDrawFont(pi.base.font);
286         InsetCollapsable::draw(pi, x, y);
287         pi.base.font = tmpfont;
288 }
289
290
291 bool InsetERT::showInsetDialog(BufferView * bv) const
292 {
293         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
294         return true;
295 }
296
297
298 void InsetERT::getDrawFont(LyXFont & font) const
299 {
300         font = LyXFont(LyXFont::ALL_INHERIT, latex_language);
301         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
302         font.setColor(LColor::latex);
303 }
304
305
306 string const InsetERTMailer::name_("ert");
307
308 InsetERTMailer::InsetERTMailer(InsetERT & inset)
309         : inset_(inset)
310 {}
311
312
313 string const InsetERTMailer::inset2string(Buffer const &) const
314 {
315         return params2string(inset_.status());
316 }
317
318
319 void InsetERTMailer::string2params(string const & in,
320                                    InsetCollapsable::CollapseStatus & status)
321 {
322         status = InsetCollapsable::Collapsed;
323         if (in.empty())
324                 return;
325
326         istringstream data(in);
327         LyXLex lex(0,0);
328         lex.setStream(data);
329
330         string name;
331         lex >> name;
332         if (name != name_)
333                 return print_mailer_error("InsetERTMailer", in, 1, name_);
334
335         int s;
336         lex >> s;
337         if (lex)
338                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
339 }
340
341
342 string const
343 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
344 {
345         ostringstream data;
346         data << name_ << ' ' << status;
347         return data.str();
348 }