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