]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
more border tweaks
[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
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CompletionList.h"
20 #include "CoordCache.h"
21 #include "Cursor.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "InsetList.h"
27 #include "Intl.h"
28 #include "Lexer.h"
29 #include "lyxfind.h"
30 #include "LyXRC.h"
31 #include "MetricsInfo.h"
32 #include "output_docbook.h"
33 #include "output_latex.h"
34 #include "OutputParams.h"
35 #include "output_plaintext.h"
36 #include "paragraph_funcs.h"
37 #include "Paragraph.h"
38 #include "ParagraphParameters.h"
39 #include "ParIterator.h"
40 #include "Row.h"
41 #include "sgml.h"
42 #include "TexRow.h"
43 #include "TextClass.h"
44 #include "Text.h"
45 #include "TextMetrics.h"
46
47 #include "frontends/alert.h"
48 #include "frontends/Painter.h"
49
50 #include "support/debug.h"
51 #include "support/gettext.h"
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 /////////////////////////////////////////////////////////////////////
69
70 InsetText::InsetText(Buffer const & buf)
71         : drawFrame_(false), frame_color_(Color_insetframe)
72 {
73         initParagraphs(buf);
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         setParagraphOwner();
85 }
86
87
88 InsetText::InsetText()
89 {}
90
91
92 void InsetText::initParagraphs(Buffer const & buf)
93 {
94         BOOST_ASSERT(paragraphs().empty());
95         buffer_ = const_cast<Buffer *>(&buf);
96         paragraphs().push_back(Paragraph());
97         Paragraph & ourpar = paragraphs().back();
98         ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
99         ourpar.setInsetOwner(this);
100 }
101
102 void InsetText::setParagraphOwner()
103 {
104         for_each(paragraphs().begin(), paragraphs().end(),
105                  bind(&Paragraph::setInsetOwner, _1, this));
106 }
107
108
109 void InsetText::clear()
110 {
111         ParagraphList & pars = paragraphs();
112         BOOST_ASSERT(!pars.empty());
113
114         // This is a gross hack...
115         Layout const & old_layout = pars.begin()->layout();
116
117         pars.clear();
118         pars.push_back(Paragraph());
119         pars.begin()->setInsetOwner(this);
120         pars.begin()->setLayout(old_layout);
121 }
122
123
124 Dimension const InsetText::dimension(BufferView const & bv) const
125 {
126         TextMetrics const & tm = bv.textMetrics(&text_);
127         Dimension dim = tm.dimension();
128         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
129         dim.des += TEXT_TO_INSET_OFFSET;
130         dim.asc += TEXT_TO_INSET_OFFSET;
131         return dim;
132 }
133
134
135 void InsetText::write(ostream & os) const
136 {
137         os << "Text\n";
138         text_.write(buffer(), os);
139 }
140
141
142 void InsetText::read(Lexer & lex)
143 {
144         clear();
145
146         // delete the initial paragraph
147         Paragraph oldpar = *paragraphs().begin();
148         paragraphs().clear();
149         ErrorList errorList;
150         bool res = text_.read(buffer(), lex, errorList, this);
151
152         if (!res) {
153                 lex.printError("Missing \\end_inset at this point. "
154                                            "Read: `$$Token'");
155         }
156
157         // sanity check
158         // ensure we have at least one paragraph.
159         if (paragraphs().empty())
160                 paragraphs().push_back(oldpar);
161 }
162
163
164 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
165 {
166         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
167
168         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
169
170         // Hand font through to contained lyxtext:
171         tm.font_.fontInfo() = mi.base.font;
172         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
173         if (hasFixedWidth())
174                 tm.metrics(mi, dim, mi.base.textwidth);
175         else
176                 tm.metrics(mi, dim);
177         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
178         dim.asc += TEXT_TO_INSET_OFFSET;
179         dim.des += TEXT_TO_INSET_OFFSET;
180         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
181 }
182
183
184 void InsetText::draw(PainterInfo & pi, int x, int y) const
185 {
186         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
187
188         if (drawFrame_ || pi.full_repaint) {
189                 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
190                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
191                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
192                 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
193                 if (pi.full_repaint)
194                         pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
195                 if (drawFrame_)
196                         pi.pain.rectangle(xframe, yframe, w, h, frameColor());
197         }
198         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
199 }
200
201
202 docstring InsetText::editMessage() const
203 {
204         return _("Opened Text Inset");
205 }
206
207
208 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
209 {
210         pit_type const pit = front ? 0 : paragraphs().size() - 1;
211         pos_type pos = front ? 0 : paragraphs().back().size();
212
213         // if visual information is not to be ignored, move to extreme right/left
214         if (entry_from != ENTRY_DIRECTION_IGNORE) {
215                 Cursor temp_cur = cur;
216                 temp_cur.pit() = pit;
217                 temp_cur.pos() = pos;
218                 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
219                 pos = temp_cur.pos();
220         }
221
222         text_.setCursor(cur.top(), pit, pos);
223         cur.clearSelection();
224         cur.finishUndo();
225 }
226
227
228 Inset * InsetText::editXY(Cursor & cur, int x, int y)
229 {
230         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
231 }
232
233
234 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
235 {
236         LYXERR(Debug::ACTION, "InsetText::doDispatch()"
237                 << " [ cmd.action = " << cmd.action << ']');
238         text_.dispatch(cur, cmd);
239 }
240
241
242 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
243         FuncStatus & status) const
244 {
245         return text_.getStatus(cur, cmd, status);
246 }
247
248
249 void InsetText::setChange(Change const & change)
250 {
251         ParagraphList::iterator pit = paragraphs().begin();
252         ParagraphList::iterator end = paragraphs().end();
253         for (; pit != end; ++pit) {
254                 pit->setChange(change);
255         }
256 }
257
258
259 void InsetText::acceptChanges(BufferParams const & bparams)
260 {
261         text_.acceptChanges(bparams);
262 }
263
264
265 void InsetText::rejectChanges(BufferParams const & bparams)
266 {
267         text_.rejectChanges(bparams);
268 }
269
270
271 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
272 {
273         TexRow texrow;
274         latexParagraphs(buffer(), text_, os, texrow, runparams);
275         return texrow.rows();
276 }
277
278
279 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
280 {
281         ParagraphList::const_iterator beg = paragraphs().begin();
282         ParagraphList::const_iterator end = paragraphs().end();
283         ParagraphList::const_iterator it = beg;
284         bool ref_printed = false;
285         int len = 0;
286         for (; it != end; ++it) {
287                 if (it != beg) {
288                         os << '\n';
289                         if (runparams.linelen > 0)
290                                 os << '\n';
291                 }
292                 odocstringstream oss;
293                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
294                 docstring const str = oss.str();
295                 os << str;
296                 // FIXME: len is not computed fully correctly; in principle,
297                 // we have to count the characters after the last '\n'
298                 len = str.size();
299         }
300
301         return len;
302 }
303
304
305 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
306 {
307         docbookParagraphs(paragraphs(), buffer(), os, runparams);
308         return 0;
309 }
310
311
312 void InsetText::validate(LaTeXFeatures & features) const
313 {
314         for_each(paragraphs().begin(), paragraphs().end(),
315                  bind(&Paragraph::validate, _1, ref(features)));
316 }
317
318
319 void InsetText::cursorPos(BufferView const & bv,
320                 CursorSlice const & sl, bool boundary, int & x, int & y) const
321 {
322         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
323         y = bv.textMetrics(&text_).cursorY(sl, boundary);
324 }
325
326
327 bool InsetText::showInsetDialog(BufferView *) const
328 {
329         return false;
330 }
331
332
333 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
334 {
335         clear();
336         Paragraph & first = paragraphs().front();
337         for (unsigned int i = 0; i < data.length(); ++i)
338                 first.insertChar(i, data[i], font, trackChanges);
339 }
340
341
342 void InsetText::setAutoBreakRows(bool flag)
343 {
344         if (flag == text_.autoBreakRows_)
345                 return;
346
347         text_.autoBreakRows_ = flag;
348         if (flag)
349                 return;
350
351         // remove previously existing newlines
352         ParagraphList::iterator it = paragraphs().begin();
353         ParagraphList::iterator end = paragraphs().end();
354         for (; it != end; ++it)
355                 for (int i = 0; i < it->size(); ++i)
356                         if (it->isNewline(i))
357                                 // do not track the change, because the user
358                                 // is not allowed to revert/reject it
359                                 it->eraseChar(i, false);
360 }
361
362
363 void InsetText::setDrawFrame(bool flag)
364 {
365         drawFrame_ = flag;
366 }
367
368
369 ColorCode InsetText::frameColor() const
370 {
371         return frame_color_;
372 }
373
374
375 void InsetText::setFrameColor(ColorCode col)
376 {
377         frame_color_ = col;
378 }
379
380
381 void InsetText::appendParagraphs(ParagraphList & plist)
382 {
383         // There is little we can do here to keep track of changes.
384         // As of 2006/10/20, appendParagraphs is used exclusively by
385         // LyXTabular::setMultiColumn. In this context, the paragraph break
386         // is lost irreversibly and the appended text doesn't really change
387
388         ParagraphList & pl = paragraphs();
389
390         ParagraphList::iterator pit = plist.begin();
391         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
392         ++pit;
393         mergeParagraph(buffer().params(), pl,
394                        distance(pl.begin(), ins) - 1);
395
396         for_each(pit, plist.end(),
397                  bind(&ParagraphList::push_back, ref(pl), _1));
398 }
399
400
401 void InsetText::addPreview(PreviewLoader & loader) const
402 {
403         ParagraphList::const_iterator pit = paragraphs().begin();
404         ParagraphList::const_iterator pend = paragraphs().end();
405
406         for (; pit != pend; ++pit) {
407                 InsetList::const_iterator it  = pit->insetList().begin();
408                 InsetList::const_iterator end = pit->insetList().end();
409                 for (; it != end; ++it)
410                         it->inset->addPreview(loader);
411         }
412 }
413
414
415 ParagraphList const & InsetText::paragraphs() const
416 {
417         return text_.paragraphs();
418 }
419
420
421 ParagraphList & InsetText::paragraphs()
422 {
423         return text_.paragraphs();
424 }
425
426
427 //void InsetInclude::addToToc(ParConstIterator const & cpit) const
428 //{
429 //}
430
431
432 void InsetText::updateLabels(ParIterator const & it)
433 {
434         ParIterator it2 = it;
435         it2.forwardPos();
436         BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
437         lyx::updateLabels(buffer(), it2);
438 }
439
440
441 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
442 {
443         if (cur.buffer().isClean())
444                 return Inset::notifyCursorLeaves(old, cur);
445         
446         // find text inset in old cursor
447         Cursor insetCur = old;
448         int scriptSlice = insetCur.find(this);
449         BOOST_ASSERT(scriptSlice != -1);
450         insetCur.cutOff(scriptSlice);
451         BOOST_ASSERT(&insetCur.inset() == this);
452         
453         // update the old paragraph's words
454         insetCur.paragraph().updateWords(insetCur.top());
455         
456         return Inset::notifyCursorLeaves(old, cur);
457 }
458
459
460 bool InsetText::completionSupported(Cursor const & cur) const
461 {
462         Cursor const & bvCur = cur.bv().cursor();
463         if (&bvCur.inset() != this)
464                 return false;
465         return text_.completionSupported(cur);
466 }
467
468
469 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
470 {
471         return completionSupported(cur);
472 }
473
474
475 bool InsetText::automaticInlineCompletion() const
476 {
477         return lyxrc.completion_inline_text;
478 }
479
480
481 bool InsetText::automaticPopupCompletion() const
482 {
483         return lyxrc.completion_popup_text;
484 }
485
486
487 bool InsetText::showCompletionCursor() const
488 {
489         return lyxrc.completion_cursor_text;
490 }
491
492
493 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
494 {
495         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
496 }
497
498
499 docstring InsetText::completionPrefix(Cursor const & cur) const
500 {
501         if (!completionSupported(cur))
502                 return docstring();
503         return text_.completionPrefix(cur);
504 }
505
506
507 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
508         bool finished)
509 {
510         if (!completionSupported(cur))
511                 return false;
512
513         return text_.insertCompletion(cur, s, finished);
514 }
515
516
517 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
518         Dimension & dim) const
519 {
520         TextMetrics const & tm = cur.bv().textMetrics(&text_);
521         tm.completionPosAndDim(cur, x, y, dim);
522 }
523
524
525 docstring InsetText::contextMenu(BufferView const &, int, int) const
526 {
527         return from_ascii("context-edit");
528 }
529
530
531 } // namespace lyx