winex11: Add window data locking to the system tray functions.
[wine] / dlls / winex11.drv / pen.c
1 /*
2  * X11DRV pen objects
3  *
4  * Copyright 1993 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include "x11drv.h"
24
25
26 static DWORD get_user_dashes( char *res, const DWORD *style, DWORD len )
27 {
28     DWORD i, pos, dashes[MAX_DASHLEN];
29
30     len = min( len, MAX_DASHLEN );
31     memcpy( dashes, style, len * sizeof(DWORD) );
32     for (i = pos = 0; i < len; i++)
33     {
34         if (!dashes[i])  /* get rid of 0 entry */
35         {
36             if (i < len - 1)
37             {
38                 i++;
39                 if (pos) dashes[pos - 1] += dashes[i];
40                 else dashes[len - 1] += dashes[i];
41             }
42             else if (pos)
43             {
44                 dashes[0] += dashes[pos - 1];
45                 pos--;
46             }
47         }
48         else dashes[pos++] = dashes[i];
49     }
50     for (i = 0; i < pos; i++) res[i] = min( dashes[i], 255 );
51     return pos;
52 }
53
54 /***********************************************************************
55  *           SelectPen   (X11DRV.@)
56  */
57 HPEN X11DRV_SelectPen( PHYSDEV dev, HPEN hpen, const struct brush_pattern *pattern )
58 {
59     static const char PEN_dash[]          = { 16,8 };
60     static const char PEN_dot[]           = { 4,4 };
61     static const char PEN_dashdot[]       = { 12,8,4,8 };
62     static const char PEN_dashdotdot[]    = { 12,4,4,4,4,4 };
63     static const char PEN_alternate[]     = { 1,1 };
64     static const char EXTPEN_dash[]       = { 3,1 };
65     static const char EXTPEN_dot[]        = { 1,1 };
66     static const char EXTPEN_dashdot[]    = { 3,1,1,1 };
67     static const char EXTPEN_dashdotdot[] = { 3,1,1,1,1,1 };
68     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
69     LOGPEN logpen;
70     int i;
71     EXTLOGPEN *elp = NULL;
72
73     if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
74     {
75         /* must be an extended pen */
76         INT size = GetObjectW( hpen, 0, NULL );
77
78         if (!size) return 0;
79
80         physDev->pen.ext = 1;
81         elp = HeapAlloc( GetProcessHeap(), 0, size );
82
83         GetObjectW( hpen, size, elp );
84         logpen.lopnStyle = elp->elpPenStyle;
85         logpen.lopnWidth.x = elp->elpWidth;
86         logpen.lopnWidth.y = 0;
87         logpen.lopnColor = elp->elpColor;
88     }
89     else
90         physDev->pen.ext = 0;
91
92     physDev->pen.style = logpen.lopnStyle & PS_STYLE_MASK;
93     physDev->pen.type = logpen.lopnStyle & PS_TYPE_MASK;
94     physDev->pen.endcap = logpen.lopnStyle & PS_ENDCAP_MASK;
95     physDev->pen.linejoin = logpen.lopnStyle & PS_JOIN_MASK;
96
97     physDev->pen.width = logpen.lopnWidth.x;
98     if ((logpen.lopnStyle & PS_GEOMETRIC) || (physDev->pen.width >= 1))
99     {
100         physDev->pen.width = X11DRV_XWStoDS( dev->hdc, physDev->pen.width );
101         if (physDev->pen.width < 0) physDev->pen.width = -physDev->pen.width;
102     }
103
104     if (physDev->pen.width == 1) physDev->pen.width = 0;  /* Faster */
105     if (hpen == GetStockObject( DC_PEN ))
106         logpen.lopnColor = GetDCPenColor( dev->hdc );
107     physDev->pen.pixel = X11DRV_PALETTE_ToPhysical( physDev, logpen.lopnColor );
108     switch(logpen.lopnStyle & PS_STYLE_MASK)
109     {
110       case PS_DASH:
111             physDev->pen.dash_len = sizeof(PEN_dash)/sizeof(*PEN_dash);
112             memcpy(physDev->pen.dashes, physDev->pen.ext ? EXTPEN_dash : PEN_dash,
113                    physDev->pen.dash_len);
114             break;
115       case PS_DOT:
116             physDev->pen.dash_len = sizeof(PEN_dot)/sizeof(*PEN_dot);
117             memcpy(physDev->pen.dashes, physDev->pen.ext ? EXTPEN_dot : PEN_dot,
118                    physDev->pen.dash_len);
119             break;
120       case PS_DASHDOT:
121             physDev->pen.dash_len = sizeof(PEN_dashdot)/sizeof(*PEN_dashdot);
122             memcpy(physDev->pen.dashes, physDev->pen.ext ? EXTPEN_dashdot : PEN_dashdot,
123                    physDev->pen.dash_len);
124             break;
125       case PS_DASHDOTDOT:
126             physDev->pen.dash_len = sizeof(PEN_dashdotdot)/sizeof(*PEN_dashdotdot);
127             memcpy(physDev->pen.dashes, physDev->pen.ext ? EXTPEN_dashdotdot : PEN_dashdotdot,
128                    physDev->pen.dash_len);
129             break;
130       case PS_ALTERNATE:
131             physDev->pen.dash_len = sizeof(PEN_alternate)/sizeof(*PEN_alternate);
132             memcpy(physDev->pen.dashes, PEN_alternate, physDev->pen.dash_len);
133             break;
134       case PS_USERSTYLE:
135             physDev->pen.dash_len = get_user_dashes( physDev->pen.dashes,
136                                                      elp->elpStyleEntry, elp->elpNumEntries );
137             break;
138       default:
139         physDev->pen.dash_len = 0;
140         break;
141     }
142     if(physDev->pen.ext && physDev->pen.dash_len && physDev->pen.width &&
143        (logpen.lopnStyle & PS_STYLE_MASK) != PS_USERSTYLE &&
144        (logpen.lopnStyle & PS_STYLE_MASK) != PS_ALTERNATE)
145         for(i = 0; i < physDev->pen.dash_len; i++)
146             physDev->pen.dashes[i] = min( physDev->pen.dashes[i] * physDev->pen.width, 255 );
147
148     HeapFree( GetProcessHeap(), 0, elp );
149
150     return hpen;
151 }
152
153
154 /***********************************************************************
155  *           SetDCPenColor (X11DRV.@)
156  */
157 COLORREF X11DRV_SetDCPenColor( PHYSDEV dev, COLORREF crColor )
158 {
159     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
160
161     if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
162         physDev->pen.pixel = X11DRV_PALETTE_ToPhysical( physDev, crColor );
163
164     return crColor;
165 }