]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
Missing header.
[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
23class wxColourRefData: public wxObjectRefData
24{
47c93b63 25public:
c89f5c02
RR
26 wxColourRefData()
27 {
28 m_color.red = 0;
29 m_color.green = 0;
30 m_color.blue = 0;
31 m_color.pixel = 0;
c4fa282c 32 m_colormap = NULL;
aad6765c 33 m_hasPixel = false;
c89f5c02 34 }
4e6b8309 35
dbb846c6 36 wxColourRefData(const wxColourRefData& data)
d84afea9 37 : wxObjectRefData()
dbb846c6
VZ
38 {
39 m_color = data.m_color;
40 m_colormap = data.m_colormap;
41 m_hasPixel = data.m_hasPixel;
42 }
43
c89f5c02
RR
44 ~wxColourRefData()
45 {
46 FreeColour();
47 }
15248e43 48
68dda785 49 void FreeColour();
d13fd3e4 50 void AllocColour( GdkColormap* cmap );
8bbe427f 51
c801d85f
KB
52 GdkColor m_color;
53 GdkColormap *m_colormap;
54 bool m_hasPixel;
c801d85f
KB
55};
56
68dda785 57void wxColourRefData::FreeColour()
c801d85f 58{
c4fa282c 59 if (m_hasPixel)
47c93b63 60 {
c4fa282c 61 gdk_colormap_free_colors(m_colormap, &m_color, 1);
47c93b63 62 }
ff7b1510 63}
c801d85f 64
d13fd3e4
VZ
65void wxColourRefData::AllocColour( GdkColormap *cmap )
66{
67 if (m_hasPixel && (m_colormap == cmap))
68 return;
69
70 FreeColour();
4e6b8309 71
c4fa282c 72 m_hasPixel = gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE);
d13fd3e4
VZ
73 m_colormap = cmap;
74}
75
c801d85f
KB
76//-----------------------------------------------------------------------------
77
c4fa282c 78#define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
c801d85f 79
3b9faf4c
VS
80// GDK's values are in 0..65535 range, our are in 0..255
81#define SHIFT 8
c801d85f
KB
82
83IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
84
68dda785 85wxColour::~wxColour()
c801d85f 86{
ff7b1510 87}
c801d85f 88
33b64e6f 89bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 90{
4e6b8309 91 if (m_refData == col.m_refData)
aad6765c 92 return true;
15248e43 93
4e6b8309 94 if (!m_refData || !col.m_refData)
aad6765c 95 return false;
15248e43 96
c4fa282c
VZ
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;
ff7b1510 102}
c801d85f 103
c89f5c02
RR
104wxObjectRefData *wxColour::CreateRefData() const
105{
106 return new wxColourRefData;
107}
108
109wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 110{
c89f5c02 111 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 112}
c801d85f 113
40989e46 114void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 115{
c89f5c02 116 AllocExclusive();
4e6b8309 117
1ecc4d80
RR
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;
4e6b8309 122
c4fa282c 123 M_COLDATA->m_colormap = NULL;
aad6765c 124 M_COLDATA->m_hasPixel = false;
ff7b1510 125}
c801d85f 126
68dda785 127unsigned char wxColour::Red() const
c801d85f 128{
223d09f6 129 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 130
1ecc4d80 131 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 132}
c801d85f 133
68dda785 134unsigned char wxColour::Green() const
c801d85f 135{
223d09f6 136 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 137
1ecc4d80 138 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 139}
c801d85f 140
68dda785 141unsigned char wxColour::Blue() const
c801d85f 142{
223d09f6 143 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 144
1ecc4d80 145 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 146}
c801d85f 147
c801d85f
KB
148void wxColour::CalcPixel( GdkColormap *cmap )
149{
1ecc4d80 150 if (!Ok()) return;
8bbe427f 151
d13fd3e4 152 M_COLDATA->AllocColour( cmap );
ff7b1510 153}
c801d85f 154
68dda785 155int wxColour::GetPixel() const
c801d85f 156{
223d09f6 157 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 158
1ecc4d80 159 return M_COLDATA->m_color.pixel;
ff7b1510 160}
c801d85f 161
68dda785 162GdkColor *wxColour::GetColor() const
c801d85f 163{
c4fa282c 164 wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
8bbe427f 165
1ecc4d80 166 return &M_COLDATA->m_color;
ff7b1510 167}
40989e46
WS
168
169bool wxColour::FromString(const wxChar *str)
170{
171 GdkColor colGDK;
172 if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) )
173 {
174 UnRef();
175
176 m_refData = new wxColourRefData;
177 M_COLDATA->m_color = colGDK;
178 return true;
179 }
180
181 return wxColourBase::FromString(str);
182}