]> git.saurik.com Git - wxWidgets.git/blob - src/x11/colour.cpp
added SetString(), deprecated SetLabel()
[wxWidgets.git] / src / x11 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colour.cpp
3 // Purpose: wxColour class
4 // Author: Julian Smart, Robert Roebling
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12
13 #ifdef __GNUG__
14 #pragma implementation "colour.h"
15 #endif
16
17 #include "wx/gdicmn.h"
18
19 #include "wx/x11/private.h"
20
21 //-----------------------------------------------------------------------------
22 // wxColour
23 //-----------------------------------------------------------------------------
24
25 class wxColourRefData: public wxObjectRefData
26 {
27 public:
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 }
42
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
66 unsigned 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
83 void wxColourRefData::FreeColour()
84 {
85 #if 0
86 if (m_colormap)
87 {
88 Colormap cm = (Colormap)m_colormap;
89
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 }
100 }
101 #endif
102 }
103
104 void wxColourRefData::AllocColour( WXColormap cmap )
105 {
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;
126 }
127
128 //-----------------------------------------------------------------------------
129
130 #define M_COLDATA ((wxColourRefData *)m_refData)
131
132 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
133
134 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
135
136 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
137 {
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;
143 }
144
145 void wxColour::InitFromName( const wxString &colourName )
146 {
147 wxNode *node = (wxNode *) NULL;
148 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
149 {
150 wxColour *col = (wxColour*)node->Data();
151 UnRef();
152 if (col) Ref( *col );
153 }
154 else
155 {
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 }
171 }
172 }
173
174 wxColour::~wxColour()
175 {
176 }
177
178 bool wxColour::operator == ( const wxColour& col ) const
179 {
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;
191 }
192
193 wxObjectRefData *wxColour::CreateRefData() const
194 {
195 return new wxColourRefData;
196 }
197
198 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
199 {
200 return new wxColourRefData(*(wxColourRefData *)data);
201 }
202
203 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
204 {
205 AllocExclusive();
206
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;
212 }
213
214 unsigned 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
221 unsigned 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
228 unsigned 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
235 void wxColour::CalcPixel( WXColormap cmap )
236 {
237 if (!Ok()) return;
238
239 M_COLDATA->AllocColour( cmap );
240 }
241
242 unsigned long wxColour::GetPixel() const
243 {
244 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
245
246 return M_COLDATA->m_color.pixel;
247 }
248
249 WXColor *wxColour::GetColor() const
250 {
251 wxCHECK_MSG( Ok(), (WXColor *) NULL, wxT("invalid colour") );
252
253 return (WXColor*) &M_COLDATA->m_color;
254 }