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)
154 #warning John, look here. Doesnt make much sense.
155 if (buf.params().tracking_changes)
156 paragraphs().begin()->trackChanges();
158 // delete the initial paragraph
159 Paragraph oldpar = *paragraphs().begin();
160 paragraphs().clear();
161 bool res = text_.read(buf, lex);
165 lex.printError("Missing \\end_inset at this point. "
170 // ensure we have at least one par.
171 if (paragraphs().empty())
172 paragraphs().push_back(oldpar);
176 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
178 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
179 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
180 setViewCache(mi.base.bv);
181 text_.metrics(mi, dim);
182 dim.asc += TEXT_TO_INSET_OFFSET;
183 dim.des += TEXT_TO_INSET_OFFSET;
184 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
185 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 // update our idea of where we are
196 Painter & pain = pi.pain;
198 // repaint the background if needed
199 x += TEXT_TO_INSET_OFFSET;
200 if (backgroundColor() != LColor::background)
201 clearInset(pain, x, y);
203 BufferView * bv = pi.base.bv;
208 y += bv->top_y() - text_.firstRow()->ascent_of_text();
210 text_.draw(pi, x, y);
212 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
213 drawFrame(pain, xo_);
217 void InsetText::drawFrame(Painter & pain, int x) const
219 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
220 int const frame_x = x + ttoD2;
221 int const frame_y = yo_ - dim_.asc + ttoD2;
222 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
223 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
224 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
228 void InsetText::updateLocal(BufferView * bv)
233 if (!autoBreakRows_ && paragraphs().size() > 1)
234 collapseParagraphs(bv);
236 if (!text_.selection.set())
237 text_.selection.cursor = text_.cursor;
239 bv->owner()->view_state_changed();
240 bv->owner()->updateMenubar();
241 bv->owner()->updateToolbar();
242 if (old_par != text_.cursor.par()) {
243 bv->owner()->setLayout(text_.cursorPar()->layout()->name());
244 old_par = text_.cursor.par();
249 string const InsetText::editMessage() const
251 return _("Opened Text Inset");
255 void InsetText::sanitizeEmptyText(BufferView * bv)
257 if (paragraphs().size() == 1
258 && paragraphs().begin()->empty()
259 && bv->getParentLanguage(this) != text_.current_font.language()) {
260 LyXFont font(LyXFont::ALL_IGNORE);
261 font.setLanguage(bv->getParentLanguage(this));
262 text_.setFont(font, false);
267 extern LCursor theTempCursor;
270 void InsetText::edit(BufferView * bv, bool left)
272 lyxerr << "InsetText: edit left/right" << endl;
278 text_.setCursorIntern(0, 0);
280 text_.setCursor(paragraphs().size() - 1, paragraphs().back().size());
282 sanitizeEmptyText(bv);
284 bv->updateParagraphDialog();
288 void InsetText::edit(BufferView * bv, int x, int y)
290 lyxerr << "InsetText::edit xy" << endl;
292 sanitizeEmptyText(bv);
293 text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
295 text_.clearSelection();
299 bv->updateParagraphDialog();
303 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
304 idx_type &, pos_type &)
306 lyxerr << "InsetText::priv_dispatch (begin), act: "
307 << cmd.action << " " << endl;
309 BufferView * bv = cmd.view();
312 DispatchResult result;
313 result.dispatched(true);
315 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
317 switch (cmd.action) {
318 case LFUN_MOUSE_PRESS:
319 bv->cursor() = theTempCursor;
322 result = text_.dispatch(cmd);
326 // If the action has deleted all text in the inset, we need
327 // to change the language to the language of the surronding
329 if (!was_empty && paragraphs().begin()->empty() &&
330 paragraphs().size() == 1) {
331 LyXFont font(LyXFont::ALL_IGNORE);
332 font.setLanguage(bv->getParentLanguage(this));
333 text_.setFont(font, false);
336 lyxerr << "InsetText::priv_dispatch (end)" << endl;
341 int InsetText::latex(Buffer const & buf, ostream & os,
342 OutputParams const & runparams) const
345 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
346 return texrow.rows();
350 int InsetText::plaintext(Buffer const & buf, ostream & os,
351 OutputParams const & runparams) const
353 ParagraphList::const_iterator beg = paragraphs().begin();
354 ParagraphList::const_iterator end = paragraphs().end();
355 ParagraphList::const_iterator it = beg;
356 for (; it != end; ++it)
357 asciiParagraph(buf, *it, os, runparams, it == beg);
359 //FIXME: Give the total numbers of lines
364 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
365 OutputParams const & runparams) const
367 linuxdocParagraphs(buf, paragraphs(), os, runparams);
372 int InsetText::docbook(Buffer const & buf, ostream & os,
373 OutputParams const & runparams) const
375 docbookParagraphs(buf, paragraphs(), os, runparams);
380 void InsetText::validate(LaTeXFeatures & features) const
382 for_each(paragraphs().begin(), paragraphs().end(),
383 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
387 void InsetText::getCursorPos(int & x, int & y) const
389 x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
390 y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
394 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
396 inset->setOwner(this);
397 text_.insertInset(inset);
399 #warning should we mark the buffer dirty?
404 bool InsetText::insetAllowed(InsetOld::Code code) const
406 // in_insetAllowed is a really gross hack,
407 // to allow us to call the owner's insetAllowed
408 // without stack overflow, which can happen
409 // when the owner uses InsetCollapsable::insetAllowed()
413 in_insetAllowed = true;
415 ret = owner()->insetAllowed(code);
416 in_insetAllowed = false;
421 bool InsetText::showInsetDialog(BufferView *) const
427 void InsetText::getLabelList(Buffer const & buffer,
428 std::vector<string> & list) const
430 ParagraphList::const_iterator pit = paragraphs().begin();
431 ParagraphList::const_iterator pend = paragraphs().end();
432 for (; pit != pend; ++pit) {
433 InsetList::const_iterator beg = pit->insetlist.begin();
434 InsetList::const_iterator end = pit->insetlist.end();
435 for (; beg != end; ++beg)
436 beg->inset->getLabelList(buffer, list);
441 void InsetText::markNew(bool track_changes)
443 ParagraphList::iterator pit = paragraphs().begin();
444 ParagraphList::iterator end = paragraphs().end();
445 for (; pit != end; ++pit) {
449 // no-op when not tracking
456 void InsetText::setText(string const & data, LyXFont const & font)
459 for (unsigned int i = 0; i < data.length(); ++i)
460 paragraphs().begin()->insertChar(i, data[i], font);
464 void InsetText::setAutoBreakRows(bool flag)
466 if (flag != autoBreakRows_) {
467 autoBreakRows_ = flag;
474 void InsetText::setDrawFrame(DrawFrame how)
480 LColor_color InsetText::frameColor() const
482 return LColor::color(frame_color_);
486 void InsetText::setFrameColor(LColor_color col)
492 void InsetText::setViewCache(BufferView const * bv) const
494 if (bv && bv != text_.bv_owner) {
495 //lyxerr << "setting view cache from "
496 // << text_.bv_owner << " to " << bv << "\n";
497 text_.bv_owner = const_cast<BufferView *>(bv);
502 void InsetText::removeNewlines()
504 ParagraphList::iterator it = paragraphs().begin();
505 ParagraphList::iterator end = paragraphs().end();
506 for (; it != end; ++it)
507 for (int i = 0; i < it->size(); ++i)
508 if (it->isNewline(i))
513 int InsetText::scroll(bool /*recursive*/) const
515 return UpdatableInset::scroll(false);
519 void InsetText::clearInset(Painter & pain, int x, int y) const
522 int h = dim_.asc + dim_.des;
523 int ty = y - dim_.asc;
529 if (ty + h > pain.paperHeight())
530 h = pain.paperHeight();
531 if (xo_ + w > pain.paperWidth())
532 w = pain.paperWidth();
533 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
537 ParagraphList * InsetText::getParagraphs(int i) const
539 return (i == 0) ? const_cast<ParagraphList*>(¶graphs()) : 0;
543 LyXText * InsetText::getText(int i) const
545 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
549 void InsetText::collapseParagraphs(BufferView * bv)
551 while (paragraphs().size() > 1) {
552 ParagraphList::iterator const first = paragraphs().begin();
553 ParagraphList::iterator second = first;
555 size_t const first_par_size = first->size();
557 if (!first->empty() &&
559 !first->isSeparator(first_par_size - 1)) {
560 first->insertChar(first_par_size, ' ');
563 #warning probably broken
564 if (text_.selection.set()) {
565 if (text_.selection.start.par() == 1) {
566 text_.selection.start.par(1);
567 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
569 if (text_.selection.end.par() == 2) {
570 text_.selection.end.par(1);
571 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
575 mergeParagraph(bv->buffer()->params(), paragraphs(), first);
580 void InsetText::getDrawFont(LyXFont & font) const
583 owner()->getDrawFont(font);
587 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
589 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
590 // And it probably does. You have to take a look at this John. (Lgb)
591 #warning John, have a look here. (Lgb)
592 ParagraphList::iterator pit = plist.begin();
593 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
595 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
597 ParagraphList::iterator pend = plist.end();
598 for (; pit != pend; ++pit)
599 paragraphs().push_back(*pit);
603 void InsetText::addPreview(PreviewLoader & loader) const
605 ParagraphList::const_iterator pit = paragraphs().begin();
606 ParagraphList::const_iterator pend = paragraphs().end();
608 for (; pit != pend; ++pit) {
609 InsetList::const_iterator it = pit->insetlist.begin();
610 InsetList::const_iterator end = pit->insetlist.end();
611 for (; it != end; ++it)
612 it->inset->addPreview(loader);
617 ParagraphList & InsetText::paragraphs() const
619 return const_cast<ParagraphList &>(text_.paragraphs());