1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "colour.h"
15 #include "wx/gdicmn.h"
16 #include "wx/gtk/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()
97 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
98 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
100 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) m_colormap
;
101 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
102 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
105 int idx
= m_color
.pixel
;
106 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
108 if (colMapAllocCounter
[ idx
] == 0)
109 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
114 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
116 if (m_hasPixel
&& (m_colormap
== cmap
))
122 if ( (cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
123 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
) )
125 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
126 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
127 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
130 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
131 int idx
= m_color
.pixel
;
132 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
136 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
141 //-----------------------------------------------------------------------------
143 #define M_COLDATA ((wxColourRefData *)m_refData)
145 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
147 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
149 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
151 m_refData
= new wxColourRefData();
152 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
153 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
154 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
155 M_COLDATA
->m_color
.pixel
= 0;
158 void wxColour::InitFromName( const wxString
&colourName
)
160 wxNode
*node
= (wxNode
*) NULL
;
161 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
163 wxColour
*col
= (wxColour
*)node
->Data();
165 if (col
) Ref( *col
);
169 m_refData
= new wxColourRefData();
171 if (!gdk_color_parse( wxGTK_CONV( colourName
), &M_COLDATA
->m_color
))
173 // VZ: asserts are good in general but this one is triggered by
174 // calling wxColourDatabase::FindColour() with an
175 // unrecognized colour name and this can't be avoided from the
176 // user code, so don't give it here
178 // a better solution would be to changed code in FindColour()
180 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
183 m_refData
= (wxObjectRefData
*) NULL
;
188 wxColour::~wxColour()
192 bool wxColour::operator == ( const wxColour
& col
) const
194 if (m_refData
== col
.m_refData
) return TRUE
;
196 if (!m_refData
|| !col
.m_refData
) return FALSE
;
198 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
199 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
200 if (own
->red
!= other
->red
) return FALSE
;
201 if (own
->blue
!= other
->blue
) return FALSE
;
202 if (own
->green
!= other
->green
) return FALSE
;
207 wxObjectRefData
*wxColour::CreateRefData() const
209 return new wxColourRefData
;
212 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
214 return new wxColourRefData(*(wxColourRefData
*)data
);
217 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
221 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
222 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
223 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
224 M_COLDATA
->m_color
.pixel
= 0;
226 M_COLDATA
->m_colormap
= (GdkColormap
*) NULL
;
227 M_COLDATA
->m_hasPixel
= FALSE
;
230 unsigned char wxColour::Red() const
232 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
234 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
237 unsigned char wxColour::Green() const
239 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
241 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
244 unsigned char wxColour::Blue() const
246 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
248 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
251 void wxColour::CalcPixel( GdkColormap
*cmap
)
255 M_COLDATA
->AllocColour( cmap
);
258 int wxColour::GetPixel() const
260 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
262 return M_COLDATA
->m_color
.pixel
;
265 GdkColor
*wxColour::GetColor() const
267 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
269 return &M_COLDATA
->m_color
;