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