Release 960114
[wine/gsoc-2012-control.git] / objects / gdiobj.c
blob12cacd3d73b8d3b12108e578a4cc99f0546b4682
1 /*
2 * GDI functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include "gdi.h"
10 #include "color.h"
11 #include "bitmap.h"
12 #include "brush.h"
13 #include "font.h"
14 #include "palette.h"
15 #include "pen.h"
16 #include "region.h"
17 #include "callback.h"
18 #include "stddebug.h"
19 /* #define DEBUG_GDI */
20 #include "debug.h"
21 #include "xmalloc.h"
23 LPSTR GDI_Heap = NULL;
24 WORD GDI_HeapSel = 0;
26 /* Object types for EnumObjects() */
27 #define OBJ_PEN 1
28 #define OBJ_BRUSH 2
30 #define MAX_OBJ 1024
31 HANDLE *lpPenBrushList = NULL;
34 /***********************************************************************
35 * GDI stock objects
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,
146 NULL,
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 /***********************************************************************
158 * GDI_Init
160 * GDI initialisation.
162 BOOL GDI_Init(void)
164 HPALETTE hpalette;
166 #ifndef WINELIB
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 );
172 #endif
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;
191 return TRUE;
195 /***********************************************************************
196 * GDI_AllocObject
198 HANDLE GDI_AllocObject( WORD size, WORD magic )
200 static DWORD count = 0;
201 GDIOBJHDR * obj;
202 HANDLE handle = GDI_HEAP_ALLOC( size );
203 if (!handle) return 0;
204 obj = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
205 obj->hNext = 0;
206 obj->wMagic = magic;
207 obj->dwCount = ++count;
208 return handle;
212 /***********************************************************************
213 * GDI_FreeObject
215 BOOL GDI_FreeObject( HANDLE handle )
217 GDIOBJHDR * object;
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 */
227 /* Free object */
229 GDI_HEAP_FREE( handle );
230 return TRUE;
233 /***********************************************************************
234 * GDI_GetObjPtr
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];
246 else
247 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
248 if (!ptr) return NULL;
249 if (ptr->wMagic != magic) return NULL;
250 return ptr;
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 );
266 /* Delete object */
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 );
277 return FALSE;
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 /***********************************************************************
295 * GetObject (GDI.82)
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];
306 else
307 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
308 if (!ptr) return 0;
310 switch(ptr->wMagic)
312 case PEN_MAGIC:
313 return PEN_GetObject( (PENOBJ *)ptr, count, buffer );
314 case BRUSH_MAGIC:
315 return BRUSH_GetObject( (BRUSHOBJ *)ptr, count, buffer );
316 case BITMAP_MAGIC:
317 return BITMAP_GetObject( (BITMAPOBJ *)ptr, count, buffer );
318 case FONT_MAGIC:
319 return FONT_GetObject( (FONTOBJ *)ptr, count, buffer );
320 case PALETTE_MAGIC:
321 return PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
323 return 0;
327 /***********************************************************************
328 * SelectObject (GDI.45)
330 HANDLE SelectObject( HDC hdc, HANDLE handle )
332 GDIOBJHDR * ptr = NULL;
333 DC * dc;
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];
339 else
340 ptr = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
341 if (!ptr) return 0;
343 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
344 if (!dc)
346 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
347 if (!dc) return 0;
350 switch(ptr->wMagic)
352 case PEN_MAGIC:
353 return PEN_SelectObject( dc, handle, (PENOBJ *)ptr );
354 case BRUSH_MAGIC:
355 return BRUSH_SelectObject( hdc, dc, handle, (BRUSHOBJ *)ptr );
356 case BITMAP_MAGIC:
357 return BITMAP_SelectObject( hdc, dc, handle, (BITMAPOBJ *)ptr );
358 case FONT_MAGIC:
359 return FONT_SelectObject( dc, handle, (FONTOBJ *)ptr );
360 case REGION_MAGIC:
361 return (HANDLE)SelectClipRgn( hdc, handle );
363 return 0;
367 /***********************************************************************
368 * UnrealizeObject (GDI.150)
370 BOOL UnrealizeObject( HANDLE handle )
372 dprintf_gdi(stdnimp, "UnrealizeObject: "NPFMT"\n", handle );
373 return TRUE;
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)
394 int i, retval = 0;
396 dprintf_gdi( stddeb, "EnumObjects: "NPFMT" %d %08lx %08lx\n",
397 hdc, nObjType, (DWORD)lpEnumFunc, lParam );
398 switch(nObjType)
400 case OBJ_PEN:
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),
406 lParam );
407 dprintf_gdi( stddeb, "EnumObject: solid pen %08lx, ret=%d\n",
408 solid_colors[i], retval);
409 if (!retval) break;
411 break;
413 case OBJ_BRUSH:
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),
419 lParam );
420 dprintf_gdi( stddeb, "EnumObject: solid brush %08lx, ret=%d\n",
421 solid_colors[i], retval);
422 if (!retval) break;
424 if (!retval) break;
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),
431 lParam );
432 dprintf_gdi( stddeb, "EnumObject: hatched brush %d, ret=%d\n",
433 i, retval);
434 if (!retval) break;
436 break;
438 default:
439 fprintf( stderr, "EnumObjects: invalid type %d\n", nObjType );
440 break;
442 return retval;
443 #if 0
445 /* HANDLE handle;
446 DC *dc;*/
447 HANDLE *lphObj;
448 GDIOBJHDR *header;
449 WORD wMagic;
450 LPSTR lpLog; /* Point to a LOGBRUSH or LOGPEN struct */
451 HANDLE hLog;
452 int nRet = 0;
454 if (lpEnumFunc == 0) {
455 fprintf(stderr,"EnumObjects // Bad EnumProc callback address !\n");
456 return 0;
458 switch (nObjType) {
459 case OBJ_PEN:
460 wMagic = PEN_MAGIC;
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);
465 if (lpLog == NULL) {
466 fprintf(stderr,"EnumObjects // Unable to alloc LOGPEN struct !\n");
467 return 0;
469 break;
470 case OBJ_BRUSH:
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);
476 if (lpLog == NULL) {
477 fprintf(stderr,"EnumObjects // Unable to alloc LOGBRUSH struct !\n");
478 return 0;
480 break;
481 default:
482 fprintf(stderr,"EnumObjects("NPFMT", %04X, %08lx, %p); // Unknown OBJ type !\n",
483 hDC, nObjType, (LONG)lpEnumFunc, lpData);
484 return 0;
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),
506 (int)lpData );
507 dprintf_gdi(stddeb,"EnumObjects // after Callback!\n");
508 if (nRet == 0) {
509 GDI_HEAP_FREE(hLog);
510 dprintf_gdi(stddeb,"EnumObjects // EnumEnd requested by application !\n");
511 return 0;
515 dprintf_gdi(stddeb,"EnumObjects // Now DC owned objects %p !\n", header);
516 #endif /* notdef */
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),
537 (LONG)lpData);
538 if (nRet == 0)
539 break;
542 GDI_HEAP_FREE(hLog);
543 dprintf_gdi(stddeb,"EnumObjects // End of enumeration !\n");
544 return nRet;
545 #endif
549 /***********************************************************************
550 * IsGDIObject(GDI.462)
552 BOOL IsGDIObject(HANDLE handle)
554 GDIOBJHDR *object;
556 object = (GDIOBJHDR *) GDI_HEAP_LIN_ADDR( handle );
557 if (object)
558 return TRUE;
559 else
560 return FALSE;