winex11.drv: Pass root_window to XCreatePixmap to avoid BadDrawable errors.
[wine] / dlls / gdi32 / tests / dc.c
1 /*
2  * Unit tests for dc functions
3  *
4  * Copyright (c) 2005 Huw Davies
5  * Copyright (c) 2005 Dmitry Timoshkov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdio.h>
24
25 #include "wine/test.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winerror.h"
30
31 static void dump_region(HRGN hrgn)
32 {
33     DWORD i, size;
34     RGNDATA *data = NULL;
35     RECT *rect;
36
37     if (!hrgn)
38     {
39         printf( "(null) region\n" );
40         return;
41     }
42     if (!(size = GetRegionData( hrgn, 0, NULL ))) return;
43     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return;
44     GetRegionData( hrgn, size, data );
45     printf( "%d rects:", data->rdh.nCount );
46     for (i = 0, rect = (RECT *)data->Buffer; i < data->rdh.nCount; i++, rect++)
47         printf( " (%d,%d)-(%d,%d)", rect->left, rect->top, rect->right, rect->bottom );
48     printf( "\n" );
49     HeapFree( GetProcessHeap(), 0, data );
50 }
51
52 static void test_savedc_2(void)
53 {
54     HWND hwnd;
55     HDC hdc;
56     HRGN hrgn;
57     RECT rc, rc_clip;
58     int ret;
59
60     hwnd = CreateWindowExA(0, "static", "", WS_POPUP, 0,0,100,100,
61                            0, 0, 0, NULL);
62     assert(hwnd != 0);
63     ShowWindow(hwnd, SW_SHOW);
64     UpdateWindow(hwnd);
65
66     hrgn = CreateRectRgn(0, 0, 0, 0);
67     assert(hrgn != 0);
68
69     hdc = GetDC(hwnd);
70     ok(hdc != NULL, "CreateDC rets %p\n", hdc);
71
72     ret = GetClipBox(hdc, &rc_clip);
73     ok(ret == SIMPLEREGION, "GetClipBox returned %d instead of SIMPLEREGION\n", ret);
74     ret = GetClipRgn(hdc, hrgn);
75     ok(ret == 0, "GetClipRgn returned %d instead of 0\n", ret);
76     ret = GetRgnBox(hrgn, &rc);
77     ok(ret == NULLREGION, "GetRgnBox returned %d (%d,%d-%d,%d) instead of NULLREGION\n",
78        ret, rc.left, rc.top, rc.right, rc.bottom);
79     /*dump_region(hrgn);*/
80     SetRect(&rc, 0, 0, 100, 100);
81     ok(EqualRect(&rc, &rc_clip),
82        "rects are not equal: (%d,%d-%d,%d) - (%d,%d-%d,%d)\n",
83        rc.left, rc.top, rc.right, rc.bottom,
84        rc_clip.left, rc_clip.top, rc_clip.right, rc_clip.bottom);
85
86     ret = SaveDC(hdc);
87 todo_wine
88 {
89     ok(ret == 1, "ret = %d\n", ret);
90 }
91
92     ret = IntersectClipRect(hdc, 0, 0, 50, 50);
93     if (ret == COMPLEXREGION)
94     {
95         /* XP returns COMPLEXREGION although dump_region reports only 1 rect */
96         trace("Windows BUG: IntersectClipRect returned %d instead of SIMPLEREGION\n", ret);
97         /* let's make sure that it's a simple region */
98         ret = GetClipRgn(hdc, hrgn);
99         ok(ret == 1, "GetClipRgn returned %d instead of 1\n", ret);
100         dump_region(hrgn);
101     }
102     else
103         ok(ret == SIMPLEREGION, "IntersectClipRect returned %d instead of SIMPLEREGION\n", ret);
104
105     ret = GetClipBox(hdc, &rc_clip);
106     ok(ret == SIMPLEREGION, "GetClipBox returned %d instead of SIMPLEREGION\n", ret);
107     SetRect(&rc, 0, 0, 50, 50);
108     ok(EqualRect(&rc, &rc_clip), "rects are not equal\n");
109
110     ret = RestoreDC(hdc, 1);
111     ok(ret, "ret = %d\n", ret);
112
113     ret = GetClipBox(hdc, &rc_clip);
114     ok(ret == SIMPLEREGION, "GetClipBox returned %d instead of SIMPLEREGION\n", ret);
115     SetRect(&rc, 0, 0, 100, 100);
116     ok(EqualRect(&rc, &rc_clip), "rects are not equal\n");
117
118     DeleteObject(hrgn);
119     ReleaseDC(hwnd, hdc);
120     DestroyWindow(hwnd);
121 }
122
123 static void test_savedc(void)
124 {
125     HDC hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
126     int ret;
127
128     ok(hdc != NULL, "CreateDC rets %p\n", hdc);
129
130     ret = SaveDC(hdc);
131     ok(ret == 1, "ret = %d\n", ret);
132     ret = SaveDC(hdc);
133     ok(ret == 2, "ret = %d\n", ret);
134     ret = SaveDC(hdc);
135     ok(ret == 3, "ret = %d\n", ret);
136     ret = RestoreDC(hdc, -1);
137     ok(ret, "ret = %d\n", ret);
138     ret = SaveDC(hdc);
139     ok(ret == 3, "ret = %d\n", ret);
140     ret = RestoreDC(hdc, 1);
141     ok(ret, "ret = %d\n", ret);
142     ret = SaveDC(hdc);
143     ok(ret == 1, "ret = %d\n", ret);
144     ret = SaveDC(hdc);
145     ok(ret == 2, "ret = %d\n", ret);
146     ret = SaveDC(hdc);
147     ok(ret == 3, "ret = %d\n", ret);
148     ret = RestoreDC(hdc, -2);
149     ok(ret, "ret = %d\n", ret);
150     ret = SaveDC(hdc);
151     ok(ret == 2, "ret = %d\n", ret);
152     ret = RestoreDC(hdc, -2);
153     ok(ret, "ret = %d\n", ret);
154     ret = SaveDC(hdc);
155     ok(ret == 1, "ret = %d\n", ret);
156     ret = SaveDC(hdc);
157     ok(ret == 2, "ret = %d\n", ret); 
158     ret = RestoreDC(hdc, -4);
159     ok(!ret, "ret = %d\n", ret);
160     ret = RestoreDC(hdc, 3);
161     ok(!ret, "ret = %d\n", ret);
162
163     /* Under win98 the following two succeed and both clear the save stack
164     ret = RestoreDC(hdc, -3);
165     ok(!ret, "ret = %d\n", ret);
166     ret = RestoreDC(hdc, 0);
167     ok(!ret, "ret = %d\n", ret);
168     */
169
170     ret = RestoreDC(hdc, 1);
171     ok(ret, "ret = %d\n", ret);
172
173     DeleteDC(hdc);
174 }
175
176 START_TEST(dc)
177 {
178     test_savedc();
179     test_savedc_2();
180 }