]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
adapting to init pattern
[wxWidgets.git] / src / gtk / colour.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
edc536d3 2// Name: src/gtk/colour.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
dbf858b5
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
ed39ff57 13#include "wx/colour.h"
40989e46 14
2b5f62a0 15#include "wx/gtk/private.h"
c801d85f 16
071a2d78 17#include <gdk/gdk.h>
c801d85f
KB
18
19//-----------------------------------------------------------------------------
20// wxColour
21//-----------------------------------------------------------------------------
22
8f884a0d 23class wxColourRefData : public wxGDIRefData
c801d85f 24{
47c93b63 25public:
3bbc1d95 26 wxColourRefData(guint16 red, guint16 green, guint16 blue, wxByte alpha = 0xff)
c89f5c02 27 {
fa21f438 28 m_color.red =
c6685317 29 m_red = red;
fa21f438 30 m_color.green =
c6685317 31 m_green = green;
fa21f438 32 m_color.blue =
c6685317 33 m_blue = blue;
dc888336 34 m_alpha = alpha;
c89f5c02 35 m_color.pixel = 0;
c4fa282c 36 m_colormap = NULL;
dbb846c6
VZ
37 }
38
d3c7fc99 39 virtual ~wxColourRefData()
c89f5c02
RR
40 {
41 FreeColour();
42 }
15248e43 43
68dda785 44 void FreeColour();
d13fd3e4 45 void AllocColour( GdkColormap* cmap );
8bbe427f 46
c801d85f
KB
47 GdkColor m_color;
48 GdkColormap *m_colormap;
c6685317
PC
49 // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
50 guint16 m_red;
51 guint16 m_green;
52 guint16 m_blue;
3bbc1d95 53 wxByte m_alpha;
c6685317 54
c0c133e1 55 wxDECLARE_NO_COPY_CLASS(wxColourRefData);
c801d85f
KB
56};
57
68dda785 58void wxColourRefData::FreeColour()
c801d85f 59{
c6685317 60 if (m_colormap)
47c93b63 61 {
c4fa282c 62 gdk_colormap_free_colors(m_colormap, &m_color, 1);
c6685317
PC
63 m_colormap = NULL;
64 m_color.pixel = 0;
47c93b63 65 }
ff7b1510 66}
c801d85f 67
d13fd3e4
VZ
68void wxColourRefData::AllocColour( GdkColormap *cmap )
69{
c6685317
PC
70 if (m_colormap != cmap)
71 {
72 FreeColour();
4e6b8309 73
c6685317
PC
74 m_color.red = m_red;
75 m_color.green = m_green;
76 m_color.blue = m_blue;
77 if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE))
78 {
79 m_colormap = cmap;
80 }
81 }
d13fd3e4
VZ
82}
83
c801d85f
KB
84//-----------------------------------------------------------------------------
85
5c33522f 86#define M_COLDATA static_cast<wxColourRefData*>(m_refData)
c801d85f 87
c6685317 88// GDK's values are in 0..65535 range, ours are in 0..255
3b9faf4c 89#define SHIFT 8
c801d85f 90
c6685317
PC
91wxColour::wxColour(const GdkColor& gdkColor)
92{
3bbc1d95 93 m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue);
c6685317
PC
94}
95
68dda785 96wxColour::~wxColour()
c801d85f 97{
ff7b1510 98}
c801d85f 99
33b64e6f 100bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 101{
4e6b8309 102 if (m_refData == col.m_refData)
aad6765c 103 return true;
15248e43 104
4e6b8309 105 if (!m_refData || !col.m_refData)
aad6765c 106 return false;
15248e43 107
c6685317 108 wxColourRefData* refData = M_COLDATA;
5c33522f 109 wxColourRefData* that_refData = static_cast<wxColourRefData*>(col.m_refData);
c6685317
PC
110 return refData->m_red == that_refData->m_red &&
111 refData->m_green == that_refData->m_green &&
dc888336
SC
112 refData->m_blue == that_refData->m_blue &&
113 refData->m_alpha == that_refData->m_alpha;
ff7b1510 114}
c801d85f 115
aea95b1c 116void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
dc888336 117 unsigned char alpha)
c801d85f 118{
c6685317 119 UnRef();
4e6b8309 120
c6685317
PC
121 m_refData = new wxColourRefData(
122 (guint16(red) << SHIFT) + red,
123 (guint16(green) << SHIFT) + green,
dc888336 124 (guint16(blue) << SHIFT) + blue,
3bbc1d95 125 alpha);
ff7b1510 126}
c801d85f 127
68dda785 128unsigned char wxColour::Red() const
c801d85f 129{
a1b806b9 130 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
8bbe427f 131
c6685317 132 return wxByte(M_COLDATA->m_red >> SHIFT);
ff7b1510 133}
c801d85f 134
68dda785 135unsigned char wxColour::Green() const
c801d85f 136{
a1b806b9 137 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
8bbe427f 138
c6685317 139 return wxByte(M_COLDATA->m_green >> SHIFT);
ff7b1510 140}
c801d85f 141
68dda785 142unsigned char wxColour::Blue() const
c801d85f 143{
a1b806b9 144 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
8bbe427f 145
c6685317 146 return wxByte(M_COLDATA->m_blue >> SHIFT);
ff7b1510 147}
c801d85f 148
dc888336
SC
149unsigned char wxColour::Alpha() const
150{
a1b806b9 151 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
dc888336 152
3bbc1d95 153 return M_COLDATA->m_alpha;
dc888336
SC
154}
155
c801d85f
KB
156void wxColour::CalcPixel( GdkColormap *cmap )
157{
a1b806b9 158 if (!IsOk()) return;
8bbe427f 159
d13fd3e4 160 M_COLDATA->AllocColour( cmap );
ff7b1510 161}
c801d85f 162
68dda785 163int wxColour::GetPixel() const
c801d85f 164{
a1b806b9 165 wxCHECK_MSG( IsOk(), 0, wxT("invalid colour") );
8bbe427f 166
1ecc4d80 167 return M_COLDATA->m_color.pixel;
ff7b1510 168}
c801d85f 169
c6685317 170const GdkColor *wxColour::GetColor() const
c801d85f 171{
a1b806b9 172 wxCHECK_MSG( IsOk(), NULL, wxT("invalid colour") );
8bbe427f 173
1ecc4d80 174 return &M_COLDATA->m_color;
ff7b1510 175}
40989e46 176
e86d4e59 177bool wxColour::FromString(const wxString& str)
40989e46
WS
178{
179 GdkColor colGDK;
180 if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) )
181 {
c6685317 182 *this = wxColour(colGDK);
40989e46
WS
183 return true;
184 }
185
186 return wxColourBase::FromString(str);
187}