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