Merge branch 'msw-stattext-markup'

Implement support for markup in wxMSW wxStaticText.

See #25000.
This commit is contained in:
Vadim Zeitlin
2024-12-12 23:04:24 +01:00
5 changed files with 119 additions and 15 deletions
+10 -7
View File
@@ -27,8 +27,16 @@ class WXDLLIMPEXP_CORE wxMarkupTextBase
public:
virtual ~wxMarkupTextBase() = default;
// Update the markup string.
void SetMarkup(const wxString& markup) { m_markup = markup; }
// Update the markup string, return false if it didn't change.
bool SetMarkup(const wxString& markup)
{
if ( markup == m_markup )
return false;
m_markup = markup;
return true;
}
// Return the width and height required by the given string and optionally
// the height of the visible part above the baseline (i.e. ascent minus
@@ -81,11 +89,6 @@ public:
// Default copy ctor, assignment operator and dtor are ok.
// Update the markup string.
//
// The same rules for mnemonics as in the ctor apply to this string.
void SetMarkup(const wxString& markup) { m_markup = markup; }
// Render the markup string into the given DC in the specified rectangle.
//
// Notice that while the function uses the provided rectangle for alignment
+11
View File
@@ -42,6 +42,10 @@ public:
protected:
// implement/override some base class virtuals
#if wxUSE_MARKUP
virtual bool DoSetLabelMarkup(const wxString& markup) override;
#endif // wxUSE_MARKUP
virtual void DoSetSize(int x, int y, int w, int h,
int sizeFlags = wxSIZE_AUTO) override;
virtual wxSize DoGetBestClientSize() const override;
@@ -54,6 +58,13 @@ protected:
virtual wxString WXGetVisibleLabel() const override;
virtual void WXSetVisibleLabel(const wxString& str) override;
#if wxUSE_MARKUP
class wxMarkupText* m_markupText = nullptr;
// This is only used when m_markupText is non-null.
void WXOnPaint(wxPaintEvent& event);
#endif // wxUSE_MARKUP
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxStaticText);
};
+3 -4
View File
@@ -342,10 +342,9 @@ public:
remains unchanged.
Currently wxButton supports markup in all major ports (wxMSW, wxGTK and
wxOSX/Cocoa) while wxStaticText supports it in wxGTK and wxOSX and its
generic version (which can be used under MSW if markup support is
required). Extending support to more controls is planned in the future.
Currently wxButton and wxStaticText support markup in all major ports
(wxMSW, wxGTK and wxOSX/Cocoa). Extending support to more controls is
planned in the future.
@since 2.9.2
*/
+3 -2
View File
@@ -124,10 +124,11 @@ bool wxGenericStaticText::DoSetLabelMarkup(const wxString& markup)
if ( !wxStaticTextBase::DoSetLabelMarkup(markup) )
return false;
if ( m_markupText && !m_markupText->SetMarkup(markup) )
return true;
if ( !m_markupText )
m_markupText = new wxMarkupText(markup);
else
m_markupText->SetMarkup(markup);
AutoResizeIfNecessary();
+92 -2
View File
@@ -27,6 +27,10 @@
#include "wx/msw/private/darkmode.h"
#include "wx/msw/private/winstyle.h"
#if wxUSE_MARKUP
#include "wx/generic/private/markuptext.h"
#endif // wxUSE_MARKUP
bool wxStaticText::Create(wxWindow *parent,
wxWindowID id,
const wxString& label,
@@ -163,8 +167,12 @@ wxStaticText::MSWHandleMessage(WXLRESULT *result,
switch ( message )
{
case WM_PAINT:
// We only customize drawing of disabled labels in dark mode.
if ( ::IsWindowEnabled(GetHwnd()) || !wxMSWDarkMode::IsActive() )
// We only customize drawing of disabled plain labels in dark mode.
if ( ::IsWindowEnabled(GetHwnd()) ||
#if wxUSE_MARKUP
m_markupText ||
#endif // wxUSE_MARKUP
!wxMSWDarkMode::IsActive() )
break;
// For them, the default "greying out" of the text for the disabled
@@ -200,6 +208,16 @@ void wxStaticText::SetLabel(const wxString& label)
if ( label == m_labelOrig )
return;
#if wxUSE_MARKUP
if ( m_markupText )
{
Unbind(wxEVT_PAINT, &wxStaticText::WXOnPaint, this);
delete m_markupText;
m_markupText = nullptr;
}
#endif // wxUSE_MARKUP
#ifdef SS_ENDELLIPSIS
wxMSWWinStyleUpdater updateStyle(GetHwnd());
if ( HasFlag(wxST_ELLIPSIZE_END) )
@@ -256,5 +274,77 @@ void wxStaticText::WXSetVisibleLabel(const wxString& str)
wxWindow::SetLabel(str);
}
#if wxUSE_MARKUP
bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
{
const wxString label = RemoveMarkup(markup);
if ( label.empty() && !markup.empty() )
return false;
m_labelOrig = label;
// Don't do anything if the label didn't change.
if ( m_markupText && !m_markupText->SetMarkup(markup) )
return true;
if ( !m_markupText )
{
Bind(wxEVT_PAINT, &wxStaticText::WXOnPaint, this);
m_markupText = new wxMarkupText(markup);
}
AutoResizeIfNecessary();
return true;
}
void wxStaticText::WXOnPaint(wxPaintEvent& event)
{
// We shouldn't normally be called in this case, but ensure we don't do
// anything if we are, somehow.
if ( !m_markupText )
{
event.Skip();
return;
}
wxPaintDC dc(this);
// TODO: support transparent background for static text with markup.
dc.Clear();
const wxRect rect = GetClientRect();
if ( !IsThisEnabled() )
{
if ( wxMSWDarkMode::IsActive() )
{
wxDarkModeSettings darkModeSettings;
dc.SetTextForeground(
darkModeSettings.GetMenuColour(wxMenuColour::DisabledFg)
);
}
else // Emulate traditional greyed out disabled look.
{
dc.SetTextForeground(
wxSystemSettings::GetColour(wxSYS_COLOUR_BTNHIGHLIGHT)
);
wxRect rectShadow = rect;
rectShadow.Offset(1, 1);
m_markupText->Render(dc, rectShadow, wxMarkupText::Render_ShowAccels);
dc.SetTextForeground(
wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW)
);
}
}
m_markupText->Render(dc, rect, wxMarkupText::Render_ShowAccels);
}
#endif // wxUSE_MARKUP
#endif // wxUSE_STATTEXT