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 wxObjectRefData
38 m_colormap
= (GdkColormap
*) NULL
;
42 wxColourRefData(const wxColourRefData
& data
)
45 m_color
= data
.m_color
;
46 m_colormap
= data
.m_colormap
;
47 m_hasPixel
= data
.m_hasPixel
;
55 bool operator == (const wxColourRefData
& data
) const
57 return (m_colormap
== data
.m_colormap
&&
58 m_hasPixel
== data
.m_hasPixel
&&
59 m_color
.red
== data
.m_color
.red
&&
60 m_color
.green
== data
.m_color
.green
&&
61 m_color
.blue
== data
.m_color
.blue
&&
62 m_color
.pixel
== data
.m_color
.pixel
);
66 void AllocColour( GdkColormap
* cmap
);
69 GdkColormap
*m_colormap
;
72 friend class wxColour
;
74 // reference counter for systems with <= 8-Bit display
75 static gushort colMapAllocCounter
[ 256 ];
78 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
95 void wxColourRefData::FreeColour()
99 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
100 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
101 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
103 int idx
= m_color
.pixel
;
104 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
106 if (colMapAllocCounter
[ idx
] == 0)
107 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
112 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
114 if (m_hasPixel
&& (m_colormap
== cmap
))
119 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
120 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
121 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
123 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
124 int idx
= m_color
.pixel
;
125 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
129 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
134 //-----------------------------------------------------------------------------
136 #define M_COLDATA ((wxColourRefData *)m_refData)
138 // GDK's values are in 0..65535 range, our are in 0..255
141 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
143 wxColour::~wxColour()
147 bool wxColour::operator == ( const wxColour
& col
) const
149 if (m_refData
== col
.m_refData
)
152 if (!m_refData
|| !col
.m_refData
)
155 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
156 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
157 return own
->red
== other
->red
&&
158 own
->blue
== other
->blue
&&
159 own
->green
== other
->green
;
162 wxObjectRefData
*wxColour::CreateRefData() const
164 return new wxColourRefData
;
167 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
169 return new wxColourRefData(*(wxColourRefData
*)data
);
172 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
176 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
177 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
178 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
179 M_COLDATA
->m_color
.pixel
= 0;
181 M_COLDATA
->m_colormap
= (GdkColormap
*) NULL
;
182 M_COLDATA
->m_hasPixel
= false;
185 unsigned char wxColour::Red() const
187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
189 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
192 unsigned char wxColour::Green() const
194 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
196 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
199 unsigned char wxColour::Blue() const
201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
203 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
206 void wxColour::CalcPixel( GdkColormap
*cmap
)
210 M_COLDATA
->AllocColour( cmap
);
213 int wxColour::GetPixel() const
215 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
217 return M_COLDATA
->m_color
.pixel
;
220 GdkColor
*wxColour::GetColor() const
222 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
224 return &M_COLDATA
->m_color
;
227 bool wxColour::FromString(const wxChar
*str
)
230 if ( gdk_color_parse( str
, &colGDK
) )
234 m_refData
= new wxColourRefData
;
235 M_COLDATA
->m_color
= colGDK
;
239 return wxColourBase::FromString(str
);