]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: colour.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
8bbe427f | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "colour.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/gdicmn.h" | |
16 | ||
071a2d78 | 17 | #include <gdk/gdk.h> |
307fc8d5 | 18 | #include <gdk/gdkx.h> |
071a2d78 | 19 | #include <gdk/gdkprivate.h> |
c801d85f KB |
20 | |
21 | //----------------------------------------------------------------------------- | |
22 | // wxColour | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | class wxColourRefData: public wxObjectRefData | |
26 | { | |
47c93b63 | 27 | public: |
68dda785 VZ |
28 | wxColourRefData(); |
29 | ~wxColourRefData(); | |
15248e43 | 30 | |
68dda785 | 31 | void FreeColour(); |
8bbe427f | 32 | |
47c93b63 | 33 | public: |
c801d85f KB |
34 | GdkColor m_color; |
35 | GdkColormap *m_colormap; | |
36 | bool m_hasPixel; | |
8bbe427f | 37 | |
f6bcfd97 | 38 | friend class wxColour; |
c801d85f KB |
39 | }; |
40 | ||
68dda785 | 41 | wxColourRefData::wxColourRefData() |
c801d85f | 42 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 49 | } |
c801d85f | 50 | |
68dda785 | 51 | wxColourRefData::~wxColourRefData() |
c801d85f | 52 | { |
1ecc4d80 | 53 | FreeColour(); |
ff7b1510 | 54 | } |
c801d85f | 55 | |
68dda785 | 56 | void wxColourRefData::FreeColour() |
c801d85f | 57 | { |
47c93b63 RR |
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 | } | |
ff7b1510 | 74 | } |
c801d85f KB |
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 | ||
68dda785 | 84 | wxColour::wxColour() |
c801d85f | 85 | { |
ff7b1510 | 86 | } |
c801d85f | 87 | |
4b5f3fe6 | 88 | wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 89 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 95 | } |
8bbe427f | 96 | |
68dda785 | 97 | void wxColour::InitFromName( const wxString &colourName ) |
c801d85f | 98 | { |
1ecc4d80 RR |
99 | wxNode *node = (wxNode *) NULL; |
100 | if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) ) | |
c801d85f | 101 | { |
1ecc4d80 RR |
102 | wxColour *col = (wxColour*)node->Data(); |
103 | UnRef(); | |
104 | if (col) Ref( *col ); | |
105 | } | |
106 | else | |
107 | { | |
108 | m_refData = new wxColourRefData(); | |
93c5dd39 | 109 | if (!gdk_color_parse( colourName.mb_str(), &M_COLDATA->m_color )) |
1ecc4d80 | 110 | { |
15248e43 VZ |
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 | ||
1ecc4d80 RR |
120 | delete m_refData; |
121 | m_refData = (wxObjectRefData *) NULL; | |
122 | } | |
ff7b1510 | 123 | } |
ff7b1510 | 124 | } |
c801d85f KB |
125 | |
126 | wxColour::wxColour( const wxColour& col ) | |
8bbe427f | 127 | { |
1ecc4d80 | 128 | Ref( col ); |
ff7b1510 | 129 | } |
c801d85f | 130 | |
68dda785 | 131 | wxColour::~wxColour() |
c801d85f | 132 | { |
ff7b1510 | 133 | } |
c801d85f | 134 | |
8bbe427f VZ |
135 | wxColour& wxColour::operator = ( const wxColour& col ) |
136 | { | |
1ecc4d80 RR |
137 | if (*this == col) return (*this); |
138 | Ref( col ); | |
139 | return *this; | |
ff7b1510 | 140 | } |
c801d85f | 141 | |
33b64e6f | 142 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 143 | { |
2b62ab35 | 144 | if (m_refData == col.m_refData) return TRUE; |
15248e43 | 145 | |
2b62ab35 RR |
146 | if (!m_refData) return FALSE; |
147 | if (!col.m_refData) return FALSE; | |
15248e43 | 148 | |
2b62ab35 RR |
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; | |
15248e43 | 154 | |
2b62ab35 | 155 | return TRUE; |
ff7b1510 | 156 | } |
c801d85f | 157 | |
33b64e6f | 158 | bool wxColour::operator != ( const wxColour& col) const |
8bbe427f | 159 | { |
f6bcfd97 | 160 | return !(*this == col); |
ff7b1510 | 161 | } |
c801d85f | 162 | |
4b5f3fe6 | 163 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 164 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 171 | } |
c801d85f | 172 | |
68dda785 | 173 | unsigned char wxColour::Red() const |
c801d85f | 174 | { |
223d09f6 | 175 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 176 | |
1ecc4d80 | 177 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 178 | } |
c801d85f | 179 | |
68dda785 | 180 | unsigned char wxColour::Green() const |
c801d85f | 181 | { |
223d09f6 | 182 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 183 | |
1ecc4d80 | 184 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 185 | } |
c801d85f | 186 | |
68dda785 | 187 | unsigned char wxColour::Blue() const |
c801d85f | 188 | { |
223d09f6 | 189 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 190 | |
1ecc4d80 | 191 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 192 | } |
c801d85f | 193 | |
68dda785 | 194 | bool wxColour::Ok() const |
c801d85f | 195 | { |
1ecc4d80 | 196 | return (m_refData != NULL); |
ff7b1510 | 197 | } |
c801d85f KB |
198 | |
199 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
200 | { | |
1ecc4d80 | 201 | if (!Ok()) return; |
8bbe427f | 202 | |
1ecc4d80 | 203 | if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return; |
15248e43 | 204 | |
1ecc4d80 | 205 | M_COLDATA->FreeColour(); |
8bbe427f | 206 | |
307fc8d5 OK |
207 | #ifdef __WXGTK20__ |
208 | if ((cmap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
209 | (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
210 | #else | |
f6fcbb63 RR |
211 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; |
212 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
213 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
307fc8d5 | 214 | #endif |
01111366 | 215 | { |
47c93b63 | 216 | M_COLDATA->m_hasPixel = gdk_colormap_alloc_color( cmap, &M_COLDATA->m_color, FALSE, TRUE ); |
f6fcbb63 | 217 | } |
1ecc4d80 | 218 | else |
15248e43 | 219 | { |
1ecc4d80 RR |
220 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); |
221 | } | |
8bbe427f | 222 | |
1ecc4d80 | 223 | M_COLDATA->m_colormap = cmap; |
ff7b1510 | 224 | } |
c801d85f | 225 | |
68dda785 | 226 | int wxColour::GetPixel() const |
c801d85f | 227 | { |
223d09f6 | 228 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 229 | |
1ecc4d80 | 230 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 231 | } |
c801d85f | 232 | |
68dda785 | 233 | GdkColor *wxColour::GetColor() const |
c801d85f | 234 | { |
223d09f6 | 235 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 236 | |
1ecc4d80 | 237 | return &M_COLDATA->m_color; |
ff7b1510 | 238 | } |
c801d85f KB |
239 | |
240 |