I have a logical order problem.
Lets say I have three points of significance. Left side of edit field, right side of edit field, caret position in edit field.
I want to show the control that has focus, and show it’s caret.
To show a field that has focus, I generally try to ensure the following in this order.
1. Ensure right side visible.
2. Ensure left side visible.
3. Ensure caret (plus max width character) visible.
Problem is if the field is too wide for the parent. The control will always attempt to show the right, then left, then caret. If the caret is near the end (and the current scroll ensures the right side is visible), and the user types, the parent will scroll to right after the caret even if it was already visible.
This actually scrolls the parent to the left if the caret < right side.
I don’t want to do that.
Pseudo code:
if (cFocusRect.right > cClientRect.right)
{
cFinalPoint.x += cClientRect.right - cFocusRect.right;
cFocusRect.OffsetRect(cClientRect.right - cFocusRect.right, 0);
fBoundRight = TRUE;
}
if (cFocusRect.left < 0)
{
cFinalPoint.x -= cFocusRect.left;
cFocusRect.OffsetRect(cFocusRect.left, 0);
}
nCaretRight = min(cFocusRect.right, pChild->GetCaretPos().x + nMaxCharWidth);
if (nCaretRight > cClientRect.right)
{
cFinalPoint.x += cClientRect.right - nCaretRight;
cFocusRect.OffsetRect(cClientRect.right - nCaretRight, 0);
fBoundRight = TRUE;
}
Here’s the solution. Don’t scroll the Field’s rect into view if you need to scroll the caret into view. To decide, check if the field is bigger than the scrollable area.