1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/colour.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
12 #include "wx/colour.h"
15 #include "wx/gdicmn.h"
18 #include "wx/gtk1/private.h"
22 #include <gdk/gdkprivate.h>
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 class wxColourRefData
: public wxGDIRefData
41 wxColourRefData(const wxColourRefData
& data
)
43 m_color
= data
.m_color
;
44 m_colormap
= data
.m_colormap
;
45 m_hasPixel
= data
.m_hasPixel
;
48 virtual ~wxColourRefData()
53 bool operator == (const wxColourRefData
& data
) const
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
);
64 void AllocColour( GdkColormap
* cmap
);
67 GdkColormap
*m_colormap
;
70 friend class wxColour
;
72 // reference counter for systems with <= 8-Bit display
73 static gushort colMapAllocCounter
[ 256 ];
76 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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
93 void wxColourRefData::FreeColour()
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
))
101 int idx
= m_color
.pixel
;
102 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
104 if (colMapAllocCounter
[ idx
] == 0)
105 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
110 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
112 if (m_hasPixel
&& (m_colormap
== cmap
))
117 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
118 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
119 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
121 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
122 int idx
= m_color
.pixel
;
123 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
127 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
132 //-----------------------------------------------------------------------------
134 #define M_COLDATA ((wxColourRefData *)m_refData)
136 // GDK's values are in 0..65535 range, our are in 0..255
139 wxColour::~wxColour()
143 bool wxColour::operator == ( const wxColour
& col
) const
145 if (m_refData
== col
.m_refData
)
148 if (!m_refData
|| !col
.m_refData
)
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
;
158 wxGDIRefData
*wxColour::CreateGDIRefData() const
160 return new wxColourRefData
;
163 wxGDIRefData
*wxColour::CloneGDIRefData(const wxGDIRefData
*data
) const
165 return new wxColourRefData(*(wxColourRefData
*)data
);
168 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
169 unsigned char WXUNUSED(alpha
))
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;
178 M_COLDATA
->m_colormap
= NULL
;
179 M_COLDATA
->m_hasPixel
= false;
182 unsigned char wxColour::Red() const
184 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
186 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
189 unsigned char wxColour::Green() const
191 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
193 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
196 unsigned char wxColour::Blue() const
198 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
200 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
203 void wxColour::CalcPixel( GdkColormap
*cmap
)
207 M_COLDATA
->AllocColour( cmap
);
210 int wxColour::GetPixel() const
212 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
214 return M_COLDATA
->m_color
.pixel
;
217 GdkColor
*wxColour::GetColor() const
219 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid colour") );
221 return &M_COLDATA
->m_color
;
224 bool wxColour::FromString(const wxString
& str
)
227 if ( gdk_color_parse( wxGTK_CONV(str
), &colGDK
) )
231 m_refData
= new wxColourRefData
;
232 M_COLDATA
->m_color
= colGDK
;
236 return wxColourBase::FromString(str
);