]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/colour.cpp
added an error message if a bitmap can't be addedto the image list
[wxWidgets.git] / src / gtk1 / colour.cpp
CommitLineData
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
83624f79 17#include "gdk/gdk.h"
01111366 18#include "gdk/gdkprivate.h"
c801d85f
KB
19
20//-----------------------------------------------------------------------------
21// wxColour
22//-----------------------------------------------------------------------------
23
24class wxColourRefData: public wxObjectRefData
25{
26 public:
8bbe427f 27
68dda785
VZ
28 wxColourRefData();
29 ~wxColourRefData();
30 void FreeColour();
8bbe427f 31
c801d85f
KB
32 GdkColor m_color;
33 GdkColormap *m_colormap;
34 bool m_hasPixel;
8bbe427f 35
c801d85f
KB
36 friend wxColour;
37};
38
68dda785 39wxColourRefData::wxColourRefData()
c801d85f 40{
1ecc4d80
RR
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;
ff7b1510 47}
c801d85f 48
68dda785 49wxColourRefData::~wxColourRefData()
c801d85f 50{
1ecc4d80 51 FreeColour();
ff7b1510 52}
c801d85f 53
68dda785 54void wxColourRefData::FreeColour()
c801d85f
KB
55{
56// if (m_hasPixel) gdk_colors_free( m_colormap, &m_color, 1, 0 );
ff7b1510 57}
c801d85f
KB
58
59//-----------------------------------------------------------------------------
60
61#define M_COLDATA ((wxColourRefData *)m_refData)
62
63#define SHIFT (8*(sizeof(short int)-sizeof(char)))
64
65IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
66
68dda785 67wxColour::wxColour()
c801d85f 68{
ff7b1510 69}
c801d85f 70
4b5f3fe6 71wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 72{
1ecc4d80
RR
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;
ff7b1510 78}
8bbe427f 79
68dda785 80void wxColour::InitFromName( const wxString &colourName )
c801d85f 81{
1ecc4d80
RR
82 wxNode *node = (wxNode *) NULL;
83 if ( (wxTheColourDatabase) && (node = wxTheColourDatabase->Find(colourName)) )
c801d85f 84 {
1ecc4d80
RR
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, &M_COLDATA->m_color ))
93 {
94 wxFAIL_MSG( "wxColour: couldn't find colour" );
95 printf( "Colourname %s.\n", WXSTRINGCAST colourName );
96
97 delete m_refData;
98 m_refData = (wxObjectRefData *) NULL;
99 }
ff7b1510 100 }
ff7b1510 101}
c801d85f
KB
102
103wxColour::wxColour( const wxColour& col )
8bbe427f 104{
1ecc4d80 105 Ref( col );
ff7b1510 106}
c801d85f 107
68dda785 108wxColour::~wxColour()
c801d85f 109{
ff7b1510 110}
c801d85f 111
8bbe427f
VZ
112wxColour& wxColour::operator = ( const wxColour& col )
113{
1ecc4d80
RR
114 if (*this == col) return (*this);
115 Ref( col );
116 return *this;
ff7b1510 117}
c801d85f 118
8bbe427f
VZ
119bool wxColour::operator == ( const wxColour& col )
120{
1ecc4d80 121 return m_refData == col.m_refData;
ff7b1510 122}
c801d85f 123
8bbe427f
VZ
124bool wxColour::operator != ( const wxColour& col)
125{
1ecc4d80 126 return m_refData != col.m_refData;
ff7b1510 127}
c801d85f 128
4b5f3fe6 129void wxColour::Set( unsigned char red, unsigned char green, unsigned char blue )
c801d85f 130{
1ecc4d80
RR
131 UnRef();
132 m_refData = new wxColourRefData();
133 M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT;
134 M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
135 M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
136 M_COLDATA->m_color.pixel = 0;
ff7b1510 137}
c801d85f 138
68dda785 139unsigned char wxColour::Red() const
c801d85f 140{
1ecc4d80 141 wxCHECK_MSG( Ok(), 0, "invalid colour" );
8bbe427f 142
1ecc4d80 143 return (unsigned char)(M_COLDATA->m_color.red >> SHIFT);
ff7b1510 144}
c801d85f 145
68dda785 146unsigned char wxColour::Green() const
c801d85f 147{
1ecc4d80 148 wxCHECK_MSG( Ok(), 0, "invalid colour" );
8bbe427f 149
1ecc4d80 150 return (unsigned char)(M_COLDATA->m_color.green >> SHIFT);
ff7b1510 151}
c801d85f 152
68dda785 153unsigned char wxColour::Blue() const
c801d85f 154{
1ecc4d80 155 wxCHECK_MSG( Ok(), 0, "invalid colour" );
8bbe427f 156
1ecc4d80 157 return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT);
ff7b1510 158}
c801d85f 159
68dda785 160bool wxColour::Ok() const
c801d85f 161{
1ecc4d80 162 return (m_refData != NULL);
ff7b1510 163}
c801d85f
KB
164
165void wxColour::CalcPixel( GdkColormap *cmap )
166{
1ecc4d80 167 if (!Ok()) return;
8bbe427f 168
1ecc4d80
RR
169 if ((M_COLDATA->m_hasPixel) && (M_COLDATA->m_colormap == cmap)) return;
170 M_COLDATA->FreeColour();
8bbe427f 171
f6fcbb63
RR
172 GdkColormapPrivate *private_colormap = (GdkColormapPrivate*) cmap;
173 if ((private_colormap->visual->type == GDK_VISUAL_GRAYSCALE) ||
174 (private_colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR))
01111366 175 {
f6fcbb63
RR
176 GdkColor *colors = cmap->colors;
177 int max = 3 * (65536);
178 int index = -1;
8bbe427f 179
f6fcbb63
RR
180 for (int i = 0; i < cmap->size; i++)
181 {
182 int rdiff = (M_COLDATA->m_color.red - colors[i].red);
183 int gdiff = (M_COLDATA->m_color.green - colors[i].green);
184 int bdiff = (M_COLDATA->m_color.blue - colors[i].blue);
185 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
186 if (sum < max) { index = i; max = sum; }
187 }
188
189 M_COLDATA->m_hasPixel = TRUE;
190 M_COLDATA->m_color.pixel = index;
191 }
1ecc4d80
RR
192 else
193 {
194 M_COLDATA->m_hasPixel = gdk_color_alloc( cmap, &M_COLDATA->m_color );
195 }
8bbe427f 196
1ecc4d80 197 M_COLDATA->m_colormap = cmap;
ff7b1510 198}
c801d85f 199
68dda785 200int wxColour::GetPixel() const
c801d85f 201{
1ecc4d80 202 wxCHECK_MSG( Ok(), 0, "invalid colour" );
8bbe427f 203
1ecc4d80 204 return M_COLDATA->m_color.pixel;
ff7b1510 205}
c801d85f 206
68dda785 207GdkColor *wxColour::GetColor() const
c801d85f 208{
1ecc4d80 209 wxCHECK_MSG( Ok(), (GdkColor *) NULL, "invalid colour" );
8bbe427f 210
1ecc4d80 211 return &M_COLDATA->m_color;
ff7b1510 212}
c801d85f
KB
213
214