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