]>
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"
15 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 class wxColourRefData
: public wxObjectRefData
36 wxColourRefData(const wxColourRefData
& data
)
39 m_color
= data
.m_color
;
40 m_colormap
= data
.m_colormap
;
41 m_hasPixel
= data
.m_hasPixel
;
50 void AllocColour( GdkColormap
* cmap
);
53 GdkColormap
*m_colormap
;
57 void wxColourRefData::FreeColour()
61 gdk_colormap_free_colors(m_colormap
, &m_color
, 1);
65 void wxColourRefData::AllocColour( GdkColormap
*cmap
)
67 if (m_hasPixel
&& (m_colormap
== cmap
))
72 m_hasPixel
= gdk_colormap_alloc_color(cmap
, &m_color
, FALSE
, TRUE
);
76 //-----------------------------------------------------------------------------
78 #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
80 // GDK's values are in 0..65535 range, our are in 0..255
83 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
89 bool wxColour::operator == ( const wxColour
& col
) const
91 if (m_refData
== col
.m_refData
)
94 if (!m_refData
|| !col
.m_refData
)
97 const GdkColor
& own
= M_COLDATA
->m_color
;
98 const GdkColor
& other
= wx_static_cast(wxColourRefData
*, col
.m_refData
)->m_color
;
99 return own
.red
== other
.red
&&
100 own
.blue
== other
.blue
&&
101 own
.green
== other
.green
;
104 wxObjectRefData
*wxColour::CreateRefData() const
106 return new wxColourRefData
;
109 wxObjectRefData
*wxColour::CloneRefData(const wxObjectRefData
*data
) const
111 return new wxColourRefData(*(wxColourRefData
*)data
);
114 void wxColour::InitWith( unsigned char red
, unsigned char green
, unsigned char blue
)
118 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
119 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
120 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
121 M_COLDATA
->m_color
.pixel
= 0;
123 M_COLDATA
->m_colormap
= NULL
;
124 M_COLDATA
->m_hasPixel
= false;
127 unsigned char wxColour::Red() const
129 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
131 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
134 unsigned char wxColour::Green() const
136 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
138 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
141 unsigned char wxColour::Blue() const
143 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
145 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
148 void wxColour::CalcPixel( GdkColormap
*cmap
)
152 M_COLDATA
->AllocColour( cmap
);
155 int wxColour::GetPixel() const
157 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
159 return M_COLDATA
->m_color
.pixel
;
162 GdkColor
*wxColour::GetColor() const
164 wxCHECK_MSG( Ok(), NULL
, wxT("invalid colour") );
166 return &M_COLDATA
->m_color
;
169 bool wxColour::FromString(const wxChar
*str
)
172 if ( gdk_color_parse( wxGTK_CONV_SYS( str
), &colGDK
) )
176 m_refData
= new wxColourRefData
;
177 M_COLDATA
->m_color
= colGDK
;
181 return wxColourBase::FromString(str
);