]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
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/gtk/private.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 class wxColourRefData
: public wxObjectRefData
35 wxColourRefData(const wxColourRefData
& data
)
38 m_color
= data
.m_color
;
39 m_colormap
= data
.m_colormap
;
40 m_hasPixel
= data
.m_hasPixel
;
49 void AllocColour( GdkColormap
* cmap
);
52 GdkColormap
*m_colormap
;
56 void wxColourRefData::FreeColour()
60 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
64 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
66 if (m_hasPixel
&& (m_colormap
== cmap
))
71 m_hasPixel
= gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
);
75 //-----------------------------------------------------------------------------
77 #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
79 // GDK's values are in 0..65535 range, our are in 0..255
82 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
84 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
86 m_refData
= new wxColourRefData
;
87 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
88 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
89 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
90 M_COLDATA
->m_color
.pixel
= 0;
94 wxColour
wxColour::CreateByName(const wxString
& name
)
99 if ( gdk_color_parse( wxGTK_CONV( name
), &colGDK
) )
101 wxColourRefData
*refData
= new wxColourRefData
;
102 refData
->m_color
= colGDK
;
103 col
.m_refData
= refData
;
110 void wxColour::InitFromName( const wxString
&colourName
)
112 // check the cache first
113 if ( wxTheColourDatabase
)
115 *this = wxTheColourDatabase
->Find(colourName
);
120 *this = CreateByName(colourName
);
125 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
129 wxColour::~wxColour()
133 bool wxColour::operator == ( const wxColour
& col
) const
135 if (m_refData
== col
.m_refData
)
138 if (!m_refData
|| !col
.m_refData
)
141 const GdkColor
& own
= M_COLDATA
->m_color
;
142 const GdkColor
& other
= wx_static_cast(wxColourRefData
*, col
.m_refData
)->m_color
;
143 return own
.red
== other
.red
&&
144 own
.blue
== other
.blue
&&
145 own
.green
== other
.green
;
148 wxObjectRefData
*wxColour::CreateRefData() const
150 return new wxColourRefData
;
153 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
155 return new wxColourRefData(*(wxColourRefData
*)data
);
158 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
162 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
163 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
164 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
165 M_COLDATA
->m_color
.pixel
= 0;
167 M_COLDATA
->m_colormap
= NULL
;
168 M_COLDATA
->m_hasPixel
= false;
171 unsigned char wxColour::Red() const
173 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
175 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
178 unsigned char wxColour::Green() const
180 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
182 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
185 unsigned char wxColour::Blue() const
187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
189 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
192 void wxColour::CalcPixel( GdkColormap
*cmap
)
196 M_COLDATA
->AllocColour( cmap
);
199 int wxColour::GetPixel() const
201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
203 return M_COLDATA
->m_color
.pixel
;
206 GdkColor
*wxColour::GetColor() const
208 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
210 return &M_COLDATA
->m_color
;