Updates for Y positioning
[wxWidgets.git] / src / gtk / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "colour.h"
13 #endif
14
15 #include "wx/gdicmn.h"
16
17 #include <gdk/gdk.h>
18 #include <gdk/gdkx.h>
19 #include <gdk/gdkprivate.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 = (GdkColormap *) 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( GdkColormap* cmap );
55
56 GdkColor m_color;
57 GdkColormap *m_colormap;
58 bool m_hasPixel;
59
60 friend class wxColour;
61
62 // reference counter for systems with <= 8-Bit display
63 static gushort colMapAllocCounter[ 256 ];
64 };
65
66 gushort 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 (m_colormap)
86 {
87 #ifdef __WXGTK20__
88 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
89 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
90 #else
91 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
92 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
93 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
94 #endif
95 {
96 int idx = m_color.pixel;
97 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
98
99 if (colMapAllocCounter[ idx ] == 0)
100 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
101 }
102 }
103 }
104
105 void wxColourRefData::AllocColour( GdkColormap *cmap )
106 {
107 if (m_hasPixel && (m_colormap == cmap))
108 return;
109
110 FreeColour();
111
112 #ifdef __WXGTK20__
113 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
114 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
115 #else
116 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
117 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
118 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
119 #endif
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
132 //-----------------------------------------------------------------------------
133
134 #define M_COLDATA ((wxColourRefData *)m_refData)
135
136 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
137
138 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
139
140 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
141 {
142 m_refData = new wxColourRefData();
143 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
144 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
145 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
146 M_COLDATA->m_color.pixel = 0;
147 }
148
149 void wxColour::InitFromName( const wxString &colourName )
150 {
151 wxNode *node = (wxNode *) NULL;
152 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
153 {
154 wxColour *col = (wxColour*)node->Data();
155 UnRef();
156 if (col) Ref( *col );
157 }
158 else
159 {
160 m_refData = new wxColourRefData();
161 if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color ))
162 {
163 // VZ: asserts are good in general but this one is triggered by
164 // calling wxColourDatabase::FindColour() with an
165 // unrecognized colour name and this can't be avoided from the
166 // user code, so don't give it here
167 //
168 // a better solution would be to changed code in FindColour()
169
170 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
171
172 delete m_refData;
173 m_refData = (wxObjectRefData *) NULL;
174 }
175 }
176 }
177
178 wxColour::~wxColour()
179 {
180 }
181
182 bool wxColour::operator == ( const wxColour& col ) const
183 {
184 if (m_refData == col.m_refData) return TRUE;
185
186 if (!m_refData || !col.m_refData) return FALSE;
187
188 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
189 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
190 if (own->red != other->red) return FALSE;
191 if (own->blue != other->blue) return FALSE;
192 if (own->green != other->green) return FALSE;
193
194 return TRUE;
195 }
196
197 wxObjectRefData *wxColour::CreateRefData() const
198 {
199 return new wxColourRefData;
200 }
201
202 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
203 {
204 return new wxColourRefData(*(wxColourRefData *)data);
205 }
206
207 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
208 {
209 AllocExclusive();
210
211 m_refData = new wxColourRefData();
212 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
213 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
214 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
215 M_COLDATA->m_color.pixel = 0;
216 }
217
218 unsigned char wxColour::Red() const
219 {
220 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
221
222 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
223 }
224
225 unsigned char wxColour::Green() const
226 {
227 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
228
229 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
230 }
231
232 unsigned char wxColour::Blue() const
233 {
234 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
235
236 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
237 }
238
239 void wxColour::CalcPixel( GdkColormap *cmap )
240 {
241 if (!Ok()) return;
242
243 M_COLDATA->AllocColour( cmap );
244 }
245
246 int wxColour::GetPixel() const
247 {
248 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
249
250 return M_COLDATA->m_color.pixel;
251 }
252
253 GdkColor *wxColour::GetColor() const
254 {
255 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
256
257 return &M_COLDATA->m_color;
258 }
259
260