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
31 #include "gdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi
);
36 /* GDI logical pen object */
39 struct brush_pattern pattern
;
44 static HGDIOBJ
PEN_SelectObject( HGDIOBJ handle
, HDC hdc
);
45 static INT
PEN_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
46 static BOOL
PEN_DeleteObject( HGDIOBJ handle
);
48 static const struct gdi_obj_funcs pen_funcs
=
50 PEN_SelectObject
, /* pSelectObject */
51 PEN_GetObject
, /* pGetObjectA */
52 PEN_GetObject
, /* pGetObjectW */
53 NULL
, /* pUnrealizeObject */
54 PEN_DeleteObject
/* pDeleteObject */
58 /***********************************************************************
61 HPEN WINAPI
CreatePen( INT style
, INT width
, COLORREF color
)
65 TRACE("%d %d %06x\n", style
, width
, color
);
67 logpen
.lopnStyle
= style
;
68 logpen
.lopnWidth
.x
= width
;
69 logpen
.lopnWidth
.y
= 0;
70 logpen
.lopnColor
= color
;
72 return CreatePenIndirect( &logpen
);
76 /***********************************************************************
77 * CreatePenIndirect (GDI32.@)
79 HPEN WINAPI
CreatePenIndirect( const LOGPEN
* pen
)
84 if (pen
->lopnStyle
== PS_NULL
)
86 hpen
= GetStockObject(NULL_PEN
);
87 if (hpen
) return hpen
;
90 if (!(penPtr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*penPtr
) ))) return 0;
92 penPtr
->logpen
.elpPenStyle
= pen
->lopnStyle
;
93 penPtr
->logpen
.elpWidth
= abs(pen
->lopnWidth
.x
);
94 penPtr
->logpen
.elpColor
= pen
->lopnColor
;
95 penPtr
->logpen
.elpBrushStyle
= BS_SOLID
;
97 switch (pen
->lopnStyle
)
107 penPtr
->logpen
.elpWidth
= 1;
108 penPtr
->logpen
.elpColor
= 0;
111 penPtr
->logpen
.elpPenStyle
= PS_SOLID
;
115 if (!(hpen
= alloc_gdi_handle( penPtr
, OBJ_PEN
, &pen_funcs
)))
116 HeapFree( GetProcessHeap(), 0, penPtr
);
120 /***********************************************************************
121 * ExtCreatePen (GDI32.@)
124 HPEN WINAPI
ExtCreatePen( DWORD style
, DWORD width
,
125 const LOGBRUSH
* brush
, DWORD style_count
,
126 const DWORD
*style_bits
)
128 PENOBJ
*penPtr
= NULL
;
132 if ((style_count
|| style_bits
) && (style
& PS_STYLE_MASK
) != PS_USERSTYLE
)
135 switch (style
& PS_STYLE_MASK
)
138 return CreatePen( PS_NULL
, 0, brush
->lbColor
);
148 if (((INT
)style_count
) <= 0) return 0;
150 if ((style_count
> 16) || !style_bits
) goto invalid
;
152 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
)
155 BOOL has_neg
= FALSE
, all_zero
= TRUE
;
157 for(i
= 0; (i
< style_count
) && !has_neg
; i
++)
159 has_neg
= has_neg
|| (((INT
)(style_bits
[i
])) < 0);
160 all_zero
= all_zero
&& (style_bits
[i
] == 0);
163 if (all_zero
|| has_neg
) goto invalid
;
167 case PS_INSIDEFRAME
: /* applicable only for geometric pens */
168 if ((style
& PS_TYPE_MASK
) != PS_GEOMETRIC
) goto invalid
;
171 case PS_ALTERNATE
: /* applicable only for cosmetic pens */
172 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
) goto invalid
;
176 SetLastError(ERROR_INVALID_PARAMETER
);
180 if ((style
& PS_TYPE_MASK
) == PS_GEOMETRIC
)
182 if (brush
->lbStyle
== BS_NULL
) return CreatePen( PS_NULL
, 0, 0 );
186 if (width
!= 1) goto invalid
;
187 if (brush
->lbStyle
!= BS_SOLID
) goto invalid
;
190 if (!(penPtr
= HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ
,logpen
.elpStyleEntry
[style_count
]))))
194 if (!store_brush_pattern( &logbrush
, &penPtr
->pattern
)) goto invalid
;
195 if (logbrush
.lbStyle
== BS_DIBPATTERN
) logbrush
.lbStyle
= BS_DIBPATTERNPT
;
197 penPtr
->logpen
.elpPenStyle
= style
;
198 penPtr
->logpen
.elpWidth
= abs((int)width
);
199 penPtr
->logpen
.elpBrushStyle
= logbrush
.lbStyle
;
200 penPtr
->logpen
.elpColor
= logbrush
.lbColor
;
201 penPtr
->logpen
.elpHatch
= brush
->lbHatch
;
202 penPtr
->logpen
.elpNumEntries
= style_count
;
203 memcpy(penPtr
->logpen
.elpStyleEntry
, style_bits
, style_count
* sizeof(DWORD
));
205 if (!(hpen
= alloc_gdi_handle( penPtr
, OBJ_EXTPEN
, &pen_funcs
)))
207 free_brush_pattern( &penPtr
->pattern
);
208 HeapFree( GetProcessHeap(), 0, penPtr
);
213 HeapFree( GetProcessHeap(), 0, penPtr
);
214 SetLastError( ERROR_INVALID_PARAMETER
);
218 /***********************************************************************
221 static HGDIOBJ
PEN_SelectObject( HGDIOBJ handle
, HDC hdc
)
225 DC
*dc
= get_dc_ptr( hdc
);
229 SetLastError( ERROR_INVALID_HANDLE
);
233 if ((pen
= GDI_GetObjPtr( handle
, 0 )))
235 struct brush_pattern
*pattern
;
236 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectPen
);
238 switch (GetObjectType( handle
))
244 pattern
= &pen
->pattern
;
245 if (!pattern
->info
) pattern
= NULL
;
248 GDI_ReleaseObj( handle
);
249 release_dc_ptr( dc
);
253 GDI_inc_ref_count( handle
);
254 GDI_ReleaseObj( handle
);
256 if (!physdev
->funcs
->pSelectPen( physdev
, handle
, pattern
))
258 GDI_dec_ref_count( handle
);
264 GDI_dec_ref_count( ret
);
267 release_dc_ptr( dc
);
272 /***********************************************************************
275 static BOOL
PEN_DeleteObject( HGDIOBJ handle
)
277 PENOBJ
*pen
= free_gdi_handle( handle
);
279 if (!pen
) return FALSE
;
280 free_brush_pattern( &pen
->pattern
);
281 return HeapFree( GetProcessHeap(), 0, pen
);
285 /***********************************************************************
288 static INT
PEN_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
290 PENOBJ
*pen
= GDI_GetObjPtr( handle
, 0 );
295 switch (GetObjectType( handle
))
301 if (!buffer
) ret
= sizeof(LOGPEN
);
302 else if (count
< sizeof(LOGPEN
)) ret
= 0;
303 else if ((pen
->logpen
.elpPenStyle
& PS_STYLE_MASK
) == PS_NULL
&& count
== sizeof(EXTLOGPEN
))
305 EXTLOGPEN
*elp
= buffer
;
308 ret
= sizeof(EXTLOGPEN
);
313 lp
->lopnStyle
= pen
->logpen
.elpPenStyle
;
314 lp
->lopnColor
= pen
->logpen
.elpColor
;
315 lp
->lopnWidth
.x
= pen
->logpen
.elpWidth
;
317 ret
= sizeof(LOGPEN
);
323 ret
= sizeof(EXTLOGPEN
) + pen
->logpen
.elpNumEntries
* sizeof(DWORD
) - sizeof(pen
->logpen
.elpStyleEntry
);
326 if (count
< ret
) ret
= 0;
327 else memcpy(buffer
, &pen
->logpen
, ret
);
331 GDI_ReleaseObj( handle
);