]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
added a couple of base classes
[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 ~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::InitWith( unsigned char red, unsigned char green, unsigned char blue )
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 = (GdkColormap*) 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(), (GdkColor *) NULL, wxT("invalid colour") );
223
224 return &M_COLDATA->m_color;
225 }
226
227 bool wxColour::FromString(const wxChar *str)
228 {
229 GdkColor colGDK;
230 if ( gdk_color_parse( 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 }