I've removed all inset-dependent fitCursor functions,
and put toghether all fitcursor-related functions in screen.C in only one,
putting the smart tall row handling there (with some code simplification
and nice variable names).
Now the same routine is used for mathed and normal cursors, and this is
achieved by adding a LCursor::getDim(int & asc, int & desc), that handles
by itself cursors in LyXTexts and eventually asks the inset (in fact, only
for math insets).
All the cmputations in fitCursor are done with this dimension.
Comments (and/or testing vs. 1.3.x) greatly appreciated.
Regards, Alfredo
Index: BufferView.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.C,v
retrieving revision 1.208
diff -u -p -u -r1.208 BufferView.C
--- BufferView.C 10 Nov 2003 13:23:08 -0000 1.208
+++ BufferView.C 11 Nov 2003 14:55:28 -0000
@@ -369,25 +369,6 @@ void BufferView::replaceWord(string cons
}
-bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
-{
- lyxerr << "BufferView::fitLockedInsetCursor x: " << x
- << " y: " << y << std::endl;
- UpdatableInset * tli =
- static_cast<UpdatableInset *>(cursor().innerInset());
- if (tli && available()) {
- lyxerr << " text->cursor.y: " << text->cursor.y() << std::endl;
- lyxerr << " insetInInsetY: " << tli->insetInInsetY() << std::endl;
- y += text->cursor.y() + tli->insetInInsetY();
- if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
- updateScrollbar();
- return true;
- }
- }
- return false;
-}
-
-
void BufferView::hideCursor()
{
screen().hideCursor();
Index: BufferView_pimpl.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v
retrieving revision 1.460
diff -u -p -u -r1.460 BufferView_pimpl.C
--- BufferView_pimpl.C 11 Nov 2003 13:17:26 -0000 1.460
+++ BufferView_pimpl.C 11 Nov 2003 14:55:29 -0000
@@ -313,7 +313,7 @@ void BufferView::Pimpl::buffer(Buffer *
resizeCurrentBuffer();
// FIXME: needed when ?
- top_y(screen().topCursorVisible(bv_->text));
+ fitCursor();
// Buffer-dependent dialogs should be updated or
// hidden. This should go here because some dialogs (eg ToC)
@@ -343,42 +343,11 @@ void BufferView::Pimpl::buffer(Buffer *
bool BufferView::Pimpl::fitCursor()
{
lyxerr << "BufferView::Pimpl::fitCursor." << endl;
-
- int x,y;
- bv_->cursor().getPos(x, y);
-
- if (y < top_y() || y > top_y() + workarea().workHeight()) {
- int newtop = y - workarea().workHeight() / 2;
- newtop = std::max(0, newtop);
- top_y(newtop);
+ if (screen().fitCursor(bv_)) {
updateScrollbar();
return true;
}
return false;
-
-// dead code below
- bool ret;
-#if 0
- UpdatableInset * tli =
- static_cast<UpdatableInset *>(cursor_.innerInset());
- if (tli) {
- tli->fitInsetCursor(bv_);
- ret = true;
- } else {
- ret = screen().fitCursor(bv_->text, bv_);
- }
-#endif
-#if 0
- ret = screen().fitCursor(bv_->text, bv_);
-#endif
-
- //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
-
- // We need to always update, in case we did a
- // paste and we stayed anchored to a row, but
- // the actual height of the doc changed ...
- updateScrollbar();
- return ret;
}
@@ -446,7 +415,7 @@ void BufferView::Pimpl::resizeCurrentBuf
}
}
- top_y(screen().topCursorVisible(bv_->text));
+ fitCursor();
switchKeyMap();
owner_->busy(false);
Index: cursor.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.C,v
retrieving revision 1.19
diff -u -p -u -r1.19 cursor.C
--- cursor.C 11 Nov 2003 13:17:27 -0000 1.19
+++ cursor.C 11 Nov 2003 14:55:30 -0000
@@ -20,6 +20,7 @@
#include "lfuns.h"
#include "lyxtext.h"
#include "paragraph.h"
+#include "lyxrow.h"
#include "insets/updatableinset.h"
#include "insets/insettabular.h"
@@ -161,6 +162,19 @@ void LCursor::updatePos()
{
if (!data_.empty())
cached_y_ = bv_->top_y() + innerInset()->y();
+}
+
+
+void LCursor::getDim(int & asc, int & desc) const
+{
+ LyXText * txt = innerText();
+
+ if (txt) {
+ Row const & row = *txt->cursorRow();
+ asc = row.baseline();
+ desc = row.height() - asc;
+ } else
+ innerInset()->getCursorDim(bv_, asc, desc);
}
Index: cursor.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/cursor.h,v
retrieving revision 1.9
diff -u -p -u -r1.9 cursor.h
--- cursor.h 11 Nov 2003 13:17:27 -0000 1.9
+++ cursor.h 11 Nov 2003 14:55:30 -0000
@@ -75,6 +75,8 @@ public:
LyXText * innerText() const;
/// returns x,y position
void getPos(int & x, int & y) const;
+ /// returns cursor dimension
+ void getDim(int & asc, int & desc) const;
/// cache the absolute coordinate from the top inset
void updatePos();
///
Index: frontends/screen.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/screen.C,v
retrieving revision 1.80
diff -u -p -u -r1.80 screen.C
--- frontends/screen.C 11 Nov 2003 09:06:40 -0000 1.80
+++ frontends/screen.C 11 Nov 2003 14:55:31 -0000
@@ -204,70 +204,39 @@ void LyXScreen::toggleCursor(BufferView
}
-bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
- int x, int y, int asc, int desc)
+bool LyXScreen::fitCursor(BufferView * bv)
{
- lyxerr << "LyXScreen::fitManualCursor x: " << x << " y: " << y << std::endl;
- int const vheight = workarea().workHeight();
- int const topy = bv->top_y();
- int newtop = topy;
-
- if (y + desc - topy >= vheight)
- newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
- else if (y - asc < topy && topy > 0)
- newtop = y - vheight / 4;
-
- newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
-
- if (newtop == topy)
- return false;
-
- bv->top_y(newtop);
- return true;
-}
-
-
-unsigned int LyXScreen::topCursorVisible(LyXText * text)
-{
- LyXCursor const & cursor = text->cursor;
- int top_y = text->bv()->top_y();
+ int const top_y = bv->top_y();
+ int const h = workarea().workHeight();
int newtop = top_y;
- unsigned int const vheight = workarea().workHeight();
-
- Row & row = *text->cursorPar()->getRow(cursor.pos());
-
- if (int(cursor.y() - row.baseline() + row.height() - top_y) >= vheight) {
- if (row.height() < vheight
- && row.height() > vheight / 4) {
- newtop = cursor.y()
- + row.height()
- - row.baseline() - vheight;
- } else {
- // scroll down, the scroll region must be so big!!
- newtop = cursor.y() - vheight / 2;
- }
+ int x, y, asc, desc;
- } else if (int(cursor.y() - row.baseline()) < top_y && top_y > 0) {
- if (row.height() < vheight && row.height() > vheight / 4) {
- newtop = cursor.y() - row.baseline();
- } else {
- // scroll up
- newtop = cursor.y() - vheight / 2;
+ bv->cursor().getPos(x, y);
+ bv->cursor().getDim(asc, desc);
+
+ bool const big_row = h / 4 < asc + desc && asc + desc < h;
+
+ if (y + desc - top_y >= h) {
+ if (big_row)
+ newtop = y + desc - h;
+ else
+ newtop = y - h / 2;
+
+ } else if (top_y > max(y - asc, 0)) {
+ if (big_row)
+ newtop = y - asc;
+ else {
+ newtop = y - h / 2;
newtop = min(newtop, top_y);
}
}
- return max(newtop, 0);
-}
-
+ newtop = max(newtop, 0);
+ if (newtop == top_y)
+ return false;
-bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
-{
- // Is a change necessary?
- int const newtop = topCursorVisible(text);
- bool const result = (newtop != bv->top_y());
bv->top_y(newtop);
- return result;
+ return true;
}
Index: frontends/screen.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/screen.h,v
retrieving revision 1.27
diff -u -p -u -r1.27 screen.h
--- frontends/screen.h 9 Sep 2003 18:27:21 -0000 1.27
+++ frontends/screen.h 11 Nov 2003 14:55:31 -0000
@@ -36,42 +36,17 @@ public:
virtual ~LyXScreen();
- /**
- * fit the cursor onto the visible work area, scrolling if necessary
- * @param bv the buffer view
- * @param vheight the height of the visible region
- * @param base_y the top of the lyxtext to look at
- * @param x the new x position
- * @param y the new y position
- * @param a ascent of the cursor's row
- * @param d descent of the cursor's row
- * @return true if the work area needs scrolling as a result
- */
- bool fitManualCursor(BufferView * bv, LyXText * text,
- int x, int y, int a, int d);
-
/// redraw the screen, without using existing pixmap
virtual void redraw(BufferView & bv);
/**
- * topCursorVisible - get a new "top" to make the cursor visible
- * in a LyXText
- *
- * This helper function calculates a new y co-ordinate for
- * the top of the containing region such that the cursor contained
- * within the LyXText is "nicely" visible.
- */
- virtual unsigned int topCursorVisible(LyXText *);
-
- /**
* fitCursor - fit the cursor onto the work area
- * @param text the text containing the cursor
* @param bv the bufferview
* @return true if a change was necessary
*
* Scrolls the screen so that the cursor is visible
*/
- virtual bool fitCursor(LyXText *, BufferView *);
+ virtual bool fitCursor(BufferView *);
/// hide the visible cursor, if it is visible
void hideCursor();
Index: insets/insetcollapsable.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetcollapsable.C,v
retrieving revision 1.206
diff -u -p -u -r1.206 insetcollapsable.C
--- insets/insetcollapsable.C 11 Nov 2003 13:17:28 -0000 1.206
+++ insets/insetcollapsable.C 11 Nov 2003 14:55:34 -0000
@@ -433,12 +433,6 @@ bool InsetCollapsable::insetAllowed(Inse
}
-void InsetCollapsable::fitInsetCursor(BufferView * bv) const
-{
- inset.fitInsetCursor(bv);
-}
-
-
void InsetCollapsable::setLabelFont(LyXFont & f)
{
labelfont_ = f;
Index: insets/insetcollapsable.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetcollapsable.h,v
retrieving revision 1.146
diff -u -p -u -r1.146 insetcollapsable.h
--- insets/insetcollapsable.h 10 Nov 2003 18:12:59 -0000 1.146
+++ insets/insetcollapsable.h 11 Nov 2003 14:55:34 -0000
@@ -79,8 +79,6 @@ public:
/// get the screen x,y of the cursor
void getCursorPos(BufferView *, int & x, int & y) const;
///
- void fitInsetCursor(BufferView * bv) const;
- ///
void setFont(BufferView *, LyXFont const &, bool toggleall = false,
bool selectall = false);
///
Index: insets/insettabular.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insettabular.C,v
retrieving revision 1.374
diff -u -p -u -r1.374 insettabular.C
--- insets/insettabular.C 10 Nov 2003 18:12:59 -0000 1.374
+++ insets/insettabular.C 11 Nov 2003 14:55:37 -0000
@@ -973,22 +973,6 @@ void InsetTabular::getCursorPos(BufferVi
}
-void InsetTabular::fitInsetCursor(BufferView * bv) const
-{
- if (the_locking_inset) {
- the_locking_inset->fitInsetCursor(bv);
- return;
- }
-
- LyXFont font;
- int const asc = font_metrics::maxAscent(font);
- int const desc = font_metrics::maxDescent(font);
- resetPos(bv);
-
- bv->fitLockedInsetCursor(cursorx_, cursory_, asc, desc);
-}
-
-
void InsetTabular::setPos(BufferView * bv, int x, int y) const
{
cursory_ = 0;
Index: insets/insettabular.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insettabular.h,v
retrieving revision 1.165
diff -u -p -u -r1.165 insettabular.h
--- insets/insettabular.h 10 Nov 2003 18:13:00 -0000 1.165
+++ insets/insettabular.h 11 Nov 2003 14:55:37 -0000
@@ -196,8 +196,6 @@ private:
void drawCellSelection(Painter &, int x, int baseline,
int row, int column, int cell) const;
///
- void fitInsetCursor(BufferView *) const;
- ///
void setPos(BufferView *, int x, int y) const;
///
DispatchResult moveRight(BufferView *, bool lock);
Index: insets/insettext.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insettext.C,v
retrieving revision 1.539
diff -u -p -u -r1.539 insettext.C
--- insets/insettext.C 11 Nov 2003 13:17:28 -0000 1.539
+++ insets/insettext.C 11 Nov 2003 14:55:38 -0000
@@ -699,15 +699,6 @@ int InsetText::insetInInsetY() const
}
-void InsetText::fitInsetCursor(BufferView * bv) const
-{
- LyXFont const font = text_.getFont(cpar(), cpos());
- int const asc = font_metrics::maxAscent(font);
- int const desc = font_metrics::maxDescent(font);
- bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
-}
-
-
DispatchResult InsetText::moveRight(BufferView * bv)
{
if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
Index: insets/insettext.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insettext.h,v
retrieving revision 1.227
diff -u -p -u -r1.227 insettext.h
--- insets/insettext.h 10 Nov 2003 18:13:00 -0000 1.227
+++ insets/insettext.h 11 Nov 2003 14:55:39 -0000
@@ -93,8 +93,6 @@ public:
///
int insetInInsetY() const;
///
- void fitInsetCursor(BufferView *) const;
- ///
bool insertInset(BufferView *, InsetOld *);
///
bool insetAllowed(InsetOld::Code) const;
Index: insets/updatableinset.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/updatableinset.C,v
retrieving revision 1.37
diff -u -p -u -r1.37 updatableinset.C
--- insets/updatableinset.C 10 Nov 2003 15:38:20 -0000 1.37
+++ insets/updatableinset.C 11 Nov 2003 14:55:39 -0000
@@ -23,6 +23,8 @@
#include "support/lstrings.h"
+#include <boost/assert.hpp>
+
using lyx::support::strToDbl;
using lyx::support::strToInt;
@@ -35,10 +37,6 @@ InsetOld::EDITABLE UpdatableInset::edita
}
-void UpdatableInset::fitInsetCursor(BufferView *) const
-{}
-
-
void UpdatableInset::scroll(BufferView * bv, float s) const
{
if (!s) {
@@ -106,4 +104,10 @@ UpdatableInset::priv_dispatch(FuncReques
default:
return DispatchResult(false);
}
+}
+
+
+void UpdatableInset::getCursorDim(BufferView *, int &, int &) const
+{
+ BOOST_ASSERT(false);
}
Index: insets/updatableinset.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/updatableinset.h,v
retrieving revision 1.24
diff -u -p -u -r1.24 updatableinset.h
--- insets/updatableinset.h 10 Nov 2003 09:06:47 -0000 1.24
+++ insets/updatableinset.h 11 Nov 2003 14:55:39 -0000
@@ -29,10 +29,10 @@ public:
///
virtual EDITABLE editable() const;
- ///
- virtual void fitInsetCursor(BufferView *) const;
- /// FIXME
+ /// return the cursor pos, relative to the inset pos
virtual void getCursorPos(BufferView *, int &, int &) const {}
+ /// return the cursor dim
+ virtual void getCursorDim(BufferView *, int &, int &) const;
///
virtual bool insertInset(BufferView *, InsetOld *) { return false; }
///
Index: mathed/formulabase.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/formulabase.C,v
retrieving revision 1.310
diff -u -p -u -r1.310 formulabase.C
--- mathed/formulabase.C 10 Nov 2003 15:38:20 -0000 1.310
+++ mathed/formulabase.C 11 Nov 2003 14:55:40 -0000
@@ -186,18 +186,14 @@ void InsetFormulaBase::getCursorPos(Buff
}
-void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
+void InsetFormulaBase::getCursorDim(BufferView * bv,
+ int & asc, int & desc) const
{
if (!mathcursor)
return;
- int x, y, asc, des;
asc = 10;
- des = 2;
+ desc = 2;
//math_font_max_dim(font_, asc, des);
- getCursorPos(bv, x, y);
- //y += yo_;
- //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
- bv->fitLockedInsetCursor(x, y, asc, des);
}
@@ -745,7 +741,6 @@ InsetFormulaBase::priv_dispatch(FuncRequ
toggleInsetSelection(bv);
if (result.dispatched()) {
- fitInsetCursor(bv);
revealCodes(bv);
cmd.view()->stuffClipboard(mathcursor->grabSelection());
} else {
Index: mathed/formulabase.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/formulabase.h,v
retrieving revision 1.85
diff -u -p -u -r1.85 formulabase.h
--- mathed/formulabase.h 10 Nov 2003 15:38:20 -0000 1.85
+++ mathed/formulabase.h 11 Nov 2003 14:55:40 -0000
@@ -44,10 +44,10 @@ public:
virtual InsetOld::Code lyxCode() const;
/// what appears in the minibuffer when opening
virtual std::string const editMessage() const;
- ///
- virtual void fitInsetCursor(BufferView *) const;
/// FIXME
virtual void getCursorPos(BufferView *, int &, int &) const;
+ /// FIXME
+ virtual void getCursorDim(BufferView *, int &, int &) const;
/// get the absolute document x,y of the cursor
virtual void getCursor(BufferView & bv, int & x, int & y) const;
///