1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/colour.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/colour.h"
16 #include "wx/gdicmn.h"
19 #include "wx/gtk1/private.h"
23 #include <gdk/gdkprivate.h>
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 class wxColourRefData
: public wxGDIRefData
42 wxColourRefData(const wxColourRefData
& data
)
44 m_color
= data
.m_color
;
45 m_colormap
= data
.m_colormap
;
46 m_hasPixel
= data
.m_hasPixel
;
49 virtual ~wxColourRefData()
54 bool operator == (const wxColourRefData
& data
) const
56 return (m_colormap
== data
.m_colormap
&&
57 m_hasPixel
== data
.m_hasPixel
&&
58 m_color
.red
== data
.m_color
.red
&&
59 m_color
.green
== data
.m_color
.green
&&
60 m_color
.blue
== data
.m_color
.blue
&&
61 m_color
.pixel
== data
.m_color
.pixel
);
65 void AllocColour( GdkColormap
* cmap
);
68 GdkColormap
*m_colormap
;
71 friend class wxColour
;
73 // reference counter for systems with <= 8-Bit display
74 static gushort colMapAllocCounter
[ 256 ];
77 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94 void wxColourRefData::FreeColour()
98 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
99 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
100 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
102 int idx
= m_color
.pixel
;
103 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
105 if (colMapAllocCounter
[ idx
] == 0)
106 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
111 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
113 if (m_hasPixel
&& (m_colormap
== cmap
))
118 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
119 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
120 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
122 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
123 int idx
= m_color
.pixel
;
124 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
128 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
133 //-----------------------------------------------------------------------------
135 #define M_COLDATA ((wxColourRefData *)m_refData)
137 // GDK's values are in 0..65535 range, our are in 0..255
140 wxColour::~wxColour()
144 bool wxColour::operator == ( const wxColour
& col
) const
146 if (m_refData
== col
.m_refData
)
149 if (!m_refData
|| !col
.m_refData
)
152 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
153 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
154 return own
->red
== other
->red
&&
155 own
->blue
== other
->blue
&&
156 own
->green
== other
->green
;
159 wxGDIRefData
*wxColour::CreateGDIRefData() const
161 return new wxColourRefData
;
164 wxGDIRefData
*wxColour::CloneGDIRefData(const wxGDIRefData
*data
) const
166 return new wxColourRefData(*(wxColourRefData
*)data
);
169 void wxColour::InitRGBA(unsigned char red
, unsigned char green
, unsigned char blue
,
170 unsigned char WXUNUSED(alpha
))
174 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
175 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
176 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
177 M_COLDATA
->m_color
.pixel
= 0;
179 M_COLDATA
->m_colormap
= NULL
;
180 M_COLDATA
->m_hasPixel
= false;
183 unsigned char wxColour::Red() const
185 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
187 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
190 unsigned char wxColour::Green() const
192 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
194 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
197 unsigned char wxColour::Blue() const
199 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
201 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
204 void wxColour::CalcPixel( GdkColormap
*cmap
)
208 M_COLDATA
->AllocColour( cmap
);
211 int wxColour::GetPixel() const
213 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
215 return M_COLDATA
->m_color
.pixel
;
218 GdkColor
*wxColour::GetColor() const
220 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
222 return &M_COLDATA
->m_color
;
225 bool wxColour::FromString(const wxString
& str
)
228 if ( gdk_color_parse( wxGTK_CONV(str
), &colGDK
) )
232 m_refData
= new wxColourRefData
;
233 M_COLDATA
->m_color
= colGDK
;
237 return wxColourBase::FromString(str
);