attempt to convert most of this mess into something which at least look like C code
[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 extern void updateCanvasAndScrollbars(void);
19
20 void setImgXYRes(int x, int y)
21 {
22 if ((imgXRes!=x)||(imgYRes!=y))
23 {
24 imgXRes = x;
25 imgYRes = y;
26 updateCanvasAndScrollbars();
27 }
28 }
29
30 void newReversible()
31 {
32 DeleteObject(hBms[(currInd+1)%4]);
33 hBms[(currInd+1)%4] = CopyImage( hBms[currInd], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
34 currInd = (currInd+1)%4;
35 if (undoSteps<3) undoSteps++;
36 redoSteps = 0;
37 SelectObject(hDrawingDC, hBms[currInd]);
38 imgXRes = GetDIBWidth(hBms[currInd]);
39 imgYRes = GetDIBHeight(hBms[currInd]);
40 }
41
42 void undo()
43 {
44 if (undoSteps>0)
45 {
46 ShowWindow(hSelection, SW_HIDE);
47 currInd = (currInd+3)%4;
48 SelectObject(hDrawingDC, hBms[currInd]);
49 undoSteps--;
50 if (redoSteps<3) redoSteps++;
51 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
52 }
53 }
54
55 void redo()
56 {
57 if (redoSteps>0)
58 {
59 ShowWindow(hSelection, SW_HIDE);
60 currInd = (currInd+1)%4;
61 SelectObject(hDrawingDC, hBms[currInd]);
62 redoSteps--;
63 if (undoSteps<3) undoSteps++;
64 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
65 }
66 }
67
68 void resetToU1()
69 {
70 DeleteObject(hBms[currInd]);
71 hBms[currInd] = CopyImage( hBms[(currInd+3)%4], IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG);
72 SelectObject(hDrawingDC, hBms[currInd]);
73 imgXRes = GetDIBWidth(hBms[currInd]);
74 imgYRes = GetDIBHeight(hBms[currInd]);
75 }
76
77 void clearHistory()
78 {
79 undoSteps = 0;
80 redoSteps = 0;
81 }
82
83 void insertReversible(HBITMAP hbm)
84 {
85 DeleteObject(hBms[(currInd+1)%4]);
86 hBms[(currInd+1)%4] = hbm;
87 currInd = (currInd+1)%4;
88 if (undoSteps<3) undoSteps++;
89 redoSteps = 0;
90 SelectObject(hDrawingDC, hBms[currInd]);
91 setImgXYRes(GetDIBWidth(hBms[currInd]), GetDIBHeight(hBms[currInd]));
92 }
93
94 void cropReversible(int x, int y)//FIXME: This function is broken
95 {
96 HBITMAP oldBitmap;
97 HPEN oldPen;
98 HBRUSH oldBrush;
99
100 SelectObject(hDrawingDC, hBms[currInd]);
101 DeleteObject(hBms[(currInd+1)%4]);
102 hBms[(currInd+1)%4] = CreateDIBWithProperties(x, y);
103 currInd = (currInd+1)%4;
104 if (undoSteps<3) undoSteps++;
105 redoSteps = 0;
106
107 oldBitmap = SelectObject(hSelDC, hBms[currInd]);
108 oldPen = SelectObject(hSelDC, CreatePen(PS_SOLID, 1, bgColor));
109 oldBrush = SelectObject(hSelDC, CreateSolidBrush(bgColor));
110 Rectangle(hSelDC, 0, 0, x, y);
111 DeleteObject(SelectObject(hSelDC, oldBrush));
112 DeleteObject(SelectObject(hSelDC, oldPen));
113 BitBlt(hDrawingDC, 0, 0, imgXRes, imgYRes, hSelDC, 0, 0, SRCCOPY);
114 SelectObject(hDrawingDC, SelectObject(hSelDC, oldBitmap));
115
116 setImgXYRes(x, y);
117 }