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