2015-05-08 16:02:36 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: PAINT for ReactOS
|
|
|
|
* LICENSE: LGPL
|
2015-09-09 13:13:35 +00:00
|
|
|
* FILE: base/applications/mspaint/history.cpp
|
2015-05-08 16:02:36 +00:00
|
|
|
* PURPOSE: Undo and redo functionality
|
|
|
|
* PROGRAMMERS: Benedikt Freisen
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *********************************************************/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
|
|
|
/* FUNCTIONS ********************************************************/
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::NotifyDimensionsChanged()
|
|
|
|
{
|
|
|
|
imageArea.SendMessage(WM_IMAGEMODELDIMENSIONSCHANGED);
|
|
|
|
}
|
2015-05-08 16:02:36 +00:00
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::NotifyImageChanged()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
2015-07-07 11:56:37 +00:00
|
|
|
imageArea.SendMessage(WM_IMAGEMODELIMAGECHANGED);
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 07:48:32 +00:00
|
|
|
ImageModel::ImageModel()
|
|
|
|
{
|
|
|
|
currInd = 0;
|
|
|
|
undoSteps = 0;
|
|
|
|
redoSteps = 0;
|
|
|
|
imageSaved = TRUE;
|
2015-07-23 13:11:54 +00:00
|
|
|
|
2015-08-16 19:52:37 +00:00
|
|
|
// prepare a minimal usable bitmap
|
|
|
|
int imgXRes = 1;
|
|
|
|
int imgYRes = 1;
|
2015-07-23 13:11:54 +00:00
|
|
|
|
|
|
|
hDrawingDC = CreateCompatibleDC(NULL);
|
|
|
|
SelectObject(hDrawingDC, CreatePen(PS_SOLID, 0, paletteModel.GetFgColor()));
|
|
|
|
SelectObject(hDrawingDC, CreateSolidBrush(paletteModel.GetBgColor()));
|
|
|
|
|
|
|
|
hBms[0] = CreateDIBWithProperties(imgXRes, imgYRes);
|
|
|
|
SelectObject(hDrawingDC, hBms[0]);
|
|
|
|
Rectangle(hDrawingDC, 0 - 1, 0 - 1, imgXRes + 1, imgYRes + 1);
|
2015-07-23 07:48:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::CopyPrevious()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
|
2015-05-08 16:27:46 +00:00
|
|
|
hBms[(currInd + 1) % HISTORYSIZE] = (HBITMAP) CopyImage(hBms[currInd], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
|
2015-05-08 16:02:36 +00:00
|
|
|
currInd = (currInd + 1) % HISTORYSIZE;
|
|
|
|
if (undoSteps < HISTORYSIZE - 1)
|
|
|
|
undoSteps++;
|
|
|
|
redoSteps = 0;
|
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
|
|
|
imageSaved = FALSE;
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::Undo()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
if (undoSteps > 0)
|
|
|
|
{
|
2015-07-07 11:56:37 +00:00
|
|
|
int oldWidth = GetWidth();
|
|
|
|
int oldHeight = GetHeight();
|
2015-07-07 10:42:49 +00:00
|
|
|
selectionWindow.ShowWindow(SW_HIDE);
|
2015-05-08 16:02:36 +00:00
|
|
|
currInd = (currInd + HISTORYSIZE - 1) % HISTORYSIZE;
|
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
|
|
|
undoSteps--;
|
|
|
|
if (redoSteps < HISTORYSIZE - 1)
|
|
|
|
redoSteps++;
|
2015-07-07 11:56:37 +00:00
|
|
|
if (GetWidth() != oldWidth || GetHeight() != oldHeight)
|
|
|
|
NotifyDimensionsChanged();
|
|
|
|
NotifyImageChanged();
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::Redo()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
if (redoSteps > 0)
|
|
|
|
{
|
2015-07-07 11:56:37 +00:00
|
|
|
int oldWidth = GetWidth();
|
|
|
|
int oldHeight = GetHeight();
|
2015-07-07 10:42:49 +00:00
|
|
|
selectionWindow.ShowWindow(SW_HIDE);
|
2015-05-08 16:02:36 +00:00
|
|
|
currInd = (currInd + 1) % HISTORYSIZE;
|
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
|
|
|
redoSteps--;
|
|
|
|
if (undoSteps < HISTORYSIZE - 1)
|
|
|
|
undoSteps++;
|
2015-07-07 11:56:37 +00:00
|
|
|
if (GetWidth() != oldWidth || GetHeight() != oldHeight)
|
|
|
|
NotifyDimensionsChanged();
|
|
|
|
NotifyImageChanged();
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::ResetToPrevious()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
DeleteObject(hBms[currInd]);
|
|
|
|
hBms[currInd] =
|
2015-05-08 16:27:46 +00:00
|
|
|
(HBITMAP) CopyImage(hBms[(currInd + HISTORYSIZE - 1) % HISTORYSIZE], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
|
2015-05-08 16:02:36 +00:00
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
2015-07-07 11:56:37 +00:00
|
|
|
NotifyImageChanged();
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::ClearHistory()
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
undoSteps = 0;
|
|
|
|
redoSteps = 0;
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::Insert(HBITMAP hbm)
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
2015-07-07 11:56:37 +00:00
|
|
|
int oldWidth = GetWidth();
|
|
|
|
int oldHeight = GetHeight();
|
2015-05-08 16:02:36 +00:00
|
|
|
DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
|
|
|
|
hBms[(currInd + 1) % HISTORYSIZE] = hbm;
|
|
|
|
currInd = (currInd + 1) % HISTORYSIZE;
|
|
|
|
if (undoSteps < HISTORYSIZE - 1)
|
|
|
|
undoSteps++;
|
|
|
|
redoSteps = 0;
|
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
2015-07-07 11:56:37 +00:00
|
|
|
if (GetWidth() != oldWidth || GetHeight() != oldHeight)
|
|
|
|
NotifyDimensionsChanged();
|
|
|
|
NotifyImageChanged();
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
void ImageModel::Crop(int nWidth, int nHeight, int nOffsetX, int nOffsetY)
|
2015-05-08 16:02:36 +00:00
|
|
|
{
|
|
|
|
HDC hdc;
|
|
|
|
HPEN oldPen;
|
|
|
|
HBRUSH oldBrush;
|
2015-07-07 11:56:37 +00:00
|
|
|
int oldWidth = GetWidth();
|
|
|
|
int oldHeight = GetHeight();
|
2015-05-08 16:02:36 +00:00
|
|
|
|
2017-06-07 10:00:04 +00:00
|
|
|
if (nWidth <= 0)
|
|
|
|
nWidth = 1;
|
|
|
|
if (nHeight <= 0)
|
|
|
|
nHeight = 1;
|
|
|
|
|
2015-05-08 16:02:36 +00:00
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
|
|
|
DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
|
2015-07-07 11:56:37 +00:00
|
|
|
hBms[(currInd + 1) % HISTORYSIZE] = CreateDIBWithProperties(nWidth, nHeight);
|
2015-05-08 16:02:36 +00:00
|
|
|
currInd = (currInd + 1) % HISTORYSIZE;
|
|
|
|
if (undoSteps < HISTORYSIZE - 1)
|
|
|
|
undoSteps++;
|
|
|
|
redoSteps = 0;
|
|
|
|
|
|
|
|
hdc = CreateCompatibleDC(hDrawingDC);
|
|
|
|
SelectObject(hdc, hBms[currInd]);
|
|
|
|
|
2015-07-07 11:15:24 +00:00
|
|
|
oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, paletteModel.GetBgColor()));
|
|
|
|
oldBrush = (HBRUSH) SelectObject(hdc, CreateSolidBrush(paletteModel.GetBgColor()));
|
2015-07-07 11:56:37 +00:00
|
|
|
Rectangle(hdc, 0, 0, nWidth, nHeight);
|
|
|
|
BitBlt(hdc, -nOffsetX, -nOffsetY, GetWidth(), GetHeight(), hDrawingDC, 0, 0, SRCCOPY);
|
2015-05-08 16:02:36 +00:00
|
|
|
DeleteObject(SelectObject(hdc, oldBrush));
|
|
|
|
DeleteObject(SelectObject(hdc, oldPen));
|
|
|
|
DeleteDC(hdc);
|
|
|
|
SelectObject(hDrawingDC, hBms[currInd]);
|
|
|
|
|
2015-07-07 11:56:37 +00:00
|
|
|
if (GetWidth() != oldWidth || GetHeight() != oldHeight)
|
|
|
|
NotifyDimensionsChanged();
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::SaveImage(LPTSTR lpFileName)
|
|
|
|
{
|
|
|
|
SaveDIBToFile(hBms[currInd], lpFileName, hDrawingDC, &fileTime, &fileSize, fileHPPM, fileVPPM);
|
|
|
|
imageSaved = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ImageModel::IsImageSaved()
|
|
|
|
{
|
|
|
|
return imageSaved;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ImageModel::HasUndoSteps()
|
|
|
|
{
|
|
|
|
return undoSteps > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ImageModel::HasRedoSteps()
|
|
|
|
{
|
|
|
|
return redoSteps > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::StretchSkew(int nStretchPercentX, int nStretchPercentY, int nSkewDegX, int nSkewDegY)
|
|
|
|
{
|
|
|
|
int oldWidth = GetWidth();
|
|
|
|
int oldHeight = GetHeight();
|
|
|
|
Insert((HBITMAP) CopyImage(hBms[currInd], IMAGE_BITMAP,
|
|
|
|
GetWidth() * nStretchPercentX / 100,
|
|
|
|
GetHeight() * nStretchPercentY / 100, 0));
|
|
|
|
if (GetWidth() != oldWidth || GetHeight() != oldHeight)
|
|
|
|
NotifyDimensionsChanged();
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ImageModel::GetWidth()
|
|
|
|
{
|
|
|
|
return GetDIBWidth(hBms[currInd]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ImageModel::GetHeight()
|
|
|
|
{
|
|
|
|
return GetDIBHeight(hBms[currInd]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::InvertColors()
|
|
|
|
{
|
|
|
|
RECT rect = {0, 0, GetWidth(), GetHeight()};
|
|
|
|
CopyPrevious();
|
|
|
|
InvertRect(hDrawingDC, &rect);
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::Clear(COLORREF color)
|
|
|
|
{
|
|
|
|
Rectangle(hDrawingDC, 0 - 1, 0 - 1, GetWidth() + 1, GetHeight() + 1);
|
|
|
|
NotifyImageChanged();
|
2015-05-08 16:02:36 +00:00
|
|
|
}
|
2015-07-23 13:52:02 +00:00
|
|
|
|
|
|
|
HDC ImageModel::GetDC()
|
|
|
|
{
|
|
|
|
return hDrawingDC;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::FlipHorizontally()
|
|
|
|
{
|
|
|
|
CopyPrevious();
|
|
|
|
StretchBlt(hDrawingDC, GetWidth() - 1, 0, -GetWidth(), GetHeight(), GetDC(), 0, 0,
|
|
|
|
GetWidth(), GetHeight(), SRCCOPY);
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::FlipVertically()
|
|
|
|
{
|
|
|
|
CopyPrevious();
|
|
|
|
StretchBlt(hDrawingDC, 0, GetHeight() - 1, GetWidth(), -GetHeight(), GetDC(), 0, 0,
|
|
|
|
GetWidth(), GetHeight(), SRCCOPY);
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageModel::RotateNTimes90Degrees(int iN)
|
|
|
|
{
|
|
|
|
if (iN == 2)
|
|
|
|
{
|
|
|
|
CopyPrevious();
|
|
|
|
StretchBlt(hDrawingDC, GetWidth() - 1, GetHeight() - 1, -GetWidth(), -GetHeight(), GetDC(),
|
|
|
|
0, 0, GetWidth(), GetHeight(), SRCCOPY);
|
|
|
|
}
|
|
|
|
NotifyImageChanged();
|
|
|
|
}
|