How to force wxToolbar background color under OS X ?

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

How to force wxToolbar background color under OS X ?

Post by eranon »

Hello,

I have several wxToolbar for which I would like to force the background color. It works under Windows using SetBackgroundColour(), but this way seems to have no effect under OS X (tested under OS X 10.8 and wxWidgets 3.0.0).

Also, knowing my toolbars are with a 16x16 px buttons (and not positionned at standard locations), I indicated to not use the native control (using call below during app-init). However, with or without this, SetBackgroundColour is ignored (see EDIT1) :

Code: Select all

wxSystemOptions::SetOption("mac.toolbar.no-native", 1);
So, does OS X is a specific case (what a surprise - huuuu) ? There's not any warning in the SetBackgroundColour()'s documentation. Is there a special trick to know... Should I derive wxToolbar and override a specific virtual method or just handle the EVT_ERASE_BACKGROUND through Bind() or Connect() ?

What's the right way to follow for Mac, please ?

--
EDIT1 : Well, certainly that without "mac.toolbar.no-native" I'm with non-native toolbar too (implicitely), because the toolbar is not directly a frame's child. So, the question remains : how to force background color of a non-native wxToolbar under OSX.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to force wxToolbar background color under OS X ?

Post by eranon »

Tried handling EVT_ERASE_BACKGROUND like this and still no background color change :roll:

Code: Select all

/* use of connect rather than bind is done willingly here ;) */
bar1->Connect(wxID_ANY, wxEVT_ERASE_BACKGROUND, wxEraseEventHandler(MyFrame::OnToolbarEraseBackground), NULL, this);

void MyFrame::OnToolbarEraseBackground(wxEraseEvent &event)
{
    // Force background color of the given toolbar
    wxDC* pdc = event.GetDC();
    wxBrush brush(wxColour(255,0,0));   // red during test only :)
    pdc->SetBackground(brush);
    pdc->Clear();
    /* no skip event to prevent default system behavior */
}
The event handler is well fired and event.GetId() well return the right ID (so, no mistake about concerned toolbar), but the background color doesn't change (still the OSX's dark-grey) :?
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to force wxToolbar background color under OS X ?

Post by eranon »

Succeeded providing a wxEVT_PAINT handler rather than a EVT_ERASE_BACKGROUND one.
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
alan93
Experienced Solver
Experienced Solver
Posts: 93
Joined: Wed Feb 06, 2013 9:33 pm

Re: How to force wxToolbar background color under OS X ?

Post by alan93 »

and yet wxEVT_PAINT event has no GetDC().

Can I ask how you worked that out? I think I could use that.
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Re: How to force wxToolbar background color under OS X ?

Post by eranif »

You just need to construct a wxPaintDC On the stack and use that
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
alan93
Experienced Solver
Experienced Solver
Posts: 93
Joined: Wed Feb 06, 2013 9:33 pm

Re: How to force wxToolbar background color under OS X ?

Post by alan93 »

Great thanks, I'll try that.
alan93
Experienced Solver
Experienced Solver
Posts: 93
Joined: Wed Feb 06, 2013 9:33 pm

Re: How to force wxToolbar background color under OS X ?

Post by alan93 »

This didn't work for me

Code: Select all


bar1->Connect(wxID_ANY, wxEVT_PAINT, wxPaintEventHandler( MyFrame::OnToolbarEraseBackground), NULL, this->GetEventHandler() );

void MyFrame::OnToolbarEraseBackground(wxPaintEvent &event) // doesn't execute
{
    // Force background color of the given toolbar
    wxPaintDC dc(this) ;
    wxBrush brush(wxColour(255,0,0));   // red during test only :)
    dc.SetBackground(brush);
    dc.Clear();
    /* no skip event to prevent default system behavior */
}
User avatar
eranon
Can't get richer than this
Can't get richer than this
Posts: 867
Joined: Sun May 13, 2012 11:42 pm
Location: France
Contact:

Re: How to force wxToolbar background color under OS X ?

Post by eranon »

Hello,

I just see your MP on 12 Aug 2014 now (oops). Not came here since a long time. Well, I take a look at my code... wait a "minute"...

OK, here is the way I gone :

Code: Select all

#ifdef __WXOSX__
void DedeeFrame::OnToolbarPaint(wxPaintEvent &event)
{
    // Forces color of the given toolbar
    // REF : inspired by <wxWidgets-3.0.0>/src/osx/cocoa/toolbar.mm:OnPaint()
    // NB : here, the wxTB_LEFT/RIGHT/BOTTOM/TOP are ignored (if needed ***LATER, see in original REF source)
    wxWindow* pWnd = (wxWindow*) event.GetEventObject();
    wxPaintDC dc(pWnd);

    wxBrush brush(pWnd->GetParent()->GetBackgroundColour());
    dc.SetBackground(brush);
    dc.Clear();

    /* no event skip to prevent default system color */
}
#endif // __WXOSX__
[Ind. dev. - wxWidgets 3.0/3.1 under "Win 7 64-bit, TDM64-GCC" + "OS X 10.9, LLVM Clang"]
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: How to force wxToolbar background color under OS X ?

Post by cutecode »

hello, eranon

I put your code in my wxAuiToolBar. The toolbar indeed pained with frame colour, but no buttons are displaied.
Could you, pleese, help me how to show the buttons, too?
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: How to force wxToolbar background color under OS X ?

Post by cutecode »

got it, I should overwite wxAuiToolBarArt

thx
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
Post Reply