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