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