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"
15 #include "buffer_funcs.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "CompletionList.h"
20 #include "CoordCache.h"
22 #include "CutAndPaste.h"
23 #include "DispatchResult.h"
24 #include "ErrorList.h"
25 #include "FuncRequest.h"
26 #include "InsetList.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"
43 #include "TextClass.h"
45 #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 "support/lassert.h"
58 using namespace lyx::support;
65 using graphics::PreviewLoader;
68 /////////////////////////////////////////////////////////////////////
70 InsetText::InsetText(Buffer const & buf)
71 : drawFrame_(false), frame_color_(Color_insetframe)
77 InsetText::InsetText(InsetText const & in)
80 text_.autoBreakRows_ = in.text_.autoBreakRows_;
81 drawFrame_ = in.drawFrame_;
82 frame_color_ = in.frame_color_;
83 text_.paragraphs() = in.text_.paragraphs();
88 InsetText::InsetText()
92 void InsetText::initParagraphs(Buffer const & buf)
94 LASSERT(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);
102 void InsetText::setParagraphOwner()
104 for_each(paragraphs().begin(), paragraphs().end(),
105 bind(&Paragraph::setInsetOwner, _1, this));
109 void InsetText::clear()
111 ParagraphList & pars = paragraphs();
112 LASSERT(!pars.empty(), /**/);
114 // This is a gross hack...
115 Layout const & old_layout = pars.begin()->layout();
118 pars.push_back(Paragraph());
119 pars.begin()->setInsetOwner(this);
120 pars.begin()->setLayout(old_layout);
124 Dimension const InsetText::dimension(BufferView const & bv) const
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;
135 void InsetText::write(ostream & os) const
138 text_.write(buffer(), os);
142 void InsetText::read(Lexer & lex)
146 // delete the initial paragraph
147 Paragraph oldpar = *paragraphs().begin();
148 paragraphs().clear();
150 lex.setContext("InsetText::read");
151 bool res = text_.read(buffer(), lex, errorList, this);
154 lex.printError("Missing \\end_inset at this point. ");
157 // ensure we have at least one paragraph.
158 if (paragraphs().empty())
159 paragraphs().push_back(oldpar);
163 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
165 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
167 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
169 // Hand font through to contained lyxtext:
170 tm.font_.fontInfo() = mi.base.font;
171 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
173 tm.metrics(mi, dim, mi.base.textwidth);
176 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
177 dim.asc += TEXT_TO_INSET_OFFSET;
178 dim.des += TEXT_TO_INSET_OFFSET;
179 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
183 void InsetText::draw(PainterInfo & pi, int x, int y) const
185 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
187 if (drawFrame_ || pi.full_repaint) {
188 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
189 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
190 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
191 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
193 pi.pain.fillRectangle(xframe, yframe, w, h, backgroundColor());
195 pi.pain.rectangle(xframe, yframe, w, h, frameColor());
197 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
201 docstring InsetText::editMessage() const
203 return _("Opened Text Inset");
207 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
209 pit_type const pit = front ? 0 : paragraphs().size() - 1;
210 pos_type pos = front ? 0 : paragraphs().back().size();
212 // if visual information is not to be ignored, move to extreme right/left
213 if (entry_from != ENTRY_DIRECTION_IGNORE) {
214 Cursor temp_cur = cur;
215 temp_cur.pit() = pit;
216 temp_cur.pos() = pos;
217 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
218 pos = temp_cur.pos();
221 text_.setCursor(cur.top(), pit, pos);
222 cur.clearSelection();
227 Inset * InsetText::editXY(Cursor & cur, int x, int y)
229 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
233 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
235 LYXERR(Debug::ACTION, "InsetText::doDispatch()"
236 << " [ cmd.action = " << cmd.action << ']');
237 text_.dispatch(cur, cmd);
241 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
242 FuncStatus & status) const
244 return text_.getStatus(cur, cmd, status);
248 void InsetText::setChange(Change const & change)
250 ParagraphList::iterator pit = paragraphs().begin();
251 ParagraphList::iterator end = paragraphs().end();
252 for (; pit != end; ++pit) {
253 pit->setChange(change);
258 void InsetText::acceptChanges(BufferParams const & bparams)
260 text_.acceptChanges(bparams);
264 void InsetText::rejectChanges(BufferParams const & bparams)
266 text_.rejectChanges(bparams);
270 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
273 latexParagraphs(buffer(), text_, os, texrow, runparams);
274 return texrow.rows();
278 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
280 ParagraphList::const_iterator beg = paragraphs().begin();
281 ParagraphList::const_iterator end = paragraphs().end();
282 ParagraphList::const_iterator it = beg;
283 bool ref_printed = false;
285 for (; it != end; ++it) {
288 if (runparams.linelen > 0)
291 odocstringstream oss;
292 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
293 docstring const str = oss.str();
295 // FIXME: len is not computed fully correctly; in principle,
296 // we have to count the characters after the last '\n'
304 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
306 docbookParagraphs(paragraphs(), buffer(), os, runparams);
311 void InsetText::validate(LaTeXFeatures & features) const
313 for_each(paragraphs().begin(), paragraphs().end(),
314 bind(&Paragraph::validate, _1, ref(features)));
318 void InsetText::cursorPos(BufferView const & bv,
319 CursorSlice const & sl, bool boundary, int & x, int & y) const
321 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
322 y = bv.textMetrics(&text_).cursorY(sl, boundary);
326 bool InsetText::showInsetDialog(BufferView *) const
332 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
335 Paragraph & first = paragraphs().front();
336 for (unsigned int i = 0; i < data.length(); ++i)
337 first.insertChar(i, data[i], font, trackChanges);
341 void InsetText::setAutoBreakRows(bool flag)
343 if (flag == text_.autoBreakRows_)
346 text_.autoBreakRows_ = flag;
350 // remove previously existing newlines
351 ParagraphList::iterator it = paragraphs().begin();
352 ParagraphList::iterator end = paragraphs().end();
353 for (; it != end; ++it)
354 for (int i = 0; i < it->size(); ++i)
355 if (it->isNewline(i))
356 // do not track the change, because the user
357 // is not allowed to revert/reject it
358 it->eraseChar(i, false);
362 void InsetText::setDrawFrame(bool flag)
368 ColorCode InsetText::frameColor() const
374 void InsetText::setFrameColor(ColorCode col)
380 void InsetText::appendParagraphs(ParagraphList & plist)
382 // There is little we can do here to keep track of changes.
383 // As of 2006/10/20, appendParagraphs is used exclusively by
384 // LyXTabular::setMultiColumn. In this context, the paragraph break
385 // is lost irreversibly and the appended text doesn't really change
387 ParagraphList & pl = paragraphs();
389 ParagraphList::iterator pit = plist.begin();
390 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
392 mergeParagraph(buffer().params(), pl,
393 distance(pl.begin(), ins) - 1);
395 for_each(pit, plist.end(),
396 bind(&ParagraphList::push_back, ref(pl), _1));
400 void InsetText::addPreview(PreviewLoader & loader) const
402 ParagraphList::const_iterator pit = paragraphs().begin();
403 ParagraphList::const_iterator pend = paragraphs().end();
405 for (; pit != pend; ++pit) {
406 InsetList::const_iterator it = pit->insetList().begin();
407 InsetList::const_iterator end = pit->insetList().end();
408 for (; it != end; ++it)
409 it->inset->addPreview(loader);
414 ParagraphList const & InsetText::paragraphs() const
416 return text_.paragraphs();
420 ParagraphList & InsetText::paragraphs()
422 return text_.paragraphs();
426 //void InsetInclude::addToToc(DocIterator const & cpit)
431 void InsetText::updateLabels(ParIterator const & it)
433 ParIterator it2 = it;
435 LASSERT(&it2.inset() == this && it2.pit() == 0, /**/);
436 if (producesOutput())
437 lyx::updateLabels(buffer(), it2);
439 DocumentClass const & tclass = buffer().params().documentClass();
440 Counters const savecnt = tclass.counters();
441 lyx::updateLabels(buffer(), it2);
442 tclass.counters() = savecnt;
447 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
449 if (cur.buffer().isClean())
450 return Inset::notifyCursorLeaves(old, cur);
452 // find text inset in old cursor
453 Cursor insetCur = old;
454 int scriptSlice = insetCur.find(this);
455 LASSERT(scriptSlice != -1, /**/);
456 insetCur.cutOff(scriptSlice);
457 LASSERT(&insetCur.inset() == this, /**/);
459 // update the old paragraph's words
460 insetCur.paragraph().updateWords(insetCur.top());
462 return Inset::notifyCursorLeaves(old, cur);
466 bool InsetText::completionSupported(Cursor const & cur) const
468 Cursor const & bvCur = cur.bv().cursor();
469 if (&bvCur.inset() != this)
471 return text_.completionSupported(cur);
475 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
477 return completionSupported(cur);
481 bool InsetText::automaticInlineCompletion() const
483 return lyxrc.completion_inline_text;
487 bool InsetText::automaticPopupCompletion() const
489 return lyxrc.completion_popup_text;
493 bool InsetText::showCompletionCursor() const
495 return lyxrc.completion_cursor_text;
499 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
501 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
505 docstring InsetText::completionPrefix(Cursor const & cur) const
507 if (!completionSupported(cur))
509 return text_.completionPrefix(cur);
513 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
516 if (!completionSupported(cur))
519 return text_.insertCompletion(cur, s, finished);
523 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
524 Dimension & dim) const
526 TextMetrics const & tm = cur.bv().textMetrics(&text_);
527 tm.completionPosAndDim(cur, x, y, dim);
531 docstring InsetText::contextMenu(BufferView const &, int, int) const
533 return from_ascii("context-edit");