Is there any way a user can close a modal window without making a “Close” button?
On windows, there is a small “x”, but on Mac OS nothing. Esc key doesn’t do anything, “Command + Q” doesn’t do anything. Does that mean I have to add a “Cancel” button? Is there any way I can enable the Command Q option?
Have you turned the close button on in the IDE? Modal windows also disable the menu items by default, you have to enable the Quit menu item in the Enable Menu Items event to use Command+Q. If you wanted the esc key to close it you could probably add a keydown event to the modal.
I use Window.Frame = MovableModal and this code in the Open event of the dialog to enable the Close button on OS X:
[code]Declare Function standardWindowButton Lib “Cocoa” Selector “standardWindowButton:” (NSWindow As Ptr, NSWindowButton As Int32) As Ptr
Declare Sub setEnabled Lib “Cocoa” Selector “setEnabled:” (NSControl As Ptr, flag As Byte)
Thanks Eli, but it’s not working for me. I have the frame set to MovableModal and put the code in the open event, but still no close button. Is there anything else that needs to be set?
What are you trying to accomplish?
A typical modal dialog on OS X has an action button, cancel button and optional ‘alternative’ action button.
I would advise trying to stick to OS X way of doing things on OS X, it will make it easier for OS X users to use your application. Mac users, used to HATE software that wasn’t Mac like, some still do, but most are more forgiving now.
[quote=156775:@Adam Meyer]Is there any way a user can close a modal window without making a “Close” button?
On windows, there is a small “x”, but on Mac OS nothing. Esc key doesn’t do anything, “Command + Q” doesn’t do anything. Does that mean I have to add a “Cancel” button? Is there any way I can enable the Command Q option?
[/quote]
The recommended way to cancel a dialog is through a Cancel button. Why do things otherwise ? All that does is confuse the user.
Command-Q is shortcut to quit the application. It should be enabled by default in all your app unless you messed with the File/Quit menu item.
If all you want is to close the window, the recommended shortcut is Command-W, which you can implement by adding a Window menu (recommended by Apple in all apps), and within it a Window Close item with shortcut key W and menu modifier on. Then you add a menu handler to your window with this simple line :
self.close
I would concur with Greg ; using ESC chr(27) is a good idea as well. Users tend to have this reflex when they are stuck.
[quote=156799:@Eli Ott]I use Window.Frame = MovableModal and this code in the Open event of the dialog to enable the Close button on OS X:
[code]Declare Function standardWindowButton Lib “Cocoa” Selector “standardWindowButton:” (NSWindow As Ptr, NSWindowButton As Int32) As Ptr
Declare Sub setEnabled Lib “Cocoa” Selector “setEnabled:” (NSControl As Ptr, flag As Byte)
Const NSWindowCloseButton = 0
Const YES = 1
setEnabled(standardWindowButton(Ptr(Handle), NSWindowCloseButton), YES)[/code][/quote]
Sorry, wrong code. This is the correct one to let the Close button appear on Cocoa:
[code]Declare Function standardWindowButton Lib “Cocoa” Selector “standardWindowButton:” (NSWindow As Ptr, windowButtonKind As Int32) As Ptr
Declare Sub setHidden Lib “Cocoa” Selector “setHidden:” (NSButton As Ptr, flag As Byte)
Sorry, but no effect on a MovableModal. Neither in Open, nor in a button Action. That said, I stick with the idea that a cancel button and Command-W are the standard way, and leveraging Windows is not a good idea.
After trying Eli’s method to no avail, I finally placed a canvas over the window bar with Richard Berglund method he used in this https://forum.xojo.com/10914-black-windows/45 to display a grabbed picture of the traffic lights from another window.
I use MouseUp to close the window when it happens within the red dot.
Looked up my code and setEnabled was missing in my code above:
[code]Declare Function standardWindowButton Lib “Cocoa” Selector “standardWindowButton:” (NSWindow As Ptr, windowButtonKind As Int32) As Ptr
Declare Sub setHidden Lib “Cocoa” Selector “setHidden:” (NSButton As Ptr, flag As Byte)
Declare Sub setEnabled Lib “Cocoa” Selector “setEnabled:” (NSButton As Ptr, flag As Byte)
Dim closeButton As Ptr = standardWindowButton(Ptr(Self.Handle), NSWindowCloseButton)
setHidden(closeButton, NO)
setEnabled(closeButton, YES)[/code]
You can of course drop NO and YES and just use False and True.
Well. I started with a document window, then realized I needed to make it modal, so I made it MovableModal in the IDE, and all buttons stopped showing up.
Now I know what I was doing. I did not want the maximize button, so it was off. And when that is the case, the buttons don’t show.
I suppose your declare works fine, but since I do not want maximize, I will stick with my little canvas.
[code]Declare Function standardWindowButton Lib “Cocoa” Selector “standardWindowButton:” (NSWindow As Ptr, windowButtonKind As Int32) As Ptr
Declare Sub setEnabled Lib “Cocoa” Selector “setEnabled:” (NSButton As Ptr, flag As Boolean)