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