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 "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_linuxdoc.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
46 #include "frontends/Alert.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/Painter.h"
51 #include "support/lyxalgo.h" // lyx::count
53 #include <boost/bind.hpp>
55 using bv_funcs::replaceSelection;
59 using lyx::graphics::PreviewLoader;
61 using lyx::support::isStrUnsignedInt;
62 using lyx::support::strToUnsignedInt;
73 InsetText::InsetText(BufferParams const & bp)
76 autoBreakRows_(false),
78 frame_color_(LColor::insetframe),
79 text_(0, this, true, paragraphs)
81 paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
82 if (bp.tracking_changes)
83 paragraphs.begin()->trackChanges();
88 InsetText::InsetText(InsetText const & in)
90 text_(in.text_.bv_owner, this, true, paragraphs)
97 void InsetText::operator=(InsetText const & in)
99 UpdatableInset::operator=(in);
100 paragraphs = in.paragraphs;
101 autoBreakRows_ = in.autoBreakRows_;
102 drawFrame_ = in.drawFrame_;
103 frame_color_ = in.frame_color_;
104 text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
109 void InsetText::init()
111 ParagraphList::iterator pit = paragraphs.begin();
112 ParagraphList::iterator end = paragraphs.end();
113 for (; pit != end; ++pit)
114 pit->setInsetOwner(this);
115 text_.paragraphs_ = ¶graphs;
117 in_insetAllowed = false;
121 void InsetText::clear(bool just_mark_erased)
123 if (just_mark_erased) {
124 ParagraphList::iterator it = paragraphs.begin();
125 ParagraphList::iterator end = paragraphs.end();
126 for (; it != end; ++it)
131 // This is a gross hack...
132 LyXLayout_ptr old_layout = paragraphs.begin()->layout();
135 paragraphs.push_back(Paragraph());
136 paragraphs.begin()->setInsetOwner(this);
137 paragraphs.begin()->layout(old_layout);
141 auto_ptr<InsetBase> InsetText::clone() const
143 return auto_ptr<InsetBase>(new InsetText(*this));
147 void InsetText::write(Buffer const & buf, ostream & os) const
150 writeParagraphData(buf, os);
154 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
156 ParagraphList::const_iterator it = paragraphs.begin();
157 ParagraphList::const_iterator end = paragraphs.end();
158 Paragraph::depth_type dth = 0;
159 for (; it != end; ++it) {
160 it->write(buf, os, buf.params(), dth);
165 void InsetText::read(Buffer const & buf, LyXLex & lex)
168 Paragraph::depth_type depth = 0;
172 #warning John, look here. Doesnt make much sense.
173 if (buf.params().tracking_changes)
174 paragraphs.begin()->trackChanges();
176 // delete the initial paragraph
177 Paragraph oldpar = *paragraphs.begin();
179 ParagraphList::iterator pit = paragraphs.begin();
183 token = lex.getString();
186 if (token == "\\end_inset") {
190 if (token == "\\end_document") {
191 lex.printError("\\end_document read in inset! Error in document!");
196 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
199 pit = paragraphs.begin();
200 ParagraphList::iterator const end = paragraphs.end();
201 for (; pit != end; ++pit)
202 pit->setInsetOwner(this);
204 if (token != "\\end_inset") {
205 lex.printError("Missing \\end_inset at this point. "
210 // ensure we have at least one par.
211 if (paragraphs.empty())
212 paragraphs.push_back(oldpar);
216 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
218 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
219 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
220 setViewCache(mi.base.bv);
221 text_.metrics(mi, dim);
222 dim.asc += TEXT_TO_INSET_OFFSET;
223 dim.des += TEXT_TO_INSET_OFFSET;
224 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
225 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
230 void InsetText::draw(PainterInfo & pi, int x, int y) const
232 // update our idea of where we are
236 Painter & pain = pi.pain;
238 // repaint the background if needed
239 x += TEXT_TO_INSET_OFFSET;
240 if (backgroundColor() != LColor::background)
241 clearInset(pain, x, y);
243 BufferView * bv = pi.base.bv;
248 y += bv->top_y() - text_.firstRow()->ascent_of_text();
250 text_.draw(pi, x, y);
252 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
253 drawFrame(pain, xo_);
257 void InsetText::drawFrame(Painter & pain, int x) const
259 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
260 int const frame_x = x + ttoD2;
261 int const frame_y = yo_ - dim_.asc + ttoD2;
262 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
263 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
264 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
268 void InsetText::updateLocal(BufferView * bv)
273 if (!autoBreakRows_ && paragraphs.size() > 1)
274 collapseParagraphs(bv);
276 if (!text_.selection.set())
277 text_.selection.cursor = text_.cursor;
279 bv->owner()->view_state_changed();
280 bv->owner()->updateMenubar();
281 bv->owner()->updateToolbar();
282 if (old_par != text_.cursor.par()) {
283 bv->owner()->setLayout(text_.cursorPar()->layout()->name());
284 old_par = text_.cursor.par();
289 string const InsetText::editMessage() const
291 return _("Opened Text Inset");
295 void InsetText::sanitizeEmptyText(BufferView * bv)
297 if (paragraphs.size() == 1
298 && paragraphs.begin()->empty()
299 && bv->getParentLanguage(this) != text_.current_font.language()) {
300 LyXFont font(LyXFont::ALL_IGNORE);
301 font.setLanguage(bv->getParentLanguage(this));
302 text_.setFont(font, false);
307 extern LCursor theTempCursor;
310 void InsetText::edit(BufferView * bv, bool left)
312 lyxerr << "InsetText: edit left/right" << endl;
318 text_.setCursorIntern(0, 0);
320 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
322 sanitizeEmptyText(bv);
324 bv->updateParagraphDialog();
328 void InsetText::edit(BufferView * bv, int x, int y)
330 lyxerr << "InsetText::edit xy" << endl;
332 sanitizeEmptyText(bv);
333 text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
335 text_.clearSelection();
339 bv->updateParagraphDialog();
343 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
344 idx_type &, pos_type &)
346 lyxerr << "InsetText::priv_dispatch (begin), act: "
347 << cmd.action << " " << endl;
349 BufferView * bv = cmd.view();
352 DispatchResult result;
353 result.dispatched(true);
355 bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
357 switch (cmd.action) {
358 case LFUN_MOUSE_PRESS:
359 bv->cursor() = theTempCursor;
362 result = text_.dispatch(cmd);
366 // If the action has deleted all text in the inset, we need
367 // to change the language to the language of the surronding
369 if (!was_empty && paragraphs.begin()->empty() &&
370 paragraphs.size() == 1) {
371 LyXFont font(LyXFont::ALL_IGNORE);
372 font.setLanguage(bv->getParentLanguage(this));
373 text_.setFont(font, false);
376 lyxerr << "InsetText::priv_dispatch (end)" << endl;
381 int InsetText::latex(Buffer const & buf, ostream & os,
382 OutputParams const & runparams) const
385 latexParagraphs(buf, paragraphs, os, texrow, runparams);
386 return texrow.rows();
390 int InsetText::plaintext(Buffer const & buf, ostream & os,
391 OutputParams const & runparams) const
393 ParagraphList::const_iterator beg = paragraphs.begin();
394 ParagraphList::const_iterator end = paragraphs.end();
395 ParagraphList::const_iterator it = beg;
396 for (; it != end; ++it)
397 asciiParagraph(buf, *it, os, runparams, it == beg);
399 //FIXME: Give the total numbers of lines
404 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
405 OutputParams const & runparams) const
407 linuxdocParagraphs(buf, paragraphs, os, runparams);
412 int InsetText::docbook(Buffer const & buf, ostream & os,
413 OutputParams const & runparams) const
415 docbookParagraphs(buf, paragraphs, os, runparams);
420 void InsetText::validate(LaTeXFeatures & features) const
422 for_each(paragraphs.begin(), paragraphs.end(),
423 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
427 void InsetText::getCursorPos(int & x, int & y) const
429 x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
430 y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
434 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
436 inset->setOwner(this);
437 text_.insertInset(inset);
439 #warning should we mark the buffer dirty?
444 bool InsetText::insetAllowed(InsetOld::Code code) const
446 // in_insetAllowed is a really gross hack,
447 // to allow us to call the owner's insetAllowed
448 // without stack overflow, which can happen
449 // when the owner uses InsetCollapsable::insetAllowed()
453 in_insetAllowed = true;
455 ret = owner()->insetAllowed(code);
456 in_insetAllowed = false;
461 bool InsetText::showInsetDialog(BufferView *) const
467 void InsetText::getLabelList(Buffer const & buffer,
468 std::vector<string> & list) const
470 ParagraphList::const_iterator pit = paragraphs.begin();
471 ParagraphList::const_iterator pend = paragraphs.end();
472 for (; pit != pend; ++pit) {
473 InsetList::const_iterator beg = pit->insetlist.begin();
474 InsetList::const_iterator end = pit->insetlist.end();
475 for (; beg != end; ++beg)
476 beg->inset->getLabelList(buffer, list);
481 void InsetText::markNew(bool track_changes)
483 ParagraphList::iterator pit = paragraphs.begin();
484 ParagraphList::iterator end = paragraphs.end();
485 for (; pit != end; ++pit) {
489 // no-op when not tracking
496 void InsetText::setText(string const & data, LyXFont const & font)
499 for (unsigned int i = 0; i < data.length(); ++i)
500 paragraphs.begin()->insertChar(i, data[i], font);
504 void InsetText::setAutoBreakRows(bool flag)
506 if (flag != autoBreakRows_) {
507 autoBreakRows_ = flag;
514 void InsetText::setDrawFrame(DrawFrame how)
520 LColor_color InsetText::frameColor() const
522 return LColor::color(frame_color_);
526 void InsetText::setFrameColor(LColor_color col)
532 void InsetText::setViewCache(BufferView const * bv) const
534 if (bv && bv != text_.bv_owner) {
535 //lyxerr << "setting view cache from "
536 // << text_.bv_owner << " to " << bv << "\n";
537 text_.bv_owner = const_cast<BufferView *>(bv);
542 void InsetText::removeNewlines()
544 ParagraphList::iterator it = paragraphs.begin();
545 ParagraphList::iterator end = paragraphs.end();
546 for (; it != end; ++it)
547 for (int i = 0; i < it->size(); ++i)
548 if (it->isNewline(i))
553 int InsetText::scroll(bool /*recursive*/) const
555 return UpdatableInset::scroll(false);
559 void InsetText::clearInset(Painter & pain, int x, int y) const
562 int h = dim_.asc + dim_.des;
563 int ty = y - dim_.asc;
569 if (ty + h > pain.paperHeight())
570 h = pain.paperHeight();
571 if (xo_ + w > pain.paperWidth())
572 w = pain.paperWidth();
573 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
577 ParagraphList * InsetText::getParagraphs(int i) const
579 return (i == 0) ? const_cast<ParagraphList*>(¶graphs) : 0;
583 LyXText * InsetText::getText(int i) const
585 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
589 bool InsetText::checkInsertChar(LyXFont & font)
591 return owner() ? owner()->checkInsertChar(font) : true;
595 void InsetText::collapseParagraphs(BufferView * bv)
597 while (paragraphs.size() > 1) {
598 ParagraphList::iterator const first = paragraphs.begin();
599 ParagraphList::iterator second = first;
601 size_t const first_par_size = first->size();
603 if (!first->empty() &&
605 !first->isSeparator(first_par_size - 1)) {
606 first->insertChar(first_par_size, ' ');
609 #warning probably broken
610 if (text_.selection.set()) {
611 if (text_.selection.start.par() == 1) {
612 text_.selection.start.par(1);
613 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
615 if (text_.selection.end.par() == 2) {
616 text_.selection.end.par(1);
617 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
621 mergeParagraph(bv->buffer()->params(), paragraphs, first);
626 void InsetText::getDrawFont(LyXFont & font) const
629 owner()->getDrawFont(font);
633 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
635 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
636 // And it probably does. You have to take a look at this John. (Lgb)
637 #warning John, have a look here. (Lgb)
638 ParagraphList::iterator pit = plist.begin();
639 ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
641 mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
643 ParagraphList::iterator pend = plist.end();
644 for (; pit != pend; ++pit)
645 paragraphs.push_back(*pit);
649 void InsetText::addPreview(PreviewLoader & loader) const
651 ParagraphList::const_iterator pit = paragraphs.begin();
652 ParagraphList::const_iterator pend = paragraphs.end();
654 for (; pit != pend; ++pit) {
655 InsetList::const_iterator it = pit->insetlist.begin();
656 InsetList::const_iterator end = pit->insetlist.end();
657 for (; it != end; ++it)
658 it->inset->addPreview(loader);