1. more accurate conversion from 8-bit wx color to 16-bit GDK color

2. eliminate possiblity of wxColour RGB values changing depending on colormap
3. don't allow non-const pointer access to internal GdkColor


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@39748 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Paul Cornett 2006-06-15 19:29:08 +00:00
parent 06414d99a7
commit c668531784
7 changed files with 57 additions and 66 deletions

View File

@ -57,8 +57,8 @@ protected:
public: // used by the GTK callback only public: // used by the GTK callback only
GdkColor *GetGdkColor() const void SetGdkColor(const GdkColor& gdkColor)
{ return m_colour.GetColor(); } { m_colour = wxColor(gdkColor); }
wxWindow *m_topParent; wxWindow *m_topParent;

View File

@ -40,6 +40,7 @@ public:
// default // default
wxColour() {} wxColour() {}
DEFINE_STD_WXCOLOUR_CONSTRUCTORS DEFINE_STD_WXCOLOUR_CONSTRUCTORS
wxColour(const GdkColor& gdkColor);
~wxColour(); ~wxColour();
@ -55,13 +56,9 @@ public:
// Implementation part // Implementation part
void CalcPixel( GdkColormap *cmap ); void CalcPixel( GdkColormap *cmap );
int GetPixel() const; int GetPixel() const;
GdkColor *GetColor() const; const GdkColor *GetColor() const;
protected: protected:
// ref counting code
virtual wxObjectRefData *CreateRefData() const;
virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
virtual bool FromString(const wxChar *str); virtual bool FromString(const wxChar *str);
virtual void InitWith( unsigned char red, unsigned char green, unsigned char blue ); virtual void InitWith( unsigned char red, unsigned char green, unsigned char blue );

View File

@ -37,7 +37,9 @@ static void gtk_clrbutton_setcolor_callback(GtkColorButton *widget,
{ {
// update the m_colour member of the wxColourButton // update the m_colour member of the wxColourButton
wxASSERT(p); wxASSERT(p);
gtk_color_button_get_color(widget, p->GetGdkColor()); GdkColor gdkColor;
gtk_color_button_get_color(widget, &gdkColor);
p->SetGdkColor(gdkColor);
// fire the colour-changed event // fire the colour-changed event
wxColourPickerEvent event(p, p->GetId(), p->GetColour()); wxColourPickerEvent event(p, p->GetId(), p->GetColour());

View File

@ -23,22 +23,13 @@
class wxColourRefData: public wxObjectRefData class wxColourRefData: public wxObjectRefData
{ {
public: public:
wxColourRefData() wxColourRefData(guint16 red, guint16 green, guint16 blue)
{ {
m_color.red = 0; m_red = red;
m_color.green = 0; m_green = green;
m_color.blue = 0; m_blue = blue;
m_color.pixel = 0; m_color.pixel = 0;
m_colormap = NULL; m_colormap = NULL;
m_hasPixel = false;
}
wxColourRefData(const wxColourRefData& data)
: wxObjectRefData()
{
m_color = data.m_color;
m_colormap = data.m_colormap;
m_hasPixel = data.m_hasPixel;
} }
~wxColourRefData() ~wxColourRefData()
@ -51,37 +42,54 @@ public:
GdkColor m_color; GdkColor m_color;
GdkColormap *m_colormap; GdkColormap *m_colormap;
bool m_hasPixel; // gdk_colormap_alloc_color may change the RGB values in m_color, so we need separate copies
guint16 m_red;
guint16 m_green;
guint16 m_blue;
DECLARE_NO_COPY_CLASS(wxColourRefData)
}; };
void wxColourRefData::FreeColour() void wxColourRefData::FreeColour()
{ {
if (m_hasPixel) if (m_colormap)
{ {
gdk_colormap_free_colors(m_colormap, &m_color, 1); gdk_colormap_free_colors(m_colormap, &m_color, 1);
m_colormap = NULL;
m_color.pixel = 0;
} }
} }
void wxColourRefData::AllocColour( GdkColormap *cmap ) void wxColourRefData::AllocColour( GdkColormap *cmap )
{ {
if (m_hasPixel && (m_colormap == cmap)) if (m_colormap != cmap)
return; {
FreeColour();
FreeColour(); m_color.red = m_red;
m_color.green = m_green;
m_hasPixel = gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE); m_color.blue = m_blue;
m_colormap = cmap; if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE))
{
m_colormap = cmap;
}
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define M_COLDATA wx_static_cast(wxColourRefData*, m_refData) #define M_COLDATA wx_static_cast(wxColourRefData*, m_refData)
// GDK's values are in 0..65535 range, our are in 0..255 // GDK's values are in 0..65535 range, ours are in 0..255
#define SHIFT 8 #define SHIFT 8
IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject) IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
wxColour::wxColour(const GdkColor& gdkColor)
{
m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue);
}
wxColour::~wxColour() wxColour::~wxColour()
{ {
} }
@ -94,55 +102,42 @@ bool wxColour::operator == ( const wxColour& col ) const
if (!m_refData || !col.m_refData) if (!m_refData || !col.m_refData)
return false; return false;
const GdkColor& own = M_COLDATA->m_color; wxColourRefData* refData = M_COLDATA;
const GdkColor& other = wx_static_cast(wxColourRefData*, col.m_refData)->m_color; wxColourRefData* that_refData = wx_static_cast(wxColourRefData*, col.m_refData);
return own.red == other.red && return refData->m_red == that_refData->m_red &&
own.blue == other.blue && refData->m_green == that_refData->m_green &&
own.green == other.green; refData->m_blue == that_refData->m_blue;
}
wxObjectRefData *wxColour::CreateRefData() const
{
return new wxColourRefData;
}
wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
{
return new wxColourRefData(*(wxColourRefData *)data);
} }
void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue ) void wxColour::InitWith( unsigned char red, unsigned char green, unsigned char blue )
{ {
AllocExclusive(); UnRef();
M_COLDATA->m_color.red = ((unsigned short)red) << SHIFT; m_refData = new wxColourRefData(
M_COLDATA->m_color.green = ((unsigned short)green) << SHIFT; (guint16(red) << SHIFT) + red,
M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT; (guint16(green) << SHIFT) + green,
M_COLDATA->m_color.pixel = 0; (guint16(blue) << SHIFT) + blue);
M_COLDATA->m_colormap = NULL;
M_COLDATA->m_hasPixel = false;
} }
unsigned char wxColour::Red() const unsigned char wxColour::Red() const
{ {
wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
return (unsigned char)(M_COLDATA->m_color.red >> SHIFT); return wxByte(M_COLDATA->m_red >> SHIFT);
} }
unsigned char wxColour::Green() const unsigned char wxColour::Green() const
{ {
wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
return (unsigned char)(M_COLDATA->m_color.green >> SHIFT); return wxByte(M_COLDATA->m_green >> SHIFT);
} }
unsigned char wxColour::Blue() const unsigned char wxColour::Blue() const
{ {
wxCHECK_MSG( Ok(), 0, wxT("invalid colour") ); wxCHECK_MSG( Ok(), 0, wxT("invalid colour") );
return (unsigned char)(M_COLDATA->m_color.blue >> SHIFT); return wxByte(M_COLDATA->m_blue >> SHIFT);
} }
void wxColour::CalcPixel( GdkColormap *cmap ) void wxColour::CalcPixel( GdkColormap *cmap )
@ -159,7 +154,7 @@ int wxColour::GetPixel() const
return M_COLDATA->m_color.pixel; return M_COLDATA->m_color.pixel;
} }
GdkColor *wxColour::GetColor() const const GdkColor *wxColour::GetColor() const
{ {
wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") ); wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
@ -171,10 +166,7 @@ bool wxColour::FromString(const wxChar *str)
GdkColor colGDK; GdkColor colGDK;
if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) ) if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) )
{ {
UnRef(); *this = wxColour(colGDK);
m_refData = new wxColourRefData;
M_COLDATA->m_color = colGDK;
return true; return true;
} }

View File

@ -368,7 +368,7 @@ void wxWindowDC::SetUpDC()
/* background colour */ /* background colour */
m_backgroundBrush = *wxWHITE_BRUSH; m_backgroundBrush = *wxWHITE_BRUSH;
m_backgroundBrush.GetColour().CalcPixel( m_cmap ); m_backgroundBrush.GetColour().CalcPixel( m_cmap );
GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor(); const GdkColor *bg_col = m_backgroundBrush.GetColour().GetColor();
/* m_textGC */ /* m_textGC */
m_textForegroundColour.CalcPixel( m_cmap ); m_textForegroundColour.CalcPixel( m_cmap );

View File

@ -104,7 +104,7 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
if (attr.HasTextColour()) if (attr.HasTextColour())
{ {
GdkColor *colFg = attr.GetTextColour().GetColor(); const GdkColor *colFg = attr.GetTextColour().GetColor();
g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d", g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
colFg->red, colFg->green, colFg->blue); colFg->red, colFg->green, colFg->blue);
tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),
@ -117,7 +117,7 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
if (attr.HasBackgroundColour()) if (attr.HasBackgroundColour())
{ {
GdkColor *colBg = attr.GetBackgroundColour().GetColor(); const GdkColor *colBg = attr.GetBackgroundColour().GetColor();
g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d", g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
colBg->red, colBg->green, colBg->blue); colBg->red, colBg->green, colBg->blue);
tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ), tag = gtk_text_tag_table_lookup( gtk_text_buffer_get_tag_table( text_buffer ),

View File

@ -3897,7 +3897,7 @@ GtkRcStyle *wxWindowGTK::CreateWidgetStyle(bool forceStyle)
if ( m_foregroundColour.Ok() ) if ( m_foregroundColour.Ok() )
{ {
GdkColor *fg = m_foregroundColour.GetColor(); const GdkColor *fg = m_foregroundColour.GetColor();
style->fg[GTK_STATE_NORMAL] = *fg; style->fg[GTK_STATE_NORMAL] = *fg;
style->color_flags[GTK_STATE_NORMAL] = GTK_RC_FG; style->color_flags[GTK_STATE_NORMAL] = GTK_RC_FG;
@ -3911,7 +3911,7 @@ GtkRcStyle *wxWindowGTK::CreateWidgetStyle(bool forceStyle)
if ( m_backgroundColour.Ok() ) if ( m_backgroundColour.Ok() )
{ {
GdkColor *bg = m_backgroundColour.GetColor(); const GdkColor *bg = m_backgroundColour.GetColor();
style->bg[GTK_STATE_NORMAL] = *bg; style->bg[GTK_STATE_NORMAL] = *bg;
style->base[GTK_STATE_NORMAL] = *bg; style->base[GTK_STATE_NORMAL] = *bg;