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