From 564a150b1f7c63a060c1b205a7cd50035031cd4b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 18 Oct 2003 23:51:01 +0000 Subject: [PATCH] replaced all occurences of wxColourDatabase::FindColour() with Find(); minor code cleanup here and there git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@24231 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/mac/colour.h | 10 +++---- include/wx/mgl/colour.h | 6 ++-- include/wx/motif/colour.h | 3 +- include/wx/x11/colour.h | 3 ++ src/generic/colrdlgg.cpp | 10 +++---- src/generic/filedlgg.cpp | 5 ++-- src/generic/fontdlgg.cpp | 8 +++--- src/generic/sashwin.cpp | 10 +++---- src/gtk/glcanvas.cpp | 13 ++++----- src/gtk1/glcanvas.cpp | 13 ++++----- src/mac/carbon/colour.cpp | 34 ++++++---------------- src/mac/carbon/glcanvas.cpp | 13 ++++----- src/mac/colour.cpp | 34 ++++++---------------- src/mac/glcanvas.cpp | 13 ++++----- src/mgl/colour.cpp | 57 ++++++++++++++----------------------- src/motif/colour.cpp | 38 ++++--------------------- src/msw/glcanvas.cpp | 19 ++++++------- src/x11/colour.cpp | 56 ++++++++++++++++++++++-------------- src/x11/glcanvas.cpp | 36 ++++++++++++----------- 19 files changed, 158 insertions(+), 223 deletions(-) diff --git a/include/wx/mac/colour.h b/include/wx/mac/colour.h index 25b6d3b726..88deff7b3c 100644 --- a/include/wx/mac/colour.h +++ b/include/wx/mac/colour.h @@ -25,19 +25,17 @@ class WXDLLEXPORT wxColour: public wxObject public: // ctors // default - wxColour(); + wxColour() { Init(); } // from RGB - wxColour( unsigned char red, unsigned char green, unsigned char blue ); + wxColour( unsigned char red, unsigned char green, unsigned char blue ) + { Set(red, green, blue); } wxColour( unsigned long colRGB ) - : m_isInit(FALSE), m_red(0), m_blue(0), m_green(0) { Set(colRGB); } // implicit conversion from the colour name wxColour( const wxString &colourName ) - : m_isInit(FALSE), m_red(0), m_blue(0), m_green(0) { InitFromName(colourName); } wxColour( const wxChar *colourName ) - : m_isInit(FALSE), m_red(0), m_blue(0), m_green(0) { InitFromName(colourName); } // copy ctors and assignment operators @@ -86,6 +84,8 @@ private: unsigned char m_blue; unsigned char m_green; + void Init(); + public: WXCOLORREF m_pixel ; void Set( const WXCOLORREF* color ) ; diff --git a/include/wx/mgl/colour.h b/include/wx/mgl/colour.h index eb75ad93ba..1353189107 100644 --- a/include/wx/mgl/colour.h +++ b/include/wx/mgl/colour.h @@ -25,7 +25,8 @@ public: // default wxColour(); // from RGB - wxColour(unsigned char red, unsigned char green, unsigned char blue); + wxColour(unsigned char red, unsigned char green, unsigned char blue) + { Set(red, green, blue); } wxColour(unsigned long colRGB) { Set(colRGB); } // implicit conversion from the colour name @@ -74,7 +75,8 @@ private: unsigned char m_blue; unsigned char m_green; - // helper func + // ctors helpers + void Init(); void InitFromName(const wxString& colourName); private: diff --git a/include/wx/motif/colour.h b/include/wx/motif/colour.h index bc53874835..b478f37b19 100644 --- a/include/wx/motif/colour.h +++ b/include/wx/motif/colour.h @@ -28,7 +28,8 @@ public: // default wxColour(); // from RGB - wxColour( unsigned char red, unsigned char green, unsigned char blue ); + wxColour( unsigned char red, unsigned char green, unsigned char blue ) + { Set(red, green, blue); } wxColour( unsigned long colRGB ) { Set(colRGB); } // implicit conversion from the colour name diff --git a/include/wx/x11/colour.h b/include/wx/x11/colour.h index d2ab262420..30481cba51 100644 --- a/include/wx/x11/colour.h +++ b/include/wx/x11/colour.h @@ -53,6 +53,9 @@ public: wxColour( const wxChar *colourName ) { InitFromName( wxString(colourName) ); } #endif + // Get colour from name or wxNullColour + static wxColour CreateByName(const wxString& name); + wxColour( const wxColour& col ) { Ref(col); } wxColour& operator = ( const wxColour& col ) { Ref(col); return *this; } diff --git a/src/generic/colrdlgg.cpp b/src/generic/colrdlgg.cpp index 99b32860e4..38275f47bb 100644 --- a/src/generic/colrdlgg.cpp +++ b/src/generic/colrdlgg.cpp @@ -304,14 +304,14 @@ void wxGenericColourDialog::InitializeColours(void) for (i = 0; i < WXSIZEOF(wxColourDialogNames); i++) { - wxColour *col = wxTheColourDatabase->FindColour(wxColourDialogNames[i]); - if (col) - standardColours[i].Set(col->Red(), col->Green(), col->Blue()); + wxColour col = wxTheColourDatabase->Find(wxColourDialogNames[i]); + if (col.Ok()) + standardColours[i].Set(col.Red(), col.Green(), col.Blue()); else standardColours[i].Set(0, 0, 0); } - for (i = 0; i < 16; i++) + for (i = 0; i < WXSIZEOF(customColours); i++) { customColours[i] = colourData.GetCustomColour(i); } @@ -333,7 +333,7 @@ void wxGenericColourDialog::InitializeColours(void) } if ( !initColourFound ) { - for ( i = 0; i < 16; i++ ) + for ( i = 0; i < WXSIZEOF(customColours); i++ ) { if ( customColours[i] == curr ) { diff --git a/src/generic/filedlgg.cpp b/src/generic/filedlgg.cpp index 2c0933436d..69335d597f 100644 --- a/src/generic/filedlgg.cpp +++ b/src/generic/filedlgg.cpp @@ -343,8 +343,9 @@ void wxFileData::MakeItem( wxListItem &item ) if (IsLink()) { - wxColour *dg = wxTheColourDatabase->FindColour( _T("MEDIUM GREY") ); - item.SetTextColour(*dg); + wxColour dg = wxTheColourDatabase->Find( _T("MEDIUM GREY") ); + if ( dg.Ok() ) + item.SetTextColour(dg); } item.m_data = (long)this; } diff --git a/src/generic/fontdlgg.cpp b/src/generic/fontdlgg.cpp index 427f51b1a2..9ac0c7dcfa 100644 --- a/src/generic/fontdlgg.cpp +++ b/src/generic/fontdlgg.cpp @@ -339,11 +339,11 @@ void wxGenericFontDialog::OnChangeFont(wxCommandEvent& WXUNUSED(event)) m_previewer->SetFont(dialogFont); if (colourChoice->GetStringSelection() != wxT("")) { - wxColour *col = wxTheColourDatabase->FindColour(colourChoice->GetStringSelection()); - if (col) + wxColour col = wxTheColourDatabase->Find(colourChoice->GetStringSelection()); + if (col.Ok()) { - m_fontData.m_fontColour = *col; - m_previewer->SetForegroundColour(*col); + m_fontData.m_fontColour = col; + m_previewer->SetForegroundColour(col); } } m_previewer->Refresh(); diff --git a/src/generic/sashwin.cpp b/src/generic/sashwin.cpp index 55664e587c..f026ed1b74 100644 --- a/src/generic/sashwin.cpp +++ b/src/generic/sashwin.cpp @@ -677,11 +677,11 @@ void wxSashWindow::InitColours() m_lightShadowColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT); m_hilightColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHILIGHT); #else - m_faceColour = *(wxTheColourDatabase->FindColour("LIGHT GREY")); - m_mediumShadowColour = *(wxTheColourDatabase->FindColour("GREY")); - m_darkShadowColour = *(wxTheColourDatabase->FindColour("BLACK")); - m_lightShadowColour = *(wxTheColourDatabase->FindColour("LIGHT GREY")); - m_hilightColour = *(wxTheColourDatabase->FindColour("WHITE")); + m_faceColour = wxTheColourDatabase->Find("LIGHT GREY"); + m_mediumShadowColour = wxTheColourDatabase->Find("GREY"); + m_darkShadowColour = wxTheColourDatabase->Find("BLACK"); + m_lightShadowColour = wxTheColourDatabase->Find("LIGHT GREY"); + m_hilightColour = wxTheColourDatabase->Find("WHITE"); #endif } diff --git a/src/gtk/glcanvas.cpp b/src/gtk/glcanvas.cpp index f9599d1aa4..88018d4bb8 100644 --- a/src/gtk/glcanvas.cpp +++ b/src/gtk/glcanvas.cpp @@ -130,15 +130,12 @@ void wxGLContext::SetCurrent() void wxGLContext::SetColour(const wxChar *colour) { - float r = 0.0; - float g = 0.0; - float b = 0.0; - wxColour *col = wxTheColourDatabase->FindColour(colour); - if (col) + wxColour col = wxTheColourDatabase->Find(colour); + if (col.Ok()) { - r = (float)(col->Red()/256.0); - g = (float)(col->Green()/256.0); - b = (float)(col->Blue()/256.0); + float r = (float)(col.Red()/256.0); + float g = (float)(col.Green()/256.0); + float b = (float)(col.Blue()/256.0); glColor3f( r, g, b); } } diff --git a/src/gtk1/glcanvas.cpp b/src/gtk1/glcanvas.cpp index f9599d1aa4..88018d4bb8 100644 --- a/src/gtk1/glcanvas.cpp +++ b/src/gtk1/glcanvas.cpp @@ -130,15 +130,12 @@ void wxGLContext::SetCurrent() void wxGLContext::SetColour(const wxChar *colour) { - float r = 0.0; - float g = 0.0; - float b = 0.0; - wxColour *col = wxTheColourDatabase->FindColour(colour); - if (col) + wxColour col = wxTheColourDatabase->Find(colour); + if (col.Ok()) { - r = (float)(col->Red()/256.0); - g = (float)(col->Green()/256.0); - b = (float)(col->Blue()/256.0); + float r = (float)(col.Red()/256.0); + float g = (float)(col.Green()/256.0); + float b = (float)(col.Blue()/256.0); glColor3f( r, g, b); } } diff --git a/src/mac/carbon/colour.cpp b/src/mac/carbon/colour.cpp index 12a65e02a5..13b88761bc 100644 --- a/src/mac/carbon/colour.cpp +++ b/src/mac/carbon/colour.cpp @@ -33,24 +33,16 @@ static void wxComposeRGBColor( WXCOLORREF* color , int red, int blue, int green col->green = (green << 8) + green; } -wxColour::wxColour () +void wxColour::Init() { m_isInit = FALSE; - m_red = m_blue = m_green = 0; + m_red = + m_blue = + m_green = 0; wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; } -wxColour::wxColour (unsigned char r, unsigned char g, unsigned char b) -{ - m_red = r; - m_green = g; - m_blue = b; - m_isInit = TRUE; - - wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; -} - wxColour::wxColour (const wxColour& col) : wxObject() { @@ -84,25 +76,17 @@ wxColour& wxColour::operator =(const wxColour& col) return *this; } -void wxColour::InitFromName(const wxString& col) +void wxColour::InitFromName(const wxString& name) { - wxColour *the_colour = wxTheColourDatabase->FindColour (col); - if (the_colour) + wxColour col = wxTheColourDatabase->Find(name); + if ( col.Ok() ) { - m_red = the_colour->Red (); - m_green = the_colour->Green (); - m_blue = the_colour->Blue (); - m_isInit = TRUE; + *this = col; } else { - m_red = 0; - m_green = 0; - m_blue = 0; - m_isInit = FALSE; + Init(); } - - wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; } wxColour::~wxColour () diff --git a/src/mac/carbon/glcanvas.cpp b/src/mac/carbon/glcanvas.cpp index 44215b6b75..e208531ff4 100644 --- a/src/mac/carbon/glcanvas.cpp +++ b/src/mac/carbon/glcanvas.cpp @@ -97,15 +97,12 @@ void wxGLContext::Update() void wxGLContext::SetColour(const wxChar *colour) { - float r = 0.0; - float g = 0.0; - float b = 0.0; - wxColour *col = wxTheColourDatabase->FindColour(colour); - if (col) + wxColour col = wxTheColourDatabase->Find(colour); + if (col.Ok()) { - r = (float)(col->Red()/256.0); - g = (float)(col->Green()/256.0); - b = (float)(col->Blue()/256.0); + float r = (float)(col.Red()/256.0); + float g = (float)(col.Green()/256.0); + float b = (float)(col.Blue()/256.0); glColor3f( r, g, b); } } diff --git a/src/mac/colour.cpp b/src/mac/colour.cpp index 12a65e02a5..13b88761bc 100644 --- a/src/mac/colour.cpp +++ b/src/mac/colour.cpp @@ -33,24 +33,16 @@ static void wxComposeRGBColor( WXCOLORREF* color , int red, int blue, int green col->green = (green << 8) + green; } -wxColour::wxColour () +void wxColour::Init() { m_isInit = FALSE; - m_red = m_blue = m_green = 0; + m_red = + m_blue = + m_green = 0; wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; } -wxColour::wxColour (unsigned char r, unsigned char g, unsigned char b) -{ - m_red = r; - m_green = g; - m_blue = b; - m_isInit = TRUE; - - wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; -} - wxColour::wxColour (const wxColour& col) : wxObject() { @@ -84,25 +76,17 @@ wxColour& wxColour::operator =(const wxColour& col) return *this; } -void wxColour::InitFromName(const wxString& col) +void wxColour::InitFromName(const wxString& name) { - wxColour *the_colour = wxTheColourDatabase->FindColour (col); - if (the_colour) + wxColour col = wxTheColourDatabase->Find(name); + if ( col.Ok() ) { - m_red = the_colour->Red (); - m_green = the_colour->Green (); - m_blue = the_colour->Blue (); - m_isInit = TRUE; + *this = col; } else { - m_red = 0; - m_green = 0; - m_blue = 0; - m_isInit = FALSE; + Init(); } - - wxComposeRGBColor( &m_pixel , m_red , m_blue , m_green ) ; } wxColour::~wxColour () diff --git a/src/mac/glcanvas.cpp b/src/mac/glcanvas.cpp index 44215b6b75..e208531ff4 100644 --- a/src/mac/glcanvas.cpp +++ b/src/mac/glcanvas.cpp @@ -97,15 +97,12 @@ void wxGLContext::Update() void wxGLContext::SetColour(const wxChar *colour) { - float r = 0.0; - float g = 0.0; - float b = 0.0; - wxColour *col = wxTheColourDatabase->FindColour(colour); - if (col) + wxColour col = wxTheColourDatabase->Find(colour); + if (col.Ok()) { - r = (float)(col->Red()/256.0); - g = (float)(col->Green()/256.0); - b = (float)(col->Blue()/256.0); + float r = (float)(col.Red()/256.0); + float g = (float)(col.Green()/256.0); + float b = (float)(col.Blue()/256.0); glColor3f( r, g, b); } } diff --git a/src/mgl/colour.cpp b/src/mgl/colour.cpp index 9eaf41f563..ebab19fba2 100644 --- a/src/mgl/colour.cpp +++ b/src/mgl/colour.cpp @@ -27,53 +27,38 @@ IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject) // Colour -wxColour::wxColour() +void wxColour::Init() { - m_red = m_blue = m_green = 0; - m_isInit = FALSE; -} - -wxColour::wxColour(unsigned char r, unsigned char g, unsigned char b) -{ - m_red = r; - m_green = g; - m_blue = b; - m_isInit = TRUE; + m_red = + m_blue = + m_green = 0; + m_isInit = FALSE; } wxColour::wxColour(const wxColour& col) { - m_red = col.m_red; - m_green = col.m_green; - m_blue = col.m_blue; - m_isInit = col.m_isInit; + *this = col; } wxColour& wxColour::operator =(const wxColour& col) { - m_red = col.m_red; - m_green = col.m_green; - m_blue = col.m_blue; - m_isInit = col.m_isInit; - return *this; + m_red = col.m_red; + m_green = col.m_green; + m_blue = col.m_blue; + m_isInit = col.m_isInit; + return *this; } -void wxColour::InitFromName(const wxString& col) +void wxColour::InitFromName(const wxString& name) { - wxColour *the_colour = wxTheColourDatabase->FindColour (col); - if (the_colour) + wxColour *col = wxTheColourDatabase->Find(name); + if ( col.Ok() ) { - m_red = the_colour->Red(); - m_green = the_colour->Green(); - m_blue = the_colour->Blue(); - m_isInit = TRUE; + *this = col; } - else + else { - m_red = 0; - m_green = 0; - m_blue = 0; - m_isInit = FALSE; + Init(); } } @@ -83,8 +68,8 @@ wxColour::~wxColour() void wxColour::Set(unsigned char r, unsigned char g, unsigned char b) { - m_red = r; - m_green = g; - m_blue = b; - m_isInit = TRUE; + m_red = r; + m_green = g; + m_blue = b; + m_isInit = TRUE; } diff --git a/src/motif/colour.cpp b/src/motif/colour.cpp index c36d3b085c..ee95c0a045 100644 --- a/src/motif/colour.cpp +++ b/src/motif/colour.cpp @@ -37,26 +37,15 @@ IMPLEMENT_DYNAMIC_CLASS(wxColour, wxObject) wxColour::wxColour () { m_isInit = FALSE; - m_red = m_blue = m_green = 0; - m_pixel = -1; -} - -wxColour::wxColour (unsigned char r, unsigned char g, unsigned char b) -{ - m_red = r; - m_green = g; - m_blue = b; - m_isInit = TRUE; + m_red = + m_blue = + m_green = 0; m_pixel = -1; } wxColour::wxColour (const wxColour& col) { - m_red = col.m_red; - m_green = col.m_green; - m_blue = col.m_blue; - m_isInit = col.m_isInit; - m_pixel = col.m_pixel; + *this = col; } wxColour& wxColour::operator =(const wxColour& col) @@ -69,24 +58,9 @@ wxColour& wxColour::operator =(const wxColour& col) return *this; } -void wxColour::InitFromName(const wxString& col) +void wxColour::InitFromName(const wxString& name) { - wxColour *the_colour = wxTheColourDatabase->FindColour (col); - if (the_colour) - { - m_red = the_colour->Red (); - m_green = the_colour->Green (); - m_blue = the_colour->Blue (); - m_pixel = the_colour->m_pixel; - m_isInit = TRUE; - } - else - { - m_red = 0; - m_green = 0; - m_blue = 0; - m_isInit = FALSE; - } + *this = wxTheColourDatabase->Find(name); } wxColour::~wxColour () diff --git a/src/msw/glcanvas.cpp b/src/msw/glcanvas.cpp index feb72526b1..c5a0523d00 100644 --- a/src/msw/glcanvas.cpp +++ b/src/msw/glcanvas.cpp @@ -134,17 +134,14 @@ void wxGLContext::SetCurrent() void wxGLContext::SetColour(const wxChar *colour) { - float r = 0.0; - float g = 0.0; - float b = 0.0; - wxColour *col = wxTheColourDatabase->FindColour(colour); - if (col) - { - r = (float)(col->Red()/256.0); - g = (float)(col->Green()/256.0); - b = (float)(col->Blue()/256.0); - glColor3f( r, g, b); - } + wxColour col = wxTheColourDatabase->Find(colour); + if (col.Ok()) + { + float r = (float)(col.Red()/256.0); + float g = (float)(col.Green()/256.0); + float b = (float)(col.Blue()/256.0); + glColor3f( r, g, b); + } } diff --git a/src/x11/colour.cpp b/src/x11/colour.cpp index d9ef085808..95b0c786e7 100644 --- a/src/x11/colour.cpp +++ b/src/x11/colour.cpp @@ -155,34 +155,46 @@ wxColour::wxColour( unsigned char red, unsigned char green, unsigned char blue ) M_COLDATA->m_color.pixel = 0; } -void wxColour::InitFromName( const wxString &colourName ) +/* static */ +wxColour wxColour::CreateByName(const wxString& name) { - wxColour* col; - if ( (wxTheColourDatabase) && (col = wxTheColourDatabase->FindColourNoAdd(colourName)) ) + wxColour col; + + Display *dpy = wxGlobalDisplay(); + WXColormap colormap = wxTheApp->GetMainColormap( dpy ); + XColor xcol; + if ( XParseColor( dpy, (Colormap)colormap, name.mb_str(), &xcol ) ) { - UnRef(); - if (col) Ref( *col ); + wxColourRefData *refData = new wxColourRefData; + refData->m_colormap = colormap; + refData->m_color = xcol; + col.m_refData = refData; } - else + + return col; +} + +void wxColour::InitFromName( const wxString &colourName ) +{ + // check the cache first + wxColour col; + if ( wxTheColourDatabase ) { - m_refData = new wxColourRefData(); - - M_COLDATA->m_colormap = wxTheApp->GetMainColormap( wxGlobalDisplay() ); - - if (!XParseColor( wxGlobalDisplay(), (Colormap) M_COLDATA->m_colormap, colourName.mb_str(), &M_COLDATA->m_color )) - { - // VZ: asserts are good in general but this one is triggered by - // calling wxColourDatabase::FindColour() with an - // unrecognized colour name and this can't be avoided from the - // user code, so don't give it here - // - // a better solution would be to changed code in FindColour() + col = wxTheColourDatabase->Find(colourName); + } - //wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); + if ( !col.Ok() ) + { + col = CreateByName(colourName); + } - delete m_refData; - m_refData = (wxObjectRefData *) NULL; - } + if ( col.Ok() ) + { + *this = col; + } + else + { + wxFAIL_MSG( wxT("wxColour: couldn't find colour") ); } } diff --git a/src/x11/glcanvas.cpp b/src/x11/glcanvas.cpp index 5b619202a0..0059ab89cf 100644 --- a/src/x11/glcanvas.cpp +++ b/src/x11/glcanvas.cpp @@ -138,28 +138,32 @@ void wxGLContext::SetCurrent() void wxGLContext::SetColour(const wxChar *colour) { - wxColour *the_colour = wxTheColourDatabase->FindColour(colour); - if(the_colour) { - GLboolean b; - glGetBooleanv(GL_RGBA_MODE, &b); - if(b) { - glColor3ub(the_colour->Red(), - the_colour->Green(), - the_colour->Blue()); - } else { + wxColour the_colour = wxTheColourDatabase->Find(colour); + if(the_colour.Ok()) + { + GLboolean b; + glGetBooleanv(GL_RGBA_MODE, &b); + if(b) + { + glColor3ub(the_colour.Red(), + the_colour.Green(), + the_colour.Blue()); + } + else + { #ifdef __WXMOTIF__ - the_colour->AllocColour(m_window->GetXDisplay()); + the_colour.AllocColour(m_window->GetXDisplay()); #else - the_colour->CalcPixel(wxTheApp->GetMainColormap(wxGetDisplay())); + the_colour.CalcPixel(wxTheApp->GetMainColormap(wxGetDisplay())); #endif - GLint pix = (GLint)the_colour->GetPixel(); - if(pix == -1) + GLint pix = (GLint)the_colour.GetPixel(); + if(pix == -1) { wxLogError(wxT("wxGLCanvas: cannot allocate color\n")); - return; + return; } - glIndexi(pix); - } + glIndexi(pix); + } } } -- 2.47.2