]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/colour.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "colour.h"
15 #include "wx/gdicmn.h"
19 #include <gdk/gdkprivate.h>
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 class wxColourRefData
: public wxObjectRefData
35 GdkColormap
*m_colormap
;
38 friend class wxColour
;
41 wxColourRefData::wxColourRefData()
47 m_colormap
= (GdkColormap
*) NULL
;
51 wxColourRefData::~wxColourRefData()
56 void wxColourRefData::FreeColour()
61 if ((m_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
62 (m_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
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
))
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 );
76 //-----------------------------------------------------------------------------
78 #define M_COLDATA ((wxColourRefData *)m_refData)
80 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
82 IMPLEMENT_DYNAMIC_CLASS(wxColour
,wxGDIObject
)
88 wxColour::wxColour( unsigned char red
, unsigned char green
, unsigned char blue
)
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;
97 void wxColour::InitFromName( const wxString
&colourName
)
99 wxNode
*node
= (wxNode
*) NULL
;
100 if ( (wxTheColourDatabase
) && (node
= wxTheColourDatabase
->Find(colourName
)) )
102 wxColour
*col
= (wxColour
*)node
->Data();
104 if (col
) Ref( *col
);
108 m_refData
= new wxColourRefData();
109 if (!gdk_color_parse( colourName
.mb_str(), &M_COLDATA
->m_color
))
111 wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
114 m_refData
= (wxObjectRefData
*) NULL
;
119 wxColour::wxColour( const wxColour
& col
)
124 wxColour::~wxColour()
128 wxColour
& wxColour::operator = ( const wxColour
& col
)
130 if (*this == col
) return (*this);
135 bool wxColour::operator == ( const wxColour
& col
) const
137 if (m_refData
== col
.m_refData
) return TRUE
;
139 if (!m_refData
) return FALSE
;
140 if (!col
.m_refData
) return FALSE
;
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
;
151 bool wxColour::operator != ( const wxColour
& col
) const
153 return !(*this == col
);
156 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
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;
166 unsigned char wxColour::Red() const
168 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
170 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
173 unsigned char wxColour::Green() const
175 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
177 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
180 unsigned char wxColour::Blue() const
182 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
184 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
187 bool wxColour::Ok() const
189 return (m_refData
!= NULL
);
192 void wxColour::CalcPixel( GdkColormap
*cmap
)
196 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
198 M_COLDATA
->FreeColour();
201 if ((cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
202 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
204 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
205 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
206 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
209 M_COLDATA
->m_hasPixel
= gdk_colormap_alloc_color( cmap
, &M_COLDATA
->m_color
, FALSE
, TRUE
);
213 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
216 M_COLDATA
->m_colormap
= cmap
;
219 int wxColour::GetPixel() const
221 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
223 return M_COLDATA
->m_color
.pixel
;
226 GdkColor
*wxColour::GetColor() const
228 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
230 return &M_COLDATA
->m_color
;