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