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