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"
25 #include "FuncStatus.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/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>
58 using lyx::graphics::PreviewLoader;
60 using lyx::support::isStrUnsignedInt;
74 int InsetText::border_ = 2;
77 InsetText::InsetText(BufferParams const & bp)
78 : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
80 paragraphs().push_back(Paragraph());
81 paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
82 if (bp.tracking_changes)
83 paragraphs().back().trackChanges();
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::markErased(bool erased)
120 ParagraphList & pars = paragraphs();
121 for_each(pars.begin(), pars.end(),
122 bind(&Paragraph::markErased, _1, erased));
126 void InsetText::clear()
128 ParagraphList & pars = paragraphs();
130 // This is a gross hack...
131 LyXLayout_ptr old_layout = pars.begin()->layout();
134 pars.push_back(Paragraph());
135 pars.begin()->setInsetOwner(this);
136 pars.begin()->layout(old_layout);
140 auto_ptr<InsetBase> InsetText::doClone() const
142 return auto_ptr<InsetBase>(new InsetText(*this));
146 void InsetText::write(Buffer const & buf, ostream & os) const
149 text_.write(buf, os);
153 void InsetText::read(Buffer const & buf, LyXLex & lex)
158 #warning John, look here. Doesnt make much sense.
160 if (buf.params().tracking_changes)
161 paragraphs().begin()->trackChanges();
163 // delete the initial paragraph
164 Paragraph oldpar = *paragraphs().begin();
165 paragraphs().clear();
167 bool res = text_.read(buf, lex, errorList);
171 lex.printError("Missing \\end_inset at this point. "
176 // ensure we have at least one paragraph.
177 if (paragraphs().empty())
178 paragraphs().push_back(oldpar);
182 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
184 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
185 setViewCache(mi.base.bv);
186 mi.base.textwidth -= 2 * border_;
187 font_ = mi.base.font;
188 // Hand font through to contained lyxtext:
189 text_.font_ = mi.base.font;
190 text_.metrics(mi, dim);
193 dim.wid += 2 * border_;
194 mi.base.textwidth += 2 * border_;
199 void InsetText::draw(PainterInfo & pi, int x, int y) const
201 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
202 // update our idea of where we are
203 setPosCache(pi, x, y);
205 text_.background_color_ = backgroundColor();
206 text_.draw(pi, x + border_, y);
209 int const w = text_.width() + 2 * border_;
210 int const a = text_.ascent() + border_;
211 int const h = a + text_.descent() + border_;
212 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
218 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
220 int const w = text_.width() + 2 * border_;
221 int const a = text_.ascent() + border_;
222 int const h = a + text_.descent() + border_;
223 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
225 text_.drawSelection(pi, x, y);
229 string const InsetText::editMessage() const
231 return _("Opened Text Inset");
235 void InsetText::edit(LCursor & cur, bool left)
237 //lyxerr << "InsetText: edit left/right" << endl;
238 setViewCache(&cur.bv());
239 int const pit = left ? 0 : paragraphs().size() - 1;
240 int const pos = left ? 0 : paragraphs().back().size();
241 text_.setCursor(cur.top(), pit, pos);
242 cur.clearSelection();
247 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
249 return text_.editXY(cur, x, y);
253 void InsetText::forceParagraphsToDefault(LCursor & cur)
255 BufferParams const & bp = cur.buffer().params();
256 LyXLayout_ptr const layout =
257 bp.getLyXTextClass().defaultLayout();
258 ParagraphList::iterator const end = paragraphs().end();
259 for (ParagraphList::iterator par = paragraphs().begin();
265 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
267 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
268 << " [ cmd.action = "
269 << cmd.action << ']' << endl;
270 setViewCache(&cur.bv());
272 switch (cmd.action) {
274 case LFUN_CHAR_DELETE_FORWARD: {
275 if (!cur.selection() && cur.depth() > 1
276 && cur.pit() == cur.lastpit()
277 && cur.pos() == cur.lastpos())
278 // Merge inset with owner
279 cmd = FuncRequest(LFUN_INSET_DISSOLVE);
280 text_.dispatch(cur, cmd);
284 case LFUN_CHAR_DELETE_BACKWARD: {
285 if (cur.depth() > 1 && cur.pit() == 0 && cur.pos() == 0)
286 // Merge inset with owner
287 cmd = FuncRequest(LFUN_INSET_DISSOLVE);
288 text_.dispatch(cur, cmd);
293 text_.dispatch(cur, cmd);
299 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
300 FuncStatus & status) const
302 switch (cmd.action) {
304 case LFUN_CHAR_DELETE_FORWARD:
305 case LFUN_CHAR_DELETE_BACKWARD:
306 status.enabled(true);
310 return text_.getStatus(cur, cmd, status);
315 int InsetText::latex(Buffer const & buf, ostream & os,
316 OutputParams const & runparams) const
319 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
320 return texrow.rows();
324 int InsetText::plaintext(Buffer const & buf, ostream & os,
325 OutputParams const & runparams) const
327 ParagraphList::const_iterator beg = paragraphs().begin();
328 ParagraphList::const_iterator end = paragraphs().end();
329 ParagraphList::const_iterator it = beg;
330 bool ref_printed = false;
331 for (; it != end; ++it)
332 asciiParagraph(buf, *it, os, runparams, ref_printed);
334 // FIXME: Give the total numbers of lines
339 int InsetText::docbook(Buffer const & buf, ostream & os,
340 OutputParams const & runparams) const
342 docbookParagraphs(paragraphs(), buf, os, runparams);
347 void InsetText::validate(LaTeXFeatures & features) const
349 for_each(paragraphs().begin(), paragraphs().end(),
350 bind(&Paragraph::validate, _1, ref(features)));
354 void InsetText::cursorPos
355 (CursorSlice const & sl, bool boundary, int & x, int & y) const
357 x = text_.cursorX(sl, boundary) + border_;
358 y = text_.cursorY(sl, boundary);
362 bool InsetText::showInsetDialog(BufferView *) const
368 void InsetText::markNew(bool track_changes)
370 ParagraphList::iterator pit = paragraphs().begin();
371 ParagraphList::iterator end = paragraphs().end();
372 for (; pit != end; ++pit) {
375 else // no-op when not tracking
381 void InsetText::setText(docstring const & data, LyXFont const & font)
384 Paragraph & first = paragraphs().front();
385 for (unsigned int i = 0; i < data.length(); ++i)
386 first.insertChar(i, data[i], font);
390 void InsetText::setAutoBreakRows(bool flag)
392 if (flag == text_.autoBreakRows_)
395 text_.autoBreakRows_ = flag;
399 // remove previously existing newlines
400 ParagraphList::iterator it = paragraphs().begin();
401 ParagraphList::iterator end = paragraphs().end();
402 for (; it != end; ++it)
403 for (int i = 0; i < it->size(); ++i)
404 if (it->isNewline(i))
409 void InsetText::setDrawFrame(bool flag)
415 LColor_color InsetText::frameColor() const
417 return LColor::color(frame_color_);
421 void InsetText::setFrameColor(LColor_color col)
427 void InsetText::setViewCache(BufferView const * bv) const
429 if (bv && bv != text_.bv_owner) {
430 //lyxerr << "setting view cache from "
431 // << text_.bv_owner << " to " << bv << "\n";
432 text_.bv_owner = const_cast<BufferView *>(bv);
437 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
440 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
441 // And it probably does. You have to take a look at this John. (Lgb)
442 #warning John, have a look here. (Lgb)
444 ParagraphList & pl = paragraphs();
446 ParagraphList::iterator pit = plist.begin();
447 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
449 mergeParagraph(buffer->params(), pl,
450 std::distance(pl.begin(), ins) - 1);
452 for_each(pit, plist.end(),
453 bind(&ParagraphList::push_back, ref(pl), _1));
457 void InsetText::addPreview(PreviewLoader & loader) const
459 ParagraphList::const_iterator pit = paragraphs().begin();
460 ParagraphList::const_iterator pend = paragraphs().end();
462 for (; pit != pend; ++pit) {
463 InsetList::const_iterator it = pit->insetlist.begin();
464 InsetList::const_iterator end = pit->insetlist.end();
465 for (; it != end; ++it)
466 it->inset->addPreview(loader);
471 //FIXME: instead of this hack, which only works by chance,
472 // cells should have their own insetcell type, which returns CELL_CODE!
473 bool InsetText::neverIndent() const
475 // this is only true for tabular cells
476 return !text_.isMainText() && lyxCode() == TEXT_CODE;
480 ParagraphList const & InsetText::paragraphs() const
482 return text_.paragraphs();
486 ParagraphList & InsetText::paragraphs()
488 return text_.paragraphs();