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 setViewCache(mi.base.bv);
180 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
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;
187 font_ = mi.base.font;
188 text_.font_ = mi.base.font;
192 void InsetText::draw(PainterInfo & pi, int x, int y) const
194 // update our idea of where we are
195 setPosCache(pi, x, y);
197 // repaint the background if needed
198 x += TEXT_TO_INSET_OFFSET;
199 if (backgroundColor() != LColor::background)
200 clearInset(pi.pain, x, y);
202 BufferView * bv = pi.base.bv;
207 y += bv->top_y() - text_.ascent();
209 text_.draw(pi, x, y);
211 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
212 drawFrame(pi.pain, xo_);
216 void InsetText::drawFrame(Painter & pain, int x) const
218 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
219 int const frame_x = x + ttoD2;
220 int const frame_y = yo_ - dim_.asc + ttoD2;
221 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
222 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
223 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
227 void InsetText::updateLocal(LCursor & cur)
229 if (!autoBreakRows_ && paragraphs().size() > 1) {
230 // collapseParagraphs
231 while (paragraphs().size() > 1) {
232 ParagraphList::iterator const first = paragraphs().begin();
233 ParagraphList::iterator second = first;
235 size_t const first_par_size = first->size();
237 if (!first->empty() &&
239 !first->isSeparator(first_par_size - 1)) {
240 first->insertChar(first_par_size, ' ');
243 cur.clearSelection();
244 mergeParagraph(cur.bv().buffer()->params(), paragraphs(), first);
248 if (!cur.selection())
251 LyXView * lv = cur.bv().owner();
252 lv->view_state_changed();
255 if (old_par != cur.par()) {
256 lv->setLayout(text_.cursorPar()->layout()->name());
262 string const InsetText::editMessage() const
264 return _("Opened Text Inset");
268 void InsetText::sanitizeEmptyText(BufferView & bv)
270 if (paragraphs().size() == 1
271 && paragraphs().begin()->empty()
272 && bv.getParentLanguage(this) != text_.current_font.language()) {
273 LyXFont font(LyXFont::ALL_IGNORE);
274 font.setLanguage(bv.getParentLanguage(this));
275 text_.setFont(font, false);
280 extern CursorBase theTempCursor;
283 void InsetText::edit(LCursor & cur, bool left)
285 //lyxerr << "InsetText: edit left/right" << endl;
287 setViewCache(&cur.bv());
288 int const par = left ? 0 : paragraphs().size() - 1;
289 int const pos = left ? 0 : paragraphs().back().size();
290 text_.setCursor(par, pos);
291 cur.clearSelection();
293 sanitizeEmptyText(cur.bv());
295 cur.bv().updateParagraphDialog();
299 void InsetText::edit(LCursor & cur, int x, int y)
301 lyxerr << "InsetText::edit xy" << endl;
303 text_.edit(cur, x, y);
304 //sanitizeEmptyText(cur.bv());
306 //cur.bv().updateParagraphDialog();
310 DispatchResult InsetText::priv_dispatch(LCursor & cur, FuncRequest const & 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 DispatchResult result = 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(font, false);
331 //lyxerr << "InsetText::priv_dispatch (end)" << endl;
336 int InsetText::latex(Buffer const & buf, ostream & os,
337 OutputParams const & runparams) const
340 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
341 return texrow.rows();
345 int InsetText::plaintext(Buffer const & buf, ostream & os,
346 OutputParams const & runparams) const
348 ParagraphList::const_iterator beg = paragraphs().begin();
349 ParagraphList::const_iterator end = paragraphs().end();
350 ParagraphList::const_iterator it = beg;
351 for (; it != end; ++it)
352 asciiParagraph(buf, *it, os, runparams, it == beg);
354 //FIXME: Give the total numbers of lines
359 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
360 OutputParams const & runparams) const
362 linuxdocParagraphs(buf, paragraphs(), os, runparams);
367 int InsetText::docbook(Buffer const & buf, ostream & os,
368 OutputParams const & runparams) const
370 docbookParagraphs(buf, paragraphs(), os, runparams);
375 void InsetText::validate(LaTeXFeatures & features) const
377 for_each(paragraphs().begin(), paragraphs().end(),
378 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
382 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
384 x = text_.cursorX(cur);
385 y = text_.cursorY(cur);
389 bool InsetText::insetAllowed(InsetOld::Code code) const
391 // in_insetAllowed is a really gross hack,
392 // to allow us to call the owner's insetAllowed
393 // without stack overflow, which can happen
394 // when the owner uses InsetCollapsable::insetAllowed()
398 in_insetAllowed = true;
400 ret = owner()->insetAllowed(code);
401 in_insetAllowed = false;
406 bool InsetText::showInsetDialog(BufferView *) const
412 void InsetText::getLabelList(Buffer const & buffer,
413 std::vector<string> & list) const
415 ParagraphList::const_iterator pit = paragraphs().begin();
416 ParagraphList::const_iterator pend = paragraphs().end();
417 for (; pit != pend; ++pit) {
418 InsetList::const_iterator beg = pit->insetlist.begin();
419 InsetList::const_iterator end = pit->insetlist.end();
420 for (; beg != end; ++beg)
421 beg->inset->getLabelList(buffer, list);
426 void InsetText::markNew(bool track_changes)
428 ParagraphList::iterator pit = paragraphs().begin();
429 ParagraphList::iterator end = paragraphs().end();
430 for (; pit != end; ++pit) {
434 // no-op when not tracking
441 void InsetText::setText(string const & data, LyXFont const & font)
444 for (unsigned int i = 0; i < data.length(); ++i)
445 paragraphs().begin()->insertChar(i, data[i], font);
449 void InsetText::setAutoBreakRows(bool flag)
451 if (flag != autoBreakRows_) {
452 autoBreakRows_ = flag;
459 void InsetText::setDrawFrame(DrawFrame how)
465 LColor_color InsetText::frameColor() const
467 return LColor::color(frame_color_);
471 void InsetText::setFrameColor(LColor_color col)
477 void InsetText::setViewCache(BufferView const * bv) const
479 if (bv && bv != text_.bv_owner) {
480 //lyxerr << "setting view cache from "
481 // << text_.bv_owner << " to " << bv << "\n";
482 text_.bv_owner = const_cast<BufferView *>(bv);
487 void InsetText::removeNewlines()
489 ParagraphList::iterator it = paragraphs().begin();
490 ParagraphList::iterator end = paragraphs().end();
491 for (; it != end; ++it)
492 for (int i = 0; i < it->size(); ++i)
493 if (it->isNewline(i))
498 int InsetText::scroll(bool /*recursive*/) const
500 return UpdatableInset::scroll(false);
504 void InsetText::clearInset(Painter & pain, int x, int y) const
507 int h = dim_.asc + dim_.des;
508 int ty = y - dim_.asc;
514 if (ty + h > pain.paperHeight())
515 h = pain.paperHeight();
516 if (xo_ + w > pain.paperWidth())
517 w = pain.paperWidth();
518 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
522 LyXText * InsetText::getText(int i) const
524 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
528 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
530 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
531 // And it probably does. You have to take a look at this John. (Lgb)
532 #warning John, have a look here. (Lgb)
533 ParagraphList::iterator pit = plist.begin();
534 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
536 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
538 ParagraphList::iterator pend = plist.end();
539 for (; pit != pend; ++pit)
540 paragraphs().push_back(*pit);
544 void InsetText::addPreview(PreviewLoader & loader) const
546 ParagraphList::const_iterator pit = paragraphs().begin();
547 ParagraphList::const_iterator pend = paragraphs().end();
549 for (; pit != pend; ++pit) {
550 InsetList::const_iterator it = pit->insetlist.begin();
551 InsetList::const_iterator end = pit->insetlist.end();
552 for (; it != end; ++it)
553 it->inset->addPreview(loader);
558 ParagraphList & InsetText::paragraphs() const
560 return const_cast<ParagraphList &>(text_.paragraphs());