Background of wxPanel inside wxNotebook not inheriting under MSW

15 views
Skip to first unread message

d0

unread,
Apr 1, 2021, 5:13:04 AMApr 1
to wx-u...@googlegroups.com
Hi y'all,

the docs for wxNotebook suggest doing a "panel.SetBackgroundColour(notebook.GetThemeBackgroundColour())" for Windows. This does work just fine with native controls, but I noticed that because HasTransparentBackground() is still true for the panel, inheritance of the background color does not work. Custom drawn controls end up with the wrong background color.

A minimal reproducer (Python, nothing wxPython specific though):

import wx

from wx.lib.stattext import GenStaticText

 

app = wx.App()

frm = wx.Frame(None, title='Frame')

nb = wx.Notebook(frm)

 

panel = wx.Panel(nb)

panel.Sizer = wx.BoxSizer(wx.VERTICAL)

label1 = wx.StaticText(panel, label="Hello world (wx.StaticText)")

label2 = GenStaticText(panel, label="Hello world (GenStaticText)")

panel.Sizer.Add(label1)

panel.Sizer.Add(label2)

panel.SetBackgroundColour(nb.GetThemeBackgroundColour())  # As per the docs

 

nb.AddPage(panel, "Tab1")

 

print(wx.version())  # 4.1.1 msw (phoenix) wxWidgets 3.1.5

print(nb.GetThemeBackgroundColour())  # White, as expected

print(panel.GetBackgroundColour())  # White

print(panel.HasTransparentBackground())  # True(!)

print(label2.GetBackgroundColour())  # Grey

 

frm.Show()

app.MainLoop()

label1 has a white background, as it should, while label2 gets a grey background. This still happens even when multiple wxPanels are nested, and even when their background colors are explicitly set.

Is this a bug or "works as intended"?

Cheers, Marian

Igor Korot

unread,
Apr 1, 2021, 8:00:47 AMApr 1
to wx-u...@googlegroups.com
Hi,


On Thu, Apr 1, 2021, 4:13 AM d0 <pub...@enkore.de> wrote:
Hi y'all,

the docs for wxNotebook suggest doing a "panel.SetBackgroundColour(notebook.GetThemeBackgroundColour())" for Windows. This does work just fine with native controls, but I noticed that because HasTransparentBackground() is still true for the panel, inheritance of the background color does not work. Custom drawn controls end up with the wrong background color.

A minimal reproducer (Python, nothing wxPython specific though):

What version of wx do you use?
What theme?

Thank you.


import wx

from wx.lib.stattext import GenStaticText

 

app = wx.App()

frm = wx.Frame(None, title='Frame')

nb = wx.Notebook(frm)

 

panel = wx.Panel(nb)

panel.Sizer = wx.BoxSizer(wx.VERTICAL)

label1 = wx.StaticText(panel, label="Hello world (wx.StaticText)")

label2 = GenStaticText(panel, label="Hello world (GenStaticText)")

panel.Sizer.Add(label1)

panel.Sizer.Add(label2)

panel.SetBackgroundColour(nb.GetThemeBackgroundColour())  # As per the docs

 

nb.AddPage(panel, "Tab1")

 

print(wx.version())  # 4.1.1 msw (phoenix) wxWidgets 3.1.5

print(nb.GetThemeBackgroundColour())  # White, as expected

print(panel.GetBackgroundColour())  # White

print(panel.HasTransparentBackground())  # True(!)

print(label2.GetBackgroundColour())  # Grey

 

frm.Show()

app.MainLoop()

label1 has a white background, as it should, while label2 gets a grey background. This still happens even when multiple wxPanels are nested, and even when their background colors are explicitly set.

Is this a bug or "works as intended"?

Cheers, Marian

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/CAEi-PstptWjk%3D0%2BqmtOyS6kug7yAKVrcXNwpj7eBfOeGXCy3%3DQ%40mail.gmail.com.

Vadim Zeitlin

unread,
Apr 1, 2021, 12:56:34 PMApr 1
to wx-u...@googlegroups.com
On Thu, 1 Apr 2021 11:12:49 +0200 d0 wrote:

d> the docs for wxNotebook suggest doing a
d> "panel.SetBackgroundColour(notebook.GetThemeBackgroundColour())" for
d> Windows. This does work just fine with native controls, but I noticed that
d> because HasTransparentBackground() is still true for the panel, inheritance
d> of the background color does not work. Custom drawn controls end up with
d> the wrong background color.
d>
d> A minimal reproducer (Python, nothing wxPython specific though):

Thanks for the example! I've transcribed it (not quite faithfully) to C++
thus:

---------------------------------- >8 --------------------------------------
diff --git a/samples/minimal/minimal.cpp b/samples/minimal/minimal.cpp
index 470e765423..cdaad61626 100644
--- a/samples/minimal/minimal.cpp
+++ b/samples/minimal/minimal.cpp
@@ -26,6 +26,9 @@
#include "wx/wx.h"
#endif

+#include "wx/notebook.h"
+#include "wx/generic/stattextg.h"
+
// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------
@@ -175,6 +178,22 @@ bool MyApp::OnInit()
CreateStatusBar(2);
SetStatusText("Welcome to wxWidgets!");
#endif // wxUSE_STATUSBAR
+
+ auto nb = new wxNotebook(this, wxID_ANY);
+ auto panel = new wxPanel(nb);
+ auto sizer = new wxBoxSizer(wxVERTICAL);
+ sizer->Add(new wxStaticText(panel, wxID_ANY, "Native"), wxSizerFlags().Border());
+ sizer->Add(new wxGenericStaticText(panel, wxID_ANY, "Generic"), wxSizerFlags().Border());
+ panel->SetSizer(sizer);
+ panel->SetBackgroundColour(nb->GetThemeBackgroundColour());
+ nb->AddPage(panel, "Theme bg");
+
+ panel = new wxPanel(nb);
+ sizer = new wxBoxSizer(wxVERTICAL);
+ sizer->Add(new wxStaticText(panel, wxID_ANY, "Native"), wxSizerFlags().Border());
+ sizer->Add(new wxGenericStaticText(panel, wxID_ANY, "Generic"), wxSizerFlags().Border());
+ panel->SetSizer(sizer);
+ nb->AddPage(panel, "Default bg");
}


---------------------------------- >8 --------------------------------------

Visually, I don't see any difference under neither MSW 7 nor MSW 10 with
the default theme, but I do see the difference if I explicitly turn off the
use of themes for the executable: in this case, the first page has grey
background, while the second one has the dark grey one, which shows that
explicitly setting GetThemeBackgroundColour() is still useful (which has
surprised me, and not in a good way, as I think it should just work right
automatically out of the box and thought this was fixed a long time ago).
However the backgrounds of the generic and native labels still look the
same.

So I wonder which system and theme do you use for them to appear
differently? Or do they also appear the same to you and the problem is
just the part below?

d> print(wx.version()) # 4.1.1 msw (phoenix) wxWidgets 3.1.5
d>
d> print(nb.GetThemeBackgroundColour()) # White, as expected
d>
d> print(panel.GetBackgroundColour()) # White
d>
d> print(panel.HasTransparentBackground()) # True(!)
d>
d> print(label2.GetBackgroundColour()) # Grey

But I do see this and agree that it seems wrong. I'll try to think about
it more, but for now I'd like to understand whether we're seeing the same
thing or if you see another problem, which I don't see, for some reason.

Thanks,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

d0

unread,
Apr 1, 2021, 2:40:53 PMApr 1
to wx-u...@googlegroups.com
Hi Vadim,

the bigger issue is that the BackgroundColour of the panel won't be inherited to controls, so it looks like custom-drawn controls either do something wrong or the native widgets (like wxStaticText in this example) do something different from how all the custom controls draw their backgrounds. I'm not sure how background drawing works, to be honest I've gotten quite confused about it.

I'm using GenStaticText only as a placeholder here - every other widget which draws its background based on this->GetBackgroundColour() exhibits the same issue.

I don't know if attachments are allowed on this list, I attached a screenshot (wxMSW, Windows 10) showing the problem.

Cheers, Marian
Screenshot 2021-04-01 203407.png
Reply all
Reply to author
Forward
0 new messages