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"
15 #include "wx/gdicmn.h"
16 #include "wx/gtk1/private.h"
20 #include <gdk/gdkprivate.h>
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 class wxColourRefData
: public wxObjectRefData
35 m_colormap
= (GdkColormap
*) NULL
;
39 wxColourRefData(const wxColourRefData
& data
)
42 m_color
= data
.m_color
;
43 m_colormap
= data
.m_colormap
;
44 m_hasPixel
= data
.m_hasPixel
;
52 bool operator == (const wxColourRefData
& data
) const
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
);
63 void AllocColour( GdkColormap
* cmap
);
66 GdkColormap
*m_colormap
;
69 friend class wxColour
;
71 // reference counter for systems with <= 8-Bit display
72 static gushort colMapAllocCounter
[ 256 ];
75 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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
92 void wxColourRefData::FreeColour()
96 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
97 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
98 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
100 int idx
= m_color
.pixel
;
101 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
103 if (colMapAllocCounter
[ idx
] == 0)
104 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
109 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
111 if (m_hasPixel
&& (m_colormap
== cmap
))
116 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
117 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
118 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
120 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
121 int idx
= m_color
.pixel
;
122 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
126 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
131 //-----------------------------------------------------------------------------
133 #define M_COLDATA ((wxColourRefData *)m_refData)
135 // GDK's values are in 0..65535 range, our are in 0..255
138 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
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 wxObjectRefData
*wxColour::CreateRefData() const
161 return new wxColourRefData
;
164 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
166 return new wxColourRefData(*(wxColourRefData
*)data
);
169 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
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
= (GdkColormap
*) NULL
;
179 M_COLDATA
->m_hasPixel
= false;
182 unsigned char wxColour::Red() const
184 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
186 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
189 unsigned char wxColour::Green() const
191 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
193 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
196 unsigned char wxColour::Blue() const
198 wxCHECK_MSG( Ok(), 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( Ok(), 0, wxT("invalid colour") );
214 return M_COLDATA
->m_color
.pixel
;
217 GdkColor
*wxColour::GetColor() const
219 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
221 return &M_COLDATA
->m_color
;
224 bool wxColour::FromString(const wxChar
*str
)
227 if ( gdk_color_parse( str
, &colGDK
) )
231 m_refData
= new wxColourRefData
;
232 M_COLDATA
->m_color
= colGDK
;
236 return wxColourBase::FromString(str
);