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