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
GdkColor *GetGdkColor() const
{ return m_colour.GetColor(); }
void SetGdkColor(const GdkColor& gdkColor)
{ m_colour = wxColor(gdkColor); }
wxWindow *m_topParent;

View File

@ -40,6 +40,7 @@ public:
// default
wxColour() {}
DEFINE_STD_WXCOLOUR_CONSTRUCTORS
wxColour(const GdkColor& gdkColor);
~wxColour();
@ -55,13 +56,9 @@ public:
// Implementation part
void CalcPixel( GdkColormap *cmap );
int GetPixel() const;
GdkColor *GetColor() const;
const GdkColor *GetColor() const;
protected:
// ref counting code
virtual wxObjectRefData *CreateRefData() const;
virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
virtual bool FromString(const wxChar *str);
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
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
wxColourPickerEvent event(p, p->GetId(), p->GetColour());

View File

@ -23,22 +23,13 @@
class wxColourRefData: public wxObjectRefData
{
public:
wxColourRefData()
wxColourRefData(guint16 red, guint16 green, guint16 blue)
{
m_color.red = 0;
m_color.green = 0;
m_color.blue = 0;
m_red = red;
m_green = green;
m_blue = blue;
m_color.pixel = 0;
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()
@ -51,37 +42,54 @@ public:
GdkColor m_color;
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()
{
if (m_hasPixel)
if (m_colormap)
{
gdk_colormap_free_colors(m_colormap, &m_color, 1);
m_colormap = NULL;
m_color.pixel = 0;
}
}
void wxColourRefData::AllocColour( GdkColormap *cmap )
{
if (m_hasPixel && (m_colormap == cmap))
return;
if (m_colormap != cmap)
{
FreeColour();
FreeColour();
m_hasPixel = gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE);
m_colormap = cmap;
m_color.red = m_red;
m_color.green = m_green;
m_color.blue = m_blue;
if (gdk_colormap_alloc_color(cmap, &m_color, FALSE, TRUE))
{
m_colormap = cmap;
}
}
}
//-----------------------------------------------------------------------------
#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
IMPLEMENT_DYNAMIC_CLASS(wxColour,wxGDIObject)
wxColour::wxColour(const GdkColor& gdkColor)
{
m_refData = new wxColourRefData(gdkColor.red, gdkColor.green, gdkColor.blue);
}
wxColour::~wxColour()
{
}
@ -94,55 +102,42 @@ bool wxColour::operator == ( const wxColour& col ) const
if (!m_refData || !col.m_refData)
return false;
const GdkColor& own = M_COLDATA->m_color;
const GdkColor& other = wx_static_cast(wxColourRefData*, col.m_refData)->m_color;
return own.red == other.red &&
own.blue == other.blue &&
own.green == other.green;
}
wxObjectRefData *wxColour::CreateRefData() const
{
return new wxColourRefData;
}
wxObjectRefData *wxColour::CloneRefData(const wxObjectRefData *data) const
{
return new wxColourRefData(*(wxColourRefData *)data);
wxColourRefData* refData = M_COLDATA;
wxColourRefData* that_refData = wx_static_cast(wxColourRefData*, col.m_refData);
return refData->m_red == that_refData->m_red &&
refData->m_green == that_refData->m_green &&
refData->m_blue == that_refData->m_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_COLDATA->m_color.green = ((unsigned short)green) << SHIFT;
M_COLDATA->m_color.blue = ((unsigned short)blue) << SHIFT;
M_COLDATA->m_color.pixel = 0;
M_COLDATA->m_colormap = NULL;
M_COLDATA->m_hasPixel = false;
m_refData = new wxColourRefData(
(guint16(red) << SHIFT) + red,
(guint16(green) << SHIFT) + green,
(guint16(blue) << SHIFT) + blue);
}
unsigned char wxColour::Red() const
{
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
{
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
{
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 )
@ -159,7 +154,7 @@ int wxColour::GetPixel() const
return M_COLDATA->m_color.pixel;
}
GdkColor *wxColour::GetColor() const
const GdkColor *wxColour::GetColor() const
{
wxCHECK_MSG( Ok(), NULL, wxT("invalid colour") );
@ -171,10 +166,7 @@ bool wxColour::FromString(const wxChar *str)
GdkColor colGDK;
if ( gdk_color_parse( wxGTK_CONV_SYS( str ), &colGDK ) )
{
UnRef();
m_refData = new wxColourRefData;
M_COLDATA->m_color = colGDK;
*this = wxColour(colGDK);
return true;
}

View File

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

View File

@ -104,7 +104,7 @@ static void wxGtkTextApplyTagsFromAttr(GtkTextBuffer *text_buffer,
if (attr.HasTextColour())
{
GdkColor *colFg = attr.GetTextColour().GetColor();
const GdkColor *colFg = attr.GetTextColour().GetColor();
g_snprintf(buf, sizeof(buf), "WXFORECOLOR %d %d %d",
colFg->red, colFg->green, colFg->blue);
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())
{
GdkColor *colBg = attr.GetBackgroundColour().GetColor();
const GdkColor *colBg = attr.GetBackgroundColour().GetColor();
g_snprintf(buf, sizeof(buf), "WXBACKCOLOR %d %d %d",
colBg->red, colBg->green, colBg->blue);
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() )
{
GdkColor *fg = m_foregroundColour.GetColor();
const GdkColor *fg = m_foregroundColour.GetColor();
style->fg[GTK_STATE_NORMAL] = *fg;
style->color_flags[GTK_STATE_NORMAL] = GTK_RC_FG;
@ -3911,7 +3911,7 @@ GtkRcStyle *wxWindowGTK::CreateWidgetStyle(bool forceStyle)
if ( m_backgroundColour.Ok() )
{
GdkColor *bg = m_backgroundColour.GetColor();
const GdkColor *bg = m_backgroundColour.GetColor();
style->bg[GTK_STATE_NORMAL] = *bg;
style->base[GTK_STATE_NORMAL] = *bg;