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 // 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_(in.text_.bv_owner)
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::markErased(bool erased)
119 ParagraphList & pars = paragraphs();
120 for_each(pars.begin(), pars.end(),
121 bind(&Paragraph::markErased, _1, erased));
125 void InsetText::clear()
127 ParagraphList & pars = paragraphs();
129 // This is a gross hack...
130 LyXLayout_ptr old_layout = pars.begin()->layout();
133 pars.push_back(Paragraph());
134 pars.begin()->setInsetOwner(this);
135 pars.begin()->layout(old_layout);
139 auto_ptr<InsetBase> InsetText::doClone() const
141 return auto_ptr<InsetBase>(new InsetText(*this));
145 void InsetText::write(Buffer const & buf, ostream & os) const
148 text_.write(buf, os);
152 void InsetText::read(Buffer const & buf, LyXLex & lex)
156 // delete the initial paragraph
157 Paragraph oldpar = *paragraphs().begin();
158 paragraphs().clear();
160 bool res = text_.read(buf, lex, errorList);
164 lex.printError("Missing \\end_inset at this point. "
169 // ensure we have at least one paragraph.
170 if (paragraphs().empty())
171 paragraphs().push_back(oldpar);
175 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
177 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
178 setViewCache(mi.base.bv);
179 mi.base.textwidth -= 2 * border_;
180 font_ = mi.base.font;
181 // Hand font through to contained lyxtext:
182 text_.font_ = mi.base.font;
183 text_.metrics(mi, dim);
186 dim.wid += 2 * border_;
187 mi.base.textwidth += 2 * border_;
192 void InsetText::draw(PainterInfo & pi, int x, int y) const
194 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
195 // update our idea of where we are
196 setPosCache(pi, x, y);
198 text_.background_color_ = backgroundColor();
199 text_.draw(pi, x + border_, y);
202 int const w = text_.width() + 2 * border_;
203 int const a = text_.ascent() + border_;
204 int const h = a + text_.descent() + border_;
205 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
211 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
213 int const w = text_.width() + 2 * border_;
214 int const a = text_.ascent() + border_;
215 int const h = a + text_.descent() + border_;
216 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
218 text_.drawSelection(pi, x, y);
222 docstring const InsetText::editMessage() const
224 return _("Opened Text Inset");
228 void InsetText::edit(LCursor & cur, bool left)
230 //lyxerr << "InsetText: edit left/right" << endl;
231 setViewCache(&cur.bv());
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::DEBUG] << BOOST_CURRENT_FUNCTION
249 << " [ cmd.action = "
250 << cmd.action << ']' << endl;
251 setViewCache(&cur.bv());
252 text_.dispatch(cur, cmd);
256 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
257 FuncStatus & status) const
259 return text_.getStatus(cur, cmd, status);
263 int InsetText::latex(Buffer const & buf, ostream & os,
264 OutputParams const & runparams) const
267 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
268 return texrow.rows();
272 int InsetText::plaintext(Buffer const & buf, ostream & os,
273 OutputParams const & runparams) const
275 ParagraphList::const_iterator beg = paragraphs().begin();
276 ParagraphList::const_iterator end = paragraphs().end();
277 ParagraphList::const_iterator it = beg;
278 bool ref_printed = false;
279 std::ostringstream oss;
280 for (; it != end; ++it)
281 asciiParagraph(buf, *it, oss, runparams, ref_printed);
283 string const str = oss.str();
285 // Return how many newlines we issued.
286 return int(lyx::count(str.begin(), str.end(), '\n'));
290 int InsetText::docbook(Buffer const & buf, ostream & os,
291 OutputParams const & runparams) const
293 docbookParagraphs(paragraphs(), buf, os, runparams);
298 void InsetText::validate(LaTeXFeatures & features) const
300 for_each(paragraphs().begin(), paragraphs().end(),
301 bind(&Paragraph::validate, _1, ref(features)));
305 void InsetText::cursorPos
306 (CursorSlice const & sl, bool boundary, int & x, int & y) const
308 x = text_.cursorX(sl, boundary) + border_;
309 y = text_.cursorY(sl, boundary);
313 bool InsetText::showInsetDialog(BufferView *) const
319 void InsetText::markNew(bool track_changes)
321 ParagraphList::iterator pit = paragraphs().begin();
322 ParagraphList::iterator end = paragraphs().end();
323 for (; pit != end; ++pit) {
324 // FIXME: change tracking (MG)
325 // if (track_changes)
326 // set pit's text to UNCHANGED
328 // set pit's text to INSERTED in CT mode; reset CT info otherwise
333 void InsetText::setText(docstring const & data, LyXFont const & font)
336 Paragraph & first = paragraphs().front();
337 for (unsigned int i = 0; i < data.length(); ++i)
338 first.insertChar(i, data[i], font);
342 void InsetText::setAutoBreakRows(bool flag)
344 if (flag == text_.autoBreakRows_)
347 text_.autoBreakRows_ = flag;
351 // remove previously existing newlines
352 ParagraphList::iterator it = paragraphs().begin();
353 ParagraphList::iterator end = paragraphs().end();
354 for (; it != end; ++it)
355 for (int i = 0; i < it->size(); ++i)
356 if (it->isNewline(i))
361 void InsetText::setDrawFrame(bool flag)
367 LColor_color InsetText::frameColor() const
369 return LColor::color(frame_color_);
373 void InsetText::setFrameColor(LColor_color col)
379 void InsetText::setViewCache(BufferView const * bv) const
381 if (bv && bv != text_.bv_owner) {
382 //lyxerr << "setting view cache from "
383 // << text_.bv_owner << " to " << bv << "\n";
384 text_.bv_owner = const_cast<BufferView *>(bv);
389 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
392 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
393 // And it probably does. You have to take a look at this John. (Lgb)
394 #warning John, have a look here. (Lgb)
396 ParagraphList & pl = paragraphs();
398 ParagraphList::iterator pit = plist.begin();
399 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
401 mergeParagraph(buffer->params(), pl,
402 std::distance(pl.begin(), ins) - 1);
404 for_each(pit, plist.end(),
405 bind(&ParagraphList::push_back, ref(pl), _1));
409 void InsetText::addPreview(PreviewLoader & loader) const
411 ParagraphList::const_iterator pit = paragraphs().begin();
412 ParagraphList::const_iterator pend = paragraphs().end();
414 for (; pit != pend; ++pit) {
415 InsetList::const_iterator it = pit->insetlist.begin();
416 InsetList::const_iterator end = pit->insetlist.end();
417 for (; it != end; ++it)
418 it->inset->addPreview(loader);
423 //FIXME: instead of this hack, which only works by chance,
424 // cells should have their own insetcell type, which returns CELL_CODE!
425 bool InsetText::neverIndent() const
427 // this is only true for tabular cells
428 return !text_.isMainText() && lyxCode() == TEXT_CODE;
432 ParagraphList const & InsetText::paragraphs() const
434 return text_.paragraphs();
438 ParagraphList & InsetText::paragraphs()
440 return text_.paragraphs();