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