Add wxColourDatabase::GetAllNames()

Return all known colour names from this function and show them in the
drawing sample.

Closes #9264.

Closes #22724.
This commit is contained in:
Kenneth Porter 2022-08-15 17:09:09 -07:00 committed by Vadim Zeitlin
parent 9b0f715f55
commit 974a7658da
4 changed files with 77 additions and 13 deletions

View file

@ -923,6 +923,9 @@ public:
wxColour Find(const wxString& name) const;
wxString FindName(const wxColour& colour) const;
// list all known colours by name
wxVector<wxString> GetAllNames() const;
// add a new colour to the database
void AddColour(const wxString& name, const wxColour& colour);

View file

@ -871,6 +871,9 @@ const wxPoint wxDefaultPosition;
</td></tr>
@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<wxString> GetAllNames() const;
};

View file

@ -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<wxString> names(wxTheColourDatabase->GetAllNames());
for (wxVector<wxString>::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

View file

@ -455,6 +455,22 @@ wxString wxColourDatabase::FindName(const wxColour& colour) const
return wxEmptyString;
}
wxVector<wxString> wxColourDatabase::GetAllNames() const
{
wxColourDatabase * const self = wxConstCast(this, wxColourDatabase);
self->Initialize();
wxVector<wxString> 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
// ============================================================================