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" // lyx::count
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
57 using lyx::odocstream;
60 using lyx::graphics::PreviewLoader;
62 using lyx::support::isStrUnsignedInt;
76 int InsetText::border_ = 2;
79 InsetText::InsetText(BufferParams const & bp)
80 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
82 paragraphs().push_back(Paragraph());
83 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
84 // Dispose of the infamous L-shaped cursor.
85 text_.current_font.setLanguage(bp.language);
86 text_.real_current_font.setLanguage(bp.language);
91 InsetText::InsetText(InsetText const & in)
92 : InsetOld(in), text_(in.text_.bv_owner)
94 text_.autoBreakRows_ = in.text_.autoBreakRows_;
95 drawFrame_ = in.drawFrame_;
96 frame_color_ = in.frame_color_;
97 text_.paragraphs() = in.text_.paragraphs();
98 // Hand current buffer language down to "cloned" textinsets
100 text_.current_font = in.text_.current_font;
101 text_.real_current_font = in.text_.real_current_font;
106 InsetText::InsetText()
111 void InsetText::init()
113 for_each(paragraphs().begin(), paragraphs().end(),
114 bind(&Paragraph::setInsetOwner, _1, this));
118 void InsetText::clear()
120 ParagraphList & pars = paragraphs();
122 // This is a gross hack...
123 LyXLayout_ptr old_layout = pars.begin()->layout();
126 pars.push_back(Paragraph());
127 pars.begin()->setInsetOwner(this);
128 pars.begin()->layout(old_layout);
132 auto_ptr<InsetBase> InsetText::doClone() const
134 return auto_ptr<InsetBase>(new InsetText(*this));
138 void InsetText::write(Buffer const & buf, ostream & os) const
141 text_.write(buf, os);
145 void InsetText::read(Buffer const & buf, LyXLex & lex)
149 // delete the initial paragraph
150 Paragraph oldpar = *paragraphs().begin();
151 paragraphs().clear();
153 bool res = text_.read(buf, lex, errorList);
157 lex.printError("Missing \\end_inset at this point. "
162 // ensure we have at least one paragraph.
163 if (paragraphs().empty())
164 paragraphs().push_back(oldpar);
168 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
170 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
171 setViewCache(mi.base.bv);
172 mi.base.textwidth -= 2 * border_;
173 font_ = mi.base.font;
174 // Hand font through to contained lyxtext:
175 text_.font_ = mi.base.font;
176 text_.metrics(mi, dim);
179 dim.wid += 2 * border_;
180 mi.base.textwidth += 2 * border_;
185 void InsetText::draw(PainterInfo & pi, int x, int y) const
187 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
188 // update our idea of where we are
189 setPosCache(pi, x, y);
191 text_.background_color_ = backgroundColor();
192 text_.draw(pi, x + border_, y);
195 int const w = text_.width() + 2 * border_;
196 int const a = text_.ascent() + border_;
197 int const h = a + text_.descent() + border_;
198 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
204 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
206 int const w = text_.width() + 2 * border_;
207 int const a = text_.ascent() + border_;
208 int const h = a + text_.descent() + border_;
209 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
211 text_.drawSelection(pi, x, y);
215 bool InsetText::covers(BufferView & bv, int x, int y) const
217 return bv.coordCache().getInsets().has(this)
219 && x <= xo(bv) + width() + (Wide() ? text_.maxwidth_ : 0)
220 && y >= yo(bv) - ascent()
221 && y <= yo(bv) + descent();
225 docstring const InsetText::editMessage() const
227 return _("Opened Text Inset");
231 void InsetText::edit(LCursor & cur, bool left)
233 //lyxerr << "InsetText: edit left/right" << endl;
234 setViewCache(&cur.bv());
235 int const pit = left ? 0 : paragraphs().size() - 1;
236 int const pos = left ? 0 : paragraphs().back().size();
237 text_.setCursor(cur.top(), pit, pos);
238 cur.clearSelection();
243 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
245 return text_.editXY(cur, x, y);
249 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
251 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
252 << " [ cmd.action = "
253 << cmd.action << ']' << endl;
254 setViewCache(&cur.bv());
255 text_.dispatch(cur, cmd);
259 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
260 FuncStatus & status) const
262 return text_.getStatus(cur, cmd, status);
266 void InsetText::setChange(Change const & change)
268 ParagraphList::iterator pit = paragraphs().begin();
269 ParagraphList::iterator end = paragraphs().end();
270 for (; pit != end; ++pit) {
271 pit->setChange(change);
276 int InsetText::latex(Buffer const & buf, odocstream & os,
277 OutputParams const & runparams) const
280 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
281 return texrow.rows();
285 int InsetText::plaintext(Buffer const & buf, odocstream & os,
286 OutputParams const & runparams) const
288 ParagraphList::const_iterator beg = paragraphs().begin();
289 ParagraphList::const_iterator end = paragraphs().end();
290 ParagraphList::const_iterator it = beg;
291 bool ref_printed = false;
292 lyx::odocstringstream oss;
293 for (; it != end; ++it)
294 asciiParagraph(buf, *it, oss, runparams, ref_printed);
296 docstring const str = oss.str();
298 // Return how many newlines we issued.
299 return int(lyx::count(str.begin(), str.end(), '\n'));
303 int InsetText::docbook(Buffer const & buf, odocstream & os,
304 OutputParams const & runparams) const
306 docbookParagraphs(paragraphs(), buf, os, runparams);
311 void InsetText::validate(LaTeXFeatures & features) const
313 for_each(paragraphs().begin(), paragraphs().end(),
314 bind(&Paragraph::validate, _1, ref(features)));
318 void InsetText::cursorPos(BufferView const & /*bv*/,
319 CursorSlice const & sl, bool boundary, int & x, int & y) const
321 x = text_.cursorX(sl, boundary) + border_;
322 y = text_.cursorY(sl, boundary);
326 bool InsetText::showInsetDialog(BufferView *) const
332 void InsetText::setText(docstring const & data, LyXFont const & font, bool trackChanges)
335 Paragraph & first = paragraphs().front();
336 for (unsigned int i = 0; i < data.length(); ++i)
337 first.insertChar(i, data[i], font, trackChanges);
341 void InsetText::setAutoBreakRows(bool flag)
343 if (flag == text_.autoBreakRows_)
346 text_.autoBreakRows_ = flag;
350 // remove previously existing newlines
351 ParagraphList::iterator it = paragraphs().begin();
352 ParagraphList::iterator end = paragraphs().end();
353 for (; it != end; ++it)
354 for (int i = 0; i < it->size(); ++i)
355 if (it->isNewline(i))
356 // do not track the change, because the user
357 // is not allowed to revert/reject it
362 void InsetText::setDrawFrame(bool flag)
368 LColor_color InsetText::frameColor() const
370 return LColor::color(frame_color_);
374 void InsetText::setFrameColor(LColor_color col)
380 void InsetText::setViewCache(BufferView const * bv) const
382 if (bv && bv != text_.bv_owner) {
383 //lyxerr << "setting view cache from "
384 // << text_.bv_owner << " to " << bv << "\n";
385 text_.bv_owner = const_cast<BufferView *>(bv);
390 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
392 // There is little we can do here to keep track of changes.
393 // As of 2006/10/20, appendParagraphs is used exclusively by
394 // LyXTabular::setMultiColumn. In this context, the paragraph break
395 // is lost irreversibly and the appended text doesn't really change
397 ParagraphList & pl = paragraphs();
399 ParagraphList::iterator pit = plist.begin();
400 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
402 mergeParagraph(buffer->params(), pl,
403 std::distance(pl.begin(), ins) - 1);
405 for_each(pit, plist.end(),
406 bind(&ParagraphList::push_back, ref(pl), _1));
410 void InsetText::addPreview(PreviewLoader & loader) const
412 ParagraphList::const_iterator pit = paragraphs().begin();
413 ParagraphList::const_iterator pend = paragraphs().end();
415 for (; pit != pend; ++pit) {
416 InsetList::const_iterator it = pit->insetlist.begin();
417 InsetList::const_iterator end = pit->insetlist.end();
418 for (; it != end; ++it)
419 it->inset->addPreview(loader);
424 //FIXME: instead of this hack, which only works by chance,
425 // cells should have their own insetcell type, which returns CELL_CODE!
426 bool InsetText::neverIndent() const
428 // this is only true for tabular cells
429 return !text_.isMainText() && lyxCode() == TEXT_CODE;
433 ParagraphList const & InsetText::paragraphs() const
435 return text_.paragraphs();
439 ParagraphList & InsetText::paragraphs()
441 return text_.paragraphs();