]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/colour.cpp
post wxUniv merge fixes
[wxWidgets.git] / src / gtk1 / colour.cpp
... / ...
CommitLineData
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
25class wxColourRefData: public wxObjectRefData
26{
27public:
28 wxColourRefData();
29 ~wxColourRefData();
30
31 void FreeColour();
32
33public:
34 GdkColor m_color;
35 GdkColormap *m_colormap;
36 bool m_hasPixel;
37
38 friend class wxColour;
39};
40
41wxColourRefData::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
51wxColourRefData::~wxColourRefData()
52{
53 FreeColour();
54}
55
56void 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
82IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
83
84wxColour::wxColour()
85{
86}
87
88wxColour::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
97void 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 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
112
113 delete m_refData;
114 m_refData = (wxObjectRefData *) NULL;
115 }
116 }
117}
118
119wxColour::wxColour( const wxColour& col )
120{
121 Ref( col );
122}
123
124wxColour::~wxColour()
125{
126}
127
128wxColour& wxColour::operator = ( const wxColour& col )
129{
130 if (*this == col) return (*this);
131 Ref( col );
132 return *this;
133}
134
135bool wxColour::operator == ( const wxColour& col ) const
136{
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;
149}
150
151bool wxColour::operator != ( const wxColour& col) const
152{
153 return !(*this == col);
154}
155
156void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
157{
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;
164}
165
166unsigned char wxColour::Red() const
167{
168 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
169
170 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
171}
172
173unsigned char wxColour::Green() const
174{
175 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
176
177 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
178}
179
180unsigned char wxColour::Blue() const
181{
182 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
183
184 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
185}
186
187bool wxColour::Ok() const
188{
189 return (m_refData != NULL);
190}
191
192void wxColour::CalcPixel( GdkColormap *cmap )
193{
194 if (!Ok()) return;
195
196 if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return;
197
198 M_COLDATA->FreeColour();
199
200#ifdef __WXGTK20__
201 if ((cmap->visual->type == GDK_VISUAL_GRAYSCALE) ||
202 (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
203#else
204 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
205 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
206 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
207#endif
208 {
209 M_COLDATA->m_hasPixel = gdk_colormap_alloc_color( cmap, &M_COLDATA->m_color, FALSE, TRUE );
210 }
211 else
212 {
213 M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color );
214 }
215
216 M_COLDATA->m_colormap = cmap;
217}
218
219int wxColour::GetPixel() const
220{
221 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
222
223 return M_COLDATA->m_color.pixel;
224}
225
226GdkColor *wxColour::GetColor() const
227{
228 wxCHECK_MSG( Ok(), (GdkColor *) NULL, wxT("invalid colour") );
229
230 return &M_COLDATA->m_color;
231}
232
233