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_plaintext.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "rowpainter.h"
45 #include "frontends/Alert.h"
46 #include "frontends/font_metrics.h"
47 #include "frontends/Painter.h"
49 #include "support/lyxalgo.h" // lyx::count
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
59 using lyx::graphics::PreviewLoader;
61 using lyx::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 if (bp.tracking_changes)
84 paragraphs().back().trackChanges();
85 // Dispose of the infamous L-shaped cursor.
86 text_.current_font.setLanguage(bp.language);
87 text_.real_current_font.setLanguage(bp.language);
92 InsetText::InsetText(InsetText const & in)
93 : InsetOld(in), text_(in.text_.bv_owner)
95 text_.autoBreakRows_ = in.text_.autoBreakRows_;
96 drawFrame_ = in.drawFrame_;
97 frame_color_ = in.frame_color_;
98 text_.paragraphs() = in.text_.paragraphs();
99 // Hand current buffer language down to "cloned" textinsets
100 // e.g. tabular cells
101 text_.current_font = in.text_.current_font;
102 text_.real_current_font = in.text_.real_current_font;
107 InsetText::InsetText()
112 void InsetText::init()
114 for_each(paragraphs().begin(), paragraphs().end(),
115 bind(&Paragraph::setInsetOwner, _1, this));
119 void InsetText::markErased(bool erased)
121 ParagraphList & pars = paragraphs();
122 for_each(pars.begin(), pars.end(),
123 bind(&Paragraph::markErased, _1, erased));
127 void InsetText::clear()
129 ParagraphList & pars = paragraphs();
131 // This is a gross hack...
132 LyXLayout_ptr old_layout = pars.begin()->layout();
135 pars.push_back(Paragraph());
136 pars.begin()->setInsetOwner(this);
137 pars.begin()->layout(old_layout);
141 auto_ptr<InsetBase> InsetText::doClone() const
143 return auto_ptr<InsetBase>(new InsetText(*this));
147 void InsetText::write(Buffer const & buf, ostream & os) const
150 text_.write(buf, os);
154 void InsetText::read(Buffer const & buf, LyXLex & lex)
159 #warning John, look here. Doesnt make much sense.
161 if (buf.params().tracking_changes)
162 paragraphs().begin()->trackChanges();
164 // delete the initial paragraph
165 Paragraph oldpar = *paragraphs().begin();
166 paragraphs().clear();
168 bool res = text_.read(buf, lex, errorList);
172 lex.printError("Missing \\end_inset at this point. "
177 // ensure we have at least one paragraph.
178 if (paragraphs().empty())
179 paragraphs().push_back(oldpar);
183 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
185 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
186 setViewCache(mi.base.bv);
187 mi.base.textwidth -= 2 * border_;
188 font_ = mi.base.font;
189 // Hand font through to contained lyxtext:
190 text_.font_ = mi.base.font;
191 text_.metrics(mi, dim);
194 dim.wid += 2 * border_;
195 mi.base.textwidth += 2 * border_;
200 void InsetText::draw(PainterInfo & pi, int x, int y) const
202 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
203 // update our idea of where we are
204 setPosCache(pi, x, y);
206 text_.background_color_ = backgroundColor();
207 text_.draw(pi, x + border_, y);
210 int const w = text_.width() + 2 * border_;
211 int const a = text_.ascent() + border_;
212 int const h = a + text_.descent() + border_;
213 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
219 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
221 int const w = text_.width() + 2 * border_;
222 int const a = text_.ascent() + border_;
223 int const h = a + text_.descent() + border_;
224 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
226 text_.drawSelection(pi, x, y);
230 docstring const InsetText::editMessage() const
232 return _("Opened Text Inset");
236 void InsetText::edit(LCursor & cur, bool left)
238 //lyxerr << "InsetText: edit left/right" << endl;
239 setViewCache(&cur.bv());
240 int const pit = left ? 0 : paragraphs().size() - 1;
241 int const pos = left ? 0 : paragraphs().back().size();
242 text_.setCursor(cur.top(), pit, pos);
243 cur.clearSelection();
248 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
250 return text_.editXY(cur, x, y);
254 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
256 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
257 << " [ cmd.action = "
258 << cmd.action << ']' << endl;
259 setViewCache(&cur.bv());
260 text_.dispatch(cur, cmd);
264 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
265 FuncStatus & status) const
267 return text_.getStatus(cur, cmd, status);
271 int InsetText::latex(Buffer const & buf, ostream & os,
272 OutputParams const & runparams) const
275 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
276 return texrow.rows();
280 int InsetText::plaintext(Buffer const & buf, ostream & os,
281 OutputParams const & runparams) const
283 ParagraphList::const_iterator beg = paragraphs().begin();
284 ParagraphList::const_iterator end = paragraphs().end();
285 ParagraphList::const_iterator it = beg;
286 bool ref_printed = false;
287 std::ostringstream oss;
288 for (; it != end; ++it)
289 asciiParagraph(buf, *it, oss, runparams, ref_printed);
291 string const str = oss.str();
293 // Return how many newlines we issued.
294 return int(lyx::count(str.begin(), str.end(), '\n'));
298 int InsetText::docbook(Buffer const & buf, ostream & os,
299 OutputParams const & runparams) const
301 docbookParagraphs(paragraphs(), buf, os, runparams);
306 void InsetText::validate(LaTeXFeatures & features) const
308 for_each(paragraphs().begin(), paragraphs().end(),
309 bind(&Paragraph::validate, _1, ref(features)));
313 void InsetText::cursorPos
314 (CursorSlice const & sl, bool boundary, int & x, int & y) const
316 x = text_.cursorX(sl, boundary) + border_;
317 y = text_.cursorY(sl, boundary);
321 bool InsetText::showInsetDialog(BufferView *) const
327 void InsetText::markNew(bool track_changes)
329 ParagraphList::iterator pit = paragraphs().begin();
330 ParagraphList::iterator end = paragraphs().end();
331 for (; pit != end; ++pit) {
334 else // no-op when not tracking
340 void InsetText::setText(docstring const & data, LyXFont const & font)
343 Paragraph & first = paragraphs().front();
344 for (unsigned int i = 0; i < data.length(); ++i)
345 first.insertChar(i, data[i], font);
349 void InsetText::setAutoBreakRows(bool flag)
351 if (flag == text_.autoBreakRows_)
354 text_.autoBreakRows_ = flag;
358 // remove previously existing newlines
359 ParagraphList::iterator it = paragraphs().begin();
360 ParagraphList::iterator end = paragraphs().end();
361 for (; it != end; ++it)
362 for (int i = 0; i < it->size(); ++i)
363 if (it->isNewline(i))
368 void InsetText::setDrawFrame(bool flag)
374 LColor_color InsetText::frameColor() const
376 return LColor::color(frame_color_);
380 void InsetText::setFrameColor(LColor_color col)
386 void InsetText::setViewCache(BufferView const * bv) const
388 if (bv && bv != text_.bv_owner) {
389 //lyxerr << "setting view cache from "
390 // << text_.bv_owner << " to " << bv << "\n";
391 text_.bv_owner = const_cast<BufferView *>(bv);
396 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
399 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
400 // And it probably does. You have to take a look at this John. (Lgb)
401 #warning John, have a look here. (Lgb)
403 ParagraphList & pl = paragraphs();
405 ParagraphList::iterator pit = plist.begin();
406 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
408 mergeParagraph(buffer->params(), pl,
409 std::distance(pl.begin(), ins) - 1);
411 for_each(pit, plist.end(),
412 bind(&ParagraphList::push_back, ref(pl), _1));
416 void InsetText::addPreview(PreviewLoader & loader) const
418 ParagraphList::const_iterator pit = paragraphs().begin();
419 ParagraphList::const_iterator pend = paragraphs().end();
421 for (; pit != pend; ++pit) {
422 InsetList::const_iterator it = pit->insetlist.begin();
423 InsetList::const_iterator end = pit->insetlist.end();
424 for (; it != end; ++it)
425 it->inset->addPreview(loader);
430 //FIXME: instead of this hack, which only works by chance,
431 // cells should have their own insetcell type, which returns CELL_CODE!
432 bool InsetText::neverIndent() const
434 // this is only true for tabular cells
435 return !text_.isMainText() && lyxCode() == TEXT_CODE;
439 ParagraphList const & InsetText::paragraphs() const
441 return text_.paragraphs();
445 ParagraphList & InsetText::paragraphs()
447 return text_.paragraphs();