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/Painter.h"
50 #include "support/lyxalgo.h" // lyx::count
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
57 using lyx::graphics::PreviewLoader;
59 using lyx::support::isStrUnsignedInt;
73 int InsetText::border_ = 2;
76 InsetText::InsetText(BufferParams const & bp)
77 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
79 paragraphs().push_back(Paragraph());
80 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
81 if (bp.tracking_changes)
82 paragraphs().back().trackChanges();
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)
157 #warning John, look here. Doesnt make much sense.
159 if (buf.params().tracking_changes)
160 paragraphs().begin()->trackChanges();
162 // delete the initial paragraph
163 Paragraph oldpar = *paragraphs().begin();
164 paragraphs().clear();
165 bool res = text_.read(buf, lex);
169 lex.printError("Missing \\end_inset at this point. "
174 // ensure we have at least one paragraph.
175 if (paragraphs().empty())
176 paragraphs().push_back(oldpar);
180 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
182 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
183 setViewCache(mi.base.bv);
184 mi.base.textwidth -= 2 * border_;
185 font_ = mi.base.font;
186 // Hand font through to contained lyxtext:
187 text_.font_ = mi.base.font;
188 text_.metrics(mi, dim);
191 dim.wid += 2 * border_;
192 mi.base.textwidth += 2 * border_;
197 void InsetText::draw(PainterInfo & pi, int x, int y) const
199 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
200 // update our idea of where we are
201 setPosCache(pi, x, y);
203 text_.background_color_ = backgroundColor();
204 text_.draw(pi, x + border_, y);
207 int const w = text_.width() + 2 * border_;
208 int const a = text_.ascent() + border_;
209 int const h = a + text_.descent() + border_;
210 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
216 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
218 int const w = text_.width() + 2 * border_;
219 int const a = text_.ascent() + border_;
220 int const h = a + text_.descent() + border_;
221 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
223 text_.drawSelection(pi, x, y);
227 string const InsetText::editMessage() const
229 return _("Opened Text Inset");
233 void InsetText::edit(LCursor & cur, bool left)
235 //lyxerr << "InsetText: edit left/right" << endl;
236 setViewCache(&cur.bv());
237 int const pit = left ? 0 : paragraphs().size() - 1;
238 int const pos = left ? 0 : paragraphs().back().size();
239 text_.setCursor(cur.top(), pit, pos);
240 cur.clearSelection();
245 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
247 return text_.editXY(cur, x, y);
251 void InsetText::forceParagraphsToDefault(LCursor & cur)
253 BufferParams const & bp = cur.buffer().params();
254 LyXLayout_ptr const layout =
255 bp.getLyXTextClass().defaultLayout();
256 ParagraphList::iterator const end = paragraphs().end();
257 for (ParagraphList::iterator par = paragraphs().begin();
263 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
265 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
266 << " [ cmd.action = " << cmd.action << ']' << endl;
267 setViewCache(&cur.bv());
268 text_.dispatch(cur, cmd);
272 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
273 FuncStatus & status) const
275 return text_.getStatus(cur, cmd, status);
279 int InsetText::latex(Buffer const & buf, ostream & os,
280 OutputParams const & runparams) const
283 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
284 return texrow.rows();
288 int InsetText::plaintext(Buffer const & buf, ostream & os,
289 OutputParams const & runparams) const
291 ParagraphList::const_iterator beg = paragraphs().begin();
292 ParagraphList::const_iterator end = paragraphs().end();
293 ParagraphList::const_iterator it = beg;
294 bool ref_printed = false;
295 for (; it != end; ++it)
296 asciiParagraph(buf, *it, os, runparams, ref_printed);
298 // FIXME: Give the total numbers of lines
303 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
304 OutputParams const & runparams) const
306 linuxdocParagraphs(buf, paragraphs(), os, runparams);
311 int InsetText::docbook(Buffer const & buf, ostream & os,
312 OutputParams const & runparams) const
314 docbookParagraphs(paragraphs(), buf, os, runparams);
319 void InsetText::validate(LaTeXFeatures & features) const
321 for_each(paragraphs().begin(), paragraphs().end(),
322 bind(&Paragraph::validate, _1, ref(features)));
326 void InsetText::cursorPos
327 (CursorSlice const & sl, bool boundary, int & x, int & y) const
329 x = text_.cursorX(sl, boundary) + border_;
330 y = text_.cursorY(sl, boundary);
334 bool InsetText::showInsetDialog(BufferView *) const
340 void InsetText::markNew(bool track_changes)
342 ParagraphList::iterator pit = paragraphs().begin();
343 ParagraphList::iterator end = paragraphs().end();
344 for (; pit != end; ++pit) {
347 else // no-op when not tracking
353 void InsetText::setText(string const & data, LyXFont const & font)
356 Paragraph & first = paragraphs().front();
357 for (unsigned int i = 0; i < data.length(); ++i)
358 first.insertChar(i, data[i], font);
362 void InsetText::setAutoBreakRows(bool flag)
364 if (flag == text_.autoBreakRows_)
367 text_.autoBreakRows_ = flag;
371 // remove previously existing newlines
372 ParagraphList::iterator it = paragraphs().begin();
373 ParagraphList::iterator end = paragraphs().end();
374 for (; it != end; ++it)
375 for (int i = 0; i < it->size(); ++i)
376 if (it->isNewline(i))
381 void InsetText::setDrawFrame(bool flag)
387 LColor_color InsetText::frameColor() const
389 return LColor::color(frame_color_);
393 void InsetText::setFrameColor(LColor_color col)
399 void InsetText::setViewCache(BufferView const * bv) const
401 if (bv && bv != text_.bv_owner) {
402 //lyxerr << "setting view cache from "
403 // << text_.bv_owner << " to " << bv << "\n";
404 text_.bv_owner = const_cast<BufferView *>(bv);
409 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
412 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
413 // And it probably does. You have to take a look at this John. (Lgb)
414 #warning John, have a look here. (Lgb)
416 ParagraphList & pl = paragraphs();
418 ParagraphList::iterator pit = plist.begin();
419 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
421 mergeParagraph(buffer->params(), pl,
422 std::distance(pl.begin(), ins) - 1);
424 for_each(pit, plist.end(),
425 bind(&ParagraphList::push_back, ref(pl), _1));
429 void InsetText::addPreview(PreviewLoader & loader) const
431 ParagraphList::const_iterator pit = paragraphs().begin();
432 ParagraphList::const_iterator pend = paragraphs().end();
434 for (; pit != pend; ++pit) {
435 InsetList::const_iterator it = pit->insetlist.begin();
436 InsetList::const_iterator end = pit->insetlist.end();
437 for (; it != end; ++it)
438 it->inset->addPreview(loader);
443 //FIXME: instead of this hack, which only works by chance,
444 // cells should have their own insetcell type, which returns CELL_CODE!
445 bool InsetText::neverIndent() const
447 // this is only true for tabular cells
448 return !text_.isMainText() && lyxCode() == TEXT_CODE;
452 ParagraphList const & InsetText::paragraphs() const
454 return text_.paragraphs();
458 ParagraphList & InsetText::paragraphs()
460 return text_.paragraphs();