]> git.saurik.com Git - wxWidgets.git/blame - src/msw/pen.cpp
Added generic drag image source file
[wxWidgets.git] / src / msw / pen.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
bbd41262 2// Name: msw/pen.cpp
2bda0e17
KB
3// Purpose: wxPen
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "pen.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include <stdio.h>
25#include "wx/setup.h"
26#include "wx/list.h"
27#include "wx/utils.h"
28#include "wx/app.h"
29#include "wx/pen.h"
30#endif
31
32#include "wx/msw/private.h"
2bda0e17 33
2bda0e17 34IMPLEMENT_DYNAMIC_CLASS(wxPen, wxGDIObject)
2bda0e17 35
e4a81a2e 36wxPenRefData::wxPenRefData()
2bda0e17 37{
2bda0e17
KB
38 m_style = wxSOLID;
39 m_width = 1;
40 m_join = wxJOIN_ROUND ;
41 m_cap = wxCAP_ROUND ;
42 m_nbDash = 0 ;
edd97174 43 m_dash = (wxDash*)NULL;
2bda0e17
KB
44 m_hPen = 0;
45}
46
b823f5a1
JS
47wxPenRefData::wxPenRefData(const wxPenRefData& data)
48{
49 m_style = data.m_style;
50 m_width = data.m_width;
51 m_join = data.m_join;
52 m_cap = data.m_cap;
53 m_nbDash = data.m_nbDash;
54 m_dash = data.m_dash;
55 m_colour = data.m_colour;
56 m_hPen = 0;
57}
58
e4a81a2e 59wxPenRefData::~wxPenRefData()
2bda0e17 60{
bbd41262
VZ
61 if ( m_hPen )
62 ::DeleteObject((HPEN) m_hPen);
2bda0e17
KB
63}
64
65// Pens
66
e4a81a2e 67wxPen::wxPen()
2bda0e17 68{
2bda0e17
KB
69}
70
71wxPen::~wxPen()
72{
2bda0e17
KB
73}
74
75// Should implement Create
debe6624 76wxPen::wxPen(const wxColour& col, int Width, int Style)
2bda0e17
KB
77{
78 m_refData = new wxPenRefData;
79
80 M_PENDATA->m_colour = col;
81// M_PENDATA->m_stipple = NULL;
82 M_PENDATA->m_width = Width;
83 M_PENDATA->m_style = Style;
84 M_PENDATA->m_join = wxJOIN_ROUND ;
85 M_PENDATA->m_cap = wxCAP_ROUND ;
86 M_PENDATA->m_nbDash = 0 ;
edd97174 87 M_PENDATA->m_dash = (wxDash*)NULL;
2bda0e17
KB
88 M_PENDATA->m_hPen = 0 ;
89
90#ifndef __WIN32__
91 // In Windows, only a pen of width = 1 can be dotted or dashed!
92 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
93 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
94 (Style == wxUSER_DASH))
95 M_PENDATA->m_width = 1;
96#else
97/***
98 DWORD vers = GetVersion() ;
99 WORD high = HIWORD(vers) ; // high bit=0 for NT, 1 for Win32s
100 // Win32s doesn't support wide dashed pens
101
102 if ((high&0x8000)!=0)
103***/
104 if (wxGetOsVersion()==wxWIN32S)
105 {
106 // In Windows, only a pen of width = 1 can be dotted or dashed!
107 if ((Style == wxDOT) || (Style == wxLONG_DASH) ||
108 (Style == wxSHORT_DASH) || (Style == wxDOT_DASH) ||
109 (Style == wxUSER_DASH))
110 M_PENDATA->m_width = 1;
111 }
bbd41262 112#endif
2bda0e17
KB
113 RealizeResource();
114
2bda0e17
KB
115}
116
debe6624 117wxPen::wxPen(const wxBitmap& stipple, int Width)
2bda0e17 118{
c45a644e 119 m_refData = new wxPenRefData;
2bda0e17
KB
120
121// M_PENDATA->m_colour = col;
c45a644e
RR
122 M_PENDATA->m_stipple = stipple;
123 M_PENDATA->m_width = Width;
124 M_PENDATA->m_style = wxSTIPPLE;
125 M_PENDATA->m_join = wxJOIN_ROUND ;
126 M_PENDATA->m_cap = wxCAP_ROUND ;
127 M_PENDATA->m_nbDash = 0 ;
edd97174 128 M_PENDATA->m_dash = (wxDash*)NULL;
c45a644e 129 M_PENDATA->m_hPen = 0 ;
2bda0e17 130
c45a644e 131 RealizeResource();
2bda0e17 132
2bda0e17
KB
133}
134
e4a81a2e 135bool wxPen::RealizeResource()
2bda0e17 136{
c45a644e
RR
137 if (M_PENDATA && (M_PENDATA->m_hPen == 0))
138 {
139 if (M_PENDATA->m_style==wxTRANSPARENT)
140 {
141 M_PENDATA->m_hPen = (WXHPEN) ::GetStockObject(NULL_PEN);
142 return TRUE;
143 }
144
145 COLORREF ms_colour = 0;
146 ms_colour = M_PENDATA->m_colour.GetPixel();
147
148 // Join style, Cap style, Pen Stippling only on Win32.
149 // Currently no time to find equivalent on Win3.1, sorry
150 // [if such equiv exist!!]
4676948b 151#if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
c45a644e
RR
152 if (M_PENDATA->m_join==wxJOIN_ROUND &&
153 M_PENDATA->m_cap==wxCAP_ROUND &&
154 M_PENDATA->m_style!=wxUSER_DASH &&
155 M_PENDATA->m_style!=wxSTIPPLE &&
bbd41262 156 M_PENDATA->m_width <= 1)
c45a644e 157 {
bbd41262
VZ
158 M_PENDATA->m_hPen =
159 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
160 M_PENDATA->m_width,
161 ms_colour );
c45a644e
RR
162 }
163 else
164 {
bbd41262 165 DWORD ms_style = PS_GEOMETRIC | wx2msPenStyle(M_PENDATA->m_style);
c45a644e 166
bbd41262 167 switch(M_PENDATA->m_join)
c45a644e
RR
168 {
169 case wxJOIN_BEVEL: ms_style |= PS_JOIN_BEVEL; break;
170 case wxJOIN_MITER: ms_style |= PS_JOIN_MITER; break;
171 default:
172 case wxJOIN_ROUND: ms_style |= PS_JOIN_ROUND; break;
173 }
174
175 switch(M_PENDATA->m_cap)
176 {
177 case wxCAP_PROJECTING: ms_style |= PS_ENDCAP_SQUARE; break;
178 case wxCAP_BUTT: ms_style |= PS_ENDCAP_FLAT; break;
179 default:
180 case wxCAP_ROUND: ms_style |= PS_ENDCAP_ROUND; break;
181 }
182
bbd41262 183 LOGBRUSH logb;
c45a644e 184
bbd41262 185 switch(M_PENDATA->m_style)
c45a644e
RR
186 {
187 case wxSTIPPLE:
188 logb.lbStyle = BS_PATTERN ;
189 if (M_PENDATA->m_stipple.Ok())
190 logb.lbHatch = (LONG)M_PENDATA->m_stipple.GetHBITMAP();
191 else
192 logb.lbHatch = (LONG)0;
193 break;
194 case wxBDIAGONAL_HATCH:
bbd41262
VZ
195 logb.lbStyle = BS_HATCHED;
196 logb.lbHatch = HS_BDIAGONAL;
197 break;
198 case wxCROSSDIAG_HATCH:
199 logb.lbStyle = BS_HATCHED;
200 logb.lbHatch = HS_DIAGCROSS;
201 break;
202 case wxFDIAGONAL_HATCH:
203 logb.lbStyle = BS_HATCHED;
204 logb.lbHatch = HS_FDIAGONAL;
205 break;
206 case wxCROSS_HATCH:
207 logb.lbStyle = BS_HATCHED;
208 logb.lbHatch = HS_CROSS;
209 break;
210 case wxHORIZONTAL_HATCH:
211 logb.lbStyle = BS_HATCHED;
212 logb.lbHatch = HS_HORIZONTAL;
213 break;
214 case wxVERTICAL_HATCH:
215 logb.lbStyle = BS_HATCHED;
216 logb.lbHatch = HS_VERTICAL;
217 break;
218 default:
c45a644e 219 logb.lbStyle = BS_SOLID;
61ba49f2 220#ifdef __WXDEBUG__
bbd41262 221 // this should be unnecessary (it's unused) but suppresses the Purigy
c45a644e 222 // messages about uninitialized memory read
bbd41262 223 logb.lbHatch = 0;
61ba49f2 224#endif
bbd41262
VZ
225 break;
226 }
c45a644e 227
bbd41262 228 logb.lbColor = ms_colour;
c45a644e 229
bbd41262 230 wxMSWDash *real_dash;
c45a644e 231 if (M_PENDATA->m_style==wxUSER_DASH && M_PENDATA->m_nbDash && M_PENDATA->m_dash)
bbd41262
VZ
232 {
233 real_dash = new wxMSWDash[M_PENDATA->m_nbDash];
e2a5251d 234 int rw = M_PENDATA->m_width > 1 ? M_PENDATA->m_width : 1;
d275c7eb 235 for ( int i = 0; i < M_PENDATA->m_nbDash; i++ )
e2a5251d 236 real_dash[i] = M_PENDATA->m_dash[i] * rw;
c45a644e 237 }
bbd41262 238 else
c45a644e 239 {
bbd41262 240 real_dash = (wxMSWDash*)NULL;
c45a644e
RR
241 }
242
243 // Win32s doesn't have ExtCreatePen function...
bbd41262 244 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
c45a644e 245 {
bbd41262
VZ
246 M_PENDATA->m_hPen =
247 (WXHPEN) ExtCreatePen( ms_style,
248 M_PENDATA->m_width,
249 &logb,
250 M_PENDATA->m_style == wxUSER_DASH
251 ? M_PENDATA->m_nbDash
252 : 0,
253 (LPDWORD)real_dash );
254 }
255 else
256 {
257 M_PENDATA->m_hPen =
258 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
259 M_PENDATA->m_width,
260 ms_colour );
c45a644e
RR
261 }
262
263 if (real_dash)
264 delete [] real_dash;
265 }
2bda0e17 266#else
c45a644e 267 M_PENDATA->m_hPen =
bbd41262
VZ
268 (WXHPEN) CreatePen( wx2msPenStyle(M_PENDATA->m_style),
269 M_PENDATA->m_width,
270 ms_colour );
2bda0e17 271#endif
b2aef89b 272#ifdef WXDEBUG_CREATE
c45a644e
RR
273 if (M_PENDATA->m_hPen==0)
274 wxError("Cannot create pen","Internal error") ;
2bda0e17 275#endif
c45a644e
RR
276 return TRUE;
277 }
278 return FALSE;
2bda0e17
KB
279}
280
2b5f62a0 281WXHANDLE wxPen::GetResourceHandle() const
2bda0e17 282{
bbd41262
VZ
283 if ( !M_PENDATA )
284 return 0;
285 else
286 return (WXHANDLE)M_PENDATA->m_hPen;
2bda0e17
KB
287}
288
33ac7e6f 289bool wxPen::FreeResource(bool WXUNUSED(force))
2bda0e17
KB
290{
291 if (M_PENDATA && (M_PENDATA->m_hPen != 0))
292 {
293 DeleteObject((HPEN) M_PENDATA->m_hPen);
294 M_PENDATA->m_hPen = 0;
295 return TRUE;
296 }
297 else return FALSE;
298}
299
e4a81a2e 300bool wxPen::IsFree() const
2bda0e17 301{
b823f5a1 302 return (M_PENDATA && M_PENDATA->m_hPen == 0);
2bda0e17 303}
2bda0e17 304
b823f5a1 305void wxPen::Unshare()
2bda0e17 306{
bbd41262
VZ
307 // Don't change shared data
308 if (!m_refData)
b823f5a1 309 {
bbd41262
VZ
310 m_refData = new wxPenRefData();
311 }
b823f5a1
JS
312 else
313 {
bbd41262
VZ
314 wxPenRefData* ref = new wxPenRefData(*(wxPenRefData*)m_refData);
315 UnRef();
316 m_refData = ref;
317 }
2bda0e17
KB
318}
319
320void wxPen::SetColour(const wxColour& col)
321{
b823f5a1 322 Unshare();
2bda0e17 323
b823f5a1 324 M_PENDATA->m_colour = col;
bbd41262 325
2bda0e17
KB
326 RealizeResource();
327}
328
e4a81a2e 329void wxPen::SetColour(unsigned char r, unsigned char g, unsigned char b)
2bda0e17 330{
b823f5a1 331 Unshare();
2bda0e17 332
b823f5a1 333 M_PENDATA->m_colour.Set(r, g, b);
bbd41262 334
2bda0e17
KB
335 RealizeResource();
336}
337
debe6624 338void wxPen::SetWidth(int Width)
2bda0e17 339{
b823f5a1 340 Unshare();
2bda0e17 341
b823f5a1 342 M_PENDATA->m_width = Width;
2bda0e17 343
2bda0e17
KB
344 RealizeResource();
345}
346
debe6624 347void wxPen::SetStyle(int Style)
2bda0e17 348{
b823f5a1 349 Unshare();
2bda0e17 350
b823f5a1 351 M_PENDATA->m_style = Style;
2bda0e17 352
2bda0e17
KB
353 RealizeResource();
354}
355
356void wxPen::SetStipple(const wxBitmap& Stipple)
357{
b823f5a1 358 Unshare();
2bda0e17 359
b823f5a1
JS
360 M_PENDATA->m_stipple = Stipple;
361 M_PENDATA->m_style = wxSTIPPLE;
bbd41262 362
2bda0e17
KB
363 RealizeResource();
364}
365
debe6624 366void wxPen::SetDashes(int nb_dashes, const wxDash *Dash)
2bda0e17 367{
b823f5a1 368 Unshare();
2bda0e17 369
b823f5a1 370 M_PENDATA->m_nbDash = nb_dashes;
edd97174 371 M_PENDATA->m_dash = (wxDash *)Dash;
bbd41262 372
2bda0e17
KB
373 RealizeResource();
374}
375
debe6624 376void wxPen::SetJoin(int Join)
2bda0e17 377{
b823f5a1 378 Unshare();
2bda0e17 379
b823f5a1 380 M_PENDATA->m_join = Join;
2bda0e17 381
2bda0e17
KB
382 RealizeResource();
383}
384
debe6624 385void wxPen::SetCap(int Cap)
2bda0e17 386{
b823f5a1 387 Unshare();
2bda0e17 388
b823f5a1 389 M_PENDATA->m_cap = Cap;
2bda0e17 390
2bda0e17
KB
391 RealizeResource();
392}
393
394int wx2msPenStyle(int wx_style)
395{
c45a644e
RR
396 int cstyle;
397 switch (wx_style)
bbd41262 398 {
4676948b 399#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
c45a644e
RR
400 case wxDOT:
401 cstyle = PS_DOT;
bbd41262 402 break;
c45a644e
RR
403
404 case wxDOT_DASH:
bbd41262
VZ
405 cstyle = PS_DASHDOT;
406 break;
c45a644e
RR
407
408 case wxSHORT_DASH:
409 case wxLONG_DASH:
410 cstyle = PS_DASH;
bbd41262 411 break;
c45a644e
RR
412
413 case wxTRANSPARENT:
414 cstyle = PS_NULL;
bbd41262 415 break;
04ef50df 416#endif
c45a644e
RR
417
418 case wxUSER_DASH:
4676948b 419#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
2bda0e17 420#ifdef __WIN32__
c45a644e 421 // Win32s doesn't have PS_USERSTYLE
bbd41262
VZ
422 if (wxGetOsVersion()==wxWINDOWS_NT || wxGetOsVersion()==wxWIN95)
423 cstyle = PS_USERSTYLE;
424 else
c45a644e 425 cstyle = PS_DOT; // We must make a choice... This is mine!
2bda0e17 426#else
c45a644e 427 cstyle = PS_DASH;
04ef50df 428#endif
2bda0e17 429#endif
c45a644e
RR
430 break;
431 case wxSOLID:
432 default:
433 cstyle = PS_SOLID;
434 break;
435 }
436 return cstyle;
2bda0e17
KB
437}
438