4 * Copyright 1993 Alexandre Julliard
19 /* #define DEBUG_GDI */
25 /* Object types for EnumObjects() */
30 HANDLE
*lpPenBrushList
= NULL
;
33 /***********************************************************************
37 static BRUSHOBJ WhiteBrush
=
39 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
40 { BS_SOLID
, RGB(255,255,255), 0 } /* logbrush */
43 static BRUSHOBJ LtGrayBrush
=
45 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
46 { BS_SOLID
, RGB(192,192,192), 0 } /* logbrush */
49 static BRUSHOBJ GrayBrush
=
51 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
52 { BS_SOLID
, RGB(128,128,128), 0 } /* logbrush */
55 static BRUSHOBJ DkGrayBrush
=
57 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
58 { BS_SOLID
, RGB(64,64,64), 0 } /* logbrush */
61 static BRUSHOBJ BlackBrush
=
63 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
64 { BS_SOLID
, RGB(0,0,0), 0 } /* logbrush */
67 static BRUSHOBJ NullBrush
=
69 { 0, BRUSH_MAGIC
, 1, 0 }, /* header */
70 { BS_NULL
, 0, 0 } /* logbrush */
73 static PENOBJ WhitePen
=
75 { 0, PEN_MAGIC
, 1, 0 }, /* header */
76 { PS_SOLID
, { 1, 0 }, RGB(255,255,255) } /* logpen */
79 static PENOBJ BlackPen
=
81 { 0, PEN_MAGIC
, 1, 0 }, /* header */
82 { PS_SOLID
, { 1, 0 }, RGB(0,0,0) } /* logpen */
85 static PENOBJ NullPen
=
87 { 0, PEN_MAGIC
, 1, 0 }, /* header */
88 { PS_NULL
, { 1, 0 }, 0 } /* logpen */
91 static FONTOBJ OEMFixedFont
=
93 { 0, FONT_MAGIC
, 1, 0 }, /* header */
94 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, OEM_CHARSET
,
95 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
98 static FONTOBJ AnsiFixedFont
=
100 { 0, FONT_MAGIC
, 1, 0 }, /* header */
101 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
102 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
105 static FONTOBJ AnsiVarFont
=
107 { 0, FONT_MAGIC
, 1, 0 }, /* header */
108 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
109 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
112 static FONTOBJ SystemFont
=
114 { 0, FONT_MAGIC
, 1, 0 }, /* header */
115 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
116 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
119 static FONTOBJ DeviceDefaultFont
=
121 { 0, FONT_MAGIC
, 1, 0 }, /* header */
122 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
123 0, 0, DEFAULT_QUALITY
, VARIABLE_PITCH
| FF_SWISS
, "" }
126 static FONTOBJ SystemFixedFont
=
128 { 0, FONT_MAGIC
, 1, 0 }, /* header */
129 { 12, 0, 0, 0, FW_NORMAL
, FALSE
, FALSE
, FALSE
, ANSI_CHARSET
,
130 0, 0, DEFAULT_QUALITY
, FIXED_PITCH
| FF_MODERN
, "" }
134 static GDIOBJHDR
* StockObjects
[NB_STOCK_OBJECTS
] =
136 (GDIOBJHDR
*) &WhiteBrush
,
137 (GDIOBJHDR
*) &LtGrayBrush
,
138 (GDIOBJHDR
*) &GrayBrush
,
139 (GDIOBJHDR
*) &DkGrayBrush
,
140 (GDIOBJHDR
*) &BlackBrush
,
141 (GDIOBJHDR
*) &NullBrush
,
142 (GDIOBJHDR
*) &WhitePen
,
143 (GDIOBJHDR
*) &BlackPen
,
144 (GDIOBJHDR
*) &NullPen
,
146 (GDIOBJHDR
*) &OEMFixedFont
,
147 (GDIOBJHDR
*) &AnsiFixedFont
,
148 (GDIOBJHDR
*) &AnsiVarFont
,
149 (GDIOBJHDR
*) &SystemFont
,
150 (GDIOBJHDR
*) &DeviceDefaultFont
,
151 NULL
, /* DEFAULT_PALETTE created by COLOR_Init */
152 (GDIOBJHDR
*) &SystemFixedFont
156 /***********************************************************************
159 * GDI initialisation.
165 /* Create default palette */
167 if (!(hpalette
= COLOR_Init())) return FALSE
;
168 StockObjects
[DEFAULT_PALETTE
] = (GDIOBJHDR
*)GDI_HEAP_LIN_ADDR( hpalette
);
170 /* Create default bitmap */
172 if (!BITMAP_Init()) return FALSE
;
174 /* Initialise brush dithering */
176 if (!BRUSH_Init()) return FALSE
;
178 /* Initialise fonts */
180 if (!FONT_Init()) return FALSE
;
186 /***********************************************************************
189 HANDLE
GDI_AllocObject( WORD size
, WORD magic
)
191 static DWORD count
= 0;
193 HANDLE handle
= GDI_HEAP_ALLOC( size
);
194 if (!handle
) return 0;
195 obj
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
198 obj
->dwCount
= ++count
;
203 /***********************************************************************
206 BOOL
GDI_FreeObject( HANDLE handle
)
210 /* Can't free stock objects */
211 if ((handle
>= FIRST_STOCK_HANDLE
) && (handle
<= LAST_STOCK_HANDLE
))
214 object
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
215 if (!object
) return FALSE
;
216 object
->wMagic
= 0; /* Mark it as invalid */
220 GDI_HEAP_FREE( handle
);
224 /***********************************************************************
227 * Return a pointer to the GDI object associated to the handle.
228 * Return NULL if the object has the wrong magic number.
230 GDIOBJHDR
* GDI_GetObjPtr( HANDLE handle
, WORD magic
)
232 GDIOBJHDR
* ptr
= NULL
;
234 if ((handle
>= FIRST_STOCK_HANDLE
) && (handle
<= LAST_STOCK_HANDLE
))
235 ptr
= StockObjects
[handle
- FIRST_STOCK_HANDLE
];
237 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
238 if (!ptr
) return NULL
;
239 if ((magic
!= MAGIC_DONTCARE
) && (ptr
->wMagic
!= magic
)) return NULL
;
244 /***********************************************************************
245 * DeleteObject (GDI.69)
247 BOOL
DeleteObject( HGDIOBJ obj
)
249 /* Check if object is valid */
251 GDIOBJHDR
* header
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( obj
);
252 if (!header
) return FALSE
;
254 dprintf_gdi(stddeb
, "DeleteObject: %04x\n", obj
);
258 switch(header
->wMagic
)
260 case PEN_MAGIC
: return GDI_FreeObject( obj
);
261 case BRUSH_MAGIC
: return BRUSH_DeleteObject( obj
, (BRUSHOBJ
*)header
);
262 case FONT_MAGIC
: return GDI_FreeObject( obj
);
263 case PALETTE_MAGIC
: return GDI_FreeObject( obj
);
264 case BITMAP_MAGIC
: return BITMAP_DeleteObject( obj
, (BITMAPOBJ
*)header
);
265 case REGION_MAGIC
: return REGION_DeleteObject( obj
, (RGNOBJ
*)header
);
271 /***********************************************************************
272 * GetStockObject (GDI.87)
274 HANDLE
GetStockObject( int obj
)
276 if ((obj
< 0) || (obj
>= NB_STOCK_OBJECTS
)) return 0;
277 if (!StockObjects
[obj
]) return 0;
278 dprintf_gdi(stddeb
, "GetStockObject: returning %d\n",
279 FIRST_STOCK_HANDLE
+ obj
);
280 return (HANDLE
)(FIRST_STOCK_HANDLE
+ obj
);
284 /***********************************************************************
287 int GetObject( HANDLE handle
, int count
, LPSTR buffer
)
289 GDIOBJHDR
* ptr
= NULL
;
290 dprintf_gdi(stddeb
, "GetObject: %04x %d %p\n", handle
, count
, buffer
);
291 if (!count
) return 0;
293 if ((handle
>= FIRST_STOCK_HANDLE
) && (handle
<= LAST_STOCK_HANDLE
))
294 ptr
= StockObjects
[handle
- FIRST_STOCK_HANDLE
];
296 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
302 return PEN_GetObject( (PENOBJ
*)ptr
, count
, buffer
);
304 return BRUSH_GetObject( (BRUSHOBJ
*)ptr
, count
, buffer
);
306 return BITMAP_GetObject( (BITMAPOBJ
*)ptr
, count
, buffer
);
308 return FONT_GetObject( (FONTOBJ
*)ptr
, count
, buffer
);
310 return PALETTE_GetObject( (PALETTEOBJ
*)ptr
, count
, buffer
);
316 /***********************************************************************
317 * SelectObject (GDI.45)
319 HANDLE
SelectObject( HDC hdc
, HANDLE handle
)
321 GDIOBJHDR
* ptr
= NULL
;
324 dprintf_gdi(stddeb
, "SelectObject: %04x %04x\n", hdc
, handle
);
325 if ((handle
>= FIRST_STOCK_HANDLE
) && (handle
<= LAST_STOCK_HANDLE
))
326 ptr
= StockObjects
[handle
- FIRST_STOCK_HANDLE
];
328 ptr
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
331 dc
= (DC
*) GDI_GetObjPtr( hdc
, DC_MAGIC
);
334 dc
= (DC
*)GDI_GetObjPtr(hdc
, METAFILE_DC_MAGIC
);
341 return PEN_SelectObject( dc
, handle
, (PENOBJ
*)ptr
);
343 return BRUSH_SelectObject( hdc
, dc
, handle
, (BRUSHOBJ
*)ptr
);
345 return BITMAP_SelectObject( hdc
, dc
, handle
, (BITMAPOBJ
*)ptr
);
347 return FONT_SelectObject( dc
, handle
, (FONTOBJ
*)ptr
);
349 return (HANDLE
)SelectClipRgn( hdc
, handle
);
355 /***********************************************************************
356 * UnrealizeObject (GDI.150)
358 BOOL
UnrealizeObject( HANDLE handle
)
360 dprintf_gdi(stdnimp
, "UnrealizeObject: %04x\n", handle
);
365 /***********************************************************************
366 * EnumObjects (GDI.71)
368 INT
EnumObjects( HDC hdc
, INT nObjType
, GOBJENUMPROC lpEnumFunc
, LPARAM lParam
)
370 /* Solid colors to enumerate */
371 static const COLORREF solid_colors
[] =
372 { RGB(0x00,0x00,0x00), RGB(0xff,0xff,0xff),
373 RGB(0xff,0x00,0x00), RGB(0x00,0xff,0x00),
374 RGB(0x00,0x00,0xff), RGB(0xff,0xff,0x00),
375 RGB(0xff,0x00,0xff), RGB(0x00,0xff,0xff),
376 RGB(0x80,0x00,0x00), RGB(0x00,0x80,0x00),
377 RGB(0x80,0x80,0x00), RGB(0x00,0x00,0x80),
378 RGB(0x80,0x00,0x80), RGB(0x00,0x80,0x80),
379 RGB(0x80,0x80,0x80), RGB(0xc0,0xc0,0xc0)
384 dprintf_gdi( stddeb
, "EnumObjects: %04x %d %08lx %08lx\n",
385 hdc
, nObjType
, (DWORD
)lpEnumFunc
, lParam
);
389 /* Enumerate solid pens */
390 for (i
= 0; i
< sizeof(solid_colors
)/sizeof(solid_colors
[0]); i
++)
392 LOGPEN pen
= { PS_SOLID
, { 1, 0 }, solid_colors
[i
] };
393 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&pen
),
395 dprintf_gdi( stddeb
, "EnumObject: solid pen %08lx, ret=%d\n",
396 solid_colors
[i
], retval
);
402 /* Enumerate solid brushes */
403 for (i
= 0; i
< sizeof(solid_colors
)/sizeof(solid_colors
[0]); i
++)
405 LOGBRUSH brush
= { BS_SOLID
, solid_colors
[i
], 0 };
406 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&brush
),
408 dprintf_gdi( stddeb
, "EnumObject: solid brush %08lx, ret=%d\n",
409 solid_colors
[i
], retval
);
414 /* Now enumerate hatched brushes */
415 for (i
= HS_HORIZONTAL
; i
<= HS_DIAGCROSS
; i
++)
417 LOGBRUSH brush
= { BS_HATCHED
, RGB(0,0,0), i
};
418 retval
= CallEnumObjectsProc( lpEnumFunc
, MAKE_SEGPTR(&brush
),
420 dprintf_gdi( stddeb
, "EnumObject: hatched brush %d, ret=%d\n",
427 fprintf( stderr
, "EnumObjects: invalid type %d\n", nObjType
);
434 /***********************************************************************
435 * IsGDIObject (GDI.462)
437 BOOL
IsGDIObject(HANDLE handle
)
441 object
= (GDIOBJHDR
*) GDI_HEAP_LIN_ADDR( handle
);
442 /* FIXME: should check magic here */
443 return (object
!= NULL
);
447 /***********************************************************************
450 INT16
MulDiv16( INT16 foo
, INT16 bar
, INT16 baz
)
453 if (!baz
) return -32768;
454 ret
= (foo
* bar
) / baz
;
455 if ((ret
> 32767) || (ret
< -32767)) return -32768;
460 /***********************************************************************
461 * MulDiv32 (KERNEL32.391)
463 INT32
MulDiv32( INT32 foo
, INT32 bar
, INT32 baz
)
468 ret
= ((long long)foo
* bar
) / baz
;
469 if ((ret
> 2147483647) || (ret
< -2147483647)) return -1;
473 return (foo
* bar
) / baz
;