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"
47 #include "frontends/alert.h"
48 #include "frontends/Painter.h"
50 #include "support/debug.h"
51 #include "support/gettext.h"
52 #include "support/lstrings.h"
54 #include <boost/bind.hpp>
55 #include <boost/assert.hpp>
58 using namespace lyx::support;
65 using graphics::PreviewLoader;
68 class TextCompletionList : public Inset::CompletionList {
71 TextCompletionList(Cursor const & cur)
72 : buf_(cur.buffer()), it_(buf_.registeredWords().begin()), pos_(0) {}
74 virtual ~TextCompletionList() {}
77 virtual size_t size() const {
78 return buf_.registeredWords().size();
81 virtual docstring data(size_t idx) const {
82 std::set<docstring>::const_iterator it
83 = buf_.registeredWords().begin();
84 for (size_t i = 0; i < idx; ++i)
91 std::set<docstring>::const_iterator const it_;
96 /////////////////////////////////////////////////////////////////////
98 InsetText::InsetText(BufferParams const & bp)
99 : drawFrame_(false), frame_color_(Color_insetframe)
101 paragraphs().push_back(Paragraph());
102 Paragraph & ourpar = paragraphs().back();
103 ourpar.setEmptyOrDefaultLayout(bp.textClass());
104 ourpar.setInsetOwner(this);
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::setParagraphOwner()
125 for_each(paragraphs().begin(), paragraphs().end(),
126 bind(&Paragraph::setInsetOwner, _1, this));
130 void InsetText::clear()
132 ParagraphList & pars = paragraphs();
134 // This is a gross hack...
135 LayoutPtr old_layout = pars.begin()->layout();
138 pars.push_back(Paragraph());
139 pars.begin()->setInsetOwner(this);
140 pars.begin()->setLayout(old_layout);
144 Inset * InsetText::clone() const
146 return new InsetText(*this);
150 Dimension const InsetText::dimension(BufferView const & bv) const
152 TextMetrics const & tm = bv.textMetrics(&text_);
153 Dimension dim = tm.dimension();
154 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
155 dim.des += TEXT_TO_INSET_OFFSET;
156 dim.asc += TEXT_TO_INSET_OFFSET;
161 void InsetText::write(Buffer const & buf, ostream & os) const
164 text_.write(buf, os);
168 void InsetText::read(Buffer const & buf, Lexer & lex)
172 // delete the initial paragraph
173 Paragraph oldpar = *paragraphs().begin();
174 paragraphs().clear();
176 bool res = text_.read(buf, lex, errorList, this);
179 lex.printError("Missing \\end_inset at this point. "
184 // ensure we have at least one paragraph.
185 if (paragraphs().empty())
186 paragraphs().push_back(oldpar);
190 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
192 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
194 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
196 // Hand font through to contained lyxtext:
197 tm.font_.fontInfo() = mi.base.font;
198 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
200 tm.metrics(mi, dim, mi.base.textwidth);
203 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
204 dim.asc += TEXT_TO_INSET_OFFSET;
205 dim.des += TEXT_TO_INSET_OFFSET;
206 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
210 void InsetText::draw(PainterInfo & pi, int x, int y) const
212 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
214 if (drawFrame_ || pi.full_repaint) {
215 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
216 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
217 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
218 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
220 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
222 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
224 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
228 docstring const InsetText::editMessage() const
230 return _("Opened Text Inset");
234 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
236 pit_type const pit = front ? 0 : paragraphs().size() - 1;
237 pos_type pos = front ? 0 : paragraphs().back().size();
239 // if visual information is not to be ignored, move to extreme right/left
240 if (entry_from != ENTRY_DIRECTION_IGNORE) {
241 Cursor temp_cur = cur;
242 temp_cur.pit() = pit;
243 temp_cur.pos() = pos;
244 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
245 pos = temp_cur.pos();
248 text_.setCursor(cur.top(), pit, pos);
249 cur.clearSelection();
254 Inset * InsetText::editXY(Cursor & cur, int x, int y)
256 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
260 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
262 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
263 << " [ cmd.action = " << cmd.action << ']');
264 text_.dispatch(cur, cmd);
268 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
269 FuncStatus & status) const
271 return text_.getStatus(cur, cmd, status);
275 void InsetText::setChange(Change const & change)
277 ParagraphList::iterator pit = paragraphs().begin();
278 ParagraphList::iterator end = paragraphs().end();
279 for (; pit != end; ++pit) {
280 pit->setChange(change);
285 void InsetText::acceptChanges(BufferParams const & bparams)
287 text_.acceptChanges(bparams);
291 void InsetText::rejectChanges(BufferParams const & bparams)
293 text_.rejectChanges(bparams);
297 int InsetText::latex(Buffer const & buf, odocstream & os,
298 OutputParams const & runparams) const
301 latexParagraphs(buf, text_, os, texrow, runparams);
302 return texrow.rows();
306 int InsetText::plaintext(Buffer const & buf, odocstream & os,
307 OutputParams const & runparams) const
309 ParagraphList::const_iterator beg = paragraphs().begin();
310 ParagraphList::const_iterator end = paragraphs().end();
311 ParagraphList::const_iterator it = beg;
312 bool ref_printed = false;
314 for (; it != end; ++it) {
317 if (runparams.linelen > 0)
320 odocstringstream oss;
321 writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
322 docstring const str = oss.str();
324 // FIXME: len is not computed fully correctly; in principle,
325 // we have to count the characters after the last '\n'
333 int InsetText::docbook(Buffer const & buf, odocstream & os,
334 OutputParams const & runparams) const
336 docbookParagraphs(paragraphs(), buf, os, runparams);
341 void InsetText::validate(LaTeXFeatures & features) const
343 for_each(paragraphs().begin(), paragraphs().end(),
344 bind(&Paragraph::validate, _1, ref(features)));
348 void InsetText::cursorPos(BufferView const & bv,
349 CursorSlice const & sl, bool boundary, int & x, int & y) const
351 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
352 y = bv.textMetrics(&text_).cursorY(sl, boundary);
356 bool InsetText::showInsetDialog(BufferView *) const
362 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
365 Paragraph & first = paragraphs().front();
366 for (unsigned int i = 0; i < data.length(); ++i)
367 first.insertChar(i, data[i], font, trackChanges);
371 void InsetText::setAutoBreakRows(bool flag)
373 if (flag == text_.autoBreakRows_)
376 text_.autoBreakRows_ = flag;
380 // remove previously existing newlines
381 ParagraphList::iterator it = paragraphs().begin();
382 ParagraphList::iterator end = paragraphs().end();
383 for (; it != end; ++it)
384 for (int i = 0; i < it->size(); ++i)
385 if (it->isNewline(i))
386 // do not track the change, because the user
387 // is not allowed to revert/reject it
388 it->eraseChar(i, false);
392 void InsetText::setDrawFrame(bool flag)
398 ColorCode InsetText::frameColor() const
404 void InsetText::setFrameColor(ColorCode col)
410 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
412 // There is little we can do here to keep track of changes.
413 // As of 2006/10/20, appendParagraphs is used exclusively by
414 // LyXTabular::setMultiColumn. In this context, the paragraph break
415 // is lost irreversibly and the appended text doesn't really change
417 ParagraphList & pl = paragraphs();
419 ParagraphList::iterator pit = plist.begin();
420 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
422 mergeParagraph(buffer->params(), pl,
423 distance(pl.begin(), ins) - 1);
425 for_each(pit, plist.end(),
426 bind(&ParagraphList::push_back, ref(pl), _1));
430 void InsetText::addPreview(PreviewLoader & loader) const
432 ParagraphList::const_iterator pit = paragraphs().begin();
433 ParagraphList::const_iterator pend = paragraphs().end();
435 for (; pit != pend; ++pit) {
436 InsetList::const_iterator it = pit->insetList().begin();
437 InsetList::const_iterator end = pit->insetList().end();
438 for (; it != end; ++it)
439 it->inset->addPreview(loader);
444 //FIXME: instead of this hack, which only works by chance,
445 // cells should have their own insetcell type, which returns CELL_CODE!
446 bool InsetText::neverIndent(Buffer const & buffer) const
448 // this is only true for tabular cells
449 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
453 ParagraphList const & InsetText::paragraphs() const
455 return text_.paragraphs();
459 ParagraphList & InsetText::paragraphs()
461 return text_.paragraphs();
465 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
467 ParIterator it2 = it;
469 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
470 lyx::updateLabels(buf, it2);
474 bool InsetText::completionSupported(Cursor const & cur) const
476 Cursor const & bvCur = cur.bv().cursor();
477 if (&bvCur.inset() != this)
479 Paragraph const & par = cur.paragraph();
481 && (cur.pos() >= par.size() || !par.isLetter(cur.pos()))
482 && par.isLetter(cur.pos() - 1);
486 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
488 return completionSupported(cur);
492 bool InsetText::automaticInlineCompletion() const
494 return lyxrc.completion_inline_text;
498 bool InsetText::automaticPopupCompletion() const
500 return lyxrc.completion_popup_text;
504 Inset::CompletionList const
505 * InsetText::createCompletionList(Cursor const & cur) const
507 if (!completionSupported(cur))
510 return new TextCompletionList(cur);
514 docstring InsetText::previousWord(Buffer const & buffer, CursorSlice const & sl) const
516 CursorSlice from = sl;
518 text_.getWord(from, to, PREVIOUS_WORD);
519 if (sl == from || to == from)
522 Paragraph const & par = sl.paragraph();
523 return par.asString(buffer, from.pos(), to.pos(), false);
527 docstring InsetText::completionPrefix(Cursor const & cur) const
529 if (!completionSupported(cur))
532 return previousWord(cur.buffer(), cur.top());
536 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
539 if (!completionSupported(cur))
542 BOOST_ASSERT(cur.bv().cursor() == cur);
544 cur.bv().cursor() = cur;
549 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
550 Dimension & dim) const
552 Cursor const & bvcur = cur.bv().cursor();
554 // get word in front of cursor
555 docstring word = previousWord(cur.buffer(), bvcur.top());
556 DocIterator wordStart = bvcur;
557 wordStart.pos() -= word.length();
559 // get position on screen of the word start and end
560 Point lxy = cur.bv().getPos(wordStart, false);
561 Point rxy = cur.bv().getPos(bvcur, bvcur.boundary());
563 // calculate dimensions of the word
564 TextMetrics const & tm = cur.bv().textMetrics(&text_);
565 dim = tm.rowHeight(bvcur.pit(), wordStart.pos(), bvcur.pos(), false);
566 dim.wid = abs(rxy.x_ - lxy.x_);
568 // calculate position of word
570 x = min(rxy.x_, lxy.x_);
572 //lyxerr << "wid=" << dim.width() << " x=" << x << " y=" << y << " lxy.x_=" << lxy.x_ << " rxy.x_=" << rxy.x_ << " word=" << word << std::endl;
573 //lyxerr << " wordstart=" << wordStart << " bvcur=" << bvcur << " cur=" << cur << std::endl;