Release 940405
[wine/multimedia.git] / objects / gdiobj.c
blobcd7888755c3809d4a3a5b5a975d2bcf7a563715e
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 "gdi.h"
12 #include "prototypes.h"
14 MDESC *GDI_Heap = NULL;
16 extern HPALETTE COLOR_Init(); /* color.c */
18 /***********************************************************************
19 * GDI stock objects
22 static BRUSHOBJ WhiteBrush =
24 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
25 { BS_SOLID, RGB(255,255,255), 0 } /* logbrush */
28 static BRUSHOBJ LtGrayBrush =
30 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
31 { BS_SOLID, RGB(192,192,192), 0 } /* logbrush */
34 static BRUSHOBJ GrayBrush =
36 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
37 { BS_SOLID, RGB(128,128,128), 0 } /* logbrush */
40 static BRUSHOBJ DkGrayBrush =
42 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
43 { BS_SOLID, RGB(64,64,64), 0 } /* logbrush */
46 static BRUSHOBJ BlackBrush =
48 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
49 { BS_SOLID, RGB(0,0,0), 0 } /* logbrush */
52 static BRUSHOBJ NullBrush =
54 { 0, BRUSH_MAGIC, 1, 0 }, /* header */
55 { BS_NULL, 0, 0 } /* logbrush */
58 static PENOBJ WhitePen =
60 { 0, PEN_MAGIC, 1, 0 }, /* header */
61 { PS_SOLID, { 1, 0 }, RGB(255,255,255) } /* logpen */
64 static PENOBJ BlackPen =
66 { 0, PEN_MAGIC, 1, 0 }, /* header */
67 { PS_SOLID, { 1, 0 }, RGB(0,0,0) } /* logpen */
70 static PENOBJ NullPen =
72 { 0, PEN_MAGIC, 1, 0 }, /* header */
73 { PS_NULL, { 1, 0 }, 0 } /* logpen */
76 static FONTOBJ OEMFixedFont =
78 { 0, FONT_MAGIC, 1, 0 }, /* header */
79 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, OEM_CHARSET,
80 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
83 static FONTOBJ AnsiFixedFont =
85 { 0, FONT_MAGIC, 1, 0 }, /* header */
86 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
87 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
90 static FONTOBJ AnsiVarFont =
92 { 0, FONT_MAGIC, 1, 0 }, /* header */
93 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
94 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, "" }
97 static FONTOBJ SystemFont =
99 { 0, FONT_MAGIC, 1, 0 }, /* header */
100 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
101 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, "" }
104 static FONTOBJ DeviceDefaultFont =
106 { 0, FONT_MAGIC, 1, 0 }, /* header */
107 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
108 0, 0, DEFAULT_QUALITY, VARIABLE_PITCH | FF_SWISS, "" }
111 static FONTOBJ SystemFixedFont =
113 { 0, FONT_MAGIC, 1, 0 }, /* header */
114 { 12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET,
115 0, 0, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, "" }
119 static GDIOBJHDR * StockObjects[NB_STOCK_OBJECTS] =
121 (GDIOBJHDR *) &WhiteBrush,
122 (GDIOBJHDR *) &LtGrayBrush,
123 (GDIOBJHDR *) &GrayBrush,
124 (GDIOBJHDR *) &DkGrayBrush,
125 (GDIOBJHDR *) &BlackBrush,
126 (GDIOBJHDR *) &NullBrush,
127 (GDIOBJHDR *) &WhitePen,
128 (GDIOBJHDR *) &BlackPen,
129 (GDIOBJHDR *) &NullPen,
130 NULL,
131 (GDIOBJHDR *) &OEMFixedFont,
132 (GDIOBJHDR *) &AnsiFixedFont,
133 (GDIOBJHDR *) &AnsiVarFont,
134 (GDIOBJHDR *) &SystemFont,
135 (GDIOBJHDR *) &DeviceDefaultFont,
136 NULL, /* DEFAULT_PALETTE created by COLOR_Init */
137 (GDIOBJHDR *) &SystemFixedFont
141 /***********************************************************************
142 * GDI_Init
144 * GDI initialisation.
146 BOOL GDI_Init()
148 HPALETTE hpalette;
149 struct segment_descriptor_s * s;
151 #ifndef WINELIB
152 /* Create GDI heap */
154 s = (struct segment_descriptor_s *)GetNextSegment( 0, 0x10000 );
155 if (s == NULL) return FALSE;
156 HEAP_Init( &GDI_Heap, s->base_addr, GDI_HEAP_SIZE );
157 #endif
159 /* Create default palette */
161 if (!(hpalette = COLOR_Init())) return FALSE;
162 StockObjects[DEFAULT_PALETTE] = (GDIOBJHDR *) GDI_HEAP_ADDR( hpalette );
164 /* Create default bitmap */
166 if (!BITMAP_Init()) return FALSE;
168 /* Initialise regions */
170 if (!REGION_Init()) return FALSE;
172 /* Initialise dithering */
174 if (!DITHER_Init()) return FALSE;
176 return TRUE;
180 /***********************************************************************
181 * GDI_FindPrevObject
183 * Return the GDI object whose hNext field points to obj.
185 HANDLE GDI_FindPrevObject( HANDLE first, HANDLE obj )
187 HANDLE handle;
189 for (handle = first; handle && (handle != obj); )
191 GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
192 handle = header->hNext;
194 return handle;
198 /***********************************************************************
199 * GDI_AllocObject
201 HANDLE GDI_AllocObject( WORD size, WORD magic )
203 static DWORD count = 0;
204 GDIOBJHDR * obj;
205 HANDLE handle = GDI_HEAP_ALLOC( GMEM_MOVEABLE, size );
206 if (!handle) return 0;
207 obj = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
208 if (obj == NULL) {
209 printf("GDI_AllocObject // Error trying to get GDI_HEAD_ADDR !\n");
210 return 0;
212 obj->hNext = 0;
213 obj->wMagic = magic;
214 obj->dwCount = ++count;
215 return handle;
219 /***********************************************************************
220 * GDI_FreeObject
222 BOOL GDI_FreeObject( HANDLE handle )
224 GDIOBJHDR * object;
226 /* Can't free stock objects */
227 if (handle >= FIRST_STOCK_HANDLE) return FALSE;
229 object = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
230 if (!object) return FALSE;
232 /* Free object */
234 GDI_HEAP_FREE( handle );
235 return TRUE;
238 /***********************************************************************
239 * GDI_GetObjPtr
241 * Return a pointer to the GDI object associated to the handle.
242 * Return NULL if the object has the wrong magic number.
244 GDIOBJHDR * GDI_GetObjPtr( HANDLE handle, WORD magic )
246 GDIOBJHDR * ptr = NULL;
248 if (handle >= FIRST_STOCK_HANDLE)
250 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
251 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
253 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
254 if (!ptr) return NULL;
255 if (ptr->wMagic != magic) return NULL;
256 return ptr;
260 /***********************************************************************
261 * DeleteObject (GDI.69)
263 BOOL DeleteObject( HANDLE obj )
265 /* Check if object is valid */
267 GDIOBJHDR * header = (GDIOBJHDR *) GDI_HEAP_ADDR( obj );
268 if (!header) return FALSE;
270 #ifdef DEBUG_GDI
271 printf( "DeleteObject: %d\n", obj );
272 #endif
274 /* Delete object */
276 switch(header->wMagic)
278 case PEN_MAGIC: return GDI_FreeObject( obj );
279 case BRUSH_MAGIC: return BRUSH_DeleteObject( obj, header );
280 case FONT_MAGIC: return GDI_FreeObject( obj );
281 case PALETTE_MAGIC: return GDI_FreeObject( obj );
282 case BITMAP_MAGIC: return BMP_DeleteObject( obj, header );
283 case REGION_MAGIC: return REGION_DeleteObject( obj, header );
285 return FALSE;
289 /***********************************************************************
290 * GetStockObject (GDI.87)
292 HANDLE GetStockObject( int obj )
294 if ((obj < 0) || (obj >= NB_STOCK_OBJECTS)) return 0;
295 if (!StockObjects[obj]) return 0;
296 #ifdef DEBUG_GDI
297 printf( "GetStockObject: returning %04x\n", FIRST_STOCK_HANDLE + obj );
298 #endif
299 return FIRST_STOCK_HANDLE + obj;
303 /***********************************************************************
304 * GetObject (GDI.82)
306 int GetObject( HANDLE handle, int count, LPSTR buffer )
308 GDIOBJHDR * ptr = NULL;
309 #ifdef DEBUG_GDI
310 printf( "GetObject: %04x %d %08x\n", handle, count, buffer );
311 #endif
312 if (!count) return 0;
314 if (handle >= FIRST_STOCK_HANDLE)
316 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
317 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
319 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
320 if (!ptr) return 0;
322 switch(ptr->wMagic)
324 case PEN_MAGIC:
325 return PEN_GetObject( (PENOBJ *)ptr, count, buffer );
326 case BRUSH_MAGIC:
327 return BRUSH_GetObject( (BRUSHOBJ *)ptr, count, buffer );
328 case BITMAP_MAGIC:
329 return BMP_GetObject( (BITMAPOBJ *)ptr, count, buffer );
330 case FONT_MAGIC:
331 return FONT_GetObject( (FONTOBJ *)ptr, count, buffer );
332 case PALETTE_MAGIC:
333 return PALETTE_GetObject( (PALETTEOBJ *)ptr, count, buffer );
335 return 0;
339 /***********************************************************************
340 * SelectObject (GDI.45)
342 HANDLE SelectObject( HDC hdc, HANDLE handle )
344 GDIOBJHDR * ptr = NULL;
345 DC * dc;
347 #ifdef DEBUG_GDI
348 printf( "SelectObject: %d %04x\n", hdc, handle );
349 #endif
350 if (handle >= FIRST_STOCK_HANDLE)
352 if (handle < FIRST_STOCK_HANDLE + NB_STOCK_OBJECTS)
353 ptr = StockObjects[handle - FIRST_STOCK_HANDLE];
355 else ptr = (GDIOBJHDR *) GDI_HEAP_ADDR( handle );
356 if (!ptr) return 0;
358 dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
359 if (!dc) return 0;
361 switch(ptr->wMagic)
363 case PEN_MAGIC:
364 return PEN_SelectObject( dc, handle, (PENOBJ *)ptr );
365 case BRUSH_MAGIC:
366 return BRUSH_SelectObject( hdc, dc, handle, (BRUSHOBJ *)ptr );
367 case BITMAP_MAGIC:
368 return BITMAP_SelectObject( hdc, dc, handle, (BITMAPOBJ *)ptr );
369 case FONT_MAGIC:
370 return FONT_SelectObject( dc, handle, (FONTOBJ *)ptr );
371 case REGION_MAGIC:
372 return SelectClipRgn( hdc, handle );
374 return 0;
378 /***********************************************************************
379 * UnrealizeObject (GDI.150)
381 BOOL UnrealizeObject( HANDLE handle )
383 #ifdef DEBUG_GDI
384 printf( "UnrealizeObject: %04x\n", handle );
385 #endif
386 return TRUE;