:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / tests / gditest / gditest.c
1 /*
2  * gditest
3    dec 26, 2001 -- gditest bug fix by Richard Campbell
4  */
5
6 #include <windows.h>
7
8 extern BOOL STDCALL GdiDllInitialize(HANDLE hInstance, DWORD Event, LPVOID Reserved);
9
10 void __stdcall Background (HDC Desktop)
11 {
12         HPEN Pen;
13         int x, y;
14
15         Pen = CreatePen(PS_SOLID, 1, RGB(64, 64, 128));
16
17         SelectObject (Desktop, Pen);
18
19         MoveToEx (Desktop, 0, 0, NULL);
20         LineTo (Desktop, 640, 480);
21         for (y = 479, x = 0; x < 640; x+=42)
22         {
23                 MoveToEx (Desktop, 0, 0, NULL);
24                 LineTo (Desktop, x, y);
25         }
26         for (y = 0, x = 639; y < 480; y+=42)
27         {
28                 MoveToEx (Desktop, 0, 0, NULL);
29                 LineTo (Desktop, x, y);
30         }
31 }
32
33 void gditest( void ){
34   HDC  Desktop, MyDC, DC24;
35   HPEN  RedPen, GreenPen, BluePen, WhitePen;
36   HBITMAP  MyBitmap, DIB24;
37   HFONT  hf, tf;
38   BITMAPINFOHEADER BitInf;
39   BITMAPINFO BitPalInf;
40   HRGN hRgn1, hRgn2, hRgn3;
41   HBRUSH BlueBrush, DefBrush;
42
43   // Set up a DC called Desktop that accesses DISPLAY
44   Desktop = CreateDCA("DISPLAY", NULL, NULL, NULL);
45   if (Desktop == NULL){
46         printf("Can't create desktop\n");
47     return;
48   }
49
50   // Background
51   Background (Desktop);
52
53
54 //ei
55   BlueBrush = CreateSolidBrush( RGB(0, 0, 0xff) );
56   DefBrush = SelectObject( Desktop, BlueBrush );
57
58   hRgn1 = CreateRectRgn( 1, 2, 100, 101 );
59   hRgn2 = CreateRectRgn( 10, 20, 150, 151 );
60   hRgn3 = CreateRectRgn( 1, 1, 1, 1);
61   CombineRgn( hRgn3, hRgn1, hRgn2, RGN_XOR );
62
63   PaintRgn( Desktop, hRgn3 );
64   SelectObject( Desktop, DefBrush );
65   DeleteObject( BlueBrush );
66
67   // Create a blue pen and select it into the DC
68   BluePen = CreatePen(PS_SOLID, 8, RGB(0, 0, 0xff));
69   SelectObject(Desktop, BluePen);
70
71   // Draw a shape on the DC
72   MoveToEx(Desktop, 50, 50, NULL);
73   LineTo(Desktop, 200, 60);
74   LineTo(Desktop, 200, 300);
75   LineTo(Desktop, 50, 50);
76   MoveToEx(Desktop, 50, 50, NULL);
77   LineTo(Desktop, 200, 50);
78
79   WhitePen = CreatePen(PS_SOLID, 3, RGB(0xff, 0xff, 0xff));
80   SelectObject(Desktop, WhitePen);
81
82   MoveToEx(Desktop, 20, 70, NULL);
83   LineTo(Desktop, 500, 70);
84   MoveToEx(Desktop, 70, 20, NULL);
85   LineTo(Desktop, 70, 150);
86
87   // Test font support
88   GreenPen = CreatePen(PS_SOLID, 3, RGB(0, 0xff, 0));
89   RedPen = CreatePen(PS_SOLID, 3, RGB(0xff, 0, 0));
90
91   hf = CreateFontA(24, 0, 0, TA_BASELINE, FW_NORMAL, FALSE, FALSE, FALSE,
92                    ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
93                    DEFAULT_QUALITY, FIXED_PITCH|FF_DONTCARE, "Helmet");
94   SelectObject(Desktop, hf);
95   SetTextColor(Desktop, RGB(0xff, 0, 0));
96   TextOutA(Desktop, 70, 70, "React", 5);
97   SetTextColor(Desktop, RGB(0, 0xff, 0));
98   TextOutA(Desktop, 140, 70, "OS", 2);
99
100   tf = CreateFontA(14, 0, 0, TA_BASELINE, FW_NORMAL, FALSE, FALSE, FALSE,
101                    ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
102                    DEFAULT_QUALITY, FIXED_PITCH|FF_DONTCARE, "Timmons");
103   SelectObject(Desktop, tf);
104   SetTextColor(Desktop, RGB(0xff, 0xff, 0xff));
105   TextOutA(Desktop, 70, 90, "This is a test of ReactOS text, using the FreeType 2 library!", 61);
106
107   // TEST 1: Copy from the VGA into a device compatible DC, draw on it, then blt it to the VGA again
108   MyDC = CreateCompatibleDC(Desktop);
109   MyBitmap = CreateCompatibleBitmap(Desktop, 151, 251);
110   SelectObject(MyDC, MyBitmap);
111   BitBlt(MyDC, 0, 0, 151, 251, Desktop, 50, 50, SRCCOPY); // can we say 151, 251 since bottom corner is not inclusive?
112
113   SelectObject(MyDC, GreenPen);
114   Rectangle(MyDC, 10, 10, 50, 50);
115
116   // TEST 2: Copy from the device compatible DC into a 24BPP bitmap, draw on it, then blt to the VGA again
117   BitInf.biSize = sizeof(BITMAPINFOHEADER);
118   BitInf.biWidth = 152;
119   BitInf.biHeight = -252; // it's top down (since BI_RGB is used, the sign is operative of direction)
120   BitInf.biPlanes = 1;
121   BitInf.biBitCount = 24;
122   BitInf.biCompression = BI_RGB;
123   BitInf.biSizeImage = 0;
124   BitInf.biXPelsPerMeter = 0;
125   BitInf.biYPelsPerMeter = 0;
126   BitInf.biClrUsed = 0;
127   BitInf.biClrImportant = 0;
128   BitPalInf.bmiHeader = BitInf;
129   DIB24 = (HBITMAP) CreateDIBSection(NULL, &BitPalInf, DIB_RGB_COLORS, NULL, NULL, 0);
130   DC24 = CreateCompatibleDC(NULL);
131   SelectObject(DC24, DIB24);
132
133   BitBlt(DC24, 0, 0, 101, 201, MyDC, 0, 0, SRCCOPY);
134   SelectObject(DC24, RedPen);
135   Rectangle(DC24, 80, 90, 100, 110);
136   MoveToEx(DC24, 80, 90, NULL);
137   LineTo(DC24, 100, 110);
138   BitBlt(Desktop, 200, 200, 110, 120, DC24, 0, 0, SRCCOPY);
139
140   Sleep( 10000 ); // fixme delay only 10000 (for 10 seconds)
141   // Free up everything
142   DeleteDC(Desktop);
143   DeleteDC(MyDC);
144 }
145
146 void DumpRgnData( HRGN hRgn )
147 {
148         int size, ret, i;
149         LPRGNDATA rgnData;
150
151         size = GetRegionData( hRgn, 0, NULL );
152         if( size == 0 ){
153                 printf("GetRegionData returned 0\n");
154                 return;
155         }
156         rgnData = (LPRGNDATA) malloc( size );
157         ret = GetRegionData( hRgn, size, rgnData );
158         if( ret == 0 ){
159                 printf("GetRegionData( hRgn, size, rgnData ) returned 0\n");
160                 return;
161         }
162         printf("Bounds: left=%d top=%d right=%d bottom=%d, count: %d, type: %i\n\n",
163                 rgnData->rdh.rcBound.left, rgnData->rdh.rcBound.top, rgnData->rdh.rcBound.right, rgnData->rdh.rcBound.bottom,
164                 rgnData->rdh.nCount, rgnData->rdh.iType);
165         printf("Rects:\t i \t left \t top \t right \t bottom\n");
166         for ( i = 0; i < rgnData->rdh.nCount; i++ ) {
167                 PRECT pr = (PRECT) rgnData->Buffer + i;
168                 printf("\t %d \t %d \t %d \t %d \t %d\n", i, pr->left, pr->top, pr->right, pr->bottom );
169         }
170         printf("\n");
171 }
172
173 void rgntest( void )
174 {
175         HRGN hRgn1, hRgn2, hRgn3;
176         RECT Rect;
177         int i;
178
179         hRgn1 = CreateRectRgn( 1, 2, 100, 101 );
180         if( hRgn1 == NULL ) {
181                 printf("Failed at hRgn1 = CreateRectRgn( 1, 2, 100, 101 )\n");
182                 return;
183         }
184         i = GetRgnBox( hRgn1, &Rect );
185         if( i==0 ){
186                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
187                 return;
188         }
189         printf("GetRgnBox( hRgn1, &Rect ): i=%d, left=%d top=%d right=%d bottom=%d\n\n",
190                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
191
192         DumpRgnData( hRgn1 );
193
194         hRgn2 = CreateRectRgn( 51, 53, 150, 152 );
195         if( hRgn2 == NULL ) {
196                 printf("Failed at hRgn2 = CreateRectRgn( 51, 53, 150, 152 )\n");
197                 return;
198         }
199         i = GetRgnBox( hRgn2, &Rect );
200         if( i==0 ){
201                 printf("Failed GetRgnBox( hRgn2, &Rect )\n");
202                 return;
203         }
204         printf("GetRgnBox( hRgn2, &Rect ): i=%d, left=%d top=%d right=%d bottom=%d\n\n",
205                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
206
207         DumpRgnData( hRgn2 );
208
209         if( EqualRgn( hRgn1, hRgn2 ) == TRUE ){
210                 printf("\t hRgn1, hRgn2 are equal\n");
211         }
212         else{
213                 printf("\t hRgn1, hRgn2 are NOT equal\n\n");
214         }
215
216         i = OffsetRgn(hRgn1,50,51);
217         if( i==ERROR ){
218                 printf("Failed OffsetRgn(hRgn1,50,51)\n");
219                 return;
220         }
221
222         i = GetRgnBox( hRgn1, &Rect );
223         if( i==0 ){
224                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
225                 return;
226         }
227         printf("After offset\nGetRgnBox( hRgn1, &Rect ): i=%d, left=%d top=%d right=%d bottom=%d\n\n",
228                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
229
230         if( EqualRgn( hRgn1, hRgn2 ) == TRUE ){
231                 printf("\t hRgn1, hRgn2 are equal after offset\n");
232         }
233         else{
234                 printf("\t hRgn1, hRgn2 are NOT equal after offset!\n\n");
235         }
236
237         i = SetRectRgn(hRgn1, 10, 11, 110, 111 );
238         if( i==0 ){
239                 printf("Failed SetRectRgn(hRgn1... )\n");
240                 return;
241         }
242         i = GetRgnBox( hRgn1, &Rect );
243         if( i==0 ){
244                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
245                 return;
246         }
247         printf("after SetRectRgn(hRgn1, 10, 11, 110, 111 ):\n i=%d, left=%d top=%d right=%d bottom=%d\n\n",
248                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
249
250         hRgn3 = CreateRectRgn( 1, 1, 1, 1);
251         i = CombineRgn( hRgn3, hRgn1, hRgn2, RGN_AND );
252         if( i==ERROR ){
253                 printf("Fail: CombineRgn( hRgn3, hRgn1, hRgn2, RGN_AND ). LastError: %d\n", GetLastError);
254                 return;
255         }
256
257         if( GetRgnBox( hRgn3, &Rect )==0 ){
258                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
259                 return;
260         }
261         printf("After CombineRgn( hRgn3, hRgn1, hRgn2, RGN_AND ): \nGetRgnBox( hRgn3, &Rect ): CR_i=%d, left=%d top=%d right=%d bottom=%d\n\n",
262                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
263         DumpRgnData( hRgn3 );
264
265         i = CombineRgn( hRgn3, hRgn1, hRgn2, RGN_OR );
266         if( i==ERROR ){
267                 printf("Fail: CombineRgn( hRgn3, hRgn1, hRgn2, RGN_OR ). LastError: %d\n", GetLastError);
268                 return;
269         }
270
271         if( GetRgnBox( hRgn3, &Rect )==0 ){
272                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
273                 return;
274         }
275         printf("After CombineRgn( hRgn3, hRgn1, hRgn2, RGN_OR ): \nGetRgnBox( hRgn3, &Rect ): CR_i=%d, left=%d top=%d right=%d bottom=%d\n\n",
276                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
277         DumpRgnData( hRgn3 );
278
279         i = CombineRgn( hRgn3, hRgn1, hRgn2, RGN_DIFF );
280         if( i==ERROR ){
281                 printf("Fail: CombineRgn( hRgn3, hRgn1, hRgn2, RGN_DIFF ). LastError: %d\n", GetLastError);
282                 return;
283         }
284
285         if( GetRgnBox( hRgn3, &Rect )==0 ){
286                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
287                 return;
288         }
289         printf("After CombineRgn( hRgn3, hRgn1, hRgn2, RGN_DIFF ): \nGetRgnBox( hRgn3, &Rect ): CR_i=%d, left=%d top=%d right=%d bottom=%d\n\n",
290                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
291         DumpRgnData( hRgn3 );
292
293         i = CombineRgn( hRgn3, hRgn1, hRgn2, RGN_XOR );
294         if( i==ERROR ){
295                 printf("Fail: CombineRgn( hRgn3, hRgn1, hRgn2, RGN_XOR ). LastError: %d\n", GetLastError);
296                 return;
297         }
298
299         if( GetRgnBox( hRgn3, &Rect )==0 ){
300                 printf("Failed GetRgnBox( hRgn3, &Rect )\n");
301                 return;
302         }
303         printf("After CombineRgn( hRgn3, hRgn1, hRgn2, RGN_XOR ): \nGetRgnBox( hRgn3, &Rect ): CR_i=%d, left=%d top=%d right=%d bottom=%d\n\n",
304                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
305         DumpRgnData( hRgn3 );
306
307         i = CombineRgn( hRgn1, hRgn3, hRgn2, RGN_COPY );
308         if( i==ERROR ){
309                 printf("Fail: CombineRgn( hRgn1, hRgn3, hRgn2, RGN_COPY ). LastError: %d\n", GetLastError);
310                 return;
311         }
312
313         if( GetRgnBox( hRgn1, &Rect )==0 ){
314                 printf("Failed GetRgnBox( hRgn1, &Rect )\n");
315                 return;
316         }
317         printf("After CombineRgn( hRgn1, hRgn3, hRgn2, RGN_COPY ): \nGetRgnBox( hRgn1, &Rect ): CR_i=%d, left=%d top=%d right=%d bottom=%d\n\n",
318                 i, Rect.left, Rect.top, Rect.right, Rect.bottom );
319         DumpRgnData( hRgn1 );
320
321
322         DeleteObject( hRgn1 );
323         DeleteObject( hRgn2 );
324         DeleteObject( hRgn3 );
325         printf("region test finished\n");
326 }
327
328 int main (int argc, char* argv[])
329 {
330   printf("Entering GDITest..\n");
331   printf("use gditest for older tests\n");
332   printf("use gditest 1 for region test\n");
333
334   GdiDllInitialize (NULL, DLL_PROCESS_ATTACH, NULL);
335   if( argc < 2 )
336         gditest();
337   else {
338         if( !strncmp( argv[1], "1", 1 ) ) {
339                 rgntest();
340         }
341   }
342
343   return 0;
344 }