]> git.lyx.org Git - lyx.git/blobdiff - src/TextMetrics.cpp
Transfer metrics and screen related methods from Text to TextMetrics.
[lyx.git] / src / TextMetrics.cpp
index 061b07fbb10fe1c5e98694b5e25ef390fd67c112..9bde046c8edc25a659edfeba7285a1e40c166512 100644 (file)
 #include "TextMetrics.h"
 
 #include "Buffer.h"
+#include "buffer_funcs.h"
 #include "BufferParams.h"
 #include "BufferView.h"
 #include "bufferview_funcs.h"
 #include "Color.h"
 #include "CoordCache.h"
+#include "CutAndPaste.h"
 #include "debug.h"
 #include "FontIterator.h"
 #include "FuncRequest.h"
@@ -1400,6 +1402,61 @@ void TextMetrics::cursorNext(Cursor & cur)
 }
 
 
+// the cursor set functions have a special mechanism. When they
+// realize you left an empty paragraph, they will delete it.
+
+bool TextMetrics::cursorHome(Cursor & cur)
+{
+       BOOST_ASSERT(text_ == cur.text());
+       ParagraphMetrics const & pm = par_metrics_[cur.pit()];
+       Row const & row = pm.getRow(cur.pos(),cur.boundary());
+       return text_->setCursor(cur, cur.pit(), row.pos());
+}
+
+
+bool TextMetrics::cursorEnd(Cursor & cur)
+{
+       BOOST_ASSERT(text_ == cur.text());
+       // if not on the last row of the par, put the cursor before
+       // the final space exept if I have a spanning inset or one string
+       // is so long that we force a break.
+       pos_type end = cur.textRow().endpos();
+       if (end == 0)
+               // empty text, end-1 is no valid position
+               return false;
+       bool boundary = false;
+       if (end != cur.lastpos()) {
+               if (!cur.paragraph().isLineSeparator(end-1)
+                   && !cur.paragraph().isNewline(end-1))
+                       boundary = true;
+               else
+                       --end;
+       }
+       return text_->setCursor(cur, cur.pit(), end, true, boundary);
+}
+
+
+void TextMetrics::deleteLineForward(Cursor & cur)
+{
+       BOOST_ASSERT(text_ == cur.text());
+       if (cur.lastpos() == 0) {
+               // Paragraph is empty, so we just go to the right
+               text_->cursorRight(cur);
+       } else {
+               cur.resetAnchor();
+               cur.selection() = true; // to avoid deletion
+               cursorEnd(cur);
+               cur.setSelection();
+               // What is this test for ??? (JMarc)
+               if (!cur.selection())
+                       text_->deleteWordForward(cur);
+               else
+                       cap::cutSelection(cur, true, false);
+               checkBufferStructure(cur.buffer(), cur);
+       }
+}
+
+
 int TextMetrics::leftMargin(int max_width, pit_type pit) const
 {
        BOOST_ASSERT(pit >= 0);
@@ -1703,6 +1760,11 @@ void TextMetrics::drawSelection(PainterInfo & pi, int x, int) const
            || bv_funcs::status(bv_, end) == bv_funcs::CUR_ABOVE)
                return;
 
+       if (beg.pit() < par_metrics_.begin()->first)
+               beg.pit() = par_metrics_.begin()->first;
+       if (end.pit() > par_metrics_.rbegin()->first)
+               end.pit() = par_metrics_.rbegin()->first;
+
        ParagraphMetrics const & pm1 = par_metrics_[beg.pit()];
        ParagraphMetrics const & pm2 = par_metrics_[end.pit()];
        Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());