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)
74 : autoBreakRows_(false), drawFrame_(NEVER),
75 frame_color_(LColor::insetframe), text_(0, true)
77 paragraphs().push_back(Paragraph());
78 paragraphs().begin()->layout(bp.getLyXTextClass().defaultLayout());
79 if (bp.tracking_changes)
80 paragraphs().begin()->trackChanges();
85 InsetText::InsetText(InsetText const & in)
86 : UpdatableInset(in), text_(in.text_.bv_owner, true)
93 void InsetText::operator=(InsetText const & in)
95 UpdatableInset::operator=(in);
96 autoBreakRows_ = in.autoBreakRows_;
97 drawFrame_ = in.drawFrame_;
98 frame_color_ = in.frame_color_;
99 text_ = LyXText(in.text_.bv_owner, true);
100 text_.paragraphs() = in.text_.paragraphs();
105 void InsetText::init()
107 ParagraphList::iterator pit = paragraphs().begin();
108 ParagraphList::iterator end = paragraphs().end();
109 for (; pit != end; ++pit)
110 pit->setInsetOwner(this);
112 in_insetAllowed = false;
116 void InsetText::clear(bool just_mark_erased)
118 ParagraphList & pars = paragraphs();
119 if (just_mark_erased) {
120 ParagraphList::iterator it = pars.begin();
121 ParagraphList::iterator end = pars.end();
122 for (; it != end; ++it)
127 // This is a gross hack...
128 LyXLayout_ptr old_layout = pars.begin()->layout();
131 pars.push_back(Paragraph());
132 pars.begin()->setInsetOwner(this);
133 pars.begin()->layout(old_layout);
137 auto_ptr<InsetBase> InsetText::clone() const
139 return auto_ptr<InsetBase>(new InsetText(*this));
143 void InsetText::write(Buffer const & buf, ostream & os) const
146 writeParagraphData(buf, os);
150 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
152 ParagraphList::const_iterator it = paragraphs().begin();
153 ParagraphList::const_iterator end = paragraphs().end();
154 Paragraph::depth_type dth = 0;
155 for (; it != end; ++it) {
156 it->write(buf, os, buf.params(), dth);
161 void InsetText::read(Buffer const & buf, LyXLex & lex)
164 Paragraph::depth_type depth = 0;
168 #warning John, look here. Doesnt make much sense.
169 if (buf.params().tracking_changes)
170 paragraphs().begin()->trackChanges();
172 // delete the initial paragraph
173 Paragraph oldpar = *paragraphs().begin();
174 paragraphs().clear();
175 ParagraphList::iterator pit = paragraphs().begin();
179 token = lex.getString();
182 if (token == "\\end_inset") {
186 if (token == "\\end_document") {
187 lex.printError("\\end_document read in inset! Error in document!");
192 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs(), pit, depth);
195 pit = paragraphs().begin();
196 ParagraphList::iterator const end = paragraphs().end();
197 for (; pit != end; ++pit)
198 pit->setInsetOwner(this);
200 if (token != "\\end_inset") {
201 lex.printError("Missing \\end_inset at this point. "
206 // ensure we have at least one par.
207 if (paragraphs().empty())
208 paragraphs().push_back(oldpar);
212 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
214 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
215 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
216 setViewCache(mi.base.bv);
217 text_.metrics(mi, dim);
218 dim.asc += TEXT_TO_INSET_OFFSET;
219 dim.des += TEXT_TO_INSET_OFFSET;
220 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
221 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
226 void InsetText::draw(PainterInfo & pi, int x, int y) const
228 // update our idea of where we are
232 Painter & pain = pi.pain;
234 // repaint the background if needed
235 x += TEXT_TO_INSET_OFFSET;
236 if (backgroundColor() != LColor::background)
237 clearInset(pain, x, y);
239 BufferView * bv = pi.base.bv;
244 y += bv->top_y() - text_.firstRow()->ascent_of_text();
246 text_.draw(pi, x, y);
248 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
249 drawFrame(pain, xo_);
253 void InsetText::drawFrame(Painter & pain, int x) const
255 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
256 int const frame_x = x + ttoD2;
257 int const frame_y = yo_ - dim_.asc + ttoD2;
258 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
259 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
260 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
264 void InsetText::updateLocal(BufferView * bv)
269 if (!autoBreakRows_ && paragraphs().size() > 1)
270 collapseParagraphs(bv);
272 if (!text_.selection.set())
273 text_.selection.cursor = text_.cursor;
275 bv->owner()->view_state_changed();
276 bv->owner()->updateMenubar();
277 bv->owner()->updateToolbar();
278 if (old_par != text_.cursor.par()) {
279 bv->owner()->setLayout(text_.cursorPar()->layout()->name());
280 old_par = text_.cursor.par();
285 string const InsetText::editMessage() const
287 return _("Opened Text Inset");
291 void InsetText::sanitizeEmptyText(BufferView * bv)
293 if (paragraphs().size() == 1
294 && paragraphs().begin()->empty()
295 && bv->getParentLanguage(this) != text_.current_font.language()) {
296 LyXFont font(LyXFont::ALL_IGNORE);
297 font.setLanguage(bv->getParentLanguage(this));
298 text_.setFont(font, false);
303 extern LCursor theTempCursor;
306 void InsetText::edit(BufferView * bv, bool left)
308 lyxerr << "InsetText: edit left/right" << endl;
314 text_.setCursorIntern(0, 0);
316 text_.setCursor(paragraphs().size() - 1, paragraphs().back().size());
318 sanitizeEmptyText(bv);
320 bv->updateParagraphDialog();
324 void InsetText::edit(BufferView * bv, int x, int y)
326 lyxerr << "InsetText::edit xy" << endl;
328 sanitizeEmptyText(bv);
329 text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
331 text_.clearSelection();
335 bv->updateParagraphDialog();
339 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
340 idx_type &, pos_type &)
342 lyxerr << "InsetText::priv_dispatch (begin), act: "
343 << cmd.action << " " << endl;
345 BufferView * bv = cmd.view();
348 DispatchResult result;
349 result.dispatched(true);
351 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
353 switch (cmd.action) {
354 case LFUN_MOUSE_PRESS:
355 bv->cursor() = theTempCursor;
358 result = text_.dispatch(cmd);
362 // If the action has deleted all text in the inset, we need
363 // to change the language to the language of the surronding
365 if (!was_empty && paragraphs().begin()->empty() &&
366 paragraphs().size() == 1) {
367 LyXFont font(LyXFont::ALL_IGNORE);
368 font.setLanguage(bv->getParentLanguage(this));
369 text_.setFont(font, false);
372 lyxerr << "InsetText::priv_dispatch (end)" << endl;
377 int InsetText::latex(Buffer const & buf, ostream & os,
378 OutputParams const & runparams) const
381 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
382 return texrow.rows();
386 int InsetText::plaintext(Buffer const & buf, ostream & os,
387 OutputParams const & runparams) const
389 ParagraphList::const_iterator beg = paragraphs().begin();
390 ParagraphList::const_iterator end = paragraphs().end();
391 ParagraphList::const_iterator it = beg;
392 for (; it != end; ++it)
393 asciiParagraph(buf, *it, os, runparams, it == beg);
395 //FIXME: Give the total numbers of lines
400 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
401 OutputParams const & runparams) const
403 linuxdocParagraphs(buf, paragraphs(), os, runparams);
408 int InsetText::docbook(Buffer const & buf, ostream & os,
409 OutputParams const & runparams) const
411 docbookParagraphs(buf, paragraphs(), os, runparams);
416 void InsetText::validate(LaTeXFeatures & features) const
418 for_each(paragraphs().begin(), paragraphs().end(),
419 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
423 void InsetText::getCursorPos(int & x, int & y) const
425 x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
426 y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
430 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
432 inset->setOwner(this);
433 text_.insertInset(inset);
435 #warning should we mark the buffer dirty?
440 bool InsetText::insetAllowed(InsetOld::Code code) const
442 // in_insetAllowed is a really gross hack,
443 // to allow us to call the owner's insetAllowed
444 // without stack overflow, which can happen
445 // when the owner uses InsetCollapsable::insetAllowed()
449 in_insetAllowed = true;
451 ret = owner()->insetAllowed(code);
452 in_insetAllowed = false;
457 bool InsetText::showInsetDialog(BufferView *) const
463 void InsetText::getLabelList(Buffer const & buffer,
464 std::vector<string> & list) const
466 ParagraphList::const_iterator pit = paragraphs().begin();
467 ParagraphList::const_iterator pend = paragraphs().end();
468 for (; pit != pend; ++pit) {
469 InsetList::const_iterator beg = pit->insetlist.begin();
470 InsetList::const_iterator end = pit->insetlist.end();
471 for (; beg != end; ++beg)
472 beg->inset->getLabelList(buffer, list);
477 void InsetText::markNew(bool track_changes)
479 ParagraphList::iterator pit = paragraphs().begin();
480 ParagraphList::iterator end = paragraphs().end();
481 for (; pit != end; ++pit) {
485 // no-op when not tracking
492 void InsetText::setText(string const & data, LyXFont const & font)
495 for (unsigned int i = 0; i < data.length(); ++i)
496 paragraphs().begin()->insertChar(i, data[i], font);
500 void InsetText::setAutoBreakRows(bool flag)
502 if (flag != autoBreakRows_) {
503 autoBreakRows_ = flag;
510 void InsetText::setDrawFrame(DrawFrame how)
516 LColor_color InsetText::frameColor() const
518 return LColor::color(frame_color_);
522 void InsetText::setFrameColor(LColor_color col)
528 void InsetText::setViewCache(BufferView const * bv) const
530 if (bv && bv != text_.bv_owner) {
531 //lyxerr << "setting view cache from "
532 // << text_.bv_owner << " to " << bv << "\n";
533 text_.bv_owner = const_cast<BufferView *>(bv);
538 void InsetText::removeNewlines()
540 ParagraphList::iterator it = paragraphs().begin();
541 ParagraphList::iterator end = paragraphs().end();
542 for (; it != end; ++it)
543 for (int i = 0; i < it->size(); ++i)
544 if (it->isNewline(i))
549 int InsetText::scroll(bool /*recursive*/) const
551 return UpdatableInset::scroll(false);
555 void InsetText::clearInset(Painter & pain, int x, int y) const
558 int h = dim_.asc + dim_.des;
559 int ty = y - dim_.asc;
565 if (ty + h > pain.paperHeight())
566 h = pain.paperHeight();
567 if (xo_ + w > pain.paperWidth())
568 w = pain.paperWidth();
569 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
573 ParagraphList * InsetText::getParagraphs(int i) const
575 return (i == 0) ? const_cast<ParagraphList*>(¶graphs()) : 0;
579 LyXText * InsetText::getText(int i) const
581 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
585 void InsetText::collapseParagraphs(BufferView * bv)
587 while (paragraphs().size() > 1) {
588 ParagraphList::iterator const first = paragraphs().begin();
589 ParagraphList::iterator second = first;
591 size_t const first_par_size = first->size();
593 if (!first->empty() &&
595 !first->isSeparator(first_par_size - 1)) {
596 first->insertChar(first_par_size, ' ');
599 #warning probably broken
600 if (text_.selection.set()) {
601 if (text_.selection.start.par() == 1) {
602 text_.selection.start.par(1);
603 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
605 if (text_.selection.end.par() == 2) {
606 text_.selection.end.par(1);
607 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
611 mergeParagraph(bv->buffer()->params(), paragraphs(), first);
616 void InsetText::getDrawFont(LyXFont & font) const
619 owner()->getDrawFont(font);
623 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
625 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
626 // And it probably does. You have to take a look at this John. (Lgb)
627 #warning John, have a look here. (Lgb)
628 ParagraphList::iterator pit = plist.begin();
629 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
631 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
633 ParagraphList::iterator pend = plist.end();
634 for (; pit != pend; ++pit)
635 paragraphs().push_back(*pit);
639 void InsetText::addPreview(PreviewLoader & loader) const
641 ParagraphList::const_iterator pit = paragraphs().begin();
642 ParagraphList::const_iterator pend = paragraphs().end();
644 for (; pit != pend; ++pit) {
645 InsetList::const_iterator it = pit->insetlist.begin();
646 InsetList::const_iterator end = pit->insetlist.end();
647 for (; it != end; ++it)
648 it->inset->addPreview(loader);
653 ParagraphList & InsetText::paragraphs() const
655 return const_cast<ParagraphList &>(text_.paragraphs());