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