]> git.saurik.com Git - wxWidgets.git/blame - src/msw/brush.cpp
popupcmn is needed by wxGTK too, why was it excluded?
[wxWidgets.git] / src / msw / brush.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: brush.cpp
3// Purpose: wxBrush
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 "brush.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/brush.h"
30#endif
31
32#include "wx/msw/private.h"
33
34#include "assert.h"
35
2bda0e17 36IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
2bda0e17
KB
37
38wxBrushRefData::wxBrushRefData(void)
39{
40 m_style = wxSOLID;
b823f5a1
JS
41 m_hBrush = 0;
42}
43
44wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
45{
46 m_style = data.m_style;
47 m_stipple = data.m_stipple;
48 m_colour = data.m_colour;
2bda0e17
KB
49 m_hBrush = 0;
50}
51
52wxBrushRefData::~wxBrushRefData(void)
53{
54 if ( m_hBrush )
55 ::DeleteObject((HBRUSH) m_hBrush);
56}
57
58// Brushes
59wxBrush::wxBrush(void)
60{
61 if ( wxTheBrushList )
62 wxTheBrushList->AddBrush(this);
63}
64
65wxBrush::~wxBrush()
66{
67 if (wxTheBrushList)
68 wxTheBrushList->RemoveBrush(this);
69}
70
debe6624 71wxBrush::wxBrush(const wxColour& col, int Style)
2bda0e17
KB
72{
73 m_refData = new wxBrushRefData;
74
75 M_BRUSHDATA->m_colour = col;
76 M_BRUSHDATA->m_style = Style;
77 M_BRUSHDATA->m_hBrush = 0;
78
79 RealizeResource();
80
81 if ( wxTheBrushList )
82 wxTheBrushList->AddBrush(this);
83}
84
2bda0e17
KB
85wxBrush::wxBrush(const wxBitmap& stipple)
86{
87 m_refData = new wxBrushRefData;
88
2bda0e17 89 M_BRUSHDATA->m_stipple = stipple;
de2d2cdc
VZ
90 if (M_BRUSHDATA->m_stipple.GetMask())
91 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
92 else
93 M_BRUSHDATA->m_style = wxSTIPPLE;
94
2bda0e17
KB
95 M_BRUSHDATA->m_hBrush = 0;
96
97 RealizeResource();
98
99 if ( wxTheBrushList )
100 wxTheBrushList->AddBrush(this);
101}
102
de2d2cdc 103bool wxBrush::RealizeResource(void)
2bda0e17
KB
104{
105 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0))
106 {
107 if (M_BRUSHDATA->m_style==wxTRANSPARENT)
108 {
109 M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH);
110 return TRUE;
111 }
112 COLORREF ms_colour = 0 ;
113
114 ms_colour = M_BRUSHDATA->m_colour.GetPixel() ;
115
116 switch (M_BRUSHDATA->m_style)
117 {
118/****
119 // Don't reset cbrush, wxTRANSPARENT is handled by wxBrush::SelectBrush()
120 // this could save (many) time if frequently switching from
121 // wxSOLID to wxTRANSPARENT, because Create... is not always called!!
122 //
123 // NB August 95: now create and select a Null brush instead.
124 // This could be optimized as above.
125 case wxTRANSPARENT:
126 M_BRUSHDATA->m_hBrush = NULL; // Must always select a suitable background brush
127 // - could choose white always for a quick solution
128 break;
129***/
04ef50df 130#ifndef __WXMICROWIN__
2bda0e17
KB
131 case wxBDIAGONAL_HATCH:
132 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_BDIAGONAL,ms_colour) ;
133 break ;
134 case wxCROSSDIAG_HATCH:
135 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_DIAGCROSS,ms_colour) ;
136 break ;
137 case wxFDIAGONAL_HATCH:
138 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_FDIAGONAL,ms_colour) ;
139 break ;
140 case wxCROSS_HATCH:
141 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_CROSS,ms_colour) ;
142 break ;
143 case wxHORIZONTAL_HATCH:
144 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_HORIZONTAL,ms_colour) ;
145 break ;
146 case wxVERTICAL_HATCH:
147 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_VERTICAL,ms_colour) ;
148 break ;
149 case wxSTIPPLE:
150 if (M_BRUSHDATA->m_stipple.Ok())
151 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetHBITMAP()) ;
152 else
153 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
154 break ;
de2d2cdc
VZ
155 case wxSTIPPLE_MASK_OPAQUE:
156 if (M_BRUSHDATA->m_stipple.Ok() && M_BRUSHDATA->m_stipple.GetMask())
157 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetMask()->GetMaskBitmap());
158 else
159 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
160 break ;
04ef50df 161#endif
2bda0e17
KB
162 case wxSOLID:
163 default:
164 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
165 break;
166 }
b2aef89b 167#ifdef WXDEBUG_CREATE
2bda0e17
KB
168 if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
169#endif
170 return TRUE;
171 }
172 else
173 return FALSE;
174}
175
176WXHANDLE wxBrush::GetResourceHandle(void)
177{
178 return (WXHANDLE) M_BRUSHDATA->m_hBrush;
179}
180
57c208c5 181bool wxBrush::FreeResource(bool WXUNUSED(force))
2bda0e17
KB
182{
183 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
184 {
185 DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush);
186 M_BRUSHDATA->m_hBrush = 0;
187 return TRUE;
188 }
189 else return FALSE;
190}
191
e90babdf 192bool wxBrush::IsFree() const
2bda0e17 193{
b823f5a1 194 return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
2bda0e17
KB
195}
196
b823f5a1 197void wxBrush::Unshare()
2bda0e17 198{
b823f5a1
JS
199 // Don't change shared data
200 if (!m_refData)
201 {
202 m_refData = new wxBrushRefData();
203 }
204 else
205 {
206 wxBrushRefData* ref = new wxBrushRefData(*(wxBrushRefData*)m_refData);
207 UnRef();
208 m_refData = ref;
209 }
2bda0e17 210}
2bda0e17 211
2bda0e17
KB
212
213void wxBrush::SetColour(const wxColour& col)
214{
b823f5a1 215 Unshare();
2bda0e17 216
b823f5a1 217 M_BRUSHDATA->m_colour = col;
2bda0e17 218
2bda0e17
KB
219 RealizeResource();
220}
221
e4a81a2e 222void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
2bda0e17 223{
b823f5a1 224 Unshare();
2bda0e17 225
b823f5a1 226 M_BRUSHDATA->m_colour.Set(r, g, b);
2bda0e17 227
2bda0e17
KB
228 RealizeResource();
229}
230
debe6624 231void wxBrush::SetStyle(int Style)
2bda0e17 232{
b823f5a1 233 Unshare();
2bda0e17 234
b823f5a1 235 M_BRUSHDATA->m_style = Style;
2bda0e17 236
2bda0e17
KB
237 RealizeResource();
238}
239
240void wxBrush::SetStipple(const wxBitmap& Stipple)
241{
b823f5a1 242 Unshare();
2bda0e17 243
b823f5a1 244 M_BRUSHDATA->m_stipple = Stipple;
de2d2cdc
VZ
245 if (M_BRUSHDATA->m_stipple.GetMask())
246 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
247 else
248 M_BRUSHDATA->m_style = wxSTIPPLE;
2bda0e17 249
2bda0e17
KB
250 RealizeResource();
251}
252
253