4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi
);
34 /* GDI logical pen object */
37 struct brush_pattern pattern
;
42 static HGDIOBJ
PEN_SelectObject( HGDIOBJ handle
, HDC hdc
);
43 static INT
PEN_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
44 static BOOL
PEN_DeleteObject( HGDIOBJ handle
);
46 static const struct gdi_obj_funcs pen_funcs
=
48 PEN_SelectObject
, /* pSelectObject */
49 PEN_GetObject
, /* pGetObjectA */
50 PEN_GetObject
, /* pGetObjectW */
51 NULL
, /* pUnrealizeObject */
52 PEN_DeleteObject
/* pDeleteObject */
56 /***********************************************************************
59 HPEN WINAPI
CreatePen( INT style
, INT width
, COLORREF color
)
63 TRACE("%d %d %06x\n", style
, width
, color
);
65 logpen
.lopnStyle
= style
;
66 logpen
.lopnWidth
.x
= width
;
67 logpen
.lopnWidth
.y
= 0;
68 logpen
.lopnColor
= color
;
70 return CreatePenIndirect( &logpen
);
74 /***********************************************************************
75 * CreatePenIndirect (GDI32.@)
77 HPEN WINAPI
CreatePenIndirect( const LOGPEN
* pen
)
82 if (pen
->lopnStyle
== PS_NULL
)
84 hpen
= GetStockObject(NULL_PEN
);
85 if (hpen
) return hpen
;
88 if (!(penPtr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*penPtr
) ))) return 0;
90 penPtr
->logpen
.elpPenStyle
= pen
->lopnStyle
;
91 penPtr
->logpen
.elpWidth
= abs(pen
->lopnWidth
.x
);
92 penPtr
->logpen
.elpColor
= pen
->lopnColor
;
93 penPtr
->logpen
.elpBrushStyle
= BS_SOLID
;
95 switch (pen
->lopnStyle
)
105 penPtr
->logpen
.elpWidth
= 1;
106 penPtr
->logpen
.elpColor
= 0;
109 penPtr
->logpen
.elpPenStyle
= PS_SOLID
;
113 if (!(hpen
= alloc_gdi_handle( penPtr
, OBJ_PEN
, &pen_funcs
)))
114 HeapFree( GetProcessHeap(), 0, penPtr
);
118 /***********************************************************************
119 * ExtCreatePen (GDI32.@)
122 HPEN WINAPI
ExtCreatePen( DWORD style
, DWORD width
,
123 const LOGBRUSH
* brush
, DWORD style_count
,
124 const DWORD
*style_bits
)
126 PENOBJ
*penPtr
= NULL
;
130 if ((style_count
|| style_bits
) && (style
& PS_STYLE_MASK
) != PS_USERSTYLE
)
133 switch (style
& PS_STYLE_MASK
)
136 return CreatePen( PS_NULL
, 0, brush
->lbColor
);
146 if (((INT
)style_count
) <= 0) return 0;
148 if ((style_count
> 16) || !style_bits
) goto invalid
;
150 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
)
153 BOOL has_neg
= FALSE
, all_zero
= TRUE
;
155 for(i
= 0; (i
< style_count
) && !has_neg
; i
++)
157 has_neg
= has_neg
|| (((INT
)(style_bits
[i
])) < 0);
158 all_zero
= all_zero
&& (style_bits
[i
] == 0);
161 if (all_zero
|| has_neg
) goto invalid
;
165 case PS_INSIDEFRAME
: /* applicable only for geometric pens */
166 if ((style
& PS_TYPE_MASK
) != PS_GEOMETRIC
) goto invalid
;
169 case PS_ALTERNATE
: /* applicable only for cosmetic pens */
170 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
) goto invalid
;
174 SetLastError(ERROR_INVALID_PARAMETER
);
178 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
)
180 if (brush
->lbStyle
== BS_NULL
) return CreatePen( PS_NULL
, 0, 0 );
184 if (width
!= 1) goto invalid
;
185 if (brush
->lbStyle
!= BS_SOLID
) goto invalid
;
188 if (!(penPtr
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ
,logpen
.elpStyleEntry
[style_count
]))))
192 if (!store_brush_pattern( &logbrush
, &penPtr
->pattern
)) goto invalid
;
193 if (logbrush
.lbStyle
== BS_DIBPATTERN
) logbrush
.lbStyle
= BS_DIBPATTERNPT
;
195 penPtr
->logpen
.elpPenStyle
= style
;
196 penPtr
->logpen
.elpWidth
= abs((int)width
);
197 penPtr
->logpen
.elpBrushStyle
= logbrush
.lbStyle
;
198 penPtr
->logpen
.elpColor
= logbrush
.lbColor
;
199 penPtr
->logpen
.elpHatch
= brush
->lbHatch
;
200 penPtr
->logpen
.elpNumEntries
= style_count
;
201 memcpy(penPtr
->logpen
.elpStyleEntry
, style_bits
, style_count
* sizeof(DWORD
));
203 if (!(hpen
= alloc_gdi_handle( penPtr
, OBJ_EXTPEN
, &pen_funcs
)))
205 free_brush_pattern( &penPtr
->pattern
);
206 HeapFree( GetProcessHeap(), 0, penPtr
);
211 HeapFree( GetProcessHeap(), 0, penPtr
);
212 SetLastError( ERROR_INVALID_PARAMETER
);
216 /***********************************************************************
219 static HGDIOBJ
PEN_SelectObject( HGDIOBJ handle
, HDC hdc
)
223 DC
*dc
= get_dc_ptr( hdc
);
228 SetLastError( ERROR_INVALID_HANDLE
);
232 if ((pen
= get_any_obj_ptr( handle
, &type
)))
234 struct brush_pattern
*pattern
;
235 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectPen
);
243 pattern
= &pen
->pattern
;
244 if (!pattern
->info
) pattern
= NULL
;
247 GDI_ReleaseObj( handle
);
248 release_dc_ptr( dc
);
252 GDI_inc_ref_count( handle
);
253 GDI_ReleaseObj( handle
);
255 if (!physdev
->funcs
->pSelectPen( physdev
, handle
, pattern
))
257 GDI_dec_ref_count( handle
);
263 GDI_dec_ref_count( ret
);
266 release_dc_ptr( dc
);
271 /***********************************************************************
274 static BOOL
PEN_DeleteObject( HGDIOBJ handle
)
276 PENOBJ
*pen
= free_gdi_handle( handle
);
278 if (!pen
) return FALSE
;
279 free_brush_pattern( &pen
->pattern
);
280 HeapFree( GetProcessHeap(), 0, pen
);
285 /***********************************************************************
288 static INT
PEN_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
291 PENOBJ
*pen
= get_any_obj_ptr( handle
, &type
);
302 if (!buffer
) ret
= sizeof(LOGPEN
);
303 else if (count
< sizeof(LOGPEN
)) ret
= 0;
304 else if ((pen
->logpen
.elpPenStyle
& PS_STYLE_MASK
) == PS_NULL
&& count
== sizeof(EXTLOGPEN
))
306 EXTLOGPEN
*elp
= buffer
;
309 ret
= sizeof(EXTLOGPEN
);
314 lp
->lopnStyle
= pen
->logpen
.elpPenStyle
;
315 lp
->lopnColor
= pen
->logpen
.elpColor
;
316 lp
->lopnWidth
.x
= pen
->logpen
.elpWidth
;
318 ret
= sizeof(LOGPEN
);
324 ret
= sizeof(EXTLOGPEN
) + pen
->logpen
.elpNumEntries
* sizeof(DWORD
) - sizeof(pen
->logpen
.elpStyleEntry
);
327 if (count
< ret
) ret
= 0;
328 else memcpy(buffer
, &pen
->logpen
, ret
);
332 GDI_ReleaseObj( handle
);