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