]>
Commit | Line | Data |
---|---|---|
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 | wxColourRefData(); | |
29 | ~wxColourRefData(); | |
30 | ||
31 | void FreeColour(); | |
32 | ||
33 | public: | |
34 | GdkColor m_color; | |
35 | GdkColormap *m_colormap; | |
36 | bool m_hasPixel; | |
37 | ||
38 | friend class 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_colormap) | |
59 | { | |
60 | #ifdef __WXGTK20__ | |
61 | if ((m_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
62 | (m_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
63 | #else | |
64 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) m_colormap; | |
65 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
66 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
67 | #endif | |
68 | { | |
69 | // What happens if the colour has not been allocated | |
70 | // anew but has been found? RR. | |
71 | gdk_colormap_free_colors( m_colormap, &m_color, 1 ); | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
76 | //----------------------------------------------------------------------------- | |
77 | ||
78 | #define M_COLDATA ((wxColourRefData *)m_refData) | |
79 | ||
80 | #define SHIFT (8*(sizeof(short int)-sizeof(char))) | |
81 | ||
82 | IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) | |
83 | ||
84 | wxColour::wxColour() | |
85 | { | |
86 | } | |
87 | ||
88 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) | |
89 | { | |
90 | m_refData = new wxColourRefData(); | |
91 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
92 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
93 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
94 | M_COLDATA->m_color.pixel = 0; | |
95 | } | |
96 | ||
97 | void wxColour::InitFromName( const wxString &colourName ) | |
98 | { | |
99 | wxNode *node = (wxNode *) NULL; | |
100 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) | |
101 | { | |
102 | wxColour *col = (wxColour*)node->Data(); | |
103 | UnRef(); | |
104 | if (col) Ref( *col ); | |
105 | } | |
106 | else | |
107 | { | |
108 | m_refData = new wxColourRefData(); | |
109 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) | |
110 | { | |
111 | // VZ: asserts are good in general but this one is triggered by | |
112 | // calling wxColourDatabase::FindColour() with an | |
113 | // unrecognized colour name and this can't be avoided from the | |
114 | // user code, so don't give it here | |
115 | // | |
116 | // a better solution would be to changed code in FindColour() | |
117 | ||
118 | //wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); | |
119 | ||
120 | delete m_refData; | |
121 | m_refData = (wxObjectRefData *) NULL; | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | wxColour::wxColour( const wxColour& col ) | |
127 | { | |
128 | Ref( col ); | |
129 | } | |
130 | ||
131 | wxColour::~wxColour() | |
132 | { | |
133 | } | |
134 | ||
135 | wxColour& wxColour::operator = ( const wxColour& col ) | |
136 | { | |
137 | if (*this == col) return (*this); | |
138 | Ref( col ); | |
139 | return *this; | |
140 | } | |
141 | ||
142 | bool wxColour::operator == ( const wxColour& col ) const | |
143 | { | |
144 | if (m_refData == col.m_refData) return TRUE; | |
145 | ||
146 | if (!m_refData) return FALSE; | |
147 | if (!col.m_refData) return FALSE; | |
148 | ||
149 | GdkColor *own = &(((wxColourRefData*)m_refData)->m_color); | |
150 | GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
151 | if (own->red != other->red) return FALSE; | |
152 | if (own->blue != other->blue) return FALSE; | |
153 | if (own->green != other->green) return FALSE; | |
154 | ||
155 | return TRUE; | |
156 | } | |
157 | ||
158 | bool wxColour::operator != ( const wxColour& col) const | |
159 | { | |
160 | return !(*this == col); | |
161 | } | |
162 | ||
163 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) | |
164 | { | |
165 | UnRef(); | |
166 | m_refData = new wxColourRefData(); | |
167 | M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; | |
168 | M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; | |
169 | M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; | |
170 | M_COLDATA->m_color.pixel = 0; | |
171 | } | |
172 | ||
173 | unsigned char wxColour::Red() const | |
174 | { | |
175 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
176 | ||
177 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); | |
178 | } | |
179 | ||
180 | unsigned char wxColour::Green() const | |
181 | { | |
182 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
183 | ||
184 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); | |
185 | } | |
186 | ||
187 | unsigned char wxColour::Blue() const | |
188 | { | |
189 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
190 | ||
191 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); | |
192 | } | |
193 | ||
194 | bool wxColour::Ok() const | |
195 | { | |
196 | return (m_refData != NULL); | |
197 | } | |
198 | ||
199 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
200 | { | |
201 | if (!Ok()) return; | |
202 | ||
203 | if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return; | |
204 | ||
205 | M_COLDATA->FreeColour(); | |
206 | ||
207 | #ifdef __WXGTK20__ | |
208 | if ((cmap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
209 | (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
210 | #else | |
211 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; | |
212 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
213 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
214 | #endif | |
215 | { | |
216 | M_COLDATA->m_hasPixel = gdk_colormap_alloc_color( cmap, &M_COLDATA->m_color, FALSE, TRUE ); | |
217 | } | |
218 | else | |
219 | { | |
220 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); | |
221 | } | |
222 | ||
223 | M_COLDATA->m_colormap = cmap; | |
224 | } | |
225 | ||
226 | int wxColour::GetPixel() const | |
227 | { | |
228 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); | |
229 | ||
230 | return M_COLDATA->m_color.pixel; | |
231 | } | |
232 | ||
233 | GdkColor *wxColour::GetColor() const | |
234 | { | |
235 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); | |
236 | ||
237 | return &M_COLDATA->m_color; | |
238 | } | |
239 | ||
240 |