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