]>
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 | |
dd05139a WS |
15 | #ifndef WX_PRECOMP |
16 | #include "wx/gdicmn.h" | |
17 | #endif | |
18 | ||
3cbab641 | 19 | #include "wx/gtk1/private.h" |
c801d85f | 20 | |
071a2d78 | 21 | #include <gdk/gdk.h> |
307fc8d5 | 22 | #include <gdk/gdkx.h> |
071a2d78 | 23 | #include <gdk/gdkprivate.h> |
c801d85f KB |
24 | |
25 | //----------------------------------------------------------------------------- | |
26 | // wxColour | |
27 | //----------------------------------------------------------------------------- | |
28 | ||
8f884a0d | 29 | class wxColourRefData : public wxGDIRefData |
c801d85f | 30 | { |
47c93b63 | 31 | public: |
c89f5c02 RR |
32 | wxColourRefData() |
33 | { | |
34 | m_color.red = 0; | |
35 | m_color.green = 0; | |
36 | m_color.blue = 0; | |
37 | m_color.pixel = 0; | |
d3b9f782 | 38 | m_colormap = NULL; |
aad6765c | 39 | m_hasPixel = false; |
c89f5c02 | 40 | } |
4e6b8309 | 41 | |
dbb846c6 VZ |
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 | ||
d3c7fc99 | 49 | virtual ~wxColourRefData() |
c89f5c02 RR |
50 | { |
51 | FreeColour(); | |
52 | } | |
15248e43 | 53 | |
c89f5c02 RR |
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 | } | |
4e6b8309 | 63 | |
68dda785 | 64 | void FreeColour(); |
d13fd3e4 | 65 | void AllocColour( GdkColormap* cmap ); |
8bbe427f | 66 | |
c801d85f KB |
67 | GdkColor m_color; |
68 | GdkColormap *m_colormap; | |
69 | bool m_hasPixel; | |
8bbe427f | 70 | |
f6bcfd97 | 71 | friend class wxColour; |
d13fd3e4 VZ |
72 | |
73 | // reference counter for systems with <= 8-Bit display | |
74 | static gushort colMapAllocCounter[ 256 ]; | |
75 | }; | |
76 | ||
4e6b8309 VZ |
77 | gushort wxColourRefData::colMapAllocCounter[ 256 ] = |
78 | { | |
d13fd3e4 VZ |
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 | |
c801d85f KB |
92 | }; |
93 | ||
68dda785 | 94 | void wxColourRefData::FreeColour() |
c801d85f | 95 | { |
47c93b63 RR |
96 | if (m_colormap) |
97 | { | |
47c93b63 RR |
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)) | |
47c93b63 | 101 | { |
d13fd3e4 VZ |
102 | int idx = m_color.pixel; |
103 | colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1; | |
4e6b8309 | 104 | |
d13fd3e4 | 105 | if (colMapAllocCounter[ idx ] == 0) |
0a164d4c | 106 | gdk_colormap_free_colors( m_colormap, &m_color, 1 ); |
47c93b63 RR |
107 | } |
108 | } | |
ff7b1510 | 109 | } |
c801d85f | 110 | |
d13fd3e4 VZ |
111 | void wxColourRefData::AllocColour( GdkColormap *cmap ) |
112 | { | |
113 | if (m_hasPixel && (m_colormap == cmap)) | |
114 | return; | |
115 | ||
116 | FreeColour(); | |
4e6b8309 | 117 | |
7e52dfd2 MW |
118 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; |
119 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
120 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
d13fd3e4 VZ |
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 | ||
c801d85f KB |
133 | //----------------------------------------------------------------------------- |
134 | ||
135 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
136 | ||
3b9faf4c VS |
137 | // GDK's values are in 0..65535 range, our are in 0..255 |
138 | #define SHIFT 8 | |
c801d85f | 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 | |
8f884a0d | 159 | wxGDIRefData *wxColour::CreateGDIRefData() const |
c89f5c02 RR |
160 | { |
161 | return new wxColourRefData; | |
162 | } | |
163 | ||
8f884a0d | 164 | wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const |
8bbe427f | 165 | { |
c89f5c02 | 166 | return new wxColourRefData(*(wxColourRefData *)data); |
ff7b1510 | 167 | } |
c801d85f | 168 | |
aea95b1c VZ |
169 | void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue, |
170 | unsigned char WXUNUSED(alpha)) | |
c801d85f | 171 | { |
c89f5c02 | 172 | AllocExclusive(); |
4e6b8309 | 173 | |
1ecc4d80 RR |
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; | |
4e6b8309 | 178 | |
d3b9f782 | 179 | M_COLDATA->m_colormap = NULL; |
aad6765c | 180 | M_COLDATA->m_hasPixel = false; |
ff7b1510 | 181 | } |
c801d85f | 182 | |
68dda785 | 183 | unsigned char wxColour::Red() const |
c801d85f | 184 | { |
a1b806b9 | 185 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 186 | |
1ecc4d80 | 187 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 188 | } |
c801d85f | 189 | |
68dda785 | 190 | unsigned char wxColour::Green() const |
c801d85f | 191 | { |
a1b806b9 | 192 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 193 | |
1ecc4d80 | 194 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 195 | } |
c801d85f | 196 | |
68dda785 | 197 | unsigned char wxColour::Blue() const |
c801d85f | 198 | { |
a1b806b9 | 199 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 200 | |
1ecc4d80 | 201 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 202 | } |
c801d85f | 203 | |
c801d85f KB |
204 | void wxColour::CalcPixel( GdkColormap *cmap ) |
205 | { | |
a1b806b9 | 206 | if (!IsOk()) return; |
8bbe427f | 207 | |
d13fd3e4 | 208 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 209 | } |
c801d85f | 210 | |
68dda785 | 211 | int wxColour::GetPixel() const |
c801d85f | 212 | { |
a1b806b9 | 213 | wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") ); |
8bbe427f | 214 | |
1ecc4d80 | 215 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 216 | } |
c801d85f | 217 | |
68dda785 | 218 | GdkColor *wxColour::GetColor() const |
c801d85f | 219 | { |
a1b806b9 | 220 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") ); |
8bbe427f | 221 | |
1ecc4d80 | 222 | return &M_COLDATA->m_color; |
ff7b1510 | 223 | } |
40989e46 | 224 | |
e86d4e59 | 225 | bool wxColour::FromString(const wxString& str) |
40989e46 WS |
226 | { |
227 | GdkColor colGDK; | |
e86d4e59 | 228 | if ( gdk_color_parse( wxGTK_CONV(str), &colGDK ) ) |
40989e46 WS |
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 | } |