Catching window destruction 
Author Message
 Catching window destruction

I'm trying to write some code to catch when the user attempts to close
the main window and pop up a dialog if their data has not been saved.

While I seem to be able to catch the event by binding to "Destroy",
I've not been able to work out how to tell whatever is calling my
handler that the window destruction should be cancelled.

Is this the right approach, or is there a better way to do it?  If I'm
on the right track, can someone explain how I can stop the window
being destroyed?

Note that I'm actually writing this in Ruby, but there's very little
Tk expertise on the Ruby newsgroup, whereas I reckon there's heaps
here.



Tue, 17 Jun 2003 09:52:00 GMT  
 Catching window destruction


Quote:
> I'm trying to write some code to catch when the user attempts to close
> the main window and pop up a dialog if their data has not been saved.

> While I seem to be able to catch the event by binding to "Destroy",
> I've not been able to work out how to tell whatever is calling my
> handler that the window destruction should be cancelled.

> Is this the right approach, or is there a better way to do it?  If I'm
> on the right track, can someone explain how I can stop the window
> being destroyed?

$mw->protocol('WM_DELETE_WINDOW', \&YourWindowCloseSub);

You can do whatever you want in YourWindowCloseSub.  If you want to exit
the program, you have to do that specifically.  If you just return, the
program goes on its merry way.
--
Kevin Michael Vail | a billion stars go spinning through the night,

. . . . . . . . .  | But _in_ you is the presence that
 . . . . . . . . . | will be, when all the stars are dead.  (Rainer Maria Rilke)



Tue, 17 Jun 2003 10:14:41 GMT  
 Catching window destruction

Quote:
> I'm trying to write some code to catch when the user attempts to close
> the main window and pop up a dialog if their data has not been saved.

> While I seem to be able to catch the event by binding to "Destroy",
> I've not been able to work out how to tell whatever is calling my
> handler that the window destruction should be cancelled.

> Is this the right approach, or is there a better way to do it?  If I'm
> on the right track, can someone explain how I can stop the window
> being destroyed?

> Note that I'm actually writing this in Ruby, but there's very little
> Tk expertise on the Ruby newsgroup, whereas I reckon there's heaps
> here.

I'm not sure if the following facility is in Ruby, but in perl/Tk
I would use:

        $mw->protocol('WM_DELETE_WINDOW', \&sub);

--
Marc Dashevsky  (remove "_" from address to reply by e-mail)



Tue, 17 Jun 2003 10:38:40 GMT  
 Catching window destruction
As far as I can tell, I'm doing exactly the Ruby equivalent of what
Kevin and Marc suggested, since my function gets called when I click
on the little 'X' in the top right hand corner.

However, just returning from the function doesn't seem to have the
desired effect.  So, it sounds like it's an implementation issue.
Hopefully, the person who wrote the Ruby/Tk binding will respond to my
query over on that newsgroup.

Thanks for the suggestions.  At least I have more confidence that what
I've written makes sense now.

If anyone has any further ideas re catching an event before the
WM_DESTROY stuff happens, I'd still be interested in hearing.



Tue, 17 Jun 2003 13:43:56 GMT  
 Catching window destruction
Hi,

Ummm... Maybe this work for ya (Dunno Ruby tho):

my $MW=MainWindow->new(-title => "test");
$MW->OnDestroy(sub {do your save stuff here});

or

my $MW=MainWindow->new(-title => "test");
$MW->OnDestroy(\&SaveFirst);

sub SaveFirst {
... Do save stuff here ...

Quote:
}

Good luck :)

Manfred Engels

Sent via Deja.com
http://www.deja.com/



Tue, 17 Jun 2003 14:36:24 GMT  
 Catching window destruction

Quote:
>my $MW=MainWindow->new(-title => "test");
>$MW->OnDestroy(\&SaveFirst);

>sub SaveFirst {
>... Do save stuff here ...
>}

Yes, I'd pretty much come to that conclusion myself.  I'm trying to
write some code that will decide whether the data has been changed
since the last save and, if not, save it to another file before
exiting.

At least that way, the user can't lose anything, so long as they know
about this behaviour.

I'd still rather have the confirmation box, but I'm not getting much
joy from the Ruby newsgroup as to why what I'm doing isn't working.

The problem is that Ruby is big in Japan, which means there's a lot of
documentation in Japanese, but very little in English at this stage,
particularly re the Tk bindings.

I may end up delving into the code myself, which would be fun, but
consume a lot of time.



Tue, 17 Jun 2003 15:49:33 GMT  
 Catching window destruction

Quote:

> I'm trying to write some code to catch when the user attempts to close
> the main window and pop up a dialog if their data has not been saved.

> While I seem to be able to catch the event by binding to "Destroy",
> I've not been able to work out how to tell whatever is calling my
> handler that the window destruction should be cancelled.

> Is this the right approach, or is there a better way to do it?  If I'm
> on the right track, can someone explain how I can stop the window
> being destroyed?

> Note that I'm actually writing this in Ruby, but there's very little
> Tk expertise on the Ruby newsgroup, whereas I reckon there's heaps
> here.

The Tk::TextUndo widget has what you're looking for. You have to read the
source. Search on destroy.


Sun, 13 Jul 2003 13:20:05 GMT  
 Catching window destruction
Try using
  Toplevel->protocol(WM_DESTROY_WINDOW, &myCleanUpRoutine)

(But you'd need to check the exact syntax; I don't have a reference with
me.)  Truthfully I don't know whether it "absorbs" the event or not.

Quote:


> > I'm trying to write some code to catch when the user attempts to close
> > the main window and pop up a dialog if their data has not been saved.

> > While I seem to be able to catch the event by binding to "Destroy",
> > I've not been able to work out how to tell whatever is calling my
> > handler that the window destruction should be cancelled.

> > Is this the right approach, or is there a better way to do it?  If I'm
> > on the right track, can someone explain how I can stop the window
> > being destroyed?

> > Note that I'm actually writing this in Ruby, but there's very little
> > Tk expertise on the Ruby newsgroup, whereas I reckon there's heaps
> > here.

> The Tk::TextUndo widget has what you're looking for. You have to read the
> source. Search on destroy.



Sat, 19 Jul 2003 12:01:41 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Handeling destruction of main window.

2. Catch window close event

3. How to catch keystrokes in a canvas window

4. Catching Close or Kill window

5. Object destruction order?

6. Detecting global destruction phase in DESTROY

7. Detecting global destruction phase in DESTROY

8. Debugger defers destruction of closures [ID 19990722.003]

9. Use of uninitialized value during global destruction

10. HELP: Advice on (global) destruction

11. Death, destruction, and grep strangeness

12. Perl5a11b, variable scopes/lifetimes, object destruction ...

 

 
Powered by phpBB® Forum Software