1. wxTextCtrl::SetBackgroundColour() now works
[wxWidgets.git] / src / gtk1 / colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: colour.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11
12 #ifdef __GNUG__
13 #pragma implementation "colour.h"
14 #endif
15
16 #include "wx/gdicmn.h"
17
18 #ifdef wxUSE_GDK_IMLIB
19 #include "../gdk_imlib/gdk_imlib.h"
20 #endif
21
22 //-----------------------------------------------------------------------------
23 // wxColour
24 //-----------------------------------------------------------------------------
25
26 class wxColourRefData: public wxObjectRefData
27 {
28 public:
29
30 wxColourRefData();
31 ~wxColourRefData();
32 void FreeColour();
33
34 GdkColor m_color;
35 GdkColormap *m_colormap;
36 bool m_hasPixel;
37
38 friend wxColour;
39 };
40
41 wxColourRefData::wxColourRefData()
42 {
43 m_color.red = 0;
44 m_color.green = 0;
45 m_color.blue = 0;
46 m_color.pixel = 0;
47 m_colormap = (GdkColormap *) NULL;
48 m_hasPixel = FALSE;
49 }
50
51 wxColourRefData::~wxColourRefData()
52 {
53 FreeColour();
54 }
55
56 void wxColourRefData::FreeColour()
57 {
58 // if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
59 }
60
61 //-----------------------------------------------------------------------------
62
63 #define M_COLDATA ((wxColourRefData *)m_refData)
64
65 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
66
67 IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
68
69 wxColour::wxColour()
70 {
71 }
72
73 wxColour::wxColour( char red, char green, char blue )
74 {
75 m_refData = new wxColourRefData();
76 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
77 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
78 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
79 M_COLDATA->m_color.pixel = 0;
80 }
81
82 void wxColour::InitFromName( const wxString &colourName )
83 {
84 wxNode *node = (wxNode *) NULL;
85 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
86 {
87 wxColour *col = (wxColour*)node->Data();
88 UnRef();
89 if (col) Ref( *col );
90 }
91 else
92 {
93 m_refData = new wxColourRefData();
94 if (!gdk_color_parse( colourName, &M_COLDATA->m_color ))
95 {
96 wxFAIL_MSG( "wxColour: couldn't find colour" );
97 delete m_refData;
98 m_refData = (wxObjectRefData *) NULL;
99 }
100 }
101 }
102
103 wxColour::wxColour( const wxColour& col )
104 {
105 Ref( col );
106 }
107
108 wxColour::wxColour( const wxColour* col )
109 {
110 if (col) Ref( *col );
111 }
112
113 wxColour::~wxColour()
114 {
115 }
116
117 wxColour& wxColour::operator = ( const wxColour& col )
118 {
119 if (*this == col) return (*this);
120 Ref( col );
121 return *this;
122 }
123
124 wxColour& wxColour::operator = ( const wxString& colourName )
125 {
126 UnRef();
127 wxNode *node = (wxNode *) NULL;
128 if ((wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
129 {
130 wxColour *col = (wxColour*)node->Data();
131 if (col) Ref( *col );
132 }
133 else
134 {
135 m_refData = new wxColourRefData();
136 if (!gdk_color_parse( colourName, &M_COLDATA->m_color ))
137 {
138 wxFAIL_MSG( "wxColour: couldn't find colour" );
139 delete m_refData;
140 m_refData = (wxObjectRefData *) NULL;
141 }
142 }
143 return *this;
144 }
145
146 bool wxColour::operator == ( const wxColour& col )
147 {
148 return m_refData == col.m_refData;
149 }
150
151 bool wxColour::operator != ( const wxColour& col)
152 {
153 return m_refData != col.m_refData;
154 }
155
156 void wxColour::Set( const unsigned char red, const unsigned char green, const unsigned char blue )
157 {
158 UnRef();
159 m_refData = new wxColourRefData();
160 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
161 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
162 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
163 M_COLDATA->m_color.pixel = 0;
164 }
165
166 unsigned char wxColour::Red() const
167 {
168 if (!Ok())
169 {
170 wxFAIL_MSG( "invalid colour" );
171 return 0;
172 }
173
174 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
175 }
176
177 unsigned char wxColour::Green() const
178 {
179 if (!Ok())
180 {
181 wxFAIL_MSG( "invalid colour" );
182 return 0;
183 }
184
185 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
186 }
187
188 unsigned char wxColour::Blue() const
189 {
190 if (!Ok())
191 {
192 wxFAIL_MSG( "invalid colour" );
193 return 0;
194 }
195
196 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
197 }
198
199 bool wxColour::Ok() const
200 {
201 return (m_refData != NULL);
202 }
203
204 void wxColour::CalcPixel( GdkColormap *cmap )
205 {
206 if (!Ok()) return;
207
208 if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return;
209 M_COLDATA->FreeColour();
210
211 #ifdef wxUSE_GDK_IMLIB
212
213 int r = M_COLDATA->m_color.red >> SHIFT;
214 int g = M_COLDATA->m_color.green >> SHIFT;
215 int b = M_COLDATA->m_color.blue >> SHIFT;
216 M_COLDATA->m_hasPixel = TRUE;
217 M_COLDATA->m_color.pixel = gdk_imlib_best_color_match( &r, &g, &b );
218
219 #else
220
221 M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color );
222
223 #endif
224
225 M_COLDATA->m_colormap = cmap;
226 }
227
228 int wxColour::GetPixel() const
229 {
230 if (!Ok()) return 0;
231
232 return M_COLDATA->m_color.pixel;
233 }
234
235 GdkColor *wxColour::GetColor() const
236 {
237 if (!Ok()) return (GdkColor *) NULL;
238
239 return &M_COLDATA->m_color;
240 }
241
242