Enabling App Menu Items when Modal Dialog Shown

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
LarryM
Earned a small fee
Earned a small fee
Posts: 11
Joined: Wed Jul 31, 2013 12:28 am

Enabling App Menu Items when Modal Dialog Shown

Post by LarryM »

In my wxWidgets OSX/Cocoa (3.0.1) app, all menu items are always disabled (ie. grayed out) during calls to wxDialog::ShowModal(). I want to enable some menu items while modal dialogs are shown, but nothing I have tried works at all. The application's EVT_UPDATE_UI_RANGE method is never called when modal dialogs are shown, so I defined a method on the wxMenu subclass, but that is not called either. Can someone here please tell me how to enable application menu items while wxDialog::ShowModal() is active on Cocoa?

Thank You


Larry
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Enabling App Menu Items when Modal Dialog Shown

Post by ONEEYEMAN »

Hi, Larry,
The point of making dialog modal is that everything is disabled on your app until you close the dialog.
If you want to keep the main application responsive - use wxDialog::Show().

Thank you.
LarryM
Earned a small fee
Earned a small fee
Posts: 11
Joined: Wed Jul 31, 2013 12:28 am

Re: Enabling App Menu Items when Modal Dialog Shown

Post by LarryM »

ONEEYEMAN wrote:Hi, Larry,
The point of making dialog modal is that everything is disabled on your app until you close the dialog.
If you want to keep the main application responsive - use wxDialog::Show().

Thank you.

I realize that, but on Mac OS, the "Cut", "Copy", "Paste", "Select", and "Undo" menu items are expected to be active for editable text controls, even in modal dialogs.

Thank You


Larry
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Enabling App Menu Items when Modal Dialog Shown

Post by ONEEYEMAN »

LarryM,
Can you reproduce it with the latest 3.1 ore even Git HEAD?
Also, did you try the dialogs sample to see if it exists there?

If you can check the trac.wxwidgets.org if such issue exists and if not open a new ticket.
moki
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Dec 08, 2014 1:08 pm
Location: Brive la gaillarde

Re: Enabling App Menu Items when Modal Dialog Shown

Post by moki »

For information, the bug is still present in the 3.1 but has just been fixed in the HEAD revision (https://trac.wxwidgets.org/ticket/17459).

For users who don't want to use head revision, here is the way I fix the problem without patching the wxWidgets :

In a .mm file :

Code: Select all

bool disableOSXMenuItemAutoEnabling(wxMenu *menu)
{
    if (!menu)
	return false;

    // Retrieve Objective C raw object and set option
    wxMenuImpl *menu_impl = menu->GetPeer();

    if (!menu_impl)
	return false;

    WXHMENU ns_menu = menu_impl->GetHMenu();

    if (!ns_menu)
	return false;
    
    [ns_menu setAutoenablesItems:NO];

    // Do recursively the same for all submenus
    wxMenuItemList menu_items = menu->GetMenuItems();

    for (wxMenuItemList::iterator it = menu_items.begin();
	 it != menu_items.end(); it++)
    {
	wxMenu *sub_menu = (*it)->GetSubMenu();

	if (sub_menu)
	    disableOSXMenuItemAutoEnabling(sub_menu);
    }

    return true;
}
Override DoPopupmenu fonction of wxDialog class :

Code: Select all

bool MyDialog::DoPopupMenu(wxMenu *menu, int x, int y)
{
#ifdef __WXOSX__
    disableOSXMenuItemAutoEnabling(menu);
#endif

    return wxDialog::DoPopupMenu(menu, x, y);
}
moki
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Dec 08, 2014 1:08 pm
Location: Brive la gaillarde

Re: Enabling App Menu Items when Modal Dialog Shown

Post by moki »

My previous post is not relevant anymore as the bug as been reopened (because of side effects).

Here is the patch I finally applied to my application (in a .mm file), without any wxWidgets library modification :

Code: Select all

#include <wx/menu.h>
#include <wx/osx/core/private.h>

bool popUpOSXMenu(wxMenu *menu, wxWindow *win, int x, int y)
{
    // Retrieve Objective C raw object and set option
    wxMenuImpl *menu_impl = menu->GetPeer();

    if (!menu_impl)
	return false;

    WXHMENU ns_menu = menu_impl->GetHMenu();

    if (!ns_menu)
	return false;

    // Fix bad menu position with default coordinates
    wxPoint pt = wxGetMousePosition();

    if (x == wxDefaultCoord) 
	x = pt.x;

    if (y == wxDefaultCoord)
	y = pt.y;
    
    win->ScreenToClient(&x, &y);

    // Show menu
    NSView *view = win->GetPeer()->GetWXWidget();
    NSView *tempView = [[NSView alloc] initWithFrame:NSMakeRect(x, y, 1, 1)];
    [view addSubview:tempView];
    
    NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
    [popUpButtonCell setAutoenablesItems:NO];
    [popUpButtonCell setAltersStateOfSelectedItem:NO];
    [popUpButtonCell setMenu:ns_menu];
    [popUpButtonCell selectItem:nil];
    [popUpButtonCell performClickWithFrame:NSMakeRect(0, 0, 1, 1) inView:tempView];
    [popUpButtonCell release];
    [tempView removeFromSuperview];
    [tempView release];
    
    return true;
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Enabling App Menu Items when Modal Dialog Shown

Post by ONEEYEMAN »

Hi,
I believe Stefan fixed it in the Git HEAD.

Please try to compile the latest Git sources and check if it was fixed.

Thank you.
moki
Earned a small fee
Earned a small fee
Posts: 14
Joined: Mon Dec 08, 2014 1:08 pm
Location: Brive la gaillarde

Re: Enabling App Menu Items when Modal Dialog Shown

Post by moki »

You are right, thanks. I will check it.
tonyvsuk
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 19, 2011 12:41 pm

Re: Enabling App Menu Items when Modal Dialog Shown

Post by tonyvsuk »

I'm now hitting this bug in wx 3.1.5 on the Mac.

Your function displays the menu ok for me, but the events do not trigger. Am I doing anything obviously wrong (code below).

Thanks in advance,

Tony.

Code: Select all

wxMenu    _popupMenu;

_popupMenu.Append(ID_MENU_ID, "Do something));

   _popupMenu.Bind(wxEVT_MENU, &MyClass::MyFunction, this, ID_MENU_ID);
   wxPoint _pt = wxGetMousePosition();
   
   bool bSelected = popUpOSXMenu(&_popupMenu, this, _pt.x, _pt.y);
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Enabling App Menu Items when Modal Dialog Shown

Post by ONEEYEMAN »

Hi,
Are you running an Application Bundle?
And is "minimal" sample works for you?

Thank you.
tonyvsuk
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 19, 2011 12:41 pm

Re: Enabling App Menu Items when Modal Dialog Shown

Post by tonyvsuk »

I'm not running in an app bundle as still debugging.

Also, this is when I right click on a wxScrolled<wxPanel> window. I only need a popup menu with two entries, but they are disabled (they work fine on Windows).

I haven't tried the minimal bundle yet, time is a little tight on this one (I need to get a fix out tomorrow if I can).
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Enabling App Menu Items when Modal Dialog Shown

Post by ONEEYEMAN »

Hi,
You can still debug in a Bundle.

You can run:

lldb <myapp.app>

and the application will execute.

Thank you,

P.S.: If its a context menu - try the "menu" sample.
Also, make sure there is no UPDATE_UI event that disables the menu.
tonyvsuk
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 19, 2011 12:41 pm

Re: Enabling App Menu Items when Modal Dialog Shown

Post by tonyvsuk »

Creating the app bundle to debug with is not really an option for me at the moment. I'm really not a MacOS programmer. The app bundle is created when I do a full release build.

Strangely, if I do the following, it works and triggers the events.

Code: Select all

bool bSelected = popUpOSXMenu(&_popupMenu, this, _pt.x, _pt.y);
PopupMenu(&_popupMenu, _pt);
But doing this, I get the menu appearing twice.
tonyvsuk
Knows some wx things
Knows some wx things
Posts: 41
Joined: Wed Oct 19, 2011 12:41 pm

Re: Enabling App Menu Items when Modal Dialog Shown

Post by tonyvsuk »

In case it helps anyone else, I've modified the function above to return the index of the clicked menu item. I can now work out which function to call based on the return value.

Here is the code,

Code: Select all


int popUpOSXMenu(wxMenu *menu, wxWindow *win, int x, int y)
{
    // Retrieve Objective C raw object and set option
    wxMenuImpl *menu_impl = menu->GetPeer();

    if (!menu_impl)
    return -1;

    WXHMENU ns_menu = menu_impl->GetHMenu();

    if (!ns_menu)
    return -1;

    // Fix bad menu position with default coordinates
    wxPoint pt = wxGetMousePosition();

    if (x == wxDefaultCoord) 
    x = pt.x;

    if (y == wxDefaultCoord)
    y = pt.y;
    
  //  win->ScreenToClient(&x, &y);

    // Show menu
    NSView *view = win->GetPeer()->GetWXWidget();
    NSView *tempView = [[NSView alloc] initWithFrame:NSMakeRect(x, y, 1, 1)];
    [view addSubview:tempView];
    
    NSPopUpButtonCell *popUpButtonCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
    [popUpButtonCell setAutoenablesItems:NO];
    [popUpButtonCell setAltersStateOfSelectedItem:NO];
    [popUpButtonCell setMenu:ns_menu];
    [popUpButtonCell selectItem:nil];
    [popUpButtonCell performClickWithFrame:NSMakeRect(0, 0, 1, 1) inView:tempView];
    [popUpButtonCell release];
    [tempView removeFromSuperview];
    [tempView release];
    
    return [popUpButtonCell indexOfSelectedItem];
   // return true;
}
Post Reply