4 * Copyright 1993 Alexandre Julliard
19 /* #define DEBUG_GDI */
23 LPSTR GDI_Heap
= NULL
;
26 /* Object types for EnumObjects() */
31 HANDLE
*lpPenBrushList
= NULL
;
34 /***********************************************************************
38 static BRUSHOBJ WhiteBrush
=
40 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
41 { BS_SOLID
, RGB(255,255,255), 0 } /* logbrush */
44 static BRUSHOBJ LtGrayBrush
=
46 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
47 { BS_SOLID
, RGB(192,192,192), 0 } /* logbrush */
50 static BRUSHOBJ GrayBrush
=
52 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
53 { BS_SOLID
, RGB(128,128,128), 0 } /* logbrush */
56 static BRUSHOBJ DkGrayBrush
=
58 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
59 { BS_SOLID
, RGB(64,64,64), 0 } /* logbrush */
62 static BRUSHOBJ BlackBrush
=
64 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
65 { BS_SOLID
, RGB(0,0,0), 0 } /* logbrush */
68 static BRUSHOBJ NullBrush
=
70 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
71 { BS_NULL
, 0, 0 } /* logbrush */
74 static PENOBJ WhitePen
=
76 { 0, PEN_MAGIC
, 1, 0 }, /* header */
77 { PS_SOLID
, { 1, 0 }, RGB(255,255,255) } /* logpen */
80 static PENOBJ BlackPen
=
82 { 0, PEN_MAGIC
, 1, 0 }, /* header */
83 { PS_SOLID
, { 1, 0 }, RGB(0,0,0) } /* logpen */
86 static PENOBJ NullPen
=
88 { 0, PEN_MAGIC
, 1, 0 }, /* header */
89 { PS_NULL
, { 1, 0 }, 0 } /* logpen */
92 static FONTOBJ OEMFixedFont
=
94 { 0, FONT_MAGIC
, 1, 0 }, /* header */
95 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, OEM_CHARSET
,
96 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
99 static FONTOBJ AnsiFixedFont
=
101 { 0, FONT_MAGIC
, 1, 0 }, /* header */
102 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
103 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
106 static FONTOBJ AnsiVarFont
=
108 { 0, FONT_MAGIC
, 1, 0 }, /* header */
109 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
110 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
113 static FONTOBJ SystemFont
=
115 { 0, FONT_MAGIC
, 1, 0 }, /* header */
116 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
117 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
120 static FONTOBJ DeviceDefaultFont
=
122 { 0, FONT_MAGIC
, 1, 0 }, /* header */
123 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
124 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
127 static FONTOBJ SystemFixedFont
=
129 { 0, FONT_MAGIC
, 1, 0 }, /* header */
130 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
131 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
135 static GDIOBJHDR
* StockObjects
[NB_STOCK_OBJECTS
] =
137 (GDIOBJHDR
*) &WhiteBrush
,
138 (GDIOBJHDR
*) &LtGrayBrush
,
139 (GDIOBJHDR
*) &GrayBrush
,
140 (GDIOBJHDR
*) &DkGrayBrush
,
141 (GDIOBJHDR
*) &BlackBrush
,
142 (GDIOBJHDR
*) &NullBrush
,
143 (GDIOBJHDR
*) &WhitePen
,
144 (GDIOBJHDR
*) &BlackPen
,
145 (GDIOBJHDR
*) &NullPen
,
147 (GDIOBJHDR
*) &OEMFixedFont
,
148 (GDIOBJHDR
*) &AnsiFixedFont
,
149 (GDIOBJHDR
*) &AnsiVarFont
,
150 (GDIOBJHDR
*) &SystemFont
,
151 (GDIOBJHDR
*) &DeviceDefaultFont
,
152 NULL
, /* DEFAULT_PALETTE created by COLOR_Init */
153 (GDIOBJHDR
*) &SystemFixedFont
157 /***********************************************************************
160 * GDI initialisation.
167 /* Create GDI heap */
169 if (!(GDI_HeapSel
= GlobalAlloc(GMEM_FIXED
, GDI_HEAP_SIZE
))) return FALSE
;
170 GDI_Heap
= GlobalLock( GDI_HeapSel
);
171 LocalInit( GDI_HeapSel
, 0, GDI_HEAP_SIZE
-1 );
174 /* Create default palette */
176 if (!(hpalette
= COLOR_Init())) return FALSE
;
177 StockObjects
[DEFAULT_PALETTE
] = (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( hpalette
);
179 /* Create default bitmap */
181 if (!BITMAP_Init()) return FALSE
;
183 /* Initialise brush dithering */
185 if (!BRUSH_Init()) return FALSE
;
187 /* Initialise fonts */
189 if (!FONT_Init()) return FALSE
;
195 /***********************************************************************
198 HANDLE
GDI_AllocObject( WORD size
, WORD magic
)
200 static DWORD count
= 0;
202 HANDLE handle
= GDI_HEAP_ALLOC( size
);
203 if (!handle
) return 0;
204 obj
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
207 obj
->dwCount
= ++count
;
212 /***********************************************************************
215 BOOL
GDI_FreeObject( HANDLE handle
)
219 /* Can't free stock objects */
220 if ((DWORD
)handle
>= FIRST_STOCK_HANDLE
&&
221 (DWORD
)handle
<= LAST_STOCK_HANDLE
) return TRUE
;
223 object
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
224 if (!object
) return FALSE
;
225 object
->wMagic
= 0; /* Mark it as invalid */
229 GDI_HEAP_FREE( handle
);
233 /***********************************************************************
236 * Return a pointer to the GDI object associated to the handle.
237 * Return NULL if the object has the wrong magic number.
239 GDIOBJHDR
* GDI_GetObjPtr( HANDLE handle
, WORD magic
)
241 GDIOBJHDR
* ptr
= NULL
;
243 if ((DWORD
)handle
>= FIRST_STOCK_HANDLE
&&
244 (DWORD
)handle
<= LAST_STOCK_HANDLE
)
245 ptr
= StockObjects
[(DWORD
)handle
- FIRST_STOCK_HANDLE
];
247 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
248 if (!ptr
) return NULL
;
249 if (ptr
->wMagic
!= magic
) return NULL
;
254 /***********************************************************************
255 * DeleteObject (GDI.69)
257 BOOL
DeleteObject( HANDLE obj
)
259 /* Check if object is valid */
261 GDIOBJHDR
* header
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( obj
);
262 if (!header
) return FALSE
;
264 dprintf_gdi(stddeb
, "DeleteObject: "NPFMT
"\n", obj
);
268 switch(header
->wMagic
)
270 case PEN_MAGIC
: return GDI_FreeObject( obj
);
271 case BRUSH_MAGIC
: return BRUSH_DeleteObject( obj
, (BRUSHOBJ
*)header
);
272 case FONT_MAGIC
: return GDI_FreeObject( obj
);
273 case PALETTE_MAGIC
: return GDI_FreeObject( obj
);
274 case BITMAP_MAGIC
: return BITMAP_DeleteObject( obj
, (BITMAPOBJ
*)header
);
275 case REGION_MAGIC
: return REGION_DeleteObject( obj
, (RGNOBJ
*)header
);
281 /***********************************************************************
282 * GetStockObject (GDI.87)
284 HANDLE
GetStockObject( int obj
)
286 if ((obj
< 0) || (obj
>= NB_STOCK_OBJECTS
)) return 0;
287 if (!StockObjects
[obj
]) return 0;
288 dprintf_gdi(stddeb
, "GetStockObject: returning %ld\n",
289 (DWORD
)FIRST_STOCK_HANDLE
+ obj
);
290 return (HANDLE
)((DWORD
)FIRST_STOCK_HANDLE
+ obj
);
294 /***********************************************************************
297 int GetObject( HANDLE handle
, int count
, LPSTR buffer
)
299 GDIOBJHDR
* ptr
= NULL
;
300 dprintf_gdi(stddeb
, "GetObject: "NPFMT
" %d %p\n", handle
, count
, buffer
);
301 if (!count
) return 0;
303 if ((DWORD
)handle
>= FIRST_STOCK_HANDLE
&&
304 (DWORD
)handle
<= LAST_STOCK_HANDLE
)
305 ptr
= StockObjects
[(DWORD
)handle
- FIRST_STOCK_HANDLE
];
307 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
313 return PEN_GetObject( (PENOBJ
*)ptr
, count
, buffer
);
315 return BRUSH_GetObject( (BRUSHOBJ
*)ptr
, count
, buffer
);
317 return BITMAP_GetObject( (BITMAPOBJ
*)ptr
, count
, buffer
);
319 return FONT_GetObject( (FONTOBJ
*)ptr
, count
, buffer
);
321 return PALETTE_GetObject( (PALETTEOBJ
*)ptr
, count
, buffer
);
327 /***********************************************************************
328 * SelectObject (GDI.45)
330 HANDLE
SelectObject( HDC hdc
, HANDLE handle
)
332 GDIOBJHDR
* ptr
= NULL
;
335 dprintf_gdi(stddeb
, "SelectObject: "NPFMT
" "NPFMT
"\n", hdc
, handle
);
336 if ((DWORD
)handle
>= FIRST_STOCK_HANDLE
&&
337 (DWORD
)handle
<= LAST_STOCK_HANDLE
)
338 ptr
= StockObjects
[(DWORD
)handle
- FIRST_STOCK_HANDLE
];
340 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
343 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
346 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
353 return PEN_SelectObject( dc
, handle
, (PENOBJ
*)ptr
);
355 return BRUSH_SelectObject( hdc
, dc
, handle
, (BRUSHOBJ
*)ptr
);
357 return BITMAP_SelectObject( hdc
, dc
, handle
, (BITMAPOBJ
*)ptr
);
359 return FONT_SelectObject( dc
, handle
, (FONTOBJ
*)ptr
);
361 return (HANDLE
)SelectClipRgn( hdc
, handle
);
367 /***********************************************************************
368 * UnrealizeObject (GDI.150)
370 BOOL
UnrealizeObject( HANDLE handle
)
372 dprintf_gdi(stdnimp
, "UnrealizeObject: "NPFMT
"\n", handle
);
377 /***********************************************************************
378 * EnumObjects (GDI.71)
380 int EnumObjects( HDC hdc
, int nObjType
, FARPROC lpEnumFunc
, LPARAM lParam
)
382 /* Solid colors to enumerate */
383 static const COLORREF solid_colors
[] =
384 { RGB(0x00,0x00,0x00), RGB(0xff,0xff,0xff),
385 RGB(0xff,0x00,0x00), RGB(0x00,0xff,0x00),
386 RGB(0x00,0x00,0xff), RGB(0xff,0xff,0x00),
387 RGB(0xff,0x00,0xff), RGB(0x00,0xff,0xff),
388 RGB(0x80,0x00,0x00), RGB(0x00,0x80,0x00),
389 RGB(0x80,0x80,0x00), RGB(0x00,0x00,0x80),
390 RGB(0x80,0x00,0x80), RGB(0x00,0x80,0x80),
391 RGB(0x80,0x80,0x80), RGB(0xc0,0xc0,0xc0)
396 dprintf_gdi( stddeb
, "EnumObjects: "NPFMT
" %d %08lx %08lx\n",
397 hdc
, nObjType
, (DWORD
)lpEnumFunc
, lParam
);
401 /* Enumerate solid pens */
402 for (i
= 0; i
< sizeof(solid_colors
)/sizeof(solid_colors
[0]); i
++)
404 LOGPEN pen
= { PS_SOLID
, { 1, 0 }, solid_colors
[i
] };
405 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&pen
),
407 dprintf_gdi( stddeb
, "EnumObject: solid pen %08lx, ret=%d\n",
408 solid_colors
[i
], retval
);
414 /* Enumerate solid brushes */
415 for (i
= 0; i
< sizeof(solid_colors
)/sizeof(solid_colors
[0]); i
++)
417 LOGBRUSH brush
= { BS_SOLID
, solid_colors
[i
], 0 };
418 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&brush
),
420 dprintf_gdi( stddeb
, "EnumObject: solid brush %08lx, ret=%d\n",
421 solid_colors
[i
], retval
);
426 /* Now enumerate hatched brushes */
427 for (i
= HS_HORIZONTAL
; i
<= HS_DIAGCROSS
; i
++)
429 LOGBRUSH brush
= { BS_HATCHED
, RGB(0,0,0), i
};
430 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&brush
),
432 dprintf_gdi( stddeb
, "EnumObject: hatched brush %d, ret=%d\n",
439 fprintf( stderr
, "EnumObjects: invalid type %d\n", nObjType
);
450 LPSTR lpLog
; /* Point to a LOGBRUSH or LOGPEN struct */
454 if (lpEnumFunc
== 0) {
455 fprintf(stderr
,"EnumObjects // Bad EnumProc callback address !\n");
461 dprintf_gdi(stddeb
,"EnumObjects("NPFMT
", OBJ_PEN, %08lx, %p);\n",
462 hDC
, (LONG
)lpEnumFunc
, lpData
);
463 hLog
= GDI_HEAP_ALLOC( sizeof(LOGPEN
) );
464 lpLog
= (LPSTR
) GDI_HEAP_LIN_ADDR(hLog
);
466 fprintf(stderr
,"EnumObjects // Unable to alloc LOGPEN struct !\n");
471 wMagic
= BRUSH_MAGIC
;
472 dprintf_gdi(stddeb
,"EnumObjects("NPFMT
", OBJ_BRUSH, %08lx, %p);\n",
473 hDC
, (LONG
)lpEnumFunc
, lpData
);
474 hLog
= GDI_HEAP_ALLOC( sizeof(LOGBRUSH
) );
475 lpLog
= (LPSTR
) GDI_HEAP_LIN_ADDR(hLog
);
477 fprintf(stderr
,"EnumObjects // Unable to alloc LOGBRUSH struct !\n");
482 fprintf(stderr
,"EnumObjects("NPFMT
", %04X, %08lx, %p); // Unknown OBJ type !\n",
483 hDC
, nObjType
, (LONG
)lpEnumFunc
, lpData
);
486 #ifdef notdef /* FIXME: stock object ptr won't work in callback */
487 dprintf_gdi(stddeb
,"EnumObjects // Stock Objects first !\n");
488 for (i
= 0; i
< NB_STOCK_OBJECTS
; i
++) {
489 header
= StockObjects
[i
];
490 if (header
->wMagic
== wMagic
) {
491 PEN_GetObject( (PENOBJ
*)header
, sizeof(LOGPEN
), lpLog
);
492 BRUSH_GetObject( (BRUSHOBJ
*)header
, sizeof(LOGBRUSH
),lpLog
);
493 dprintf_gdi(stddeb
,"EnumObjects // StockObj lpLog=%p lpData=%p\n", lpLog
, lpData
);
494 if (header
->wMagic
== BRUSH_MAGIC
) {
495 dprintf_gdi(stddeb
,"EnumObjects // StockBrush lbStyle=%04X\n", ((LPLOGBRUSH
)lpLog
)->lbStyle
);
496 dprintf_gdi(stddeb
,"EnumObjects // StockBrush lbColor=%08lX\n", ((LPLOGBRUSH
)lpLog
)->lbColor
);
497 dprintf_gdi(stddeb
,"EnumObjects // StockBrush lbHatch=%04X\n", ((LPLOGBRUSH
)lpLog
)->lbHatch
);
499 if (header
->wMagic
== PEN_MAGIC
) {
500 dprintf_gdi(stddeb
,"EnumObjects // StockPen lopnStyle=%04X\n", ((LPLOGPEN
)lpLog
)->lopnStyle
);
501 dprintf_gdi(stddeb
,"EnumObjects // StockPen lopnWidth=%d\n", ((LPLOGPEN
)lpLog
)->lopnWidth
.x
);
502 dprintf_gdi(stddeb
,"EnumObjects // StockPen lopnColor=%08lX\n", ((LPLOGPEN
)lpLog
)->lopnColor
);
504 nRet
= CallEnumObjectsProc( lpEnumFunc
,
505 GDI_HEAP_SEG_ADDR(hLog
),
507 dprintf_gdi(stddeb
,"EnumObjects // after Callback!\n");
510 dprintf_gdi(stddeb
,"EnumObjects // EnumEnd requested by application !\n");
515 dprintf_gdi(stddeb
,"EnumObjects // Now DC owned objects %p !\n", header
);
518 if (lpPenBrushList
== NULL
) return 0;
519 for (lphObj
= lpPenBrushList
; *lphObj
!= 0; ) {
520 dprintf_gdi(stddeb
,"EnumObjects // *lphObj="NPFMT
"\n", *lphObj
);
521 header
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR(*lphObj
++);
522 if (header
->wMagic
== wMagic
) {
523 dprintf_gdi(stddeb
,"EnumObjects // DC_Obj lpLog=%p lpData=%p\n", lpLog
, lpData
);
524 if (header
->wMagic
== BRUSH_MAGIC
) {
525 BRUSH_GetObject( (BRUSHOBJ
*)header
, sizeof(LOGBRUSH
), lpLog
);
526 dprintf_gdi(stddeb
,"EnumObjects // DC_Brush lbStyle=%04X\n", ((LPLOGBRUSH
)lpLog
)->lbStyle
);
527 dprintf_gdi(stddeb
,"EnumObjects // DC_Brush lbColor=%08lX\n", ((LPLOGBRUSH
)lpLog
)->lbColor
);
528 dprintf_gdi(stddeb
,"EnumObjects // DC_Brush lbHatch=%04lX\n", (LONG
)((LPLOGBRUSH
)lpLog
)->lbHatch
);
530 if (header
->wMagic
== PEN_MAGIC
) {
531 PEN_GetObject( (PENOBJ
*)header
, sizeof(LOGPEN
), lpLog
);
532 dprintf_gdi(stddeb
,"EnumObjects // DC_Pen lopnStyle=%04X\n", ((LPLOGPEN
)lpLog
)->lopnStyle
);
533 dprintf_gdi(stddeb
,"EnumObjects // DC_Pen lopnWidth=%ld\n", (LONG
)((LPLOGPEN
)lpLog
)->lopnWidth
.x
);
534 dprintf_gdi(stddeb
,"EnumObjects // DC_Pen lopnColor=%08lX\n", ((LPLOGPEN
)lpLog
)->lopnColor
);
536 nRet
= CallEnumObjectsProc(lpEnumFunc
, GDI_HEAP_SEG_ADDR(hLog
),
543 dprintf_gdi(stddeb
,"EnumObjects // End of enumeration !\n");
549 /***********************************************************************
550 * IsGDIObject(GDI.462)
552 BOOL
IsGDIObject(HANDLE handle
)
556 object
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);