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