]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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 // VZ: asserts are good in general but this one is triggered by
112 // calling wxColourDatabase::FindColour() with an
113 // unrecognized colour name and this can't be avoided from the
114 // user code, so don't give it here
116 // a better solution would be to changed code in FindColour()
118 //wxFAIL_MSG( wxT("wxColour: couldn't find colour") );
121 m_refData
= (wxObjectRefData
*) NULL
;
126 wxColour::wxColour( const wxColour
& col
)
131 wxColour::~wxColour()
135 wxColour
& wxColour::operator = ( const wxColour
& col
)
137 if (*this == col
) return (*this);
142 bool wxColour::operator == ( const wxColour
& col
) const
144 if (m_refData
== col
.m_refData
) return TRUE
;
146 if (!m_refData
) return FALSE
;
147 if (!col
.m_refData
) return FALSE
;
149 GdkColor
*own
= &(((wxColourRefData
*)m_refData
)->m_color
);
150 GdkColor
*other
= &(((wxColourRefData
*)col
.m_refData
)->m_color
);
151 if (own
->red
!= other
->red
) return FALSE
;
152 if (own
->blue
!= other
->blue
) return FALSE
;
153 if (own
->green
!= other
->green
) return FALSE
;
158 bool wxColour::operator != ( const wxColour
& col
) const
160 return !(*this == col
);
163 void wxColour::Set( unsigned char red
, unsigned char green
, unsigned char blue
)
166 m_refData
= new wxColourRefData();
167 M_COLDATA
->m_color
.red
= ((unsigned short)red
) << SHIFT
;
168 M_COLDATA
->m_color
.green
= ((unsigned short)green
) << SHIFT
;
169 M_COLDATA
->m_color
.blue
= ((unsigned short)blue
) << SHIFT
;
170 M_COLDATA
->m_color
.pixel
= 0;
173 unsigned char wxColour::Red() const
175 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
177 return (unsigned char)(M_COLDATA
->m_color
.red
>> SHIFT
);
180 unsigned char wxColour::Green() const
182 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
184 return (unsigned char)(M_COLDATA
->m_color
.green
>> SHIFT
);
187 unsigned char wxColour::Blue() const
189 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
191 return (unsigned char)(M_COLDATA
->m_color
.blue
>> SHIFT
);
194 bool wxColour::Ok() const
196 return (m_refData
!= NULL
);
199 void wxColour::CalcPixel( GdkColormap
*cmap
)
203 if ((M_COLDATA
->m_hasPixel
) && (M_COLDATA
->m_colormap
== cmap
)) return;
205 M_COLDATA
->FreeColour();
208 if ((cmap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
209 (cmap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
211 GdkColormapPrivate
*private_colormap
= (GdkColormapPrivate
*) cmap
;
212 if ((private_colormap
->visual
->type
== GDK_VISUAL_GRAYSCALE
) ||
213 (private_colormap
->visual
->type
== GDK_VISUAL_PSEUDO_COLOR
))
216 M_COLDATA
->m_hasPixel
= gdk_colormap_alloc_color( cmap
, &M_COLDATA
->m_color
, FALSE
, TRUE
);
220 M_COLDATA
->m_hasPixel
= gdk_color_alloc( cmap
, &M_COLDATA
->m_color
);
223 M_COLDATA
->m_colormap
= cmap
;
226 int wxColour::GetPixel() const
228 wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
230 return M_COLDATA
->m_color
.pixel
;
233 GdkColor
*wxColour::GetColor() const
235 wxCHECK_MSG( Ok(), (GdkColor
*) NULL
, wxT("invalid colour") );
237 return &M_COLDATA
->m_color
;