// Undo without scroll
// version 0.1.1 - 2009-05-03

// Based on:
// - UndoAll.js        http://akelpad.sourceforge.net/forum/viewtopic.php?p=4050#4050
// - SelAllNoScroll.js http://akelpad.sourceforge.net/forum/viewtopic.php?p=4168#4168

//var AkelPad = new ActiveXObject("AkelPad.document");

var hMainWnd = AkelPad.GetMainWnd();
var hWndEdit = AkelPad.GetEditWnd();
var bAkelEdit = AkelPad.IsAkelEdit();
var oSys = AkelPad.SystemFunction();

if(hMainWnd
	&& bAkelEdit
	&& AkelPad.SendMessage(hWndEdit, 3075 /*AEM_CANUNDO*/, 0, 0)
	// && AkelPad.SendMessage(hWndEdit, 3086 /*AEM_GETMODIFY*/, 0, 0)
) {
	var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/, true);
	if(lpPoint) {
		toggleRedraw(hWndEdit, false);
		AkelPad.SendMessage(hWndEdit, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
		AkelPad.SendMessage(hWndEdit, 3077 /*AEM_UNDO*/, 0, 0);
		AkelPad.SendMessage(hWndEdit, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
		toggleRedraw(hWndEdit, true);
		AkelPad.MemFree(lpPoint, true);
	}
}

function toggleRedraw(hWnd, bRedraw) {
	AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
	if(!bRedraw)
		return;
	oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}