How to know the down-left coordinate of one of toolbutton in wxToolbar Topic is 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.
Post Reply
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

Hi,

I'm using wxToolbar. Id like to know the down-left coordinate of one of bitmap button in toolbar.
Is there somebody could help me? Thanks!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by ONEEYEMAN »

Hi,
What are you trying to do? What is the reason you need this?
Can you show some code?

Thank you.
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

ONEEYEMAN wrote:Hi,
What are you trying to do? What is the reason you need this?
Can you show some code?

Thank you.
Oh, I just want to do some trick popup menu for one of clicked toolbutton. I know, there is dropdown menu style for toolbutton, but the bitmap button is also could be clicked. For my special purpose, I don't like this, I want to response menu clicking only.

I expect the popup position is below of clicked toolbutton and align the left of clicked toolbutton.

For the first toolbutton, it's easy to know its coordinate, just use wxPoint(0,0) and convert this coordinate into parent coordinate. But for other toolbuttons, I couldn't find out a way to get their coordinate in toolbar.

Thanks!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

Catching the mouse clicking position is another way to get coordinate, but it can't always align the left of clicked toolbutton.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by ONEEYEMAN »

Hi,
You mean you want to distinguish between the left and right clicks?

Thank you.
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

ONEEYEMAN wrote:Hi,
You mean you want to distinguish between the left and right clicks?

Thank you.
No, I want to get the coordinate of clicked(left mouse) toolbutton, and I'm meant to popup a menu at the below of toolbutton with left alignment to toolbutton.
dropdown style is not my desire, the reason I've said in above post.
Thanks!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by ONEEYEMAN »

Hi,
I know, there is dropdown menu style for toolbutton, but the bitmap button is also could be clicked.
OK, so you mean to differentiate between the click on the tool and click on the dropdown?
But it should produce the same event naturally and unless you really want to have a different behaviour I don't see a point.

Also, did you try to check the toolbar sample? Does it work for you? If not - how can I see the issue?

And finally - what is your target OS and wx version?

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by PB »

Well, after looking at the wxToolBar API (and its MSW source), I believe it is not possible (unless using horrible hacks such as iterative calls to FindToolByPosition).
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by doublemax »

There is no function in the public API that does this.

Under MSW, wx uses this helper function internally. It's not public, so you'd have to copy the code in order to use it.

Code: Select all

static RECT wxGetTBItemRect(HWND hwnd, int index, int id = wxID_NONE)
{
    RECT r;

    // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
    // only appeared in v4.70 of comctl32.dll
    if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) )
    {
        // This call can return false status even when there is no real error,
        // e.g. for a hidden button, so check for this to avoid spurious logs.
        const DWORD err = ::GetLastError();
        if ( err != ERROR_SUCCESS )
        {
            bool reportError = true;

            if ( id != wxID_NONE )
            {
                const LRESULT state = ::SendMessage(hwnd, TB_GETSTATE, id, 0);
                if ( state != -1 && (state & TBSTATE_HIDDEN) )
                {
                    // There is no real error to report after all.
                    reportError = false;
                }
                else // It is not hidden.
                {
                    // So it must have been a real error, report it with the
                    // original error code and not the one from TB_GETSTATE.
                    ::SetLastError(err);
                }
            }

            if ( reportError )
                wxLogLastError(wxT("TB_GETITEMRECT"));
        }

        ::SetRectEmpty(&r);
    }

    return r;
}
Use the source, Luke!
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

PB wrote:Well, after looking at the wxToolBar API (and its MSW source), I believe it is not possible (unless using horrible hacks such as iterative calls to FindToolByPosition).
Yes, I didn't find it out, too. Thank you PB.
shawnee
Experienced Solver
Experienced Solver
Posts: 78
Joined: Tue Jan 16, 2018 1:05 am

Re: How to know the down-left coordinate of one of toolbutton in wxToolbar

Post by shawnee »

doublemax wrote:There is no function in the public API that does this.

Under MSW, wx uses this helper function internally. It's not public, so you'd have to copy the code in order to use it.

Code: Select all

static RECT wxGetTBItemRect(HWND hwnd, int index, int id = wxID_NONE)
{
    RECT r;

    // note that we use TB_GETITEMRECT and not TB_GETRECT because the latter
    // only appeared in v4.70 of comctl32.dll
    if ( !::SendMessage(hwnd, TB_GETITEMRECT, index, (LPARAM)&r) )
    {
        // This call can return false status even when there is no real error,
        // e.g. for a hidden button, so check for this to avoid spurious logs.
        const DWORD err = ::GetLastError();
        if ( err != ERROR_SUCCESS )
        {
            bool reportError = true;

            if ( id != wxID_NONE )
            {
                const LRESULT state = ::SendMessage(hwnd, TB_GETSTATE, id, 0);
                if ( state != -1 && (state & TBSTATE_HIDDEN) )
                {
                    // There is no real error to report after all.
                    reportError = false;
                }
                else // It is not hidden.
                {
                    // So it must have been a real error, report it with the
                    // original error code and not the one from TB_GETSTATE.
                    ::SetLastError(err);
                }
            }

            if ( reportError )
                wxLogLastError(wxT("TB_GETITEMRECT"));
        }

        ::SetRectEmpty(&r);
    }

    return r;
}
Oh, It's so trick. Thanks you, doublemax!
Post Reply