Release 940912
[wine/gsoc-2012-control.git] / objects / gdiobj.c
blob7937282c4a660ba7458efcc57a37611a6ef528f5
1 /*
2 * GDI functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "user.h"
12 #include "gdi.h"
13 #include "prototypes.h"
15 MDESC *GDI_Heap = NULL;
17 /* Object types for EnumObjects() */
18 #define OBJ_PEN 1
19 #define OBJ_BRUSH 2
21 #define MAX_OBJ 1024
22 HANDLE *lpPenBrushList = NULL;
24 extern HPALETTE COLOR_Init(); /* color.c */
26 /***********************************************************************
27 * GDI stock objects
30 static BRUSHOBJ WhiteBrush =
32 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
33 { BS_SOLID, RGB(255,255,255), 0 } /* logbrush */
36 static BRUSHOBJ LtGrayBrush =
38 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
39 { BS_SOLID, RGB(192,192,192), 0 } /* logbrush */
42 static BRUSHOBJ GrayBrush =
44 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
45 { BS_SOLID, RGB(128,128,128), 0 } /* logbrush */
48 static BRUSHOBJ DkGrayBrush =
50 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
51 { BS_SOLID, RGB(64,64,64), 0 } /* logbrush */
54 static BRUSHOBJ BlackBrush =
56 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
57 { BS_SOLID, RGB(0,0,0), 0 } /* logbrush */
60 static BRUSHOBJ NullBrush =
62 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
63 { BS_NULL, 0, 0 } /* logbrush */
66 static PENOBJ WhitePen =
68 { 0, PEN_MAGIC, 1, 0 }, /* header */
69 { PS_SOLID, { 1, 0 }, RGB(255,255,255) } /* logpen */
72 static PENOBJ BlackPen =
74 { 0, PEN_MAGIC, 1, 0 }, /* header */
75 { PS_SOLID, { 1, 0 }, RGB(0,0,0) } /* logpen */
78 static PENOBJ NullPen =
80 { 0, PEN_MAGIC, 1, 0 }, /* header */
81 { PS_NULL, { 1, 0 }, 0 } /* logpen */
84 static FONTOBJ OEMFixedFont =
86 { 0, FONT_MAGIC, 1, 0 }, /* header */
87 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, OEM_CHARSET,
88 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
91 static FONTOBJ AnsiFixedFont =
93 { 0, FONT_MAGIC, 1, 0 }, /* header */
94 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
95 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
98 static FONTOBJ AnsiVarFont =
100 { 0, FONT_MAGIC, 1, 0 }, /* header */
101 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
102 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, "" }
105 static FONTOBJ SystemFont =
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 DeviceDefaultFont =
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 SystemFixedFont =
121 { 0, FONT_MAGIC, 1, 0 }, /* header */
122 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
123 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
127 static GDIOBJHDR * StockObjects[NB_STOCK_OBJECTS] =
129 (GDIOBJHDR *) &WhiteBrush,
130 (GDIOBJHDR *) &LtGrayBrush,
131 (GDIOBJHDR *) &GrayBrush,
132 (GDIOBJHDR *) &DkGrayBrush,
133 (GDIOBJHDR *) &BlackBrush,
134 (GDIOBJHDR *) &NullBrush,
135 (GDIOBJHDR *) &WhitePen,
136 (GDIOBJHDR *) &BlackPen,
137 (GDIOBJHDR *) &NullPen,
138 NULL,
139 (GDIOBJHDR *) &OEMFixedFont,
140 (GDIOBJHDR *) &AnsiFixedFont,
141 (GDIOBJHDR *) &AnsiVarFont,
142 (GDIOBJHDR *) &SystemFont,
143 (GDIOBJHDR *) &DeviceDefaultFont,
144 NULL, /* DEFAULT_PALETTE created by COLOR_Init */
145 (GDIOBJHDR *) &SystemFixedFont
149 /***********************************************************************
150 * GDI_Init
152 * GDI initialisation.
154 BOOL GDI_Init()
156 HPALETTE hpalette;
157 struct segment_descriptor_s * s;
159 #ifndef WINELIB
160 /* Create GDI heap */
162 s = (struct segment_descriptor_s *)GetNextSegment( 0, 0x10000 );
163 if (s == NULL) return FALSE;
164 HEAP_Init( &GDI_Heap, s->base_addr, GDI_HEAP_SIZE );
165 #endif
167 /* Create default palette */
169 if (!(hpalette = COLOR_Init())) return FALSE;
170 StockObjects[DEFAULT_PALETTE] = (GDIOBJHDR *) GDI_HEAP_ADDR( hpalette );
172 /* Create default bitmap */
174 if (!BITMAP_Init()) return FALSE;
176 /* Initialise regions */
178 if (!REGION_Init()) return FALSE;
180 /* Initialise dithering */
182 if (!DITHER_Init()) return FALSE;
184 return TRUE;
188 /***********************************************************************
189 * GDI_AppendToPenBrushList
191 BOOL GDI_AppendToPenBrushList(HANDLE hNewObj)
193 HANDLE *lphObj;
194 int i = 1;
195 if (hNewObj == 0) return FALSE;
196 if (lpPenBrushList == NULL) {
197 lpPenBrushList = malloc(MAX_OBJ * sizeof(HANDLE));
198 lpPenBrushList[0] = 0;
199 #ifdef DEBUG_GDI
200 printf("GDI_AppendToPenBrushList() lpPenBrushList allocated !\n");
201 #endif
203 for (lphObj = lpPenBrushList; i < MAX_OBJ; i++) {
204 if (*lphObj == 0) {
205 *lphObj = hNewObj;
206 *(lphObj + 1) = 0;
207 #ifdef DEBUG_GDI
208 printf("GDI_AppendToPenBrushList(%04X) appended (count=%d)\n", hNewObj, i);
209 #endif
210 return TRUE;
212 lphObj++;
214 return FALSE;
218 /***********************************************************************
219 * GDI_FindPrevObject
221 * Return the GDI object whose hNext field points to obj.
223 HANDLE GDI_FindPrevObject( HANDLE first, HANDLE obj )
225 HANDLE handle;
227 for (handle = first; handle && (handle != obj); )
229 GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
230 handle = header->hNext;
232 return handle;
236 /***********************************************************************
237 * GDI_AllocObject
239 HANDLE GDI_AllocObject( WORD size, WORD magic )
241 static DWORD count = 0;
242 GDIOBJHDR * obj;
243 HANDLE handle = GDI_HEAP_ALLOC( GMEM_MOVEABLE, size );
244 if (!handle) return 0;
245 obj = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
246 if (obj == NULL) {
247 printf("GDI_AllocObject // Error trying to get GDI_HEAD_ADDR !\n");
248 return 0;
250 obj->hNext = 0;
251 obj->wMagic = magic;
252 obj->dwCount = ++count;
253 if (magic == PEN_MAGIC || magic == BRUSH_MAGIC) {
254 GDI_AppendToPenBrushList(handle);
256 return handle;
260 /***********************************************************************
261 * GDI_FreeObject
263 BOOL GDI_FreeObject( HANDLE handle )
265 GDIOBJHDR * object;
267 /* Can't free stock objects */
268 if (handle >= FIRST_STOCK_HANDLE) return FALSE;
270 object = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
271 if (!object) return FALSE;
273 /* Free object */
275 GDI_HEAP_FREE( handle );
276 return TRUE;
279 /***********************************************************************
280 * GDI_GetObjPtr
282 * Return a pointer to the GDI object associated to the handle.
283 * Return NULL if the object has the wrong magic number.
285 GDIOBJHDR * GDI_GetObjPtr( HANDLE handle, WORD magic )
287 GDIOBJHDR * ptr = NULL;
289 if (handle >= FIRST_STOCK_HANDLE)
291 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
292 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
294 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
295 if (!ptr) return NULL;
296 if (ptr->wMagic != magic) return NULL;
297 return ptr;
301 /***********************************************************************
302 * DeleteObject (GDI.69)
304 BOOL DeleteObject( HANDLE obj )
306 /* Check if object is valid */
308 GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_ADDR( obj );
309 if (!header) return FALSE;
311 #ifdef DEBUG_GDI
312 printf( "DeleteObject: %d\n", obj );
313 #endif
315 /* Delete object */
317 switch(header->wMagic)
319 case PEN_MAGIC: return GDI_FreeObject( obj );
320 case BRUSH_MAGIC: return BRUSH_DeleteObject( obj, header );
321 case FONT_MAGIC: return GDI_FreeObject( obj );
322 case PALETTE_MAGIC: return GDI_FreeObject( obj );
323 case BITMAP_MAGIC: return BMP_DeleteObject( obj, header );
324 case REGION_MAGIC: return REGION_DeleteObject( obj, header );
326 return FALSE;
330 /***********************************************************************
331 * GetStockObject (GDI.87)
333 HANDLE GetStockObject( int obj )
335 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
336 if (!StockObjects[obj]) return 0;
337 #ifdef DEBUG_GDI
338 printf( "GetStockObject: returning %04x\n", FIRST_STOCK_HANDLE + obj );
339 #endif
340 return FIRST_STOCK_HANDLE + obj;
344 /***********************************************************************
345 * GetObject (GDI.82)
347 int GetObject( HANDLE handle, int count, LPSTR buffer )
349 GDIOBJHDR * ptr = NULL;
350 #ifdef DEBUG_GDI
351 printf( "GetObject: %04x %d %08x\n", handle, count, buffer );
352 #endif
353 if (!count) return 0;
355 if (handle >= FIRST_STOCK_HANDLE)
357 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
358 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
360 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
361 if (!ptr) return 0;
363 switch(ptr->wMagic)
365 case PEN_MAGIC:
366 return PEN_GetObject( (PENOBJ *)ptr, count, buffer );
367 case BRUSH_MAGIC:
368 return BRUSH_GetObject( (BRUSHOBJ *)ptr, count, buffer );
369 case BITMAP_MAGIC:
370 return BMP_GetObject( (BITMAPOBJ *)ptr, count, buffer );
371 case FONT_MAGIC:
372 return FONT_GetObject( (FONTOBJ *)ptr, count, buffer );
373 case PALETTE_MAGIC:
374 return PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
376 return 0;
380 /***********************************************************************
381 * SelectObject (GDI.45)
383 HANDLE SelectObject( HDC hdc, HANDLE handle )
385 GDIOBJHDR * ptr = NULL;
386 DC * dc;
388 #ifdef DEBUG_GDI
389 printf( "SelectObject: %d %04x\n", hdc, handle );
390 #endif
391 if (handle >= FIRST_STOCK_HANDLE)
393 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
394 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
396 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
397 if (!ptr) return 0;
399 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
400 if (!dc)
402 dc = (DC *)GDI_GetObjPtr(hdc, METAFILE_DC_MAGIC);
403 if (!dc) return 0;
406 switch(ptr->wMagic)
408 case PEN_MAGIC:
409 return PEN_SelectObject( dc, handle, (PENOBJ *)ptr );
410 case BRUSH_MAGIC:
411 return BRUSH_SelectObject( hdc, dc, handle, (BRUSHOBJ *)ptr );
412 case BITMAP_MAGIC:
413 return BITMAP_SelectObject( hdc, dc, handle, (BITMAPOBJ *)ptr );
414 case FONT_MAGIC:
415 return FONT_SelectObject( dc, handle, (FONTOBJ *)ptr );
416 case REGION_MAGIC:
417 return SelectClipRgn( hdc, handle );
419 return 0;
423 /***********************************************************************
424 * UnrealizeObject (GDI.150)
426 BOOL UnrealizeObject( HANDLE handle )
428 #ifdef DEBUG_GDI
429 printf( "UnrealizeObject: %04x\n", handle );
430 #endif
431 return TRUE;
435 /***********************************************************************
436 * EnumObjects (GDI.71)
438 int EnumObjects(HDC hDC, int nObjType, FARPROC lpEnumFunc, LPSTR lpData)
440 HANDLE handle;
441 DC *dc;
442 HANDLE *lphObj;
443 GDIOBJHDR *header;
444 WORD wMagic;
445 LPSTR lpLog; /* Point to a LOGBRUSH or LOGPEN struct */
446 HANDLE hLog;
447 int i, nRet;
448 if (lpEnumFunc == NULL) {
449 printf("EnumObjects // Bad EnumProc callback address !\n");
450 return 0;
452 switch (nObjType) {
453 case OBJ_PEN:
454 wMagic = PEN_MAGIC;
455 printf("EnumObjects(%04X, OBJ_PEN, %08X, %08X);\n",
456 hDC, lpEnumFunc, lpData);
457 hLog = USER_HEAP_ALLOC(GMEM_MOVEABLE, sizeof(LOGPEN));
458 lpLog = (LPSTR) USER_HEAP_ADDR(hLog);
459 if (lpLog == NULL) {
460 printf("EnumObjects // Unable to alloc LOGPEN struct !\n");
461 return 0;
463 break;
464 case OBJ_BRUSH:
465 wMagic = BRUSH_MAGIC;
466 printf("EnumObjects(%04X, OBJ_BRUSH, %08X, %08X);\n",
467 hDC, lpEnumFunc, lpData);
468 hLog = USER_HEAP_ALLOC(GMEM_MOVEABLE, sizeof(LOGBRUSH));
469 lpLog = (LPSTR) USER_HEAP_ADDR(hLog);
470 if (lpLog == NULL) {
471 printf("EnumObjects // Unable to alloc LOGBRUSH struct !\n");
472 return 0;
474 break;
475 default:
476 printf("EnumObjects(%04X, %04X, %08X, %08X); // Unknown OBJ type !\n",
477 hDC, nObjType, lpEnumFunc, lpData);
478 return 0;
480 printf("EnumObjects // Stock Objects first !\n");
481 for (i = 0; i < NB_STOCK_OBJECTS; i++) {
482 header = StockObjects[i];
483 if (header->wMagic == wMagic) {
484 PEN_GetObject( (PENOBJ *)header, sizeof(LOGPEN), (LPLOGPEN)lpLog);
485 BRUSH_GetObject( (BRUSHOBJ *)header, sizeof(LOGBRUSH), (LPLOGBRUSH)lpLog);
486 printf("EnumObjects // StockObj lpLog=%08X lpData=%08X\n", lpLog, lpData);
487 if (header->wMagic == BRUSH_MAGIC) {
488 printf("EnumObjects // StockBrush lbStyle=%04X\n", ((LPLOGBRUSH)lpLog)->lbStyle);
489 printf("EnumObjects // StockBrush lbColor=%08X\n", ((LPLOGBRUSH)lpLog)->lbColor);
490 printf("EnumObjects // StockBrush lbHatch=%04X\n", ((LPLOGBRUSH)lpLog)->lbHatch);
492 if (header->wMagic == PEN_MAGIC) {
493 printf("EnumObjects // StockPen lopnStyle=%04X\n", ((LPLOGPEN)lpLog)->lopnStyle);
494 printf("EnumObjects // StockPen lopnWidth=%08X\n", ((LPLOGPEN)lpLog)->lopnWidth);
495 printf("EnumObjects // StockPen lopnColor=%08X\n", ((LPLOGPEN)lpLog)->lopnColor);
497 nRet = 1;
499 #ifdef WINELIB
500 nRet = (*lpEnumFunc)(lpLog, lpData);
501 #else
502 nRet = CallBack16(lpEnumFunc, 4, 2, (int)lpLog, 2, (int)lpData);
503 #endif
505 printf("EnumObjects // after CallBack16 !\n");
506 if (nRet == 0) {
507 USER_HEAP_FREE(hLog);
508 printf("EnumObjects // EnumEnd requested by application !\n");
509 return 0;
513 if (lpPenBrushList == NULL) return 0;
514 printf("EnumObjects // Now DC owned objects %08X !\n", header);
515 for (lphObj = lpPenBrushList; *lphObj != 0; ) {
516 #ifdef DEBUG_GDI
517 printf("EnumObjects // *lphObj=%04X\n", *lphObj);
518 #endif
519 header = (GDIOBJHDR *) GDI_HEAP_ADDR(*lphObj++);
520 if (header->wMagic == wMagic) {
521 #ifdef DEBUG_GDI
522 printf("EnumObjects // DC_Obj lpLog=%08X lpData=%08X\n", lpLog, lpData);
523 #endif
524 if (header->wMagic == BRUSH_MAGIC) {
525 BRUSH_GetObject( (BRUSHOBJ *)header, sizeof(LOGBRUSH), (LPLOGBRUSH)lpLog);
526 printf("EnumObjects // DC_Brush lbStyle=%04X\n", ((LPLOGBRUSH)lpLog)->lbStyle);
527 printf("EnumObjects // DC_Brush lbColor=%08X\n", ((LPLOGBRUSH)lpLog)->lbColor);
528 printf("EnumObjects // DC_Brush lbHatch=%04X\n", ((LPLOGBRUSH)lpLog)->lbHatch);
530 if (header->wMagic == PEN_MAGIC) {
531 PEN_GetObject( (PENOBJ *)header, sizeof(LOGPEN), (LPLOGPEN)lpLog);
532 printf("EnumObjects // DC_Pen lopnStyle=%04X\n", ((LPLOGPEN)lpLog)->lopnStyle);
533 printf("EnumObjects // DC_Pen lopnWidth=%08X\n", ((LPLOGPEN)lpLog)->lopnWidth);
534 printf("EnumObjects // DC_Pen lopnColor=%08X\n", ((LPLOGPEN)lpLog)->lopnColor);
537 #ifdef WINELIB
538 nRet = (*lpEnumFunc)(lpLog, lpData);
539 #else
540 nRet = CallBack16(lpEnumFunc, 4, 2, (int)lpLog, 2, (int)lpData);
541 #endif
543 nRet = 1;
544 printf("EnumObjects // after CallBack16 !\n");
545 if (nRet == 0) {
546 USER_HEAP_FREE(hLog);
547 printf("EnumObjects // EnumEnd requested by application !\n");
548 return 0;
552 USER_HEAP_FREE(hLog);
553 printf("EnumObjects // End of enumeration !\n");
554 return 0;
557 /***********************************************************************
558 * IsGDIObject(GDI.462)
560 BOOL IsGDIObject(HANDLE handle)
562 GDIOBJHDR *object;
564 object = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
565 if (object)
566 return TRUE;
567 else
568 return FALSE;