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)
156 #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 bool res = text_.read(buf, lex);
168 lex.printError("Missing \\end_inset at this point. "
173 // ensure we have at least one par.
174 if (paragraphs().empty())
175 paragraphs().push_back(oldpar);
179 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
181 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
182 setViewCache(mi.base.bv);
183 text_.metrics(mi, dim);
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 BufferView * bv = pi.base.bv;
202 // repaint the background if needed
203 if (backgroundColor() != LColor::background)
204 clearInset(pi.pain, x, y);
206 text_.draw(pi, x, y + bv->top_y());
208 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
209 drawFrame(pi.pain, x, 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 w = text_.width();
222 int const h = text_.height();
223 pain.rectangle(x, y, w, h, frameColor());
227 void InsetText::clearInset(Painter & pain, int x, int y) const
229 int const w = text_.width();
230 int const h = text_.height();
231 pain.fillRectangle(x, y, w, h, backgroundColor());
235 void InsetText::updateLocal(LCursor & cur)
237 if (!autoBreakRows_ && paragraphs().size() > 1) {
238 // collapseParagraphs
239 while (paragraphs().size() > 1) {
240 ParagraphList::iterator const first = paragraphs().begin();
241 ParagraphList::iterator second = first;
243 size_t const first_par_size = first->size();
245 if (!first->empty() &&
247 !first->isSeparator(first_par_size - 1)) {
248 first->insertChar(first_par_size, ' ');
251 cur.clearSelection();
252 mergeParagraph(cur.buffer().params(), paragraphs(), 0);
256 if (!cur.selection())
259 LyXView * lv = cur.bv().owner();
260 lv->view_state_changed();
262 lv->updateToolbars();
263 if (old_par != cur.par()) {
264 lv->setLayout(text_.getPar(cur.par()).layout()->name());
270 string const InsetText::editMessage() const
272 return _("Opened Text Inset");
276 void InsetText::sanitizeEmptyText(BufferView & bv)
278 if (paragraphs().size() == 1
279 && paragraphs().begin()->empty()
280 && bv.getParentLanguage(this) != text_.current_font.language()) {
281 LyXFont font(LyXFont::ALL_IGNORE);
282 font.setLanguage(bv.getParentLanguage(this));
283 text_.setFont(bv.cursor(), font, false);
288 void InsetText::edit(LCursor & cur, bool left)
290 //lyxerr << "InsetText: edit left/right" << endl;
292 setViewCache(&cur.bv());
293 int const par = left ? 0 : paragraphs().size() - 1;
294 int const pos = left ? 0 : paragraphs().back().size();
295 text_.setCursor(cur.top(), par, pos);
296 cur.clearSelection();
298 sanitizeEmptyText(cur.bv());
300 #warning can someone check if/when this is needed?
307 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
309 lyxerr << "InsetText::edit xy" << endl;
311 return text_.editXY(cur, x, y);
312 //sanitizeEmptyText(cur.bv());
317 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
319 //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
320 setViewCache(&cur.bv());
322 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
323 text_.dispatch(cur, cmd);
325 // If the action has deleted all text in the inset, we need
326 // to change the language to the language of the surronding
328 // Why this cleverness? (Andre')
329 if (!was_empty && paragraphs().begin()->empty() &&
330 paragraphs().size() == 1) {
331 LyXFont font(LyXFont::ALL_IGNORE);
332 font.setLanguage(cur.bv().getParentLanguage(this));
333 text_.setFont(cur, font, false);
338 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
339 FuncStatus & status) const
341 return text_.getStatus(cur, cmd, status);
345 int InsetText::latex(Buffer const & buf, ostream & os,
346 OutputParams const & runparams) const
349 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
350 return texrow.rows();
354 int InsetText::plaintext(Buffer const & buf, ostream & os,
355 OutputParams const & runparams) const
357 ParagraphList::const_iterator beg = paragraphs().begin();
358 ParagraphList::const_iterator end = paragraphs().end();
359 ParagraphList::const_iterator it = beg;
360 for (; it != end; ++it)
361 asciiParagraph(buf, *it, os, runparams, it == beg);
363 //FIXME: Give the total numbers of lines
368 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
369 OutputParams const & runparams) const
371 linuxdocParagraphs(buf, paragraphs(), os, runparams);
376 int InsetText::docbook(Buffer const & buf, ostream & os,
377 OutputParams const & runparams) const
379 docbookParagraphs(buf, paragraphs(), os, runparams);
384 void InsetText::validate(LaTeXFeatures & features) const
386 for_each(paragraphs().begin(), paragraphs().end(),
387 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
391 void InsetText::getCursorPos(LCursor const & cur, int & x, int & y) const
393 x = text_.cursorX(cur.top());
394 y = text_.cursorY(cur.top());
398 bool InsetText::showInsetDialog(BufferView *) const
404 void InsetText::getLabelList(Buffer const & buffer,
405 std::vector<string> & list) const
407 ParagraphList::const_iterator pit = paragraphs().begin();
408 ParagraphList::const_iterator pend = paragraphs().end();
409 for (; pit != pend; ++pit) {
410 InsetList::const_iterator beg = pit->insetlist.begin();
411 InsetList::const_iterator end = pit->insetlist.end();
412 for (; beg != end; ++beg)
413 beg->inset->getLabelList(buffer, list);
418 void InsetText::markNew(bool track_changes)
420 ParagraphList::iterator pit = paragraphs().begin();
421 ParagraphList::iterator end = paragraphs().end();
422 for (; pit != end; ++pit) {
426 // no-op when not tracking
433 void InsetText::setText(string const & data, LyXFont const & font)
436 for (unsigned int i = 0; i < data.length(); ++i)
437 paragraphs().begin()->insertChar(i, data[i], font);
441 void InsetText::setAutoBreakRows(bool flag)
443 if (flag != autoBreakRows_) {
444 autoBreakRows_ = flag;
451 void InsetText::setDrawFrame(DrawFrame how)
457 LColor_color InsetText::frameColor() const
459 return LColor::color(frame_color_);
463 void InsetText::setFrameColor(LColor_color col)
469 void InsetText::setViewCache(BufferView const * bv) const
471 if (bv && bv != text_.bv_owner) {
472 //lyxerr << "setting view cache from "
473 // << text_.bv_owner << " to " << bv << "\n";
474 text_.bv_owner = const_cast<BufferView *>(bv);
479 void InsetText::removeNewlines()
481 ParagraphList::iterator it = paragraphs().begin();
482 ParagraphList::iterator end = paragraphs().end();
483 for (; it != end; ++it)
484 for (int i = 0; i < it->size(); ++i)
485 if (it->isNewline(i))
490 LyXText * InsetText::getText(int i) const
492 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
496 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
499 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
500 // And it probably does. You have to take a look at this John. (Lgb)
501 #warning John, have a look here. (Lgb)
503 ParagraphList::iterator pit = plist.begin();
504 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
506 mergeParagraph(buffer->params(), paragraphs(),
507 ins - paragraphs().begin() - 1);
509 ParagraphList::iterator pend = plist.end();
510 for (; pit != pend; ++pit)
511 paragraphs().push_back(*pit);
515 void InsetText::addPreview(PreviewLoader & loader) const
517 ParagraphList::const_iterator pit = paragraphs().begin();
518 ParagraphList::const_iterator pend = paragraphs().end();
520 for (; pit != pend; ++pit) {
521 InsetList::const_iterator it = pit->insetlist.begin();
522 InsetList::const_iterator end = pit->insetlist.end();
523 for (; it != end; ++it)
524 it->inset->addPreview(loader);
529 ParagraphList const & InsetText::paragraphs() const
531 return text_.paragraphs();
535 ParagraphList & InsetText::paragraphs()
537 return text_.paragraphs();