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