Index: trunk/Assembly_Library/Src/Menu/MenuScrollbars.asm
===================================================================
--- trunk/Assembly_Library/Src/Menu/MenuScrollbars.asm	(revision 133)
+++ trunk/Assembly_Library/Src/Menu/MenuScrollbars.asm	(revision 181)
@@ -37,15 +37,19 @@
 MenuScrollbars_GetScrollCharacterToALForLineInDI:
 	call	MenuScrollbars_GetMaxVisibleItemsOnPageToCX
-	call	.GetFirstThumbLineToAX
+	; Get first thumb line to AX
+	mov		ax, [bp+MENU.wFirstVisibleItem]
+	call	.CalculateFirstOrLastThumbLineToAX
+
 	cmp		di, ax				; Before first thumb line?
 	jb		SHORT .ReturnTrackCharacter
 	call	.GetLastThumbLineToAX
-	cmp		di, ax				; After last thumb line?
-	ja		SHORT .ReturnTrackCharacter
-	mov		al, SCROLL_THUMB_CHARACTER
-	ret
+	cmp		ax, di				; After last thumb line?
 ALIGN JUMP_ALIGN
 .ReturnTrackCharacter:
 	mov		al, SCROLL_TRACK_CHARACTER
+	jb		SHORT .Return
+	mov		al, SCROLL_THUMB_CHARACTER
+ALIGN JUMP_ALIGN, ret
+.Return:
 	ret
 
@@ -63,9 +67,10 @@
 .GetLastThumbLineToAX:
 	call	MenuScrollbars_GetLastVisibleItemOnPageToAX
-	jmp		SHORT .CalculateFirstOrLastThumbLineToAX
-
-;--------------------------------------------------------------------
-; .GetFirstThumbLineToAX
-;	Parameters
+	; Fall to .CalculateFirstOrLastThumbLineToAX
+
+;--------------------------------------------------------------------
+; .CalculateFirstOrLastThumbLineToAX
+;	Parameters
+;		AX:		Index of first or last visible item on page
 ;		CX:		Max visible items on page
 ;		SS:BP:	Ptr to MENU
@@ -76,6 +81,4 @@
 ;--------------------------------------------------------------------
 ALIGN JUMP_ALIGN
-.GetFirstThumbLineToAX:
-	mov		ax, [bp+MENU.wFirstVisibleItem]
 .CalculateFirstOrLastThumbLineToAX:
 	mul		cx
@@ -98,5 +101,26 @@
 	mov		cx, [bp+MENUINIT.wHighlightedItem]
 	add		cx, ax
-	call	.RotateItemInCX
+	; Fall to .RotateItemInCX
+
+;--------------------------------------------------------------------
+; .RotateItemInCX
+;	Parameters
+;		CX:		Possibly under of overflown item to be rotated
+;		SS:BP:	Ptr to MENU
+;	Returns:
+;		CX:		Valid item index
+;	Corrupts registers:
+;		DX
+;--------------------------------------------------------------------
+;.RotateItemInCX:
+	mov		dx, [bp+MENUINIT.wItems]
+	test	cx, cx
+	js		SHORT .RotateNegativeItemInCX
+	sub		cx, dx
+	jae		SHORT .ScrollPageForNewItemInCX
+
+ALIGN JUMP_ALIGN
+.RotateNegativeItemInCX:
+	add		cx, dx
 	; Fall to .ScrollPageForNewItemInCX
 
@@ -111,4 +135,5 @@
 ;		AX, BX, CX, DX, SI, DI
 ;--------------------------------------------------------------------
+ALIGN JUMP_ALIGN
 .ScrollPageForNewItemInCX:
 	call	MenuScrollbars_IsItemInCXonVisiblePage
@@ -117,8 +142,24 @@
 	mov		dx, [bp+MENU.wFirstVisibleItem]
 	sub		dx, [bp+MENUINIT.wHighlightedItem]
+
+	; Get MaxFirstVisibleItem to AX
+	push	cx
+	call	MenuScrollbars_GetMaxVisibleItemsOnPageToCX
+	mov		ax, [bp+MENUINIT.wItems]
+	sub		ax, cx
+	pop		cx
+
 	add		dx, cx
-	MAX_S	dx, 0
-	call	.GetMaxFirstVisibleItemToAX
-	MIN_U	ax, dx
+	jns		.DXisPositive
+	cwd		; This won't work if MaxFirstVisibleItem > 32767
+
+ALIGN JUMP_ALIGN
+.DXisPositive:
+	cmp		ax, dx
+	jb		.AXisLessThanDX
+	xchg	dx, ax
+
+ALIGN JUMP_ALIGN
+.AXisLessThanDX:
 	mov		[bp+MENU.wFirstVisibleItem], ax
 	call	MenuText_RefreshAllItems
@@ -127,55 +168,4 @@
 .HighlightNewItemOnCX:
 	jmp		MenuEvent_HighlightItemFromCX
-
-;--------------------------------------------------------------------
-; .GetMaxFirstVisibleItemToAX
-;	Parameters
-;		SS:BP:	Ptr to MENU
-;	Returns:
-;		AX:		Max first visible item
-;	Corrupts registers:
-;		Nothing
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-.GetMaxFirstVisibleItemToAX:
-	push	cx
-
-	call	MenuScrollbars_GetMaxVisibleItemsOnPageToCX
-	mov		ax, [bp+MENUINIT.wItems]
-	sub		ax, cx
-
-	pop		cx
-	ret
-
-;--------------------------------------------------------------------
-; .RotateItemInCX
-;	Parameters
-;		CX:		Possibly under of overflown item to be rotated
-;		SS:BP:	Ptr to MENU
-;	Returns:
-;		CX:		Valid item index
-;	Corrupts registers:
-;		DX
-;--------------------------------------------------------------------
-ALIGN JUMP_ALIGN
-.RotateItemInCX:
-	mov		dx, [bp+MENUINIT.wItems]
-	test	cx, cx
-	js		SHORT .RotateNegativeItemInCX
-	cmp		cx, dx
-	jae		SHORT .RotatePositiveItemInCX
-	ret
-
-ALIGN JUMP_ALIGN
-.RotatePositiveItemInCX:
-	sub		cx, dx
-	;jae	SHORT .RotatePositiveItemInCX	; Not needed by scrolling
-	ret
-
-ALIGN JUMP_ALIGN
-.RotateNegativeItemInCX:
-	add		cx, dx
-	;js		SHORT .RotateNegativeItemInCX	; Not needed by scrolling
-	ret
 
 
@@ -216,12 +206,9 @@
 ALIGN JUMP_ALIGN
 MenuScrollbars_GetLastVisibleItemOnPageToAX:
-	push	cx
-
+	xchg	cx, ax
 	call	MenuScrollbars_GetActualVisibleItemsOnPageToCX
 	xchg	ax, cx
 	dec		ax
 	add		ax, [bp+MENU.wFirstVisibleItem]
-
-	pop		cx
 	ret
 
@@ -239,5 +226,9 @@
 MenuScrollbars_GetActualVisibleItemsOnPageToCX:
 	call	MenuScrollbars_GetMaxVisibleItemsOnPageToCX
-	MIN_U	cx, [bp+MENUINIT.wItems]
+	cmp		cx, [bp+MENUINIT.wItems]
+	jb		SHORT .Return
+	mov		cx, [bp+MENUINIT.wItems]
+ALIGN JUMP_ALIGN, ret
+.Return:
 	ret
 
