[LyX/master] Display bookmarks in the workarea (take 2).

Jean-Marc Lasgouttes lasgouttes at lyx.org
Fri Feb 26 18:11:06 UTC 2021


commit 42b23f3fb24ea38e2b40543f4822a62d492e305a
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Fri Feb 26 19:05:35 2021 +0100

    Display bookmarks in the workarea (take 2).
    
    The bookmarks are shown with circled numbers in the right margin (or
    the left margin in RTL mode). A new color "bookmarks" has been added.
    Currently bookmark 0 (the return position) is not displayed because it
    is very disturbing in practice.
    
    To make this work, a new method BookmarksSection::bookmarksInPar
    retuns the list of bookmarks in a paragraph along with their position.
    
    Force redraw when using bookmark-save and bookmark-clear.
    
    Caveats:
    
    - this solution does not show the precise position of the bookmark
      (this is supposedly not a problem).
    
    - if several bookmarks are on the same row, they will currently be
      garbled. It would be easy to make sure that only one is shown ; what
      would be more difficult would be to move the second bookmark lower.
    
    - it is possible to make sure that the markers are correctly centered
      in the margin, and that the margin size is large enough to hold the
      marker (I did not try all fonts).
    
    Fixes bug #2496.
---
 src/BufferView.cpp                  |    1 +
 src/Color.cpp                       |    3 ++-
 src/ColorCode.h                     |    2 ++
 src/ParagraphMetrics.h              |    2 ++
 src/RowPainter.cpp                  |   11 +++++++++++
 src/RowPainter.h                    |    1 +
 src/Session.cpp                     |   14 ++++++++++++++
 src/Session.h                       |    6 ++++++
 src/TextMetrics.cpp                 |    9 +++++++++
 src/frontends/qt/GuiApplication.cpp |    1 +
 10 files changed, 49 insertions(+), 1 deletions(-)

diff --git a/src/BufferView.cpp b/src/BufferView.cpp
index 373baa4..0025277 100644
--- a/src/BufferView.cpp
+++ b/src/BufferView.cpp
@@ -1472,6 +1472,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 		break;
 
 	case LFUN_BOOKMARK_SAVE:
+		dr.screenUpdate(Update::Force);
 		saveBookmark(convert<unsigned int>(to_utf8(cmd.argument())));
 		break;
 
diff --git a/src/Color.cpp b/src/Color.cpp
index 53fd0ee..cf297e9 100644
--- a/src/Color.cpp
+++ b/src/Color.cpp
@@ -341,8 +341,9 @@ ColorSet::ColorSet()
 	{ Color_buttonhoverbg, N_("button background under focus"), "buttonhoverbg", "#C7C7CA", "#C7C7CA", "buttonhoverbg" },
 	{ Color_paragraphmarker, N_("paragraph marker"), "paragraphmarker", grey80, grey40, "paragraphmarker"},
 	{ Color_previewframe, N_("preview frame"), "previewframe", black, Linen, "previewframe"},
-	{ Color_inherit, N_("inherit"), "inherit", black, Linen, "inherit" },
 	{ Color_regexpframe, N_("regexp frame"), "regexpframe", Green, green, "regexpframe" },
+	{ Color_bookmark, N_("bookmark"), "bookmark", RoyalBlue, RoyalBlue, "bookmark" },
+	{ Color_inherit, N_("inherit"), "inherit", black, Linen, "inherit" },
 	{ Color_ignore, N_("ignore"), "ignore", black, Linen, "ignore" },
 	{ Color_ignore, nullptr, nullptr, nullptr, nullptr, nullptr }
 	};
diff --git a/src/ColorCode.h b/src/ColorCode.h
index 9badae9..c7253ff 100644
--- a/src/ColorCode.h
+++ b/src/ColorCode.h
@@ -228,6 +228,8 @@ enum ColorCode {
 	Color_paragraphmarker,
 	/// Preview frame color
 	Color_previewframe,
+	/// Bookmark indicator color
+	Color_bookmark,
 
 	// Logical attributes
 
diff --git a/src/ParagraphMetrics.h b/src/ParagraphMetrics.h
index 2e57f83..9889473 100644
--- a/src/ParagraphMetrics.h
+++ b/src/ParagraphMetrics.h
@@ -69,6 +69,8 @@ public:
 	RowList const & rows() const { return rows_; }
 	///
 	int rightMargin(BufferView const & bv) const;
+	///
+	Paragraph const & par() const { return *par_; }
 
 	/// dump some information to lyxerr
 	void dump() const;
diff --git a/src/RowPainter.cpp b/src/RowPainter.cpp
index 2175e4f..8cc7202 100644
--- a/src/RowPainter.cpp
+++ b/src/RowPainter.cpp
@@ -627,7 +627,18 @@ void RowPainter::paintSelection() const
 		pi_.pain.fillRectangle(int(x), y1,
 		                       int(xo_ + tm_.width()) - int(x), y2 - y1,
 		                       Color_selection);
+}
+
 
+void RowPainter::paintBookmark(int num) const
+{
+	int const x = row_.isRTL()
+		? pi_.base.bv->workWidth() - pi_.base.bv->rightMargin() : 0;
+	FontInfo fi = pi_.base.bv->buffer().params().getFont().fontInfo();
+	fi.setColor(Color_bookmark);
+	// ❶ U+2776 DINGBAT NEGATIVE CIRCLED DIGIT ONE
+	char_type const ch = 0x2775 + num;
+	pi_.pain.text(x, yo_, ch, fi);
 }
 
 
diff --git a/src/RowPainter.h b/src/RowPainter.h
index 275ff92..918fc9f 100644
--- a/src/RowPainter.h
+++ b/src/RowPainter.h
@@ -44,6 +44,7 @@ public:
 	void paintText();
 	void paintOnlyInsets();
 	void paintSelection() const;
+	void paintBookmark(int num) const;
 
 private:
 	void paintLanguageMarkings(Row::Element const & e) const;
diff --git a/src/Session.cpp b/src/Session.cpp
index f8d6116..16b6df7 100644
--- a/src/Session.cpp
+++ b/src/Session.cpp
@@ -339,6 +339,20 @@ BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) co
 }
 
 
+BookmarksSection::BookmarkPosList
+BookmarksSection::bookmarksInPar(FileName const & fn, int const par_id) const
+{
+	// FIXME: we do not consider the case of bottom_pit.
+	// This is probably not a problem.
+	BookmarksSection::BookmarkPosList bip;
+	for (size_t i = 1; i < bookmarks.size(); ++i)
+		if (bookmarks[i].filename == fn && bookmarks[i].top_id == par_id)
+			bip.push_back({i, bookmarks[i].top_pos});
+
+	return bip;
+}
+
+
 LastCommandsSection::LastCommandsSection(unsigned int num) :
 	default_num_last_commands(30),
 	absolute_max_last_commands(100)
diff --git a/src/Session.h b/src/Session.h
index 09e9d7d..857e056 100644
--- a/src/Session.h
+++ b/src/Session.h
@@ -262,6 +262,12 @@ public:
 	*/
 	BookmarkList & load() { return bookmarks; }
 
+	///
+	typedef std::vector<std::pair<unsigned int, pos_type>> BookmarkPosList;
+
+	/// return a list of bookmarks and position for this paragraph
+	BookmarkPosList bookmarksInPar(support::FileName const & fn, int par_id) const;
+
 private:
 
 	/// allow 9 regular bookmarks, bookmark 0 is temporary
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index 6616956..e95757c 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -30,6 +30,7 @@
 #include "MetricsInfo.h"
 #include "ParagraphParameters.h"
 #include "RowPainter.h"
+#include "Session.h"
 #include "Text.h"
 #include "TextClass.h"
 #include "VSpace.h"
@@ -1857,6 +1858,9 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
 	if (text_->isRTL(pit))
 		swap(pi.leftx, pi.rightx);
 
+	BookmarksSection::BookmarkPosList bpl =
+		theSession().bookmarks().bookmarksInPar(bv_->buffer().fileName(), pm.par().id());
+
 	for (size_t i = 0; i != nrows; ++i) {
 
 		Row const & row = pm.rows()[i];
@@ -1945,6 +1949,11 @@ void TextMetrics::drawParagraph(PainterInfo & pi, pit_type const pit, int const
 		rp.paintText();
 		rp.paintTooLargeMarks(row_x + row.left_x() < 0,
 				      row_x + row.right_x() > bv_->workWidth());
+		// indicate bookmarks presence in margin
+		for (auto const & bp_p : bpl)
+			if (bp_p.second >= row.pos() && bp_p.second < row.endpos())
+				rp.paintBookmark(bp_p.first);
+
 		y += row.descent();
 
 #if 0
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index c504ade..2296a52 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -2192,6 +2192,7 @@ void GuiApplication::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
 	case LFUN_BOOKMARK_CLEAR:
 		theSession().bookmarks().clear();
+		dr.screenUpdate(Update::Force);
 		break;
 
 	case LFUN_DEBUG_LEVEL_SET:


More information about the lyx-cvs mailing list