views:

164

answers:

1

Hi, I want to catch the close event of my xulrunner app when user clicks the close button of the main window and ask them for confirmation.If they dont confirm ,i want to cancel this close event and continue the application.

I tried OnClose Event ,but i was not able to cancel it.

How do i implement this?

+2  A: 

There are two different things you may want:

1) prompting when a window is closed. You can use the "close" event, e.g.:

<!-- Creates an unclosable window -->
<window onclose="return false" ...>

I just checked it and it does work.

2) prompting when the application is about to quit. You can do this by observing the quit-application-requested notification via the nsIObserverService (example).

(1) is closer to what you asked, since when the user presses the [X] button on the window, the sequence of events is this:

  1. "close" event is dispatched to the window
  2. if nothing prevented it, the window closes
  3. if no windows are left, and it's not Mac OS X, the application starts to shut down, quit-application-requested is sent.
  4. If no observers deny the quit request, the shutdown proceeds.

On the other hand, if you put your check solely in the onclose handler of the window, other ways of quitting application (File-Exit on Win/-Quit on Mac) may trigger it too late (e.g. after closing other windows). Also, if your app can have more than one "main" window (with onclose logic), the user will have to deal with multiple confirmations, which may or may not be what you want.

Here are some references to the Firefox code (if I'm not missing something, it should also be packaged with your application), you can research and reuse the approach taken:

Nickolay
Thanks Nickolay.It was a mistake from my part I did some thing like <window onclose="OnAppExit()" >and returned false from the function.Obviously it did not work.
NightCoder