]> git.saurik.com Git - wxWidgets.git/commitdiff
Simplify and correct MSW selection of background brush for children painting.
authorVadim Zeitlin <vadim@wxwidgets.org>
Fri, 21 May 2010 12:51:20 +0000 (12:51 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Fri, 21 May 2010 12:51:20 +0000 (12:51 +0000)
Check for the transparency of the window background in MSWGetBgBrush() itself
and let MSWGetBgBrushForChild() just return the brush to be used.

This required adding a MSW-specific hack to wxPanel to allow inheriting
background of wxNotebook by its children which is slightly ugly but is exactly
what we need to do in this case and makes the rest of the code much simpler.

Also add a possibility to set the background of the page containing the
control being tested to the widgets sample to test for background inheritance.
This shows problems with wxSlider and wxRadioBox backgrounds but they're not
new with this patch.

Closes #12057.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64372 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/generic/panelg.h
include/wx/msw/window.h
samples/widgets/widgets.cpp
src/generic/panelg.cpp
src/msw/control.cpp
src/msw/notebook.cpp
src/msw/statbox.cpp
src/msw/window.cpp

index 5824c0efc43890083e62523cdcd72b0d016255e7..4aa4c8c9886c87b898bd1ca4584d653938acc596 100644 (file)
@@ -76,6 +76,20 @@ public:
     virtual bool IsCanvasWindow() const { return true; }
 #endif
 
+#ifdef __WXMSW__
+    // This is a hack to support inheriting of background through child
+    // wxPanel: at least wxNotebook needs this under wxMSW as its background
+    // should apply to its children which are usually wxPanels which normally
+    // don't have a transparent background. Calling this function allows to
+    // change this for the panels which are used as notebook pages.
+    void MSWSetTransparentBackground(bool isTransparent = true)
+    {
+        m_isTransparent = isTransparent;
+    }
+
+    virtual bool HasTransparentBackground() { return m_isTransparent; }
+#endif // __WXMSW__
+
     WX_DECLARE_CONTROL_CONTAINER();
 
 protected:
@@ -85,6 +99,11 @@ protected:
     // choose the default border for this window
     virtual wxBorder GetDefaultBorder() const { return wxWindowBase::GetDefaultBorder(); }
 
+private:
+#ifdef __WXMSW__
+    bool m_isTransparent;
+#endif // __WXMSW__
+
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxPanel)
     DECLARE_EVENT_TABLE()
 };
index 96b6d1bbe6dd96c9c59f265d46db5b126ceaf428..33b4354107859885c4653756466d2dcd0a781b68 100644 (file)
@@ -395,16 +395,16 @@ public:
     // the hDC parameter is the DC background will be drawn on, it can be used
     // to call SetBrushOrgEx() on it if the returned brush is a bitmap one
     //
-    // child parameter is never NULL
+    // child parameter is never NULL, it can be this window itself or one of
+    // its (grand)children
     //
     // the base class version returns a solid brush if we have a non default
     // background colour or 0 otherwise
     virtual WXHBRUSH MSWGetBgBrushForChild(WXHDC hDC, wxWindowMSW *child);
 
     // return the background brush to use for painting the given window by
-    // quering the parent windows via their MSWGetBgBrushForChild() recursively
-    WXHBRUSH MSWGetBgBrush(WXHDC hDC) { return MSWGetBgBrush(hDC, this); }
-    WXHBRUSH MSWGetBgBrush(WXHDC hDC, wxWindowMSW *child);
+    // querying the parent windows via MSWGetBgBrushForChild() recursively
+    WXHBRUSH MSWGetBgBrush(WXHDC hDC);
 
     enum MSWThemeColour
     {
index 53a7b37016fe6fb9a89deb1a23e131506afd9bf3..d71656dd40bcc79287b0014d94912fb86b46e04d 100644 (file)
@@ -75,6 +75,7 @@ enum
 #endif // wxUSE_TOOLTIPS
     Widgets_SetFgColour,
     Widgets_SetBgColour,
+    Widgets_SetPageBg,
     Widgets_SetFont,
     Widgets_Enable,
 
@@ -159,6 +160,7 @@ protected:
 #endif // wxUSE_TOOLTIPS
     void OnSetFgCol(wxCommandEvent& event);
     void OnSetBgCol(wxCommandEvent& event);
+    void OnSetPageBg(wxCommandEvent& event);
     void OnSetFont(wxCommandEvent& event);
     void OnEnable(wxCommandEvent& event);
     void OnSetBorder(wxCommandEvent& event);
@@ -285,6 +287,7 @@ BEGIN_EVENT_TABLE(WidgetsFrame, wxFrame)
 
     EVT_MENU(Widgets_SetFgColour, WidgetsFrame::OnSetFgCol)
     EVT_MENU(Widgets_SetBgColour, WidgetsFrame::OnSetBgCol)
+    EVT_MENU(Widgets_SetPageBg,   WidgetsFrame::OnSetPageBg)
     EVT_MENU(Widgets_SetFont,     WidgetsFrame::OnSetFont)
     EVT_MENU(Widgets_Enable,      WidgetsFrame::OnEnable)
 
@@ -381,6 +384,7 @@ WidgetsFrame::WidgetsFrame(const wxString& title)
 #endif // wxUSE_TOOLTIPS
     menuWidget->Append(Widgets_SetFgColour, wxT("Set &foreground...\tCtrl-F"));
     menuWidget->Append(Widgets_SetBgColour, wxT("Set &background...\tCtrl-B"));
+    menuWidget->Append(Widgets_SetPageBg,   wxT("Set &page background...\tShift-Ctrl-B"));
     menuWidget->Append(Widgets_SetFont,     wxT("Set f&ont...\tCtrl-O"));
     menuWidget->AppendCheckItem(Widgets_Enable,  wxT("&Enable/disable\tCtrl-E"));
 
@@ -766,16 +770,35 @@ void WidgetsFrame::OnSetTooltip(wxCommandEvent& WXUNUSED(event))
 
 #endif // wxUSE_TOOLTIPS
 
-void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
+namespace
+{
+
+// Trivial wrapper for wxGetColourFromUser() which also does something even if
+// the colour dialog is not available in the current build (which may happen
+// for the ports in development and it is still useful to see how colours work)
+wxColour GetColourFromUser(wxWindow *parent, const wxColour& colDefault)
 {
 #if wxUSE_COLOURDLG
+    return wxGetColourFromUser(parent, colDefault);
+#else // !wxUSE_COLOURDLG
+    if ( colDefault == *wxBLACK )
+        return *wxWHITE;
+    else
+        return *wxBLACK;
+#endif // wxUSE_COLOURDLG/!wxUSE_COLOURDLG
+}
+
+} // anonymous namespace
+
+void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
+{
     // allow for debugging the default colour the first time this is called
     WidgetsPage *page = CurrentPage();
 
     if (!m_colFg.Ok())
         m_colFg = page->GetForegroundColour();
 
-    wxColour col = wxGetColourFromUser(this, m_colFg);
+    wxColour col = GetColourFromUser(this, m_colFg);
     if ( !col.Ok() )
         return;
 
@@ -789,20 +812,16 @@ void WidgetsFrame::OnSetFgCol(wxCommandEvent& WXUNUSED(event))
         (*it)->SetForegroundColour(m_colFg);
         (*it)->Refresh();
     }
-#else
-    wxLogMessage(wxT("Colour selection dialog not available in current build."));
-#endif
 }
 
 void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
 {
-#if wxUSE_COLOURDLG
     WidgetsPage *page = CurrentPage();
 
     if ( !m_colBg.Ok() )
         m_colBg = page->GetBackgroundColour();
 
-    wxColour col = wxGetColourFromUser(this, m_colBg);
+    wxColour col = GetColourFromUser(this, m_colBg);
     if ( !col.Ok() )
         return;
 
@@ -816,9 +835,16 @@ void WidgetsFrame::OnSetBgCol(wxCommandEvent& WXUNUSED(event))
         (*it)->SetBackgroundColour(m_colBg);
         (*it)->Refresh();
     }
-#else
-    wxLogMessage(wxT("Colour selection dialog not available in current build."));
-#endif
+}
+
+void WidgetsFrame::OnSetPageBg(wxCommandEvent& WXUNUSED(event))
+{
+    wxColour col = GetColourFromUser(this, GetBackgroundColour());
+    if ( !col.Ok() )
+        return;
+
+    CurrentPage()->SetBackgroundColour(col);
+    CurrentPage()->Refresh();
 }
 
 void WidgetsFrame::OnSetFont(wxCommandEvent& WXUNUSED(event))
index 3bff49d8d709497c2e080076093fe53dd74b827f..ee4a901a06c105d6d431934a31a54f54a9d5f70b 100644 (file)
@@ -104,6 +104,10 @@ WX_DELEGATE_TO_CONTROL_CONTAINER(wxPanel, wxWindow)
 void wxPanel::Init()
 {
     WX_INIT_CONTROL_CONTAINER();
+
+#ifdef __WXMSW__
+    m_isTransparent = false;
+#endif // __WXMSW__
 }
 
 bool wxPanel::Create(wxWindow *parent, wxWindowID id,
index 1cc9dce77e25cf1e66285c1b3372e0cc582b0612..056794b517d96eee3368a24a770f67095a5a1629 100644 (file)
@@ -368,7 +368,7 @@ WXHBRUSH wxControl::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
     if ( !colBg.Ok() )
     {
         if ( wxWindow *win = wxFindWinFromHandle(hWnd) )
-            hbr = MSWGetBgBrush(pDC, win);
+            hbr = win->MSWGetBgBrush(pDC);
 
         // if the control doesn't have any bg colour, foreground colour will be
         // ignored as the return value would be 0 -- so forcefully give it a
index 7148d34139424790a81ead54c004b9eb769824d6..793d0dd8d7355391b1b118a105f30b743d2702f4 100644 (file)
@@ -818,6 +818,14 @@ bool wxNotebook::InsertPage(size_t nPage,
     // succeeded: save the pointer to the page
     m_pages.Insert(pPage, nPage);
 
+    // also ensure that the notebook background is used for its pages by making
+    // them transparent: this ensures that MSWGetBgBrush() queries the notebook
+    // for the background brush to be used for erasing them
+    if ( wxPanel *panel = wxDynamicCast(pPage, wxPanel) )
+    {
+        panel->MSWSetTransparentBackground();
+    }
+
     // we may need to adjust the size again if the notebook size changed:
     // normally this only happens for the first page we add (the tabs which
     // hadn't been there before are now shown) but for a multiline notebook it
@@ -1252,23 +1260,7 @@ void wxNotebook::UpdateBgBrush()
 
 WXHBRUSH wxNotebook::MSWGetBgBrushForChild(WXHDC hDC, wxWindow *child)
 {
-    // Only apply to notebook pages and transparent children, see
-    // wxWindow::MSWGetBgBrushForChild() for explanation
-    bool shouldApply;
-    if ( child->GetParent() == this )
-    {
-        // notebook page -- apply background
-        shouldApply = true;
-    }
-    else
-    {
-        // controls in a notebook page with transparent background should
-        // be handled too
-        shouldApply = child->HasTransparentBackground() &&
-                      child->GetParent()->GetParent() == this;
-    }
-
-    if ( m_hbrBackground && shouldApply )
+    if ( m_hbrBackground )
     {
         // before drawing with the background brush, we need to position it
         // correctly
index 0a5b78abd29f011495f93c4486d82dc799fd4061..3c2dfcf06d00a72faf08c8b3b91137649cb34c50 100644 (file)
@@ -363,16 +363,15 @@ void wxStaticBox::PaintBackground(wxDC& dc, const RECT& rc)
     //     we did it
     //  3. this is backwards compatible behaviour and some people rely on it,
     //     see http://groups.google.com/groups?selm=4252E932.3080801%40able.es
-    wxWindow *parent = GetParent();
     wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
-    HBRUSH hbr = (HBRUSH)parent->MSWGetBgBrush(impl->GetHDC(), this);
+    HBRUSH hbr = MSWGetBgBrush(impl->GetHDC());
 
     // if there is no special brush for painting this control, just use the
     // solid background colour
     wxBrush brush;
     if ( !hbr )
     {
-        brush = wxBrush(parent->GetBackgroundColour());
+        brush = wxBrush(GetParent()->GetBackgroundColour());
         hbr = GetHbrushOf(brush);
     }
 
index b74739669998bf012430bc93ef33da61c7fa8bb2..4767df40887ff6c02d041d6ce7fae6a3c0dfc3a0 100644 (file)
@@ -4916,42 +4916,32 @@ bool wxWindowMSW::DoEraseBackground(WXHDC hDC)
 }
 
 WXHBRUSH
-wxWindowMSW::MSWGetBgBrushForChild(WXHDC WXUNUSED(hDC), wxWindowMSW *child)
+wxWindowMSW::MSWGetBgBrushForChild(WXHDC WXUNUSED(hDC),
+                                   wxWindowMSW * WXUNUSED(child))
 {
     if ( m_hasBgCol )
     {
-        // our background colour applies to:
-        //  1. this window itself, always
-        //  2. all children unless the colour is "not inheritable"
-        //  3. even if it is not inheritable, our immediate transparent
-        //     children should still inherit it -- but not any transparent
-        //     children because it would look wrong if a child of non
-        //     transparent child would show our bg colour when the child itself
-        //     does not
-        if ( child == this ||
-                m_inheritBgCol ||
-                    (child->HasTransparentBackground() &&
-                        child->GetParent() == this) )
-        {
-            // draw children with the same colour as the parent
-            wxBrush *
-                brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour());
+        wxBrush *
+            brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour());
 
-            return (WXHBRUSH)GetHbrushOf(*brush);
-        }
+        return (WXHBRUSH)GetHbrushOf(*brush);
     }
 
     return 0;
 }
 
-WXHBRUSH wxWindowMSW::MSWGetBgBrush(WXHDC hDC, wxWindowMSW *child)
+WXHBRUSH wxWindowMSW::MSWGetBgBrush(WXHDC hDC)
 {
     for ( wxWindowMSW *win = this; win; win = win->GetParent() )
     {
-        WXHBRUSH hBrush = win->MSWGetBgBrushForChild(hDC, child);
+        WXHBRUSH hBrush = win->MSWGetBgBrushForChild(hDC, this);
         if ( hBrush )
             return hBrush;
 
+        // don't use the parent background if we're not transparent
+        if ( !win->HasTransparentBackground() )
+            break;
+
         // background is not inherited beyond top level windows
         if ( win->IsTopLevel() )
             break;