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>
57 using lyx::graphics::PreviewLoader;
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
71 InsetText::InsetText(BufferParams const & bp)
72 : autoBreakRows_(false), drawFrame_(NEVER),
73 frame_color_(LColor::insetframe), text_(0)
75 paragraphs().push_back(Paragraph());
76 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
77 if (bp.tracking_changes)
78 paragraphs().back().trackChanges();
83 InsetText::InsetText(InsetText const & in)
84 : UpdatableInset(in), text_(in.text_.bv_owner)
91 void InsetText::operator=(InsetText const & in)
93 UpdatableInset::operator=(in);
94 autoBreakRows_ = in.autoBreakRows_;
95 drawFrame_ = in.drawFrame_;
96 frame_color_ = in.frame_color_;
97 text_ = LyXText(in.text_.bv_owner);
98 text_.paragraphs() = in.text_.paragraphs();
103 void InsetText::init()
105 ParagraphList::iterator pit = paragraphs().begin();
106 ParagraphList::iterator end = paragraphs().end();
107 for (; pit != end; ++pit)
108 pit->setInsetOwner(this);
110 in_insetAllowed = false;
114 void InsetText::clear(bool just_mark_erased)
116 ParagraphList & pars = paragraphs();
117 if (just_mark_erased) {
118 ParagraphList::iterator it = pars.begin();
119 ParagraphList::iterator end = pars.end();
120 for (; it != end; ++it)
125 // This is a gross hack...
126 LyXLayout_ptr old_layout = pars.begin()->layout();
129 pars.push_back(Paragraph());
130 pars.begin()->setInsetOwner(this);
131 pars.begin()->layout(old_layout);
135 auto_ptr<InsetBase> InsetText::clone() const
137 return auto_ptr<InsetBase>(new InsetText(*this));
141 void InsetText::write(Buffer const & buf, ostream & os) const
144 text_.write(buf, os);
148 void InsetText::read(Buffer const & buf, LyXLex & lex)
152 #warning John, look here. Doesnt make much sense.
153 if (buf.params().tracking_changes)
154 paragraphs().begin()->trackChanges();
156 // delete the initial paragraph
157 Paragraph oldpar = *paragraphs().begin();
158 paragraphs().clear();
159 bool res = text_.read(buf, lex);
163 lex.printError("Missing \\end_inset at this point. "
168 // ensure we have at least one par.
169 if (paragraphs().empty())
170 paragraphs().push_back(oldpar);
174 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
176 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
177 setViewCache(mi.base.bv);
178 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
179 text_.metrics(mi, dim);
180 dim.asc += TEXT_TO_INSET_OFFSET;
181 dim.des += TEXT_TO_INSET_OFFSET;
182 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
183 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
185 font_ = mi.base.font;
186 text_.font_ = mi.base.font;
190 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
193 // update our idea of where we are
194 setPosCache(pi, x, y);
196 // repaint the background if needed
197 x += TEXT_TO_INSET_OFFSET;
198 if (backgroundColor() != LColor::background)
199 clearInset(pi.pain, x, y);
201 BufferView * bv = pi.base.bv;
206 y += bv->top_y() - text_.ascent();
208 text_.draw(pi, x, y);
210 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
211 drawFrame(pi.pain, xo_, yo_ - bv->top_y());
215 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
217 text_.drawSelection(pi, x, y);
221 void InsetText::drawFrame(Painter & pain, int x, int y) const
223 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
224 int const frame_x = x + ttoD2;
225 int const frame_y = y - dim_.asc + ttoD2;
226 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
227 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
228 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
232 void InsetText::updateLocal(LCursor & cur)
234 if (!autoBreakRows_ && paragraphs().size() > 1) {
235 // collapseParagraphs
236 while (paragraphs().size() > 1) {
237 ParagraphList::iterator const first = paragraphs().begin();
238 ParagraphList::iterator second = first;
240 size_t const first_par_size = first->size();
242 if (!first->empty() &&
244 !first->isSeparator(first_par_size - 1)) {
245 first->insertChar(first_par_size, ' ');
248 cur.clearSelection();
249 mergeParagraph(cur.bv().buffer()->params(), paragraphs(), first);
253 if (!cur.selection())
256 LyXView * lv = cur.bv().owner();
257 lv->view_state_changed();
260 if (old_par != cur.par()) {
261 lv->setLayout(text_.getPar(cur.par())->layout()->name());
267 string const InsetText::editMessage() const
269 return _("Opened Text Inset");
273 void InsetText::sanitizeEmptyText(BufferView & bv)
275 if (paragraphs().size() == 1
276 && paragraphs().begin()->empty()
277 && bv.getParentLanguage(this) != text_.current_font.language()) {
278 LyXFont font(LyXFont::ALL_IGNORE);
279 font.setLanguage(bv.getParentLanguage(this));
280 text_.setFont(bv.cursor(), font, false);
285 void InsetText::edit(LCursor & cur, bool left)
287 //lyxerr << "InsetText: edit left/right" << endl;
289 setViewCache(&cur.bv());
290 int const par = left ? 0 : paragraphs().size() - 1;
291 int const pos = left ? 0 : paragraphs().back().size();
292 text_.setCursor(cur.top(), par, pos);
293 cur.clearSelection();
295 sanitizeEmptyText(cur.bv());
300 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
302 lyxerr << "InsetText::edit xy" << endl;
304 return text_.editXY(cur, x, y);
305 //sanitizeEmptyText(cur.bv());
310 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
312 //lyxerr << "InsetText::priv_dispatch (begin), act: "
313 // << cmd.action << " " << endl;
315 setViewCache(&cur.bv());
317 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
318 text_.dispatch(cur, cmd);
320 // If the action has deleted all text in the inset, we need
321 // to change the language to the language of the surronding
323 // Why this cleverness? (Andre')
324 if (!was_empty && paragraphs().begin()->empty() &&
325 paragraphs().size() == 1) {
326 LyXFont font(LyXFont::ALL_IGNORE);
327 font.setLanguage(cur.bv().getParentLanguage(this));
328 text_.setFont(cur, font, false);
331 //lyxerr << "InsetText::priv_dispatch (end)" << endl;
335 int InsetText::latex(Buffer const & buf, ostream & os,
336 OutputParams const & runparams) const
339 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
340 return texrow.rows();
344 int InsetText::plaintext(Buffer const & buf, ostream & os,
345 OutputParams const & runparams) const
347 ParagraphList::const_iterator beg = paragraphs().begin();
348 ParagraphList::const_iterator end = paragraphs().end();
349 ParagraphList::const_iterator it = beg;
350 for (; it != end; ++it)
351 asciiParagraph(buf, *it, os, runparams, it == beg);
353 //FIXME: Give the total numbers of lines
358 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
359 OutputParams const & runparams) const
361 linuxdocParagraphs(buf, paragraphs(), os, runparams);
366 int InsetText::docbook(Buffer const & buf, ostream & os,
367 OutputParams const & runparams) const
369 docbookParagraphs(buf, paragraphs(), os, runparams);
374 void InsetText::validate(LaTeXFeatures & features) const
376 for_each(paragraphs().begin(), paragraphs().end(),
377 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
381 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
383 x = text_.cursorX(cur);
384 y = text_.cursorY(cur);
388 bool InsetText::insetAllowed(InsetOld::Code code) const
390 // in_insetAllowed is a really gross hack,
391 // to allow us to call the owner's insetAllowed
392 // without stack overflow, which can happen
393 // when the owner uses InsetCollapsable::insetAllowed()
396 in_insetAllowed = true;
397 bool const ret = owner() && owner()->insetAllowed(code);
398 in_insetAllowed = false;
403 bool InsetText::showInsetDialog(BufferView *) const
409 void InsetText::getLabelList(Buffer const & buffer,
410 std::vector<string> & list) const
412 ParagraphList::const_iterator pit = paragraphs().begin();
413 ParagraphList::const_iterator pend = paragraphs().end();
414 for (; pit != pend; ++pit) {
415 InsetList::const_iterator beg = pit->insetlist.begin();
416 InsetList::const_iterator end = pit->insetlist.end();
417 for (; beg != end; ++beg)
418 beg->inset->getLabelList(buffer, list);
423 void InsetText::markNew(bool track_changes)
425 ParagraphList::iterator pit = paragraphs().begin();
426 ParagraphList::iterator end = paragraphs().end();
427 for (; pit != end; ++pit) {
431 // no-op when not tracking
438 void InsetText::setText(string const & data, LyXFont const & font)
441 for (unsigned int i = 0; i < data.length(); ++i)
442 paragraphs().begin()->insertChar(i, data[i], font);
446 void InsetText::setAutoBreakRows(bool flag)
448 if (flag != autoBreakRows_) {
449 autoBreakRows_ = flag;
456 void InsetText::setDrawFrame(DrawFrame how)
462 LColor_color InsetText::frameColor() const
464 return LColor::color(frame_color_);
468 void InsetText::setFrameColor(LColor_color col)
474 void InsetText::setViewCache(BufferView const * bv) const
476 if (bv && bv != text_.bv_owner) {
477 //lyxerr << "setting view cache from "
478 // << text_.bv_owner << " to " << bv << "\n";
479 text_.bv_owner = const_cast<BufferView *>(bv);
484 void InsetText::removeNewlines()
486 ParagraphList::iterator it = paragraphs().begin();
487 ParagraphList::iterator end = paragraphs().end();
488 for (; it != end; ++it)
489 for (int i = 0; i < it->size(); ++i)
490 if (it->isNewline(i))
495 int InsetText::scroll(bool /*recursive*/) const
497 return UpdatableInset::scroll(false);
501 void InsetText::clearInset(Painter & pain, int x, int y) const
504 int h = dim_.asc + dim_.des;
505 int ty = y - dim_.asc;
511 if (ty + h > pain.paperHeight())
512 h = pain.paperHeight();
513 if (xo_ + w > pain.paperWidth())
514 w = pain.paperWidth();
515 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
519 LyXText * InsetText::getText(int i) const
521 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
525 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
527 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
528 // And it probably does. You have to take a look at this John. (Lgb)
529 #warning John, have a look here. (Lgb)
530 ParagraphList::iterator pit = plist.begin();
531 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
533 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
535 ParagraphList::iterator pend = plist.end();
536 for (; pit != pend; ++pit)
537 paragraphs().push_back(*pit);
541 void InsetText::addPreview(PreviewLoader & loader) const
543 ParagraphList::const_iterator pit = paragraphs().begin();
544 ParagraphList::const_iterator pend = paragraphs().end();
546 for (; pit != pend; ++pit) {
547 InsetList::const_iterator it = pit->insetlist.begin();
548 InsetList::const_iterator end = pit->insetlist.end();
549 for (; it != end; ++it)
550 it->inset->addPreview(loader);
555 ParagraphList & InsetText::paragraphs() const
557 return const_cast<ParagraphList &>(text_.paragraphs());