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 if (useEmptyLayout())
104 ourpar.layout(bp.getTextClass().emptyLayout());
106 ourpar.layout(bp.getTextClass().defaultLayout());
107 ourpar.setInsetOwner(this);
111 InsetText::InsetText(InsetText const & in)
114 text_.autoBreakRows_ = in.text_.autoBreakRows_;
115 drawFrame_ = in.drawFrame_;
116 frame_color_ = in.frame_color_;
117 text_.paragraphs() = in.text_.paragraphs();
122 InsetText::InsetText()
126 void InsetText::setParagraphOwner()
128 for_each(paragraphs().begin(), paragraphs().end(),
129 bind(&Paragraph::setInsetOwner, _1, this));
133 void InsetText::clear()
135 ParagraphList & pars = paragraphs();
137 // This is a gross hack...
138 LayoutPtr old_layout = pars.begin()->layout();
141 pars.push_back(Paragraph());
142 pars.begin()->setInsetOwner(this);
143 pars.begin()->layout(old_layout);
147 Inset * InsetText::clone() const
149 return new InsetText(*this);
153 Dimension const InsetText::dimension(BufferView const & bv) const
155 TextMetrics const & tm = bv.textMetrics(&text_);
156 Dimension dim = tm.dimension();
157 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
158 dim.des += TEXT_TO_INSET_OFFSET;
159 dim.asc += TEXT_TO_INSET_OFFSET;
164 void InsetText::write(Buffer const & buf, ostream & os) const
167 text_.write(buf, os);
171 void InsetText::read(Buffer const & buf, Lexer & lex)
175 // delete the initial paragraph
176 Paragraph oldpar = *paragraphs().begin();
177 paragraphs().clear();
179 bool res = text_.read(buf, lex, errorList, this);
182 lex.printError("Missing \\end_inset at this point. "
187 // ensure we have at least one paragraph.
188 if (paragraphs().empty())
189 paragraphs().push_back(oldpar);
193 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
195 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
197 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
199 // Hand font through to contained lyxtext:
200 tm.font_.fontInfo() = mi.base.font;
201 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
203 tm.metrics(mi, dim, mi.base.textwidth);
206 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
207 dim.asc += TEXT_TO_INSET_OFFSET;
208 dim.des += TEXT_TO_INSET_OFFSET;
209 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
213 void InsetText::draw(PainterInfo & pi, int x, int y) const
215 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
217 if (drawFrame_ || pi.full_repaint) {
218 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
219 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
220 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
221 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
223 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
225 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
227 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
231 docstring const InsetText::editMessage() const
233 return _("Opened Text Inset");
237 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
239 pit_type const pit = front ? 0 : paragraphs().size() - 1;
240 pos_type pos = front ? 0 : paragraphs().back().size();
242 // if visual information is not to be ignored, move to extreme right/left
243 if (entry_from != ENTRY_DIRECTION_IGNORE) {
244 Cursor temp_cur = cur;
245 temp_cur.pit() = pit;
246 temp_cur.pos() = pos;
247 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
248 pos = temp_cur.pos();
251 text_.setCursor(cur.top(), pit, pos);
252 cur.clearSelection();
257 Inset * InsetText::editXY(Cursor & cur, int x, int y)
259 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
263 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
265 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
266 << " [ cmd.action = " << cmd.action << ']');
267 text_.dispatch(cur, cmd);
271 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
272 FuncStatus & status) const
274 return text_.getStatus(cur, cmd, status);
278 void InsetText::setChange(Change const & change)
280 ParagraphList::iterator pit = paragraphs().begin();
281 ParagraphList::iterator end = paragraphs().end();
282 for (; pit != end; ++pit) {
283 pit->setChange(change);
288 void InsetText::acceptChanges(BufferParams const & bparams)
290 text_.acceptChanges(bparams);
294 void InsetText::rejectChanges(BufferParams const & bparams)
296 text_.rejectChanges(bparams);
300 int InsetText::latex(Buffer const & buf, odocstream & os,
301 OutputParams const & runparams) const
304 latexParagraphs(buf, text_, os, texrow, runparams);
305 return texrow.rows();
309 int InsetText::plaintext(Buffer const & buf, odocstream & os,
310 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(buf, *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(Buffer const & buf, odocstream & os,
337 OutputParams const & runparams) const
339 docbookParagraphs(paragraphs(), buf, os, runparams);
344 void InsetText::validate(LaTeXFeatures & features) const
346 for_each(paragraphs().begin(), paragraphs().end(),
347 bind(&Paragraph::validate, _1, ref(features)));
351 void InsetText::cursorPos(BufferView const & bv,
352 CursorSlice const & sl, bool boundary, int & x, int & y) const
354 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
355 y = bv.textMetrics(&text_).cursorY(sl, boundary);
359 bool InsetText::showInsetDialog(BufferView *) const
365 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
368 Paragraph & first = paragraphs().front();
369 for (unsigned int i = 0; i < data.length(); ++i)
370 first.insertChar(i, data[i], font, trackChanges);
374 void InsetText::setAutoBreakRows(bool flag)
376 if (flag == text_.autoBreakRows_)
379 text_.autoBreakRows_ = flag;
383 // remove previously existing newlines
384 ParagraphList::iterator it = paragraphs().begin();
385 ParagraphList::iterator end = paragraphs().end();
386 for (; it != end; ++it)
387 for (int i = 0; i < it->size(); ++i)
388 if (it->isNewline(i))
389 // do not track the change, because the user
390 // is not allowed to revert/reject it
391 it->eraseChar(i, false);
395 void InsetText::setDrawFrame(bool flag)
401 ColorCode InsetText::frameColor() const
407 void InsetText::setFrameColor(ColorCode col)
413 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
415 // There is little we can do here to keep track of changes.
416 // As of 2006/10/20, appendParagraphs is used exclusively by
417 // LyXTabular::setMultiColumn. In this context, the paragraph break
418 // is lost irreversibly and the appended text doesn't really change
420 ParagraphList & pl = paragraphs();
422 ParagraphList::iterator pit = plist.begin();
423 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
425 mergeParagraph(buffer->params(), pl,
426 distance(pl.begin(), ins) - 1);
428 for_each(pit, plist.end(),
429 bind(&ParagraphList::push_back, ref(pl), _1));
433 void InsetText::addPreview(PreviewLoader & loader) const
435 ParagraphList::const_iterator pit = paragraphs().begin();
436 ParagraphList::const_iterator pend = paragraphs().end();
438 for (; pit != pend; ++pit) {
439 InsetList::const_iterator it = pit->insetList().begin();
440 InsetList::const_iterator end = pit->insetList().end();
441 for (; it != end; ++it)
442 it->inset->addPreview(loader);
447 //FIXME: instead of this hack, which only works by chance,
448 // cells should have their own insetcell type, which returns CELL_CODE!
449 bool InsetText::neverIndent(Buffer const & buffer) const
451 // this is only true for tabular cells
452 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
456 ParagraphList const & InsetText::paragraphs() const
458 return text_.paragraphs();
462 ParagraphList & InsetText::paragraphs()
464 return text_.paragraphs();
468 void InsetText::updateLabels(Buffer const & buf, ParIterator const & it)
470 ParIterator it2 = it;
472 BOOST_ASSERT(&it2.inset() == this && it2.pit() == 0);
473 lyx::updateLabels(buf, it2);
477 bool InsetText::completionSupported(Cursor const & cur) const
479 Cursor const & bvCur = cur.bv().cursor();
480 if (&bvCur.inset() != this)
482 Paragraph const & par = cur.paragraph();
484 && !par.isLetter(cur.pos())
485 && par.isLetter(cur.pos() - 1);
489 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
491 return completionSupported(cur);
495 bool InsetText::automaticInlineCompletion() const
497 return lyxrc.completion_inline_text;
501 bool InsetText::automaticPopupCompletion() const
503 return lyxrc.completion_popup_text;
507 Inset::CompletionList const * InsetText::completionList(Cursor const & cur) const
509 if (!completionSupported(cur))
512 return new TextCompletionList(cur);
516 docstring InsetText::previousWord(Buffer const & buffer, CursorSlice const & sl) const
518 CursorSlice from = sl;
520 text_.getWord(from, to, PREVIOUS_WORD);
521 if (sl == from || to == from)
524 Paragraph const & par = sl.paragraph();
525 return par.asString(buffer, from.pos(), to.pos(), false);
529 docstring InsetText::completionPrefix(Cursor const & cur) const
531 if (!completionSupported(cur))
534 return previousWord(cur.buffer(), cur.top());
538 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
541 if (!completionSupported(cur))
544 BOOST_ASSERT(cur.bv().cursor() == cur);
546 cur.bv().cursor() = cur;
551 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
552 Dimension & dim) const
554 // get word in front of cursor
555 docstring word = previousWord(cur.buffer(), cur.top());
556 DocIterator wordStart = cur;
557 wordStart.pos() -= word.length();
559 // get position on screen of the word start
560 Point lxy = cur.bv().getPos(wordStart, false);
564 // Calculate dimensions of the word
565 TextMetrics const & tm = cur.bv().textMetrics(&text_);
566 dim = tm.rowHeight(cur.pit(), wordStart.pos(), cur.pos(), false);
567 Point rxy = cur.bv().getPos(cur, cur.boundary());
568 dim.wid = abs(rxy.x_ - x);
569 x = (rxy.x_ < x) ? x - dim.wid : x; // for RTL