* Sync with recent trunk (r52637).
[reactos.git] / base / applications / mspaint / history.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: base/applications/paint/history.c
5 * PURPOSE: Undo and redo functionality
6 * PROGRAMMERS: Benedikt Freisen
7 */
8
9 /* INCLUDES *********************************************************/
10
11 #include <windows.h>
12 #include "globalvar.h"
13 #include "dib.h"
14 #include "definitions.h"
15
16 /* FUNCTIONS ********************************************************/
17
18 extern void updateCanvasAndScrollbars(void);
19
20 void
21 setImgXYRes(int x, int y)
22 {
23 if ((imgXRes != x) || (imgYRes != y))
24 {
25 imgXRes = x;
26 imgYRes = y;
27 updateCanvasAndScrollbars();
28 }
29 }
30
31 void
32 newReversible()
33 {
34 DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
35 hBms[(currInd + 1) % HISTORYSIZE] = CopyImage(hBms[currInd], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
36 currInd = (currInd + 1) % HISTORYSIZE;
37 if (undoSteps < HISTORYSIZE - 1)
38 undoSteps++;
39 redoSteps = 0;
40 SelectObject(hDrawingDC, hBms[currInd]);
41 imgXRes = GetDIBWidth(hBms[currInd]);
42 imgYRes = GetDIBHeight(hBms[currInd]);
43 imageSaved = FALSE;
44 }
45
46 void
47 undo()
48 {
49 if (undoSteps > 0)
50 {
51 ShowWindow(hSelection, SW_HIDE);
52 currInd = (currInd + HISTORYSIZE - 1) % HISTORYSIZE;
53 SelectObject(hDrawingDC, hBms[currInd]);
54 undoSteps--;
55 if (redoSteps < HISTORYSIZE - 1)
56 redoSteps++;
57 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
58 }
59 }
60
61 void
62 redo()
63 {
64 if (redoSteps > 0)
65 {
66 ShowWindow(hSelection, SW_HIDE);
67 currInd = (currInd + 1) % HISTORYSIZE;
68 SelectObject(hDrawingDC, hBms[currInd]);
69 redoSteps--;
70 if (undoSteps < HISTORYSIZE - 1)
71 undoSteps++;
72 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
73 }
74 }
75
76 void
77 resetToU1()
78 {
79 DeleteObject(hBms[currInd]);
80 hBms[currInd] =
81 CopyImage(hBms[(currInd + HISTORYSIZE - 1) % HISTORYSIZE], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
82 SelectObject(hDrawingDC, hBms[currInd]);
83 imgXRes = GetDIBWidth(hBms[currInd]);
84 imgYRes = GetDIBHeight(hBms[currInd]);
85 }
86
87 void
88 clearHistory()
89 {
90 undoSteps = 0;
91 redoSteps = 0;
92 }
93
94 void
95 insertReversible(HBITMAP hbm)
96 {
97 DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
98 hBms[(currInd + 1) % HISTORYSIZE] = hbm;
99 currInd = (currInd + 1) % HISTORYSIZE;
100 if (undoSteps < HISTORYSIZE - 1)
101 undoSteps++;
102 redoSteps = 0;
103 SelectObject(hDrawingDC, hBms[currInd]);
104 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
105 }
106
107 void
108 cropReversible(int width, int height, int xOffset, int yOffset)
109 {
110 HDC hdc;
111 HPEN oldPen;
112 HBRUSH oldBrush;
113
114 SelectObject(hDrawingDC, hBms[currInd]);
115 DeleteObject(hBms[(currInd + 1) % HISTORYSIZE]);
116 hBms[(currInd + 1) % HISTORYSIZE] = CreateDIBWithProperties(width, height);
117 currInd = (currInd + 1) % HISTORYSIZE;
118 if (undoSteps < HISTORYSIZE - 1)
119 undoSteps++;
120 redoSteps = 0;
121
122 hdc = CreateCompatibleDC(hDrawingDC);
123 SelectObject(hdc, hBms[currInd]);
124
125 oldPen = SelectObject(hdc, CreatePen(PS_SOLID, 1, bgColor));
126 oldBrush = SelectObject(hdc, CreateSolidBrush(bgColor));
127 Rectangle(hdc, 0, 0, width, height);
128 BitBlt(hdc, -xOffset, -yOffset, imgXRes, imgYRes, hDrawingDC, 0, 0, SRCCOPY);
129 DeleteObject(SelectObject(hdc, oldBrush));
130 DeleteObject(SelectObject(hdc, oldPen));
131 DeleteDC(hdc);
132 SelectObject(hDrawingDC, hBms[currInd]);
133
134 setImgXYRes(width, height);
135 }