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 text_.write(buf, os);
150 void InsetText::read(Buffer const & buf, LyXLex & lex)
153 Paragraph::depth_type depth = 0;
157 #warning John, look here. Doesnt make much sense.
158 if (buf.params().tracking_changes)
159 paragraphs().begin()->trackChanges();
161 // delete the initial paragraph
162 Paragraph oldpar = *paragraphs().begin();
163 paragraphs().clear();
164 ParagraphList::iterator pit = paragraphs().begin();
168 token = lex.getString();
171 if (token == "\\end_inset") {
175 if (token == "\\end_document") {
176 lex.printError("\\end_document read in inset! Error in document!");
181 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs(), pit, depth);
184 pit = paragraphs().begin();
185 ParagraphList::iterator const end = paragraphs().end();
186 for (; pit != end; ++pit)
187 pit->setInsetOwner(this);
189 if (token != "\\end_inset") {
190 lex.printError("Missing \\end_inset at this point. "
195 // ensure we have at least one par.
196 if (paragraphs().empty())
197 paragraphs().push_back(oldpar);
201 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
203 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
204 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
205 setViewCache(mi.base.bv);
206 text_.metrics(mi, dim);
207 dim.asc += TEXT_TO_INSET_OFFSET;
208 dim.des += TEXT_TO_INSET_OFFSET;
209 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
210 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
215 void InsetText::draw(PainterInfo & pi, int x, int y) const
217 // update our idea of where we are
221 Painter & pain = pi.pain;
223 // repaint the background if needed
224 x += TEXT_TO_INSET_OFFSET;
225 if (backgroundColor() != LColor::background)
226 clearInset(pain, x, y);
228 BufferView * bv = pi.base.bv;
233 y += bv->top_y() - text_.firstRow()->ascent_of_text();
235 text_.draw(pi, x, y);
237 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
238 drawFrame(pain, xo_);
242 void InsetText::drawFrame(Painter & pain, int x) const
244 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
245 int const frame_x = x + ttoD2;
246 int const frame_y = yo_ - dim_.asc + ttoD2;
247 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
248 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
249 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
253 void InsetText::updateLocal(BufferView * bv)
258 if (!autoBreakRows_ && paragraphs().size() > 1)
259 collapseParagraphs(bv);
261 if (!text_.selection.set())
262 text_.selection.cursor = text_.cursor;
264 bv->owner()->view_state_changed();
265 bv->owner()->updateMenubar();
266 bv->owner()->updateToolbar();
267 if (old_par != text_.cursor.par()) {
268 bv->owner()->setLayout(text_.cursorPar()->layout()->name());
269 old_par = text_.cursor.par();
274 string const InsetText::editMessage() const
276 return _("Opened Text Inset");
280 void InsetText::sanitizeEmptyText(BufferView * bv)
282 if (paragraphs().size() == 1
283 && paragraphs().begin()->empty()
284 && bv->getParentLanguage(this) != text_.current_font.language()) {
285 LyXFont font(LyXFont::ALL_IGNORE);
286 font.setLanguage(bv->getParentLanguage(this));
287 text_.setFont(font, false);
292 extern LCursor theTempCursor;
295 void InsetText::edit(BufferView * bv, bool left)
297 lyxerr << "InsetText: edit left/right" << endl;
303 text_.setCursorIntern(0, 0);
305 text_.setCursor(paragraphs().size() - 1, paragraphs().back().size());
307 sanitizeEmptyText(bv);
309 bv->updateParagraphDialog();
313 void InsetText::edit(BufferView * bv, int x, int y)
315 lyxerr << "InsetText::edit xy" << endl;
317 sanitizeEmptyText(bv);
318 text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
320 text_.clearSelection();
324 bv->updateParagraphDialog();
328 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
329 idx_type &, pos_type &)
331 lyxerr << "InsetText::priv_dispatch (begin), act: "
332 << cmd.action << " " << endl;
334 BufferView * bv = cmd.view();
337 DispatchResult result;
338 result.dispatched(true);
340 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
342 switch (cmd.action) {
343 case LFUN_MOUSE_PRESS:
344 bv->cursor() = theTempCursor;
347 result = text_.dispatch(cmd);
351 // If the action has deleted all text in the inset, we need
352 // to change the language to the language of the surronding
354 if (!was_empty && paragraphs().begin()->empty() &&
355 paragraphs().size() == 1) {
356 LyXFont font(LyXFont::ALL_IGNORE);
357 font.setLanguage(bv->getParentLanguage(this));
358 text_.setFont(font, false);
361 lyxerr << "InsetText::priv_dispatch (end)" << endl;
366 int InsetText::latex(Buffer const & buf, ostream & os,
367 OutputParams const & runparams) const
370 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
371 return texrow.rows();
375 int InsetText::plaintext(Buffer const & buf, ostream & os,
376 OutputParams const & runparams) const
378 ParagraphList::const_iterator beg = paragraphs().begin();
379 ParagraphList::const_iterator end = paragraphs().end();
380 ParagraphList::const_iterator it = beg;
381 for (; it != end; ++it)
382 asciiParagraph(buf, *it, os, runparams, it == beg);
384 //FIXME: Give the total numbers of lines
389 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
390 OutputParams const & runparams) const
392 linuxdocParagraphs(buf, paragraphs(), os, runparams);
397 int InsetText::docbook(Buffer const & buf, ostream & os,
398 OutputParams const & runparams) const
400 docbookParagraphs(buf, paragraphs(), os, runparams);
405 void InsetText::validate(LaTeXFeatures & features) const
407 for_each(paragraphs().begin(), paragraphs().end(),
408 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
412 void InsetText::getCursorPos(int & x, int & y) const
414 x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
415 y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
419 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
421 inset->setOwner(this);
422 text_.insertInset(inset);
424 #warning should we mark the buffer dirty?
429 bool InsetText::insetAllowed(InsetOld::Code code) const
431 // in_insetAllowed is a really gross hack,
432 // to allow us to call the owner's insetAllowed
433 // without stack overflow, which can happen
434 // when the owner uses InsetCollapsable::insetAllowed()
438 in_insetAllowed = true;
440 ret = owner()->insetAllowed(code);
441 in_insetAllowed = false;
446 bool InsetText::showInsetDialog(BufferView *) const
452 void InsetText::getLabelList(Buffer const & buffer,
453 std::vector<string> & list) const
455 ParagraphList::const_iterator pit = paragraphs().begin();
456 ParagraphList::const_iterator pend = paragraphs().end();
457 for (; pit != pend; ++pit) {
458 InsetList::const_iterator beg = pit->insetlist.begin();
459 InsetList::const_iterator end = pit->insetlist.end();
460 for (; beg != end; ++beg)
461 beg->inset->getLabelList(buffer, list);
466 void InsetText::markNew(bool track_changes)
468 ParagraphList::iterator pit = paragraphs().begin();
469 ParagraphList::iterator end = paragraphs().end();
470 for (; pit != end; ++pit) {
474 // no-op when not tracking
481 void InsetText::setText(string const & data, LyXFont const & font)
484 for (unsigned int i = 0; i < data.length(); ++i)
485 paragraphs().begin()->insertChar(i, data[i], font);
489 void InsetText::setAutoBreakRows(bool flag)
491 if (flag != autoBreakRows_) {
492 autoBreakRows_ = flag;
499 void InsetText::setDrawFrame(DrawFrame how)
505 LColor_color InsetText::frameColor() const
507 return LColor::color(frame_color_);
511 void InsetText::setFrameColor(LColor_color col)
517 void InsetText::setViewCache(BufferView const * bv) const
519 if (bv && bv != text_.bv_owner) {
520 //lyxerr << "setting view cache from "
521 // << text_.bv_owner << " to " << bv << "\n";
522 text_.bv_owner = const_cast<BufferView *>(bv);
527 void InsetText::removeNewlines()
529 ParagraphList::iterator it = paragraphs().begin();
530 ParagraphList::iterator end = paragraphs().end();
531 for (; it != end; ++it)
532 for (int i = 0; i < it->size(); ++i)
533 if (it->isNewline(i))
538 int InsetText::scroll(bool /*recursive*/) const
540 return UpdatableInset::scroll(false);
544 void InsetText::clearInset(Painter & pain, int x, int y) const
547 int h = dim_.asc + dim_.des;
548 int ty = y - dim_.asc;
554 if (ty + h > pain.paperHeight())
555 h = pain.paperHeight();
556 if (xo_ + w > pain.paperWidth())
557 w = pain.paperWidth();
558 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
562 ParagraphList * InsetText::getParagraphs(int i) const
564 return (i == 0) ? const_cast<ParagraphList*>(¶graphs()) : 0;
568 LyXText * InsetText::getText(int i) const
570 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
574 void InsetText::collapseParagraphs(BufferView * bv)
576 while (paragraphs().size() > 1) {
577 ParagraphList::iterator const first = paragraphs().begin();
578 ParagraphList::iterator second = first;
580 size_t const first_par_size = first->size();
582 if (!first->empty() &&
584 !first->isSeparator(first_par_size - 1)) {
585 first->insertChar(first_par_size, ' ');
588 #warning probably broken
589 if (text_.selection.set()) {
590 if (text_.selection.start.par() == 1) {
591 text_.selection.start.par(1);
592 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
594 if (text_.selection.end.par() == 2) {
595 text_.selection.end.par(1);
596 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
600 mergeParagraph(bv->buffer()->params(), paragraphs(), first);
605 void InsetText::getDrawFont(LyXFont & font) const
608 owner()->getDrawFont(font);
612 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
614 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
615 // And it probably does. You have to take a look at this John. (Lgb)
616 #warning John, have a look here. (Lgb)
617 ParagraphList::iterator pit = plist.begin();
618 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
620 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
622 ParagraphList::iterator pend = plist.end();
623 for (; pit != pend; ++pit)
624 paragraphs().push_back(*pit);
628 void InsetText::addPreview(PreviewLoader & loader) const
630 ParagraphList::const_iterator pit = paragraphs().begin();
631 ParagraphList::const_iterator pend = paragraphs().end();
633 for (; pit != pend; ++pit) {
634 InsetList::const_iterator it = pit->insetlist.begin();
635 InsetList::const_iterator end = pit->insetlist.end();
636 for (; it != end; ++it)
637 it->inset->addPreview(loader);
642 ParagraphList & InsetText::paragraphs() const
644 return const_cast<ParagraphList &>(text_.paragraphs());