Close modal window

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?

Thanks

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.

Yes, I have the close button turned on, but it doesn’t show on Mac OS, only Windows.

How can I enable the Command+Q option? I’m not sure how the Enable Menu Items event works.

Thank you!

You may also be able to check for asc(27) in the window’s KeyDown event.

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]

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?

Another idea I had, what’s the downside of use a Document window type with .ShowModal. Is there any?

ugh
just screwy behaviour

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.

Command+Q is “QUIT”
And entirely different kettle of fish

Are you compiling for Carbon? The above code works for Cocoa builds only.

[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.

Don’t try to have exactly the same controls in Windows and Mac. Each has it’s own interface design. Try to stick to the recommended way for each. You will greatly improve your user experience.
Mac :
https://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/OSXHIGuidelines/
Windows :
http://msdn.microsoft.com/en-us/library/jj651618(v=nav.80).aspx

[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)

Const NSWindowCloseButton = 0
Const NO = 0

setHidden(standardWindowButton(Ptr(win.Handle), NSWindowCloseButton), NO)[/code]

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.

It works.

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)

Const NSWindowCloseButton = 0
Const NO = 0
Const YES = 1

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.

I placed that in Open, no joy. Then in case it needed some delay, I tried in a button Action. No joy either.

The window is a Movable Modal, 2015R4.1, El Capitan 10.11.4 beta.

Seems Movable Modal is decidedly reluctant to show the close button.

A standard window set to MovableModal shows all three buttons, albeit Close and Minimize are disabled.

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.

Thank you Eli.

That will do:

[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)

Const NSWindowCloseButton = 0
Const NSWindowZoomButton = 2

setEnabled(standardWindowButton(Ptr(Self.Handle), NSWindowCloseButton), True)
setEnabled(standardWindowButton(Ptr(Self.Handle), NSWindowZoomButton), False)[/code]