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 "coordcache.h"
20 #include "CutAndPaste.h"
23 #include "dispatchresult.h"
24 #include "errorlist.h"
25 #include "funcrequest.h"
33 #include "metricsinfo.h"
34 #include "output_docbook.h"
35 #include "output_latex.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/Painter.h"
49 #include "support/lyxalgo.h" // count
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
59 using graphics::PreviewLoader;
61 using support::isStrUnsignedInt;
75 int InsetText::border_ = 2;
78 InsetText::InsetText(BufferParams const & bp)
79 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
81 paragraphs().push_back(Paragraph());
82 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
83 // Dispose of the infamous L-shaped cursor.
84 text_.current_font.setLanguage(bp.language);
85 text_.real_current_font.setLanguage(bp.language);
90 InsetText::InsetText(InsetText const & in)
91 : InsetOld(in), text_()
93 text_.autoBreakRows_ = in.text_.autoBreakRows_;
94 drawFrame_ = in.drawFrame_;
95 frame_color_ = in.frame_color_;
96 text_.paragraphs() = in.text_.paragraphs();
97 // Hand current buffer language down to "cloned" textinsets
99 text_.current_font = in.text_.current_font;
100 text_.real_current_font = in.text_.real_current_font;
105 InsetText::InsetText()
110 void InsetText::init()
112 for_each(paragraphs().begin(), paragraphs().end(),
113 bind(&Paragraph::setInsetOwner, _1, this));
117 void InsetText::clear()
119 ParagraphList & pars = paragraphs();
121 // This is a gross hack...
122 LyXLayout_ptr old_layout = pars.begin()->layout();
125 pars.push_back(Paragraph());
126 pars.begin()->setInsetOwner(this);
127 pars.begin()->layout(old_layout);
131 auto_ptr<InsetBase> InsetText::doClone() const
133 return auto_ptr<InsetBase>(new InsetText(*this));
137 void InsetText::write(Buffer const & buf, ostream & os) const
140 text_.write(buf, os);
144 void InsetText::read(Buffer const & buf, LyXLex & lex)
148 // delete the initial paragraph
149 Paragraph oldpar = *paragraphs().begin();
150 paragraphs().clear();
152 bool res = text_.read(buf, lex, errorList);
156 lex.printError("Missing \\end_inset at this point. "
161 // ensure we have at least one paragraph.
162 if (paragraphs().empty())
163 paragraphs().push_back(oldpar);
167 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
169 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
170 mi.base.textwidth -= 2 * border_;
171 font_ = mi.base.font;
172 // Hand font through to contained lyxtext:
173 text_.font_ = mi.base.font;
174 text_.metrics(mi, dim);
177 dim.wid += 2 * border_;
178 mi.base.textwidth += 2 * border_;
183 void InsetText::draw(PainterInfo & pi, int x, int y) const
185 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
186 // update our idea of where we are
187 setPosCache(pi, x, y);
189 text_.background_color_ = backgroundColor();
190 text_.draw(pi, x + border_, y);
193 int const w = text_.width() + 2 * border_;
194 int const a = text_.ascent() + border_;
195 int const h = a + text_.descent() + border_;
196 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
202 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
204 int const w = text_.width() + 2 * border_;
205 int const a = text_.ascent() + border_;
206 int const h = a + text_.descent() + border_;
207 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
209 text_.drawSelection(pi, x, y);
213 bool InsetText::covers(BufferView const & bv, int x, int y) const
215 return bv.coordCache().getInsets().has(this)
217 && x <= xo(bv) + width() + (Wide() ? text_.maxwidth_ : 0)
218 && y >= yo(bv) - ascent()
219 && y <= yo(bv) + descent();
223 docstring const InsetText::editMessage() const
225 return _("Opened Text Inset");
229 void InsetText::edit(LCursor & cur, bool left)
231 //lyxerr << "InsetText: edit left/right" << endl;
232 int const pit = left ? 0 : paragraphs().size() - 1;
233 int const pos = left ? 0 : paragraphs().back().size();
234 text_.setCursor(cur.top(), pit, pos);
235 cur.clearSelection();
240 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
242 return text_.editXY(cur, x, y);
246 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
248 lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
249 << " [ cmd.action = "
250 << cmd.action << ']' << endl;
251 text_.dispatch(cur, cmd);
255 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
256 FuncStatus & status) const
258 return text_.getStatus(cur, cmd, status);
262 void InsetText::setChange(Change const & change)
264 ParagraphList::iterator pit = paragraphs().begin();
265 ParagraphList::iterator end = paragraphs().end();
266 for (; pit != end; ++pit) {
267 pit->setChange(change);
272 void InsetText::acceptChanges()
274 ParagraphList::iterator pit = paragraphs().begin();
275 ParagraphList::iterator end = paragraphs().end();
276 for (; pit != end; ++pit) {
277 // FIXME: change tracking (MG)
278 // we must handle end-of-par chars!
279 pit->acceptChanges(0, pit->size() + 1);
284 void InsetText::rejectChanges()
286 ParagraphList::iterator pit = paragraphs().begin();
287 ParagraphList::iterator end = paragraphs().end();
288 for (; pit != end; ++pit) {
289 // FIXME: change tracking (MG)
290 // we must handle end-of-par chars!
291 pit->rejectChanges(0, pit->size() + 1);
296 int InsetText::latex(Buffer const & buf, odocstream & os,
297 OutputParams const & runparams) const
300 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
301 return texrow.rows();
305 int InsetText::plaintext(Buffer const & buf, odocstream & os,
306 OutputParams const & runparams) const
308 ParagraphList::const_iterator beg = paragraphs().begin();
309 ParagraphList::const_iterator end = paragraphs().end();
310 ParagraphList::const_iterator it = beg;
311 bool ref_printed = false;
312 odocstringstream oss;
313 for (; it != end; ++it)
314 asciiParagraph(buf, *it, oss, runparams, ref_printed);
316 docstring const str = oss.str();
318 // Return how many newlines we issued.
319 return int(lyx::count(str.begin(), str.end(), '\n'));
323 int InsetText::docbook(Buffer const & buf, odocstream & os,
324 OutputParams const & runparams) const
326 docbookParagraphs(paragraphs(), buf, os, runparams);
331 void InsetText::validate(LaTeXFeatures & features) const
333 for_each(paragraphs().begin(), paragraphs().end(),
334 bind(&Paragraph::validate, _1, ref(features)));
338 void InsetText::cursorPos(BufferView const & bv,
339 CursorSlice const & sl, bool boundary, int & x, int & y) const
341 x = text_.cursorX(*bv.buffer(), sl, boundary) + border_;
342 y = text_.cursorY(sl, boundary);
346 bool InsetText::showInsetDialog(BufferView *) const
352 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
355 Paragraph & first = paragraphs().front();
356 for (unsigned int i = 0; i < data.length(); ++i)
357 first.insertChar(i, data[i], font, trackChanges);
361 void InsetText::setAutoBreakRows(bool flag)
363 if (flag == text_.autoBreakRows_)
366 text_.autoBreakRows_ = flag;
370 // remove previously existing newlines
371 ParagraphList::iterator it = paragraphs().begin();
372 ParagraphList::iterator end = paragraphs().end();
373 for (; it != end; ++it)
374 for (int i = 0; i < it->size(); ++i)
375 if (it->isNewline(i))
376 // do not track the change, because the user
377 // is not allowed to revert/reject it
378 it->eraseChar(i, false);
382 void InsetText::setDrawFrame(bool flag)
388 LColor_color InsetText::frameColor() const
390 return LColor::color(frame_color_);
394 void InsetText::setFrameColor(LColor_color col)
400 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
402 // There is little we can do here to keep track of changes.
403 // As of 2006/10/20, appendParagraphs is used exclusively by
404 // LyXTabular::setMultiColumn. In this context, the paragraph break
405 // is lost irreversibly and the appended text doesn't really change
407 ParagraphList & pl = paragraphs();
409 ParagraphList::iterator pit = plist.begin();
410 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
412 mergeParagraph(buffer->params(), pl,
413 std::distance(pl.begin(), ins) - 1);
415 for_each(pit, plist.end(),
416 bind(&ParagraphList::push_back, ref(pl), _1));
420 void InsetText::addPreview(PreviewLoader & loader) const
422 ParagraphList::const_iterator pit = paragraphs().begin();
423 ParagraphList::const_iterator pend = paragraphs().end();
425 for (; pit != pend; ++pit) {
426 InsetList::const_iterator it = pit->insetlist.begin();
427 InsetList::const_iterator end = pit->insetlist.end();
428 for (; it != end; ++it)
429 it->inset->addPreview(loader);
434 //FIXME: instead of this hack, which only works by chance,
435 // cells should have their own insetcell type, which returns CELL_CODE!
436 bool InsetText::neverIndent(Buffer const & buffer) const
438 // this is only true for tabular cells
439 return !text_.isMainText(buffer) && lyxCode() == TEXT_CODE;
443 ParagraphList const & InsetText::paragraphs() const
445 return text_.paragraphs();
449 ParagraphList & InsetText::paragraphs()
451 return text_.paragraphs();