]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
Fix InsetBox drawing and remove remnant of the wide() hack.
[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 InsetText::InsetText(BufferParams const & bp)
78         : drawFrame_(false), frame_color_(Color::insetframe)
79 {
80         paragraphs().push_back(Paragraph());
81         paragraphs().back().layout(bp.getTextClass().defaultLayout());
82         init();
83 }
84
85
86 InsetText::InsetText(InsetText const & in)
87         : Inset(in), text_()
88 {
89         text_.autoBreakRows_ = in.text_.autoBreakRows_;
90         drawFrame_ = in.drawFrame_;
91         frame_color_ = in.frame_color_;
92         text_.paragraphs() = in.text_.paragraphs();
93         init();
94 }
95
96
97 InsetText::InsetText()
98 {}
99
100
101 void InsetText::init()
102 {
103         for_each(paragraphs().begin(), paragraphs().end(),
104                  bind(&Paragraph::setInsetOwner, _1, this));
105 }
106
107
108 void InsetText::clear()
109 {
110         ParagraphList & pars = paragraphs();
111
112         // This is a gross hack...
113         LayoutPtr old_layout = pars.begin()->layout();
114
115         pars.clear();
116         pars.push_back(Paragraph());
117         pars.begin()->setInsetOwner(this);
118         pars.begin()->layout(old_layout);
119 }
120
121
122 Inset * InsetText::clone() const
123 {
124         return new InsetText(*this);
125 }
126
127
128 Dimension const InsetText::dimension(BufferView const & bv) const
129 {
130         TextMetrics const & tm = bv.textMetrics(&text_);
131         Dimension dim = tm.dimension();
132         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
133         dim.des += TEXT_TO_INSET_OFFSET;
134         dim.asc += TEXT_TO_INSET_OFFSET;
135         return dim;
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
175         // Hand font through to contained lyxtext:
176         tm.font_ = mi.base.font;
177         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
178         if (hasFixedWidth())
179                 tm.metrics(mi, dim, mi.base.textwidth);
180         else
181                 tm.metrics(mi, dim);
182         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
183         dim.asc += TEXT_TO_INSET_OFFSET;
184         dim.des += TEXT_TO_INSET_OFFSET;
185         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
186         bool const changed = dim_ != dim;
187         dim_ = dim;
188         return changed;
189 }
190
191
192 void InsetText::draw(PainterInfo & pi, int x, int y) const
193 {
194         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
195
196         if (drawFrame_ || pi.full_repaint) {
197                 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
198                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
199                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
200                 if (pi.full_repaint)
201                         pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
202                 if (drawFrame_)
203                         pi.pain.rectangle(x, yframe, w, h, frameColor());
204         }
205         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
206 }
207
208
209 docstring const InsetText::editMessage() const
210 {
211         return _("Opened Text Inset");
212 }
213
214
215 void InsetText::edit(Cursor & cur, bool left)
216 {
217         //lyxerr << "InsetText: edit left/right" << endl;
218         int const pit = left ? 0 : paragraphs().size() - 1;
219         int const pos = left ? 0 : paragraphs().back().size();
220         text_.setCursor(cur.top(), pit, pos);
221         cur.clearSelection();
222         finishUndo();
223 }
224
225
226 Inset * InsetText::editXY(Cursor & cur, int x, int y)
227 {
228         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
229 }
230
231
232 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
233 {
234         LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
235                              << " [ cmd.action = "
236                              << cmd.action << ']' << endl;
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, paragraphs(), 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 Color_color InsetText::frameColor() const
372 {
373         return Color::color(frame_color_);
374 }
375
376
377 void InsetText::setFrameColor(Color_color 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                        std::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