]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
* BufferParams:
[lyx.git] / src / insets / insettext.C
index 0b843353a0bc99f2669b6b141abe10f1ca92d85c..9b7426c643e8580853a835d74cae7bca4953c623 100644 (file)
@@ -31,6 +31,7 @@
 #include "lyxrc.h"
 #include "lyxtext.h"
 #include "metricsinfo.h"
+#include "outputparams.h"
 #include "output_docbook.h"
 #include "output_latex.h"
 #include "output_plaintext.h"
@@ -76,7 +77,7 @@ int InsetText::border_ = 2;
 
 
 InsetText::InsetText(BufferParams const & bp)
-       : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
+       : drawFrame_(false), frame_color_(LColor::insetframe)
 {
        paragraphs().push_back(Paragraph());
        paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
@@ -103,7 +104,6 @@ InsetText::InsetText(InsetText const & in)
 
 
 InsetText::InsetText()
-       : text_(0)
 {}
 
 
@@ -164,36 +164,41 @@ void InsetText::read(Buffer const & buf, LyXLex & lex)
 }
 
 
-void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
+bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+       TextMetrics & tm = mi.base.bv->textMetrics(&text_);
+
        //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
        mi.base.textwidth -= 2 * border_;
        font_ = mi.base.font;
        // Hand font through to contained lyxtext:
        text_.font_ = mi.base.font;
-       text_.metrics(mi, dim);
+       tm.metrics(mi, dim);
        dim.asc += border_;
        dim.des += border_;
        dim.wid += 2 * border_;
        mi.base.textwidth += 2 * border_;
+       bool const changed = dim_ != dim;
        dim_ = dim;
+       return changed;
 }
 
 
 void InsetText::draw(PainterInfo & pi, int x, int y) const
 {
-       BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
        // update our idea of where we are
        setPosCache(pi, x, y);
 
+       TextMetrics & tm = pi.base.bv->textMetrics(&text_);
+
        text_.background_color_ = backgroundColor();
        text_.draw(pi, x + border_, y);
 
        if (drawFrame_) {
-               int const w = text_.width() + 2 * border_;
-               int const a = text_.ascent() + border_;
-               int const h = a + text_.descent() + border_;
-               pi.pain.rectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
+               int const w = tm.width() + 2 * border_;
+               int const a = tm.ascent() + border_;
+               int const h = a + tm.descent() + border_;
+               pi.pain.rectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
                        frameColor());
        }
 }
@@ -201,20 +206,24 @@ void InsetText::draw(PainterInfo & pi, int x, int y) const
 
 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
 {
-       int const w = text_.width() + 2 * border_;
-       int const a = text_.ascent() + border_;
-       int const h = a + text_.descent() + border_;
-       pi.pain.fillRectangle(x, y - a, (wide() ? text_.maxwidth_ : w), h,
+       TextMetrics & tm = pi.base.bv->textMetrics(&text_);
+
+       int const w = tm.width() + 2 * border_;
+       int const a = tm.ascent() + border_;
+       int const h = a + tm.descent() + border_;
+       pi.pain.fillRectangle(x, y - a, (wide() ? tm.maxWidth() : w), h,
                backgroundColor());
-       text_.drawSelection(pi, x, y);
+       text_.drawSelection(pi, x + border_, y);
 }
 
 
 bool InsetText::covers(BufferView const & bv, int x, int y) const
 {
+       TextMetrics const & tm = bv.textMetrics(&text_);
+
        return bv.coordCache().getInsets().has(this)
                        && x >= xo(bv)
-                       && x <= xo(bv) + width() + (wide() ? text_.maxwidth_ : 0)
+                       && x <= xo(bv) + width() + (wide() ? tm.maxWidth() : 0)
                        && y >= yo(bv) - ascent()
                        && y <= yo(bv) + descent();
 }
@@ -269,32 +278,20 @@ void InsetText::setChange(Change const & change)
 }
 
 
-void InsetText::acceptChanges()
+void InsetText::acceptChanges(BufferParams const & bparams)
 {
-       ParagraphList::iterator pit = paragraphs().begin();
-       ParagraphList::iterator end = paragraphs().end();
-       for (; pit != end; ++pit) {
-               // FIXME: change tracking (MG)
-               // we must handle end-of-par chars!
-               pit->acceptChanges(0, pit->size() + 1);
-       }
+       text_.acceptChanges(bparams);
 }
 
 
-void InsetText::rejectChanges()
+void InsetText::rejectChanges(BufferParams const & bparams)
 {
-       ParagraphList::iterator pit = paragraphs().begin();
-       ParagraphList::iterator end = paragraphs().end();
-       for (; pit != end; ++pit) {
-               // FIXME: change tracking (MG)
-               // we must handle end-of-par chars!
-               pit->rejectChanges(0, pit->size() + 1);
-       }
+       text_.rejectChanges(bparams);
 }
 
 
 int InsetText::latex(Buffer const & buf, odocstream & os,
-                    OutputParams const & runparams) const
+                     OutputParams const & runparams) const
 {
        TexRow texrow;
        latexParagraphs(buf, paragraphs(), os, texrow, runparams);
@@ -303,25 +300,34 @@ int InsetText::latex(Buffer const & buf, odocstream & os,
 
 
 int InsetText::plaintext(Buffer const & buf, odocstream & os,
-                    OutputParams const & runparams) const
+                         OutputParams const & runparams) const
 {
        ParagraphList::const_iterator beg = paragraphs().begin();
        ParagraphList::const_iterator end = paragraphs().end();
        ParagraphList::const_iterator it = beg;
        bool ref_printed = false;
-       odocstringstream oss;
-       for (; it != end; ++it)
-               asciiParagraph(buf, *it, oss, runparams, ref_printed);
+       int len = 0;
+       for (; it != end; ++it) {
+               if (it != beg) {
+                       os << '\n';
+                       if (runparams.linelen > 0)
+                               os << '\n';
+               }
+               odocstringstream oss;
+               writePlaintextParagraph(buf, *it, oss, runparams, ref_printed);
+               docstring const str = oss.str();
+               os << str;
+               // FIXME: len is not computed fully correctly; in principle,
+               // we have to count the characters after the last '\n'
+               len = str.size();
+       }
 
-       docstring const str = oss.str();
-       os << str;
-       // Return how many newlines we issued.
-       return int(lyx::count(str.begin(), str.end(), '\n'));
+       return len;
 }
 
 
 int InsetText::docbook(Buffer const & buf, odocstream & os,
-                      OutputParams const & runparams) const
+                       OutputParams const & runparams) const
 {
        docbookParagraphs(paragraphs(), buf, os, runparams);
        return 0;
@@ -338,8 +344,8 @@ void InsetText::validate(LaTeXFeatures & features) const
 void InsetText::cursorPos(BufferView const & bv,
                CursorSlice const & sl, bool boundary, int & x, int & y) const
 {
-       x = text_.cursorX(*bv.buffer(), sl, boundary) + border_;
-       y = text_.cursorY(sl, boundary);
+       x = text_.cursorX(bv, sl, boundary) + border_;
+       y = text_.cursorY(bv, sl, boundary);
 }