]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/gtk/colour.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #include "wx/colour.h" | |
14 | ||
15 | #include "wx/gtk/private.h" | |
16 | ||
17 | #include <gdk/gdk.h> | |
18 | ||
19 | //----------------------------------------------------------------------------- | |
20 | // wxColour | |
21 | //----------------------------------------------------------------------------- | |
22 | ||
23 | class wxColourRefData : public wxGDIRefData | |
24 | { | |
25 | public: | |
26 | wxColourRefData(guint16 red, guint16 green, guint16 blue, wxByte alpha = 0xff) | |
27 | { | |
28 | m_color.red = | |
29 | m_red = red; | |
30 | m_color.green = | |
31 | m_green = green; | |
32 | m_color.blue = | |
33 | m_blue = blue; | |
34 | m_alpha = alpha; | |
35 | m_color.pixel = 0; | |
36 | m_colormap = NULL; | |
37 | } | |
38 | ||
39 | virtual ~wxColourRefData() | |
40 | { | |
41 | FreeColour(); | |
42 | } | |
43 | ||
44 | void FreeColour(); | |
45 | void AllocColour( GdkColormap* cmap ); | |
46 | ||
47 | GdkColor m_color; | |
48 | GdkColormap *m_colormap; | |
49 | // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies | |
50 | guint16 m_red; | |
51 | guint16 m_green; | |
52 | guint16 m_blue; | |
53 | wxByte m_alpha; | |
54 | ||
55 | DECLARE_NO_COPY_CLASS(wxColourRefData) | |
56 | }; | |
57 | ||
58 | void wxColourRefData::FreeColour() | |
59 | { | |
60 | if (m_colormap) | |
61 | { | |
62 | gdk_colormap_free_colors(m_colormap, &m_color, 1); | |
63 | m_colormap = NULL; | |
64 | m_color.pixel = 0; | |
65 | } | |
66 | } | |
67 | ||
68 | void wxColourRefData::AllocColour( GdkColormap *cmap ) | |
69 | { | |
70 | if (m_colormap != cmap) | |
71 | { | |
72 | FreeColour(); | |
73 | ||
74 | m_color.red = m_red; | |
75 | m_color.green = m_green; | |
76 | m_color.blue = m_blue; | |
77 | if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE)) | |
78 | { | |
79 | m_colormap = cmap; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | //----------------------------------------------------------------------------- | |
85 | ||
86 | #define M_COLDATA static_cast<wxColourRefData*>(m_refData) | |
87 | ||
88 | // GDK's values are in 0..65535 range, ours are in 0..255 | |
89 | #define SHIFT 8 | |
90 | ||
91 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
92 | ||
93 | wxColour::wxColour(const GdkColor& gdkColor) | |
94 | { | |
95 | m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue); | |
96 | } | |
97 | ||
98 | wxColour::~wxColour() | |
99 | { | |
100 | } | |
101 | ||
102 | bool wxColour::operator == ( const wxColour& col ) const | |
103 | { | |
104 | if (m_refData == col.m_refData) | |
105 | return true; | |
106 | ||
107 | if (!m_refData || !col.m_refData) | |
108 | return false; | |
109 | ||
110 | wxColourRefData* refData = M_COLDATA; | |
111 | wxColourRefData* that_refData = static_cast<wxColourRefData*>(col.m_refData); | |
112 | return refData->m_red == that_refData->m_red && | |
113 | refData->m_green == that_refData->m_green && | |
114 | refData->m_blue == that_refData->m_blue && | |
115 | refData->m_alpha == that_refData->m_alpha; | |
116 | } | |
117 | ||
118 | void wxColour::InitRGBA(unsigned char red, unsigned char green, unsigned char blue, | |
119 | unsigned char alpha) | |
120 | { | |
121 | UnRef(); | |
122 | ||
123 | m_refData = new wxColourRefData( | |
124 | (guint16(red) << SHIFT) + red, | |
125 | (guint16(green) << SHIFT) + green, | |
126 | (guint16(blue) << SHIFT) + blue, | |
127 | alpha); | |
128 | } | |
129 | ||
130 | unsigned char wxColour::Red() const | |
131 | { | |
132 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
133 | ||
134 | return wxByte(M_COLDATA->m_red >> SHIFT); | |
135 | } | |
136 | ||
137 | unsigned char wxColour::Green() const | |
138 | { | |
139 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
140 | ||
141 | return wxByte(M_COLDATA->m_green >> SHIFT); | |
142 | } | |
143 | ||
144 | unsigned char wxColour::Blue() const | |
145 | { | |
146 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
147 | ||
148 | return wxByte(M_COLDATA->m_blue >> SHIFT); | |
149 | } | |
150 | ||
151 | unsigned char wxColour::Alpha() const | |
152 | { | |
153 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
154 | ||
155 | return M_COLDATA->m_alpha; | |
156 | } | |
157 | ||
158 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
159 | { | |
160 | if (!Ok()) return; | |
161 | ||
162 | M_COLDATA->AllocColour( cmap ); | |
163 | } | |
164 | ||
165 | int wxColour::GetPixel() const | |
166 | { | |
167 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
168 | ||
169 | return M_COLDATA->m_color.pixel; | |
170 | } | |
171 | ||
172 | const GdkColor *wxColour::GetColor() const | |
173 | { | |
174 | wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") ); | |
175 | ||
176 | return &M_COLDATA->m_color; | |
177 | } | |
178 | ||
179 | bool wxColour::FromString(const wxString& str) | |
180 | { | |
181 | GdkColor colGDK; | |
182 | if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) | |
183 | { | |
184 | *this = wxColour(colGDK); | |
185 | return true; | |
186 | } | |
187 | ||
188 | return wxColourBase::FromString(str); | |
189 | } |