Mac wxOwnerDrawnComboBox - change button background? Topic is solved

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
Forbin
Earned a small fee
Earned a small fee
Posts: 21
Joined: Mon Oct 31, 2016 7:26 pm

Mac wxOwnerDrawnComboBox - change button background?

Post by Forbin »

Platform: Mac OSX 10.11.6 and 10.12
Compiler: Xcode 7.3.1 (7D1014)
wxWidgets: OSX/cocoa 3.0.2

The wxOwnerDrawnComboBox on the Mac does not look the same as a wxComboBox:
MacWrongCombo01.png
MacWrongCombo01.png (11.46 KiB) Viewed 1934 times
Unfortunately, I'm required to preserve the same look & feel as a "regular" combo box, so I need to change this.

QUESTIONS
It would sure save me a lot of time if someone happens to know the answer(s) to one or more of the following questions:
  1. Is there a button control (wxButton or similar) member buried somewhere within the inheritance tree of wxOwnerDrawnComboBox?
  2. If there is, where?
  3. Any ideas how I can get the button shape the same as on wxComboBox?
  4. Any ideas how to move the button over?

Thanks!
--forbin
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by doublemax »

I haven't tried, but looking through the docs, wxComboCtrl::SetButtonBitmaps() and wxComboCtrl::SetButtonPosition() look like they could be worth investigating.

http://docs.wxwidgets.org/trunk/classwx ... 548106376b
http://docs.wxwidgets.org/trunk/classwx ... a78117ebad
Use the source, Luke!
Forbin
Earned a small fee
Earned a small fee
Posts: 21
Joined: Mon Oct 31, 2016 7:26 pm

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by Forbin »

Yes, I saw those and have been playing around with them. I'd like to avoid setting a bitmap if possible because that makes it more difficult to integrate with OS-native theming (coloring). But if I don't find a better way, and if it gets me where I need to go without me having to rewrite my subclassed owner-drawn combo box with something else, then that's still a win!

Thanks for the response!

PS: I also noticed that the button shape isn't the same. I don't know if our QA testers will notice or care, but they might. That alone might sink this raft.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by doublemax »

The whole control is custom drawn, getting a 100% native look might be difficult.
Use the source, Luke!
Forbin
Earned a small fee
Earned a small fee
Posts: 21
Joined: Mon Oct 31, 2016 7:26 pm

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by Forbin »

Ugh, I was afraid of that. Thanks for the warning!!

What if I inherited directly from wxComboCtrl (meaning a bit of refactoring on my end)?
Does that look different on the Mac? The images in the online documentation imply it might.
User avatar
doublemax
Moderator
Moderator
Posts: 19103
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by doublemax »

As wxOwnerDrawnComboBox derives from wxComboCtrl i don't see how that could make any difference. It's still custom-drawn.
Use the source, Luke!
Forbin
Earned a small fee
Earned a small fee
Posts: 21
Joined: Mon Oct 31, 2016 7:26 pm

Re: Mac wxOwnerDrawnComboBox - change button background?

Post by Forbin »

I have found a workaround to make the non-native combo box look native.
I overrode the OnPaintEvent. Example:

Code: Select all

BEGIN_EVENT_TABLE(MyComboBoxCtrl, wxOwnerDrawnComboBox)
#ifdef __WXMAC__
    EVT_PAINT(MyComboBoxCtrl::OnPaintEvent)
#endif
END_EVENT_TABLE()

#ifdef __WXMAC__
void MyComboBoxCtrl::OnPaintEvent(wxPaintEvent& WXUNUSED(event))
{
    wxPaintDC dc(this);
    wxRect clientRect = GetClientRect();
    wxWindow* pWindow = this;

    int flags = wxCONTROL_NONE;
    if (pWindow->IsEnabled() == false)
        flags |= wxCONTROL_DISABLED;

    wxRendererNative& renderer = wxRendererNative::GetDefault();
    renderer.DrawComboBox(pWindow, dc, clientRect, flags);
}
#endif
You may also need a PositionTextCtrl() to get the inner text box lined up close to how the native control does it:

Code: Select all

#if defined(__WXMAC__)

// in class declaration in MyComboBoxCtrl.h: virtual void PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust );

void MyComboBoxCtrl::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
{
    wxTextCtrl* textCtrl = GetTextCtrl();

    if ( !textCtrl )
        return;

    textCtrl->SetSize(m_tcArea.x + textCtrlXAdjust,
                      m_tcArea.y + textCtrlYAdjust,
                      m_tcArea.width,
                      m_tcArea.height - 6);
    textCtrl->Move(3, 6);
}
#endif
I hope this is helpful to anyone else finding they need a Mac-native-looking owner-drawn combo box!
Post Reply