]>
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 | ||
c89f5c02 RR |
38 | ~wxColourRefData() |
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 | |
40989e46 | 115 | void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 116 | { |
c6685317 | 117 | UnRef(); |
4e6b8309 | 118 | |
c6685317 PC |
119 | m_refData = new wxColourRefData( |
120 | (guint16(red) << SHIFT) + red, | |
121 | (guint16(green) << SHIFT) + green, | |
122 | (guint16(blue) << SHIFT) + blue); | |
ff7b1510 | 123 | } |
c801d85f | 124 | |
68dda785 | 125 | unsigned char wxColour::Red() const |
c801d85f | 126 | { |
223d09f6 | 127 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 128 | |
c6685317 | 129 | return wxByte(M_COLDATA->m_red >> SHIFT); |
ff7b1510 | 130 | } |
c801d85f | 131 | |
68dda785 | 132 | unsigned char wxColour::Green() const |
c801d85f | 133 | { |
223d09f6 | 134 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 135 | |
c6685317 | 136 | return wxByte(M_COLDATA->m_green >> SHIFT); |
ff7b1510 | 137 | } |
c801d85f | 138 | |
68dda785 | 139 | unsigned char wxColour::Blue() const |
c801d85f | 140 | { |
223d09f6 | 141 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 142 | |
c6685317 | 143 | return wxByte(M_COLDATA->m_blue >> SHIFT); |
ff7b1510 | 144 | } |
c801d85f | 145 | |
c801d85f KB |
146 | void wxColour::CalcPixel( GdkColormap *cmap ) |
147 | { | |
1ecc4d80 | 148 | if (!Ok()) return; |
8bbe427f | 149 | |
d13fd3e4 | 150 | M_COLDATA->AllocColour( cmap ); |
ff7b1510 | 151 | } |
c801d85f | 152 | |
68dda785 | 153 | int wxColour::GetPixel() const |
c801d85f | 154 | { |
223d09f6 | 155 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 156 | |
1ecc4d80 | 157 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 158 | } |
c801d85f | 159 | |
4f558e23 | 160 | #ifdef __WXGTK24__ |
c6685317 | 161 | const GdkColor *wxColour::GetColor() const |
4f558e23 PC |
162 | #else |
163 | GdkColor *wxColour::GetColor() const | |
164 | #endif | |
c801d85f | 165 | { |
c4fa282c | 166 | wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") ); |
8bbe427f | 167 | |
1ecc4d80 | 168 | return &M_COLDATA->m_color; |
ff7b1510 | 169 | } |
40989e46 WS |
170 | |
171 | bool wxColour::FromString(const wxChar *str) | |
172 | { | |
173 | GdkColor colGDK; | |
174 | if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) | |
175 | { | |
c6685317 | 176 | *this = wxColour(colGDK); |
40989e46 WS |
177 | return true; |
178 | } | |
179 | ||
180 | return wxColourBase::FromString(str); | |
181 | } |