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();
143 BOOST_ASSERT(!pars.empty());
145 // This is a gross hack...
146 Layout const & old_layout = pars.begin()->layout();
149 pars.push_back(Paragraph());
150 pars.begin()->setInsetOwner(this);
151 pars.begin()->setLayout(old_layout);
155 Dimension const InsetText::dimension(BufferView const & bv) const
157 TextMetrics const & tm = bv.textMetrics(&text_);
158 Dimension dim = tm.dimension();
159 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
160 dim.des += TEXT_TO_INSET_OFFSET;
161 dim.asc += TEXT_TO_INSET_OFFSET;
166 void InsetText::write(ostream & os) const
169 text_.write(buffer(), os);
173 void InsetText::read(Lexer & lex)
177 // delete the initial paragraph
178 Paragraph oldpar = *paragraphs().begin();
179 paragraphs().clear();
181 bool res = text_.read(buffer(), lex, errorList, this);
184 lex.printError("Missing \\end_inset at this point. "
189 // ensure we have at least one paragraph.
190 if (paragraphs().empty())
191 paragraphs().push_back(oldpar);
195 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
197 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
199 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
201 // Hand font through to contained lyxtext:
202 tm.font_.fontInfo() = mi.base.font;
203 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
205 tm.metrics(mi, dim, mi.base.textwidth);
208 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
209 dim.asc += TEXT_TO_INSET_OFFSET;
210 dim.des += TEXT_TO_INSET_OFFSET;
211 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
215 void InsetText::draw(PainterInfo & pi, int x, int y) const
217 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
219 if (drawFrame_ || pi.full_repaint) {
220 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
221 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
222 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
223 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
225 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
227 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
229 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
233 docstring InsetText::editMessage() const
235 return _("Opened Text Inset");
239 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
241 pit_type const pit = front ? 0 : paragraphs().size() - 1;
242 pos_type pos = front ? 0 : paragraphs().back().size();
244 // if visual information is not to be ignored, move to extreme right/left
245 if (entry_from != ENTRY_DIRECTION_IGNORE) {
246 Cursor temp_cur = cur;
247 temp_cur.pit() = pit;
248 temp_cur.pos() = pos;
249 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
250 pos = temp_cur.pos();
253 text_.setCursor(cur.top(), pit, pos);
254 cur.clearSelection();
259 Inset * InsetText::editXY(Cursor & cur, int x, int y)
261 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
265 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
267 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
268 << " [ cmd.action = " << cmd.action << ']');
269 text_.dispatch(cur, cmd);
273 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
274 FuncStatus & status) const
276 return text_.getStatus(cur, cmd, status);
280 void InsetText::setChange(Change const & change)
282 ParagraphList::iterator pit = paragraphs().begin();
283 ParagraphList::iterator end = paragraphs().end();
284 for (; pit != end; ++pit) {
285 pit->setChange(change);
290 void InsetText::acceptChanges(BufferParams const & bparams)
292 text_.acceptChanges(bparams);
296 void InsetText::rejectChanges(BufferParams const & bparams)
298 text_.rejectChanges(bparams);
302 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
305 latexParagraphs(buffer(), text_, os, texrow, runparams);
306 return texrow.rows();
310 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
312 ParagraphList::const_iterator beg = paragraphs().begin();
313 ParagraphList::const_iterator end = paragraphs().end();
314 ParagraphList::const_iterator it = beg;
315 bool ref_printed = false;
317 for (; it != end; ++it) {
320 if (runparams.linelen > 0)
323 odocstringstream oss;
324 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
325 docstring const str = oss.str();
327 // FIXME: len is not computed fully correctly; in principle,
328 // we have to count the characters after the last '\n'
336 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
338 docbookParagraphs(paragraphs(), buffer(), os, runparams);
343 void InsetText::validate(LaTeXFeatures & features) const
345 for_each(paragraphs().begin(), paragraphs().end(),
346 bind(&Paragraph::validate, _1, ref(features)));
350 void InsetText::cursorPos(BufferView const & bv,
351 CursorSlice const & sl, bool boundary, int & x, int & y) const
353 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
354 y = bv.textMetrics(&text_).cursorY(sl, boundary);
358 bool InsetText::showInsetDialog(BufferView *) const
364 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
367 Paragraph & first = paragraphs().front();
368 for (unsigned int i = 0; i < data.length(); ++i)
369 first.insertChar(i, data[i], font, trackChanges);
373 void InsetText::setAutoBreakRows(bool flag)
375 if (flag == text_.autoBreakRows_)
378 text_.autoBreakRows_ = flag;
382 // remove previously existing newlines
383 ParagraphList::iterator it = paragraphs().begin();
384 ParagraphList::iterator end = paragraphs().end();
385 for (; it != end; ++it)
386 for (int i = 0; i < it->size(); ++i)
387 if (it->isNewline(i))
388 // do not track the change, because the user
389 // is not allowed to revert/reject it
390 it->eraseChar(i, false);
394 void InsetText::setDrawFrame(bool flag)
400 ColorCode InsetText::frameColor() const
406 void InsetText::setFrameColor(ColorCode col)
412 void InsetText::appendParagraphs(ParagraphList & plist)
414 // There is little we can do here to keep track of changes.
415 // As of 2006/10/20, appendParagraphs is used exclusively by
416 // LyXTabular::setMultiColumn. In this context, the paragraph break
417 // is lost irreversibly and the appended text doesn't really change
419 ParagraphList & pl = paragraphs();
421 ParagraphList::iterator pit = plist.begin();
422 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
424 mergeParagraph(buffer().params(), pl,
425 distance(pl.begin(), ins) - 1);
427 for_each(pit, plist.end(),
428 bind(&ParagraphList::push_back, ref(pl), _1));
432 void InsetText::addPreview(PreviewLoader & loader) const
434 ParagraphList::const_iterator pit = paragraphs().begin();
435 ParagraphList::const_iterator pend = paragraphs().end();
437 for (; pit != pend; ++pit) {
438 InsetList::const_iterator it = pit->insetList().begin();
439 InsetList::const_iterator end = pit->insetList().end();
440 for (; it != end; ++it)
441 it->inset->addPreview(loader);
446 // FIXME: instead of this hack, which only works by chance,
447 // cells should have their own insetcell type, which returns CELL_CODE!
448 bool InsetText::neverIndent() const
450 // this is only true for tabular cells
451 return !text_.isMainText(buffer()) && lyxCode() == TEXT_CODE;
455 ParagraphList const & InsetText::paragraphs() const
457 return text_.paragraphs();
461 ParagraphList & InsetText::paragraphs()
463 return text_.paragraphs();
467 void InsetText::updateLabels(ParIterator const & it)
469 ParIterator it2 = it;
471 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
472 lyx::updateLabels(buffer(), it2);
476 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
478 if (cur.buffer().isClean())
479 return Inset::notifyCursorLeaves(old, cur);
481 // find text inset in old cursor
482 Cursor insetCur = old;
483 int scriptSlice = insetCur.find(this);
484 BOOST_ASSERT(scriptSlice != -1);
485 insetCur.cutOff(scriptSlice);
486 BOOST_ASSERT(&insetCur.inset() == this);
488 // update the old paragraph's words
489 insetCur.paragraph().updateWords(insetCur.top());
491 return Inset::notifyCursorLeaves(old, cur);
495 bool InsetText::completionSupported(Cursor const & cur) const
497 Cursor const & bvCur = cur.bv().cursor();
498 if (&bvCur.inset() != this)
500 Paragraph const & par = cur.paragraph();
502 && (cur.pos() >= par.size() || !par.isLetter(cur.pos()))
503 && par.isLetter(cur.pos() - 1);
507 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
509 return completionSupported(cur);
513 bool InsetText::automaticInlineCompletion() const
515 return lyxrc.completion_inline_text;
519 bool InsetText::automaticPopupCompletion() const
521 return lyxrc.completion_popup_text;
525 Inset::CompletionList const * InsetText::createCompletionList(
526 Cursor const & cur) const
528 if (!completionSupported(cur))
531 return new TextCompletionList(cur);
535 docstring InsetText::previousWord(CursorSlice const & sl) const
537 CursorSlice from = sl;
539 text_.getWord(from, to, PREVIOUS_WORD);
540 if (sl == from || to == from)
543 Paragraph const & par = sl.paragraph();
544 return par.asString(from.pos(), to.pos(), false);
548 docstring InsetText::completionPrefix(Cursor const & cur) const
550 if (!completionSupported(cur))
552 return previousWord(cur.top());
556 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
559 if (!completionSupported(cur))
562 BOOST_ASSERT(cur.bv().cursor() == cur);
564 cur.bv().cursor() = cur;
565 if (!(cur.disp_.update() & Update::Force))
566 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
571 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
572 Dimension & dim) const
574 Cursor const & bvcur = cur.bv().cursor();
576 // get word in front of cursor
577 docstring word = previousWord(bvcur.top());
578 DocIterator wordStart = bvcur;
579 wordStart.pos() -= word.length();
581 // get position on screen of the word start and end
582 Point lxy = cur.bv().getPos(wordStart, false);
583 Point rxy = cur.bv().getPos(bvcur, bvcur.boundary());
585 // calculate dimensions of the word
586 TextMetrics const & tm = cur.bv().textMetrics(&text_);
587 dim = tm.rowHeight(bvcur.pit(), wordStart.pos(), bvcur.pos(), false);
588 dim.wid = abs(rxy.x_ - lxy.x_);
590 // calculate position of word
592 x = min(rxy.x_, lxy.x_);
594 //lyxerr << "wid=" << dim.width() << " x=" << x << " y=" << y << " lxy.x_=" << lxy.x_ << " rxy.x_=" << rxy.x_ << " word=" << word << std::endl;
595 //lyxerr << " wordstart=" << wordStart << " bvcur=" << bvcur << " cur=" << cur << std::endl;