ReactOS  0.4.15-dev-5109-g2469ce2
history.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: PAINT for ReactOS
3  * LICENSE: LGPL
4  * FILE: base/applications/mspaint/history.cpp
5  * PURPOSE: Undo and redo functionality
6  * PROGRAMMERS: Benedikt Freisen
7  */
8 
9 /* INCLUDES *********************************************************/
10 
11 #include "precomp.h"
12 
13 /* FUNCTIONS ********************************************************/
14 
16 {
17  if (imageArea.IsWindow())
19 }
20 
22 {
23  if (imageArea.IsWindow())
25 }
26 
28 {
29  currInd = 0;
30  undoSteps = 0;
31  redoSteps = 0;
32  imageSaved = TRUE;
33 
34  // prepare a minimal usable bitmap
35  int imgXRes = 1;
36  int imgYRes = 1;
37 
41 
42  hBms[0] = CreateDIBWithProperties(imgXRes, imgYRes);
44  Rectangle(hDrawingDC, 0 - 1, 0 - 1, imgXRes + 1, imgYRes + 1);
45 }
46 
48 {
49  ATLTRACE("%s: %d\n", __FUNCTION__, currInd);
52  currInd = (currInd + 1) % HISTORYSIZE;
53  if (undoSteps < HISTORYSIZE - 1)
54  undoSteps++;
55  redoSteps = 0;
57  imageSaved = FALSE;
58 }
59 
60 void ImageModel::Undo(BOOL bClearRedo)
61 {
62  ATLTRACE("%s: %d\n", __FUNCTION__, undoSteps);
63  if (undoSteps > 0)
64  {
65  int oldWidth = GetWidth();
66  int oldHeight = GetHeight();
67  selectionWindow.ShowWindow(SW_HIDE);
70  undoSteps--;
71  if (bClearRedo)
72  redoSteps = 0;
73  else if (redoSteps < HISTORYSIZE - 1)
74  redoSteps++;
75  if (GetWidth() != oldWidth || GetHeight() != oldHeight)
78  }
79 }
80 
82 {
83  ATLTRACE("%s: %d\n", __FUNCTION__, redoSteps);
84  if (redoSteps > 0)
85  {
86  int oldWidth = GetWidth();
87  int oldHeight = GetHeight();
88  selectionWindow.ShowWindow(SW_HIDE);
89  currInd = (currInd + 1) % HISTORYSIZE;
91  redoSteps--;
92  if (undoSteps < HISTORYSIZE - 1)
93  undoSteps++;
94  if (GetWidth() != oldWidth || GetHeight() != oldHeight)
97  }
98 }
99 
101 {
102  ATLTRACE("%s: %d\n", __FUNCTION__, currInd);
107 }
108 
110 {
111  undoSteps = 0;
112  redoSteps = 0;
113 }
114 
116 {
117  int oldWidth = GetWidth();
118  int oldHeight = GetHeight();
120  hBms[(currInd + 1) % HISTORYSIZE] = hbm;
121  currInd = (currInd + 1) % HISTORYSIZE;
122  if (undoSteps < HISTORYSIZE - 1)
123  undoSteps++;
124  redoSteps = 0;
126  if (GetWidth() != oldWidth || GetHeight() != oldHeight)
129 }
130 
131 void ImageModel::Crop(int nWidth, int nHeight, int nOffsetX, int nOffsetY)
132 {
133  HDC hdc;
134  HPEN oldPen;
135  HBRUSH oldBrush;
136  int oldWidth = GetWidth();
137  int oldHeight = GetHeight();
138 
139  if (nWidth <= 0)
140  nWidth = 1;
141  if (nHeight <= 0)
142  nHeight = 1;
143 
146  hBms[(currInd + 1) % HISTORYSIZE] = CreateDIBWithProperties(nWidth, nHeight);
147  currInd = (currInd + 1) % HISTORYSIZE;
148  if (undoSteps < HISTORYSIZE - 1)
149  undoSteps++;
150  redoSteps = 0;
151 
154 
155  oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, paletteModel.GetBgColor()));
156  oldBrush = (HBRUSH) SelectObject(hdc, CreateSolidBrush(paletteModel.GetBgColor()));
157  Rectangle(hdc, 0, 0, nWidth, nHeight);
158  BitBlt(hdc, -nOffsetX, -nOffsetY, GetWidth(), GetHeight(), hDrawingDC, 0, 0, SRCCOPY);
159  DeleteObject(SelectObject(hdc, oldBrush));
160  DeleteObject(SelectObject(hdc, oldPen));
161  DeleteDC(hdc);
163 
164  if (GetWidth() != oldWidth || GetHeight() != oldHeight)
167 }
168 
170 {
172 }
173 
175 {
176  return imageSaved;
177 }
178 
180 {
181  return undoSteps > 0;
182 }
183 
185 {
186  return redoSteps > 0;
187 }
188 
189 void ImageModel::StretchSkew(int nStretchPercentX, int nStretchPercentY, int nSkewDegX, int nSkewDegY)
190 {
191  int oldWidth = GetWidth();
192  int oldHeight = GetHeight();
193  INT newWidth = oldWidth * nStretchPercentX / 100;
194  INT newHeight = oldHeight * nStretchPercentY / 100;
195  if (oldWidth != newWidth || oldHeight != newHeight)
196  {
197  HBITMAP hbm0 = CopyDIBImage(hBms[currInd], newWidth, newHeight);
198  Insert(hbm0);
199  }
200  if (nSkewDegX)
201  {
202  HBITMAP hbm1 = SkewDIB(hDrawingDC, hBms[currInd], nSkewDegX, FALSE);
203  Insert(hbm1);
204  }
205  if (nSkewDegY)
206  {
207  HBITMAP hbm2 = SkewDIB(hDrawingDC, hBms[currInd], nSkewDegY, TRUE);
208  Insert(hbm2);
209  }
210  if (GetWidth() != oldWidth || GetHeight() != oldHeight)
213 }
214 
216 {
217  return GetDIBWidth(hBms[currInd]);
218 }
219 
221 {
222  return GetDIBHeight(hBms[currInd]);
223 }
224 
226 {
227  RECT rect = {0, 0, GetWidth(), GetHeight()};
228  CopyPrevious();
231 }
232 
234 {
235  Rectangle(hDrawingDC, 0 - 1, 0 - 1, GetWidth() + 1, GetHeight() + 1);
237 }
238 
240 {
241  return hDrawingDC;
242 }
243 
245 {
246  CopyPrevious();
247  StretchBlt(hDrawingDC, GetWidth() - 1, 0, -GetWidth(), GetHeight(), GetDC(), 0, 0,
248  GetWidth(), GetHeight(), SRCCOPY);
250 }
251 
253 {
254  CopyPrevious();
255  StretchBlt(hDrawingDC, 0, GetHeight() - 1, GetWidth(), -GetHeight(), GetDC(), 0, 0,
256  GetWidth(), GetHeight(), SRCCOPY);
258 }
259 
261 {
262  switch (iN)
263  {
264  case 1:
265  case 3:
268  currInd = (currInd + 1) % HISTORYSIZE;
269  if (undoSteps < HISTORYSIZE - 1)
270  undoSteps++;
271  redoSteps = 0;
273  imageSaved = FALSE;
275  break;
276  case 2:
277  CopyPrevious();
279  0, 0, GetWidth(), GetHeight(), SRCCOPY);
280  break;
281  }
283 }
284 
286 {
289  else
291 }
292 
294 {
295  if (selectionWindow.IsWindowVisible())
296  ResetToPrevious();
297  CopyPrevious();
298  if (selectionWindow.IsWindowVisible())
299  Undo(TRUE);
301  selectionWindow.ShowWindow(SW_HIDE);
303 }
304 
306 {
307  pt.x = max(0, min(pt.x, GetWidth()));
308  pt.y = max(0, min(pt.y, GetHeight()));
309 }
int redoSteps
Definition: history.h:25
void FlipHorizontally()
Definition: history.cpp:244
void SaveImage(LPTSTR lpFileName)
Definition: history.cpp:169
ToolsModel toolsModel
Definition: main.cpp:18
ImageModel()
Definition: history.cpp:27
void CopyPrevious(void)
Definition: history.cpp:47
#define max(a, b)
Definition: svc.c:63
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
int currInd
Definition: history.h:23
#define HISTORYSIZE
Definition: history.h:12
BOOL HasUndoSteps() const
Definition: history.cpp:179
BOOL WINAPI Rectangle(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
static HBITMAP CopyDIBImage(HBITMAP hbm, INT cx=0, INT cy=0)
Definition: dib.h:14
#define TRUE
Definition: types.h:120
#define pt(x, y)
Definition: drawing.c:79
#define SW_HIDE
Definition: winuser.h:762
static HDC
Definition: imagelist.c:92
BOOL IsWindow() const
Definition: atlwin.h:941
HDC hDrawingDC
Definition: history.h:19
void InvertColors()
Definition: history.cpp:225
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
#define ATLTRACE(format,...)
Definition: atltrace.h:276
int GetDIBHeight(HBITMAP hBitmap)
Definition: dib.cpp:62
SelectionModel selectionModel
Definition: main.cpp:20
BOOL WINAPI InvertRect(_In_ HDC, _In_ LPCRECT)
void Redo(void)
Definition: history.cpp:81
BOOL HasRedoSteps() const
Definition: history.cpp:184
int32_t INT
Definition: typedefs.h:58
BOOL WINAPI StretchBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_opt_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ DWORD)
& rect
Definition: startmenu.cpp:1413
COLORREF GetBgColor() const
CHAR * LPTSTR
Definition: xmlstorage.h:192
COLORREF GetFgColor() const
void Crop(int nWidth, int nHeight, int nOffsetX=0, int nOffsetY=0)
Definition: history.cpp:131
CImgAreaWindow imageArea
Definition: main.cpp:72
HBITMAP SkewDIB(HDC hDC1, HBITMAP hbm, INT nDegree, BOOL bVertical)
Definition: dib.cpp:252
int GetDIBWidth(HBITMAP hBitmap)
Definition: dib.cpp:54
#define PS_SOLID
Definition: wingdi.h:586
#define FALSE
Definition: types.h:117
void ClearHistory(void)
Definition: history.cpp:109
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint color
Definition: glext.h:6243
HDC GetDC()
Definition: history.cpp:239
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:17
void DrawBackgroundPoly(HDC hDCImage, COLORREF crBg)
void Insert(HBITMAP hbm)
Definition: history.cpp:115
int GetWidth() const
Definition: history.cpp:215
void Clear(COLORREF color=0x00ffffff)
Definition: history.cpp:233
void DrawBackgroundRect(HDC hDCImage, COLORREF crBg)
int undoSteps
Definition: history.h:24
DWORD COLORREF
Definition: windef.h:300
void StretchSkew(int nStretchPercentX, int nStretchPercentY, int nSkewDegX=0, int nSkewDegY=0)
Definition: history.cpp:189
void RotateNTimes90Degrees(int iN)
Definition: history.cpp:260
HBITMAP Rotate90DegreeBlt(HDC hDC1, INT cx, INT cy, BOOL bRight)
Definition: dib.cpp:213
TOOLTYPE GetActiveTool() const
Definition: toolsmodel.cpp:76
BOOL imageSaved
Definition: main.cpp:54
#define WM_IMAGEMODELIMAGECHANGED
Definition: precomp.h:38
HDC hdc
Definition: main.c:9
BOOL SaveDIBToFile(HBITMAP hBitmap, LPTSTR FileName, HDC hDC)
Definition: dib.cpp:69
int GetHeight() const
Definition: history.cpp:220
PaletteModel paletteModel
Definition: main.cpp:28
BOOL WINAPI DeleteDC(_In_ HDC)
_In_ HBITMAP hbm
Definition: ntgdi.h:2776
#define WM_IMAGEMODELDIMENSIONSCHANGED
Definition: precomp.h:37
void NotifyDimensionsChanged()
Definition: history.cpp:15
#define min(a, b)
Definition: monoChain.cc:55
#define NULL
Definition: types.h:112
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
void DrawSelectionBackground(COLORREF rgbBG)
Definition: history.cpp:285
void FlipVertically()
Definition: history.cpp:252
LRESULT SendMessage(UINT message, WPARAM wParam=0, LPARAM lParam=0)
Definition: atlwin.h:1110
void Bound(POINT &pt)
Definition: history.cpp:305
void NotifyImageChanged()
Definition: history.cpp:21
static HBITMAP
Definition: button.c:44
void DeleteSelection()
Definition: history.cpp:293
void Undo(BOOL bClearRedo=FALSE)
Definition: history.cpp:60
#define __FUNCTION__
Definition: types.h:112
void ResetToPrevious(void)
Definition: history.cpp:100
#define SRCCOPY
Definition: wingdi.h:333
HBITMAP hBms[HISTORYSIZE]
Definition: history.h:21
static VOID NTAPI BitBlt(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Width, _In_ ULONG Height, _In_reads_bytes_(Delta *Height) PUCHAR Buffer, _In_ ULONG BitsPerPixel, _In_ ULONG Delta)
Definition: common.c:49
_In_ LPCSTR lpFileName
Definition: winbase.h:3055
CSelectionWindow selectionWindow
Definition: main.cpp:71
BOOL IsImageSaved() const
Definition: history.cpp:174