4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
42 #define HGDIOBJ_32(h16) ((HGDIOBJ)(ULONG_PTR)(h16))
44 /***********************************************************************
48 static const LOGBRUSH WhiteBrush = { BS_SOLID, RGB(255,255,255), 0 };
49 static const LOGBRUSH BlackBrush = { BS_SOLID, RGB(0,0,0), 0 };
50 static const LOGBRUSH NullBrush = { BS_NULL, 0, 0 };
52 /* FIXME: these should perhaps be BS_HATCHED, at least for 1 bitperpixel */
53 static const LOGBRUSH LtGrayBrush = { BS_SOLID, RGB(192,192,192), 0 };
54 static const LOGBRUSH GrayBrush = { BS_SOLID, RGB(128,128,128), 0 };
56 /* This is BS_HATCHED, for 1 bitperpixel. This makes the spray work in pbrush */
57 /* See HatchBrushes in x11drv for the HS_DIAGCROSS+1 hack */
58 static const LOGBRUSH DkGrayBrush = { BS_HATCHED, RGB(0,0,0), (HS_DIAGCROSS+1) };
60 static const LOGPEN WhitePen = { PS_SOLID, { 0, 0 }, RGB(255,255,255) };
61 static const LOGPEN BlackPen = { PS_SOLID, { 0, 0 }, RGB(0,0,0) };
62 static const LOGPEN NullPen = { PS_NULL, { 0, 0 }, 0 };
65 /* reserve one extra entry for the stock default bitmap */
66 /* this is what Windows does too */
67 #define NB_STOCK_OBJECTS (STOCK_LAST+2)
69 static HGDIOBJ stock_objects[NB_STOCK_OBJECTS];
71 static SYSLEVEL GDI_level;
72 static CRITICAL_SECTION_DEBUG critsect_debug =
74 0, 0, &GDI_level.crst,
75 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
76 0, 0, { 0, (DWORD)(__FILE__ ": GDI_level") }
78 static SYSLEVEL GDI_level = { { &critsect_debug, -1, 0, 0, 0, 0 }, 3 };
80 static WORD GDI_HeapSel;
82 inline static BOOL get_bool(char *buffer)
84 return (buffer[0] == 'y' || buffer[0] == 'Y' ||
85 buffer[0] == 't' || buffer[0] == 'T' ||
90 /****************************************************************************
92 * language-independent stock fonts
96 static const LOGFONTW OEMFixedFont =
97 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, OEM_CHARSET,
98 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, {'\0'} };
100 static const LOGFONTW AnsiFixedFont =
101 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
102 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, {'\0'} };
104 static const LOGFONTW AnsiVarFont =
105 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
106 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
107 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'} };
109 /******************************************************************************
111 * language-dependent stock fonts
113 * 'ANSI' charset and 'DEFAULT' charset is not same.
114 * The chars in CP_ACP should be drawn with 'DEFAULT' charset.
115 * 'ANSI' charset seems to be identical with ISO-8859-1.
116 * 'DEFAULT' charset is a language-dependent charset.
118 * 'System' font seems to be an alias for language-dependent font.
122 * language-dependenet stock fonts for all known charsets
123 * please see TranslateCharsetInfo (objects/font.c) and
124 * CharsetBindingInfo (graphics/x11drv/xfont.c),
125 * and modify entries for your language if needed.
127 struct DefaultFontInfo
131 LOGFONTW DeviceDefaultFont;
132 LOGFONTW SystemFixedFont;
133 LOGFONTW DefaultGuiFont;
136 static const struct DefaultFontInfo default_fonts[] =
140 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
141 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
142 {'S','y','s','t','e','m','\0'}
144 { /* Device Default */
145 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
146 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
150 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
151 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
154 { /* DefaultGuiFont */
155 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
156 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
157 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
160 { EASTEUROPE_CHARSET,
162 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
163 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
164 {'S','y','s','t','e','m','\0'}
166 { /* Device Default */
167 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
168 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
172 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
173 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
176 { /* DefaultGuiFont */
177 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
178 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
179 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
184 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
185 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
186 {'S','y','s','t','e','m','\0'}
188 { /* Device Default */
189 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
190 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
194 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
195 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
198 { /* DefaultGuiFont */
199 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
200 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
201 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
206 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
207 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
208 {'S','y','s','t','e','m','\0'}
210 { /* Device Default */
211 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
212 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
216 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
217 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
220 { /* DefaultGuiFont */
221 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
222 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
223 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
228 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
229 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
230 {'S','y','s','t','e','m','\0'}
232 { /* Device Default */
233 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
234 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
238 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
239 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
242 { /* DefaultGuiFont */
243 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
244 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
245 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
250 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
251 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
252 {'S','y','s','t','e','m','\0'}
254 { /* Device Default */
255 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
256 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
260 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
261 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
264 { /* DefaultGuiFont */
265 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
266 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
267 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
272 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
273 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
274 {'S','y','s','t','e','m','\0'}
276 { /* Device Default */
277 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
278 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
282 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
283 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
286 { /* DefaultGuiFont */
287 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
288 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
289 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
294 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
295 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
296 {'S','y','s','t','e','m','\0'}
298 { /* Device Default */
299 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
300 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
304 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
305 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
308 { /* DefaultGuiFont */
309 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
310 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
311 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
316 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
317 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
318 {'S','y','s','t','e','m','\0'}
320 { /* Device Default */
321 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
322 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
326 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
327 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
330 { /* DefaultGuiFont */
331 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
332 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
333 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'}
338 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
339 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
340 {'S','y','s','t','e','m','\0'}
342 { /* Device Default */
343 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
344 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
348 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
349 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
352 { /* DefaultGuiFont */
353 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
354 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
355 {'M','S',' ','P',' ','g','o','t','h','i','c','\0'} /* FIXME: Is this correct? */
360 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
361 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
362 {'S','y','s','t','e','m','\0'}
364 { /* Device Default */
365 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
366 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
370 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
371 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
374 { /* DefaultGuiFont */
375 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
376 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
377 {'M','S',' ','S','o','n','g','\0'} /* FIXME: Is this correct? */
382 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
383 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
384 {'S','y','s','t','e','m','\0'}
386 { /* Device Default */
387 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
388 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
392 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
393 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
396 { /* DefaultGuiFont */
397 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
398 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
399 {'G','u','l','i','m'},
402 { CHINESEBIG5_CHARSET,
404 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
405 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
406 {'S','y','s','t','e','m','\0'}
408 { /* Device Default */
409 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
410 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
414 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
415 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
418 { /* DefaultGuiFont */
419 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
420 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
421 {'\0'} /* FIXME - what is the native font??? */
426 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
427 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
428 {'S','y','s','t','e','m','\0'}
430 { /* Device Default */
431 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
432 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
436 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
437 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
440 { /* DefaultGuiFont */
441 -11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
442 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
443 {'M','S',' ','M','i','n','g','l','i','u','\0'} /* FIXME: Is this correct? */
449 /******************************************************************************
452 static const struct DefaultFontInfo* get_default_fonts(UINT charset)
456 for(n=0;n<(sizeof(default_fonts)/sizeof(default_fonts[0]));n++)
458 if ( default_fonts[n].charset == charset )
459 return &default_fonts[n];
462 FIXME( "unhandled charset 0x%08x - use ANSI_CHARSET for default stock objects\n", charset );
463 return &default_fonts[0];
467 /******************************************************************************
468 * get_default_charset (internal)
470 * get the language-dependent charset that can handle CP_ACP correctly.
472 static UINT get_default_charset( void )
478 csi.ciCharset = ANSI_CHARSET;
479 if ( ! TranslateCharsetInfo( (LPDWORD)uACP, &csi, TCI_SRCCODEPAGE ) )
481 FIXME( "unhandled codepage %u - use ANSI_CHARSET for default stock objects\n", uACP );
485 return csi.ciCharset;
489 /******************************************************************************
492 static HFONT create_stock_font( char const *fontName, const LOGFONTW *font, HKEY hkey )
496 char buffer[MAX_PATH];
499 if (!hkey) return CreateFontIndirectW( font );
502 sprintf(key, "%s.Height", fontName);
503 count = sizeof(buffer);
504 if(!RegQueryValueExA(hkey, key, 0, &type, buffer, &count))
505 lf.lfHeight = atoi(buffer);
507 sprintf(key, "%s.Bold", fontName);
508 count = sizeof(buffer);
509 if(!RegQueryValueExA(hkey, key, 0, &type, buffer, &count))
510 lf.lfWeight = get_bool(buffer) ? FW_BOLD : FW_NORMAL;
512 sprintf(key, "%s.Italic", fontName);
513 count = sizeof(buffer);
514 if(!RegQueryValueExA(hkey, key, 0, &type, buffer, &count))
515 lf.lfItalic = get_bool(buffer);
517 sprintf(key, "%s.Underline", fontName);
518 count = sizeof(buffer);
519 if(!RegQueryValueExA(hkey, key, 0, &type, buffer, &count))
520 lf.lfUnderline = get_bool(buffer);
522 sprintf(key, "%s.StrikeOut", fontName);
523 count = sizeof(buffer);
524 if(!RegQueryValueExA(hkey, key, 0, &type, buffer, &count))
525 lf.lfStrikeOut = get_bool(buffer);
526 return CreateFontIndirectW( &lf );
530 #define TRACE_SEC(handle,text) \
531 TRACE("(%p): " text " %ld\n", (handle), GDI_level.crst.RecursionCount)
534 /***********************************************************************
537 * Increment the reference count of a GDI object.
539 inline static void inc_ref_count( HGDIOBJ handle )
543 if ((header = GDI_GetObjPtr( handle, MAGIC_DONTCARE )))
546 GDI_ReleaseObj( handle );
551 /***********************************************************************
554 * Decrement the reference count of a GDI object.
556 inline static void dec_ref_count( HGDIOBJ handle )
560 if ((header = GDI_GetObjPtr( handle, MAGIC_DONTCARE )))
562 if (header->dwCount) header->dwCount--;
563 if (header->dwCount != 0x80000000) GDI_ReleaseObj( handle );
566 /* handle delayed DeleteObject*/
568 GDI_ReleaseObj( handle );
569 TRACE( "executing delayed DeleteObject for %p\n", handle );
570 DeleteObject( handle );
576 /***********************************************************************
579 * GDI initialization.
583 HINSTANCE16 instance;
586 const struct DefaultFontInfo* deffonts;
589 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\Tweak.Fonts", &hkey))
592 /* create GDI heap */
593 if ((instance = LoadLibrary16( "GDI.EXE" )) >= 32) GDI_HeapSel = instance | 7;
595 /* create stock objects */
596 stock_objects[WHITE_BRUSH] = CreateBrushIndirect( &WhiteBrush );
597 stock_objects[LTGRAY_BRUSH] = CreateBrushIndirect( &LtGrayBrush );
598 stock_objects[GRAY_BRUSH] = CreateBrushIndirect( &GrayBrush );
599 stock_objects[DKGRAY_BRUSH] = CreateBrushIndirect( &DkGrayBrush );
600 stock_objects[BLACK_BRUSH] = CreateBrushIndirect( &BlackBrush );
601 stock_objects[NULL_BRUSH] = CreateBrushIndirect( &NullBrush );
603 stock_objects[WHITE_PEN] = CreatePenIndirect( &WhitePen );
604 stock_objects[BLACK_PEN] = CreatePenIndirect( &BlackPen );
605 stock_objects[NULL_PEN] = CreatePenIndirect( &NullPen );
607 stock_objects[DEFAULT_PALETTE] = PALETTE_Init();
608 stock_objects[DEFAULT_BITMAP] = CreateBitmap( 1, 1, 1, 1, NULL );
610 /* language-independent stock fonts */
611 stock_objects[OEM_FIXED_FONT] = create_stock_font( "OEMFixed", &OEMFixedFont, hkey );
612 stock_objects[ANSI_FIXED_FONT] = create_stock_font( "AnsiFixed", &AnsiFixedFont, hkey );
613 stock_objects[ANSI_VAR_FONT] = create_stock_font( "AnsiVar", &AnsiVarFont, hkey );
615 /* language-dependent stock fonts */
616 deffonts = get_default_fonts(get_default_charset());
617 stock_objects[SYSTEM_FONT] = create_stock_font( "System", &deffonts->SystemFont, hkey );
618 stock_objects[DEVICE_DEFAULT_FONT] = create_stock_font( "DeviceDefault", &deffonts->DeviceDefaultFont, hkey );
619 stock_objects[SYSTEM_FIXED_FONT] = create_stock_font( "SystemFixed", &deffonts->SystemFixedFont, hkey );
620 stock_objects[DEFAULT_GUI_FONT] = create_stock_font( "DefaultGui", &deffonts->DefaultGuiFont, hkey );
623 /* clear the NOSYSTEM bit on all stock objects*/
624 for (i = 0; i < NB_STOCK_OBJECTS; i++)
626 if (!stock_objects[i])
628 if (i == 9) continue; /* there's no stock object 9 */
629 ERR( "could not create stock object %d\n", i );
632 ptr = GDI_GetObjPtr( stock_objects[i], MAGIC_DONTCARE );
633 ptr->wMagic &= ~OBJECT_NOSYSTEM;
634 GDI_ReleaseObj( stock_objects[i] );
637 if (hkey) RegCloseKey( hkey );
644 #define FIRST_LARGE_HANDLE 16
645 #define MAX_LARGE_HANDLES ((GDI_HEAP_SIZE >> 2) - FIRST_LARGE_HANDLE)
646 static GDIOBJHDR *large_handles[MAX_LARGE_HANDLES];
647 static int next_large_handle;
649 /***********************************************************************
652 * Allocate a GDI handle from the large heap. Helper for GDI_AllocObject
654 inline static GDIOBJHDR *alloc_large_heap( WORD size, HGDIOBJ *handle )
659 for (i = next_large_handle + 1; i < MAX_LARGE_HANDLES; i++)
660 if (!large_handles[i]) goto found;
661 for (i = 0; i <= next_large_handle; i++)
662 if (!large_handles[i]) goto found;
667 if ((obj = HeapAlloc( GetProcessHeap(), 0, size )))
669 large_handles[i] = obj;
670 *handle = (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2);
671 next_large_handle = i;
677 /***********************************************************************
680 void *GDI_AllocObject( WORD size, WORD magic, HGDIOBJ *handle, const struct gdi_obj_funcs *funcs )
685 _EnterSysLevel( &GDI_level );
691 if (!(hlocal = LOCAL_Alloc( GDI_HeapSel, LMEM_MOVEABLE, size ))) goto error;
692 assert( hlocal & 2 );
693 obj = (GDIOBJHDR *)LOCAL_Lock( GDI_HeapSel, hlocal );
694 *handle = (HGDIOBJ)(ULONG_PTR)hlocal;
699 case DISABLED_DC_MAGIC:
702 case METAFILE_DC_MAGIC:
703 case ENHMETAFILE_MAGIC:
704 case ENHMETAFILE_DC_MAGIC:
705 case MEMORY_DC_MAGIC:
708 if (!(obj = alloc_large_heap( size, handle ))) goto error;
713 obj->wMagic = magic|OBJECT_NOSYSTEM;
717 TRACE_SEC( *handle, "enter" );
721 _LeaveSysLevel( &GDI_level );
727 /***********************************************************************
730 * The object ptr must have been obtained with GDI_GetObjPtr.
731 * The new pointer must be released with GDI_ReleaseObj.
733 void *GDI_ReallocObject( WORD size, HGDIOBJ handle, void *object )
737 if ((UINT_PTR)handle & 2) /* GDI heap handle */
739 HLOCAL16 h = LOWORD(handle);
740 LOCAL_Unlock( GDI_HeapSel, h );
741 if ((new_handle = (HGDIOBJ)(ULONG_PTR)LOCAL_ReAlloc( GDI_HeapSel, h, size, LMEM_MOVEABLE )))
743 assert( new_handle == handle ); /* moveable handle cannot change */
744 return LOCAL_Lock( GDI_HeapSel, h );
749 int i = ((ULONG_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
750 if (i >= 0 && i < MAX_LARGE_HANDLES && large_handles[i])
752 void *new_ptr = HeapReAlloc( GetProcessHeap(), 0, large_handles[i], size );
755 large_handles[i] = new_ptr;
759 else ERR( "Invalid handle %p\n", handle );
761 TRACE_SEC( handle, "leave" );
762 _LeaveSysLevel( &GDI_level );
767 /***********************************************************************
770 BOOL GDI_FreeObject( HGDIOBJ handle, void *ptr )
772 GDIOBJHDR *object = ptr;
774 object->wMagic = 0; /* Mark it as invalid */
775 object->funcs = NULL;
776 if ((UINT_PTR)handle & 2) /* GDI heap handle */
778 HLOCAL16 h = LOWORD(handle);
779 LOCAL_Unlock( GDI_HeapSel, h );
780 LOCAL_Free( GDI_HeapSel, h );
782 else /* large heap handle */
784 int i = ((ULONG_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
785 if (i >= 0 && i < MAX_LARGE_HANDLES && large_handles[i])
787 HeapFree( GetProcessHeap(), 0, large_handles[i] );
788 large_handles[i] = NULL;
790 else ERR( "Invalid handle %p\n", handle );
792 TRACE_SEC( handle, "leave" );
793 _LeaveSysLevel( &GDI_level );
798 /***********************************************************************
801 * Return a pointer to the GDI object associated to the handle.
802 * Return NULL if the object has the wrong magic number.
803 * The object must be released with GDI_ReleaseObj.
805 void *GDI_GetObjPtr( HGDIOBJ handle, WORD magic )
807 GDIOBJHDR *ptr = NULL;
809 _EnterSysLevel( &GDI_level );
811 if ((UINT_PTR)handle & 2) /* GDI heap handle */
813 HLOCAL16 h = LOWORD(handle);
814 ptr = (GDIOBJHDR *)LOCAL_Lock( GDI_HeapSel, h );
817 if (((magic != MAGIC_DONTCARE) && (GDIMAGIC(ptr->wMagic) != magic)) ||
818 (GDIMAGIC(ptr->wMagic) < FIRST_MAGIC) ||
819 (GDIMAGIC(ptr->wMagic) > LAST_MAGIC))
821 LOCAL_Unlock( GDI_HeapSel, h );
826 else /* large heap handle */
828 int i = ((UINT_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
829 if (i >= 0 && i < MAX_LARGE_HANDLES)
831 ptr = large_handles[i];
832 if (ptr && (magic != MAGIC_DONTCARE) && (GDIMAGIC(ptr->wMagic) != magic)) ptr = NULL;
838 _LeaveSysLevel( &GDI_level );
839 SetLastError( ERROR_INVALID_HANDLE );
840 WARN( "Invalid handle %p\n", handle );
842 else TRACE_SEC( handle, "enter" );
848 /***********************************************************************
852 void GDI_ReleaseObj( HGDIOBJ handle )
854 if ((UINT_PTR)handle & 2) LOCAL_Unlock( GDI_HeapSel, LOWORD(handle) );
855 TRACE_SEC( handle, "leave" );
856 _LeaveSysLevel( &GDI_level );
860 /***********************************************************************
863 void GDI_CheckNotLock(void)
865 _CheckNotSysLevel( &GDI_level );
869 /***********************************************************************
870 * DeleteObject (GDI32.@)
872 BOOL WINAPI DeleteObject( HGDIOBJ obj )
874 /* Check if object is valid */
877 if (HIWORD(obj)) return FALSE;
879 if (!(header = GDI_GetObjPtr( obj, MAGIC_DONTCARE ))) return FALSE;
881 if (!(header->wMagic & OBJECT_NOSYSTEM)
882 && (header->wMagic >= FIRST_MAGIC) && (header->wMagic <= LAST_MAGIC))
884 TRACE("Preserving system object %p\n", obj);
885 GDI_ReleaseObj( obj );
891 TRACE("delayed for %p because object in use, count %ld\n", obj, header->dwCount );
892 header->dwCount |= 0x80000000; /* mark for delete */
893 GDI_ReleaseObj( obj );
901 if (header->funcs && header->funcs->pDeleteObject)
902 return header->funcs->pDeleteObject( obj, header );
904 GDI_ReleaseObj( obj );
909 /***********************************************************************
910 * GetStockObject (GDI32.@)
912 HGDIOBJ WINAPI GetStockObject( INT obj )
915 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
916 ret = stock_objects[obj];
917 TRACE("returning %p\n", ret );
922 /***********************************************************************
925 INT16 WINAPI GetObject16( HANDLE16 handle16, INT16 count, LPVOID buffer )
928 HGDIOBJ handle = HGDIOBJ_32( handle16 );
931 TRACE("%p %d %p\n", handle, count, buffer );
932 if (!count) return 0;
934 if (!(ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE ))) return 0;
936 if (ptr->funcs && ptr->funcs->pGetObject16)
937 result = ptr->funcs->pGetObject16( handle, ptr, count, buffer );
939 SetLastError( ERROR_INVALID_HANDLE );
941 GDI_ReleaseObj( handle );
946 /***********************************************************************
947 * GetObjectA (GDI32.@)
949 INT WINAPI GetObjectA( HANDLE handle, INT count, LPVOID buffer )
953 TRACE("%p %d %p\n", handle, count, buffer );
955 if (!(ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE ))) return 0;
957 if (ptr->funcs && ptr->funcs->pGetObjectA)
958 result = ptr->funcs->pGetObjectA( handle, ptr, count, buffer );
960 SetLastError( ERROR_INVALID_HANDLE );
962 GDI_ReleaseObj( handle );
966 /***********************************************************************
967 * GetObjectW (GDI32.@)
969 INT WINAPI GetObjectW( HANDLE handle, INT count, LPVOID buffer )
973 TRACE("%p %d %p\n", handle, count, buffer );
975 if (!(ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE ))) return 0;
977 if (ptr->funcs && ptr->funcs->pGetObjectW)
978 result = ptr->funcs->pGetObjectW( handle, ptr, count, buffer );
980 SetLastError( ERROR_INVALID_HANDLE );
982 GDI_ReleaseObj( handle );
986 /***********************************************************************
987 * GetObjectType (GDI32.@)
989 DWORD WINAPI GetObjectType( HANDLE handle )
993 TRACE("%p\n", handle );
995 if (!(ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE ))) return 0;
997 switch(GDIMAGIC(ptr->wMagic))
1006 result = OBJ_BITMAP;
1015 result = OBJ_REGION;
1021 result = OBJ_METADC;
1023 case METAFILE_MAGIC:
1024 result = OBJ_METAFILE;
1026 case METAFILE_DC_MAGIC:
1027 result = OBJ_METADC;
1029 case ENHMETAFILE_MAGIC:
1030 result = OBJ_ENHMETAFILE;
1032 case ENHMETAFILE_DC_MAGIC:
1033 result = OBJ_ENHMETADC;
1035 case MEMORY_DC_MAGIC:
1039 FIXME("Magic %04x not implemented\n", GDIMAGIC(ptr->wMagic) );
1042 GDI_ReleaseObj( handle );
1046 /***********************************************************************
1047 * GetCurrentObject (GDI32.@)
1049 HANDLE WINAPI GetCurrentObject(HDC hdc,UINT type)
1052 DC * dc = DC_GetDCPtr( hdc );
1057 case OBJ_PEN: ret = dc->hPen; break;
1058 case OBJ_BRUSH: ret = dc->hBrush; break;
1059 case OBJ_PAL: ret = dc->hPalette; break;
1060 case OBJ_FONT: ret = dc->hFont; break;
1061 case OBJ_BITMAP: ret = dc->hBitmap; break;
1063 /* the SDK only mentions those above */
1064 FIXME("(%p,%d): unknown type.\n",hdc,type);
1067 GDI_ReleaseObj( hdc );
1073 /***********************************************************************
1074 * SelectObject (GDI32.@)
1076 HGDIOBJ WINAPI SelectObject( HDC hdc, HGDIOBJ handle )
1079 GDIOBJHDR *header = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
1080 if (!header) return 0;
1082 TRACE("hdc=%p %p\n", hdc, handle );
1084 if (header->funcs && header->funcs->pSelectObject)
1086 ret = header->funcs->pSelectObject( handle, header, hdc );
1087 if (ret && ret != handle && (INT)ret > COMPLEXREGION)
1089 inc_ref_count( handle );
1090 dec_ref_count( ret );
1093 GDI_ReleaseObj( handle );
1098 /***********************************************************************
1099 * UnrealizeObject (GDI32.@)
1101 BOOL WINAPI UnrealizeObject( HGDIOBJ obj )
1104 /* Check if object is valid */
1106 GDIOBJHDR * header = GDI_GetObjPtr( obj, MAGIC_DONTCARE );
1107 if (!header) return FALSE;
1109 TRACE("%p\n", obj );
1111 /* Unrealize object */
1113 if (header->funcs && header->funcs->pUnrealizeObject)
1114 result = header->funcs->pUnrealizeObject( obj, header );
1116 GDI_ReleaseObj( obj );
1121 /* Solid colors to enumerate */
1122 static const COLORREF solid_colors[] =
1123 { RGB(0x00,0x00,0x00), RGB(0xff,0xff,0xff),
1124 RGB(0xff,0x00,0x00), RGB(0x00,0xff,0x00),
1125 RGB(0x00,0x00,0xff), RGB(0xff,0xff,0x00),
1126 RGB(0xff,0x00,0xff), RGB(0x00,0xff,0xff),
1127 RGB(0x80,0x00,0x00), RGB(0x00,0x80,0x00),
1128 RGB(0x80,0x80,0x00), RGB(0x00,0x00,0x80),
1129 RGB(0x80,0x00,0x80), RGB(0x00,0x80,0x80),
1130 RGB(0x80,0x80,0x80), RGB(0xc0,0xc0,0xc0)
1134 /***********************************************************************
1135 * EnumObjects (GDI32.@)
1137 INT WINAPI EnumObjects( HDC hdc, INT nObjType,
1138 GOBJENUMPROC lpEnumFunc, LPARAM lParam )
1144 TRACE("%p %d %p %08lx\n", hdc, nObjType, lpEnumFunc, lParam );
1148 /* Enumerate solid pens */
1149 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1151 pen.lopnStyle = PS_SOLID;
1152 pen.lopnWidth.x = 1;
1153 pen.lopnWidth.y = 0;
1154 pen.lopnColor = solid_colors[i];
1155 retval = lpEnumFunc( &pen, lParam );
1156 TRACE("solid pen %08lx, ret=%d\n",
1157 solid_colors[i], retval);
1163 /* Enumerate solid brushes */
1164 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1166 brush.lbStyle = BS_SOLID;
1167 brush.lbColor = solid_colors[i];
1169 retval = lpEnumFunc( &brush, lParam );
1170 TRACE("solid brush %08lx, ret=%d\n",
1171 solid_colors[i], retval);
1175 /* Now enumerate hatched brushes */
1176 if (retval) for (i = HS_HORIZONTAL; i <= HS_DIAGCROSS; i++)
1178 brush.lbStyle = BS_HATCHED;
1179 brush.lbColor = RGB(0,0,0);
1181 retval = lpEnumFunc( &brush, lParam );
1182 TRACE("hatched brush %d, ret=%d\n",
1189 /* FIXME: implement Win32 types */
1190 WARN("(%d): Invalid type\n", nObjType );
1197 /***********************************************************************
1198 * IsGDIObject (GDI.462)
1200 * returns type of object if valid (W95 system programming secrets p. 264-5)
1202 BOOL16 WINAPI IsGDIObject16( HGDIOBJ16 handle16 )
1205 HGDIOBJ handle = HGDIOBJ_32( handle16 );
1207 GDIOBJHDR *object = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
1210 magic = GDIMAGIC(object->wMagic) - PEN_MAGIC + 1;
1211 GDI_ReleaseObj( handle );
1217 /***********************************************************************
1218 * SetObjectOwner (GDI32.@)
1220 void WINAPI SetObjectOwner( HGDIOBJ handle, HANDLE owner )
1226 /***********************************************************************
1227 * MakeObjectPrivate (GDI.463)
1229 * What does that mean ?
1230 * Some little docu can be found in "Undocumented Windows",
1231 * but this is basically useless.
1232 * At least we know that this flags the GDI object's wMagic
1233 * with 0x2000 (OBJECT_PRIVATE), so we just do it.
1234 * But Wine doesn't react on that yet.
1236 void WINAPI MakeObjectPrivate16( HGDIOBJ16 handle16, BOOL16 private )
1238 HGDIOBJ handle = HGDIOBJ_32( handle16 );
1239 GDIOBJHDR *ptr = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
1242 ERR("invalid GDI object %p !\n", handle);
1245 ptr->wMagic |= OBJECT_PRIVATE;
1246 GDI_ReleaseObj( handle );
1250 /***********************************************************************
1251 * GdiFlush (GDI32.@)
1253 BOOL WINAPI GdiFlush(void)
1255 return TRUE; /* FIXME */
1259 /***********************************************************************
1260 * GdiGetBatchLimit (GDI32.@)
1262 DWORD WINAPI GdiGetBatchLimit(void)
1264 return 1; /* FIXME */
1268 /***********************************************************************
1269 * GdiSetBatchLimit (GDI32.@)
1271 DWORD WINAPI GdiSetBatchLimit( DWORD limit )
1273 return 1; /* FIXME */
1277 /***********************************************************************
1278 * GdiSeeGdiDo (GDI.452)
1280 DWORD WINAPI GdiSeeGdiDo16( WORD wReqType, WORD wParam1, WORD wParam2,
1285 case 0x0001: /* LocalAlloc */
1286 return LOCAL_Alloc( GDI_HeapSel, wParam1, wParam3 );
1287 case 0x0002: /* LocalFree */
1288 return LOCAL_Free( GDI_HeapSel, wParam1 );
1289 case 0x0003: /* LocalCompact */
1290 return LOCAL_Compact( GDI_HeapSel, wParam3, 0 );
1291 case 0x0103: /* LocalHeap */
1294 WARN("(wReqType=%04x): Unknown\n", wReqType);
1299 /***********************************************************************
1300 * GdiSignalProc32 (GDI.610)
1302 WORD WINAPI GdiSignalProc( UINT uCode, DWORD dwThreadOrProcessID,
1303 DWORD dwFlags, HMODULE16 hModule )
1308 /***********************************************************************
1309 * GdiInit2 (GDI.403)
1311 * See "Undocumented Windows"
1313 HANDLE16 WINAPI GdiInit216(
1314 HANDLE16 h1, /* [in] GDI object */
1315 HANDLE16 h2 /* [in] global data */
1318 FIXME("(%04x, %04x), stub.\n", h1, h2);
1320 return 0xffff; /* undefined return value */
1321 return h1; /* FIXME: should be the memory handle of h1 */
1324 /***********************************************************************
1325 * FinalGdiInit (GDI.405)
1327 void WINAPI FinalGdiInit16( HBRUSH16 hPattern /* [in] fill pattern of desktop */ )
1331 /***********************************************************************
1332 * GdiFreeResources (GDI.609)
1334 WORD WINAPI GdiFreeResources16( DWORD reserve )
1336 return (WORD)( (int)LOCAL_CountFree( GDI_HeapSel ) * 100 /
1337 (int)LOCAL_HeapSize( GDI_HeapSel ) );
1341 /*******************************************************************
1342 * GetColorAdjustment [GDI32.@]
1346 BOOL WINAPI GetColorAdjustment(HDC hdc, LPCOLORADJUSTMENT lpca)
1348 FIXME("GetColorAdjustment, stub\n");
1352 /*******************************************************************
1353 * GetMiterLimit [GDI32.@]
1357 BOOL WINAPI GetMiterLimit(HDC hdc, PFLOAT peLimit)
1359 FIXME("GetMiterLimit, stub\n");
1363 /*******************************************************************
1364 * SetMiterLimit [GDI32.@]
1368 BOOL WINAPI SetMiterLimit(HDC hdc, FLOAT eNewLimit, PFLOAT peOldLimit)
1370 FIXME("SetMiterLimit, stub\n");
1374 /*******************************************************************
1375 * GdiComment [GDI32.@]
1379 BOOL WINAPI GdiComment(HDC hdc, UINT cbSize, const BYTE *lpData)
1381 DC *dc = DC_GetDCPtr(hdc);
1385 if (dc->funcs->pGdiComment)
1386 ret = dc->funcs->pGdiComment( dc->physDev, cbSize, lpData );
1388 GDI_ReleaseObj( hdc );
1392 /*******************************************************************
1393 * SetColorAdjustment [GDI32.@]
1397 BOOL WINAPI SetColorAdjustment(HDC hdc, const COLORADJUSTMENT* lpca)
1399 FIXME("SetColorAdjustment, stub\n");