push 0a4ef4c8802fa61eba589064e60bc90a80182312
[wine/hacks.git] / dlls / gdi32 / gdiobj.c
blob7d03792041ce8b36ca94ba4eefd5fdbd1a51657e
1 /*
2 * GDI functions
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
21 #include "config.h"
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winreg.h"
32 #include "winnls.h"
33 #include "winerror.h"
34 #include "winternl.h"
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 /***********************************************************************
46 * GDI stock objects
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 =
73 0, 0, &gdi_section,
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, {'\0'} };
94 static const LOGFONTW AnsiVarFont =
95 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
96 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
97 {'M','S',' ','S','a','n','s',' ','S','e','r','i','f','\0'} };
99 /******************************************************************************
101 * language-dependent stock fonts
103 * 'ANSI' charset and 'DEFAULT' charset is not same.
104 * The chars in CP_ACP should be drawn with 'DEFAULT' charset.
105 * 'ANSI' charset seems to be identical with ISO-8859-1.
106 * 'DEFAULT' charset is a language-dependent charset.
108 * 'System' font seems to be an alias for language-dependent font.
112 * language-dependent stock fonts for all known charsets
113 * please see TranslateCharsetInfo (dlls/gdi/font.c) and
114 * CharsetBindingInfo (dlls/x11drv/xfont.c),
115 * and modify entries for your language if needed.
117 struct DefaultFontInfo
119 UINT charset;
120 LOGFONTW SystemFont;
121 LOGFONTW DeviceDefaultFont;
122 LOGFONTW SystemFixedFont;
123 LOGFONTW DefaultGuiFont; /* Note for this font the lfHeight member should be the point size */
126 static const struct DefaultFontInfo default_fonts[] =
128 { ANSI_CHARSET,
129 { /* System */
130 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
131 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
132 {'S','y','s','t','e','m','\0'}
134 { /* Device Default */
135 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
136 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
137 {'\0'}
139 { /* System Fixed */
140 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
141 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
142 {'\0'}
144 { /* DefaultGuiFont */
145 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
146 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
147 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
150 { EASTEUROPE_CHARSET,
151 { /* System */
152 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
153 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
154 {'S','y','s','t','e','m','\0'}
156 { /* Device Default */
157 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
158 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
159 {'\0'}
161 { /* System Fixed */
162 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
163 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
164 {'\0'}
166 { /* DefaultGuiFont */
167 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET,
168 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
169 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
172 { RUSSIAN_CHARSET,
173 { /* System */
174 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
175 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
176 {'S','y','s','t','e','m','\0'}
178 { /* Device Default */
179 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
180 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
181 {'\0'}
183 { /* System Fixed */
184 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
185 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
186 {'\0'}
188 { /* DefaultGuiFont */
189 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, RUSSIAN_CHARSET,
190 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
191 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
194 { GREEK_CHARSET,
195 { /* System */
196 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
197 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
198 {'S','y','s','t','e','m','\0'}
200 { /* Device Default */
201 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
202 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
203 {'\0'}
205 { /* System Fixed */
206 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
207 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
208 {'\0'}
210 { /* DefaultGuiFont */
211 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GREEK_CHARSET,
212 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
213 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
216 { TURKISH_CHARSET,
217 { /* System */
218 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
219 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
220 {'S','y','s','t','e','m','\0'}
222 { /* Device Default */
223 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
224 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
225 {'\0'}
227 { /* System Fixed */
228 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
229 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
230 {'\0'}
232 { /* DefaultGuiFont */
233 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, TURKISH_CHARSET,
234 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
235 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
238 { HEBREW_CHARSET,
239 { /* System */
240 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
241 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
242 {'S','y','s','t','e','m','\0'}
244 { /* Device Default */
245 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
246 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
247 {'\0'}
249 { /* System Fixed */
250 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
251 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
252 {'\0'}
254 { /* DefaultGuiFont */
255 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HEBREW_CHARSET,
256 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
257 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
260 { ARABIC_CHARSET,
261 { /* System */
262 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
263 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
264 {'S','y','s','t','e','m','\0'}
266 { /* Device Default */
267 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
268 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
269 {'\0'}
271 { /* System Fixed */
272 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
273 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
274 {'\0'}
276 { /* DefaultGuiFont */
277 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ARABIC_CHARSET,
278 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
279 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
282 { BALTIC_CHARSET,
283 { /* System */
284 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
285 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
286 {'S','y','s','t','e','m','\0'}
288 { /* Device Default */
289 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
290 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
291 {'\0'}
293 { /* System Fixed */
294 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
295 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
296 {'\0'}
298 { /* DefaultGuiFont */
299 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, BALTIC_CHARSET,
300 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
301 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
304 { THAI_CHARSET,
305 { /* System */
306 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
307 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
308 {'S','y','s','t','e','m','\0'}
310 { /* Device Default */
311 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
312 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
313 {'\0'}
315 { /* System Fixed */
316 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
317 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
318 {'\0'}
320 { /* DefaultGuiFont */
321 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, THAI_CHARSET,
322 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
323 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
326 { SHIFTJIS_CHARSET,
327 { /* System */
328 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
329 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
330 {'S','y','s','t','e','m','\0'}
332 { /* Device Default */
333 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
334 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
335 {'\0'}
337 { /* System Fixed */
338 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
339 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
340 {'\0'}
342 { /* DefaultGuiFont */
343 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, SHIFTJIS_CHARSET,
344 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
345 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
348 { GB2312_CHARSET,
349 { /* System */
350 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
351 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
352 {'S','y','s','t','e','m','\0'}
354 { /* Device Default */
355 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
356 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
357 {'\0'}
359 { /* System Fixed */
360 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
361 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
362 {'\0'}
364 { /* DefaultGuiFont */
365 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, GB2312_CHARSET,
366 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
367 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
370 { HANGEUL_CHARSET,
371 { /* System */
372 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
373 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
374 {'S','y','s','t','e','m','\0'}
376 { /* Device Default */
377 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
378 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
379 {'\0'}
381 { /* System Fixed */
382 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
383 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
384 {'\0'}
386 { /* DefaultGuiFont */
387 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, HANGEUL_CHARSET,
388 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
389 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
392 { CHINESEBIG5_CHARSET,
393 { /* System */
394 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
395 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
396 {'S','y','s','t','e','m','\0'}
398 { /* Device Default */
399 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
400 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
401 {'\0'}
403 { /* System Fixed */
404 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
405 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
406 {'\0'}
408 { /* DefaultGuiFont */
409 9, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, CHINESEBIG5_CHARSET,
410 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
411 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
414 { JOHAB_CHARSET,
415 { /* System */
416 16, 7, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
417 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
418 {'S','y','s','t','e','m','\0'}
420 { /* Device Default */
421 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
422 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
423 {'\0'}
425 { /* System Fixed */
426 16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
427 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN,
428 {'\0'}
430 { /* DefaultGuiFont */
431 8, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, JOHAB_CHARSET,
432 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS,
433 {'M','S',' ','S','h','e','l','l',' ','D','l','g','\0'}
439 /*************************************************************************
440 * __wine_make_gdi_object_system (GDI32.@)
442 * USER has to tell GDI that its system brushes and pens are non-deletable.
443 * For a description of the GDI object magics and their flags,
444 * see "Undocumented Windows" (wrong about the OBJECT_NOSYSTEM flag, though).
446 void CDECL __wine_make_gdi_object_system( HGDIOBJ handle, BOOL set)
448 GDIOBJHDR *ptr = GDI_GetObjPtr( handle, 0 );
449 ptr->system = !!set;
450 GDI_ReleaseObj( handle );
453 /******************************************************************************
454 * get_default_fonts
456 static const struct DefaultFontInfo* get_default_fonts(UINT charset)
458 unsigned int n;
460 for(n=0;n<(sizeof(default_fonts)/sizeof(default_fonts[0]));n++)
462 if ( default_fonts[n].charset == charset )
463 return &default_fonts[n];
466 FIXME( "unhandled charset 0x%08x - use ANSI_CHARSET for default stock objects\n", charset );
467 return &default_fonts[0];
471 /******************************************************************************
472 * get_default_charset (internal)
474 * get the language-dependent charset that can handle CP_ACP correctly.
476 static UINT get_default_charset( void )
478 CHARSETINFO csi;
479 UINT uACP;
481 uACP = GetACP();
482 csi.ciCharset = ANSI_CHARSET;
483 if ( !TranslateCharsetInfo( ULongToPtr(uACP), &csi, TCI_SRCCODEPAGE ) )
485 FIXME( "unhandled codepage %u - use ANSI_CHARSET for default stock objects\n", uACP );
486 return ANSI_CHARSET;
489 return csi.ciCharset;
492 static const WCHAR dpi_key_name[] = {'S','o','f','t','w','a','r','e','\\','F','o','n','t','s','\0'};
493 static const WCHAR dpi_value_name[] = {'L','o','g','P','i','x','e','l','s','\0'};
495 /******************************************************************************
496 * get_dpi (internal)
498 * get the dpi from the registry
500 static DWORD get_dpi( void )
502 DWORD dpi = 96;
503 HKEY hkey;
505 if (RegOpenKeyW(HKEY_CURRENT_CONFIG, dpi_key_name, &hkey) == ERROR_SUCCESS)
507 DWORD type, size, new_dpi;
509 size = sizeof(new_dpi);
510 if(RegQueryValueExW(hkey, dpi_value_name, NULL, &type, (void *)&new_dpi, &size) == ERROR_SUCCESS)
512 if(type == REG_DWORD && new_dpi != 0)
513 dpi = new_dpi;
515 RegCloseKey(hkey);
517 return dpi;
521 /***********************************************************************
522 * GDI_inc_ref_count
524 * Increment the reference count of a GDI object.
526 HGDIOBJ GDI_inc_ref_count( HGDIOBJ handle )
528 GDIOBJHDR *header;
530 if ((header = GDI_GetObjPtr( handle, 0 )))
532 header->selcount++;
533 GDI_ReleaseObj( handle );
535 else handle = 0;
537 return handle;
541 /***********************************************************************
542 * GDI_dec_ref_count
544 * Decrement the reference count of a GDI object.
546 BOOL GDI_dec_ref_count( HGDIOBJ handle )
548 GDIOBJHDR *header;
550 if ((header = GDI_GetObjPtr( handle, 0 )))
552 assert( header->selcount );
553 if (!--header->selcount && header->deleted)
555 /* handle delayed DeleteObject*/
556 header->deleted = 0;
557 GDI_ReleaseObj( handle );
558 TRACE( "executing delayed DeleteObject for %p\n", handle );
559 DeleteObject( handle );
561 else GDI_ReleaseObj( handle );
563 return header != NULL;
567 /***********************************************************************
568 * DllMain
570 * GDI initialization.
572 BOOL WINAPI DllMain( HINSTANCE inst, DWORD reason, LPVOID reserved )
574 LOGFONTW default_gui_font;
575 const struct DefaultFontInfo* deffonts;
576 int i;
578 if (reason != DLL_PROCESS_ATTACH) return TRUE;
580 DisableThreadLibraryCalls( inst );
581 LoadLibraryA( "gdi.exe16" );
583 WineEngInit();
585 /* create stock objects */
586 stock_objects[WHITE_BRUSH] = CreateBrushIndirect( &WhiteBrush );
587 stock_objects[LTGRAY_BRUSH] = CreateBrushIndirect( &LtGrayBrush );
588 stock_objects[GRAY_BRUSH] = CreateBrushIndirect( &GrayBrush );
589 stock_objects[DKGRAY_BRUSH] = CreateBrushIndirect( &DkGrayBrush );
590 stock_objects[BLACK_BRUSH] = CreateBrushIndirect( &BlackBrush );
591 stock_objects[NULL_BRUSH] = CreateBrushIndirect( &NullBrush );
593 stock_objects[WHITE_PEN] = CreatePenIndirect( &WhitePen );
594 stock_objects[BLACK_PEN] = CreatePenIndirect( &BlackPen );
595 stock_objects[NULL_PEN] = CreatePenIndirect( &NullPen );
597 stock_objects[DEFAULT_PALETTE] = PALETTE_Init();
598 stock_objects[DEFAULT_BITMAP] = CreateBitmap( 1, 1, 1, 1, NULL );
600 /* language-independent stock fonts */
601 stock_objects[OEM_FIXED_FONT] = CreateFontIndirectW( &OEMFixedFont );
602 stock_objects[ANSI_FIXED_FONT] = CreateFontIndirectW( &AnsiFixedFont );
603 stock_objects[ANSI_VAR_FONT] = CreateFontIndirectW( &AnsiVarFont );
605 /* language-dependent stock fonts */
606 deffonts = get_default_fonts(get_default_charset());
607 stock_objects[SYSTEM_FONT] = CreateFontIndirectW( &deffonts->SystemFont );
608 stock_objects[DEVICE_DEFAULT_FONT] = CreateFontIndirectW( &deffonts->DeviceDefaultFont );
609 stock_objects[SYSTEM_FIXED_FONT] = CreateFontIndirectW( &deffonts->SystemFixedFont );
611 /* For the default gui font, we use the lfHeight member in deffonts as a place-holder
612 for the point size so we must convert this into a true height */
613 default_gui_font = deffonts->DefaultGuiFont;
614 default_gui_font.lfHeight = -MulDiv(default_gui_font.lfHeight, get_dpi(), 72);
615 stock_objects[DEFAULT_GUI_FONT] = CreateFontIndirectW( &default_gui_font );
617 stock_objects[DC_BRUSH] = CreateBrushIndirect( &DCBrush );
618 stock_objects[DC_PEN] = CreatePenIndirect( &DCPen );
620 /* clear the NOSYSTEM bit on all stock objects*/
621 for (i = 0; i < NB_STOCK_OBJECTS; i++)
623 if (!stock_objects[i])
625 if (i == 9) continue; /* there's no stock object 9 */
626 ERR( "could not create stock object %d\n", i );
627 return FALSE;
629 __wine_make_gdi_object_system( stock_objects[i], TRUE );
632 return TRUE;
635 #define FIRST_LARGE_HANDLE 16
636 #define MAX_LARGE_HANDLES ((GDI_HEAP_SIZE >> 2) - FIRST_LARGE_HANDLE)
637 static GDIOBJHDR *large_handles[MAX_LARGE_HANDLES];
638 static int next_large_handle;
640 /***********************************************************************
641 * alloc_gdi_handle
643 * Allocate a GDI handle for an object, which must have been allocated on the process heap.
645 HGDIOBJ alloc_gdi_handle( GDIOBJHDR *obj, WORD type, const struct gdi_obj_funcs *funcs )
647 int i;
649 /* initialize the object header */
650 obj->type = type;
651 obj->system = 0;
652 obj->deleted = 0;
653 obj->selcount = 0;
654 obj->funcs = funcs;
655 obj->hdcs = NULL;
657 EnterCriticalSection( &gdi_section );
658 for (i = next_large_handle + 1; i < MAX_LARGE_HANDLES; i++)
659 if (!large_handles[i]) goto found;
660 for (i = 0; i <= next_large_handle; i++)
661 if (!large_handles[i]) goto found;
662 LeaveCriticalSection( &gdi_section );
663 return 0;
665 found:
666 large_handles[i] = obj;
667 next_large_handle = i;
668 LeaveCriticalSection( &gdi_section );
669 return (HGDIOBJ)(ULONG_PTR)((i + FIRST_LARGE_HANDLE) << 2);
673 /***********************************************************************
674 * free_gdi_handle
676 * Free a GDI handle and return a pointer to the object.
678 void *free_gdi_handle( HGDIOBJ handle )
680 GDIOBJHDR *object = NULL;
681 int i;
683 i = ((ULONG_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
684 if (i >= 0 && i < MAX_LARGE_HANDLES)
686 EnterCriticalSection( &gdi_section );
687 object = large_handles[i];
688 large_handles[i] = NULL;
689 LeaveCriticalSection( &gdi_section );
691 if (object)
693 object->type = 0; /* mark it as invalid */
694 object->funcs = NULL;
696 return object;
700 /***********************************************************************
701 * GDI_GetObjPtr
703 * Return a pointer to the GDI object associated to the handle.
704 * Return NULL if the object has the wrong magic number.
705 * The object must be released with GDI_ReleaseObj.
707 void *GDI_GetObjPtr( HGDIOBJ handle, WORD type )
709 GDIOBJHDR *ptr = NULL;
710 int i;
712 EnterCriticalSection( &gdi_section );
714 i = ((UINT_PTR)handle >> 2) - FIRST_LARGE_HANDLE;
715 if (i >= 0 && i < MAX_LARGE_HANDLES)
717 ptr = large_handles[i];
718 if (ptr && type && ptr->type != type) ptr = NULL;
721 if (!ptr)
723 LeaveCriticalSection( &gdi_section );
724 WARN( "Invalid handle %p\n", handle );
726 else TRACE("(%p): enter %d\n", handle, gdi_section.RecursionCount);
728 return ptr;
732 /***********************************************************************
733 * GDI_ReleaseObj
736 void GDI_ReleaseObj( HGDIOBJ handle )
738 TRACE("(%p): leave %d\n", handle, gdi_section.RecursionCount);
739 LeaveCriticalSection( &gdi_section );
743 /***********************************************************************
744 * GDI_CheckNotLock
746 void GDI_CheckNotLock(void)
748 if (gdi_section.OwningThread == ULongToHandle(GetCurrentThreadId()) && gdi_section.RecursionCount)
750 ERR( "BUG: holding GDI lock\n" );
751 DebugBreak();
756 /***********************************************************************
757 * DeleteObject (GDI32.@)
759 * Delete a Gdi object.
761 * PARAMS
762 * obj [I] Gdi object to delete
764 * RETURNS
765 * Success: TRUE. If obj was not returned from GetStockObject(), any resources
766 * it consumed are released.
767 * Failure: FALSE, if obj is not a valid Gdi object, or is currently selected
768 * into a DC.
770 BOOL WINAPI DeleteObject( HGDIOBJ obj )
772 /* Check if object is valid */
774 struct hdc_list *hdcs_head;
775 const struct gdi_obj_funcs *funcs;
776 GDIOBJHDR * header;
778 if (HIWORD(obj)) return FALSE;
780 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
782 if (header->system)
784 TRACE("Preserving system object %p\n", obj);
785 GDI_ReleaseObj( obj );
786 return TRUE;
789 while ((hdcs_head = header->hdcs) != NULL)
791 DC *dc = get_dc_ptr(hdcs_head->hdc);
793 header->hdcs = hdcs_head->next;
794 TRACE("hdc %p has interest in %p\n", hdcs_head->hdc, obj);
796 if(dc)
798 if(dc->funcs->pDeleteObject)
800 GDI_ReleaseObj( obj ); /* release the GDI lock */
801 dc->funcs->pDeleteObject( dc->physDev, obj );
802 header = GDI_GetObjPtr( obj, 0 ); /* and grab it again */
804 release_dc_ptr( dc );
806 HeapFree(GetProcessHeap(), 0, hdcs_head);
807 if (!header) return FALSE;
810 if (header->selcount)
812 TRACE("delayed for %p because object in use, count %u\n", obj, header->selcount );
813 header->deleted = 1; /* mark for delete */
814 GDI_ReleaseObj( obj );
815 return TRUE;
818 TRACE("%p\n", obj );
820 /* Delete object */
822 funcs = header->funcs;
823 GDI_ReleaseObj( obj );
824 if (funcs && funcs->pDeleteObject)
825 return funcs->pDeleteObject( obj );
826 else
827 return FALSE;
830 /***********************************************************************
831 * GDI_hdc_using_object
833 * Call this if the dc requires DeleteObject notification
835 BOOL GDI_hdc_using_object(HGDIOBJ obj, HDC hdc)
837 GDIOBJHDR * header;
838 struct hdc_list **pphdc;
840 TRACE("obj %p hdc %p\n", obj, hdc);
842 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
844 if (header->system)
846 GDI_ReleaseObj(obj);
847 return FALSE;
850 for(pphdc = &header->hdcs; *pphdc; pphdc = &(*pphdc)->next)
851 if((*pphdc)->hdc == hdc)
852 break;
854 if(!*pphdc) {
855 *pphdc = HeapAlloc(GetProcessHeap(), 0, sizeof(**pphdc));
856 (*pphdc)->hdc = hdc;
857 (*pphdc)->next = NULL;
860 GDI_ReleaseObj(obj);
861 return TRUE;
864 /***********************************************************************
865 * GDI_hdc_not_using_object
868 BOOL GDI_hdc_not_using_object(HGDIOBJ obj, HDC hdc)
870 GDIOBJHDR * header;
871 struct hdc_list *phdc, **prev;
873 TRACE("obj %p hdc %p\n", obj, hdc);
875 if (!(header = GDI_GetObjPtr( obj, 0 ))) return FALSE;
877 if (header->system)
879 GDI_ReleaseObj(obj);
880 return FALSE;
883 phdc = header->hdcs;
884 prev = &header->hdcs;
886 while(phdc) {
887 if(phdc->hdc == hdc) {
888 *prev = phdc->next;
889 HeapFree(GetProcessHeap(), 0, phdc);
890 phdc = *prev;
891 } else {
892 prev = &phdc->next;
893 phdc = phdc->next;
897 GDI_ReleaseObj(obj);
898 return TRUE;
901 /***********************************************************************
902 * GetStockObject (GDI32.@)
904 HGDIOBJ WINAPI GetStockObject( INT obj )
906 HGDIOBJ ret;
907 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
908 ret = stock_objects[obj];
909 TRACE("returning %p\n", ret );
910 return ret;
914 /***********************************************************************
915 * GetObjectA (GDI32.@)
917 INT WINAPI GetObjectA( HGDIOBJ handle, INT count, LPVOID buffer )
919 const struct gdi_obj_funcs *funcs;
920 GDIOBJHDR * ptr;
921 INT result = 0;
923 TRACE("%p %d %p\n", handle, count, buffer );
925 if (!(ptr = GDI_GetObjPtr( handle, 0 ))) return 0;
926 funcs = ptr->funcs;
927 GDI_ReleaseObj( handle );
929 if (funcs && funcs->pGetObjectA)
930 result = funcs->pGetObjectA( handle, count, buffer );
931 else
932 SetLastError( ERROR_INVALID_HANDLE );
934 return result;
937 /***********************************************************************
938 * GetObjectW (GDI32.@)
940 INT WINAPI GetObjectW( HGDIOBJ handle, INT count, LPVOID buffer )
942 const struct gdi_obj_funcs *funcs;
943 GDIOBJHDR * ptr;
944 INT result = 0;
945 TRACE("%p %d %p\n", handle, count, buffer );
947 if (!(ptr = GDI_GetObjPtr( handle, 0 ))) return 0;
948 funcs = ptr->funcs;
949 GDI_ReleaseObj( handle );
951 if (funcs && funcs->pGetObjectW)
952 result = funcs->pGetObjectW( handle, count, buffer );
953 else
954 SetLastError( ERROR_INVALID_HANDLE );
956 return result;
959 /***********************************************************************
960 * GetObjectType (GDI32.@)
962 DWORD WINAPI GetObjectType( HGDIOBJ handle )
964 GDIOBJHDR * ptr;
965 DWORD result;
967 if (!(ptr = GDI_GetObjPtr( handle, 0 )))
969 SetLastError( ERROR_INVALID_HANDLE );
970 return 0;
972 result = ptr->type;
973 GDI_ReleaseObj( handle );
974 TRACE("%p -> %u\n", handle, result );
975 return result;
978 /***********************************************************************
979 * GetCurrentObject (GDI32.@)
981 * Get the currently selected object of a given type in a device context.
983 * PARAMS
984 * hdc [I] Device context to get the current object from
985 * type [I] Type of current object to get (OBJ_* defines from "wingdi.h")
987 * RETURNS
988 * Success: The current object of the given type selected in hdc.
989 * Failure: A NULL handle.
991 * NOTES
992 * - only the following object types are supported:
993 *| OBJ_PEN
994 *| OBJ_BRUSH
995 *| OBJ_PAL
996 *| OBJ_FONT
997 *| OBJ_BITMAP
999 HGDIOBJ WINAPI GetCurrentObject(HDC hdc,UINT type)
1001 HGDIOBJ ret = 0;
1002 DC * dc = get_dc_ptr( hdc );
1004 if (!dc) return 0;
1006 switch (type) {
1007 case OBJ_EXTPEN: /* fall through */
1008 case OBJ_PEN: ret = dc->hPen; break;
1009 case OBJ_BRUSH: ret = dc->hBrush; break;
1010 case OBJ_PAL: ret = dc->hPalette; break;
1011 case OBJ_FONT: ret = dc->hFont; break;
1012 case OBJ_BITMAP: ret = dc->hBitmap; break;
1014 /* tests show that OBJ_REGION is explicitly ignored */
1015 case OBJ_REGION: break;
1016 default:
1017 /* the SDK only mentions those above */
1018 FIXME("(%p,%d): unknown type.\n",hdc,type);
1019 break;
1021 release_dc_ptr( dc );
1022 return ret;
1026 /***********************************************************************
1027 * SelectObject (GDI32.@)
1029 * Select a Gdi object into a device context.
1031 * PARAMS
1032 * hdc [I] Device context to associate the object with
1033 * hObj [I] Gdi object to associate with hdc
1035 * RETURNS
1036 * Success: A non-NULL handle representing the previously selected object of
1037 * the same type as hObj.
1038 * Failure: A NULL object. If hdc is invalid, GetLastError() returns ERROR_INVALID_HANDLE.
1039 * if hObj is not a valid object handle, no last error is set. In either
1040 * case, hdc is unaffected by the call.
1042 HGDIOBJ WINAPI SelectObject( HDC hdc, HGDIOBJ hObj )
1044 HGDIOBJ ret = 0;
1045 GDIOBJHDR *header;
1047 TRACE( "(%p,%p)\n", hdc, hObj );
1049 header = GDI_GetObjPtr( hObj, 0 );
1050 if (header)
1052 const struct gdi_obj_funcs *funcs = header->funcs;
1053 GDI_ReleaseObj( hObj );
1054 if (funcs && funcs->pSelectObject) ret = funcs->pSelectObject( hObj, hdc );
1056 return ret;
1060 /***********************************************************************
1061 * UnrealizeObject (GDI32.@)
1063 BOOL WINAPI UnrealizeObject( HGDIOBJ obj )
1065 BOOL result = FALSE;
1066 GDIOBJHDR * header = GDI_GetObjPtr( obj, 0 );
1068 if (header)
1070 const struct gdi_obj_funcs *funcs = header->funcs;
1072 GDI_ReleaseObj( obj );
1073 if (funcs && funcs->pUnrealizeObject)
1074 result = header->funcs->pUnrealizeObject( obj );
1075 else
1076 result = TRUE;
1078 return result;
1082 /* Solid colors to enumerate */
1083 static const COLORREF solid_colors[] =
1084 { RGB(0x00,0x00,0x00), RGB(0xff,0xff,0xff),
1085 RGB(0xff,0x00,0x00), RGB(0x00,0xff,0x00),
1086 RGB(0x00,0x00,0xff), RGB(0xff,0xff,0x00),
1087 RGB(0xff,0x00,0xff), RGB(0x00,0xff,0xff),
1088 RGB(0x80,0x00,0x00), RGB(0x00,0x80,0x00),
1089 RGB(0x80,0x80,0x00), RGB(0x00,0x00,0x80),
1090 RGB(0x80,0x00,0x80), RGB(0x00,0x80,0x80),
1091 RGB(0x80,0x80,0x80), RGB(0xc0,0xc0,0xc0)
1095 /***********************************************************************
1096 * EnumObjects (GDI32.@)
1098 INT WINAPI EnumObjects( HDC hdc, INT nObjType,
1099 GOBJENUMPROC lpEnumFunc, LPARAM lParam )
1101 UINT i;
1102 INT retval = 0;
1103 LOGPEN pen;
1104 LOGBRUSH brush;
1106 TRACE("%p %d %p %08lx\n", hdc, nObjType, lpEnumFunc, lParam );
1107 switch(nObjType)
1109 case OBJ_PEN:
1110 /* Enumerate solid pens */
1111 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1113 pen.lopnStyle = PS_SOLID;
1114 pen.lopnWidth.x = 1;
1115 pen.lopnWidth.y = 0;
1116 pen.lopnColor = solid_colors[i];
1117 retval = lpEnumFunc( &pen, lParam );
1118 TRACE("solid pen %08x, ret=%d\n",
1119 solid_colors[i], retval);
1120 if (!retval) break;
1122 break;
1124 case OBJ_BRUSH:
1125 /* Enumerate solid brushes */
1126 for (i = 0; i < sizeof(solid_colors)/sizeof(solid_colors[0]); i++)
1128 brush.lbStyle = BS_SOLID;
1129 brush.lbColor = solid_colors[i];
1130 brush.lbHatch = 0;
1131 retval = lpEnumFunc( &brush, lParam );
1132 TRACE("solid brush %08x, ret=%d\n",
1133 solid_colors[i], retval);
1134 if (!retval) break;
1137 /* Now enumerate hatched brushes */
1138 if (retval) for (i = HS_HORIZONTAL; i <= HS_DIAGCROSS; i++)
1140 brush.lbStyle = BS_HATCHED;
1141 brush.lbColor = RGB(0,0,0);
1142 brush.lbHatch = i;
1143 retval = lpEnumFunc( &brush, lParam );
1144 TRACE("hatched brush %d, ret=%d\n",
1145 i, retval);
1146 if (!retval) break;
1148 break;
1150 default:
1151 /* FIXME: implement Win32 types */
1152 WARN("(%d): Invalid type\n", nObjType );
1153 break;
1155 return retval;
1159 /***********************************************************************
1160 * SetObjectOwner (GDI32.@)
1162 void WINAPI SetObjectOwner( HGDIOBJ handle, HANDLE owner )
1164 /* Nothing to do */
1168 /***********************************************************************
1169 * GdiFlush (GDI32.@)
1171 BOOL WINAPI GdiFlush(void)
1173 return TRUE; /* FIXME */
1177 /***********************************************************************
1178 * GdiGetBatchLimit (GDI32.@)
1180 DWORD WINAPI GdiGetBatchLimit(void)
1182 return 1; /* FIXME */
1186 /***********************************************************************
1187 * GdiSetBatchLimit (GDI32.@)
1189 DWORD WINAPI GdiSetBatchLimit( DWORD limit )
1191 return 1; /* FIXME */
1195 /*******************************************************************
1196 * GetColorAdjustment [GDI32.@]
1200 BOOL WINAPI GetColorAdjustment(HDC hdc, LPCOLORADJUSTMENT lpca)
1202 FIXME("GetColorAdjustment, stub\n");
1203 return 0;
1206 /*******************************************************************
1207 * GdiComment [GDI32.@]
1211 BOOL WINAPI GdiComment(HDC hdc, UINT cbSize, const BYTE *lpData)
1213 DC *dc = get_dc_ptr(hdc);
1214 BOOL ret = FALSE;
1215 if(dc)
1217 if (dc->funcs->pGdiComment)
1218 ret = dc->funcs->pGdiComment( dc->physDev, cbSize, lpData );
1219 release_dc_ptr( dc );
1221 return ret;
1224 /*******************************************************************
1225 * SetColorAdjustment [GDI32.@]
1229 BOOL WINAPI SetColorAdjustment(HDC hdc, const COLORADJUSTMENT* lpca)
1231 FIXME("SetColorAdjustment, stub\n");
1232 return 0;