Merge in from trunk r67662 to r64801
[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 wxColour::~wxColour()
141 {
142 }
143
144 bool wxColour::operator == ( const wxColour& col ) const
145 {
146 if (m_refData == col.m_refData)
147 return true;
148
149 if (!m_refData || !col.m_refData)
150 return false;
151
152 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
153 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
154 return own->red == other->red &&
155 own->blue == other->blue &&
156 own->green == other->green;
157 }
158
159 wxGDIRefData *wxColour::CreateGDIRefData() const
160 {
161 return new wxColourRefData;
162 }
163
164 wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
165 {
166 return new wxColourRefData(*(wxColourRefData *)data);
167 }
168
169 void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
170 unsigned char WXUNUSED(alpha))
171 {
172 AllocExclusive();
173
174 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
175 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
176 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
177 M_COLDATA->m_color.pixel = 0;
178
179 M_COLDATA->m_colormap = NULL;
180 M_COLDATA->m_hasPixel = false;
181 }
182
183 unsigned char wxColour::Red() const
184 {
185 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
186
187 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
188 }
189
190 unsigned char wxColour::Green() const
191 {
192 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
193
194 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
195 }
196
197 unsigned char wxColour::Blue() const
198 {
199 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
200
201 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
202 }
203
204 void wxColour::CalcPixel( GdkColormap *cmap )
205 {
206 if (!IsOk()) return;
207
208 M_COLDATA->AllocColour( cmap );
209 }
210
211 int wxColour::GetPixel() const
212 {
213 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
214
215 return M_COLDATA->m_color.pixel;
216 }
217
218 GdkColor *wxColour::GetColor() const
219 {
220 wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") );
221
222 return &M_COLDATA->m_color;
223 }
224
225 bool wxColour::FromString(const wxString& str)
226 {
227 GdkColor colGDK;
228 if ( gdk_color_parse( wxGTK_CONV(str), &colGDK ) )
229 {
230 UnRef();
231
232 m_refData = new wxColourRefData;
233 M_COLDATA->m_color = colGDK;
234 return true;
235 }
236
237 return wxColourBase::FromString(str);
238 }