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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "gdi_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 #define HGDIOBJ_32(h16) ((HGDIOBJ)(ULONG_PTR)(h16))
43 #define GDI_HEAP_SIZE 0xffe0
45 /***********************************************************************
49 static const LOGBRUSH WhiteBrush = { BS_SOLID, RGB(255,255,255), 0 };
50 static const LOGBRUSH BlackBrush = { BS_SOLID, RGB(0,0,0), 0 };
51 static const LOGBRUSH NullBrush = { BS_NULL, 0, 0 };
53 static const LOGBRUSH LtGrayBrush = { BS_SOLID, RGB(192,192,192), 0 };
54 static const LOGBRUSH GrayBrush = { BS_SOLID, RGB(128,128,128), 0 };
55 static const LOGBRUSH DkGrayBrush = { BS_SOLID, RGB(64,64,64), 0 };
57 static const LOGPEN WhitePen = { PS_SOLID, { 0, 0 }, RGB(255,255,255) };
58 static const LOGPEN BlackPen = { PS_SOLID, { 0, 0 }, RGB(0,0,0) };
59 static const LOGPEN NullPen = { PS_NULL, { 0, 0 }, 0 };
61 static const LOGBRUSH DCBrush = { BS_SOLID, RGB(255,255,255), 0 };
62 static const LOGPEN DCPen = { PS_SOLID, { 0, 0 }, RGB(0,0,0) };
64 /* reserve one extra entry for the stock default bitmap */
65 /* this is what Windows does too */
66 #define NB_STOCK_OBJECTS (STOCK_LAST+2)
68 static HGDIOBJ stock_objects[NB_STOCK_OBJECTS];
70 static CRITICAL_SECTION gdi_section;
71 static CRITICAL_SECTION_DEBUG critsect_debug =
74 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
75 0, 0, { (DWORD_PTR)(__FILE__ ": gdi_section") }
77 static CRITICAL_SECTION gdi_section = { &critsect_debug, -1, 0, 0, 0, 0 };
80 /****************************************************************************
82 * language-independent stock fonts
86 static const LOGFONTW OEMFixedFont =
87 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, OEM_CHARSET,
88 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, {'\0'} };
90 static const LOGFONTW AnsiFixedFont =
91 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
92 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
93 {'C','o','u','r','i','e','r','\0'} };
95 static const LOGFONTW AnsiVarFont =
96 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
97 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
98 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'} };
100 /******************************************************************************
102 * language-dependent stock fonts
104 * 'ANSI' charset and 'DEFAULT' charset is not same.
105 * The chars in CP_ACP should be drawn with 'DEFAULT' charset.
106 * 'ANSI' charset seems to be identical with ISO-8859-1.
107 * 'DEFAULT' charset is a language-dependent charset.
109 * 'System' font seems to be an alias for language-dependent font.
113 * language-dependent stock fonts for all known charsets
114 * please see TranslateCharsetInfo (dlls/gdi/font.c) and
115 * CharsetBindingInfo (dlls/x11drv/xfont.c),
116 * and modify entries for your language if needed.
118 struct DefaultFontInfo
122 LOGFONTW DeviceDefaultFont;
123 LOGFONTW SystemFixedFont;
124 LOGFONTW DefaultGuiFont; /* Note for this font the lfHeight member should be the point size */
127 static const struct DefaultFontInfo default_fonts[] =
131 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
132 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
133 {'S','y','s','t','e','m','\0'}
135 { /* Device Default */
136 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
137 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
141 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
142 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
145 { /* DefaultGuiFont */
146 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
147 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
148 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
151 { EASTEUROPE_CHARSET,
153 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
154 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
155 {'S','y','s','t','e','m','\0'}
157 { /* Device Default */
158 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
159 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
163 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
164 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
167 { /* DefaultGuiFont */
168 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
169 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
170 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
175 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
176 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
177 {'S','y','s','t','e','m','\0'}
179 { /* Device Default */
180 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
181 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
185 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
186 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
189 { /* DefaultGuiFont */
190 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
191 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
192 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
197 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
198 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
199 {'S','y','s','t','e','m','\0'}
201 { /* Device Default */
202 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
203 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
207 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
208 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
211 { /* DefaultGuiFont */
212 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
213 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
214 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
219 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
220 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
221 {'S','y','s','t','e','m','\0'}
223 { /* Device Default */
224 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
225 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
229 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
230 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
233 { /* DefaultGuiFont */
234 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
235 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
236 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
241 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
242 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
243 {'S','y','s','t','e','m','\0'}
245 { /* Device Default */
246 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
247 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
251 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
252 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
255 { /* DefaultGuiFont */
256 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
257 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
258 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
263 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
264 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
265 {'S','y','s','t','e','m','\0'}
267 { /* Device Default */
268 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
269 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
273 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
274 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
277 { /* DefaultGuiFont */
278 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
279 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
280 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
285 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
286 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
287 {'S','y','s','t','e','m','\0'}
289 { /* Device Default */
290 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
291 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
295 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
296 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
299 { /* DefaultGuiFont */
300 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
301 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
302 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
307 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
308 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
309 {'S','y','s','t','e','m','\0'}
311 { /* Device Default */
312 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
313 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
317 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
318 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
321 { /* DefaultGuiFont */
322 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
323 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
324 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
329 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
330 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
331 {'S','y','s','t','e','m','\0'}
333 { /* Device Default */
334 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
335 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
339 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
340 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
343 { /* DefaultGuiFont */
344 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
345 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
346 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
351 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
352 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
353 {'S','y','s','t','e','m','\0'}
355 { /* Device Default */
356 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
357 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
361 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
362 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
365 { /* DefaultGuiFont */
366 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
367 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
368 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
373 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
374 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
375 {'S','y','s','t','e','m','\0'}
377 { /* Device Default */
378 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
379 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
383 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
384 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
387 { /* DefaultGuiFont */
388 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
389 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
390 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
393 { CHINESEBIG5_CHARSET,
395 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
396 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
397 {'S','y','s','t','e','m','\0'}
399 { /* Device Default */
400 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
401 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
405 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
406 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
409 { /* DefaultGuiFont */
410 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
411 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
412 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
417 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
418 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
419 {'S','y','s','t','e','m','\0'}
421 { /* Device Default */
422 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
423 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
427 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
428 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
431 { /* DefaultGuiFont */
432 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
433 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
434 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
440 /*************************************************************************
441 * __wine_make_gdi_object_system (GDI32.@)
443 * USER has to tell GDI that its system brushes and pens are non-deletable.
444 * For a description of the GDI object magics and their flags,
445 * see "Undocumented Windows" (wrong about the OBJECT_NOSYSTEM flag, though).
447 void CDECL __wine_make_gdi_object_system( HGDIOBJ handle, BOOL set)
449 GDIOBJHDR *ptr = GDI_GetObjPtr( handle, 0 );
451 GDI_ReleaseObj( handle );
454 /******************************************************************************
457 static const struct DefaultFontInfo* get_default_fonts(UINT charset)
461 for(n=0;n<(sizeof(default_fonts)/sizeof(default_fonts[0]));n++)
463 if ( default_fonts[n].charset == charset )
464 return &default_fonts[n];
467 FIXME( "unhandled charset 0x%08x - use ANSI_CHARSET for default stock objects\n", charset );
468 return &default_fonts[0];
472 /******************************************************************************
473 * get_default_charset (internal)
475 * get the language-dependent charset that can handle CP_ACP correctly.
477 static UINT get_default_charset( void )
483 csi.ciCharset = ANSI_CHARSET;
484 if ( !TranslateCharsetInfo( ULongToPtr(uACP), &csi, TCI_SRCCODEPAGE ) )
486 FIXME( "unhandled codepage %u - use ANSI_CHARSET for default stock objects\n", uACP );
490 return csi.ciCharset;
493 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
494 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
496 /******************************************************************************
499 * get the dpi from the registry
501 static DWORD get_dpi( void )
506 if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
508 DWORD type, size, new_dpi;
510 size = sizeof(new_dpi);
511 if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
513 if(type == REG_DWORD && new_dpi != 0)
522 /***********************************************************************
525 * Increment the reference count of a GDI object.
527 HGDIOBJ GDI_inc_ref_count( HGDIOBJ handle )
531 if ((header = GDI_GetObjPtr( handle, 0 )))
534 GDI_ReleaseObj( handle );
542 /***********************************************************************
545 * Decrement the reference count of a GDI object.
547 BOOL GDI_dec_ref_count( HGDIOBJ handle )
551 if ((header = GDI_GetObjPtr( handle, 0 )))
553 assert( header->selcount );
554 if (!--header->selcount && header->deleted)
556 /* handle delayed DeleteObject*/
558 GDI_ReleaseObj( handle );
559 TRACE( "executing delayed DeleteObject for %p\n", handle );
560 DeleteObject( handle );
562 else GDI_ReleaseObj( handle );
564 return header != NULL;
568 /***********************************************************************
571 * GDI initialization.
573 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
575 LOGFONTW default_gui_font;
576 const struct DefaultFontInfo* deffonts;
579 if (reason != DLL_PROCESS_ATTACH) return TRUE;
581 DisableThreadLibraryCalls( inst );
584 /* create stock objects */
585 stock_objects[WHITE_BRUSH] = CreateBrushIndirect( &WhiteBrush );
586 stock_objects[LTGRAY_BRUSH] = CreateBrushIndirect( &LtGrayBrush );
587 stock_objects[GRAY_BRUSH] = CreateBrushIndirect( &GrayBrush );
588 stock_objects[DKGRAY_BRUSH] = CreateBrushIndirect( &DkGrayBrush );
589 stock_objects[BLACK_BRUSH] = CreateBrushIndirect( &BlackBrush );
590 stock_objects[NULL_BRUSH] = CreateBrushIndirect( &NullBrush );
592 stock_objects[WHITE_PEN] = CreatePenIndirect( &WhitePen );
593 stock_objects[BLACK_PEN] = CreatePenIndirect( &BlackPen );
594 stock_objects[NULL_PEN] = CreatePenIndirect( &NullPen );
596 stock_objects[DEFAULT_PALETTE] = PALETTE_Init();
597 stock_objects[DEFAULT_BITMAP] = CreateBitmap( 1, 1, 1, 1, NULL );
599 /* language-independent stock fonts */
600 stock_objects[OEM_FIXED_FONT] = CreateFontIndirectW( &OEMFixedFont );
601 stock_objects[ANSI_FIXED_FONT] = CreateFontIndirectW( &AnsiFixedFont );
602 stock_objects[ANSI_VAR_FONT] = CreateFontIndirectW( &AnsiVarFont );
604 /* language-dependent stock fonts */
605 deffonts = get_default_fonts(get_default_charset());
606 stock_objects[SYSTEM_FONT] = CreateFontIndirectW( &deffonts->SystemFont );
607 stock_objects[DEVICE_DEFAULT_FONT] = CreateFontIndirectW( &deffonts->DeviceDefaultFont );
608 stock_objects[SYSTEM_FIXED_FONT] = CreateFontIndirectW( &deffonts->SystemFixedFont );
610 /* For the default gui font, we use the lfHeight member in deffonts as a place-holder
611 for the point size so we must convert this into a true height */
612 default_gui_font = deffonts->DefaultGuiFont;
613 default_gui_font.lfHeight = -MulDiv(default_gui_font.lfHeight, get_dpi(), 72);
614 stock_objects[DEFAULT_GUI_FONT] = CreateFontIndirectW( &default_gui_font );
616 stock_objects[DC_BRUSH] = CreateBrushIndirect( &DCBrush );
617 stock_objects[DC_PEN] = CreatePenIndirect( &DCPen );
619 /* clear the NOSYSTEM bit on all stock objects*/
620 for (i = 0; i < NB_STOCK_OBJECTS; i++)
622 if (!stock_objects[i])
624 if (i == 9) continue; /* there's no stock object 9 */
625 ERR( "could not create stock object %d\n", i );
628 __wine_make_gdi_object_system( stock_objects[i], TRUE );
634 #define FIRST_LARGE_HANDLE 16
635 #define MAX_LARGE_HANDLES ((GDI_HEAP_SIZE >> 2) - FIRST_LARGE_HANDLE)
636 static GDIOBJHDR *large_handles[MAX_LARGE_HANDLES];
637 static int next_large_handle;
638 static LONG debug_count;
640 static const char *gdi_obj_type( unsigned type )
644 case OBJ_PEN: return "OBJ_PEN";
645 case OBJ_BRUSH: return "OBJ_BRUSH";
646 case OBJ_DC: return "OBJ_DC";
647 case OBJ_METADC: return "OBJ_METADC";
648 case OBJ_PAL: return "OBJ_PAL";
649 case OBJ_FONT: return "OBJ_FONT";
650 case OBJ_BITMAP: return "OBJ_BITMAP";
651 case OBJ_REGION: return "OBJ_REGION";
652 case OBJ_METAFILE: return "OBJ_METAFILE";
653 case OBJ_MEMDC: return "OBJ_MEMDC";
654 case OBJ_EXTPEN: return "OBJ_EXTPEN";
655 case OBJ_ENHMETADC: return "OBJ_ENHMETADC";
656 case OBJ_ENHMETAFILE: return "OBJ_ENHMETAFILE";
657 case OBJ_COLORSPACE: return "OBJ_COLORSPACE";
658 default: return "UNKNOWN";
662 static void dump_gdi_objects( void )
666 TRACE( "%u objects:\n", MAX_LARGE_HANDLES );
668 EnterCriticalSection( &gdi_section );
669 for (i = 0; i < MAX_LARGE_HANDLES; i++)
671 if (!large_handles[i])
673 TRACE( "index %d handle %p FREE\n", i, (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2) );
676 TRACE( "handle %p obj %p type %s selcount %u deleted %u\n",
677 (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2),
678 large_handles[i], gdi_obj_type( large_handles[i]->type ),
679 large_handles[i]->selcount, large_handles[i]->deleted );
681 LeaveCriticalSection( &gdi_section );
684 /***********************************************************************
687 * Allocate a GDI handle for an object, which must have been allocated on the process heap.
689 HGDIOBJ alloc_gdi_handle( GDIOBJHDR *obj, WORD type, const struct gdi_obj_funcs *funcs )
693 /* initialize the object header */
701 EnterCriticalSection( &gdi_section );
702 for (i = next_large_handle + 1; i < MAX_LARGE_HANDLES; i++)
703 if (!large_handles[i]) goto found;
704 for (i = 0; i <= next_large_handle; i++)
705 if (!large_handles[i]) goto found;
706 LeaveCriticalSection( &gdi_section );
708 ERR( "out of GDI object handles, expect a crash\n" );
709 if (TRACE_ON(gdi)) dump_gdi_objects();
713 large_handles[i] = obj;
714 next_large_handle = i;
715 LeaveCriticalSection( &gdi_section );
716 TRACE( "allocated %s %p %u/%u\n",
717 gdi_obj_type(type), (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2),
718 InterlockedIncrement( &debug_count ), MAX_LARGE_HANDLES );
719 return (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2);
723 /***********************************************************************
726 * Free a GDI handle and return a pointer to the object.
728 void *free_gdi_handle( HGDIOBJ handle )
730 GDIOBJHDR *object = NULL;
733 i = ((ULONG_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
734 if (i >= 0 && i < MAX_LARGE_HANDLES)
736 EnterCriticalSection( &gdi_section );
737 object = large_handles[i];
738 large_handles[i] = NULL;
739 LeaveCriticalSection( &gdi_section );
743 TRACE( "freed %s %p %u/%u\n", gdi_obj_type( object->type ), handle,
744 InterlockedDecrement( &debug_count ) + 1, MAX_LARGE_HANDLES );
745 object->type = 0; /* mark it as invalid */
746 object->funcs = NULL;
752 /***********************************************************************
755 * Return a pointer to the GDI object associated to the handle.
756 * Return NULL if the object has the wrong magic number.
757 * The object must be released with GDI_ReleaseObj.
759 void *GDI_GetObjPtr( HGDIOBJ handle, WORD type )
761 GDIOBJHDR *ptr = NULL;
764 EnterCriticalSection( &gdi_section );
766 i = ((UINT_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
767 if (i >= 0 && i < MAX_LARGE_HANDLES)
769 ptr = large_handles[i];
770 if (ptr && type && ptr->type != type) ptr = NULL;
775 LeaveCriticalSection( &gdi_section );
776 WARN( "Invalid handle %p\n", handle );
783 /***********************************************************************
787 void GDI_ReleaseObj( HGDIOBJ handle )
789 LeaveCriticalSection( &gdi_section );
793 /***********************************************************************
796 void GDI_CheckNotLock(void)
798 if (gdi_section.OwningThread == ULongToHandle(GetCurrentThreadId()) && gdi_section.RecursionCount)
800 ERR( "BUG: holding GDI lock\n" );
806 /***********************************************************************
807 * DeleteObject (GDI32.@)
809 * Delete a Gdi object.
812 * obj [I] Gdi object to delete
815 * Success: TRUE. If obj was not returned from GetStockObject(), any resources
816 * it consumed are released.
817 * Failure: FALSE, if obj is not a valid Gdi object, or is currently selected
820 BOOL WINAPI DeleteObject( HGDIOBJ obj )
822 /* Check if object is valid */
824 struct hdc_list *hdcs_head;
825 const struct gdi_obj_funcs *funcs;
828 if (HIWORD(obj)) return FALSE;
830 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
834 TRACE("Preserving system object %p\n", obj);
835 GDI_ReleaseObj( obj );
839 while ((hdcs_head = header->hdcs) != NULL)
841 DC *dc = get_dc_ptr(hdcs_head->hdc);
843 header->hdcs = hdcs_head->next;
844 TRACE("hdc %p has interest in %p\n", hdcs_head->hdc, obj);
848 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pDeleteObject );
849 GDI_ReleaseObj( obj ); /* release the GDI lock */
850 physdev->funcs->pDeleteObject( physdev, obj );
851 header = GDI_GetObjPtr( obj, 0 ); /* and grab it again */
852 release_dc_ptr( dc );
854 HeapFree(GetProcessHeap(), 0, hdcs_head);
855 if (!header) return FALSE;
858 if (header->selcount)
860 TRACE("delayed for %p because object in use, count %u\n", obj, header->selcount );
861 header->deleted = 1; /* mark for delete */
862 GDI_ReleaseObj( obj );
870 funcs = header->funcs;
871 GDI_ReleaseObj( obj );
872 if (funcs && funcs->pDeleteObject)
873 return funcs->pDeleteObject( obj );
878 /***********************************************************************
879 * GDI_hdc_using_object
881 * Call this if the dc requires DeleteObject notification
883 BOOL GDI_hdc_using_object(HGDIOBJ obj, HDC hdc)
886 struct hdc_list **pphdc;
888 TRACE("obj %p hdc %p\n", obj, hdc);
890 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
898 for(pphdc = &header->hdcs; *pphdc; pphdc = &(*pphdc)->next)
899 if((*pphdc)->hdc == hdc)
903 *pphdc = HeapAlloc(GetProcessHeap(), 0, sizeof(**pphdc));
905 (*pphdc)->next = NULL;
912 /***********************************************************************
913 * GDI_hdc_not_using_object
916 BOOL GDI_hdc_not_using_object(HGDIOBJ obj, HDC hdc)
919 struct hdc_list *phdc, **prev;
921 TRACE("obj %p hdc %p\n", obj, hdc);
923 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
932 prev = &header->hdcs;
935 if(phdc->hdc == hdc) {
937 HeapFree(GetProcessHeap(), 0, phdc);
949 /***********************************************************************
950 * GetStockObject (GDI32.@)
952 HGDIOBJ WINAPI GetStockObject( INT obj )
955 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
956 ret = stock_objects[obj];
957 TRACE("returning %p\n", ret );
962 /***********************************************************************
963 * GetObjectA (GDI32.@)
965 INT WINAPI GetObjectA( HGDIOBJ handle, INT count, LPVOID buffer )
967 const struct gdi_obj_funcs *funcs;
971 TRACE("%p %d %p\n", handle, count, buffer );
973 if (!(ptr = GDI_GetObjPtr( handle, 0 ))) return 0;
975 GDI_ReleaseObj( handle );
977 if (funcs && funcs->pGetObjectA)
979 if (buffer && ((ULONG_PTR)buffer >> 16) == 0) /* catch apps getting argument order wrong */
980 SetLastError( ERROR_NOACCESS );
982 result = funcs->pGetObjectA( handle, count, buffer );
985 SetLastError( ERROR_INVALID_HANDLE );
990 /***********************************************************************
991 * GetObjectW (GDI32.@)
993 INT WINAPI GetObjectW( HGDIOBJ handle, INT count, LPVOID buffer )
995 const struct gdi_obj_funcs *funcs;
998 TRACE("%p %d %p\n", handle, count, buffer );
1000 if (!(ptr = GDI_GetObjPtr( handle, 0 ))) return 0;
1002 GDI_ReleaseObj( handle );
1004 if (funcs && funcs->pGetObjectW)
1006 if (buffer && ((ULONG_PTR)buffer >> 16) == 0) /* catch apps getting argument order wrong */
1007 SetLastError( ERROR_NOACCESS );
1009 result = funcs->pGetObjectW( handle, count, buffer );
1012 SetLastError( ERROR_INVALID_HANDLE );
1017 /***********************************************************************
1018 * GetObjectType (GDI32.@)
1020 DWORD WINAPI GetObjectType( HGDIOBJ handle )
1025 if (!(ptr = GDI_GetObjPtr( handle, 0 )))
1027 SetLastError( ERROR_INVALID_HANDLE );
1031 GDI_ReleaseObj( handle );
1032 TRACE("%p -> %u\n", handle, result );
1036 /***********************************************************************
1037 * GetCurrentObject (GDI32.@)
1039 * Get the currently selected object of a given type in a device context.
1042 * hdc [I] Device context to get the current object from
1043 * type [I] Type of current object to get (OBJ_* defines from "wingdi.h")
1046 * Success: The current object of the given type selected in hdc.
1047 * Failure: A NULL handle.
1050 * - only the following object types are supported:
1057 HGDIOBJ WINAPI GetCurrentObject(HDC hdc,UINT type)
1060 DC * dc = get_dc_ptr( hdc );
1065 case OBJ_EXTPEN: /* fall through */
1066 case OBJ_PEN: ret = dc->hPen; break;
1067 case OBJ_BRUSH: ret = dc->hBrush; break;
1068 case OBJ_PAL: ret = dc->hPalette; break;
1069 case OBJ_FONT: ret = dc->hFont; break;
1070 case OBJ_BITMAP: ret = dc->hBitmap; break;
1072 /* tests show that OBJ_REGION is explicitly ignored */
1073 case OBJ_REGION: break;
1075 /* the SDK only mentions those above */
1076 FIXME("(%p,%d): unknown type.\n",hdc,type);
1079 release_dc_ptr( dc );
1084 /***********************************************************************
1085 * SelectObject (GDI32.@)
1087 * Select a Gdi object into a device context.
1090 * hdc [I] Device context to associate the object with
1091 * hObj [I] Gdi object to associate with hdc
1094 * Success: A non-NULL handle representing the previously selected object of
1095 * the same type as hObj.
1096 * Failure: A NULL object. If hdc is invalid, GetLastError() returns ERROR_INVALID_HANDLE.
1097 * if hObj is not a valid object handle, no last error is set. In either
1098 * case, hdc is unaffected by the call.
1100 HGDIOBJ WINAPI SelectObject( HDC hdc, HGDIOBJ hObj )
1105 TRACE( "(%p,%p)\n", hdc, hObj );
1107 header = GDI_GetObjPtr( hObj, 0 );
1110 const struct gdi_obj_funcs *funcs = header->funcs;
1111 GDI_ReleaseObj( hObj );
1112 if (funcs && funcs->pSelectObject) ret = funcs->pSelectObject( hObj, hdc );
1118 /***********************************************************************
1119 * UnrealizeObject (GDI32.@)
1121 BOOL WINAPI UnrealizeObject( HGDIOBJ obj )
1123 BOOL result = FALSE;
1124 GDIOBJHDR * header = GDI_GetObjPtr( obj, 0 );
1128 const struct gdi_obj_funcs *funcs = header->funcs;
1130 GDI_ReleaseObj( obj );
1131 if (funcs && funcs->pUnrealizeObject)
1132 result = header->funcs->pUnrealizeObject( obj );
1140 /* Solid colors to enumerate */
1141 static const COLORREF solid_colors[] =
1142 { RGB(0x00,0x00,0x00), RGB(0xff,0xff,0xff),
1143 RGB(0xff,0x00,0x00), RGB(0x00,0xff,0x00),
1144 RGB(0x00,0x00,0xff), RGB(0xff,0xff,0x00),
1145 RGB(0xff,0x00,0xff), RGB(0x00,0xff,0xff),
1146 RGB(0x80,0x00,0x00), RGB(0x00,0x80,0x00),
1147 RGB(0x80,0x80,0x00), RGB(0x00,0x00,0x80),
1148 RGB(0x80,0x00,0x80), RGB(0x00,0x80,0x80),
1149 RGB(0x80,0x80,0x80), RGB(0xc0,0xc0,0xc0)
1153 /***********************************************************************
1154 * EnumObjects (GDI32.@)
1156 INT WINAPI EnumObjects( HDC hdc, INT nObjType,
1157 GOBJENUMPROC lpEnumFunc, LPARAM lParam )
1164 TRACE("%p %d %p %08lx\n", hdc, nObjType, lpEnumFunc, lParam );
1168 /* Enumerate solid pens */
1169 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1171 pen.lopnStyle = PS_SOLID;
1172 pen.lopnWidth.x = 1;
1173 pen.lopnWidth.y = 0;
1174 pen.lopnColor = solid_colors[i];
1175 retval = lpEnumFunc( &pen, lParam );
1176 TRACE("solid pen %08x, ret=%d\n",
1177 solid_colors[i], retval);
1183 /* Enumerate solid brushes */
1184 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1186 brush.lbStyle = BS_SOLID;
1187 brush.lbColor = solid_colors[i];
1189 retval = lpEnumFunc( &brush, lParam );
1190 TRACE("solid brush %08x, ret=%d\n",
1191 solid_colors[i], retval);
1195 /* Now enumerate hatched brushes */
1196 if (retval) for (i = HS_HORIZONTAL; i <= HS_DIAGCROSS; i++)
1198 brush.lbStyle = BS_HATCHED;
1199 brush.lbColor = RGB(0,0,0);
1201 retval = lpEnumFunc( &brush, lParam );
1202 TRACE("hatched brush %d, ret=%d\n",
1209 /* FIXME: implement Win32 types */
1210 WARN("(%d): Invalid type\n", nObjType );
1217 /***********************************************************************
1218 * SetObjectOwner (GDI32.@)
1220 void WINAPI SetObjectOwner( HGDIOBJ handle, HANDLE owner )
1225 /***********************************************************************
1226 * GdiInitializeLanguagePack (GDI32.@)
1228 DWORD WINAPI GdiInitializeLanguagePack( DWORD arg )
1234 /***********************************************************************
1235 * GdiFlush (GDI32.@)
1237 BOOL WINAPI GdiFlush(void)
1239 return TRUE; /* FIXME */
1243 /***********************************************************************
1244 * GdiGetBatchLimit (GDI32.@)
1246 DWORD WINAPI GdiGetBatchLimit(void)
1248 return 1; /* FIXME */
1252 /***********************************************************************
1253 * GdiSetBatchLimit (GDI32.@)
1255 DWORD WINAPI GdiSetBatchLimit( DWORD limit )
1257 return 1; /* FIXME */
1261 /*******************************************************************
1262 * GetColorAdjustment [GDI32.@]
1266 BOOL WINAPI GetColorAdjustment(HDC hdc, LPCOLORADJUSTMENT lpca)
1272 /*******************************************************************
1273 * GdiComment [GDI32.@]
1277 BOOL WINAPI GdiComment(HDC hdc, UINT cbSize, const BYTE *lpData)
1279 DC *dc = get_dc_ptr(hdc);
1284 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pGdiComment );
1285 ret = physdev->funcs->pGdiComment( physdev, cbSize, lpData );
1286 release_dc_ptr( dc );
1291 /*******************************************************************
1292 * SetColorAdjustment [GDI32.@]
1296 BOOL WINAPI SetColorAdjustment(HDC hdc, const COLORADJUSTMENT* lpca)