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
198 Painter & pain = pi.pain;
200 // repaint the background if needed
201 x += TEXT_TO_INSET_OFFSET;
202 if (backgroundColor() != LColor::background)
203 clearInset(pain, x, y);
205 BufferView * bv = pi.base.bv;
210 y += bv->top_y() - text_.ascent();
212 text_.draw(pi, x, y);
214 if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
215 drawFrame(pain, xo_);
219 void InsetText::drawFrame(Painter & pain, int x) const
221 int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
222 int const frame_x = x + ttoD2;
223 int const frame_y = yo_ - dim_.asc + ttoD2;
224 int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
225 int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
226 pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
230 void InsetText::updateLocal(BufferView * bv)
235 if (!autoBreakRows_ && paragraphs().size() > 1)
236 collapseParagraphs(bv);
238 if (!text_.selection.set())
239 text_.anchor() = text_.cursor();
241 bv->owner()->view_state_changed();
242 bv->owner()->updateMenubar();
243 bv->owner()->updateToolbar();
244 if (old_par != text_.cursor().par()) {
245 bv->owner()->setLayout(text_.cursorPar()->layout()->name());
246 old_par = text_.cursor().par();
251 string const InsetText::editMessage() const
253 return _("Opened Text Inset");
257 void InsetText::sanitizeEmptyText(BufferView * bv)
259 if (paragraphs().size() == 1
260 && paragraphs().begin()->empty()
261 && bv->getParentLanguage(this) != text_.current_font.language()) {
262 LyXFont font(LyXFont::ALL_IGNORE);
263 font.setLanguage(bv->getParentLanguage(this));
264 text_.setFont(font, false);
269 extern LCursor theTempCursor;
272 void InsetText::edit(BufferView * bv, bool left)
274 lyxerr << "InsetText: edit left/right" << endl;
277 int const par = left ? 0 : paragraphs().size() - 1;
278 int const pos = left ? 0 : paragraphs().back().size();
279 text_.setCursor(par, pos);
280 text_.clearSelection();
282 sanitizeEmptyText(bv);
284 bv->updateParagraphDialog();
288 void InsetText::edit(BufferView * bv, int x, int y)
290 lyxerr << "InsetText::edit xy" << endl;
292 text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y() - text_.yo_);
293 text_.clearSelection();
295 sanitizeEmptyText(bv);
297 bv->updateParagraphDialog();
301 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
302 idx_type &, pos_type &)
304 //lyxerr << "InsetText::priv_dispatch (begin), act: "
305 // << cmd.action << " " << endl;
307 BufferView * bv = cmd.view();
310 DispatchResult result;
311 result.dispatched(true);
313 bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
315 switch (cmd.action) {
316 case LFUN_MOUSE_PRESS:
317 bv->fullCursor(theTempCursor);
320 result = text_.dispatch(cmd);
324 // If the action has deleted all text in the inset, we need
325 // to change the language to the language of the surronding
327 if (!was_empty && paragraphs().begin()->empty() &&
328 paragraphs().size() == 1) {
329 LyXFont font(LyXFont::ALL_IGNORE);
330 font.setLanguage(bv->getParentLanguage(this));
331 text_.setFont(font, false);
334 //lyxerr << "InsetText::priv_dispatch (end)" << endl;
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(int, int & x, int & y) const
387 x = text_.cursorX() + TEXT_TO_INSET_OFFSET;
388 y = text_.cursorY() - dim_.asc + TEXT_TO_INSET_OFFSET;
392 bool InsetText::insetAllowed(InsetOld::Code code) const
394 // in_insetAllowed is a really gross hack,
395 // to allow us to call the owner's insetAllowed
396 // without stack overflow, which can happen
397 // when the owner uses InsetCollapsable::insetAllowed()
401 in_insetAllowed = true;
403 ret = owner()->insetAllowed(code);
404 in_insetAllowed = false;
409 bool InsetText::showInsetDialog(BufferView *) const
415 void InsetText::getLabelList(Buffer const & buffer,
416 std::vector<string> & list) const
418 ParagraphList::const_iterator pit = paragraphs().begin();
419 ParagraphList::const_iterator pend = paragraphs().end();
420 for (; pit != pend; ++pit) {
421 InsetList::const_iterator beg = pit->insetlist.begin();
422 InsetList::const_iterator end = pit->insetlist.end();
423 for (; beg != end; ++beg)
424 beg->inset->getLabelList(buffer, list);
429 void InsetText::markNew(bool track_changes)
431 ParagraphList::iterator pit = paragraphs().begin();
432 ParagraphList::iterator end = paragraphs().end();
433 for (; pit != end; ++pit) {
437 // no-op when not tracking
444 void InsetText::setText(string const & data, LyXFont const & font)
447 for (unsigned int i = 0; i < data.length(); ++i)
448 paragraphs().begin()->insertChar(i, data[i], font);
452 void InsetText::setAutoBreakRows(bool flag)
454 if (flag != autoBreakRows_) {
455 autoBreakRows_ = flag;
462 void InsetText::setDrawFrame(DrawFrame how)
468 LColor_color InsetText::frameColor() const
470 return LColor::color(frame_color_);
474 void InsetText::setFrameColor(LColor_color col)
480 void InsetText::setViewCache(BufferView const * bv) const
482 if (bv && bv != text_.bv_owner) {
483 //lyxerr << "setting view cache from "
484 // << text_.bv_owner << " to " << bv << "\n";
485 text_.bv_owner = const_cast<BufferView *>(bv);
490 void InsetText::removeNewlines()
492 ParagraphList::iterator it = paragraphs().begin();
493 ParagraphList::iterator end = paragraphs().end();
494 for (; it != end; ++it)
495 for (int i = 0; i < it->size(); ++i)
496 if (it->isNewline(i))
501 int InsetText::scroll(bool /*recursive*/) const
503 return UpdatableInset::scroll(false);
507 void InsetText::clearInset(Painter & pain, int x, int y) const
510 int h = dim_.asc + dim_.des;
511 int ty = y - dim_.asc;
517 if (ty + h > pain.paperHeight())
518 h = pain.paperHeight();
519 if (xo_ + w > pain.paperWidth())
520 w = pain.paperWidth();
521 pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
525 LyXText * InsetText::getText(int i) const
527 return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
531 void InsetText::collapseParagraphs(BufferView * bv)
533 while (paragraphs().size() > 1) {
534 ParagraphList::iterator const first = paragraphs().begin();
535 ParagraphList::iterator second = first;
537 size_t const first_par_size = first->size();
539 if (!first->empty() &&
541 !first->isSeparator(first_par_size - 1)) {
542 first->insertChar(first_par_size, ' ');
545 text_.clearSelection();
546 mergeParagraph(bv->buffer()->params(), paragraphs(), first);
551 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
553 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
554 // And it probably does. You have to take a look at this John. (Lgb)
555 #warning John, have a look here. (Lgb)
556 ParagraphList::iterator pit = plist.begin();
557 ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
559 mergeParagraph(buffer->params(), paragraphs(), boost::prior(ins));
561 ParagraphList::iterator pend = plist.end();
562 for (; pit != pend; ++pit)
563 paragraphs().push_back(*pit);
567 void InsetText::addPreview(PreviewLoader & loader) const
569 ParagraphList::const_iterator pit = paragraphs().begin();
570 ParagraphList::const_iterator pend = paragraphs().end();
572 for (; pit != pend; ++pit) {
573 InsetList::const_iterator it = pit->insetlist.begin();
574 InsetList::const_iterator end = pit->insetlist.end();
575 for (; it != end; ++it)
576 it->inset->addPreview(loader);
581 ParagraphList & InsetText::paragraphs() const
583 return const_cast<ParagraphList &>(text_.paragraphs());