]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
Removed usage of GetInternalFont() from GTK 2.0. This
[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 #include "wx/gtk/private.h"
17
18 #include <gdk/gdk.h>
19 #include <gdk/gdkx.h>
20 #include <gdk/gdkprivate.h>
21
22 //-----------------------------------------------------------------------------
23 // wxColour
24 //-----------------------------------------------------------------------------
25
26 class wxColourRefData: public wxObjectRefData
27 {
28 public:
29 wxColourRefData()
30 {
31 m_color.red = 0;
32 m_color.green = 0;
33 m_color.blue = 0;
34 m_color.pixel = 0;
35 m_colormap = (GdkColormap *) NULL;
36 m_hasPixel = FALSE;
37 }
38
39 wxColourRefData(const wxColourRefData& data)
40 : wxObjectRefData()
41 {
42 m_color = data.m_color;
43 m_colormap = data.m_colormap;
44 m_hasPixel = data.m_hasPixel;
45 }
46
47 ~wxColourRefData()
48 {
49 FreeColour();
50 }
51
52 bool operator == (const wxColourRefData& data) const
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 }
61
62 void FreeColour();
63 void AllocColour( GdkColormap* cmap );
64
65 GdkColor m_color;
66 GdkColormap *m_colormap;
67 bool m_hasPixel;
68
69 friend class wxColour;
70
71 // reference counter for systems with <= 8-Bit display
72 static gushort colMapAllocCounter[ 256 ];
73 };
74
75 gushort wxColourRefData::colMapAllocCounter[ 256 ] =
76 {
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
92 void wxColourRefData::FreeColour()
93 {
94 if (m_colormap)
95 {
96 #ifdef __WXGTK20__
97 if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
98 (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
99 #else
100 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
101 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
102 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
103 #endif
104 {
105 int idx = m_color.pixel;
106 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
107
108 if (colMapAllocCounter[ idx ] == 0)
109 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
110 }
111 }
112 }
113
114 void wxColourRefData::AllocColour( GdkColormap *cmap )
115 {
116 if (m_hasPixel && (m_colormap == cmap))
117 return;
118
119 FreeColour();
120
121 #ifdef __WXGTK20__
122 if ( (cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
123 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR) )
124 #else
125 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
126 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
127 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
128 #endif
129 {
130 m_hasPixel = gdk_colormap_alloc_color( cmap, &m_color, FALSE, TRUE );
131 int idx = m_color.pixel;
132 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] + 1;
133 }
134 else
135 {
136 m_hasPixel = gdk_color_alloc( cmap, &m_color );
137 }
138 m_colormap = cmap;
139 }
140
141 //-----------------------------------------------------------------------------
142
143 #define M_COLDATA ((wxColourRefData *)m_refData)
144
145 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
146
147 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
148
149 wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
150 {
151 m_refData = new wxColourRefData();
152 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
153 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
154 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
155 M_COLDATA->m_color.pixel = 0;
156 }
157
158 void wxColour::InitFromName( const wxString &colourName )
159 {
160 wxNode *node = (wxNode *) NULL;
161 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
162 {
163 wxColour *col = (wxColour*)node->Data();
164 UnRef();
165 if (col) Ref( *col );
166 }
167 else
168 {
169 m_refData = new wxColourRefData();
170
171 if (!gdk_color_parse( wxGTK_CONV( colourName ), &M_COLDATA->m_color ))
172 {
173 // VZ: asserts are good in general but this one is triggered by
174 // calling wxColourDatabase::FindColour() with an
175 // unrecognized colour name and this can't be avoided from the
176 // user code, so don't give it here
177 //
178 // a better solution would be to changed code in FindColour()
179
180 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
181
182 delete m_refData;
183 m_refData = (wxObjectRefData *) NULL;
184 }
185 }
186 }
187
188 wxColour::~wxColour()
189 {
190 }
191
192 bool wxColour::operator == ( const wxColour& col ) const
193 {
194 if (m_refData == col.m_refData) return TRUE;
195
196 if (!m_refData || !col.m_refData) return FALSE;
197
198 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
199 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
200 if (own->red != other->red) return FALSE;
201 if (own->blue != other->blue) return FALSE;
202 if (own->green != other->green) return FALSE;
203
204 return TRUE;
205 }
206
207 wxObjectRefData *wxColour::CreateRefData() const
208 {
209 return new wxColourRefData;
210 }
211
212 wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
213 {
214 return new wxColourRefData(*(wxColourRefData *)data);
215 }
216
217 void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
218 {
219 AllocExclusive();
220
221 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
222 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
223 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
224 M_COLDATA->m_color.pixel = 0;
225
226 M_COLDATA->m_colormap = (GdkColormap*) NULL;
227 M_COLDATA->m_hasPixel = FALSE;
228 }
229
230 unsigned char wxColour::Red() const
231 {
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
233
234 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
235 }
236
237 unsigned char wxColour::Green() const
238 {
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
240
241 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
242 }
243
244 unsigned char wxColour::Blue() const
245 {
246 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
247
248 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
249 }
250
251 void wxColour::CalcPixel( GdkColormap *cmap )
252 {
253 if (!Ok()) return;
254
255 M_COLDATA->AllocColour( cmap );
256 }
257
258 int wxColour::GetPixel() const
259 {
260 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
261
262 return M_COLDATA->m_color.pixel;
263 }
264
265 GdkColor *wxColour::GetColor() const
266 {
267 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
268
269 return &M_COLDATA->m_color;
270 }
271
272