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 InsetText::InsetText() : text_(0)
95 void InsetText::operator=(InsetText const & in)
97 UpdatableInset::operator=(in);
98 autoBreakRows_ = in.autoBreakRows_;
99 drawFrame_ = in.drawFrame_;
100 frame_color_ = in.frame_color_;
101 text_ = LyXText(in.text_.bv_owner);
102 text_.paragraphs() = in.text_.paragraphs();
107 void InsetText::init()
109 ParagraphList::iterator pit = paragraphs().begin();
110 ParagraphList::iterator end = paragraphs().end();
111 for (; pit != end; ++pit)
112 pit->setInsetOwner(this);
117 void InsetText::clear(bool just_mark_erased)
119 ParagraphList & pars = paragraphs();
120 if (just_mark_erased) {
121 ParagraphList::iterator it = pars.begin();
122 ParagraphList::iterator end = pars.end();
123 for (; it != end; ++it)
128 // This is a gross hack...
129 LyXLayout_ptr old_layout = pars.begin()->layout();
132 pars.push_back(Paragraph());
133 pars.begin()->setInsetOwner(this);
134 pars.begin()->layout(old_layout);
138 auto_ptr<InsetBase> InsetText::clone() const
140 return auto_ptr<InsetBase>(new InsetText(*this));
144 void InsetText::write(Buffer const & buf, ostream & os) const
147 text_.write(buf, os);
151 void InsetText::read(Buffer const & buf, LyXLex & lex)
155 #warning John, look here. Doesnt make much sense.
156 if (buf.params().tracking_changes)
157 paragraphs().begin()->trackChanges();
159 // delete the initial paragraph
160 Paragraph oldpar = *paragraphs().begin();
161 paragraphs().clear();
162 bool res = text_.read(buf, lex);
166 lex.printError("Missing \\end_inset at this point. "
171 // ensure we have at least one par.
172 if (paragraphs().empty())
173 paragraphs().push_back(oldpar);
177 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
179 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
180 setViewCache(mi.base.bv);
181 text_.metrics(mi, dim);
183 font_ = mi.base.font;
184 text_.font_ = mi.base.font;
188 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
191 // update our idea of where we are
192 setPosCache(pi, x, y);
194 BufferView * bv = pi.base.bv;
200 // repaint the background if needed
201 if (backgroundColor() != LColor::background)
202 clearInset(pi.pain, x, y);
204 text_.draw(pi, x, y + bv->top_y());
206 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
207 drawFrame(pi.pain, x, y);
211 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
213 text_.drawSelection(pi, x, y);
217 void InsetText::drawFrame(Painter & pain, int x, int y) const
219 int const w = text_.width();
220 int const h = text_.height();
221 pain.rectangle(x, y, w, h, frameColor());
225 void InsetText::clearInset(Painter & pain, int x, int y) const
227 int const w = text_.width();
228 int const h = text_.height();
229 pain.fillRectangle(x, y, w, h, backgroundColor());
233 void InsetText::updateLocal(LCursor & cur)
235 if (!autoBreakRows_ && paragraphs().size() > 1) {
236 // collapseParagraphs
237 while (paragraphs().size() > 1) {
238 ParagraphList::iterator const first = paragraphs().begin();
239 ParagraphList::iterator second = first;
241 size_t const first_par_size = first->size();
243 if (!first->empty() &&
245 !first->isSeparator(first_par_size - 1)) {
246 first->insertChar(first_par_size, ' ');
249 cur.clearSelection();
250 mergeParagraph(cur.bv().buffer()->params(), paragraphs(), 0);
254 if (!cur.selection())
257 LyXView * lv = cur.bv().owner();
258 lv->view_state_changed();
261 if (old_par != cur.par()) {
262 lv->setLayout(text_.getPar(cur.par()).layout()->name());
268 string const InsetText::editMessage() const
270 return _("Opened Text Inset");
274 void InsetText::sanitizeEmptyText(BufferView & bv)
276 if (paragraphs().size() == 1
277 && paragraphs().begin()->empty()
278 && bv.getParentLanguage(this) != text_.current_font.language()) {
279 LyXFont font(LyXFont::ALL_IGNORE);
280 font.setLanguage(bv.getParentLanguage(this));
281 text_.setFont(bv.cursor(), font, false);
286 void InsetText::edit(LCursor & cur, bool left)
288 //lyxerr << "InsetText: edit left/right" << endl;
290 setViewCache(&cur.bv());
291 int const par = left ? 0 : paragraphs().size() - 1;
292 int const pos = left ? 0 : paragraphs().back().size();
293 text_.setCursor(cur.top(), par, pos);
294 cur.clearSelection();
296 sanitizeEmptyText(cur.bv());
301 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
303 lyxerr << "InsetText::edit xy" << endl;
305 return text_.editXY(cur, x, y);
306 //sanitizeEmptyText(cur.bv());
311 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
313 //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
314 setViewCache(&cur.bv());
316 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
317 text_.dispatch(cur, cmd);
319 // If the action has deleted all text in the inset, we need
320 // to change the language to the language of the surronding
322 // Why this cleverness? (Andre')
323 if (!was_empty && paragraphs().begin()->empty() &&
324 paragraphs().size() == 1) {
325 LyXFont font(LyXFont::ALL_IGNORE);
326 font.setLanguage(cur.bv().getParentLanguage(this));
327 text_.setFont(cur, font, false);
332 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
333 FuncStatus & status) const
335 return text_.getStatus(cur, cmd, status);
339 int InsetText::latex(Buffer const & buf, ostream & os,
340 OutputParams const & runparams) const
343 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
344 return texrow.rows();
348 int InsetText::plaintext(Buffer const & buf, ostream & os,
349 OutputParams const & runparams) const
351 ParagraphList::const_iterator beg = paragraphs().begin();
352 ParagraphList::const_iterator end = paragraphs().end();
353 ParagraphList::const_iterator it = beg;
354 for (; it != end; ++it)
355 asciiParagraph(buf, *it, os, runparams, it == beg);
357 //FIXME: Give the total numbers of lines
362 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
363 OutputParams const & runparams) const
365 linuxdocParagraphs(buf, paragraphs(), os, runparams);
370 int InsetText::docbook(Buffer const & buf, ostream & os,
371 OutputParams const & runparams) const
373 docbookParagraphs(buf, paragraphs(), os, runparams);
378 void InsetText::validate(LaTeXFeatures & features) const
380 for_each(paragraphs().begin(), paragraphs().end(),
381 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
385 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
387 x = text_.cursorX(cur);
388 y = text_.cursorY(cur);
392 bool InsetText::showInsetDialog(BufferView *) const
398 void InsetText::getLabelList(Buffer const & buffer,
399 std::vector<string> & list) const
401 ParagraphList::const_iterator pit = paragraphs().begin();
402 ParagraphList::const_iterator pend = paragraphs().end();
403 for (; pit != pend; ++pit) {
404 InsetList::const_iterator beg = pit->insetlist.begin();
405 InsetList::const_iterator end = pit->insetlist.end();
406 for (; beg != end; ++beg)
407 beg->inset->getLabelList(buffer, list);
412 void InsetText::markNew(bool track_changes)
414 ParagraphList::iterator pit = paragraphs().begin();
415 ParagraphList::iterator end = paragraphs().end();
416 for (; pit != end; ++pit) {
420 // no-op when not tracking
427 void InsetText::setText(string const & data, LyXFont const & font)
430 for (unsigned int i = 0; i < data.length(); ++i)
431 paragraphs().begin()->insertChar(i, data[i], font);
435 void InsetText::setAutoBreakRows(bool flag)
437 if (flag != autoBreakRows_) {
438 autoBreakRows_ = flag;
445 void InsetText::setDrawFrame(DrawFrame how)
451 LColor_color InsetText::frameColor() const
453 return LColor::color(frame_color_);
457 void InsetText::setFrameColor(LColor_color col)
463 void InsetText::setViewCache(BufferView const * bv) const
465 if (bv && bv != text_.bv_owner) {
466 //lyxerr << "setting view cache from "
467 // << text_.bv_owner << " to " << bv << "\n";
468 text_.bv_owner = const_cast<BufferView *>(bv);
473 void InsetText::removeNewlines()
475 ParagraphList::iterator it = paragraphs().begin();
476 ParagraphList::iterator end = paragraphs().end();
477 for (; it != end; ++it)
478 for (int i = 0; i < it->size(); ++i)
479 if (it->isNewline(i))
484 LyXText * InsetText::getText(int i) const
486 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
490 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
492 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
493 // And it probably does. You have to take a look at this John. (Lgb)
494 #warning John, have a look here. (Lgb)
495 ParagraphList::iterator pit = plist.begin();
496 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
498 mergeParagraph(buffer->params(), paragraphs(),
499 ins - paragraphs().begin() - 1);
501 ParagraphList::iterator pend = plist.end();
502 for (; pit != pend; ++pit)
503 paragraphs().push_back(*pit);
507 void InsetText::addPreview(PreviewLoader & loader) const
509 ParagraphList::const_iterator pit = paragraphs().begin();
510 ParagraphList::const_iterator pend = paragraphs().end();
512 for (; pit != pend; ++pit) {
513 InsetList::const_iterator it = pit->insetlist.begin();
514 InsetList::const_iterator end = pit->insetlist.end();
515 for (; it != end; ++it)
516 it->inset->addPreview(loader);
521 ParagraphList const & InsetText::paragraphs() const
523 return text_.paragraphs();
527 ParagraphList & InsetText::paragraphs()
529 return text_.paragraphs();