]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
simplification
[lyx.git] / src / insets / InsetText.cpp
1 /**
2  * \file InsetText.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetText.h"
14 #include "InsetNewline.h"
15
16 #include "Buffer.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
21 #include "CutAndPaste.h"
22 #include "Cursor.h"
23 #include "debug.h"
24 #include "DispatchResult.h"
25 #include "ErrorList.h"
26 #include "FuncRequest.h"
27 #include "gettext.h"
28 #include "InsetList.h"
29 #include "Intl.h"
30 #include "lyxfind.h"
31 #include "Lexer.h"
32 #include "LyXRC.h"
33 #include "Text.h"
34 #include "MetricsInfo.h"
35 #include "OutputParams.h"
36 #include "output_docbook.h"
37 #include "output_latex.h"
38 #include "output_plaintext.h"
39 #include "Paragraph.h"
40 #include "paragraph_funcs.h"
41 #include "ParagraphParameters.h"
42 #include "ParIterator.h"
43 #include "Row.h"
44 #include "sgml.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "TexRow.h"
48
49 #include "frontends/alert.h"
50 #include "frontends/Painter.h"
51
52 #include "support/lstrings.h"
53
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
56
57
58 namespace lyx {
59
60 using graphics::PreviewLoader;
61
62 using support::isStrUnsignedInt;
63
64 using boost::bind;
65 using boost::ref;
66
67 using std::endl;
68 using std::for_each;
69 using std::max;
70 using std::string;
71 using std::ostream;
72 using std::vector;
73
74
75 InsetText::InsetText(BufferParams const & bp)
76         : drawFrame_(false), frame_color_(Color_insetframe)
77 {
78         paragraphs().push_back(Paragraph());
79         paragraphs().back().layout(bp.getTextClass().defaultLayout());
80         init();
81 }
82
83
84 InsetText::InsetText(InsetText const & in)
85         : Inset(in), text_()
86 {
87         text_.autoBreakRows_ = in.text_.autoBreakRows_;
88         drawFrame_ = in.drawFrame_;
89         frame_color_ = in.frame_color_;
90         text_.paragraphs() = in.text_.paragraphs();
91         init();
92 }
93
94
95 InsetText::InsetText()
96 {}
97
98
99 void InsetText::init()
100 {
101         for_each(paragraphs().begin(), paragraphs().end(),
102                  bind(&Paragraph::setInsetOwner, _1, this));
103 }
104
105
106 void InsetText::clear()
107 {
108         ParagraphList & pars = paragraphs();
109
110         // This is a gross hack...
111         LayoutPtr old_layout = pars.begin()->layout();
112
113         pars.clear();
114         pars.push_back(Paragraph());
115         pars.begin()->setInsetOwner(this);
116         pars.begin()->layout(old_layout);
117 }
118
119
120 Inset * InsetText::clone() const
121 {
122         return new InsetText(*this);
123 }
124
125
126 Dimension const InsetText::dimension(BufferView const & bv) const
127 {
128         TextMetrics const & tm = bv.textMetrics(&text_);
129         Dimension dim = tm.dimension();
130         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
131         dim.des += TEXT_TO_INSET_OFFSET;
132         dim.asc += TEXT_TO_INSET_OFFSET;
133         return dim;
134 }
135
136
137 void InsetText::write(Buffer const & buf, ostream & os) const
138 {
139         os << "Text\n";
140         text_.write(buf, os);
141 }
142
143
144 void InsetText::read(Buffer const & buf, Lexer & lex)
145 {
146         clear();
147
148         // delete the initial paragraph
149         Paragraph oldpar = *paragraphs().begin();
150         paragraphs().clear();
151         ErrorList errorList;
152         bool res = text_.read(buf, lex, errorList);
153         init();
154
155         if (!res) {
156                 lex.printError("Missing \\end_inset at this point. "
157                                            "Read: `$$Token'");
158         }
159
160         // sanity check
161         // ensure we have at least one paragraph.
162         if (paragraphs().empty())
163                 paragraphs().push_back(oldpar);
164 }
165
166
167 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
168 {
169         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
170
171         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
172
173         // Hand font through to contained lyxtext:
174         tm.font_.fontInfo() = mi.base.font;
175         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
176         if (hasFixedWidth())
177                 tm.metrics(mi, dim, mi.base.textwidth);
178         else
179                 tm.metrics(mi, dim);
180         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
181         dim.asc += TEXT_TO_INSET_OFFSET;
182         dim.des += TEXT_TO_INSET_OFFSET;
183         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
184 }
185
186
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
188 {
189         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
190
191         if (drawFrame_ || pi.full_repaint) {
192                 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
193                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
194                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
195                 if (pi.full_repaint)
196                         pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
197                 if (drawFrame_)
198                         pi.pain.rectangle(x, yframe, w, h, frameColor());
199         }
200         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
201 }
202
203
204 docstring const InsetText::editMessage() const
205 {
206         return _("Opened Text Inset");
207 }
208
209
210 void InsetText::edit(Cursor & cur, bool left)
211 {
212         //lyxerr << "InsetText: edit left/right" << endl;
213         int const pit = left ? 0 : paragraphs().size() - 1;
214         int const pos = left ? 0 : paragraphs().back().size();
215         text_.setCursor(cur.top(), pit, pos);
216         cur.clearSelection();
217         cur.finishUndo();
218 }
219
220
221 Inset * InsetText::editXY(Cursor & cur, int x, int y)
222 {
223         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
224 }
225
226
227 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
228 {
229         LYXERR(Debug::ACTION, "InsetText::doDispatch()"
230                 << " [ cmd.action = " << cmd.action << ']');
231         text_.dispatch(cur, cmd);
232 }
233
234
235 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
236         FuncStatus & status) const
237 {
238         return text_.getStatus(cur, cmd, status);
239 }
240
241
242 void InsetText::setChange(Change const & change)
243 {
244         ParagraphList::iterator pit = paragraphs().begin();
245         ParagraphList::iterator end = paragraphs().end();
246         for (; pit != end; ++pit) {
247                 pit->setChange(change);
248         }
249 }
250
251
252 void InsetText::acceptChanges(BufferParams const & bparams)
253 {
254         text_.acceptChanges(bparams);
255 }
256
257
258 void InsetText::rejectChanges(BufferParams const & bparams)
259 {
260         text_.rejectChanges(bparams);
261 }
262
263
264 int InsetText::latex(Buffer const & buf, odocstream & os,
265                      OutputParams const & runparams) const
266 {
267         TexRow texrow;
268         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
269         return texrow.rows();
270 }
271
272
273 int InsetText::plaintext(Buffer const & buf, odocstream & os,
274                          OutputParams const & runparams) const
275 {
276         ParagraphList::const_iterator beg = paragraphs().begin();
277         ParagraphList::const_iterator end = paragraphs().end();
278         ParagraphList::const_iterator it = beg;
279         bool ref_printed = false;
280         int len = 0;
281         for (; it != end; ++it) {
282                 if (it != beg) {
283                         os << '\n';
284                         if (runparams.linelen > 0)
285                                 os << '\n';
286                 }
287                 odocstringstream oss;
288                 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
289                 docstring const str = oss.str();
290                 os << str;
291                 // FIXME: len is not computed fully correctly; in principle,
292                 // we have to count the characters after the last '\n'
293                 len = str.size();
294         }
295
296         return len;
297 }
298
299
300 int InsetText::docbook(Buffer const & buf, odocstream & os,
301                        OutputParams const & runparams) const
302 {
303         docbookParagraphs(paragraphs(), buf, os, runparams);
304         return 0;
305 }
306
307
308 void InsetText::validate(LaTeXFeatures & features) const
309 {
310         for_each(paragraphs().begin(), paragraphs().end(),
311                  bind(&Paragraph::validate, _1, ref(features)));
312 }
313
314
315 void InsetText::cursorPos(BufferView const & bv,
316                 CursorSlice const & sl, bool boundary, int & x, int & y) const
317 {
318         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
319         y = bv.textMetrics(&text_).cursorY(sl, boundary);
320 }
321
322
323 bool InsetText::showInsetDialog(BufferView *) const
324 {
325         return false;
326 }
327
328
329 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
330 {
331         clear();
332         Paragraph & first = paragraphs().front();
333         for (unsigned int i = 0; i < data.length(); ++i)
334                 first.insertChar(i, data[i], font, trackChanges);
335 }
336
337
338 void InsetText::setAutoBreakRows(bool flag)
339 {
340         if (flag == text_.autoBreakRows_)
341                 return;
342
343         text_.autoBreakRows_ = flag;
344         if (flag)
345                 return;
346
347         // remove previously existing newlines
348         ParagraphList::iterator it = paragraphs().begin();
349         ParagraphList::iterator end = paragraphs().end();
350         for (; it != end; ++it)
351                 for (int i = 0; i < it->size(); ++i)
352                         if (it->isNewline(i))
353                                 // do not track the change, because the user
354                                 // is not allowed to revert/reject it
355                                 it->eraseChar(i, false);
356 }
357
358
359 void InsetText::setDrawFrame(bool flag)
360 {
361         drawFrame_ = flag;
362 }
363
364
365 ColorCode InsetText::frameColor() const
366 {
367         return frame_color_;
368 }
369
370
371 void InsetText::setFrameColor(ColorCode col)
372 {
373         frame_color_ = col;
374 }
375
376
377 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
378 {
379         // There is little we can do here to keep track of changes.
380         // As of 2006/10/20, appendParagraphs is used exclusively by
381         // LyXTabular::setMultiColumn. In this context, the paragraph break
382         // is lost irreversibly and the appended text doesn't really change
383
384         ParagraphList & pl = paragraphs();
385
386         ParagraphList::iterator pit = plist.begin();
387         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
388         ++pit;
389         mergeParagraph(buffer->params(), pl,
390                        std::distance(pl.begin(), ins) - 1);
391
392         for_each(pit, plist.end(),
393                  bind(&ParagraphList::push_back, ref(pl), _1));
394 }
395
396
397 void InsetText::addPreview(PreviewLoader & loader) const
398 {
399         ParagraphList::const_iterator pit = paragraphs().begin();
400         ParagraphList::const_iterator pend = paragraphs().end();
401
402         for (; pit != pend; ++pit) {
403                 InsetList::const_iterator it  = pit->insetList().begin();
404                 InsetList::const_iterator end = pit->insetList().end();
405                 for (; it != end; ++it)
406                         it->inset->addPreview(loader);
407         }
408 }
409
410
411 //FIXME: instead of this hack, which only works by chance,
412 // cells should have their own insetcell type, which returns CELL_CODE!
413 bool InsetText::neverIndent(Buffer const & buffer) const
414 {
415         // this is only true for tabular cells
416         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
417 }
418
419
420 ParagraphList const & InsetText::paragraphs() const
421 {
422         return text_.paragraphs();
423 }
424
425
426 ParagraphList & InsetText::paragraphs()
427 {
428         return text_.paragraphs();
429 }
430
431
432 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
433 {
434         ParIterator it2 = it;
435         it2.forwardPos();
436         BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
437         lyx::updateLabels(buf, it2);
438 }
439
440
441 } // namespace lyx