Added Paint for ReactOS source folder
[reactos.git] / reactos / base / applications / paint / history.c
1 /*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL
4 * FILE: 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 void setImgXYRes(int x, int y)
19 {
20 if ((imgXRes!=x)||(imgYRes!=y))
21 {
22 imgXRes = x;
23 imgYRes = y;
24 updateCanvasAndScrollbars();
25 }
26 }
27
28 void newReversible()
29 {
30 DeleteObject(hBms[(currInd+1)%4]);
31 hBms[(currInd+1)%4] = CopyImage( hBms[currInd], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
32 currInd = (currInd+1)%4;
33 if (undoSteps<3) undoSteps++;
34 redoSteps = 0;
35 SelectObject(hDrawingDC, hBms[currInd]);
36 imgXRes = GetDIBWidth(hBms[currInd]);
37 imgYRes = GetDIBHeight(hBms[currInd]);
38 }
39
40 void undo()
41 {
42 if (undoSteps>0)
43 {
44 ShowWindow(hSelection, SW_HIDE);
45 currInd = (currInd+3)%4;
46 SelectObject(hDrawingDC, hBms[currInd]);
47 undoSteps--;
48 if (redoSteps<3) redoSteps++;
49 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
50 }
51 }
52
53 void redo()
54 {
55 if (redoSteps>0)
56 {
57 ShowWindow(hSelection, SW_HIDE);
58 currInd = (currInd+1)%4;
59 SelectObject(hDrawingDC, hBms[currInd]);
60 redoSteps--;
61 if (undoSteps<3) undoSteps++;
62 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
63 }
64 }
65
66 void resetToU1()
67 {
68 DeleteObject(hBms[currInd]);
69 hBms[currInd] = CopyImage( hBms[(currInd+3)%4], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
70 SelectObject(hDrawingDC, hBms[currInd]);
71 imgXRes = GetDIBWidth(hBms[currInd]);
72 imgYRes = GetDIBHeight(hBms[currInd]);
73 }
74
75 void clearHistory()
76 {
77 undoSteps = 0;
78 redoSteps = 0;
79 }
80
81 void insertReversible(HBITMAP hbm)
82 {
83 DeleteObject(hBms[(currInd+1)%4]);
84 hBms[(currInd+1)%4] = hbm;
85 currInd = (currInd+1)%4;
86 if (undoSteps<3) undoSteps++;
87 redoSteps = 0;
88 SelectObject(hDrawingDC, hBms[currInd]);
89 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
90 }
91
92 void cropReversible(int x, int y)
93 {
94 SelectObject(hDrawingDC, hBms[currInd]);
95 DeleteObject(hBms[(currInd+1)%4]);
96 hBms[(currInd+1)%4] = CreateDIBWithProperties(x, y);
97 currInd = (currInd+1)%4;
98 if (undoSteps<3) undoSteps++;
99 redoSteps = 0;
100
101 HBITMAP oldBitmap = SelectObject(hSelDC, hBms[currInd]);
102 HPEN oldPen = SelectObject(hSelDC, CreatePen(PS_SOLID, 1, bgColor));
103 HBRUSH oldBrush = SelectObject(hSelDC, CreateSolidBrush(bgColor));
104 Rectangle(hSelDC, 0, 0, x, y);
105 DeleteObject(SelectObject(hSelDC, oldBrush));
106 DeleteObject(SelectObject(hSelDC, oldPen));
107 BitBlt(hDrawingDC, 0, 0, imgXRes, imgYRes, hSelDC, 0, 0, SRCCOPY);
108 SelectObject(hDrawingDC, SelectObject(hSelDC, oldBitmap));
109
110 setImgXYRes(x, y);
111 }