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>
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();
166 bool res = text_.read(buf, lex, errorList);
170 lex.printError("Missing \\end_inset at this point. "
175 // ensure we have at least one paragraph.
176 if (paragraphs().empty())
177 paragraphs().push_back(oldpar);
181 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
183 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
184 setViewCache(mi.base.bv);
185 mi.base.textwidth -= 2 * border_;
186 font_ = mi.base.font;
187 // Hand font through to contained lyxtext:
188 text_.font_ = mi.base.font;
189 text_.metrics(mi, dim);
192 dim.wid += 2 * border_;
193 mi.base.textwidth += 2 * border_;
198 void InsetText::draw(PainterInfo & pi, int x, int y) const
200 BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
201 // update our idea of where we are
202 setPosCache(pi, x, y);
204 text_.background_color_ = backgroundColor();
205 text_.draw(pi, x + border_, y);
208 int const w = text_.width() + 2 * border_;
209 int const a = text_.ascent() + border_;
210 int const h = a + text_.descent() + border_;
211 pi.pain.rectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
217 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
219 int const w = text_.width() + 2 * border_;
220 int const a = text_.ascent() + border_;
221 int const h = a + text_.descent() + border_;
222 pi.pain.fillRectangle(x, y - a, (Wide() ? text_.maxwidth_ : w), h,
224 text_.drawSelection(pi, x, y);
228 string const InsetText::editMessage() const
230 return _("Opened Text Inset");
234 void InsetText::edit(LCursor & cur, bool left)
236 //lyxerr << "InsetText: edit left/right" << endl;
237 setViewCache(&cur.bv());
238 int const pit = left ? 0 : paragraphs().size() - 1;
239 int const pos = left ? 0 : paragraphs().back().size();
240 text_.setCursor(cur.top(), pit, pos);
241 cur.clearSelection();
246 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
248 return text_.editXY(cur, x, y);
252 void InsetText::forceParagraphsToDefault(LCursor & cur)
254 BufferParams const & bp = cur.buffer().params();
255 LyXLayout_ptr const layout =
256 bp.getLyXTextClass().defaultLayout();
257 ParagraphList::iterator const end = paragraphs().end();
258 for (ParagraphList::iterator par = paragraphs().begin();
264 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
266 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
267 << " [ cmd.action = "
268 << cmd.action << ']' << endl;
269 setViewCache(&cur.bv());
271 switch (cmd.action) {
273 case LFUN_CHAR_DELETE_FORWARD: {
274 if (!cur.selection() && cur.depth() > 1
275 && cur.pit() == cur.lastpit()
276 && cur.pos() == cur.lastpos())
277 // Merge inset with owner
278 cmd = FuncRequest(LFUN_INSET_DISSOLVE);
279 text_.dispatch(cur, cmd);
283 case LFUN_CHAR_DELETE_BACKWARD: {
284 if (cur.depth() > 1 && cur.pit() == 0 && cur.pos() == 0)
285 // Merge inset with owner
286 cmd = FuncRequest(LFUN_INSET_DISSOLVE);
287 text_.dispatch(cur, cmd);
292 text_.dispatch(cur, cmd);
298 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
299 FuncStatus & status) const
301 switch (cmd.action) {
303 case LFUN_CHAR_DELETE_FORWARD:
304 case LFUN_CHAR_DELETE_BACKWARD:
305 status.enabled(true);
309 return text_.getStatus(cur, cmd, status);
314 int InsetText::latex(Buffer const & buf, ostream & os,
315 OutputParams const & runparams) const
318 latexParagraphs(buf, paragraphs(), os, texrow, runparams);
319 return texrow.rows();
323 int InsetText::plaintext(Buffer const & buf, ostream & os,
324 OutputParams const & runparams) const
326 ParagraphList::const_iterator beg = paragraphs().begin();
327 ParagraphList::const_iterator end = paragraphs().end();
328 ParagraphList::const_iterator it = beg;
329 bool ref_printed = false;
330 for (; it != end; ++it)
331 asciiParagraph(buf, *it, os, runparams, ref_printed);
333 // FIXME: Give the total numbers of lines
338 int InsetText::docbook(Buffer const & buf, ostream & os,
339 OutputParams const & runparams) const
341 docbookParagraphs(paragraphs(), buf, os, runparams);
346 void InsetText::validate(LaTeXFeatures & features) const
348 for_each(paragraphs().begin(), paragraphs().end(),
349 bind(&Paragraph::validate, _1, ref(features)));
353 void InsetText::cursorPos
354 (CursorSlice const & sl, bool boundary, int & x, int & y) const
356 x = text_.cursorX(sl, boundary) + border_;
357 y = text_.cursorY(sl, boundary);
361 bool InsetText::showInsetDialog(BufferView *) const
367 void InsetText::markNew(bool track_changes)
369 ParagraphList::iterator pit = paragraphs().begin();
370 ParagraphList::iterator end = paragraphs().end();
371 for (; pit != end; ++pit) {
374 else // no-op when not tracking
380 void InsetText::setText(string const & data, LyXFont const & font)
383 Paragraph & first = paragraphs().front();
384 for (unsigned int i = 0; i < data.length(); ++i)
385 first.insertChar(i, data[i], font);
389 void InsetText::setAutoBreakRows(bool flag)
391 if (flag == text_.autoBreakRows_)
394 text_.autoBreakRows_ = flag;
398 // remove previously existing newlines
399 ParagraphList::iterator it = paragraphs().begin();
400 ParagraphList::iterator end = paragraphs().end();
401 for (; it != end; ++it)
402 for (int i = 0; i < it->size(); ++i)
403 if (it->isNewline(i))
408 void InsetText::setDrawFrame(bool flag)
414 LColor_color InsetText::frameColor() const
416 return LColor::color(frame_color_);
420 void InsetText::setFrameColor(LColor_color col)
426 void InsetText::setViewCache(BufferView const * bv) const
428 if (bv && bv != text_.bv_owner) {
429 //lyxerr << "setting view cache from "
430 // << text_.bv_owner << " to " << bv << "\n";
431 text_.bv_owner = const_cast<BufferView *>(bv);
436 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
439 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
440 // And it probably does. You have to take a look at this John. (Lgb)
441 #warning John, have a look here. (Lgb)
443 ParagraphList & pl = paragraphs();
445 ParagraphList::iterator pit = plist.begin();
446 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
448 mergeParagraph(buffer->params(), pl,
449 std::distance(pl.begin(), ins) - 1);
451 for_each(pit, plist.end(),
452 bind(&ParagraphList::push_back, ref(pl), _1));
456 void InsetText::addPreview(PreviewLoader & loader) const
458 ParagraphList::const_iterator pit = paragraphs().begin();
459 ParagraphList::const_iterator pend = paragraphs().end();
461 for (; pit != pend; ++pit) {
462 InsetList::const_iterator it = pit->insetlist.begin();
463 InsetList::const_iterator end = pit->insetlist.end();
464 for (; it != end; ++it)
465 it->inset->addPreview(loader);
470 //FIXME: instead of this hack, which only works by chance,
471 // cells should have their own insetcell type, which returns CELL_CODE!
472 bool InsetText::neverIndent() const
474 // this is only true for tabular cells
475 return !text_.isMainText() && lyxCode() == TEXT_CODE;
479 ParagraphList const & InsetText::paragraphs() const
481 return text_.paragraphs();
485 ParagraphList & InsetText::paragraphs()
487 return text_.paragraphs();