1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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"
14 #include "wx/gdicmn.h"
15 #include "wx/gtk/private.h"
19 #include <gdk/gdkprivate.h>
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxColourRefData
: public wxObjectRefData
34 m_colormap
= (GdkColormap
*) NULL
;
38 wxColourRefData(const wxColourRefData
& data
)
41 m_color
= data
.m_color
;
42 m_colormap
= data
.m_colormap
;
43 m_hasPixel
= data
.m_hasPixel
;
51 bool operator == (const wxColourRefData
& data
) const
53 return (m_colormap
== data
.m_colormap
&&
54 m_hasPixel
== data
.m_hasPixel
&&
55 m_color
.red
== data
.m_color
.red
&&
56 m_color
.green
== data
.m_color
.green
&&
57 m_color
.blue
== data
.m_color
.blue
&&
58 m_color
.pixel
== data
.m_color
.pixel
);
62 void AllocColour( GdkColormap
* cmap
);
65 GdkColormap
*m_colormap
;
68 friend class wxColour
;
70 // reference counter for systems with <= 8-Bit display
71 static gushort colMapAllocCounter
[ 256 ];
74 gushort
wxColourRefData::colMapAllocCounter
[ 256 ] =
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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
91 void wxColourRefData::FreeColour()
96 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
97 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
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
))
104 int idx
= m_color
.pixel
;
105 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] - 1;
107 if (colMapAllocCounter
[ idx
] == 0)
108 gdk_colormap_free_colors( m_colormap
, &m_color
, 1 );
113 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
115 if (m_hasPixel
&& (m_colormap
== cmap
))
121 if ( (cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
122 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
) )
124 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
125 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
126 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
129 m_hasPixel
= gdk_colormap_alloc_color( cmap
, &m_color
, FALSE
, TRUE
);
130 int idx
= m_color
.pixel
;
131 colMapAllocCounter
[ idx
] = colMapAllocCounter
[ idx
] + 1;
135 m_hasPixel
= gdk_color_alloc( cmap
, &m_color
);
140 //-----------------------------------------------------------------------------
142 #define M_COLDATA ((wxColourRefData *)m_refData)
144 // GDK's values are in 0..65535 range, our are in 0..255
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;
159 wxColour
wxColour::CreateByName(const wxString
& name
)
164 if ( gdk_color_parse( wxGTK_CONV( name
), &colGDK
) )
166 wxColourRefData
*refData
= new wxColourRefData
;
167 refData
->m_color
= colGDK
;
168 col
.m_refData
= refData
;
175 void wxColour::InitFromName( const wxString
&colourName
)
177 // check the cache first
179 if ( wxTheColourDatabase
)
181 col
= wxTheColourDatabase
->Find(colourName
);
186 col
= CreateByName(colourName
);
195 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
199 wxColour::~wxColour()
203 bool wxColour::operator == ( const wxColour
& col
) const
205 if (m_refData
== col
.m_refData
)
208 if (!m_refData
|| !col
.m_refData
)
211 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
212 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
213 return own
->red
== other
->red
&&
214 own
->blue
== other
->blue
&&
215 own
->green
== other
->green
;
218 wxObjectRefData
*wxColour::CreateRefData() const
220 return new wxColourRefData
;
223 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
225 return new wxColourRefData(*(wxColourRefData
*)data
);
228 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
232 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
233 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
234 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
235 M_COLDATA
->m_color
.pixel
= 0;
237 M_COLDATA
->m_colormap
= (GdkColormap
*) NULL
;
238 M_COLDATA
->m_hasPixel
= false;
241 unsigned char wxColour::Red() const
243 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
245 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
248 unsigned char wxColour::Green() const
250 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
252 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
255 unsigned char wxColour::Blue() const
257 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
259 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
262 void wxColour::CalcPixel( GdkColormap
*cmap
)
266 M_COLDATA
->AllocColour( cmap
);
269 int wxColour::GetPixel() const
271 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
273 return M_COLDATA
->m_color
.pixel
;
276 GdkColor
*wxColour::GetColor() const
278 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
280 return &M_COLDATA
->m_color
;