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 /***********************************************************************
196 * GDI_AppendToPenBrushList
198 BOOL GDI_AppendToPenBrushList(HANDLE hNewObj)
202 if (hNewObj == 0) return FALSE;
203 if (lpPenBrushList == NULL) {
204 lpPenBrushList = xmalloc(MAX_OBJ * sizeof(HANDLE));
205 lpPenBrushList[0] = 0;
206 dprintf_gdi(stddeb,"GDI_AppendToPenBrushList() lpPenBrushList allocated !\n");
208 for (lphObj = lpPenBrushList; i < MAX_OBJ; i++) {
212 dprintf_gdi(stddeb,"GDI_AppendToPenBrushList("NPFMT") appended (count=%d)\n", hNewObj, i);
221 /***********************************************************************
224 HANDLE GDI_AllocObject( WORD size, WORD magic )
226 static DWORD count = 0;
228 HANDLE handle = GDI_HEAP_ALLOC( size );
229 if (!handle) return 0;
230 obj = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
233 obj->dwCount = ++count;
234 if (magic == PEN_MAGIC || magic == BRUSH_MAGIC) {
235 GDI_AppendToPenBrushList(handle);
241 /***********************************************************************
244 BOOL GDI_FreeObject( HANDLE handle )
248 /* Can't free stock objects */
249 if ((DWORD)handle >= FIRST_STOCK_HANDLE &&
250 (DWORD)handle <= LAST_STOCK_HANDLE ) return TRUE;
252 object = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
253 if (!object) return FALSE;
254 object->wMagic = 0; /* Mark it as invalid */
258 GDI_HEAP_FREE( handle );
262 /***********************************************************************
265 * Return a pointer to the GDI object associated to the handle.
266 * Return NULL if the object has the wrong magic number.
268 GDIOBJHDR * GDI_GetObjPtr( HANDLE handle, WORD magic )
270 GDIOBJHDR * ptr = NULL;
272 if ((DWORD)handle >= FIRST_STOCK_HANDLE &&
273 (DWORD)handle <= LAST_STOCK_HANDLE )
274 ptr = StockObjects[(DWORD)handle - FIRST_STOCK_HANDLE];
276 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
277 if (!ptr) return NULL;
278 if (ptr->wMagic != magic) return NULL;
283 /***********************************************************************
284 * DeleteObject (GDI.69)
286 BOOL DeleteObject( HANDLE obj )
288 /* Check if object is valid */
290 GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( obj );
291 if (!header) return FALSE;
293 dprintf_gdi(stddeb, "DeleteObject: "NPFMT"\n", obj );
297 switch(header->wMagic)
299 case PEN_MAGIC: return GDI_FreeObject( obj );
300 case BRUSH_MAGIC: return BRUSH_DeleteObject( obj, (BRUSHOBJ*)header );
301 case FONT_MAGIC: return GDI_FreeObject( obj );
302 case PALETTE_MAGIC: return GDI_FreeObject( obj );
303 case BITMAP_MAGIC: return BITMAP_DeleteObject( obj, (BITMAPOBJ*)header);
304 case REGION_MAGIC: return REGION_DeleteObject( obj, (RGNOBJ*)header );
310 /***********************************************************************
311 * GetStockObject (GDI.87)
313 HANDLE GetStockObject( int obj )
315 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
316 if (!StockObjects[obj]) return 0;
317 dprintf_gdi(stddeb, "GetStockObject: returning %ld\n",
318 (DWORD)FIRST_STOCK_HANDLE + obj );
319 return (HANDLE)((DWORD)FIRST_STOCK_HANDLE + obj);
323 /***********************************************************************
326 int GetObject( HANDLE handle, int count, LPSTR buffer )
328 GDIOBJHDR * ptr = NULL;
329 dprintf_gdi(stddeb, "GetObject: "NPFMT" %d %p\n", handle, count, buffer );
330 if (!count) return 0;
332 if ((DWORD)handle >= FIRST_STOCK_HANDLE &&
333 (DWORD)handle <= LAST_STOCK_HANDLE )
334 ptr = StockObjects[(DWORD)handle - FIRST_STOCK_HANDLE];
336 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
342 return PEN_GetObject( (PENOBJ *)ptr, count, buffer );
344 return BRUSH_GetObject( (BRUSHOBJ *)ptr, count, buffer );
346 return BITMAP_GetObject( (BITMAPOBJ *)ptr, count, buffer );
348 return FONT_GetObject( (FONTOBJ *)ptr, count, buffer );
350 return PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
356 /***********************************************************************
357 * SelectObject (GDI.45)
359 HANDLE SelectObject( HDC hdc, HANDLE handle )
361 GDIOBJHDR * ptr = NULL;
364 dprintf_gdi(stddeb, "SelectObject: "NPFMT" "NPFMT"\n", hdc, handle );
365 if ((DWORD)handle >= FIRST_STOCK_HANDLE &&
366 (DWORD)handle <= LAST_STOCK_HANDLE )
367 ptr = StockObjects[(DWORD)handle - FIRST_STOCK_HANDLE];
369 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
372 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
375 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
382 return PEN_SelectObject( dc, handle, (PENOBJ *)ptr );
384 return BRUSH_SelectObject( hdc, dc, handle, (BRUSHOBJ *)ptr );
386 return BITMAP_SelectObject( hdc, dc, handle, (BITMAPOBJ *)ptr );
388 return FONT_SelectObject( dc, handle, (FONTOBJ *)ptr );
390 return (HANDLE)SelectClipRgn( hdc, handle );
396 /***********************************************************************
397 * UnrealizeObject (GDI.150)
399 BOOL UnrealizeObject( HANDLE handle )
401 dprintf_gdi(stdnimp, "UnrealizeObject: "NPFMT"\n", handle );
406 /***********************************************************************
407 * EnumObjects (GDI.71)
409 int EnumObjects(HDC hDC, int nObjType, FARPROC lpEnumFunc, LPSTR lpData)
416 LPSTR lpLog; /* Point to a LOGBRUSH or LOGPEN struct */
420 if (lpEnumFunc == NULL) {
421 fprintf(stderr,"EnumObjects // Bad EnumProc callback address !\n");
427 dprintf_gdi(stddeb,"EnumObjects("NPFMT", OBJ_PEN, %08lx, %p);\n",
428 hDC, (LONG)lpEnumFunc, lpData);
429 hLog = GDI_HEAP_ALLOC( sizeof(LOGPEN) );
430 lpLog = (LPSTR) GDI_HEAP_LIN_ADDR(hLog);
432 fprintf(stderr,"EnumObjects // Unable to alloc LOGPEN struct !\n");
437 wMagic = BRUSH_MAGIC;
438 dprintf_gdi(stddeb,"EnumObjects("NPFMT", OBJ_BRUSH, %08lx, %p);\n",
439 hDC, (LONG)lpEnumFunc, lpData);
440 hLog = GDI_HEAP_ALLOC( sizeof(LOGBRUSH) );
441 lpLog = (LPSTR) GDI_HEAP_LIN_ADDR(hLog);
443 fprintf(stderr,"EnumObjects // Unable to alloc LOGBRUSH struct !\n");
448 fprintf(stderr,"EnumObjects("NPFMT", %04X, %08lx, %p); // Unknown OBJ type !\n",
449 hDC, nObjType, (LONG)lpEnumFunc, lpData);
452 #ifdef notdef /* FIXME: stock object ptr won't work in callback */
453 dprintf_gdi(stddeb,"EnumObjects // Stock Objects first !\n");
454 for (i = 0; i < NB_STOCK_OBJECTS; i++) {
455 header = StockObjects[i];
456 if (header->wMagic == wMagic) {
457 PEN_GetObject( (PENOBJ *)header, sizeof(LOGPEN), lpLog);
458 BRUSH_GetObject( (BRUSHOBJ *)header, sizeof(LOGBRUSH),lpLog);
459 dprintf_gdi(stddeb,"EnumObjects // StockObj lpLog=%p lpData=%p\n", lpLog, lpData);
460 if (header->wMagic == BRUSH_MAGIC) {
461 dprintf_gdi(stddeb,"EnumObjects // StockBrush lbStyle=%04X\n", ((LPLOGBRUSH)lpLog)->lbStyle);
462 dprintf_gdi(stddeb,"EnumObjects // StockBrush lbColor=%08lX\n", ((LPLOGBRUSH)lpLog)->lbColor);
463 dprintf_gdi(stddeb,"EnumObjects // StockBrush lbHatch=%04X\n", ((LPLOGBRUSH)lpLog)->lbHatch);
465 if (header->wMagic == PEN_MAGIC) {
466 dprintf_gdi(stddeb,"EnumObjects // StockPen lopnStyle=%04X\n", ((LPLOGPEN)lpLog)->lopnStyle);
467 dprintf_gdi(stddeb,"EnumObjects // StockPen lopnWidth=%d\n", ((LPLOGPEN)lpLog)->lopnWidth.x);
468 dprintf_gdi(stddeb,"EnumObjects // StockPen lopnColor=%08lX\n", ((LPLOGPEN)lpLog)->lopnColor);
470 nRet = CallEnumObjectsProc( lpEnumFunc,
471 GDI_HEAP_SEG_ADDR(hLog),
473 dprintf_gdi(stddeb,"EnumObjects // after Callback!\n");
476 dprintf_gdi(stddeb,"EnumObjects // EnumEnd requested by application !\n");
481 dprintf_gdi(stddeb,"EnumObjects // Now DC owned objects %p !\n", header);
484 if (lpPenBrushList == NULL) return 0;
485 for (lphObj = lpPenBrushList; *lphObj != 0; ) {
486 dprintf_gdi(stddeb,"EnumObjects // *lphObj="NPFMT"\n", *lphObj);
487 header = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR(*lphObj++);
488 if (header->wMagic == wMagic) {
489 dprintf_gdi(stddeb,"EnumObjects // DC_Obj lpLog=%p lpData=%p\n", lpLog, lpData);
490 if (header->wMagic == BRUSH_MAGIC) {
491 BRUSH_GetObject( (BRUSHOBJ *)header, sizeof(LOGBRUSH), lpLog);
492 dprintf_gdi(stddeb,"EnumObjects // DC_Brush lbStyle=%04X\n", ((LPLOGBRUSH)lpLog)->lbStyle);
493 dprintf_gdi(stddeb,"EnumObjects // DC_Brush lbColor=%08lX\n", ((LPLOGBRUSH)lpLog)->lbColor);
494 dprintf_gdi(stddeb,"EnumObjects // DC_Brush lbHatch=%04lX\n", (LONG)((LPLOGBRUSH)lpLog)->lbHatch);
496 if (header->wMagic == PEN_MAGIC) {
497 PEN_GetObject( (PENOBJ *)header, sizeof(LOGPEN), lpLog);
498 dprintf_gdi(stddeb,"EnumObjects // DC_Pen lopnStyle=%04X\n", ((LPLOGPEN)lpLog)->lopnStyle);
499 dprintf_gdi(stddeb,"EnumObjects // DC_Pen lopnWidth=%ld\n", (LONG)((LPLOGPEN)lpLog)->lopnWidth.x);
500 dprintf_gdi(stddeb,"EnumObjects // DC_Pen lopnColor=%08lX\n", ((LPLOGPEN)lpLog)->lopnColor);
502 nRet = CallEnumObjectsProc(lpEnumFunc, GDI_HEAP_SEG_ADDR(hLog),
509 dprintf_gdi(stddeb,"EnumObjects // End of enumeration !\n");
513 /***********************************************************************
514 * IsGDIObject(GDI.462)
516 BOOL IsGDIObject(HANDLE handle)
520 object = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );