diff --git a/include/wx/gdicmn.h b/include/wx/gdicmn.h index 2f5f8ee99f..613c977ce8 100644 --- a/include/wx/gdicmn.h +++ b/include/wx/gdicmn.h @@ -923,6 +923,9 @@ public: wxColour Find(const wxString& name) const; wxString FindName(const wxColour& colour) const; + // list all known colours by name + wxVector GetAllNames() const; + // add a new colour to the database void AddColour(const wxString& name, const wxColour& colour); diff --git a/interface/wx/gdicmn.h b/interface/wx/gdicmn.h index 6cb437f9e7..896402e7fa 100644 --- a/interface/wx/gdicmn.h +++ b/interface/wx/gdicmn.h @@ -871,6 +871,9 @@ const wxPoint wxDefaultPosition; @endTable + See the "Database colours" page of the @ref page_samples_drawing to see how + all these colours look like. + @library{wxcore} @category{gdi} @@ -903,6 +906,13 @@ public: colour is not found in the database. */ wxString FindName(const wxColour& colour) const; + + /** + List all known colours by name. + + @since 3.3.0 + */ + wxVector GetAllNames() const; }; diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index f6a102dd23..a39080d16b 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -155,6 +155,8 @@ protected: void DrawDefault(wxDC& dc); void DrawGradients(wxDC& dc); void DrawSystemColours(wxDC& dc); + void DrawDatabaseColours(wxDC& dc); + void DrawColour(wxDC& dc, const wxFont& mono, wxCoord x, const wxRect& r, const wxString& colourName, const wxColour& col); void DrawRegionsHelper(wxDC& dc, wxCoord x, bool firstTime); @@ -304,6 +306,7 @@ enum File_ShowGraphics, #endif File_ShowSystemColours, + File_ShowDatabaseColours, File_ShowGradients, MenuShow_Last = File_ShowGradients, @@ -1733,23 +1736,50 @@ void MyCanvas::DrawSystemColours(wxDC& dc) for (int i = 0; i < wxSYS_COLOUR_MAX; i++) { - wxString colourName(sysColours[i].name); - wxColour c(wxSystemSettings::GetColour(sysColours[i].index)); - - { - wxDCFontChanger setMono(dc, mono); - dc.DrawText(c.GetAsString(wxC2S_HTML_SYNTAX), x, r.y); - } - - dc.SetBrush(wxBrush(c)); - dc.DrawRectangle(r); - - dc.DrawText(colourName, r.GetRight() + x, r.y); - + DrawColour(dc, mono, x, r, sysColours[i].name, wxSystemSettings::GetColour(sysColours[i].index)); r.y += lineHeight; } } +void MyCanvas::DrawDatabaseColours(wxDC& dc) +{ + // initial setup to compute coordinates is same as DrawSystemColours + wxFont mono(wxFontInfo().Family(wxFONTFAMILY_TELETYPE)); + wxSize textSize; + { + wxDCFontChanger setMono(dc, mono); + textSize = dc.GetTextExtent("#01234567"); + } + + int lineHeight = textSize.GetHeight(); + wxCoord x(FromDIP(10)); + wxRect r(textSize.GetWidth() + x, x, dc.FromDIP(100), lineHeight); + + wxString title = "wxColourDatabase colours"; + dc.DrawText(title, x, r.y); + r.y += 3*lineHeight; + + const wxVector names(wxTheColourDatabase->GetAllNames()); + for (wxVector::const_iterator p = names.begin(); p != names.end(); ++p) + { + DrawColour(dc, mono, x, r, *p, wxTheColourDatabase->Find(*p)); + r.y += lineHeight; + } +} + +void MyCanvas::DrawColour(wxDC& dc, const wxFont& mono, wxCoord x, const wxRect& r, const wxString& colourName, const wxColour& col) +{ + { + wxDCFontChanger setMono(dc, mono); + dc.DrawText(col.GetAsString(wxC2S_HTML_SYNTAX), x, r.y); + } + + dc.SetBrush(wxBrush(col)); + dc.DrawRectangle(r); + + dc.DrawText(colourName, r.GetRight() + x, r.y); +} + void MyCanvas::DrawRegions(wxDC& dc) { dc.DrawText("You should see a red rect partly covered by a cyan one " @@ -1970,6 +2000,10 @@ void MyCanvas::Draw(wxDC& pdc) DrawSystemColours(dc); break; + case File_ShowDatabaseColours: + DrawDatabaseColours(dc); + break; + default: break; } @@ -2253,6 +2287,7 @@ MyFrame::MyFrame(const wxString& title) menuScreen->Append(File_ShowGraphics, "&Graphics screen"); #endif menuScreen->Append(File_ShowSystemColours, "System &colours"); + menuScreen->Append(File_ShowDatabaseColours, "Databa&se colours"); wxMenu *menuFile = new wxMenu; #if wxUSE_GRAPHICS_CONTEXT diff --git a/src/common/gdicmn.cpp b/src/common/gdicmn.cpp index db8a01f961..c700cb153a 100644 --- a/src/common/gdicmn.cpp +++ b/src/common/gdicmn.cpp @@ -455,6 +455,22 @@ wxString wxColourDatabase::FindName(const wxColour& colour) const return wxEmptyString; } +wxVector wxColourDatabase::GetAllNames() const +{ + wxColourDatabase * const self = wxConstCast(this, wxColourDatabase); + self->Initialize(); + + wxVector names; + names.reserve(m_map->size()); + + typedef wxStringToColourHashMap::iterator iterator; + + for ( iterator it = m_map->begin(), en = m_map->end(); it != en; ++it ) + names.push_back(it->first); + + return names; +} + // ============================================================================ // stock objects // ============================================================================