]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
* src/paragraph_funcs.cpp (breakParagraph): change parameter 'flag' to
[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 void 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 }
187
188
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 {
191         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
192
193         if (drawFrame_ || pi.full_repaint) {
194                 int const w = tm.width() + 2 * TEXT_TO_INSET_OFFSET;
195                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
196                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
197                 if (pi.full_repaint)
198                         pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
199                 if (drawFrame_)
200                         pi.pain.rectangle(x, yframe, w, h, frameColor());
201         }
202         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
203 }
204
205
206 docstring const InsetText::editMessage() const
207 {
208         return _("Opened Text Inset");
209 }
210
211
212 void InsetText::edit(Cursor & cur, bool left)
213 {
214         //lyxerr << "InsetText: edit left/right" << endl;
215         int const pit = left ? 0 : paragraphs().size() - 1;
216         int const pos = left ? 0 : paragraphs().back().size();
217         text_.setCursor(cur.top(), pit, pos);
218         cur.clearSelection();
219         finishUndo();
220 }
221
222
223 Inset * InsetText::editXY(Cursor & cur, int x, int y)
224 {
225         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
226 }
227
228
229 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
230 {
231         LYXERR(Debug::ACTION) << BOOST_CURRENT_FUNCTION
232                              << " [ cmd.action = "
233                              << cmd.action << ']' << endl;
234         text_.dispatch(cur, cmd);
235 }
236
237
238 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
239         FuncStatus & status) const
240 {
241         return text_.getStatus(cur, cmd, status);
242 }
243
244
245 void InsetText::setChange(Change const & change)
246 {
247         ParagraphList::iterator pit = paragraphs().begin();
248         ParagraphList::iterator end = paragraphs().end();
249         for (; pit != end; ++pit) {
250                 pit->setChange(change);
251         }
252 }
253
254
255 void InsetText::acceptChanges(BufferParams const & bparams)
256 {
257         text_.acceptChanges(bparams);
258 }
259
260
261 void InsetText::rejectChanges(BufferParams const & bparams)
262 {
263         text_.rejectChanges(bparams);
264 }
265
266
267 int InsetText::latex(Buffer const & buf, odocstream & os,
268                      OutputParams const & runparams) const
269 {
270         TexRow texrow;
271         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
272         return texrow.rows();
273 }
274
275
276 int InsetText::plaintext(Buffer const & buf, odocstream & os,
277                          OutputParams const & runparams) const
278 {
279         ParagraphList::const_iterator beg = paragraphs().begin();
280         ParagraphList::const_iterator end = paragraphs().end();
281         ParagraphList::const_iterator it = beg;
282         bool ref_printed = false;
283         int len = 0;
284         for (; it != end; ++it) {
285                 if (it != beg) {
286                         os << '\n';
287                         if (runparams.linelen > 0)
288                                 os << '\n';
289                 }
290                 odocstringstream oss;
291                 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
292                 docstring const str = oss.str();
293                 os << str;
294                 // FIXME: len is not computed fully correctly; in principle,
295                 // we have to count the characters after the last '\n'
296                 len = str.size();
297         }
298
299         return len;
300 }
301
302
303 int InsetText::docbook(Buffer const & buf, odocstream & os,
304                        OutputParams const & runparams) const
305 {
306         docbookParagraphs(paragraphs(), buf, os, runparams);
307         return 0;
308 }
309
310
311 void InsetText::validate(LaTeXFeatures & features) const
312 {
313         for_each(paragraphs().begin(), paragraphs().end(),
314                  bind(&Paragraph::validate, _1, ref(features)));
315 }
316
317
318 void InsetText::cursorPos(BufferView const & bv,
319                 CursorSlice const & sl, bool boundary, int & x, int & y) const
320 {
321         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
322         y = bv.textMetrics(&text_).cursorY(sl, boundary);
323 }
324
325
326 bool InsetText::showInsetDialog(BufferView *) const
327 {
328         return false;
329 }
330
331
332 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
333 {
334         clear();
335         Paragraph & first = paragraphs().front();
336         for (unsigned int i = 0; i < data.length(); ++i)
337                 first.insertChar(i, data[i], font, trackChanges);
338 }
339
340
341 void InsetText::setAutoBreakRows(bool flag)
342 {
343         if (flag == text_.autoBreakRows_)
344                 return;
345
346         text_.autoBreakRows_ = flag;
347         if (flag)
348                 return;
349
350         // remove previously existing newlines
351         ParagraphList::iterator it = paragraphs().begin();
352         ParagraphList::iterator end = paragraphs().end();
353         for (; it != end; ++it)
354                 for (int i = 0; i < it->size(); ++i)
355                         if (it->isNewline(i))
356                                 // do not track the change, because the user
357                                 // is not allowed to revert/reject it
358                                 it->eraseChar(i, false);
359 }
360
361
362 void InsetText::setDrawFrame(bool flag)
363 {
364         drawFrame_ = flag;
365 }
366
367
368 Color_color InsetText::frameColor() const
369 {
370         return Color::color(frame_color_);
371 }
372
373
374 void InsetText::setFrameColor(Color_color col)
375 {
376         frame_color_ = col;
377 }
378
379
380 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
381 {
382         // There is little we can do here to keep track of changes.
383         // As of 2006/10/20, appendParagraphs is used exclusively by
384         // LyXTabular::setMultiColumn. In this context, the paragraph break
385         // is lost irreversibly and the appended text doesn't really change
386
387         ParagraphList & pl = paragraphs();
388
389         ParagraphList::iterator pit = plist.begin();
390         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
391         ++pit;
392         mergeParagraph(buffer->params(), pl,
393                        std::distance(pl.begin(), ins) - 1);
394
395         for_each(pit, plist.end(),
396                  bind(&ParagraphList::push_back, ref(pl), _1));
397 }
398
399
400 void InsetText::addPreview(PreviewLoader & loader) const
401 {
402         ParagraphList::const_iterator pit = paragraphs().begin();
403         ParagraphList::const_iterator pend = paragraphs().end();
404
405         for (; pit != pend; ++pit) {
406                 InsetList::const_iterator it  = pit->insetlist.begin();
407                 InsetList::const_iterator end = pit->insetlist.end();
408                 for (; it != end; ++it)
409                         it->inset->addPreview(loader);
410         }
411 }
412
413
414 //FIXME: instead of this hack, which only works by chance,
415 // cells should have their own insetcell type, which returns CELL_CODE!
416 bool InsetText::neverIndent(Buffer const & buffer) const
417 {
418         // this is only true for tabular cells
419         return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
420 }
421
422
423 ParagraphList const & InsetText::paragraphs() const
424 {
425         return text_.paragraphs();
426 }
427
428
429 ParagraphList & InsetText::paragraphs()
430 {
431         return text_.paragraphs();
432 }
433
434
435 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
436 {
437         ParIterator it2 = it;
438         it2.forwardPos();
439         BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
440         lyx::updateLabels(buf, it2);
441 }
442
443
444 } // namespace lyx