/*
	OnEditCopy()
*/
void CWallBrowserStretchView::OnEditCopy(void)
{
	CWaitCursor cursor;
	BOOL bCopied = FALSE;

	// copia il contenuto dell'immagine nella clipboard
	if(::OpenClipboard(NULL))
	{
		if(::EmptyClipboard())
		{
			CWallBrowserDoc* pDoc = (CWallBrowserDoc*)GetDocument();
			if(pDoc)
			{
				CImage *pImage = pDoc->GetImage();
				if(pImage && pImage->GetWidth() > 0 && pImage->GetHeight() > 0)
					bCopied = ::SetClipboardData(CF_DIB,pImage->GetDIB())!=(HANDLE)NULL;
			}
		}

		::CloseClipboard();
	}

	// imposta il redo con l'ultima operazione effettuata
	if(bCopied)
		RedoPush(ID_EDIT_COPY);
}
/*
	UndoPop()
*/
BOOL CWallBrowserStretchView::UndoPop(void)
{
	BOOL bUndo = FALSE;

	if(--m_nUndoLevel >= 0)
	{
		CWallBrowserDoc* pDoc = (CWallBrowserDoc*)GetDocument();
		if(pDoc)
		{
			CImage *pImage = pDoc->GetImage();
			if(pImage && pImage->GetWidth() > 0 && pImage->GetHeight() > 0)
			{
				int index;
				CImage* pImg;

				UNDOINFO* undoinfo = m_UndoInfo.Pop(index);
				if(index >= 0 && undoinfo)
				{
					m_nRedo = undoinfo->nRedo;

					CImageFactory* pImgFactory = undoinfo->pImageFactory;
					pImg = pImgFactory->GetImage();
					HDIB hDib = pImg->GetDIB(&(undoinfo->DibInfo));
					
					if(hDib)
					{
						pImage->SetDIB(hDib,&(undoinfo->DibInfo));
						::GlobalFree(hDib);
						delete (CImageFactory*)undoinfo->pImageFactory;
						delete undoinfo;
						bUndo = TRUE;
					}
					else
					{
						delete (CImageFactory*)undoinfo->pImageFactory;
						delete undoinfo;
					}
				}
				else
					::MessageBeep(MB_ICONEXCLAMATION);
			}
		}
	}
	else
		m_nUndoLevel++;

	CWnd* pWnd = AfxGetMainWnd();
	if(pWnd)
	{
		pWnd->SendMessage(WM_EDIT_UNDO,UndoEnabled(),0L);
		pWnd->SendMessage(WM_EDIT_REDO,RedoEnabled(),0L);
	}

	return(bUndo);
}
/*
	UndoPush()
*/
BOOL CWallBrowserStretchView::UndoPush(int nID)
{
	BOOL bUndo = FALSE;

	if(++m_nUndoLevel <= MAX_STACK_SIZE)
	{
		// salva il contenuto dell'immagine nello stack per l'undo
		CWallBrowserDoc* pDoc = (CWallBrowserDoc*)GetDocument();
		if(pDoc)
		{
			CImage *pImage = pDoc->GetImage();
			if(pImage && pImage->GetWidth() > 0 && pImage->GetHeight() > 0)
			{
				CImage* pImg;
				CImageFactory* pImgFactory = new CImageFactory();
				char szLibraryName[_MAX_PATH+1];
				strcpy(szLibraryName,pImage->GetLibraryName());
				pImg = pImgFactory->Create(szLibraryName,sizeof(szLibraryName));
				DIBINFO DibInfo;
				DibInfo.nPerspective = 0;

				HDIB hDib = pImage->GetDIB(&DibInfo);
				if(hDib)
				{
					pImg->SetDIB(hDib,&DibInfo);
					::GlobalFree(hDib);
					
					UNDOINFO* undoinfo = new UNDOINFO;
					undoinfo->pImageFactory = pImgFactory;
					undoinfo->DibInfo.nPerspective = DibInfo.nPerspective;
					undoinfo->nRedo = nID;
					int nLevel = m_UndoInfo.Push(undoinfo);
					if(nLevel!=-1)
						bUndo = TRUE;
				}
				else
				{
					delete pImgFactory;
				}
			}
		}
	}
	else
		m_nUndoLevel--;

	CWnd* pWnd = AfxGetMainWnd();
	if(pWnd)
	{
		pWnd->SendMessage(WM_EDIT_UNDO,UndoEnabled(),0L);
		pWnd->SendMessage(WM_EDIT_REDO,FALSE,0L);
	}

	return(bUndo);
}