]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/colour.cpp
Use wxGtkString here too
[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"
2b5f62a0 14#include "wx/gtk/private.h"
c801d85f 15
071a2d78 16#include <gdk/gdk.h>
c801d85f
KB
17
18//-----------------------------------------------------------------------------
19// wxColour
20//-----------------------------------------------------------------------------
21
22class wxColourRefData: public wxObjectRefData
23{
47c93b63 24public:
c89f5c02
RR
25 wxColourRefData()
26 {
27 m_color.red = 0;
28 m_color.green = 0;
29 m_color.blue = 0;
30 m_color.pixel = 0;
c4fa282c 31 m_colormap = NULL;
aad6765c 32 m_hasPixel = false;
c89f5c02 33 }
4e6b8309 34
dbb846c6 35 wxColourRefData(const wxColourRefData& data)
d84afea9 36 : wxObjectRefData()
dbb846c6
VZ
37 {
38 m_color = data.m_color;
39 m_colormap = data.m_colormap;
40 m_hasPixel = data.m_hasPixel;
41 }
42
c89f5c02
RR
43 ~wxColourRefData()
44 {
45 FreeColour();
46 }
15248e43 47
68dda785 48 void FreeColour();
d13fd3e4 49 void AllocColour( GdkColormap* cmap );
8bbe427f 50
c801d85f
KB
51 GdkColor m_color;
52 GdkColormap *m_colormap;
53 bool m_hasPixel;
c801d85f
KB
54};
55
68dda785 56void wxColourRefData::FreeColour()
c801d85f 57{
c4fa282c 58 if (m_hasPixel)
47c93b63 59 {
c4fa282c 60 gdk_colormap_free_colors(m_colormap, &m_color, 1);
47c93b63 61 }
ff7b1510 62}
c801d85f 63
d13fd3e4
VZ
64void wxColourRefData::AllocColour( GdkColormap *cmap )
65{
66 if (m_hasPixel && (m_colormap == cmap))
67 return;
68
69 FreeColour();
4e6b8309 70
c4fa282c 71 m_hasPixel = gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE);
d13fd3e4
VZ
72 m_colormap = cmap;
73}
74
c801d85f
KB
75//-----------------------------------------------------------------------------
76
c4fa282c 77#define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
c801d85f 78
3b9faf4c
VS
79// GDK's values are in 0..65535 range, our are in 0..255
80#define SHIFT 8
c801d85f
KB
81
82IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
83
4b5f3fe6 84wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 85{
c4fa282c 86 m_refData = new wxColourRefData;
1ecc4d80
RR
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;
ff7b1510 91}
8bbe427f 92
4e6b8309
VZ
93/* static */
94wxColour wxColour::CreateByName(const wxString& name)
95{
96 wxColour col;
97
98 GdkColor colGDK;
99 if ( gdk_color_parse( wxGTK_CONV( name ), &colGDK ) )
100 {
101 wxColourRefData *refData = new wxColourRefData;
102 refData->m_color = colGDK;
103 col.m_refData = refData;
104 }
105
106 return col;
107}
222ed1d6
MB
108
109
68dda785 110void wxColour::InitFromName( const wxString &colourName )
c801d85f 111{
4e6b8309 112 // check the cache first
4e6b8309 113 if ( wxTheColourDatabase )
c801d85f 114 {
c4fa282c 115 *this = wxTheColourDatabase->Find(colourName);
1ecc4d80 116 }
15248e43 117
c4fa282c 118 if ( !Ok() )
4e6b8309 119 {
c4fa282c 120 *this = CreateByName(colourName);
4e6b8309 121 }
15248e43 122
c4fa282c 123 if ( !Ok() )
4e6b8309
VZ
124 {
125 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
ff7b1510 126 }
ff7b1510 127}
c801d85f 128
68dda785 129wxColour::~wxColour()
c801d85f 130{
ff7b1510 131}
c801d85f 132
33b64e6f 133bool wxColour::operator == ( const wxColour& col ) const
8bbe427f 134{
4e6b8309 135 if (m_refData == col.m_refData)
aad6765c 136 return true;
15248e43 137
4e6b8309 138 if (!m_refData || !col.m_refData)
aad6765c 139 return false;
15248e43 140
c4fa282c
VZ
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;
ff7b1510 146}
c801d85f 147
c89f5c02
RR
148wxObjectRefData *wxColour::CreateRefData() const
149{
150 return new wxColourRefData;
151}
152
153wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
8bbe427f 154{
c89f5c02 155 return new wxColourRefData(*(wxColourRefData *)data);
ff7b1510 156}
c801d85f 157
4b5f3fe6 158void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 159{
c89f5c02 160 AllocExclusive();
4e6b8309 161
1ecc4d80
RR
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;
4e6b8309 166
c4fa282c 167 M_COLDATA->m_colormap = NULL;
aad6765c 168 M_COLDATA->m_hasPixel = false;
ff7b1510 169}
c801d85f 170
68dda785 171unsigned char wxColour::Red() const
c801d85f 172{
223d09f6 173 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 174
1ecc4d80 175 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 176}
c801d85f 177
68dda785 178unsigned char wxColour::Green() const
c801d85f 179{
223d09f6 180 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 181
1ecc4d80 182 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 183}
c801d85f 184
68dda785 185unsigned char wxColour::Blue() const
c801d85f 186{
223d09f6 187 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 188
1ecc4d80 189 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 190}
c801d85f 191
c801d85f
KB
192void wxColour::CalcPixel( GdkColormap *cmap )
193{
1ecc4d80 194 if (!Ok()) return;
8bbe427f 195
d13fd3e4 196 M_COLDATA->AllocColour( cmap );
ff7b1510 197}
c801d85f 198
68dda785 199int wxColour::GetPixel() const
c801d85f 200{
223d09f6 201 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
8bbe427f 202
1ecc4d80 203 return M_COLDATA->m_color.pixel;
ff7b1510 204}
c801d85f 205
68dda785 206GdkColor *wxColour::GetColor() const
c801d85f 207{
c4fa282c 208 wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
8bbe427f 209
1ecc4d80 210 return &M_COLDATA->m_color;
ff7b1510 211}