wxPanel draws over the scrollbar of wxScrolledWindow  [SOLVED]

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
ongledda
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Aug 10, 2017 2:16 am

wxPanel draws over the scrollbar of wxScrolledWindow

Postby ongledda » Fri Sep 14, 2018 7:39 am

I have a wxPanel that is being redrawn when mouse pointer has entered on it.
This panel is in a wxScrollWindow.

The problem is when the wxPanel is re-drawn, while the scrollbar of the wxScrollBarWindow is on top of it, the wxPanel will be drawn over the scrollbar.

This seems to happen only on MacOS.
My environment:
- MacOS X 10.12 Sierra
- wxcocoa (wxWidgets-3.1.1)

I have reproduced this behavior in "scroll" sample code.
I added a wxPanel inside the MyCanvas.

Created a wxPanel button:

Code: Select all

class MyButton : public wxWindow
{
   
public:
    MyButton(wxWindow *parent)
    : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxSize(100, 50))
    {
        Connect(wxEVT_PAINT, wxPaintEventHandler(MyButton::OnPaint));
        Bind(wxEVT_ENTER_WINDOW, &MyButton::mouseEntered, this);
        Bind(wxEVT_LEAVE_WINDOW, &MyButton::mouseLeave, this);
    }
   
private:
    void OnPaint(wxPaintEvent& WXUNUSED(event))
    {
        render();
    }
   
    void mouseEntered(wxMouseEvent& WXUNUSED(evt))
    {
        render(*wxYELLOW);
    }
   
    void mouseLeave(wxMouseEvent& WXUNUSED(evt))
    {
        render();
    }
   
    void render(wxColor color = *wxRED)
    {
        wxClientDC dc(this);
        dc.SetBackground(wxBrush(color));
       
        dc.Clear();
    }
};


In MyCanvass class:

Code: Select all

    // you can use either a single SetScrollbars() call or these 2 functions,
    // usually using them is better because you normally won't need to change
    // the scroll rate in the future and the sizer can be used to update the
    // virtual size automatically
    SetScrollRate( 10, 10 );
    SetVirtualSize( 500, 1000 );

    (void) new wxButton( this, ID_ADDBUTTON,  "add button", wxPoint(10,10) );
    (void) new wxButton( this, ID_DELBUTTON,  "del button", wxPoint(10,40) );
    (void) new wxButton( this, ID_MOVEBUTTON, "move button", wxPoint(150,10) );
    (void) new wxButton( this, ID_SCROLLWIN,  "scroll win", wxPoint(250,10) );
   
    MyButton *btn = new MyButton(this);

    wxPanel *test = new wxPanel( this, wxID_ANY,
                                 wxPoint(10, 110), wxSize(130,50),
                                 wxSIMPLE_BORDER | wxTAB_TRAVERSAL );
    test->SetBackgroundColour( "WHEAT" );


Screenshots:

Scrollbar is over the panel:
scrollbar_over_panel.png
scrollbar_over_panel.png (18.72 KiB) Viewed 469 times


..when mouse is hovered, the panel is redrawn over the scrollbar:
hovered.png
hovered.png (16.5 KiB) Viewed 469 times



Thanks for the help!
Last edited by ongledda on Fri Sep 14, 2018 9:55 am, edited 1 time in total.

catalin
Moderator
Moderator
Posts: 1473
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby catalin » Fri Sep 14, 2018 8:05 am

Does it still happen if you move it around so that it will not overlap any of its siblings?

User avatar
doublemax
Moderator
Moderator
Posts: 12751
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby doublemax » Fri Sep 14, 2018 9:27 am

Code: Select all

 void render(wxColor color = *wxRED)
    {
        wxClientDC dc(this);
        dc.SetBackground(wxBrush(color));
       
        dc.Clear();
    }
Don't use wxClientDC in a paint event handler, use wxPaintDC.
Use the source, Luke!

ongledda
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Aug 10, 2017 2:16 am

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby ongledda » Fri Sep 14, 2018 9:37 am

Yes, it still happens.

I do not know if I'm missing something on wxPanel drawing or not.
This issue does not seem to happen on windows.

[img]
test_code.png
[/img]
Attachments
test_code.png
test_code.png (34.26 KiB) Viewed 458 times

ongledda
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Aug 10, 2017 2:16 am

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby ongledda » Fri Sep 14, 2018 9:52 am

Don't use wxClientDC in a paint event handler, use wxPaintDC.


I tried to use wxPaintDC in the paint event handler, but it still happens.

Code: Select all

class MyButton : public wxWindow
{
   
public:
    MyButton(wxWindow *parent, wxPoint pos)
    : wxWindow(parent, wxID_ANY, pos, wxSize(100, 50))
    {
        Connect(wxEVT_PAINT, wxPaintEventHandler(MyButton::OnPaint));
        Bind(wxEVT_ENTER_WINDOW, &MyButton::mouseEntered, this);
        Bind(wxEVT_LEAVE_WINDOW, &MyButton::mouseLeave, this);
    }
   
private:
    void OnPaint(wxPaintEvent& WXUNUSED(event))
    {
        wxPaintDC dc(this);
        render(dc);
    }
   
    void mouseEntered(wxMouseEvent& WXUNUSED(evt))
    {
        wxClientDC dc(this);
        render(dc, *wxYELLOW);
    }
   
    void mouseLeave(wxMouseEvent& WXUNUSED(evt))
    {
        wxClientDC dc(this);
        render(dc);
    }
   
    void render(wxDC& dc, wxColor color = *wxRED)
    {
        dc.SetBackground(wxBrush(color));
        dc.Clear();
    }
};

User avatar
doublemax
Moderator
Moderator
Posts: 12751
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby doublemax » Fri Sep 14, 2018 9:59 am

You're still using wxClientDC in the mouse enter/leave handlers. Try disabling these for a test.
Use the source, Luke!

ongledda
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Aug 10, 2017 2:16 am

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby ongledda » Fri Sep 14, 2018 10:17 am

Disabling the enter/leave handlers and drawing the panel in the paint event using wxPaintDC is working fine, it does not overlap the scrollbar.

But, I'd like to redraw the panel on mouse enter or leave, without overlapping the scrollbar.

User avatar
doublemax
Moderator
Moderator
Posts: 12751
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxPanel draws over the scrollbar of wxScrolledWindow  [SOLVED]

Postby doublemax » Fri Sep 14, 2018 10:22 am

Get rid of the render method, do your drawing in the paint event handler only. Use a flag that controls the color.

Code: Select all

bool m_mouse_inside;    // member of class

void OnPaint(wxPaintEvent& WXUNUSED(event))
{
    wxPaintDC dc(this);
   
    dc.SetBackground( m_mouse_inside ? *wxYELLOW : *wxRED );
    dc.Clear();
}

void mouseEntered(wxMouseEvent& WXUNUSED(evt))
{
  m_mouse_inside = true;
  Refresh();
}

void mouseLeave(wxMouseEvent& WXUNUSED(evt))
{
  m_mouse_inside = false;
  Refresh();
}
Use the source, Luke!

ongledda
In need of some credit
In need of some credit
Posts: 5
Joined: Thu Aug 10, 2017 2:16 am

Re: wxPanel draws over the scrollbar of wxScrolledWindow

Postby ongledda » Fri Sep 14, 2018 10:31 am

Your answer worked!

Thank you very much for the help. :D


Return to “C++ Development”

Who is online

Users browsing this forum: No registered users and 33 guests