dc.c 54.5 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * GDI Device Context functions
 *
 * Copyright 1993 Alexandre Julliard
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21 22
#include "config.h"

23
#include <assert.h>
24
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
25
#include <stdlib.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <string.h>
27
#include "windef.h"
28
#include "winbase.h"
29
#include "wingdi.h"
30
#include "winreg.h"
31
#include "winternl.h"
32
#include "winerror.h"
33
#include "gdi_private.h"
34
#include "wine/unicode.h"
35
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
36

37
WINE_DEFAULT_DEBUG_CHANNEL(dc);
38

39 40
static const WCHAR displayW[] = { 'd','i','s','p','l','a','y',0 };

41
static BOOL DC_DeleteObject( HGDIOBJ handle );
42 43 44 45 46 47 48 49 50 51

static const struct gdi_obj_funcs dc_funcs =
{
    NULL,             /* pSelectObject */
    NULL,             /* pGetObjectA */
    NULL,             /* pGetObjectW */
    NULL,             /* pUnrealizeObject */
    DC_DeleteObject   /* pDeleteObject */
};

52 53 54

static inline DC *get_dc_obj( HDC hdc )
{
55 56
    WORD type;
    DC *dc = get_any_obj_ptr( hdc, &type );
57 58
    if (!dc) return NULL;

59
    switch (type)
60
    {
61 62 63 64 65 66
    case OBJ_DC:
    case OBJ_MEMDC:
    case OBJ_METADC:
    case OBJ_ENHMETADC:
        return dc;
    default:
67 68
        GDI_ReleaseObj( hdc );
        SetLastError( ERROR_INVALID_HANDLE );
69
        return NULL;
70 71 72 73
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
74
/***********************************************************************
75
 *           set_initial_dc_state
Alexandre Julliard's avatar
Alexandre Julliard committed
76
 */
77
static void set_initial_dc_state( DC *dc )
Alexandre Julliard's avatar
Alexandre Julliard committed
78
{
79 80 81 82 83 84 85 86
    dc->wnd_org.x           = 0;
    dc->wnd_org.y           = 0;
    dc->wnd_ext.cx          = 1;
    dc->wnd_ext.cy          = 1;
    dc->vport_org.x         = 0;
    dc->vport_org.y         = 0;
    dc->vport_ext.cx        = 1;
    dc->vport_ext.cy        = 1;
87
    dc->miterLimit          = 10.0f; /* 10.0 is the default, from MSDN */
88
    dc->layout              = 0;
89
    dc->font_code_page      = CP_ACP;
90 91 92 93 94 95
    dc->ROPmode             = R2_COPYPEN;
    dc->polyFillMode        = ALTERNATE;
    dc->stretchBltMode      = BLACKONWHITE;
    dc->relAbsMode          = ABSOLUTE;
    dc->backgroundMode      = OPAQUE;
    dc->backgroundColor     = RGB( 255, 255, 255 );
96 97
    dc->dcBrushColor        = RGB( 255, 255, 255 );
    dc->dcPenColor          = RGB( 0, 0, 0 );
98
    dc->textColor           = RGB( 0, 0, 0 );
99 100
    dc->brush_org.x         = 0;
    dc->brush_org.y         = 0;
101
    dc->mapperFlags         = 0;
102
    dc->textAlign           = TA_LEFT | TA_TOP | TA_NOUPDATECP;
103 104 105
    dc->charExtra           = 0;
    dc->breakExtra          = 0;
    dc->breakRem            = 0;
106 107
    dc->MapMode             = MM_TEXT;
    dc->GraphicsMode        = GM_COMPATIBLE;
108 109
    dc->cur_pos.x           = 0;
    dc->cur_pos.y           = 0;
110 111 112 113 114 115 116 117 118 119
    dc->ArcDirection        = AD_COUNTERCLOCKWISE;
    dc->xformWorld2Wnd.eM11 = 1.0f;
    dc->xformWorld2Wnd.eM12 = 0.0f;
    dc->xformWorld2Wnd.eM21 = 0.0f;
    dc->xformWorld2Wnd.eM22 = 1.0f;
    dc->xformWorld2Wnd.eDx  = 0.0f;
    dc->xformWorld2Wnd.eDy  = 0.0f;
    dc->xformWorld2Vport    = dc->xformWorld2Wnd;
    dc->xformVport2World    = dc->xformWorld2Wnd;
    dc->vport2WorldValid    = TRUE;
120

121
    reset_bounds( &dc->bounds );
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

/***********************************************************************
 *           alloc_dc_ptr
 */
DC *alloc_dc_ptr( WORD magic )
{
    DC *dc;

    if (!(dc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dc) ))) return NULL;

    dc->nulldrv.funcs       = &null_driver;
    dc->physDev             = &dc->nulldrv;
    dc->thread              = GetCurrentThreadId();
    dc->refcount            = 1;
    dc->hPen                = GDI_inc_ref_count( GetStockObject( BLACK_PEN ));
    dc->hBrush              = GDI_inc_ref_count( GetStockObject( WHITE_BRUSH ));
    dc->hFont               = GDI_inc_ref_count( GetStockObject( SYSTEM_FONT ));
    dc->hPalette            = GetStockObject( DEFAULT_PALETTE );

    set_initial_dc_state( dc );
143

144
    if (!(dc->hSelf = alloc_gdi_handle( dc, magic, &dc_funcs )))
145 146
    {
        HeapFree( GetProcessHeap(), 0, dc );
147
        return NULL;
148
    }
149
    dc->nulldrv.hdc = dc->hSelf;
150 151 152 153 154 155

    if (font_driver && !font_driver->pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
    {
        free_dc_ptr( dc );
        return NULL;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
156 157 158 159
    return dc;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
160

161
/***********************************************************************
162
 *           free_dc_state
163
 */
164
static void free_dc_state( DC *dc )
165
{
166 167 168
    if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
    if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
    if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
169
    if (dc->region) DeleteObject( dc->region );
170
    if (dc->path) free_gdi_path( dc->path );
171
    HeapFree( GetProcessHeap(), 0, dc->font_gamma_ramp );
172 173 174 175 176 177 178 179 180 181
    HeapFree( GetProcessHeap(), 0, dc );
}


/***********************************************************************
 *           free_dc_ptr
 */
void free_dc_ptr( DC *dc )
{
    assert( dc->refcount == 1 );
182

183 184
    while (dc->physDev != &dc->nulldrv)
    {
185 186
        PHYSDEV physdev = dc->physDev;
        dc->physDev = physdev->next;
187 188
        physdev->funcs->pDeleteDC( physdev );
    }
189 190 191 192
    GDI_dec_ref_count( dc->hPen );
    GDI_dec_ref_count( dc->hBrush );
    GDI_dec_ref_count( dc->hFont );
    if (dc->hBitmap) GDI_dec_ref_count( dc->hBitmap );
193 194
    free_gdi_handle( dc->hSelf );
    free_dc_state( dc );
195 196 197
}


198 199 200 201 202 203 204 205 206
/***********************************************************************
 *           get_dc_ptr
 *
 * Retrieve a DC pointer but release the GDI lock.
 */
DC *get_dc_ptr( HDC hdc )
{
    DC *dc = get_dc_obj( hdc );
    if (!dc) return NULL;
207 208 209 210 211
    if (dc->disabled)
    {
        GDI_ReleaseObj( hdc );
        return NULL;
    }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

    if (!InterlockedCompareExchange( &dc->refcount, 1, 0 ))
    {
        dc->thread = GetCurrentThreadId();
    }
    else if (dc->thread != GetCurrentThreadId())
    {
        WARN( "dc %p belongs to thread %04x\n", hdc, dc->thread );
        GDI_ReleaseObj( hdc );
        return NULL;
    }
    else InterlockedIncrement( &dc->refcount );

    GDI_ReleaseObj( hdc );
    return dc;
}


/***********************************************************************
 *           release_dc_ptr
 */
void release_dc_ptr( DC *dc )
{
    LONG ref;

    dc->thread = 0;
    ref = InterlockedDecrement( &dc->refcount );
    assert( ref >= 0 );
    if (ref) dc->thread = GetCurrentThreadId();  /* we still own it */
}


/***********************************************************************
 *           update_dc
 *
 * Make sure the DC vis region is up to date.
 * This function may call up to USER so the GDI lock should _not_
 * be held when calling it.
 */
void update_dc( DC *dc )
{
253 254
    if (InterlockedExchange( &dc->dirty, 0 ) && dc->hookProc)
        dc->hookProc( dc->hSelf, DCHC_INVALIDVISRGN, dc->dwHookData, 0 );
255 256 257
}


258 259 260
/***********************************************************************
 *           DC_DeleteObject
 */
261
static BOOL DC_DeleteObject( HGDIOBJ handle )
262 263 264 265 266
{
    return DeleteDC( handle );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
267
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
268
 *           DC_InitDC
Alexandre Julliard's avatar
Alexandre Julliard committed
269
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
270
 * Setup device-specific DC values for a newly created DC.
Alexandre Julliard's avatar
Alexandre Julliard committed
271
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
272
void DC_InitDC( DC* dc )
Alexandre Julliard's avatar
Alexandre Julliard committed
273
{
274 275
    PHYSDEV physdev = GET_DC_PHYSDEV( dc, pRealizeDefaultPalette );
    physdev->funcs->pRealizeDefaultPalette( physdev );
276 277 278 279 280
    SetTextColor( dc->hSelf, dc->textColor );
    SetBkColor( dc->hSelf, dc->backgroundColor );
    SelectObject( dc->hSelf, dc->hPen );
    SelectObject( dc->hSelf, dc->hBrush );
    SelectObject( dc->hSelf, dc->hFont );
281
    update_dc_clipping( dc );
282
    SetVirtualResolution( dc->hSelf, 0, 0, 0, 0 );
283 284
    physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
    physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
285 286 287
}


Alexandre Julliard's avatar
Alexandre Julliard committed
288 289 290 291 292 293 294
/***********************************************************************
 *           DC_InvertXform
 *
 * Computes the inverse of the transformation xformSrc and stores it to
 * xformDest. Returns TRUE if successful or FALSE if the xformSrc matrix
 * is singular.
 */
295
static BOOL DC_InvertXform( const XFORM *xformSrc, XFORM *xformDest )
Alexandre Julliard's avatar
Alexandre Julliard committed
296
{
297
    double determinant;
298

Alexandre Julliard's avatar
Alexandre Julliard committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    determinant = xformSrc->eM11*xformSrc->eM22 -
        xformSrc->eM12*xformSrc->eM21;
    if (determinant > -1e-12 && determinant < 1e-12)
        return FALSE;

    xformDest->eM11 =  xformSrc->eM22 / determinant;
    xformDest->eM12 = -xformSrc->eM12 / determinant;
    xformDest->eM21 = -xformSrc->eM21 / determinant;
    xformDest->eM22 =  xformSrc->eM11 / determinant;
    xformDest->eDx  = -xformSrc->eDx * xformDest->eM11 -
                       xformSrc->eDy * xformDest->eM21;
    xformDest->eDy  = -xformSrc->eDx * xformDest->eM12 -
                       xformSrc->eDy * xformDest->eM22;

    return TRUE;
}

316 317 318 319
/* Construct a transformation to do the window-to-viewport conversion */
static void construct_window_to_viewport(DC *dc, XFORM *xform)
{
    double scaleX, scaleY;
320 321
    scaleX = (double)dc->vport_ext.cx / (double)dc->wnd_ext.cx;
    scaleY = (double)dc->vport_ext.cy / (double)dc->wnd_ext.cy;
322

323
    if (dc->layout & LAYOUT_RTL) scaleX = -scaleX;
324 325 326 327
    xform->eM11 = scaleX;
    xform->eM12 = 0.0;
    xform->eM21 = 0.0;
    xform->eM22 = scaleY;
328 329
    xform->eDx  = (double)dc->vport_org.x - scaleX * (double)dc->wnd_org.x;
    xform->eDy  = (double)dc->vport_org.y - scaleY * (double)dc->wnd_org.y;
330
    if (dc->layout & LAYOUT_RTL) xform->eDx = dc->vis_rect.right - dc->vis_rect.left - 1 - xform->eDx;
331
}
Alexandre Julliard's avatar
Alexandre Julliard committed
332

333 334 335 336 337 338 339 340 341 342 343
/***********************************************************************
 *        linear_xform_cmp
 *
 * Compares the linear transform portion of two XFORMs (i.e. the 2x2 submatrix).
 * Returns 0 if they match.
 */
static inline int linear_xform_cmp( const XFORM *a, const XFORM *b )
{
    return memcmp( a, b, FIELD_OFFSET( XFORM, eDx ) );
}

Alexandre Julliard's avatar
Alexandre Julliard committed
344 345 346 347 348 349 350 351 352 353 354 355
/***********************************************************************
 *           DC_UpdateXforms
 *
 * Updates the xformWorld2Vport, xformVport2World and vport2WorldValid
 * fields of the specified DC by creating a transformation that
 * represents the current mapping mode and combining it with the DC's
 * world transform. This function should be called whenever the
 * parameters associated with the mapping mode (window and viewport
 * extents and origins) or the world transform change.
 */
void DC_UpdateXforms( DC *dc )
{
356
    XFORM xformWnd2Vport, oldworld2vport;
357

358
    construct_window_to_viewport(dc, &xformWnd2Vport);
Alexandre Julliard's avatar
Alexandre Julliard committed
359

360
    oldworld2vport = dc->xformWorld2Vport;
Alexandre Julliard's avatar
Alexandre Julliard committed
361
    /* Combine with the world transformation */
362
    CombineTransform( &dc->xformWorld2Vport, &dc->xformWorld2Wnd,
Alexandre Julliard's avatar
Alexandre Julliard committed
363 364 365
        &xformWnd2Vport );

    /* Create inverse of world-to-viewport transformation */
366 367
    dc->vport2WorldValid = DC_InvertXform( &dc->xformWorld2Vport,
        &dc->xformVport2World );
368

369
    /* Reselect the font and pen back into the dc so that the size
370
       gets updated. */
371
    if (linear_xform_cmp( &oldworld2vport, &dc->xformWorld2Vport ) &&
372
        !GdiIsMetaFileDC(dc->hSelf))
373
    {
374 375
        SelectObject(dc->hSelf, dc->hFont);
        SelectObject(dc->hSelf, dc->hPen);
376
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
377 378 379
}


Alexandre Julliard's avatar
Alexandre Julliard committed
380
/***********************************************************************
381
 *           nulldrv_SaveDC
Alexandre Julliard's avatar
Alexandre Julliard committed
382
 */
383
INT nulldrv_SaveDC( PHYSDEV dev )
Alexandre Julliard's avatar
Alexandre Julliard committed
384
{
385
    DC *newdc, *dc = get_nulldrv_dc( dev );
386

387
    if (!(newdc = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*newdc )))) return 0;
388
    newdc->layout           = dc->layout;
389 390 391 392 393
    newdc->hPen             = dc->hPen;
    newdc->hBrush           = dc->hBrush;
    newdc->hFont            = dc->hFont;
    newdc->hBitmap          = dc->hBitmap;
    newdc->hPalette         = dc->hPalette;
394 395 396 397 398 399 400
    newdc->ROPmode          = dc->ROPmode;
    newdc->polyFillMode     = dc->polyFillMode;
    newdc->stretchBltMode   = dc->stretchBltMode;
    newdc->relAbsMode       = dc->relAbsMode;
    newdc->backgroundMode   = dc->backgroundMode;
    newdc->backgroundColor  = dc->backgroundColor;
    newdc->textColor        = dc->textColor;
401 402
    newdc->dcBrushColor     = dc->dcBrushColor;
    newdc->dcPenColor       = dc->dcPenColor;
403
    newdc->brush_org        = dc->brush_org;
404
    newdc->mapperFlags      = dc->mapperFlags;
405 406 407 408 409 410
    newdc->textAlign        = dc->textAlign;
    newdc->charExtra        = dc->charExtra;
    newdc->breakExtra       = dc->breakExtra;
    newdc->breakRem         = dc->breakRem;
    newdc->MapMode          = dc->MapMode;
    newdc->GraphicsMode     = dc->GraphicsMode;
411
    newdc->cur_pos          = dc->cur_pos;
412 413 414 415 416
    newdc->ArcDirection     = dc->ArcDirection;
    newdc->xformWorld2Wnd   = dc->xformWorld2Wnd;
    newdc->xformWorld2Vport = dc->xformWorld2Vport;
    newdc->xformVport2World = dc->xformVport2World;
    newdc->vport2WorldValid = dc->vport2WorldValid;
417 418 419 420
    newdc->wnd_org          = dc->wnd_org;
    newdc->wnd_ext          = dc->wnd_ext;
    newdc->vport_org        = dc->vport_org;
    newdc->vport_ext        = dc->vport_ext;
421 422
    newdc->virtual_res      = dc->virtual_res;
    newdc->virtual_size     = dc->virtual_size;
Alexandre Julliard's avatar
Alexandre Julliard committed
423

Alexandre Julliard's avatar
Alexandre Julliard committed
424 425
    /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */

426
    if (dc->hClipRgn)
Alexandre Julliard's avatar
Alexandre Julliard committed
427
    {
428 429
        newdc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
        CombineRgn( newdc->hClipRgn, dc->hClipRgn, 0, RGN_COPY );
Alexandre Julliard's avatar
Alexandre Julliard committed
430
    }
431 432 433 434 435
    if (dc->hMetaRgn)
    {
        newdc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
        CombineRgn( newdc->hMetaRgn, dc->hMetaRgn, 0, RGN_COPY );
    }
436

437
    if (!PATH_SavePath( newdc, dc ))
438 439
    {
        release_dc_ptr( dc );
440
        free_dc_state( newdc );
441 442 443 444
	return 0;
    }

    newdc->saved_dc = dc->saved_dc;
445 446
    dc->saved_dc = newdc;
    return ++dc->saveLevel;
Alexandre Julliard's avatar
Alexandre Julliard committed
447 448 449 450
}


/***********************************************************************
451
 *           nulldrv_RestoreDC
Alexandre Julliard's avatar
Alexandre Julliard committed
452
 */
453
BOOL nulldrv_RestoreDC( PHYSDEV dev, INT level )
Alexandre Julliard's avatar
Alexandre Julliard committed
454
{
455
    DC *dcs, *first_dcs, *dc = get_nulldrv_dc( dev );
456 457 458 459
    INT save_level;

    /* find the state level to restore */

460
    if (abs(level) > dc->saveLevel || level == 0) return FALSE;
461 462
    if (level < 0) level = dc->saveLevel + level + 1;
    first_dcs = dc->saved_dc;
463 464
    for (dcs = first_dcs, save_level = dc->saveLevel; save_level > level; save_level--)
        dcs = dcs->saved_dc;
465 466

    /* restore the state */
467

468
    if (!PATH_RestorePath( dc, dcs )) return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
469

470
    dc->layout           = dcs->layout;
471 472 473 474 475 476 477
    dc->ROPmode          = dcs->ROPmode;
    dc->polyFillMode     = dcs->polyFillMode;
    dc->stretchBltMode   = dcs->stretchBltMode;
    dc->relAbsMode       = dcs->relAbsMode;
    dc->backgroundMode   = dcs->backgroundMode;
    dc->backgroundColor  = dcs->backgroundColor;
    dc->textColor        = dcs->textColor;
478 479
    dc->dcBrushColor     = dcs->dcBrushColor;
    dc->dcPenColor       = dcs->dcPenColor;
480
    dc->brush_org        = dcs->brush_org;
481
    dc->mapperFlags      = dcs->mapperFlags;
482 483 484 485 486 487
    dc->textAlign        = dcs->textAlign;
    dc->charExtra        = dcs->charExtra;
    dc->breakExtra       = dcs->breakExtra;
    dc->breakRem         = dcs->breakRem;
    dc->MapMode          = dcs->MapMode;
    dc->GraphicsMode     = dcs->GraphicsMode;
488
    dc->cur_pos          = dcs->cur_pos;
489 490 491 492 493
    dc->ArcDirection     = dcs->ArcDirection;
    dc->xformWorld2Wnd   = dcs->xformWorld2Wnd;
    dc->xformWorld2Vport = dcs->xformWorld2Vport;
    dc->xformVport2World = dcs->xformVport2World;
    dc->vport2WorldValid = dcs->vport2WorldValid;
494 495 496 497
    dc->wnd_org          = dcs->wnd_org;
    dc->wnd_ext          = dcs->wnd_ext;
    dc->vport_org        = dcs->vport_org;
    dc->vport_ext        = dcs->vport_ext;
498 499
    dc->virtual_res      = dcs->virtual_res;
    dc->virtual_size     = dcs->virtual_size;
500 501

    if (dcs->hClipRgn)
502
    {
503 504
        if (!dc->hClipRgn) dc->hClipRgn = CreateRectRgn( 0, 0, 0, 0 );
        CombineRgn( dc->hClipRgn, dcs->hClipRgn, 0, RGN_COPY );
505 506
    }
    else
507
    {
508
        if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
509
        dc->hClipRgn = 0;
510
    }
511 512 513 514 515 516 517 518 519 520
    if (dcs->hMetaRgn)
    {
        if (!dc->hMetaRgn) dc->hMetaRgn = CreateRectRgn( 0, 0, 0, 0 );
        CombineRgn( dc->hMetaRgn, dcs->hMetaRgn, 0, RGN_COPY );
    }
    else
    {
        if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
        dc->hMetaRgn = 0;
    }
521
    DC_UpdateXforms( dc );
522
    update_dc_clipping( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
523

524 525 526 527 528 529 530
    SelectObject( dev->hdc, dcs->hBitmap );
    SelectObject( dev->hdc, dcs->hBrush );
    SelectObject( dev->hdc, dcs->hFont );
    SelectObject( dev->hdc, dcs->hPen );
    SetBkColor( dev->hdc, dcs->backgroundColor);
    SetTextColor( dev->hdc, dcs->textColor);
    GDISelectPalette( dev->hdc, dcs->hPalette, FALSE );
531 532 533 534 535 536 537 538 539

    dc->saved_dc  = dcs->saved_dc;
    dcs->saved_dc = 0;
    dc->saveLevel = save_level - 1;

    /* now destroy all the saved DCs */

    while (first_dcs)
    {
540 541 542
        DC *next = first_dcs->saved_dc;
        free_dc_state( first_dcs );
        first_dcs = next;
543 544
    }
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
545 546 547
}


548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
/***********************************************************************
 *           reset_dc_state
 */
static BOOL reset_dc_state( HDC hdc )
{
    DC *dc, *dcs, *next;

    if (!(dc = get_dc_ptr( hdc ))) return FALSE;

    set_initial_dc_state( dc );
    SetBkColor( hdc, RGB( 255, 255, 255 ));
    SetTextColor( hdc, RGB( 0, 0, 0 ));
    SelectObject( hdc, GetStockObject( WHITE_BRUSH ));
    SelectObject( hdc, GetStockObject( SYSTEM_FONT ));
    SelectObject( hdc, GetStockObject( BLACK_PEN ));
    SetVirtualResolution( hdc, 0, 0, 0, 0 );
    GDISelectPalette( hdc, GetStockObject( DEFAULT_PALETTE ), FALSE );
    SetBoundsRect( hdc, NULL, DCB_DISABLE );
    AbortPath( hdc );

    if (dc->hClipRgn) DeleteObject( dc->hClipRgn );
    if (dc->hMetaRgn) DeleteObject( dc->hMetaRgn );
    dc->hClipRgn = 0;
    dc->hMetaRgn = 0;
    update_dc_clipping( dc );

    for (dcs = dc->saved_dc; dcs; dcs = next)
    {
        next = dcs->saved_dc;
        free_dc_state( dcs );
    }
    dc->saved_dc = NULL;
    dc->saveLevel = 0;
    release_dc_ptr( dc );
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
586
/***********************************************************************
587
 *           SaveDC    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
588
 */
589
INT WINAPI SaveDC( HDC hdc )
Alexandre Julliard's avatar
Alexandre Julliard committed
590
{
591
    DC * dc;
592
    INT ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
593

594 595 596 597 598 599
    if ((dc = get_dc_ptr( hdc )))
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSaveDC );
        ret = physdev->funcs->pSaveDC( physdev );
        release_dc_ptr( dc );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
600
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
601 602 603
}


Alexandre Julliard's avatar
Alexandre Julliard committed
604
/***********************************************************************
605
 *           RestoreDC    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
606
 */
607
BOOL WINAPI RestoreDC( HDC hdc, INT level )
Alexandre Julliard's avatar
Alexandre Julliard committed
608
{
609
    PHYSDEV physdev;
610
    DC *dc;
611
    BOOL success = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
612

613
    TRACE("%p %d\n", hdc, level );
614 615 616
    if ((dc = get_dc_ptr( hdc )))
    {
        update_dc( dc );
617
        physdev = GET_DC_PHYSDEV( dc, pRestoreDC );
618 619 620
        success = physdev->funcs->pRestoreDC( physdev, level );
        release_dc_ptr( dc );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
621
    return success;
Alexandre Julliard's avatar
Alexandre Julliard committed
622 623 624
}


625
/***********************************************************************
626
 *           CreateDCW    (GDI32.@)
627
 */
628 629
HDC WINAPI CreateDCW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
                      const DEVMODEW *initData )
Alexandre Julliard's avatar
Alexandre Julliard committed
630
{
631
    HDC hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
632
    DC * dc;
633
    const struct gdi_dc_funcs *funcs;
634
    WCHAR buf[300];
Alexandre Julliard's avatar
Alexandre Julliard committed
635

636 637
    GDI_CheckNotLock();

638
    if (!device || !DRIVER_GetDriverName( device, buf, 300 ))
639
    {
640 641 642 643 644
        if (!driver)
        {
            ERR( "no device found for %s\n", debugstr_w(device) );
            return 0;
        }
645
        strcpyW(buf, driver);
646
    }
647

648
    if (!(funcs = DRIVER_load_driver( buf )))
649
    {
650
        ERR( "no driver found for %s\n", debugstr_w(buf) );
651 652
        return 0;
    }
653
    if (!(dc = alloc_dc_ptr( OBJ_DC ))) return 0;
654
    hdc = dc->hSelf;
655

656
    dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
Alexandre Julliard's avatar
Alexandre Julliard committed
657

658
    TRACE("(driver=%s, device=%s, output=%s): returning %p\n",
659
          debugstr_w(driver), debugstr_w(device), debugstr_w(output), dc->hSelf );
Alexandre Julliard's avatar
Alexandre Julliard committed
660

661
    if (funcs->pCreateDC)
Alexandre Julliard's avatar
Alexandre Julliard committed
662
    {
663
        if (!funcs->pCreateDC( &dc->physDev, buf, device, output, initData ))
664 665
        {
            WARN("creation aborted by device\n" );
666 667
            free_dc_ptr( dc );
            return 0;
668
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
669
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
670

671 672 673 674
    dc->vis_rect.left   = 0;
    dc->vis_rect.top    = 0;
    dc->vis_rect.right  = GetDeviceCaps( hdc, DESKTOPHORZRES );
    dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
675

Alexandre Julliard's avatar
Alexandre Julliard committed
676
    DC_InitDC( dc );
677
    release_dc_ptr( dc );
678
    return hdc;
Alexandre Julliard's avatar
Alexandre Julliard committed
679 680 681
}


Alexandre Julliard's avatar
Alexandre Julliard committed
682
/***********************************************************************
683
 *           CreateDCA    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
684
 */
685 686
HDC WINAPI CreateDCA( LPCSTR driver, LPCSTR device, LPCSTR output,
                      const DEVMODEA *initData )
687
{
688
    UNICODE_STRING driverW, deviceW, outputW;
689
    DEVMODEW *initDataW;
690 691 692 693 694 695 696 697 698 699 700
    HDC ret;

    if (driver) RtlCreateUnicodeStringFromAsciiz(&driverW, driver);
    else driverW.Buffer = NULL;

    if (device) RtlCreateUnicodeStringFromAsciiz(&deviceW, device);
    else deviceW.Buffer = NULL;

    if (output) RtlCreateUnicodeStringFromAsciiz(&outputW, output);
    else outputW.Buffer = NULL;

701 702 703 704 705 706 707
    initDataW = NULL;
    if (initData)
    {
        /* don't convert initData for DISPLAY driver, it's not used */
        if (!driverW.Buffer || strcmpiW( driverW.Buffer, displayW ))
            initDataW = GdiConvertToDevmodeW(initData);
    }
708

709
    ret = CreateDCW( driverW.Buffer, deviceW.Buffer, outputW.Buffer, initDataW );
710 711 712 713

    RtlFreeUnicodeString(&driverW);
    RtlFreeUnicodeString(&deviceW);
    RtlFreeUnicodeString(&outputW);
714
    HeapFree(GetProcessHeap(), 0, initDataW);
715
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
716 717 718
}


Alexandre Julliard's avatar
Alexandre Julliard committed
719
/***********************************************************************
720
 *           CreateICA    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
721
 */
722 723
HDC WINAPI CreateICA( LPCSTR driver, LPCSTR device, LPCSTR output,
                          const DEVMODEA* initData )
Alexandre Julliard's avatar
Alexandre Julliard committed
724 725
{
      /* Nothing special yet for ICs */
726
    return CreateDCA( driver, device, output, initData );
Alexandre Julliard's avatar
Alexandre Julliard committed
727 728 729 730
}


/***********************************************************************
731
 *           CreateICW    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
732
 */
733 734
HDC WINAPI CreateICW( LPCWSTR driver, LPCWSTR device, LPCWSTR output,
                          const DEVMODEW* initData )
Alexandre Julliard's avatar
Alexandre Julliard committed
735 736
{
      /* Nothing special yet for ICs */
737
    return CreateDCW( driver, device, output, initData );
Alexandre Julliard's avatar
Alexandre Julliard committed
738 739 740 741
}


/***********************************************************************
742
 *           CreateCompatibleDC   (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
743
 */
744
HDC WINAPI CreateCompatibleDC( HDC hdc )
Alexandre Julliard's avatar
Alexandre Julliard committed
745
{
Alexandre Julliard's avatar
Alexandre Julliard committed
746
    DC *dc, *origDC;
747
    HDC ret;
748
    const struct gdi_dc_funcs *funcs;
749
    PHYSDEV physDev = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
750

751
    GDI_CheckNotLock();
752

753
    if (hdc)
754
    {
755
        if (!(origDC = get_dc_ptr( hdc ))) return 0;
756 757
        physDev = GET_DC_PHYSDEV( origDC, pCreateCompatibleDC );
        funcs = physDev->funcs;
758
        release_dc_ptr( origDC );
759
    }
760
    else funcs = DRIVER_load_driver( displayW );
761

762
    if (!(dc = alloc_dc_ptr( OBJ_MEMDC ))) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
763

764
    TRACE("(%p): returning %p\n", hdc, dc->hSelf );
Alexandre Julliard's avatar
Alexandre Julliard committed
765

766
    dc->hBitmap = GDI_inc_ref_count( GetStockObject( DEFAULT_BITMAP ));
767 768 769 770
    dc->vis_rect.left   = 0;
    dc->vis_rect.top    = 0;
    dc->vis_rect.right  = 1;
    dc->vis_rect.bottom = 1;
771
    dc->device_rect = dc->vis_rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
772

773
    ret = dc->hSelf;
774

775
    if (funcs->pCreateCompatibleDC && !funcs->pCreateCompatibleDC( physDev, &dc->physDev ))
Alexandre Julliard's avatar
Alexandre Julliard committed
776
    {
777
        WARN("creation aborted by device\n");
778 779
        free_dc_ptr( dc );
        return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
780
    }
781 782 783 784 785 786 787 788 789

    if (!dib_driver.pCreateDC( &dc->physDev, NULL, NULL, NULL, NULL ))
    {
        free_dc_ptr( dc );
        return 0;
    }
    physDev = GET_DC_PHYSDEV( dc, pSelectBitmap );
    physDev->funcs->pSelectBitmap( physDev, dc->hBitmap );

Alexandre Julliard's avatar
Alexandre Julliard committed
790
    DC_InitDC( dc );
791
    release_dc_ptr( dc );
792
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
793 794 795
}


Alexandre Julliard's avatar
Alexandre Julliard committed
796
/***********************************************************************
797
 *           DeleteDC    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
798
 */
799
BOOL WINAPI DeleteDC( HDC hdc )
Alexandre Julliard's avatar
Alexandre Julliard committed
800
{
801
    DC * dc;
Alexandre Julliard's avatar
Alexandre Julliard committed
802

803
    TRACE("%p\n", hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
804

805 806
    GDI_CheckNotLock();

807
    if (!(dc = get_dc_ptr( hdc ))) return FALSE;
808 809 810
    if (dc->refcount != 1)
    {
        FIXME( "not deleting busy DC %p refcount %u\n", dc->hSelf, dc->refcount );
811
        release_dc_ptr( dc );
812 813
        return FALSE;
    }
814

815
    /* Call hook procedure to check whether is it OK to delete this DC */
816
    if (dc->hookProc && !dc->hookProc( dc->hSelf, DCHC_DELETEDC, dc->dwHookData, 0 ))
817
    {
818
        release_dc_ptr( dc );
819
        return TRUE;
820
    }
821
    reset_dc_state( hdc );
822
    free_dc_ptr( dc );
823
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
824 825 826
}


Alexandre Julliard's avatar
Alexandre Julliard committed
827
/***********************************************************************
828
 *           ResetDCW    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
829
 */
830
HDC WINAPI ResetDCW( HDC hdc, const DEVMODEW *devmode )
Alexandre Julliard's avatar
Alexandre Julliard committed
831
{
832
    DC *dc;
833
    HDC ret = 0;
834

835
    if ((dc = get_dc_ptr( hdc )))
836
    {
837 838 839
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pResetDC );
        ret = physdev->funcs->pResetDC( physdev, devmode );
        if (ret)  /* reset the visible region */
840
        {
841 842 843 844 845
            dc->dirty = 0;
            dc->vis_rect.left   = 0;
            dc->vis_rect.top    = 0;
            dc->vis_rect.right  = GetDeviceCaps( hdc, DESKTOPHORZRES );
            dc->vis_rect.bottom = GetDeviceCaps( hdc, DESKTOPVERTRES );
846 847
            if (dc->hVisRgn) DeleteObject( dc->hVisRgn );
            dc->hVisRgn = 0;
848
            update_dc_clipping( dc );
849
        }
850
        release_dc_ptr( dc );
851 852
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
853 854 855 856
}


/***********************************************************************
857
 *           ResetDCA    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
858
 */
859
HDC WINAPI ResetDCA( HDC hdc, const DEVMODEA *devmode )
Alexandre Julliard's avatar
Alexandre Julliard committed
860
{
861 862 863 864 865 866 867 868
    DEVMODEW *devmodeW;
    HDC ret;

    if (devmode) devmodeW = GdiConvertToDevmodeW(devmode);
    else devmodeW = NULL;

    ret = ResetDCW(hdc, devmodeW);

869
    HeapFree(GetProcessHeap(), 0, devmodeW);
870
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
871 872 873
}


Alexandre Julliard's avatar
Alexandre Julliard committed
874
/***********************************************************************
875
 *           GetDeviceCaps    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
876
 */
877
INT WINAPI GetDeviceCaps( HDC hdc, INT cap )
Alexandre Julliard's avatar
Alexandre Julliard committed
878
{
879
    DC *dc;
880
    INT ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
881

882
    if ((dc = get_dc_ptr( hdc )))
883
    {
884 885
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceCaps );
        ret = physdev->funcs->pGetDeviceCaps( physdev, cap );
886
        release_dc_ptr( dc );
887
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
888
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
889 890 891
}


892 893 894 895 896 897
/***********************************************************************
 *		GetBkColor (GDI32.@)
 */
COLORREF WINAPI GetBkColor( HDC hdc )
{
    COLORREF ret = 0;
898
    DC * dc = get_dc_ptr( hdc );
899 900 901
    if (dc)
    {
        ret = dc->backgroundColor;
902
        release_dc_ptr( dc );
903 904 905 906 907
    }
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
908
/***********************************************************************
909
 *           SetBkColor    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
910
 */
911
COLORREF WINAPI SetBkColor( HDC hdc, COLORREF color )
Alexandre Julliard's avatar
Alexandre Julliard committed
912
{
913
    COLORREF ret = CLR_INVALID;
914
    DC * dc = get_dc_ptr( hdc );
915

916
    TRACE("hdc=%p color=0x%08x\n", hdc, color);
917

918
    if (dc)
919
    {
920
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkColor );
921 922
        ret = dc->backgroundColor;
        dc->backgroundColor = physdev->funcs->pSetBkColor( physdev, color );
923
        release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
924
    }
925
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
926 927 928
}


929 930 931 932 933 934
/***********************************************************************
 *		GetTextColor (GDI32.@)
 */
COLORREF WINAPI GetTextColor( HDC hdc )
{
    COLORREF ret = 0;
935
    DC * dc = get_dc_ptr( hdc );
936 937 938
    if (dc)
    {
        ret = dc->textColor;
939
        release_dc_ptr( dc );
940 941 942 943 944
    }
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
945
/***********************************************************************
946
 *           SetTextColor    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
947
 */
948
COLORREF WINAPI SetTextColor( HDC hdc, COLORREF color )
Alexandre Julliard's avatar
Alexandre Julliard committed
949
{
950
    COLORREF ret = CLR_INVALID;
951
    DC * dc = get_dc_ptr( hdc );
952

953
    TRACE(" hdc=%p color=0x%08x\n", hdc, color);
954

955
    if (dc)
956
    {
957
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextColor );
958 959
        ret = dc->textColor;
        dc->textColor = physdev->funcs->pSetTextColor( physdev, color );
960
        release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
961
    }
962
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
963 964
}

Alexandre Julliard's avatar
Alexandre Julliard committed
965

966 967 968 969 970 971
/***********************************************************************
 *		GetTextAlign (GDI32.@)
 */
UINT WINAPI GetTextAlign( HDC hdc )
{
    UINT ret = 0;
972
    DC * dc = get_dc_ptr( hdc );
973 974 975
    if (dc)
    {
        ret = dc->textAlign;
976
        release_dc_ptr( dc );
977 978 979 980 981
    }
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
982
/***********************************************************************
983
 *           SetTextAlign    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
984
 */
985
UINT WINAPI SetTextAlign( HDC hdc, UINT align )
Alexandre Julliard's avatar
Alexandre Julliard committed
986
{
987
    UINT ret = GDI_ERROR;
988
    DC *dc = get_dc_ptr( hdc );
989 990 991

    TRACE("hdc=%p align=%d\n", hdc, align);

992 993 994 995 996 997 998 999 1000 1001 1002
    if (dc)
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetTextAlign );
        align = physdev->funcs->pSetTextAlign( physdev, align );
        if (align != GDI_ERROR)
        {
            ret = dc->textAlign;
            dc->textAlign = align;
        }
        release_dc_ptr( dc );
    }
1003
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1004 1005
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1006
/***********************************************************************
1007
 *           GetDCOrgEx  (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1008
 */
1009
BOOL WINAPI GetDCOrgEx( HDC hDC, LPPOINT lpp )
Alexandre Julliard's avatar
Alexandre Julliard committed
1010
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1011
    DC * dc;
1012

Alexandre Julliard's avatar
Alexandre Julliard committed
1013
    if (!lpp) return FALSE;
1014
    if (!(dc = get_dc_ptr( hDC ))) return FALSE;
1015 1016
    lpp->x = dc->vis_rect.left;
    lpp->y = dc->vis_rect.top;
1017
    release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1018 1019 1020 1021
    return TRUE;
}


1022 1023 1024 1025 1026 1027
/***********************************************************************
 *		GetGraphicsMode (GDI32.@)
 */
INT WINAPI GetGraphicsMode( HDC hdc )
{
    INT ret = 0;
1028
    DC * dc = get_dc_ptr( hdc );
1029 1030 1031
    if (dc)
    {
        ret = dc->GraphicsMode;
1032
        release_dc_ptr( dc );
1033 1034 1035 1036 1037
    }
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1038
/***********************************************************************
1039
 *           SetGraphicsMode    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1040
 */
1041
INT WINAPI SetGraphicsMode( HDC hdc, INT mode )
Alexandre Julliard's avatar
Alexandre Julliard committed
1042
{
1043
    INT ret = 0;
1044
    DC *dc = get_dc_ptr( hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1045 1046 1047 1048 1049 1050

    /* One would think that setting the graphics mode to GM_COMPATIBLE
     * would also reset the world transformation matrix to the unity
     * matrix. However, in Windows, this is not the case. This doesn't
     * make a lot of sense to me, but that's the way it is.
     */
Alexandre Julliard's avatar
Alexandre Julliard committed
1051
    if (!dc) return 0;
1052
    if ((mode > 0) && (mode <= GM_LAST))
1053
    {
1054 1055 1056
        ret = dc->GraphicsMode;
        dc->GraphicsMode = mode;
    }
1057
    /* font metrics depend on the graphics mode */
1058
    if (ret != mode) SelectObject(dc->hSelf, dc->hFont);
1059
    release_dc_ptr( dc );
1060
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1061 1062 1063
}


1064 1065 1066 1067 1068 1069
/***********************************************************************
 *		GetArcDirection (GDI32.@)
 */
INT WINAPI GetArcDirection( HDC hdc )
{
    INT ret = 0;
1070
    DC * dc = get_dc_ptr( hdc );
1071 1072 1073
    if (dc)
    {
        ret = dc->ArcDirection;
1074
        release_dc_ptr( dc );
1075 1076 1077 1078 1079
    }
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1080
/***********************************************************************
1081
 *           SetArcDirection    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1082
 */
1083
INT WINAPI SetArcDirection( HDC hdc, INT dir )
Alexandre Julliard's avatar
Alexandre Julliard committed
1084
{
1085
    DC * dc;
1086
    INT ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1087

1088
    if (dir != AD_COUNTERCLOCKWISE && dir != AD_CLOCKWISE)
Alexandre Julliard's avatar
Alexandre Julliard committed
1089 1090 1091 1092 1093
    {
        SetLastError(ERROR_INVALID_PARAMETER);
	return 0;
    }

1094
    if ((dc = get_dc_ptr( hdc )))
1095
    {
1096 1097 1098
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetArcDirection );
        dir = physdev->funcs->pSetArcDirection( physdev, dir );
        if (dir)
1099
        {
1100 1101
            ret = dc->ArcDirection;
            dc->ArcDirection = dir;
1102
        }
1103
        release_dc_ptr( dc );
1104
    }
1105
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1106 1107 1108
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1109
/***********************************************************************
1110
 *           GetWorldTransform    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1111
 */
1112
BOOL WINAPI GetWorldTransform( HDC hdc, LPXFORM xform )
Alexandre Julliard's avatar
Alexandre Julliard committed
1113
{
1114 1115
    DC * dc;
    if (!xform) return FALSE;
1116
    if (!(dc = get_dc_ptr( hdc ))) return FALSE;
1117
    *xform = dc->xformWorld2Wnd;
1118
    release_dc_ptr( dc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1119 1120 1121 1122
    return TRUE;
}


1123 1124
/***********************************************************************
 *           GetTransform    (GDI32.@)
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
 *
 * Undocumented
 *
 * Returns one of the co-ordinate space transforms
 *
 * PARAMS
 *    hdc   [I] Device context.
 *    which [I] Which xform to return:
 *                  0x203 World -> Page transform (that set by SetWorldTransform).
 *                  0x304 Page -> Device transform (the mapping mode transform).
 *                  0x204 World -> Device transform (the combination of the above two).
 *                  0x402 Device -> World transform (the inversion of the above).
 *    xform [O] The xform.
 *
1139
 */
1140
BOOL WINAPI GetTransform( HDC hdc, DWORD which, XFORM *xform )
1141
{
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
    BOOL ret = TRUE;
    DC *dc = get_dc_ptr( hdc );
    if (!dc) return FALSE;

    switch(which)
    {
    case 0x203:
        *xform = dc->xformWorld2Wnd;
        break;

    case 0x304:
        construct_window_to_viewport(dc, xform);
        break;

    case 0x204:
        *xform = dc->xformWorld2Vport;
        break;

    case 0x402:
        *xform = dc->xformVport2World;
        break;

    default:
        FIXME("Unknown code %x\n", which);
        ret = FALSE;
    }

    release_dc_ptr( dc );
    return ret;
1171 1172 1173
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1174
/****************************************************************************
1175
 * CombineTransform [GDI32.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
 * Combines two transformation matrices.
 *
 * PARAMS
 *    xformResult [O] Stores the result of combining the two matrices
 *    xform1      [I] Specifies the first matrix to apply
 *    xform2      [I] Specifies the second matrix to apply
 *
 * REMARKS
 *    The same matrix can be passed in for more than one of the parameters.
 *
Jon Griffiths's avatar
Jon Griffiths committed
1186 1187 1188
 * RETURNS
 *  Success: TRUE.
 *  Failure: FALSE. Use GetLastError() to determine the cause.
Alexandre Julliard's avatar
Alexandre Julliard committed
1189
 */
1190
BOOL WINAPI CombineTransform( LPXFORM xformResult, const XFORM *xform1,
Alexandre Julliard's avatar
Alexandre Julliard committed
1191 1192 1193
    const XFORM *xform2 )
{
    XFORM xformTemp;
1194

Alexandre Julliard's avatar
Alexandre Julliard committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    /* Check for illegal parameters */
    if (!xformResult || !xform1 || !xform2)
        return FALSE;

    /* Create the result in a temporary XFORM, since xformResult may be
     * equal to xform1 or xform2 */
    xformTemp.eM11 = xform1->eM11 * xform2->eM11 +
                     xform1->eM12 * xform2->eM21;
    xformTemp.eM12 = xform1->eM11 * xform2->eM12 +
                     xform1->eM12 * xform2->eM22;
    xformTemp.eM21 = xform1->eM21 * xform2->eM11 +
                     xform1->eM22 * xform2->eM21;
    xformTemp.eM22 = xform1->eM21 * xform2->eM12 +
                     xform1->eM22 * xform2->eM22;
    xformTemp.eDx  = xform1->eDx  * xform2->eM11 +
                     xform1->eDy  * xform2->eM21 +
                     xform2->eDx;
    xformTemp.eDy  = xform1->eDx  * xform2->eM12 +
                     xform1->eDy  * xform2->eM22 +
                     xform2->eDy;

    /* Copy the result to xformResult */
    *xformResult = xformTemp;

Alexandre Julliard's avatar
Alexandre Julliard committed
1219
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1220 1221 1222
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1223
/***********************************************************************
1224 1225 1226
 *           SetDCHook   (GDI32.@)
 *
 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
Alexandre Julliard's avatar
Alexandre Julliard committed
1227
 */
1228
BOOL WINAPI SetDCHook( HDC hdc, DCHOOKPROC hookProc, DWORD_PTR dwHookData )
1229
{
1230
    DC *dc = get_dc_ptr( hdc );
1231

1232
    if (!dc) return FALSE;
1233

1234 1235
    dc->dwHookData = dwHookData;
    dc->hookProc = hookProc;
1236
    release_dc_ptr( dc );
1237 1238
    return TRUE;
}
1239 1240


1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
/***********************************************************************
 *           GetDCHook   (GDI32.@)
 *
 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
 */
DWORD_PTR WINAPI GetDCHook( HDC hdc, DCHOOKPROC *proc )
{
    DC *dc = get_dc_ptr( hdc );
    DWORD_PTR ret;

    if (!dc) return 0;
1252
    if (proc) *proc = dc->hookProc;
1253
    ret = dc->dwHookData;
1254
    release_dc_ptr( dc );
1255
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1256 1257 1258 1259
}


/***********************************************************************
1260 1261 1262
 *           SetHookFlags   (GDI32.@)
 *
 * Note: this doesn't exist in Win32, we add it here because user32 needs it.
Alexandre Julliard's avatar
Alexandre Julliard committed
1263
 */
1264
WORD WINAPI SetHookFlags( HDC hdc, WORD flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
1265
{
1266 1267
    DC *dc = get_dc_obj( hdc );  /* not get_dc_ptr, this needs to work from any thread */
    LONG ret = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1268

1269
    if (!dc) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1270

1271
    TRACE("hDC %p, flags %04x\n",hdc,flags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1272

1273 1274 1275 1276 1277
    if (flags & DCHF_INVALIDATEVISRGN)
        ret = InterlockedExchange( &dc->dirty, 1 );
    else if (flags & DCHF_VALIDATEVISRGN || !flags)
        ret = InterlockedExchange( &dc->dirty, 0 );

1278 1279 1280 1281 1282
    if (flags & DCHF_DISABLEDC)
        ret = InterlockedExchange( &dc->disabled, 1 );
    else if (flags & DCHF_ENABLEDC)
        ret = InterlockedExchange( &dc->disabled, 0 );

1283
    GDI_ReleaseObj( hdc );
1284 1285

    if (flags & DCHF_RESETDC) ret = reset_dc_state( hdc );
1286
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
1287 1288
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1289
/***********************************************************************
1290
 *           SetICMMode    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1291
 */
1292
INT WINAPI SetICMMode(HDC hdc, INT iEnableICM)
Alexandre Julliard's avatar
Alexandre Julliard committed
1293
{
Austin English's avatar
Austin English committed
1294
/*FIXME:  Assume that ICM is always off, and cannot be turned on */
Alexandre Julliard's avatar
Alexandre Julliard committed
1295 1296 1297 1298 1299 1300
    if (iEnableICM == ICM_OFF) return ICM_OFF;
    if (iEnableICM == ICM_ON) return 0;
    if (iEnableICM == ICM_QUERY) return ICM_OFF;
    return 0;
}

1301
/***********************************************************************
1302
 *           GetDeviceGammaRamp    (GDI32.@)
1303
 */
1304 1305 1306
BOOL WINAPI GetDeviceGammaRamp(HDC hDC, LPVOID ptr)
{
    BOOL ret = FALSE;
1307
    DC *dc = get_dc_ptr( hDC );
1308

1309
    TRACE("%p, %p\n", hDC, ptr);
1310 1311
    if( dc )
    {
1312 1313 1314 1315 1316 1317
        if (GetObjectType( hDC ) != OBJ_MEMDC)
        {
            PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGetDeviceGammaRamp );
            ret = physdev->funcs->pGetDeviceGammaRamp( physdev, ptr );
        }
        else SetLastError( ERROR_INVALID_PARAMETER );
1318
	release_dc_ptr( dc );
1319 1320 1321 1322
    }
    return ret;
}

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
static BOOL check_gamma_ramps(void *ptr)
{
    WORD *ramp = ptr;

    while (ramp < (WORD*)ptr + 3 * 256)
    {
        float r_x, r_y, r_lx, r_ly, r_d, r_v, r_e, g_avg, g_min, g_max;
        unsigned i, f, l, g_n, c;

        f = ramp[0];
        l = ramp[255];
        if (f >= l)
        {
            TRACE("inverted or flat gamma ramp (%d->%d), rejected\n", f, l);
            return FALSE;
        }
        r_d = l - f;
        g_min = g_max = g_avg = 0.0;

        /* check gamma ramp entries to estimate the gamma */
        TRACE("analyzing gamma ramp (%d->%d)\n", f, l);
        for (i=1, g_n=0; i<255; i++)
        {
            if (ramp[i] < f || ramp[i] > l)
            {
                TRACE("strange gamma ramp ([%d]=%d for %d->%d), rejected\n", i, ramp[i], f, l);
                return FALSE;
            }
            c = ramp[i] - f;
            if (!c) continue; /* avoid log(0) */

            /* normalize entry values into 0..1 range */
            r_x = i/255.0; r_y = c / r_d;
            /* compute logarithms of values */
            r_lx = log(r_x); r_ly = log(r_y);
            /* compute gamma for this entry */
            r_v = r_ly / r_lx;
            /* compute differential (error estimate) for this entry */
            /* some games use table-based logarithms that magnifies the error by 128 */
            r_e = -r_lx * 128 / (c * r_lx * r_lx);

            /* compute min & max while compensating for estimated error */
            if (!g_n || g_min > (r_v + r_e)) g_min = r_v + r_e;
            if (!g_n || g_max < (r_v - r_e)) g_max = r_v - r_e;

            /* add to average */
            g_avg += r_v;
            g_n++;
        }

        if (!g_n)
        {
            TRACE("no gamma data, shouldn't happen\n");
            return FALSE;
        }
        g_avg /= g_n;
        TRACE("low bias is %d, high is %d, gamma is %5.3f\n", f, 65535-l, g_avg);

        /* check that the gamma is reasonably uniform across the ramp */
        if (g_max - g_min > 12.8)
        {
            TRACE("ramp not uniform (max=%f, min=%f, avg=%f), rejected\n", g_max, g_min, g_avg);
            return FALSE;
        }

        /* check that the gamma is not too bright */
        if (g_avg < 0.2)
        {
            TRACE("too bright gamma ( %5.3f), rejected\n", g_avg);
            return FALSE;
        }

        ramp += 256;
    }

    return TRUE;
}

1401 1402 1403 1404 1405 1406
/***********************************************************************
 *           SetDeviceGammaRamp    (GDI32.@)
 */
BOOL WINAPI SetDeviceGammaRamp(HDC hDC, LPVOID ptr)
{
    BOOL ret = FALSE;
1407
    DC *dc = get_dc_ptr( hDC );
1408

1409
    TRACE("%p, %p\n", hDC, ptr);
1410 1411
    if( dc )
    {
1412 1413 1414
        if (GetObjectType( hDC ) != OBJ_MEMDC)
        {
            PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDeviceGammaRamp );
1415 1416 1417

            if (check_gamma_ramps(ptr))
                ret = physdev->funcs->pSetDeviceGammaRamp( physdev, ptr );
1418 1419
        }
        else SetLastError( ERROR_INVALID_PARAMETER );
1420
	release_dc_ptr( dc );
1421 1422
    }
    return ret;
1423
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1424 1425

/***********************************************************************
1426
 *           GetColorSpace    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1427
 */
1428
HCOLORSPACE WINAPI GetColorSpace(HDC hdc)
Alexandre Julliard's avatar
Alexandre Julliard committed
1429
{
1430
/*FIXME    Need to do whatever GetColorSpace actually does */
Alexandre Julliard's avatar
Alexandre Julliard committed
1431 1432
    return 0;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1433

1434
/***********************************************************************
1435
 *           CreateColorSpaceA    (GDI32.@)
1436 1437 1438 1439
 */
HCOLORSPACE WINAPI CreateColorSpaceA( LPLOGCOLORSPACEA lpLogColorSpace )
{
  FIXME( "stub\n" );
1440
  return 0;
1441 1442 1443
}

/***********************************************************************
1444
 *           CreateColorSpaceW    (GDI32.@)
1445 1446 1447 1448 1449 1450 1451 1452
 */
HCOLORSPACE WINAPI CreateColorSpaceW( LPLOGCOLORSPACEW lpLogColorSpace )
{
  FIXME( "stub\n" );
  return 0;
}

/***********************************************************************
1453
 *           DeleteColorSpace     (GDI32.@)
1454 1455 1456 1457
 */
BOOL WINAPI DeleteColorSpace( HCOLORSPACE hColorSpace )
{
  FIXME( "stub\n" );
1458

1459
  return TRUE;
1460 1461 1462
}

/***********************************************************************
1463
 *           SetColorSpace     (GDI32.@)
1464 1465 1466 1467 1468 1469 1470 1471
 */
HCOLORSPACE WINAPI SetColorSpace( HDC hDC, HCOLORSPACE hColorSpace )
{
  FIXME( "stub\n" );

  return hColorSpace;
}

1472
/***********************************************************************
1473
 *           GetBoundsRect    (GDI32.@)
1474
 */
1475
UINT WINAPI GetBoundsRect(HDC hdc, LPRECT rect, UINT flags)
1476
{
1477 1478 1479
    PHYSDEV physdev;
    RECT device_rect;
    UINT ret;
1480
    DC *dc = get_dc_ptr( hdc );
1481

1482 1483
    if ( !dc ) return 0;

1484 1485 1486
    physdev = GET_DC_PHYSDEV( dc, pGetBoundsRect );
    ret = physdev->funcs->pGetBoundsRect( physdev, &device_rect, DCB_RESET );
    if (!ret)
1487 1488 1489 1490
    {
        release_dc_ptr( dc );
        return 0;
    }
1491
    if (dc->bounds_enabled && ret == DCB_SET) add_bounds_rect( &dc->bounds, &device_rect );
1492

1493 1494
    if (rect)
    {
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
        if (is_rect_empty( &dc->bounds ))
        {
            rect->left = rect->top = rect->right = rect->bottom = 0;
            ret = DCB_RESET;
        }
        else
        {
            *rect = dc->bounds;
            rect->left   = max( rect->left, 0 );
            rect->top    = max( rect->top, 0 );
            rect->right  = min( rect->right, dc->vis_rect.right - dc->vis_rect.left );
            rect->bottom = min( rect->bottom, dc->vis_rect.bottom - dc->vis_rect.top );
            ret = DCB_SET;
        }
1509
        dp_to_lp( dc, (POINT *)rect, 2 );
1510
    }
1511 1512
    else ret = 0;

1513
    if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1514
    release_dc_ptr( dc );
1515
    return ret;
1516 1517
}

1518

1519
/***********************************************************************
1520
 *           SetBoundsRect    (GDI32.@)
1521
 */
1522
UINT WINAPI SetBoundsRect(HDC hdc, const RECT* rect, UINT flags)
1523
{
1524
    PHYSDEV physdev;
1525 1526 1527 1528
    UINT ret;
    DC *dc;

    if ((flags & DCB_ENABLE) && (flags & DCB_DISABLE)) return 0;
1529
    if (!(dc = get_dc_ptr( hdc ))) return 0;
1530

1531 1532 1533
    physdev = GET_DC_PHYSDEV( dc, pSetBoundsRect );
    ret = physdev->funcs->pSetBoundsRect( physdev, &dc->bounds, flags );
    if (!ret)
1534 1535 1536 1537 1538
    {
        release_dc_ptr( dc );
        return 0;
    }

1539
    ret = (dc->bounds_enabled ? DCB_ENABLE : DCB_DISABLE) |
1540
          (is_rect_empty( &dc->bounds ) ? ret & DCB_SET : DCB_SET);
1541

1542
    if (flags & DCB_RESET) reset_bounds( &dc->bounds );
1543

1544
    if ((flags & DCB_ACCUMULATE) && rect)
1545
    {
1546 1547
        RECT rc = *rect;

1548
        lp_to_dp( dc, (POINT *)&rc, 2 );
1549
        add_bounds_rect( &dc->bounds, &rc );
1550 1551
    }

1552 1553
    if (flags & DCB_ENABLE) dc->bounds_enabled = TRUE;
    if (flags & DCB_DISABLE) dc->bounds_enabled = FALSE;
1554

1555
    release_dc_ptr( dc );
1556
    return ret;
1557 1558
}

1559 1560

/***********************************************************************
1561
 *		GetRelAbs		(GDI32.@)
1562 1563 1564
 */
INT WINAPI GetRelAbs( HDC hdc, DWORD dwIgnore )
{
1565
    INT ret = 0;
1566
    DC *dc = get_dc_ptr( hdc );
1567 1568 1569
    if (dc)
    {
        ret = dc->relAbsMode;
1570
        release_dc_ptr( dc );
1571
    }
1572
    return ret;
1573 1574
}

1575 1576 1577 1578 1579 1580 1581 1582 1583



/***********************************************************************
 *		GetBkMode (GDI32.@)
 */
INT WINAPI GetBkMode( HDC hdc )
{
    INT ret = 0;
1584
    DC * dc = get_dc_ptr( hdc );
1585 1586 1587
    if (dc)
    {
        ret = dc->backgroundMode;
1588
        release_dc_ptr( dc );
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
    }
    return ret;
}


/***********************************************************************
 *		SetBkMode (GDI32.@)
 */
INT WINAPI SetBkMode( HDC hdc, INT mode )
{
1599
    INT ret = 0;
1600
    DC *dc;
1601

1602 1603 1604 1605 1606
    if ((mode <= 0) || (mode > BKMODE_LAST))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
    if ((dc = get_dc_ptr( hdc )))
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetBkMode );
        mode = physdev->funcs->pSetBkMode( physdev, mode );
        if (mode)
        {
            ret = dc->backgroundMode;
            dc->backgroundMode = mode;
        }
        release_dc_ptr( dc );
    }
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
    return ret;
}


/***********************************************************************
 *		GetROP2 (GDI32.@)
 */
INT WINAPI GetROP2( HDC hdc )
{
    INT ret = 0;
1628
    DC * dc = get_dc_ptr( hdc );
1629 1630 1631
    if (dc)
    {
        ret = dc->ROPmode;
1632
        release_dc_ptr( dc );
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
    }
    return ret;
}


/***********************************************************************
 *		SetROP2 (GDI32.@)
 */
INT WINAPI SetROP2( HDC hdc, INT mode )
{
1643
    INT ret = 0;
1644
    DC *dc;
1645

1646 1647 1648 1649 1650
    if ((mode < R2_BLACK) || (mode > R2_WHITE))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
    if ((dc = get_dc_ptr( hdc )))
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetROP2 );
        mode = physdev->funcs->pSetROP2( physdev, mode );
        if (mode)
        {
            ret = dc->ROPmode;
            dc->ROPmode = mode;
        }
        release_dc_ptr( dc );
    }
1662 1663 1664 1665 1666 1667 1668 1669 1670
    return ret;
}


/***********************************************************************
 *		SetRelAbs (GDI32.@)
 */
INT WINAPI SetRelAbs( HDC hdc, INT mode )
{
1671
    INT ret = 0;
1672
    DC *dc;
1673

1674 1675 1676 1677 1678
    if ((mode != ABSOLUTE) && (mode != RELATIVE))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
1679
    if ((dc = get_dc_ptr( hdc )))
1680
    {
1681 1682 1683 1684 1685 1686 1687 1688
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetRelAbs );
        mode = physdev->funcs->pSetRelAbs( physdev, mode );
        if (mode)
        {
            ret = dc->relAbsMode;
            dc->relAbsMode = mode;
        }
        release_dc_ptr( dc );
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
    }
    return ret;
}


/***********************************************************************
 *		GetPolyFillMode (GDI32.@)
 */
INT WINAPI GetPolyFillMode( HDC hdc )
{
    INT ret = 0;
1700
    DC * dc = get_dc_ptr( hdc );
1701 1702 1703
    if (dc)
    {
        ret = dc->polyFillMode;
1704
        release_dc_ptr( dc );
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
    }
    return ret;
}


/***********************************************************************
 *		SetPolyFillMode (GDI32.@)
 */
INT WINAPI SetPolyFillMode( HDC hdc, INT mode )
{
1715
    INT ret = 0;
1716
    DC *dc;
1717

1718 1719 1720 1721 1722
    if ((mode <= 0) || (mode > POLYFILL_LAST))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
    if ((dc = get_dc_ptr( hdc )))
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetPolyFillMode );
        mode = physdev->funcs->pSetPolyFillMode( physdev, mode );
        if (mode)
        {
            ret = dc->polyFillMode;
            dc->polyFillMode = mode;
        }
        release_dc_ptr( dc );
    }
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    return ret;
}


/***********************************************************************
 *		GetStretchBltMode (GDI32.@)
 */
INT WINAPI GetStretchBltMode( HDC hdc )
{
    INT ret = 0;
1744
    DC * dc = get_dc_ptr( hdc );
1745 1746 1747
    if (dc)
    {
        ret = dc->stretchBltMode;
1748
        release_dc_ptr( dc );
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
    }
    return ret;
}


/***********************************************************************
 *		SetStretchBltMode (GDI32.@)
 */
INT WINAPI SetStretchBltMode( HDC hdc, INT mode )
{
1759
    INT ret = 0;
1760
    DC *dc;
1761

1762 1763 1764 1765 1766
    if ((mode <= 0) || (mode > MAXSTRETCHBLTMODE))
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
    if ((dc = get_dc_ptr( hdc )))
    {
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetStretchBltMode );
        mode = physdev->funcs->pSetStretchBltMode( physdev, mode );
        if (mode)
        {
            ret = dc->stretchBltMode;
            dc->stretchBltMode = mode;
        }
        release_dc_ptr( dc );
    }
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
    return ret;
}


/***********************************************************************
 *		GetMapMode (GDI32.@)
 */
INT WINAPI GetMapMode( HDC hdc )
{
    INT ret = 0;
1788
    DC * dc = get_dc_ptr( hdc );
1789 1790 1791
    if (dc)
    {
        ret = dc->MapMode;
1792
        release_dc_ptr( dc );
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
    }
    return ret;
}


/***********************************************************************
 *		GetBrushOrgEx (GDI32.@)
 */
BOOL WINAPI GetBrushOrgEx( HDC hdc, LPPOINT pt )
{
1803
    DC * dc = get_dc_ptr( hdc );
1804
    if (!dc) return FALSE;
1805
    *pt = dc->brush_org;
1806
    release_dc_ptr( dc );
1807 1808 1809 1810 1811 1812 1813 1814 1815
    return TRUE;
}


/***********************************************************************
 *		GetCurrentPositionEx (GDI32.@)
 */
BOOL WINAPI GetCurrentPositionEx( HDC hdc, LPPOINT pt )
{
1816
    DC * dc = get_dc_ptr( hdc );
1817
    if (!dc) return FALSE;
1818
    *pt = dc->cur_pos;
1819
    release_dc_ptr( dc );
1820 1821 1822 1823 1824 1825 1826 1827 1828
    return TRUE;
}


/***********************************************************************
 *		GetViewportExtEx (GDI32.@)
 */
BOOL WINAPI GetViewportExtEx( HDC hdc, LPSIZE size )
{
1829
    DC * dc = get_dc_ptr( hdc );
1830
    if (!dc) return FALSE;
1831
    *size = dc->vport_ext;
1832
    release_dc_ptr( dc );
1833 1834 1835 1836 1837 1838 1839 1840 1841
    return TRUE;
}


/***********************************************************************
 *		GetViewportOrgEx (GDI32.@)
 */
BOOL WINAPI GetViewportOrgEx( HDC hdc, LPPOINT pt )
{
1842
    DC * dc = get_dc_ptr( hdc );
1843
    if (!dc) return FALSE;
1844
    *pt = dc->vport_org;
1845
    release_dc_ptr( dc );
1846 1847 1848 1849 1850 1851 1852 1853 1854
    return TRUE;
}


/***********************************************************************
 *		GetWindowExtEx (GDI32.@)
 */
BOOL WINAPI GetWindowExtEx( HDC hdc, LPSIZE size )
{
1855
    DC * dc = get_dc_ptr( hdc );
1856
    if (!dc) return FALSE;
1857
    *size = dc->wnd_ext;
1858
    release_dc_ptr( dc );
1859 1860 1861 1862 1863 1864 1865 1866 1867
    return TRUE;
}


/***********************************************************************
 *		GetWindowOrgEx (GDI32.@)
 */
BOOL WINAPI GetWindowOrgEx( HDC hdc, LPPOINT pt )
{
1868
    DC * dc = get_dc_ptr( hdc );
1869
    if (!dc) return FALSE;
1870
    *pt = dc->wnd_org;
1871
    release_dc_ptr( dc );
1872 1873 1874 1875
    return TRUE;
}


1876
/***********************************************************************
1877
 *           GetLayout    (GDI32.@)
1878 1879 1880 1881 1882 1883
 *
 * Gets left->right or right->left text layout flags of a dc.
 *
 */
DWORD WINAPI GetLayout(HDC hdc)
{
1884 1885
    DWORD layout = GDI_ERROR;

1886
    DC * dc = get_dc_ptr( hdc );
1887 1888 1889
    if (dc)
    {
        layout = dc->layout;
1890
        release_dc_ptr( dc );
1891 1892
    }

1893
    TRACE("hdc : %p, layout : %08x\n", hdc, layout);
1894 1895

    return layout;
1896 1897 1898
}

/***********************************************************************
1899
 *           SetLayout    (GDI32.@)
1900 1901 1902 1903 1904 1905
 *
 * Sets left->right or right->left text layout flags of a dc.
 *
 */
DWORD WINAPI SetLayout(HDC hdc, DWORD layout)
{
1906 1907
    DWORD oldlayout = GDI_ERROR;

1908
    DC * dc = get_dc_ptr( hdc );
1909 1910
    if (dc)
    {
1911 1912 1913
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetLayout );
        layout = physdev->funcs->pSetLayout( physdev, layout );
        if (layout != GDI_ERROR)
1914
        {
1915 1916 1917 1918 1919 1920 1921
            oldlayout = dc->layout;
            dc->layout = layout;
            if (layout != oldlayout)
            {
                if (layout & LAYOUT_RTL) dc->MapMode = MM_ANISOTROPIC;
                DC_UpdateXforms( dc );
            }
1922
        }
1923
        release_dc_ptr( dc );
1924 1925
    }

1926
    TRACE("hdc : %p, old layout : %08x, new layout : %08x\n", hdc, oldlayout, layout);
1927 1928

    return oldlayout;
1929
}
1930

1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
/***********************************************************************
 *           GetDCBrushColor    (GDI32.@)
 */
COLORREF WINAPI GetDCBrushColor(HDC hdc)
{
    DC *dc;
    COLORREF dcBrushColor = CLR_INVALID;

    TRACE("hdc(%p)\n", hdc);

1941
    dc = get_dc_ptr( hdc );
1942 1943 1944
    if (dc)
    {
        dcBrushColor = dc->dcBrushColor;
1945
	release_dc_ptr( dc );
1946 1947 1948 1949 1950
    }

    return dcBrushColor;
}

1951 1952 1953 1954 1955
/***********************************************************************
 *           SetDCBrushColor    (GDI32.@)
 */
COLORREF WINAPI SetDCBrushColor(HDC hdc, COLORREF crColor)
{
1956 1957 1958
    DC *dc;
    COLORREF oldClr = CLR_INVALID;

1959
    TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
1960

1961
    dc = get_dc_ptr( hdc );
1962 1963
    if (dc)
    {
1964 1965
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCBrushColor );
        crColor = physdev->funcs->pSetDCBrushColor( physdev, crColor );
1966 1967 1968 1969 1970
        if (crColor != CLR_INVALID)
        {
            oldClr = dc->dcBrushColor;
            dc->dcBrushColor = crColor;
        }
1971
        release_dc_ptr( dc );
1972 1973 1974 1975 1976
    }

    return oldClr;
}

1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
/***********************************************************************
 *           GetDCPenColor    (GDI32.@)
 */
COLORREF WINAPI GetDCPenColor(HDC hdc)
{
    DC *dc;
    COLORREF dcPenColor = CLR_INVALID;

    TRACE("hdc(%p)\n", hdc);

1987
    dc = get_dc_ptr( hdc );
1988 1989 1990
    if (dc)
    {
        dcPenColor = dc->dcPenColor;
1991
	release_dc_ptr( dc );
1992 1993 1994 1995 1996
    }

    return dcPenColor;
}

1997 1998 1999 2000 2001 2002 2003 2004
/***********************************************************************
 *           SetDCPenColor    (GDI32.@)
 */
COLORREF WINAPI SetDCPenColor(HDC hdc, COLORREF crColor)
{
    DC *dc;
    COLORREF oldClr = CLR_INVALID;

2005
    TRACE("hdc(%p) crColor(%08x)\n", hdc, crColor);
2006

2007
    dc = get_dc_ptr( hdc );
2008 2009
    if (dc)
    {
2010 2011
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSetDCPenColor );
        crColor = physdev->funcs->pSetDCPenColor( physdev, crColor );
2012 2013 2014 2015 2016
        if (crColor != CLR_INVALID)
        {
            oldClr = dc->dcPenColor;
            dc->dcPenColor = crColor;
        }
2017
        release_dc_ptr( dc );
2018 2019 2020
    }

    return oldClr;
2021
}
2022

2023 2024 2025 2026 2027 2028 2029 2030 2031
/***********************************************************************
 *           CancelDC    (GDI32.@)
 */
BOOL WINAPI CancelDC(HDC hdc)
{
    FIXME("stub\n");
    return TRUE;
}

2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
/*******************************************************************
 *      GetMiterLimit [GDI32.@]
 *
 *
 */
BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
{
    BOOL bRet = FALSE;
    DC *dc;

    TRACE("(%p,%p)\n", hdc, peLimit);

2044
    dc = get_dc_ptr( hdc );
2045 2046 2047 2048 2049
    if (dc)
    {
        if (peLimit)
            *peLimit = dc->miterLimit;

2050
        release_dc_ptr( dc );
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
        bRet = TRUE;
    }
    return bRet;
}

/*******************************************************************
 *      SetMiterLimit [GDI32.@]
 *
 *
 */
BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
{
    BOOL bRet = FALSE;
    DC *dc;

    TRACE("(%p,%f,%p)\n", hdc, eNewLimit, peOldLimit);

2068
    dc = get_dc_ptr( hdc );
2069 2070 2071 2072 2073
    if (dc)
    {
        if (peOldLimit)
            *peOldLimit = dc->miterLimit;
        dc->miterLimit = eNewLimit;
2074
        release_dc_ptr( dc );
2075 2076 2077 2078
        bRet = TRUE;
    }
    return bRet;
}
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112

/*******************************************************************
 *      GdiIsMetaPrintDC [GDI32.@]
 */
BOOL WINAPI GdiIsMetaPrintDC(HDC hdc)
{
    FIXME("%p\n", hdc);
    return FALSE;
}

/*******************************************************************
 *      GdiIsMetaFileDC [GDI32.@]
 */
BOOL WINAPI GdiIsMetaFileDC(HDC hdc)
{
    TRACE("%p\n", hdc);

    switch( GetObjectType( hdc ) )
    {
    case OBJ_METADC:
    case OBJ_ENHMETADC:
        return TRUE;
    }
    return FALSE;
}

/*******************************************************************
 *      GdiIsPlayMetafileDC [GDI32.@]
 */
BOOL WINAPI GdiIsPlayMetafileDC(HDC hdc)
{
    FIXME("%p\n", hdc);
    return FALSE;
}