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(); | |
47c93b63 | 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 | { |
223d09f6 | 111 | wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); |
1ecc4d80 RR |
112 | |
113 | delete m_refData; | |
114 | m_refData = (wxObjectRefData *) NULL; | |
115 | } | |
ff7b1510 | 116 | } |
ff7b1510 | 117 | } |
c801d85f KB |
118 | |
119 | wxColour::wxColour( const wxColour& col ) | |
8bbe427f | 120 | { |
1ecc4d80 | 121 | Ref( col ); |
ff7b1510 | 122 | } |
c801d85f | 123 | |
68dda785 | 124 | wxColour::~wxColour() |
c801d85f | 125 | { |
ff7b1510 | 126 | } |
c801d85f | 127 | |
8bbe427f VZ |
128 | wxColour& wxColour::operator = ( const wxColour& col ) |
129 | { | |
1ecc4d80 RR |
130 | if (*this == col) return (*this); |
131 | Ref( col ); | |
132 | return *this; | |
ff7b1510 | 133 | } |
c801d85f | 134 | |
33b64e6f | 135 | bool wxColour::operator == ( const wxColour& col ) const |
8bbe427f | 136 | { |
2b62ab35 RR |
137 | if (m_refData == col.m_refData) return TRUE; |
138 | ||
139 | if (!m_refData) return FALSE; | |
140 | if (!col.m_refData) return FALSE; | |
141 | ||
142 | GdkColor *own = &(((wxColourRefData*)m_refData)->m_color); | |
143 | GdkColor *other = &(((wxColourRefData*)col.m_refData)->m_color); | |
144 | if (own->red != other->red) return FALSE; | |
145 | if (own->blue != other->blue) return FALSE; | |
146 | if (own->green != other->green) return FALSE; | |
147 | ||
148 | return TRUE; | |
ff7b1510 | 149 | } |
c801d85f | 150 | |
33b64e6f | 151 | bool wxColour::operator != ( const wxColour& col) const |
8bbe427f | 152 | { |
f6bcfd97 | 153 | return !(*this == col); |
ff7b1510 | 154 | } |
c801d85f | 155 | |
4b5f3fe6 | 156 | void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue ) |
c801d85f | 157 | { |
1ecc4d80 RR |
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; | |
ff7b1510 | 164 | } |
c801d85f | 165 | |
68dda785 | 166 | unsigned char wxColour::Red() const |
c801d85f | 167 | { |
223d09f6 | 168 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 169 | |
1ecc4d80 | 170 | return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); |
ff7b1510 | 171 | } |
c801d85f | 172 | |
68dda785 | 173 | unsigned char wxColour::Green() const |
c801d85f | 174 | { |
223d09f6 | 175 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 176 | |
1ecc4d80 | 177 | return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); |
ff7b1510 | 178 | } |
c801d85f | 179 | |
68dda785 | 180 | unsigned char wxColour::Blue() const |
c801d85f | 181 | { |
223d09f6 | 182 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 183 | |
1ecc4d80 | 184 | return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); |
ff7b1510 | 185 | } |
c801d85f | 186 | |
68dda785 | 187 | bool wxColour::Ok() const |
c801d85f | 188 | { |
1ecc4d80 | 189 | return (m_refData != NULL); |
ff7b1510 | 190 | } |
c801d85f KB |
191 | |
192 | void wxColour::CalcPixel( GdkColormap *cmap ) | |
193 | { | |
1ecc4d80 | 194 | if (!Ok()) return; |
8bbe427f | 195 | |
1ecc4d80 | 196 | if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return; |
f234c60c | 197 | |
1ecc4d80 | 198 | M_COLDATA->FreeColour(); |
8bbe427f | 199 | |
307fc8d5 OK |
200 | #ifdef __WXGTK20__ |
201 | if ((cmap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
202 | (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
203 | #else | |
f6fcbb63 RR |
204 | GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap; |
205 | if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) || | |
206 | (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR)) | |
307fc8d5 | 207 | #endif |
01111366 | 208 | { |
47c93b63 | 209 | M_COLDATA->m_hasPixel = gdk_colormap_alloc_color( cmap, &M_COLDATA->m_color, FALSE, TRUE ); |
f6fcbb63 | 210 | } |
1ecc4d80 RR |
211 | else |
212 | { | |
213 | M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color ); | |
214 | } | |
8bbe427f | 215 | |
1ecc4d80 | 216 | M_COLDATA->m_colormap = cmap; |
ff7b1510 | 217 | } |
c801d85f | 218 | |
68dda785 | 219 | int wxColour::GetPixel() const |
c801d85f | 220 | { |
223d09f6 | 221 | wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); |
8bbe427f | 222 | |
1ecc4d80 | 223 | return M_COLDATA->m_color.pixel; |
ff7b1510 | 224 | } |
c801d85f | 225 | |
68dda785 | 226 | GdkColor *wxColour::GetColor() const |
c801d85f | 227 | { |
223d09f6 | 228 | wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") ); |
8bbe427f | 229 | |
1ecc4d80 | 230 | return &M_COLDATA->m_color; |
ff7b1510 | 231 | } |
c801d85f KB |
232 | |
233 |