]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/colour.cpp
The rounded corners look really dumb at this size.
[wxWidgets.git] / src / gtk1 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/colour.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/gdicmn.h"
16 #endif
17
18 #include "wx/gtk1/private.h"
19
20 #include <gdk/gdk.h>
21 #include <gdk/gdkx.h>
22 #include <gdk/gdkprivate.h>
23
24 //-----------------------------------------------------------------------------
25 // wxColour
26 //-----------------------------------------------------------------------------
27
28 class wxColourRefData : public wxGDIRefData
29 {
30 public:
31 wxColourRefData()
32 {
33 m_color.red = 0;
34 m_color.green = 0;
35 m_color.blue = 0;
36 m_color.pixel = 0;
37 m_colormap = NULL;
38 m_hasPixel = false;
39 }
40
41 wxColourRefData(const wxColourRefData& data)
42 {
43 m_color = data.m_color;
44 m_colormap = data.m_colormap;
45 m_hasPixel = data.m_hasPixel;
46 }
47
48 virtual ~wxColourRefData()
49 {
50 FreeColour();
51 }
52
53 bool operator == (const wxColourRefData& data) const
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 }
62
63 void FreeColour();
64 void AllocColour( GdkColormap* cmap );
65
66 GdkColor m_color;
67 GdkColormap *m_colormap;
68 bool m_hasPixel;
69
70 friend class wxColour;
71
72 // reference counter for systems with <= 8-Bit display
73 static gushort colMapAllocCounter[ 256 ];
74 };
75
76 gushort wxColourRefData::colMapAllocCounter[ 256 ] =
77 {
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
93 void wxColourRefData::FreeColour()
94 {
95 if (m_colormap)
96 {
97 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap;
98 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
99 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
100 {
101 int idx = m_color.pixel;
102 colMapAllocCounter[ idx ] = colMapAllocCounter[ idx ] - 1;
103
104 if (colMapAllocCounter[ idx ] == 0)
105 gdk_colormap_free_colors( m_colormap, &m_color, 1 );
106 }
107 }
108 }
109
110 void wxColourRefData::AllocColour( GdkColormap *cmap )
111 {
112 if (m_hasPixel && (m_colormap == cmap))
113 return;
114
115 FreeColour();
116
117 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
118 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
119 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
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 // GDK's values are in 0..65535 range, our are in 0..255
137 #define SHIFT 8
138
139 wxColour::~wxColour()
140 {
141 }
142
143 bool wxColour::operator == ( const wxColour& col ) const
144 {
145 if (m_refData == col.m_refData)
146 return true;
147
148 if (!m_refData || !col.m_refData)
149 return false;
150
151 GdkColor *own = &(((wxColourRefData*)m_refData)->m_color);
152 GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color);
153 return own->red == other->red &&
154 own->blue == other->blue &&
155 own->green == other->green;
156 }
157
158 wxGDIRefData *wxColour::CreateGDIRefData() const
159 {
160 return new wxColourRefData;
161 }
162
163 wxGDIRefData *wxColour::CloneGDIRefData(const wxGDIRefData *data) const
164 {
165 return new wxColourRefData(*(wxColourRefData *)data);
166 }
167
168 void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
169 unsigned char WXUNUSED(alpha))
170 {
171 AllocExclusive();
172
173 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
174 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
175 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
176 M_COLDATA->m_color.pixel = 0;
177
178 M_COLDATA->m_colormap = NULL;
179 M_COLDATA->m_hasPixel = false;
180 }
181
182 unsigned char wxColour::Red() const
183 {
184 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
185
186 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
187 }
188
189 unsigned char wxColour::Green() const
190 {
191 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
192
193 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
194 }
195
196 unsigned char wxColour::Blue() const
197 {
198 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
199
200 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
201 }
202
203 void wxColour::CalcPixel( GdkColormap *cmap )
204 {
205 if (!IsOk()) return;
206
207 M_COLDATA->AllocColour( cmap );
208 }
209
210 int wxColour::GetPixel() const
211 {
212 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
213
214 return M_COLDATA->m_color.pixel;
215 }
216
217 GdkColor *wxColour::GetColor() const
218 {
219 wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") );
220
221 return &M_COLDATA->m_color;
222 }
223
224 bool wxColour::FromString(const wxString& str)
225 {
226 GdkColor colGDK;
227 if ( gdk_color_parse( wxGTK_CONV(str), &colGDK ) )
228 {
229 UnRef();
230
231 m_refData = new wxColourRefData;
232 M_COLDATA->m_color = colGDK;
233 return true;
234 }
235
236 return wxColourBase::FromString(str);
237 }