gdi32: Fix handling of the DC brush/pen in metafiles.
[wine] / dlls / gdi32 / tests / mapping.c
1 /*
2  * Unit tests for mapping functions
3  *
4  * Copyright (c) 2005 Huw Davies
5  * Copyright (c) 2008 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 #include <math.h>
25
26 #include "wine/test.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "winuser.h"
30 #include "winerror.h"
31
32 static DWORD (WINAPI *pSetLayout)(HDC hdc, DWORD layout);
33 static DWORD (WINAPI *pGetLayout)(HDC hdc);
34 static INT (WINAPI *pGetRandomRgn)(HDC hDC, HRGN hRgn, INT iCode);
35 static BOOL (WINAPI *pGetTransform)(HDC, DWORD, XFORM *);
36 static DWORD (WINAPI *pSetVirtualResolution)(HDC, DWORD, DWORD, DWORD, DWORD);
37
38 #define rough_match(got, expected) (abs( MulDiv( (got) - (expected), 1000, (expected) )) <= 5)
39
40 #define expect_LPtoDP(_hdc, _x, _y) \
41 { \
42     POINT _pt = { 1000, 1000 }; \
43     LPtoDP(_hdc, &_pt, 1); \
44     ok(rough_match(_pt.x, _x), "expected x %d, got %d\n", (_x), _pt.x); \
45     ok(rough_match(_pt.y, _y), "expected y %d, got %d\n", (_y), _pt.y); \
46 }
47
48 #define expect_world_transform(_hdc, _em11, _em22) \
49 { \
50     BOOL _ret; \
51     XFORM _xform; \
52     SetLastError(0xdeadbeef); \
53     _ret = GetWorldTransform(_hdc, &_xform); \
54     if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) \
55     { \
56         ok(_ret, "GetWorldTransform error %u\n", GetLastError()); \
57         ok(_xform.eM11 == (_em11), "expected %f, got %f\n", (_em11), _xform.eM11); \
58         ok(_xform.eM12 == 0.0, "expected 0.0, got %f\n", _xform.eM12); \
59         ok(_xform.eM21 == 0.0, "expected 0.0, got %f\n", _xform.eM21); \
60         ok(_xform.eM22 == (_em22), "expected %f, got %f\n", (_em22), _xform.eM22); \
61         ok(_xform.eDx == 0.0, "expected 0.0, got %f\n", _xform.eDx); \
62         ok(_xform.eDy == 0.0, "expected 0.0, got %f\n", _xform.eDy); \
63     } \
64 }
65
66 #define expect_dc_ext(_func, _hdc, _cx, _cy) \
67 { \
68     BOOL _ret; \
69     SIZE _size; \
70     SetLastError(0xdeadbeef); \
71     _ret = _func(_hdc, &_size); \
72     ok(_ret, #_func " error %u\n", GetLastError()); \
73     ok(_size.cx == (_cx), "expected cx %d, got %d\n", (_cx), _size.cx); \
74     ok(_size.cy == (_cy), "expected cy %d, got %d\n", (_cy), _size.cy); \
75 }
76
77 #define expect_viewport_ext(_hdc, _cx, _cy) expect_dc_ext(GetViewportExtEx, _hdc, _cx, _cy)
78 #define expect_window_ext(_hdc, _cx, _cy)  expect_dc_ext(GetWindowExtEx, _hdc, _cx, _cy)
79
80 static void test_world_transform(void)
81 {
82     BOOL is_win9x;
83     HDC hdc;
84     INT ret, size_cx, size_cy, res_x, res_y, dpi_x, dpi_y;
85     XFORM xform;
86     SIZE size;
87
88     SetLastError(0xdeadbeef);
89     GetWorldTransform(0, NULL);
90     is_win9x = GetLastError() == ERROR_CALL_NOT_IMPLEMENTED;
91
92     hdc = CreateCompatibleDC(0);
93
94     xform.eM11 = 1.0f;
95     xform.eM12 = 0.0f;
96     xform.eM21 = 0.0f;
97     xform.eM22 = 1.0f;
98     xform.eDx = 0.0f;
99     xform.eDy = 0.0f;
100     ret = SetWorldTransform(hdc, &xform);
101     ok(!ret, "SetWorldTransform should fail in GM_COMPATIBLE mode\n");
102
103     size_cx = GetDeviceCaps(hdc, HORZSIZE);
104     size_cy = GetDeviceCaps(hdc, VERTSIZE);
105     res_x = GetDeviceCaps(hdc, HORZRES);
106     res_y = GetDeviceCaps(hdc, VERTRES);
107     dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
108     dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
109     trace("dc size %d x %d, resolution %d x %d dpi %d x %d\n",
110           size_cx, size_cy, res_x, res_y, dpi_x, dpi_y );
111
112     expect_viewport_ext(hdc, 1, 1);
113     expect_window_ext(hdc, 1, 1);
114     expect_world_transform(hdc, 1.0, 1.0);
115     expect_LPtoDP(hdc, 1000, 1000);
116
117     SetLastError(0xdeadbeef);
118     ret = SetMapMode(hdc, MM_LOMETRIC);
119     ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
120
121     if (is_win9x)
122     {
123         expect_viewport_ext(hdc, dpi_x, dpi_y);
124         expect_window_ext(hdc, 254, -254);
125     }
126     else
127     {
128         expect_viewport_ext(hdc, res_x, -res_y);
129         ok( GetWindowExtEx( hdc, &size ), "GetWindowExtEx failed\n" );
130         ok( rough_match( size.cx, size_cx * 10 ) ||
131             rough_match( size.cx, MulDiv( res_x, 254, dpi_x )),  /* Vista uses a more precise method */
132             "expected cx %d or %d, got %d\n", size_cx * 10, MulDiv( res_x, 254, dpi_x ), size.cx );
133         ok( rough_match( size.cy, size_cy * 10 ) ||
134             rough_match( size.cy, MulDiv( res_y, 254, dpi_y )),  /* Vista uses a more precise method */
135             "expected cy %d or %d, got %d\n", size_cy * 10, MulDiv( res_y, 254, dpi_y ), size.cy );
136     }
137     expect_world_transform(hdc, 1.0, 1.0);
138     expect_LPtoDP(hdc, MulDiv(1000 / 10, res_x, size_cx), -MulDiv(1000 / 10, res_y, size_cy));
139
140     SetLastError(0xdeadbeef);
141     ret = SetMapMode(hdc, MM_TEXT);
142     ok(ret == MM_LOMETRIC, "expected MM_LOMETRIC, got %d\n", ret);
143
144     expect_viewport_ext(hdc, 1, 1);
145     expect_window_ext(hdc, 1, 1);
146     expect_world_transform(hdc, 1.0, 1.0);
147     expect_LPtoDP(hdc, 1000, 1000);
148
149     ret = SetGraphicsMode(hdc, GM_ADVANCED);
150     if (!ret)
151     {
152         DeleteDC(hdc);
153         skip("GM_ADVANCED is not supported on this platform\n");
154         return;
155     }
156
157     expect_viewport_ext(hdc, 1, 1);
158     expect_window_ext(hdc, 1, 1);
159     expect_world_transform(hdc, 1.0, 1.0);
160     expect_LPtoDP(hdc, 1000, 1000);
161
162     /* The transform must conform to (eM11 * eM22 != eM12 * eM21) requirement */
163     xform.eM11 = 1.0f;
164     xform.eM12 = 2.0f;
165     xform.eM21 = 1.0f;
166     xform.eM22 = 2.0f;
167     xform.eDx = 0.0f;
168     xform.eDy = 0.0f;
169     ret = SetWorldTransform(hdc, &xform);
170     ok(!ret ||
171        broken(ret), /* NT4 */
172        "SetWorldTransform should fail with an invalid xform\n");
173
174     xform.eM11 = 20.0f;
175     xform.eM12 = 0.0f;
176     xform.eM21 = 0.0f;
177     xform.eM22 = 20.0f;
178     xform.eDx = 0.0f;
179     xform.eDy = 0.0f;
180     SetLastError(0xdeadbeef);
181     ret = SetWorldTransform(hdc, &xform);
182     ok(ret, "SetWorldTransform error %u\n", GetLastError());
183
184     expect_viewport_ext(hdc, 1, 1);
185     expect_window_ext(hdc, 1, 1);
186     expect_world_transform(hdc, 20.0, 20.0);
187     expect_LPtoDP(hdc, 20000, 20000);
188
189     SetLastError(0xdeadbeef);
190     ret = SetMapMode(hdc, MM_LOMETRIC);
191     ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
192
193     expect_viewport_ext(hdc, res_x, -res_y);
194     ok( GetWindowExtEx( hdc, &size ), "GetWindowExtEx failed\n" );
195     ok( rough_match( size.cx, size_cx * 10 ) ||
196         rough_match( size.cx, MulDiv( res_x, 254, dpi_x )),  /* Vista uses a more precise method */
197         "expected cx %d or %d, got %d\n", size_cx * 10, MulDiv( res_x, 254, dpi_x ), size.cx );
198     ok( rough_match( size.cy, size_cy * 10 ) ||
199         rough_match( size.cy, MulDiv( res_y, 254, dpi_y )),  /* Vista uses a more precise method */
200         "expected cy %d or %d, got %d\n", size_cy * 10, MulDiv( res_y, 254, dpi_y ), size.cy );
201     expect_world_transform(hdc, 20.0, 20.0);
202     expect_LPtoDP(hdc, MulDiv(20000, res_x, size.cx), -MulDiv(20000, res_y, size.cy));
203
204     SetLastError(0xdeadbeef);
205     ret = SetMapMode(hdc, MM_TEXT);
206     ok(ret == MM_LOMETRIC, "expected MM_LOMETRIC, got %d\n", ret);
207
208     expect_viewport_ext(hdc, 1, 1);
209     expect_window_ext(hdc, 1, 1);
210     expect_world_transform(hdc, 20.0, 20.0);
211     expect_LPtoDP(hdc, 20000, 20000);
212
213     size.cx = 0xdeadbeef;
214     size.cy = 0xdeadbeef;
215     ret = SetViewportExtEx(hdc, -1, -1, &size);
216     ok(ret, "SetViewportExtEx(-1, -1) failed\n");
217     ok(size.cx == 1 && size.cy == 1, "expected 1,1 got %d,%d\n", size.cx, size.cy);
218     expect_viewport_ext(hdc, 1, 1);
219     expect_window_ext(hdc, 1, 1);
220     expect_world_transform(hdc, 20.0, 20.0);
221     expect_LPtoDP(hdc, 20000, 20000);
222
223     ret = SetMapMode(hdc, MM_ANISOTROPIC);
224     ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
225
226     expect_viewport_ext(hdc, 1, 1);
227     expect_window_ext(hdc, 1, 1);
228     expect_world_transform(hdc, 20.0, 20.0);
229     expect_LPtoDP(hdc, 20000, 20000);
230
231     size.cx = 0xdeadbeef;
232     size.cy = 0xdeadbeef;
233     ret = SetViewportExtEx(hdc, -1, -1, &size);
234     ok(ret, "SetViewportExtEx(-1, -1) failed\n");
235     ok(size.cx == 1 && size.cy == 1, "expected 1,1 got %d,%d\n", size.cx, size.cy);
236     expect_viewport_ext(hdc, -1, -1);
237     expect_window_ext(hdc, 1, 1);
238     expect_world_transform(hdc, 20.0, 20.0);
239     expect_LPtoDP(hdc, -20000, -20000);
240
241     ret = SetGraphicsMode(hdc, GM_COMPATIBLE);
242     ok(ret, "SetGraphicsMode(GM_COMPATIBLE) should not fail if DC has't an identity transform\n");
243     ret = GetGraphicsMode(hdc);
244     ok(ret == GM_COMPATIBLE, "expected GM_COMPATIBLE, got %d\n", ret);
245
246     expect_viewport_ext(hdc, -1, -1);
247     expect_window_ext(hdc, 1, 1);
248     expect_world_transform(hdc, 20.0, 20.0);
249     expect_LPtoDP(hdc, -20000, -20000);
250
251     DeleteDC(hdc);
252 }
253
254 static void test_dc_layout(void)
255 {
256     INT ret, size_cx, size_cy, res_x, res_y, dpi_x, dpi_y;
257     SIZE size;
258     POINT pt;
259     HBITMAP bitmap;
260     RECT rc, ret_rc;
261     HDC hdc;
262     HRGN hrgn;
263
264     if (!pGetLayout || !pSetLayout)
265     {
266         win_skip( "Don't have SetLayout\n" );
267         return;
268     }
269
270     hdc = CreateCompatibleDC(0);
271     bitmap = CreateCompatibleBitmap( hdc, 100, 100 );
272     SelectObject( hdc, bitmap );
273
274     size_cx = GetDeviceCaps(hdc, HORZSIZE);
275     size_cy = GetDeviceCaps(hdc, VERTSIZE);
276     res_x = GetDeviceCaps(hdc, HORZRES);
277     res_y = GetDeviceCaps(hdc, VERTRES);
278     dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
279     dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
280
281     ret = GetMapMode( hdc );
282     ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
283     expect_viewport_ext(hdc, 1, 1);
284     expect_window_ext(hdc, 1, 1);
285     expect_world_transform(hdc, 1.0, 1.0);
286     expect_LPtoDP(hdc, 1000, 1000);
287
288     pSetLayout( hdc, LAYOUT_RTL );
289     if (!pGetLayout( hdc ))
290     {
291         win_skip( "SetLayout not supported\n" );
292         DeleteDC(hdc);
293         return;
294     }
295
296     ret = GetMapMode( hdc );
297     ok(ret == MM_ANISOTROPIC, "expected MM_ANISOTROPIC, got %d\n", ret);
298     expect_viewport_ext(hdc, 1, 1);
299     expect_window_ext(hdc, 1, 1);
300     expect_world_transform(hdc, 1.0, 1.0);
301     expect_LPtoDP(hdc, -1000 + 99, 1000);
302     GetViewportOrgEx( hdc, &pt );
303     ok( pt.x == 0 && pt.y == 0, "wrong origin %d,%d\n", pt.x, pt.y );
304     GetWindowOrgEx( hdc, &pt );
305     ok( pt.x == 0 && pt.y == 0, "wrong origin %d,%d\n", pt.x, pt.y );
306     GetDCOrgEx( hdc, &pt );
307     ok( pt.x == 0 && pt.y == 0, "wrong origin %d,%d\n", pt.x, pt.y );
308     if (pGetTransform)
309     {
310         XFORM xform;
311         BOOL ret = pGetTransform( hdc, 0x204, &xform ); /* World -> Device */
312         ok( ret, "got %d\n", ret );
313         ok( xform.eM11 == -1.0, "got %f\n", xform.eM11 );
314         ok( xform.eM12 == 0.0, "got %f\n", xform.eM12 );
315         ok( xform.eM21 == 0.0, "got %f\n", xform.eM21 );
316         ok( xform.eM22 == 1.0, "got %f\n", xform.eM22 );
317         ok( xform.eDx == 99.0, "got %f\n", xform.eDx );
318         ok( xform.eDy == 0.0, "got %f\n", xform.eDy );
319     }
320
321     SetRect( &rc, 10, 10, 20, 20 );
322     IntersectClipRect( hdc, 10, 10, 20, 20 );
323     hrgn = CreateRectRgn( 0, 0, 0, 0 );
324     GetClipRgn( hdc, hrgn );
325     GetRgnBox( hrgn, &ret_rc );
326     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
327         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
328     pSetLayout( hdc, LAYOUT_LTR );
329     SetRect( &rc, 80, 10, 90, 20 );
330     GetClipRgn( hdc, hrgn );
331     GetRgnBox( hrgn, &ret_rc );
332     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
333         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
334     GetClipBox( hdc, &ret_rc );
335     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
336         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
337     IntersectClipRect( hdc, 80, 10, 85, 20 );
338     pSetLayout( hdc, LAYOUT_RTL );
339     SetRect( &rc, 15, 10, 20, 20 );
340     GetClipRgn( hdc, hrgn );
341     GetRgnBox( hrgn, &ret_rc );
342     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
343         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
344     GetClipBox( hdc, &ret_rc );
345     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
346         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
347     SetRectRgn( hrgn, 60, 10, 80, 20 );
348     pSetLayout( hdc, LAYOUT_LTR );
349     ExtSelectClipRgn( hdc, hrgn, RGN_OR );
350     pSetLayout( hdc, LAYOUT_RTL );
351     SetRect( &rc, 15, 10, 40, 20 );
352     GetClipRgn( hdc, hrgn );
353     GetRgnBox( hrgn, &ret_rc );
354     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
355         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
356     GetClipBox( hdc, &ret_rc );
357     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
358         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
359
360     /* OffsetClipRgn mirrors too */
361     OffsetClipRgn( hdc, 5, 5 );
362     OffsetRect( &rc, 5, 5 );
363     GetClipRgn( hdc, hrgn );
364     GetRgnBox( hrgn, &ret_rc );
365     ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
366         ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
367
368     /* GetRandomRgn returns the raw region */
369     if (pGetRandomRgn)
370     {
371         SetRect( &rc, 55, 15, 80, 25 );
372         pGetRandomRgn( hdc, hrgn, 1 );
373         GetRgnBox( hrgn, &ret_rc );
374         ok( EqualRect( &rc, &ret_rc ), "wrong clip box %d,%d - %d,%d\n",
375             ret_rc.left, ret_rc.top, ret_rc.right, ret_rc.bottom );
376     }
377
378     SetMapMode(hdc, MM_LOMETRIC);
379     ret = GetMapMode( hdc );
380     ok(ret == MM_ANISOTROPIC, "expected MM_ANISOTROPIC, got %d\n", ret);
381
382     expect_viewport_ext(hdc, res_x, -res_y);
383     ok( GetWindowExtEx( hdc, &size ), "GetWindowExtEx failed\n" );
384     ok( rough_match( size.cx, size_cx * 10 ) ||
385         rough_match( size.cx, MulDiv( res_x, 254, dpi_x )),  /* Vista uses a more precise method */
386         "expected cx %d or %d, got %d\n", size_cx * 10, MulDiv( res_x, 254, dpi_x ), size.cx );
387     ok( rough_match( size.cy, size_cy * 10 ) ||
388         rough_match( size.cy, MulDiv( res_y, 254, dpi_y )),  /* Vista uses a more precise method */
389         "expected cy %d or %d, got %d\n", size_cy * 10, MulDiv( res_y, 254, dpi_y ), size.cy );
390     expect_world_transform(hdc, 1.0, 1.0);
391     expect_LPtoDP(hdc, -MulDiv(1000 / 10, res_x, size_cx) + 99, -MulDiv(1000 / 10, res_y, size_cy));
392
393     SetMapMode(hdc, MM_TEXT);
394     ret = GetMapMode( hdc );
395     ok(ret == MM_ANISOTROPIC, "expected MM_ANISOTROPIC, got %d\n", ret);
396     pSetLayout( hdc, LAYOUT_LTR );
397     ret = GetMapMode( hdc );
398     ok(ret == MM_ANISOTROPIC, "expected MM_ANISOTROPIC, got %d\n", ret);
399     SetMapMode(hdc, MM_TEXT);
400     ret = GetMapMode( hdc );
401     ok(ret == MM_TEXT, "expected MM_TEXT, got %d\n", ret);
402
403     DeleteDC(hdc);
404     DeleteObject( bitmap );
405 }
406
407 static void test_modify_world_transform(void)
408 {
409     HDC hdc = GetDC(0);
410     int ret;
411
412     ret = SetGraphicsMode(hdc, GM_ADVANCED);
413     if(!ret) /* running in win9x so quit */
414     {
415         ReleaseDC(0, hdc);
416         skip("GM_ADVANCED is not supported on this platform\n");
417         return;
418     }
419
420     ret = ModifyWorldTransform(hdc, NULL, MWT_IDENTITY);
421     ok(ret, "ret = %d\n", ret);
422
423     ret = ModifyWorldTransform(hdc, NULL, MWT_LEFTMULTIPLY);
424     ok(!ret, "ret = %d\n", ret);
425
426     ret = ModifyWorldTransform(hdc, NULL, MWT_RIGHTMULTIPLY);
427     ok(!ret, "ret = %d\n", ret);
428
429     ReleaseDC(0, hdc);
430 }
431
432 static void test_SetWindowExt(HDC hdc, LONG cx, LONG cy, LONG expected_vp_cx, LONG expected_vp_cy)
433 {
434     SIZE windowExt, viewportExt;
435     POINT windowOrg, windowOrgAfter, viewportOrg, viewportOrgAfter;
436
437     GetWindowOrgEx(hdc, &windowOrg);
438     GetViewportOrgEx(hdc, &viewportOrg);
439
440     SetWindowExtEx(hdc, cx, cy, NULL);
441     GetWindowExtEx(hdc, &windowExt);
442     ok(windowExt.cx == cx && windowExt.cy == cy,
443        "Window extension: Expected %dx%d, got %dx%d\n",
444        cx, cy, windowExt.cx, windowExt.cy);
445
446     GetViewportExtEx(hdc, &viewportExt);
447     ok(rough_match(viewportExt.cx, expected_vp_cx) && rough_match(viewportExt.cy, expected_vp_cy),
448         "Viewport extents have not been properly adjusted: Expected %dx%d, got %dx%d\n",
449         expected_vp_cx, expected_vp_cy, viewportExt.cx, viewportExt.cy);
450
451     GetWindowOrgEx(hdc, &windowOrgAfter);
452     ok(windowOrg.x == windowOrgAfter.x && windowOrg.y == windowOrgAfter.y,
453         "Window origin changed from (%d,%d) to (%d,%d)\n",
454         windowOrg.x, windowOrg.y, windowOrgAfter.x, windowOrgAfter.y);
455
456     GetViewportOrgEx(hdc, &viewportOrgAfter);
457     ok(viewportOrg.x == viewportOrgAfter.x && viewportOrg.y == viewportOrgAfter.y,
458         "Viewport origin changed from (%d,%d) to (%d,%d)\n",
459         viewportOrg.x, viewportOrg.y, viewportOrgAfter.x, viewportOrgAfter.y);
460 }
461
462 static void test_SetViewportExt(HDC hdc, LONG cx, LONG cy, LONG expected_vp_cx, LONG expected_vp_cy)
463 {
464     SIZE windowExt, windowExtAfter, viewportExt;
465     POINT windowOrg, windowOrgAfter, viewportOrg, viewportOrgAfter;
466
467     GetWindowOrgEx(hdc, &windowOrg);
468     GetViewportOrgEx(hdc, &viewportOrg);
469     GetWindowExtEx(hdc, &windowExt);
470
471     SetViewportExtEx(hdc, cx, cy, NULL);
472     GetViewportExtEx(hdc, &viewportExt);
473     ok(rough_match(viewportExt.cx, expected_vp_cx) && rough_match(viewportExt.cy, expected_vp_cy),
474         "Viewport extents have not been properly adjusted: Expected %dx%d, got %dx%d\n",
475         expected_vp_cx, expected_vp_cy, viewportExt.cx, viewportExt.cy);
476
477     GetWindowExtEx(hdc, &windowExtAfter);
478     ok(windowExt.cx == windowExtAfter.cx && windowExt.cy == windowExtAfter.cy,
479        "Window extension changed from %dx%d to %dx%d\n",
480        windowExt.cx, windowExt.cy, windowExtAfter.cx, windowExtAfter.cy);
481
482     GetWindowOrgEx(hdc, &windowOrgAfter);
483     ok(windowOrg.x == windowOrgAfter.x && windowOrg.y == windowOrgAfter.y,
484         "Window origin changed from (%d,%d) to (%d,%d)\n",
485         windowOrg.x, windowOrg.y, windowOrgAfter.x, windowOrgAfter.y);
486
487     GetViewportOrgEx(hdc, &viewportOrgAfter);
488     ok(viewportOrg.x == viewportOrgAfter.x && viewportOrg.y == viewportOrgAfter.y,
489         "Viewport origin changed from (%d,%d) to (%d,%d)\n",
490         viewportOrg.x, viewportOrg.y, viewportOrgAfter.x, viewportOrgAfter.y);
491 }
492
493 static void test_isotropic_mapping(void)
494 {
495     SIZE win, vp;
496     HDC hdc = GetDC(0);
497     
498     SetMapMode(hdc, MM_ISOTROPIC);
499     
500     /* MM_ISOTROPIC is set up like MM_LOMETRIC.
501        Initial values after SetMapMode():
502        (1 inch = 25.4 mm)
503        
504                        Windows 9x:               Windows NT:
505        Window Ext:     254 x -254                HORZSIZE*10 x VERTSIZE*10
506        Viewport Ext:   LOGPIXELSX x LOGPIXELSY   HORZRES x -VERTRES
507        
508        To test without rounding errors, we have to use multiples of
509        these values!
510      */
511     
512     GetWindowExtEx(hdc, &win);
513     GetViewportExtEx(hdc, &vp);
514     
515     test_SetViewportExt(hdc, 10 * vp.cx, 10 * vp.cy, 10 * vp.cx, 10 * vp.cy);
516     test_SetWindowExt(hdc, win.cx, win.cy, 10 * vp.cx, 10 * vp.cy);
517     test_SetWindowExt(hdc, 2 * win.cx, win.cy, 10 * vp.cx, 5 * vp.cy);
518     test_SetWindowExt(hdc, win.cx, win.cy, 5 * vp.cx, 5 * vp.cy);
519     test_SetViewportExt(hdc, 4 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
520     test_SetViewportExt(hdc, vp.cx, 2 * vp.cy, vp.cx, vp.cy);
521     test_SetViewportExt(hdc, 2 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
522     test_SetViewportExt(hdc, 4 * vp.cx, 2 * vp.cy, 2 * vp.cx, 2 * vp.cy);
523     test_SetWindowExt(hdc, 4 * win.cx, 2 * win.cy, 2 * vp.cx, vp.cy);
524     test_SetViewportExt(hdc, -2 * vp.cx, -4 * vp.cy, -2 * vp.cx, -vp.cy);
525     test_SetViewportExt(hdc, -2 * vp.cx, -1 * vp.cy, -2 * vp.cx, -vp.cy);    
526     test_SetWindowExt(hdc, -4 * win.cx, -2 * win.cy, -2 * vp.cx, -vp.cy);
527     test_SetWindowExt(hdc, 4 * win.cx, -4 * win.cy, -vp.cx, -vp.cy);
528     
529     ReleaseDC(0, hdc);
530 }
531
532 static void test_setvirtualresolution(void)
533 {
534     HDC hdc = CreateICA("DISPLAY", NULL, NULL, NULL);
535     DWORD r;
536     INT horz_res = GetDeviceCaps(hdc, HORZRES);
537     INT horz_size = GetDeviceCaps(hdc, HORZSIZE);
538     INT log_pixels_x = GetDeviceCaps(hdc, LOGPIXELSX);
539     SIZE orig_lometric_vp, orig_lometric_wnd;
540
541     if(!pSetVirtualResolution)
542     {
543         win_skip("Don't have SetVirtualResolution\n");
544         return;
545     }
546
547     /* Get the true resolution limits */
548     SetMapMode(hdc, MM_LOMETRIC);
549     GetViewportExtEx(hdc, &orig_lometric_vp);
550     GetWindowExtEx(hdc, &orig_lometric_wnd);
551     SetMapMode(hdc, MM_TEXT);
552
553     r = pSetVirtualResolution(hdc, 4000, 1000, 400, 200); /* 10 pix/mm x 5 pix/mm */
554     ok(r == TRUE, "got %d\n", r);
555     expect_LPtoDP(hdc, 1000, 1000);
556     expect_viewport_ext(hdc, 1, 1);
557     expect_window_ext(hdc, 1, 1);
558
559     SetMapMode(hdc, MM_LOMETRIC);
560     expect_LPtoDP(hdc, 1000, -500);
561     expect_viewport_ext(hdc, 4000, -1000);
562     expect_window_ext(hdc, 4000, 2000);
563
564     /* Doesn't change the device caps */
565     ok(horz_res == GetDeviceCaps(hdc, HORZRES), "horz_res changed\n");
566     ok(horz_size == GetDeviceCaps(hdc, HORZSIZE), "horz_size changed\n");
567     ok(log_pixels_x == GetDeviceCaps(hdc, LOGPIXELSX), "log_pixels_x changed\n");
568
569     r = pSetVirtualResolution(hdc, 8000, 1000, 400, 200); /* 20 pix/mm x 5 pix/mm */
570     ok(r == TRUE, "got %d\n", r);
571     expect_LPtoDP(hdc, 1000, -500); /* No change, need to re-set the mapping mode */
572     SetMapMode(hdc, MM_TEXT);
573     SetMapMode(hdc, MM_LOMETRIC);
574     expect_LPtoDP(hdc, 2000, -500);
575     expect_viewport_ext(hdc, 8000, -1000);
576     expect_window_ext(hdc, 4000, 2000);
577
578     r = pSetVirtualResolution(hdc, 8000, 1000, 200, 200); /* 40 pix/mm x 5 pix/mm */
579     ok(r == TRUE, "got %d\n", r);
580     SetMapMode(hdc, MM_TEXT);
581     SetMapMode(hdc, MM_LOMETRIC);
582     expect_LPtoDP(hdc, 4000, -500);
583     expect_viewport_ext(hdc, 8000, -1000);
584     expect_window_ext(hdc, 2000, 2000);
585
586     r = pSetVirtualResolution(hdc, 8000, 1000, 200, 200); /* 40 pix/mm x 5 pix/mm */
587     ok(r == TRUE, "got %d\n", r);
588     SetMapMode(hdc, MM_TEXT);
589     SetMapMode(hdc, MM_LOMETRIC);
590     expect_LPtoDP(hdc, 4000, -500);
591     expect_viewport_ext(hdc, 8000, -1000);
592     expect_window_ext(hdc, 2000, 2000);
593
594     r = pSetVirtualResolution(hdc, 8000, 2000, 200, 200); /* 40 pix/mm x 10 pix/mm */
595     ok(r == TRUE, "got %d\n", r);
596     SetMapMode(hdc, MM_TEXT);
597     SetMapMode(hdc, MM_LOMETRIC);
598     expect_LPtoDP(hdc, 4000, -1000);
599     expect_viewport_ext(hdc, 8000, -2000);
600     expect_window_ext(hdc, 2000, 2000);
601
602     r = pSetVirtualResolution(hdc, 0, 0, 10, 0); /* Error */
603     ok(r == FALSE, "got %d\n", r);
604     SetMapMode(hdc, MM_TEXT);
605     SetMapMode(hdc, MM_LOMETRIC);
606     expect_LPtoDP(hdc, 4000, -1000);
607     expect_viewport_ext(hdc, 8000, -2000);
608     expect_window_ext(hdc, 2000, 2000);
609
610     r = pSetVirtualResolution(hdc, 0, 0, 0, 0); /* Reset to true resolution */
611     ok(r == TRUE, "got %d\n", r);
612     SetMapMode(hdc, MM_TEXT);
613     SetMapMode(hdc, MM_LOMETRIC);
614     expect_viewport_ext(hdc, orig_lometric_vp.cx, orig_lometric_vp.cy);
615     expect_window_ext(hdc, orig_lometric_wnd.cx, orig_lometric_wnd.cy);
616
617     DeleteDC(hdc);
618 }
619
620
621 static inline void expect_identity(int line, XFORM *xf)
622 {
623     ok(xf->eM11 == 1.0, "%d: got %f\n", line, xf->eM11);
624     ok(xf->eM12 == 0.0, "%d: got %f\n", line, xf->eM12);
625     ok(xf->eM21 == 0.0, "%d: got %f\n", line, xf->eM21);
626     ok(xf->eM22 == 1.0, "%d: got %f\n", line, xf->eM22);
627     ok(xf->eDx == 0.0, "%d: got %f\n", line, xf->eDx);
628     ok(xf->eDy == 0.0, "%d: got %f\n", line, xf->eDy);
629 }
630
631 static inline void xform_near_match(int line, XFORM *got, XFORM *expect)
632 {
633     ok(fabs(got->eM11 - expect->eM11) < 0.001, "%d: got %f expect %f\n", line, got->eM11, expect->eM11);
634     ok(fabs(got->eM12 - expect->eM12) < 0.001, "%d: got %f expect %f\n", line, got->eM12, expect->eM12);
635     ok(fabs(got->eM21 - expect->eM21) < 0.001, "%d: got %f expect %f\n", line, got->eM21, expect->eM21);
636     ok(fabs(got->eM22 - expect->eM22) < 0.001, "%d: got %f expect %f\n", line, got->eM22, expect->eM22);
637     ok(fabs(got->eDx - expect->eDx) < 0.001, "%d: got %f expect %f\n", line, got->eDx, expect->eDx);
638     ok(fabs(got->eDy - expect->eDy) < 0.001, "%d: got %f expect %f\n", line, got->eDy, expect->eDy);
639 }
640
641
642 static void test_gettransform(void)
643 {
644     HDC hdc = CreateICA("DISPLAY", NULL, NULL, NULL);
645     XFORM xform, expect;
646     BOOL r;
647     SIZE lometric_vp, lometric_wnd;
648
649     if(!pGetTransform)
650     {
651         win_skip("Don't have GetTransform\n");
652         return;
653     }
654
655     r = pGetTransform(hdc, 0x203, &xform); /* World -> Page */
656     ok(r == TRUE, "got %d\n", r);
657     expect_identity(__LINE__, &xform);
658     r = pGetTransform(hdc, 0x304, &xform); /* Page -> Device */
659     ok(r == TRUE, "got %d\n", r);
660     expect_identity(__LINE__, &xform);
661     r = pGetTransform(hdc, 0x204, &xform); /* World -> Device */
662     ok(r == TRUE, "got %d\n", r);
663     expect_identity(__LINE__, &xform);
664     r = pGetTransform(hdc, 0x402, &xform); /* Device -> World */
665     ok(r == TRUE, "got %d\n", r);
666     expect_identity(__LINE__, &xform);
667
668     SetMapMode(hdc, MM_LOMETRIC);
669     GetViewportExtEx(hdc, &lometric_vp);
670     GetWindowExtEx(hdc, &lometric_wnd);
671
672     r = pGetTransform(hdc, 0x203, &xform); /* World -> Page */
673     ok(r == TRUE, "got %d\n", r);
674     expect_identity(__LINE__, &xform);
675
676     r = pGetTransform(hdc, 0x304, &xform); /* Page -> Device */
677     ok(r == TRUE, "got %d\n", r);
678     expect.eM11 = (FLOAT) lometric_vp.cx / lometric_wnd.cx;
679     expect.eM12 = expect.eM21 = 0.0;
680     expect.eM22 = (FLOAT) lometric_vp.cy / lometric_wnd.cy;
681     expect.eDx = expect.eDy = 0.0;
682     xform_near_match(__LINE__, &xform, &expect);
683
684     r = pGetTransform(hdc, 0x204, &xform);  /* World -> Device */
685     ok(r == TRUE, "got %d\n", r);
686     xform_near_match(__LINE__, &xform, &expect);
687
688     r = pGetTransform(hdc, 0x402, &xform); /* Device -> World */
689     ok(r == TRUE, "got %d\n", r);
690     expect.eM11 = (FLOAT) lometric_wnd.cx / lometric_vp.cx;
691     expect.eM22 = (FLOAT) lometric_wnd.cy / lometric_vp.cy;
692     xform_near_match(__LINE__, &xform, &expect);
693
694
695     SetGraphicsMode(hdc, GM_ADVANCED);
696
697     expect.eM11 = 10.0;
698     expect.eM22 = 20.0;
699     SetWorldTransform(hdc, &expect);
700     r = pGetTransform(hdc, 0x203, &xform);  /* World -> Page */
701     ok(r == TRUE, "got %d\n", r);
702     xform_near_match(__LINE__, &xform, &expect);
703
704     r = pGetTransform(hdc, 0x304, &xform); /* Page -> Device */
705     ok(r == TRUE, "got %d\n", r);
706     expect.eM11 = (FLOAT) lometric_vp.cx / lometric_wnd.cx;
707     expect.eM22 = (FLOAT) lometric_vp.cy / lometric_wnd.cy;
708     xform_near_match(__LINE__, &xform, &expect);
709
710     r = pGetTransform(hdc, 0x204, &xform); /* World -> Device */
711     ok(r == TRUE, "got %d\n", r);
712     expect.eM11 *= 10.0;
713     expect.eM22 *= 20.0;
714     xform_near_match(__LINE__, &xform, &expect);
715
716     r = pGetTransform(hdc, 0x402, &xform); /* Device -> World */
717     ok(r == TRUE, "got %d\n", r);
718     expect.eM11 = 1 / expect.eM11;
719     expect.eM22 = 1 / expect.eM22;
720     xform_near_match(__LINE__, &xform, &expect);
721
722     r = pGetTransform(hdc, 0x102, &xform);
723     ok(r == FALSE, "got %d\n", r);
724     r = pGetTransform(hdc, 0x103, &xform);
725     ok(r == FALSE, "got %d\n", r);
726     r = pGetTransform(hdc, 0x104, &xform);
727     ok(r == FALSE, "got %d\n", r);
728     r = pGetTransform(hdc, 0x202, &xform);
729     ok(r == FALSE, "got %d\n", r);
730     r = pGetTransform(hdc, 0x302, &xform);
731     ok(r == FALSE, "got %d\n", r);
732     r = pGetTransform(hdc, 0x303, &xform);
733     ok(r == FALSE, "got %d\n", r);
734     r = pGetTransform(hdc, 0x403, &xform);
735     ok(r == FALSE, "got %d\n", r);
736     r = pGetTransform(hdc, 0x404, &xform);
737     ok(r == FALSE, "got %d\n", r);
738     r = pGetTransform(hdc, 0xffff, &xform);
739     ok(r == FALSE, "got %d\n", r);
740 }
741
742 START_TEST(mapping)
743 {
744     HMODULE mod = GetModuleHandleA("gdi32.dll");
745     pGetLayout = (void *)GetProcAddress( mod, "GetLayout" );
746     pSetLayout = (void *)GetProcAddress( mod, "SetLayout" );
747     pGetRandomRgn = (void *)GetProcAddress( mod, "GetRandomRgn" );
748     pGetTransform = (void *)GetProcAddress( mod, "GetTransform" );
749     pSetVirtualResolution = (void *)GetProcAddress( mod, "SetVirtualResolution" );
750
751     test_modify_world_transform();
752     test_world_transform();
753     test_dc_layout();
754     test_isotropic_mapping();
755     test_setvirtualresolution();
756     test_gettransform();
757 }