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