]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
Allow user code to override key events in generic wxListCtrl.
[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
KB
90
91IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
92
c6685317
PC
93wxColour::wxColour(const GdkColor& gdkColor)
94{
3bbc1d95 95 m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue);
c6685317
PC
96}
97
68dda785 98wxColour::~wxColour()
c801d85f 99{
ff7b1510 100}
c801d85f 101
33b64e6f 102bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 103{
4e6b8309 104 if (m_refData == col.m_refData)
aad6765c 105 return true;
15248e43 106
4e6b8309 107 if (!m_refData || !col.m_refData)
aad6765c 108 return false;
15248e43 109
c6685317 110 wxColourRefData* refData = M_COLDATA;
5c33522f 111 wxColourRefData* that_refData = static_cast<wxColourRefData*>(col.m_refData);
c6685317
PC
112 return refData->m_red == that_refData->m_red &&
113 refData->m_green == that_refData->m_green &&
dc888336
SC
114 refData->m_blue == that_refData->m_blue &&
115 refData->m_alpha == that_refData->m_alpha;
ff7b1510 116}
c801d85f 117
aea95b1c 118void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue,
dc888336 119 unsigned char alpha)
c801d85f 120{
c6685317 121 UnRef();
4e6b8309 122
c6685317
PC
123 m_refData = new wxColourRefData(
124 (guint16(red) << SHIFT) + red,
125 (guint16(green) << SHIFT) + green,
dc888336 126 (guint16(blue) << SHIFT) + blue,
3bbc1d95 127 alpha);
ff7b1510 128}
c801d85f 129
68dda785 130unsigned char wxColour::Red() const
c801d85f 131{
223d09f6 132 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 133
c6685317 134 return wxByte(M_COLDATA->m_red >> SHIFT);
ff7b1510 135}
c801d85f 136
68dda785 137unsigned char wxColour::Green() const
c801d85f 138{
223d09f6 139 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 140
c6685317 141 return wxByte(M_COLDATA->m_green >> SHIFT);
ff7b1510 142}
c801d85f 143
68dda785 144unsigned char wxColour::Blue() const
c801d85f 145{
223d09f6 146 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 147
c6685317 148 return wxByte(M_COLDATA->m_blue >> SHIFT);
ff7b1510 149}
c801d85f 150
dc888336
SC
151unsigned char wxColour::Alpha() const
152{
153 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
154
3bbc1d95 155 return M_COLDATA->m_alpha;
dc888336
SC
156}
157
c801d85f
KB
158void wxColour::CalcPixel( GdkColormap *cmap )
159{
1ecc4d80 160 if (!Ok()) return;
8bbe427f 161
d13fd3e4 162 M_COLDATA->AllocColour( cmap );
ff7b1510 163}
c801d85f 164
68dda785 165int wxColour::GetPixel() const
c801d85f 166{
223d09f6 167 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 168
1ecc4d80 169 return M_COLDATA->m_color.pixel;
ff7b1510 170}
c801d85f 171
c6685317 172const GdkColor *wxColour::GetColor() const
c801d85f 173{
c4fa282c 174 wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
8bbe427f 175
1ecc4d80 176 return &M_COLDATA->m_color;
ff7b1510 177}
40989e46 178
e86d4e59 179bool wxColour::FromString(const wxString& str)
40989e46
WS
180{
181 GdkColor colGDK;
182 if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) )
183 {
c6685317 184 *this = wxColour(colGDK);
40989e46
WS
185 return true;
186 }
187
188 return wxColourBase::FromString(str);
189}