wxCocoa 3.1: Appended SubMenu and 'Connect'

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.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

Hi all,

under wxCocoa 3.1 / Xcode 5, i create a menu like this

Code: Select all

	wxMenu m;
	m.AppendRadioItem(GO_A, _("A Mode"), _("-"));
	m.AppendRadioItem(GO_B, _("B Mode"), _("-"));
	m.AppendRadioItem(GO_C, _("C Mode"), _("-));

	wxMenu* submenu = new wxMenu();
	submenu->AppendRadioItem( T1, _("1"), _("-"));
	submenu->AppendRadioItem( T2, _("2"), _("-"));
	submenu->AppendRadioItem( T3, _("3"), _("-"));
	m.AppendSubMenu( submenu, _("Sub Menu") );

	m.Connect( wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(some_object::OnPopupClick), NULL, this );
Under Windows, 2.8 'm' and 'submenu' use the handler from Connect, under wxCocoa only 'm' uses the handler. 'submenu' uses the application frame as event handler, so each submenu must be connected indiviually. Is this correct behaviour, or should i file a bug?

Best regards,
Heiner
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by ONEEYEMAN »

Hi,
Please upgrade to wxWidgets-3.1 and see if the problem persists.
If it is - check the Git HEAD.

Thank you.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

Hi,
sorry what is the difference between wxCocoa 3.1 and wxWidgets-3.1?

Best regards,
Heiner
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by ONEEYEMAN »

Hi,
I'm not sure I understand the question.
There is no such thing as wxCocoa. There is however a library called wxWidgets.

WHat I'm saying is that you need to download version 3.1 of this library, compile it and use it in you program.

Will you be able to do that?

Thank you.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

- Download is Latest Development Release: 3.1.0 from here wxwidgets.org/downloads/
- Build is wxOSX/Cocoa in 32 Bit - built with Xcode
- Changes made by me: version_min 10.7, compile with clang -std=c++11
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by doublemax »

If the behavior is different to the two other main platforms (Windows and Linux), you should open a bug report.

Can you test this under at least one other platform?
Use the source, Luke!
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by PB »

This code seems to work as expected on MSW with wxWidgets 3.1, i.e. the commands from both the menu and submenu arrive at the specified handler; cannot test it on the other two platforms.

Code: Select all

#include <wx/wx.h>

class MyFrame : public wxFrame
{
public:
    enum 
    {
        GO_A = wxID_HIGHEST + 1, GO_B, GO_C,
        T1, T2, T3
    };
    MyFrame()
        : wxFrame(NULL, wxID_ANY, _("Left click inside the frame"), wxDefaultPosition, wxSize(600, 400))
    {         
       // to show the pop up menu
       Bind(wxEVT_LEFT_UP, &MyFrame::OnLeftUp, this); 

       // to handle the pop up menu command
       Connect(wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnMenuClick),  NULL, this);        
    }   
private:    
    void OnLeftUp(wxMouseEvent& event)    
    {         
        wxMenu m;
        m.AppendRadioItem(GO_A, _("A Mode"), _("-"));
        m.AppendRadioItem(GO_B, _("B Mode"), _("-"));
        m.AppendRadioItem(GO_C, _("C Mode"), _("-"));

        wxMenu* submenu = new wxMenu();
        submenu->AppendRadioItem( T1, _("1"), _("-"));
        submenu->AppendRadioItem( T2, _("2"), _("-"));
        submenu->AppendRadioItem( T3, _("3"), _("-"));
        m.AppendSubMenu( submenu, _("Sub Menu") );
       
        PopupMenu(&m, event.GetPosition()); 
    } 

    void OnMenuClick(wxCommandEvent& event)
    {
        wxLogMessage("Menu event with ID of %d", event.GetId());
    }    
};

class MyApp : public wxApp
{
public:     
    virtual bool OnInit()
    {       
        (new MyFrame())->Show();
        return true;
    }
};
wxIMPLEMENT_APP(MyApp);
The only differences from the code you posted are:
1. Menu events are command events therefore they propagate upwards so the handler is connected from the frame itself as usual.
2. The handler is connected only once and not during every menu invocation.

I would expect it to work the same way on all the supported platforms, unless stated otherwise. If not I would consider it a bug. By the way, assuming you use PopupMenu(), does GetPopupMenuSelectionFromUser() work as expected for you?
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

In your sample everything as expected. Also GetPopupMenuSelectionFromUser() works as expected: i get the correct return values, even when i do not Connect(..) the submenus
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by PB »

Well, did you try to connect the menu event handler in your application the same way it is in the sample above (i.e. from the frame and only once) and did it work or not?

Also, are you sure you are Connect()ing the event handler properly with a correct event sink in the original code, i.e., is that code called from a someobject method? I assume your menu IDs are unique, so there should be no problems with that...
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

PB wrote:Well, did you try to connect the menu event handler in your application the same way it is in the sample above (i.e. from the frame and only once) and did it work or not?
This is difficult to test, because the event handler is an object different from the application frame. Yes, the IDs overlap - they all start from wxID_HIGHEST. I really like this concept, because i can always start at this ID and simply change the event handler, that makes programming a lot easier - i do not have to carry around IDs across the whole application source. Also it is working fine - except in this case :D
I assume your menu IDs are unique, [...]
Inside the menu the Is are unique.
I have a bit trouble understanding this part:
are you sure you are Connect()ing the event handler properly with a correct event sink
To my understandung the event handler is the event sink?
- my class derives from wxEvtHandler
- i Connect() to a method OnPopupClick() of it - signature

Code: Select all

void OnPopupClick(wxCommandEvent& event);
- this method gets called for all menus which are explicitely connected
- it does not get called for the non-explicitely-connected submenus. In this case the application frame gets called with the IDs from the submenu (generating completely unrelated events)
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by ONEEYEMAN »

Hi,

This is the code you posted in the OP:

Code: Select all

  wxMenu m;
   m.AppendRadioItem(GO_A, _("A Mode"), _("-"));
   m.AppendRadioItem(GO_B, _("B Mode"), _("-"));
   m.AppendRadioItem(GO_C, _("C Mode"), _("-));

   wxMenu* submenu = new wxMenu();
   submenu->AppendRadioItem( T1, _("1"), _("-"));
   submenu->AppendRadioItem( T2, _("2"), _("-"));
   submenu->AppendRadioItem( T3, _("3"), _("-"));
   m.AppendSubMenu( submenu, _("Sub Menu") );

   m.Connect( wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(some_object::OnPopupClick), NULL, this );
   
I presume this code is located somewhere in the main frame class?
What is "some_object" class?
Is "some_object" pointer is available inside the main frame class (or whatever class this piece of code is written on?
Why do you use "this" as event sink parameter? Try use "some_object" pointer instead?

Thank you.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

Ah, sorry it was me who used 'some_object' the first time :D
No, some_object is not the main frame, it is an object derived from wxEvtHandler, it is drawn by OpenGL and gets is first hit test from there. So i know where to place a popoup menu and can show it and handle it inside this instance via some_object::OnPopupClick() - all in one place. It works fine, too which is really nice - freely mix OpenGL objects with wxMenus.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by ONEEYEMAN »

OK, so the issue is solved then?
Everything works under both Windows and OSX?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by PB »

By the way, it is generally recommended to use Bind(available since wxWidgets 2.9) instead of Connect. Bind has much greater flexibility but it also is not as error-prone as Connect where it was very easy to connect the event the wrong way.
heinermueller
Earned some good credits
Earned some good credits
Posts: 100
Joined: Sat Oct 26, 2013 11:54 am

Re: wxCocoa 3.1: Appended SubMenu and 'Connect'

Post by heinermueller »

I really have to start reading reading and writing clearly, sorry :D
No, the issue is not solved - i still have to Connect() to sub menus explicitly. But only for this platform. Testing connecting from the frame is difficult, because i generate the sub menus items - and thus the IDs - dynamically.
Post Reply