]> git.saurik.com Git - wxWidgets.git/blame - src/x11/colour.cpp
wxGTK1 : wx/private/eventloopsourcesmanager.h was missing in evtloop.cpp
[wxWidgets.git] / src / x11 / colour.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
edc536d3 2// Name: src/x11/colour.cpp
83df96d6 3// Purpose: wxColour class
3cd0b8c5 4// Author: Julian Smart, Robert Roebling
83df96d6
JS
5// Modified by:
6// Created: 17/09/98
3cd0b8c5 7// Copyright: (c) Julian Smart, Robert Roebling
edc536d3 8// Licence: wxWindows licence
83df96d6
JS
9/////////////////////////////////////////////////////////////////////////////
10
40989e46
WS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#include "wx/colour.h"
15
670f9935
WS
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
dd05139a 18 #include "wx/gdicmn.h"
670f9935
WS
19#endif
20
7266b672 21#include "wx/x11/private.h"
83df96d6 22
3cd0b8c5
RR
23//-----------------------------------------------------------------------------
24// wxColour
25//-----------------------------------------------------------------------------
83df96d6 26
8f884a0d 27class wxColourRefData : public wxGDIRefData
83df96d6 28{
3cd0b8c5
RR
29public:
30 wxColourRefData()
31 {
32 m_color.red = 0;
33 m_color.green = 0;
34 m_color.blue = 0;
35 m_color.pixel = 0;
d3b9f782 36 m_colormap = NULL;
aad6765c 37 m_hasPixel = false;
3cd0b8c5 38 }
8f884a0d
VZ
39
40 wxColourRefData(const wxColourRefData& data)
d5cf34f4
JS
41 {
42 m_color = data.m_color;
43 m_colormap = data.m_colormap;
44 m_hasPixel = data.m_hasPixel;
45 }
aad6765c 46
d3c7fc99 47 virtual ~wxColourRefData()
3cd0b8c5
RR
48 {
49 FreeColour();
50 }
83df96d6 51
8f884a0d 52 bool operator==(const wxColourRefData& data) const
3cd0b8c5
RR
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 }
aad6765c 61
3cd0b8c5
RR
62 void FreeColour();
63 void AllocColour( WXColormap cmap );
64
65 XColor m_color;
66 WXColormap m_colormap;
67 bool m_hasPixel;
68
69 friend class wxColour;
70
71 // reference counter for systems with <= 8-Bit display
72 static unsigned short colMapAllocCounter[ 256 ];
73};
74
aad6765c
JS
75unsigned short wxColourRefData::colMapAllocCounter[ 256 ] =
76{
3cd0b8c5
RR
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
90};
91
92void wxColourRefData::FreeColour()
83df96d6 93{
dc4025af
RR
94 if (!m_colormap)
95 return;
aad6765c 96#if !wxUSE_NANOX
ac7f6cf0
VZ
97 if ( wxTheApp &&
98 (wxTheApp->m_visualInfo->m_visualType == GrayScale ||
99 wxTheApp->m_visualInfo->m_visualType == PseudoColor) )
3cd0b8c5 100 {
dc4025af
RR
101 int idx = m_color.pixel;
102 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
aad6765c 103
dc4025af
RR
104 if (colMapAllocCounter[ idx ] == 0)
105 {
106 unsigned long pixel = m_color.pixel;
107 XFreeColors( wxGlobalDisplay(), (Colormap) m_colormap, &pixel, 1, 0 );
3cd0b8c5 108 }
3cd0b8c5 109 }
8601b2e1 110#endif
83df96d6
JS
111}
112
3cd0b8c5 113void wxColourRefData::AllocColour( WXColormap cmap )
83df96d6 114{
3cd0b8c5
RR
115 if (m_hasPixel && (m_colormap == cmap))
116 return;
117
118 FreeColour();
119
8601b2e1 120#if !wxUSE_NANOX
9ce8d6a2
MB
121 if ((wxTheApp->m_visualInfo->m_visualType == GrayScale) ||
122 (wxTheApp->m_visualInfo->m_visualType == PseudoColor))
3cd0b8c5 123 {
dc4025af 124 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
3cd0b8c5
RR
125 int idx = m_color.pixel;
126 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
127 }
128 else
8601b2e1 129#endif
3cd0b8c5
RR
130 {
131 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
132 }
aad6765c 133
3cd0b8c5 134 m_colormap = cmap;
83df96d6
JS
135}
136
3cd0b8c5
RR
137//-----------------------------------------------------------------------------
138
139#define M_COLDATA ((wxColourRefData *)m_refData)
140
141#define SHIFT (8*(sizeof(short int)-sizeof(char)))
142
3cd0b8c5 143wxColour::~wxColour()
83df96d6
JS
144{
145}
146
3cd0b8c5 147bool wxColour::operator == ( const wxColour& col ) const
83df96d6 148{
aad6765c 149 if (m_refData == col.m_refData) return true;
3cd0b8c5 150
aad6765c 151 if (!m_refData || !col.m_refData) return false;
3cd0b8c5
RR
152
153 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
154 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
3cd0b8c5 155
aad6765c
JS
156 return (own->red == other->red)
157 && (own->green == other->green)
d35dec9b 158 && (own->blue == other->blue) ;
aad6765c 159
83df96d6
JS
160}
161
8f884a0d 162wxGDIRefData *wxColour::CreateGDIRefData() const
3cd0b8c5
RR
163{
164 return new wxColourRefData;
165}
83df96d6 166
8f884a0d 167wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
3cd0b8c5
RR
168{
169 return new wxColourRefData(*(wxColourRefData *)data);
170}
83df96d6 171
aea95b1c
VZ
172void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
173 unsigned char WXUNUSED(alpha))
83df96d6 174{
3cd0b8c5 175 AllocExclusive();
aad6765c 176
0b5c0e1a
JS
177#if wxUSE_NANOX
178 M_COLDATA->m_color.red = ((unsigned short)red) ;
179 M_COLDATA->m_color.green = ((unsigned short)green) ;
180 M_COLDATA->m_color.blue = ((unsigned short)blue) ;
181#else
3cd0b8c5
RR
182 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
183 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
184 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
0b5c0e1a 185#endif
3cd0b8c5 186 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
187}
188
3cd0b8c5
RR
189unsigned char wxColour::Red() const
190{
a1b806b9 191 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
3cd0b8c5 192
0b5c0e1a
JS
193#if wxUSE_NANOX
194 return (unsigned char) M_COLDATA->m_color.red ;
195#else
3cd0b8c5 196 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
0b5c0e1a 197#endif
3cd0b8c5
RR
198}
199
200unsigned char wxColour::Green() const
201{
a1b806b9 202 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
3cd0b8c5 203
0b5c0e1a
JS
204#if wxUSE_NANOX
205 return (unsigned char) M_COLDATA->m_color.green ;
206#else
3cd0b8c5 207 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
0b5c0e1a 208#endif
3cd0b8c5
RR
209}
210
211unsigned char wxColour::Blue() const
212{
a1b806b9 213 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
3cd0b8c5 214
0b5c0e1a
JS
215#if wxUSE_NANOX
216 return (unsigned char) M_COLDATA->m_color.blue ;
217#else
3cd0b8c5 218 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
0b5c0e1a 219#endif
3cd0b8c5
RR
220}
221
222void wxColour::CalcPixel( WXColormap cmap )
223{
a1b806b9 224 wxCHECK_RET( IsOk(), wxT("invalid colour") );
887dd52f
RR
225
226 wxCHECK_RET( cmap, wxT("invalid colormap") );
3cd0b8c5
RR
227
228 M_COLDATA->AllocColour( cmap );
229}
230
231unsigned long wxColour::GetPixel() const
232{
a1b806b9 233 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
3cd0b8c5
RR
234
235 return M_COLDATA->m_color.pixel;
236}
237
238WXColor *wxColour::GetColor() const
239{
a1b806b9 240 wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") );
3cd0b8c5
RR
241
242 return (WXColor*) &M_COLDATA->m_color;
83df96d6 243}
40989e46 244
e86d4e59 245bool wxColour::FromString(const wxString& name)
40989e46
WS
246{
247 Display *dpy = wxGlobalDisplay();
248 WXColormap colormap = wxTheApp->GetMainColormap( dpy );
249 XColor xcol;
e86d4e59 250 if ( XParseColor( dpy, (Colormap)colormap, name.mbc_str(), &xcol ) )
40989e46
WS
251 {
252 UnRef();
253
254 m_refData = new wxColourRefData;
255 M_COLDATA->m_colormap = colormap;
256 M_COLDATA->m_color = xcol;
257 return true;
258 }
259
260 return wxColourBase::FromString(name);
261}