Skip to content

Commit

Permalink
Merge branch 'msw-stattext-markup'
Browse files Browse the repository at this point in the history
Implement support for markup in wxMSW wxStaticText.

See wxWidgets#25000.
  • Loading branch information
vadz committed Dec 12, 2024
2 parents 87c1aae + 9aedc79 commit 0f26d71
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 15 deletions.
17 changes: 10 additions & 7 deletions include/wx/generic/private/markuptext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,11 +89,6 @@ class WXDLLIMPEXP_CORE wxMarkupText : public wxMarkupTextBase

// 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
Expand Down
11 changes: 11 additions & 0 deletions include/wx/msw/stattext.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class WXDLLIMPEXP_CORE wxStaticText : public wxStaticTextBase

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;
Expand All @@ -54,6 +58,13 @@ class WXDLLIMPEXP_CORE wxStaticText : public wxStaticTextBase
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);
};

Expand Down
7 changes: 3 additions & 4 deletions interface/wx/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,9 @@ class wxControl : public wxWindow
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
*/
Expand Down
5 changes: 3 additions & 2 deletions src/generic/stattextg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
94 changes: 92 additions & 2 deletions src/msw/stattext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) )
Expand Down Expand Up @@ -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

0 comments on commit 0f26d71

Please sign in to comment.