]> git.saurik.com Git - wxWidgets.git/blame - src/x11/colour.cpp
Unicode fixes and warnings cleanup
[wxWidgets.git] / src / x11 / colour.cpp
CommitLineData
83df96d6
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: colour.cpp
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
83df96d6
JS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
83df96d6
JS
12
13#ifdef __GNUG__
14#pragma implementation "colour.h"
15#endif
16
17#include "wx/gdicmn.h"
83df96d6 18
7266b672 19#include "wx/x11/private.h"
83df96d6 20
3cd0b8c5
RR
21//-----------------------------------------------------------------------------
22// wxColour
23//-----------------------------------------------------------------------------
83df96d6 24
3cd0b8c5 25class wxColourRefData: public wxObjectRefData
83df96d6 26{
3cd0b8c5
RR
27public:
28 wxColourRefData()
29 {
30 m_color.red = 0;
31 m_color.green = 0;
32 m_color.blue = 0;
33 m_color.pixel = 0;
34 m_colormap = (WXColormap *) NULL;
35 m_hasPixel = FALSE;
36 }
37
38 ~wxColourRefData()
39 {
40 FreeColour();
41 }
83df96d6 42
3cd0b8c5
RR
43 bool operator == (const wxColourRefData& data) const
44 {
45 return (m_colormap == data.m_colormap &&
46 m_hasPixel == data.m_hasPixel &&
47 m_color.red == data.m_color.red &&
48 m_color.green == data.m_color.green &&
49 m_color.blue == data.m_color.blue &&
50 m_color.pixel == data.m_color.pixel);
51 }
52
53 void FreeColour();
54 void AllocColour( WXColormap cmap );
55
56 XColor m_color;
57 WXColormap m_colormap;
58 bool m_hasPixel;
59
60 friend class wxColour;
61
62 // reference counter for systems with <= 8-Bit display
63 static unsigned short colMapAllocCounter[ 256 ];
64};
65
66unsigned short wxColourRefData::colMapAllocCounter[ 256 ] =
67{
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
81};
82
83void wxColourRefData::FreeColour()
83df96d6 84{
ac014163 85#if 0
3cd0b8c5
RR
86 if (m_colormap)
87 {
88 Colormap cm = (Colormap)m_colormap;
89
3cd0b8c5
RR
90 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
91 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
92 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
93 {
94 int idx = m_color.pixel;
95 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
96
97 if (colMapAllocCounter[ idx ] == 0)
98 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
99 }
3cd0b8c5 100 }
ac014163 101#endif
83df96d6
JS
102}
103
3cd0b8c5 104void wxColourRefData::AllocColour( WXColormap cmap )
83df96d6 105{
3cd0b8c5
RR
106 if (m_hasPixel && (m_colormap == cmap))
107 return;
108
109 FreeColour();
110
111#if 0
112 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
113 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
114 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
115 {
116 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
117 int idx = m_color.pixel;
118 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
119 }
120 else
121#endif
122 {
123 m_hasPixel = XAllocColor( wxGlobalDisplay(), (Colormap) cmap, &m_color );
124 }
125 m_colormap = cmap;
83df96d6
JS
126}
127
3cd0b8c5
RR
128//-----------------------------------------------------------------------------
129
130#define M_COLDATA ((wxColourRefData *)m_refData)
131
132#define SHIFT (8*(sizeof(short int)-sizeof(char)))
133
134IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
135
136wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 137{
3cd0b8c5
RR
138 m_refData = new wxColourRefData();
139 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
140 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
141 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
142 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
143}
144
3cd0b8c5 145void wxColour::InitFromName( const wxString &colourName )
83df96d6 146{
3cd0b8c5
RR
147 wxNode *node = (wxNode *) NULL;
148 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
83df96d6 149 {
3cd0b8c5
RR
150 wxColour *col = (wxColour*)node->Data();
151 UnRef();
152 if (col) Ref( *col );
83df96d6
JS
153 }
154 else
155 {
3cd0b8c5
RR
156 m_refData = new wxColourRefData();
157 if (!XParseColor( wxGlobalDisplay(), (Colormap) M_COLDATA->m_colormap, colourName.mb_str(), &M_COLDATA->m_color ))
158 {
159 // VZ: asserts are good in general but this one is triggered by
160 // calling wxColourDatabase::FindColour() with an
161 // unrecognized colour name and this can't be avoided from the
162 // user code, so don't give it here
163 //
164 // a better solution would be to changed code in FindColour()
165
166 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
167
168 delete m_refData;
169 m_refData = (wxObjectRefData *) NULL;
170 }
83df96d6
JS
171 }
172}
173
3cd0b8c5 174wxColour::~wxColour()
83df96d6
JS
175{
176}
177
3cd0b8c5 178bool wxColour::operator == ( const wxColour& col ) const
83df96d6 179{
3cd0b8c5
RR
180 if (m_refData == col.m_refData) return TRUE;
181
182 if (!m_refData || !col.m_refData) return FALSE;
183
184 XColor *own = &(((wxColourRefData*)m_refData)->m_color);
185 XColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
186 if (own->red != other->red) return FALSE;
187 if (own->blue != other->blue) return FALSE;
188 if (own->green != other->green) return FALSE;
189
190 return TRUE;
83df96d6
JS
191}
192
3cd0b8c5
RR
193wxObjectRefData *wxColour::CreateRefData() const
194{
195 return new wxColourRefData;
196}
83df96d6 197
3cd0b8c5
RR
198wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
199{
200 return new wxColourRefData(*(wxColourRefData *)data);
201}
83df96d6 202
3cd0b8c5 203void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
83df96d6 204{
3cd0b8c5 205 AllocExclusive();
83df96d6 206
3cd0b8c5
RR
207 m_refData = new wxColourRefData();
208 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
209 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
210 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
211 M_COLDATA->m_color.pixel = 0;
83df96d6
JS
212}
213
3cd0b8c5
RR
214unsigned char wxColour::Red() const
215{
216 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
217
218 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
219}
220
221unsigned char wxColour::Green() const
222{
223 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
224
225 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
226}
227
228unsigned char wxColour::Blue() const
229{
230 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
231
232 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
233}
234
235void wxColour::CalcPixel( WXColormap cmap )
236{
237 if (!Ok()) return;
238
239 M_COLDATA->AllocColour( cmap );
240}
241
242unsigned long wxColour::GetPixel() const
243{
244 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
245
246 return M_COLDATA->m_color.pixel;
247}
248
249WXColor *wxColour::GetColor() const
250{
251 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
252
253 return (WXColor*) &M_COLDATA->m_color;
83df96d6 254}