1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "colour.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
18 #include "wx/gdicmn.h"
19 #include "wx/colour.h"
20 #include "wx/gtk/private.h"
24 #include <gdk/gdkprivate.h>
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 class wxColourRefData
: public wxObjectRefData
39 m_colormap
= (GdkColormap
*) NULL
;
43 wxColourRefData(const wxColourRefData
& data
)
46 m_color
= data
.m_color
;
47 m_colormap
= data
.m_colormap
;
48 m_hasPixel
= data
.m_hasPixel
;
56 bool operator == (const wxColourRefData
& data
) const
58 return (m_colormap
== data
.m_colormap
&&
59 m_hasPixel
== data
.m_hasPixel
&&
60 m_color
.red
== data
.m_color
.red
&&
61 m_color
.green
== data
.m_color
.green
&&
62 m_color
.blue
== data
.m_color
.blue
&&
63 m_color
.pixel
== data
.m_color
.pixel
);
67 void AllocColour( GdkColormap
* cmap
);
70 GdkColormap
*m_colormap
;
73 friend class wxColour
;
75 // reference counter for systems with <= 8-Bit display
76 static gushort colMapAllocCounter
[ 256 ];
79 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
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, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
96 void wxColourRefData::FreeColour()
101 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
102 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
104 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
105 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
106 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
109 int idx
= m_color
.pixel
;
110 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
112 if (colMapAllocCounter
[ idx
] == 0)
113 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
118 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
120 if (m_hasPixel
&& (m_colormap
== cmap
))
126 if ( (cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
127 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
) )
129 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
130 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
131 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
134 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
135 int idx
= m_color
.pixel
;
136 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
140 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
145 //-----------------------------------------------------------------------------
147 #define M_COLDATA ((wxColourRefData *)m_refData)
149 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
151 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
153 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
155 m_refData
= new wxColourRefData();
156 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
157 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
158 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
159 M_COLDATA
->m_color
.pixel
= 0;
164 void wxColour::InitFromName( const wxString
&colourName
)
166 wxColour
* col
= NULL
;
167 if ( (wxTheColourDatabase
) && (col
= wxTheColourDatabase
->FindColourNoAdd(colourName
)) )
170 if (col
) Ref( *col
);
174 m_refData
= new wxColourRefData();
176 if (!gdk_color_parse( wxGTK_CONV( colourName
), &M_COLDATA
->m_color
))
178 // VZ: asserts are good in general but this one is triggered by
179 // calling wxColourDatabase::FindColour() with an
180 // unrecognized colour name and this can't be avoided from the
181 // user code, so don't give it here
183 // a better solution would be to changed code in FindColour()
185 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
188 m_refData
= (wxObjectRefData
*) NULL
;
193 wxColour::~wxColour()
197 bool wxColour::operator == ( const wxColour
& col
) const
199 if (m_refData
== col
.m_refData
) return TRUE
;
201 if (!m_refData
|| !col
.m_refData
) return FALSE
;
203 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
204 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
205 if (own
->red
!= other
->red
) return FALSE
;
206 if (own
->blue
!= other
->blue
) return FALSE
;
207 if (own
->green
!= other
->green
) return FALSE
;
212 wxObjectRefData
*wxColour::CreateRefData() const
214 return new wxColourRefData
;
217 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
219 return new wxColourRefData(*(wxColourRefData
*)data
);
222 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
226 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
227 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
228 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
229 M_COLDATA
->m_color
.pixel
= 0;
231 M_COLDATA
->m_colormap
= (GdkColormap
*) NULL
;
232 M_COLDATA
->m_hasPixel
= FALSE
;
235 unsigned char wxColour::Red() const
237 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
239 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
242 unsigned char wxColour::Green() const
244 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
246 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
249 unsigned char wxColour::Blue() const
251 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
253 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
256 void wxColour::CalcPixel( GdkColormap
*cmap
)
260 M_COLDATA
->AllocColour( cmap
);
263 int wxColour::GetPixel() const
265 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
267 return M_COLDATA
->m_color
.pixel
;
270 GdkColor
*wxColour::GetColor() const
272 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
274 return &M_COLDATA
->m_color
;