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