don't use annoying and unneeded in C++ casts of NULL to "T *" in all other files...
[wxWidgets.git] / src / gtk1 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/colour.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/gdicmn.h"
17 #endif
18
19 #include "wx/gtk1/private.h"
20
21 #include <gdk/gdk.h>
22 #include <gdk/gdkx.h>
23 #include <gdk/gdkprivate.h>
24
25 //-----------------------------------------------------------------------------
26 // wxColour
27 //-----------------------------------------------------------------------------
28
29 class wxColourRefData : public wxGDIRefData
30 {
31 public:
32 wxColourRefData()
33 {
34 m_color.red = 0;
35 m_color.green = 0;
36 m_color.blue = 0;
37 m_color.pixel = 0;
38 m_colormap = NULL;
39 m_hasPixel = false;
40 }
41
42 wxColourRefData(const wxColourRefData& data)
43 {
44 m_color = data.m_color;
45 m_colormap = data.m_colormap;
46 m_hasPixel = data.m_hasPixel;
47 }
48
49 virtual ~wxColourRefData()
50 {
51 FreeColour();
52 }
53
54 bool operator == (const wxColourRefData& data) const
55 {
56 return (m_colormap == data.m_colormap &&
57 m_hasPixel == data.m_hasPixel &&
58 m_color.red == data.m_color.red &&
59 m_color.green == data.m_color.green &&
60 m_color.blue == data.m_color.blue &&
61 m_color.pixel == data.m_color.pixel);
62 }
63
64 void FreeColour();
65 void AllocColour( GdkColormap* cmap );
66
67 GdkColor m_color;
68 GdkColormap *m_colormap;
69 bool m_hasPixel;
70
71 friend class wxColour;
72
73 // reference counter for systems with <= 8-Bit display
74 static gushort colMapAllocCounter[ 256 ];
75 };
76
77 gushort wxColourRefData::colMapAllocCounter[ 256 ] =
78 {
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
92 };
93
94 void wxColourRefData::FreeColour()
95 {
96 if (m_colormap)
97 {
98 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
99 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
100 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
101 {
102 int idx = m_color.pixel;
103 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
104
105 if (colMapAllocCounter[ idx ] == 0)
106 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
107 }
108 }
109 }
110
111 void wxColourRefData::AllocColour( GdkColormap *cmap )
112 {
113 if (m_hasPixel && (m_colormap == cmap))
114 return;
115
116 FreeColour();
117
118 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
119 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
120 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
121 {
122 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
123 int idx = m_color.pixel;
124 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
125 }
126 else
127 {
128 m_hasPixel = gdk_color_alloc( cmap, &m_color );
129 }
130 m_colormap = cmap;
131 }
132
133 //-----------------------------------------------------------------------------
134
135 #define M_COLDATA ((wxColourRefData *)m_refData)
136
137 // GDK's values are in 0..65535 range, our are in 0..255
138 #define SHIFT 8
139
140 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
141
142 wxColour::~wxColour()
143 {
144 }
145
146 bool wxColour::operator == ( const wxColour& col ) const
147 {
148 if (m_refData == col.m_refData)
149 return true;
150
151 if (!m_refData || !col.m_refData)
152 return false;
153
154 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
155 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
156 return own->red == other->red &&
157 own->blue == other->blue &&
158 own->green == other->green;
159 }
160
161 wxGDIRefData *wxColour::CreateGDIRefData() const
162 {
163 return new wxColourRefData;
164 }
165
166 wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
167 {
168 return new wxColourRefData(*(wxColourRefData *)data);
169 }
170
171 void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
172 unsigned char WXUNUSED(alpha))
173 {
174 AllocExclusive();
175
176 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
177 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
178 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
179 M_COLDATA->m_color.pixel = 0;
180
181 M_COLDATA->m_colormap = NULL;
182 M_COLDATA->m_hasPixel = false;
183 }
184
185 unsigned char wxColour::Red() const
186 {
187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
188
189 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
190 }
191
192 unsigned char wxColour::Green() const
193 {
194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
195
196 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
197 }
198
199 unsigned char wxColour::Blue() const
200 {
201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
202
203 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
204 }
205
206 void wxColour::CalcPixel( GdkColormap *cmap )
207 {
208 if (!Ok()) return;
209
210 M_COLDATA->AllocColour( cmap );
211 }
212
213 int wxColour::GetPixel() const
214 {
215 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
216
217 return M_COLDATA->m_color.pixel;
218 }
219
220 GdkColor *wxColour::GetColor() const
221 {
222 wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
223
224 return &M_COLDATA->m_color;
225 }
226
227 bool wxColour::FromString(const wxString& str)
228 {
229 GdkColor colGDK;
230 if ( gdk_color_parse( wxGTK_CONV(str), &colGDK ) )
231 {
232 UnRef();
233
234 m_refData = new wxColourRefData;
235 M_COLDATA->m_color = colGDK;
236 return true;
237 }
238
239 return wxColourBase::FromString(str);
240 }