]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
cleanup
[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 wxObjectRefData
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 = (GdkColormap *) NULL;
39 m_hasPixel = false;
40 }
41
42 wxColourRefData(const wxColourRefData& data)
43 : wxObjectRefData()
44 {
45 m_color = data.m_color;
46 m_colormap = data.m_colormap;
47 m_hasPixel = data.m_hasPixel;
48 }
49
50 virtual ~wxColourRefData()
51 {
52 FreeColour();
53 }
54
55 bool operator == (const wxColourRefData& data) const
56 {
57 return (m_colormap == data.m_colormap &&
58 m_hasPixel == data.m_hasPixel &&
59 m_color.red == data.m_color.red &&
60 m_color.green == data.m_color.green &&
61 m_color.blue == data.m_color.blue &&
62 m_color.pixel == data.m_color.pixel);
63 }
64
65 void FreeColour();
66 void AllocColour( GdkColormap* cmap );
67
68 GdkColor m_color;
69 GdkColormap *m_colormap;
70 bool m_hasPixel;
71
72 friend class wxColour;
73
74 // reference counter for systems with <= 8-Bit display
75 static gushort colMapAllocCounter[ 256 ];
76 };
77
78 gushort wxColourRefData::colMapAllocCounter[ 256 ] =
79 {
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, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
93 };
94
95 void wxColourRefData::FreeColour()
96 {
97 if (m_colormap)
98 {
99 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
100 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
101 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
102 {
103 int idx = m_color.pixel;
104 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
105
106 if (colMapAllocCounter[ idx ] == 0)
107 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
108 }
109 }
110 }
111
112 void wxColourRefData::AllocColour( GdkColormap *cmap )
113 {
114 if (m_hasPixel && (m_colormap == cmap))
115 return;
116
117 FreeColour();
118
119 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
120 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
121 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
122 {
123 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
124 int idx = m_color.pixel;
125 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
126 }
127 else
128 {
129 m_hasPixel = gdk_color_alloc( cmap, &m_color );
130 }
131 m_colormap = cmap;
132 }
133
134 //-----------------------------------------------------------------------------
135
136 #define M_COLDATA ((wxColourRefData *)m_refData)
137
138 // GDK's values are in 0..65535 range, our are in 0..255
139 #define SHIFT 8
140
141 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
142
143 wxColour::~wxColour()
144 {
145 }
146
147 bool wxColour::operator == ( const wxColour& col ) const
148 {
149 if (m_refData == col.m_refData)
150 return true;
151
152 if (!m_refData || !col.m_refData)
153 return false;
154
155 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
156 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
157 return own->red == other->red &&
158 own->blue == other->blue &&
159 own->green == other->green;
160 }
161
162 wxObjectRefData *wxColour::CreateRefData() const
163 {
164 return new wxColourRefData;
165 }
166
167 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
168 {
169 return new wxColourRefData(*(wxColourRefData *)data);
170 }
171
172 void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
173 unsigned char WXUNUSED(alpha))
174 {
175 AllocExclusive();
176
177 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
178 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
179 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
180 M_COLDATA->m_color.pixel = 0;
181
182 M_COLDATA->m_colormap = (GdkColormap*) NULL;
183 M_COLDATA->m_hasPixel = false;
184 }
185
186 unsigned char wxColour::Red() const
187 {
188 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
189
190 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
191 }
192
193 unsigned char wxColour::Green() const
194 {
195 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
196
197 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
198 }
199
200 unsigned char wxColour::Blue() const
201 {
202 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
203
204 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
205 }
206
207 void wxColour::CalcPixel( GdkColormap *cmap )
208 {
209 if (!Ok()) return;
210
211 M_COLDATA->AllocColour( cmap );
212 }
213
214 int wxColour::GetPixel() const
215 {
216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
217
218 return M_COLDATA->m_color.pixel;
219 }
220
221 GdkColor *wxColour::GetColor() const
222 {
223 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
224
225 return &M_COLDATA->m_color;
226 }
227
228 bool wxColour::FromString(const wxString& str)
229 {
230 GdkColor colGDK;
231 if ( gdk_color_parse( wxGTK_CONV(str), &colGDK ) )
232 {
233 UnRef();
234
235 m_refData = new wxColourRefData;
236 M_COLDATA->m_color = colGDK;
237 return true;
238 }
239
240 return wxColourBase::FromString(str);
241 }