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);
113 void InsetText::clear(bool just_mark_erased)
115 ParagraphList & pars = paragraphs();
116 if (just_mark_erased) {
117 ParagraphList::iterator it = pars.begin();
118 ParagraphList::iterator end = pars.end();
119 for (; it != end; ++it)
124 // This is a gross hack...
125 LyXLayout_ptr old_layout = pars.begin()->layout();
128 pars.push_back(Paragraph());
129 pars.begin()->setInsetOwner(this);
130 pars.begin()->layout(old_layout);
134 auto_ptr<InsetBase> InsetText::clone() const
136 return auto_ptr<InsetBase>(new InsetText(*this));
140 void InsetText::write(Buffer const & buf, ostream & os) const
143 text_.write(buf, os);
147 void InsetText::read(Buffer const & buf, LyXLex & lex)
151 #warning John, look here. Doesnt make much sense.
152 if (buf.params().tracking_changes)
153 paragraphs().begin()->trackChanges();
155 // delete the initial paragraph
156 Paragraph oldpar = *paragraphs().begin();
157 paragraphs().clear();
158 bool res = text_.read(buf, lex);
162 lex.printError("Missing \\end_inset at this point. "
167 // ensure we have at least one par.
168 if (paragraphs().empty())
169 paragraphs().push_back(oldpar);
173 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
175 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
176 setViewCache(mi.base.bv);
177 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
178 text_.metrics(mi, dim);
179 dim.asc += TEXT_TO_INSET_OFFSET;
180 dim.des += TEXT_TO_INSET_OFFSET;
181 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
182 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
184 font_ = mi.base.font;
185 text_.font_ = mi.base.font;
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
191 BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
192 // update our idea of where we are
193 setPosCache(pi, x, y);
195 // repaint the background if needed
196 x += TEXT_TO_INSET_OFFSET;
197 if (backgroundColor() != LColor::background)
198 clearInset(pi.pain, x, y);
200 BufferView * bv = pi.base.bv;
204 y += bv->top_y() - text_.ascent();
206 text_.draw(pi, x, y);
208 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
209 drawFrame(pi.pain, xo_, yo_ - bv->top_y());
213 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
215 text_.drawSelection(pi, x, y);
219 void InsetText::drawFrame(Painter & pain, int x, int y) const
221 int const frame_x = x + TEXT_TO_INSET_OFFSET / 2;
222 int const frame_y = y - dim_.asc + TEXT_TO_INSET_OFFSET / 2;
223 int const frame_w = text_.width();
224 int const frame_h = text_.height();
225 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
229 void InsetText::updateLocal(LCursor & cur)
231 if (!autoBreakRows_ && paragraphs().size() > 1) {
232 // collapseParagraphs
233 while (paragraphs().size() > 1) {
234 ParagraphList::iterator const first = paragraphs().begin();
235 ParagraphList::iterator second = first;
237 size_t const first_par_size = first->size();
239 if (!first->empty() &&
241 !first->isSeparator(first_par_size - 1)) {
242 first->insertChar(first_par_size, ' ');
245 cur.clearSelection();
246 mergeParagraph(cur.bv().buffer()->params(), paragraphs(), 0);
250 if (!cur.selection())
253 LyXView * lv = cur.bv().owner();
254 lv->view_state_changed();
257 if (old_par != cur.par()) {
258 lv->setLayout(text_.getPar(cur.par()).layout()->name());
264 string const InsetText::editMessage() const
266 return _("Opened Text Inset");
270 void InsetText::sanitizeEmptyText(BufferView & bv)
272 if (paragraphs().size() == 1
273 && paragraphs().begin()->empty()
274 && bv.getParentLanguage(this) != text_.current_font.language()) {
275 LyXFont font(LyXFont::ALL_IGNORE);
276 font.setLanguage(bv.getParentLanguage(this));
277 text_.setFont(bv.cursor(), font, false);
282 void InsetText::edit(LCursor & cur, bool left)
284 //lyxerr << "InsetText: edit left/right" << endl;
286 setViewCache(&cur.bv());
287 int const par = left ? 0 : paragraphs().size() - 1;
288 int const pos = left ? 0 : paragraphs().back().size();
289 text_.setCursor(cur.top(), par, pos);
290 cur.clearSelection();
292 sanitizeEmptyText(cur.bv());
297 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
299 lyxerr << "InsetText::edit xy" << endl;
301 return text_.editXY(cur, x, y);
302 //sanitizeEmptyText(cur.bv());
307 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
309 //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
310 setViewCache(&cur.bv());
312 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
313 text_.dispatch(cur, cmd);
315 // If the action has deleted all text in the inset, we need
316 // to change the language to the language of the surronding
318 // Why this cleverness? (Andre')
319 if (!was_empty && paragraphs().begin()->empty() &&
320 paragraphs().size() == 1) {
321 LyXFont font(LyXFont::ALL_IGNORE);
322 font.setLanguage(cur.bv().getParentLanguage(this));
323 text_.setFont(cur, font, false);
328 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
329 FuncStatus & status) const
331 return text_.getStatus(cur, cmd, status);
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::showInsetDialog(BufferView *) const
394 void InsetText::getLabelList(Buffer const & buffer,
395 std::vector<string> & list) const
397 ParagraphList::const_iterator pit = paragraphs().begin();
398 ParagraphList::const_iterator pend = paragraphs().end();
399 for (; pit != pend; ++pit) {
400 InsetList::const_iterator beg = pit->insetlist.begin();
401 InsetList::const_iterator end = pit->insetlist.end();
402 for (; beg != end; ++beg)
403 beg->inset->getLabelList(buffer, list);
408 void InsetText::markNew(bool track_changes)
410 ParagraphList::iterator pit = paragraphs().begin();
411 ParagraphList::iterator end = paragraphs().end();
412 for (; pit != end; ++pit) {
416 // no-op when not tracking
423 void InsetText::setText(string const & data, LyXFont const & font)
426 for (unsigned int i = 0; i < data.length(); ++i)
427 paragraphs().begin()->insertChar(i, data[i], font);
431 void InsetText::setAutoBreakRows(bool flag)
433 if (flag != autoBreakRows_) {
434 autoBreakRows_ = flag;
441 void InsetText::setDrawFrame(DrawFrame how)
447 LColor_color InsetText::frameColor() const
449 return LColor::color(frame_color_);
453 void InsetText::setFrameColor(LColor_color col)
459 void InsetText::setViewCache(BufferView const * bv) const
461 if (bv && bv != text_.bv_owner) {
462 //lyxerr << "setting view cache from "
463 // << text_.bv_owner << " to " << bv << "\n";
464 text_.bv_owner = const_cast<BufferView *>(bv);
469 void InsetText::removeNewlines()
471 ParagraphList::iterator it = paragraphs().begin();
472 ParagraphList::iterator end = paragraphs().end();
473 for (; it != end; ++it)
474 for (int i = 0; i < it->size(); ++i)
475 if (it->isNewline(i))
480 void InsetText::clearInset(Painter & pain, int x, int y) const
483 int h = dim_.asc + dim_.des;
484 int ty = y - dim_.asc;
490 if (ty + h > pain.paperHeight())
491 h = pain.paperHeight();
492 if (xo_ + w > pain.paperWidth())
493 w = pain.paperWidth();
494 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
498 LyXText * InsetText::getText(int i) const
500 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
504 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
506 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
507 // And it probably does. You have to take a look at this John. (Lgb)
508 #warning John, have a look here. (Lgb)
509 ParagraphList::iterator pit = plist.begin();
510 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
512 mergeParagraph(buffer->params(), paragraphs(),
513 ins - paragraphs().begin() - 1);
515 ParagraphList::iterator pend = plist.end();
516 for (; pit != pend; ++pit)
517 paragraphs().push_back(*pit);
521 void InsetText::addPreview(PreviewLoader & loader) const
523 ParagraphList::const_iterator pit = paragraphs().begin();
524 ParagraphList::const_iterator pend = paragraphs().end();
526 for (; pit != pend; ++pit) {
527 InsetList::const_iterator it = pit->insetlist.begin();
528 InsetList::const_iterator end = pit->insetlist.end();
529 for (; it != end; ++it)
530 it->inset->addPreview(loader);
535 ParagraphList & InsetText::paragraphs() const
537 return const_cast<ParagraphList &>(text_.paragraphs());