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