3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetText.h"
14 #include "InsetNewline.h"
17 #include "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CoordCache.h"
21 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "InsetList.h"
32 #include "MetricsInfo.h"
33 #include "OutputParams.h"
34 #include "output_docbook.h"
35 #include "output_latex.h"
36 #include "output_plaintext.h"
37 #include "Paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "ParIterator.h"
43 #include "TextClass.h"
44 #include "TextMetrics.h"
48 #include "frontends/alert.h"
49 #include "frontends/Painter.h"
51 #include "support/debug.h"
52 #include "support/gettext.h"
53 #include "support/lstrings.h"
55 #include <boost/bind.hpp>
56 #include <boost/assert.hpp>
59 using namespace lyx::support;
66 using graphics::PreviewLoader;
69 class TextCompletionList : public Inset::CompletionList
73 TextCompletionList(Cursor const & cur)
74 : buf_(cur.buffer()), pos_(0) {}
76 virtual ~TextCompletionList() {}
79 virtual bool sorted() const { return true; }
81 virtual size_t size() const
83 return theWordList().size();
86 virtual docstring const & data(size_t idx) const
88 return theWordList().word(idx);
99 /////////////////////////////////////////////////////////////////////
101 InsetText::InsetText(Buffer const & buf)
102 : drawFrame_(false), frame_color_(Color_insetframe)
108 InsetText::InsetText(InsetText const & in)
111 text_.autoBreakRows_ = in.text_.autoBreakRows_;
112 drawFrame_ = in.drawFrame_;
113 frame_color_ = in.frame_color_;
114 text_.paragraphs() = in.text_.paragraphs();
119 InsetText::InsetText()
123 void InsetText::initParagraphs(Buffer const & buf)
125 BOOST_ASSERT(paragraphs().empty());
126 buffer_ = const_cast<Buffer *>(&buf);
127 paragraphs().push_back(Paragraph());
128 Paragraph & ourpar = paragraphs().back();
129 ourpar.setEmptyOrDefaultLayout(buf.params().documentClass());
130 ourpar.setInsetOwner(this);
133 void InsetText::setParagraphOwner()
135 for_each(paragraphs().begin(), paragraphs().end(),
136 bind(&Paragraph::setInsetOwner, _1, this));
140 void InsetText::clear()
142 ParagraphList & pars = paragraphs();
144 // This is a gross hack...
145 Layout old_layout = pars.begin()->layout();
148 pars.push_back(Paragraph());
149 pars.begin()->setInsetOwner(this);
150 pars.begin()->setLayout(old_layout);
154 Dimension const InsetText::dimension(BufferView const & bv) const
156 TextMetrics const & tm = bv.textMetrics(&text_);
157 Dimension dim = tm.dimension();
158 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
159 dim.des += TEXT_TO_INSET_OFFSET;
160 dim.asc += TEXT_TO_INSET_OFFSET;
165 void InsetText::write(ostream & os) const
168 text_.write(buffer(), os);
172 void InsetText::read(Lexer & lex)
176 // delete the initial paragraph
177 Paragraph oldpar = *paragraphs().begin();
178 paragraphs().clear();
180 bool res = text_.read(buffer(), lex, errorList, this);
183 lex.printError("Missing \\end_inset at this point. "
188 // ensure we have at least one paragraph.
189 if (paragraphs().empty())
190 paragraphs().push_back(oldpar);
194 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
196 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
198 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
200 // Hand font through to contained lyxtext:
201 tm.font_.fontInfo() = mi.base.font;
202 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
204 tm.metrics(mi, dim, mi.base.textwidth);
207 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
208 dim.asc += TEXT_TO_INSET_OFFSET;
209 dim.des += TEXT_TO_INSET_OFFSET;
210 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
214 void InsetText::draw(PainterInfo & pi, int x, int y) const
216 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
218 if (drawFrame_ || pi.full_repaint) {
219 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
220 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
221 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
222 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
224 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
226 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
228 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
232 docstring InsetText::editMessage() const
234 return _("Opened Text Inset");
238 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
240 pit_type const pit = front ? 0 : paragraphs().size() - 1;
241 pos_type pos = front ? 0 : paragraphs().back().size();
243 // if visual information is not to be ignored, move to extreme right/left
244 if (entry_from != ENTRY_DIRECTION_IGNORE) {
245 Cursor temp_cur = cur;
246 temp_cur.pit() = pit;
247 temp_cur.pos() = pos;
248 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
249 pos = temp_cur.pos();
252 text_.setCursor(cur.top(), pit, pos);
253 cur.clearSelection();
258 Inset * InsetText::editXY(Cursor & cur, int x, int y)
260 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
264 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
266 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
267 << " [ cmd.action = " << cmd.action << ']');
268 text_.dispatch(cur, cmd);
272 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
273 FuncStatus & status) const
275 return text_.getStatus(cur, cmd, status);
279 void InsetText::setChange(Change const & change)
281 ParagraphList::iterator pit = paragraphs().begin();
282 ParagraphList::iterator end = paragraphs().end();
283 for (; pit != end; ++pit) {
284 pit->setChange(change);
289 void InsetText::acceptChanges(BufferParams const & bparams)
291 text_.acceptChanges(bparams);
295 void InsetText::rejectChanges(BufferParams const & bparams)
297 text_.rejectChanges(bparams);
301 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
304 latexParagraphs(buffer(), text_, os, texrow, runparams);
305 return texrow.rows();
309 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
311 ParagraphList::const_iterator beg = paragraphs().begin();
312 ParagraphList::const_iterator end = paragraphs().end();
313 ParagraphList::const_iterator it = beg;
314 bool ref_printed = false;
316 for (; it != end; ++it) {
319 if (runparams.linelen > 0)
322 odocstringstream oss;
323 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
324 docstring const str = oss.str();
326 // FIXME: len is not computed fully correctly; in principle,
327 // we have to count the characters after the last '\n'
335 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
337 docbookParagraphs(paragraphs(), buffer(), os, runparams);
342 void InsetText::validate(LaTeXFeatures & features) const
344 for_each(paragraphs().begin(), paragraphs().end(),
345 bind(&Paragraph::validate, _1, ref(features)));
349 void InsetText::cursorPos(BufferView const & bv,
350 CursorSlice const & sl, bool boundary, int & x, int & y) const
352 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
353 y = bv.textMetrics(&text_).cursorY(sl, boundary);
357 bool InsetText::showInsetDialog(BufferView *) const
363 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
366 Paragraph & first = paragraphs().front();
367 for (unsigned int i = 0; i < data.length(); ++i)
368 first.insertChar(i, data[i], font, trackChanges);
372 void InsetText::setAutoBreakRows(bool flag)
374 if (flag == text_.autoBreakRows_)
377 text_.autoBreakRows_ = flag;
381 // remove previously existing newlines
382 ParagraphList::iterator it = paragraphs().begin();
383 ParagraphList::iterator end = paragraphs().end();
384 for (; it != end; ++it)
385 for (int i = 0; i < it->size(); ++i)
386 if (it->isNewline(i))
387 // do not track the change, because the user
388 // is not allowed to revert/reject it
389 it->eraseChar(i, false);
393 void InsetText::setDrawFrame(bool flag)
399 ColorCode InsetText::frameColor() const
405 void InsetText::setFrameColor(ColorCode col)
411 void InsetText::appendParagraphs(ParagraphList & plist)
413 // There is little we can do here to keep track of changes.
414 // As of 2006/10/20, appendParagraphs is used exclusively by
415 // LyXTabular::setMultiColumn. In this context, the paragraph break
416 // is lost irreversibly and the appended text doesn't really change
418 ParagraphList & pl = paragraphs();
420 ParagraphList::iterator pit = plist.begin();
421 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
423 mergeParagraph(buffer().params(), pl,
424 distance(pl.begin(), ins) - 1);
426 for_each(pit, plist.end(),
427 bind(&ParagraphList::push_back, ref(pl), _1));
431 void InsetText::addPreview(PreviewLoader & loader) const
433 ParagraphList::const_iterator pit = paragraphs().begin();
434 ParagraphList::const_iterator pend = paragraphs().end();
436 for (; pit != pend; ++pit) {
437 InsetList::const_iterator it = pit->insetList().begin();
438 InsetList::const_iterator end = pit->insetList().end();
439 for (; it != end; ++it)
440 it->inset->addPreview(loader);
445 // FIXME: instead of this hack, which only works by chance,
446 // cells should have their own insetcell type, which returns CELL_CODE!
447 bool InsetText::neverIndent() const
449 // this is only true for tabular cells
450 return !text_.isMainText(buffer()) && lyxCode() == TEXT_CODE;
454 ParagraphList const & InsetText::paragraphs() const
456 return text_.paragraphs();
460 ParagraphList & InsetText::paragraphs()
462 return text_.paragraphs();
466 void InsetText::updateLabels(ParIterator const & it)
468 ParIterator it2 = it;
470 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
471 lyx::updateLabels(buffer(), it2);
475 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
477 if (cur.buffer().isClean())
478 return Inset::notifyCursorLeaves(old, cur);
480 // find text inset in old cursor
481 Cursor insetCur = old;
482 int scriptSlice = insetCur.find(this);
483 BOOST_ASSERT(scriptSlice != -1);
484 insetCur.cutOff(scriptSlice);
485 BOOST_ASSERT(&insetCur.inset() == this);
487 // update the old paragraph's words
488 insetCur.paragraph().updateWords(insetCur.top());
490 return Inset::notifyCursorLeaves(old, cur);
494 bool InsetText::completionSupported(Cursor const & cur) const
496 Cursor const & bvCur = cur.bv().cursor();
497 if (&bvCur.inset() != this)
499 Paragraph const & par = cur.paragraph();
501 && (cur.pos() >= par.size() || !par.isLetter(cur.pos()))
502 && par.isLetter(cur.pos() - 1);
506 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
508 return completionSupported(cur);
512 bool InsetText::automaticInlineCompletion() const
514 return lyxrc.completion_inline_text;
518 bool InsetText::automaticPopupCompletion() const
520 return lyxrc.completion_popup_text;
524 Inset::CompletionList const * InsetText::createCompletionList(
525 Cursor const & cur) const
527 if (!completionSupported(cur))
530 return new TextCompletionList(cur);
534 docstring InsetText::previousWord(CursorSlice const & sl) const
536 CursorSlice from = sl;
538 text_.getWord(from, to, PREVIOUS_WORD);
539 if (sl == from || to == from)
542 Paragraph const & par = sl.paragraph();
543 return par.asString(from.pos(), to.pos(), false);
547 docstring InsetText::completionPrefix(Cursor const & cur) const
549 if (!completionSupported(cur))
551 return previousWord(cur.top());
555 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
558 if (!completionSupported(cur))
561 BOOST_ASSERT(cur.bv().cursor() == cur);
563 cur.bv().cursor() = cur;
564 if (!(cur.disp_.update() & Update::Force))
565 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
570 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
571 Dimension & dim) const
573 Cursor const & bvcur = cur.bv().cursor();
575 // get word in front of cursor
576 docstring word = previousWord(bvcur.top());
577 DocIterator wordStart = bvcur;
578 wordStart.pos() -= word.length();
580 // get position on screen of the word start and end
581 Point lxy = cur.bv().getPos(wordStart, false);
582 Point rxy = cur.bv().getPos(bvcur, bvcur.boundary());
584 // calculate dimensions of the word
585 TextMetrics const & tm = cur.bv().textMetrics(&text_);
586 dim = tm.rowHeight(bvcur.pit(), wordStart.pos(), bvcur.pos(), false);
587 dim.wid = abs(rxy.x_ - lxy.x_);
589 // calculate position of word
591 x = min(rxy.x_, lxy.x_);
593 //lyxerr << "wid=" << dim.width() << " x=" << x << " y=" << y << " lxy.x_=" << lxy.x_ << " rxy.x_=" << rxy.x_ << " word=" << word << std::endl;
594 //lyxerr << " wordstart=" << wordStart << " bvcur=" << bvcur << " cur=" << cur << std::endl;