Fixing Leo and Idle OS shutdown bug?

Mike Clarkson support at internetdiscovery.com
Fri Aug 9 07:43:42 CEST 2002


On Wed, 07 Aug 2002 14:30:42 GMT, "Edward K. Ream" <edream at tds.net>
wrote:

>Leo does not prompt the user to save unsaved files when Windows 2K or XP is
>shutting down.  Neither does IDLE.

No Tkinter based Tk app will catch the OS message, because Tk doesn't.

>Is there any way to interrupt the shutdown process in TK?  I have tried
>binding to Destroy or WM_DELETE_WINDOW for the top level window, like this,
>with no effect:

It's WM_DELETE_WINDOW for the normal program exit, but the OS shutting
down message is WM_QUERYENDSESSION  and  WM_ENDSESSION.
Problem is that wm_protocol doesn't catch either of these messages.

To change this you have to change Tk.  Attached is an old posting
requesting this change to Tk, but AFAIK it still hasn't been
implemented (sorry about the line wrapping).

Mike.

---------------------------------------------------------------------------------------------------------------
>From stephan at frontierd.com Mon Nov 22 05:45:53 1999
To: Jeffrey Hobbs <jeffrey.hobbs at scriptics.com>
Newsgroups: comp.lang.tcl
Subject: Re: Win* Shutdown from Tcl
From: Stephan Uyttebroeck <stephan at frontierd.com>
Date: Mon, 22 Nov 1999 10:45:53 +0100

F.Y.I.

About a year ago I entered the following request/patch...



Tk 8.0.4 Request

Name:  Stephan Uyttebroeck
email:  stephan at frontierd.com
Support:  None
Severity:  3
OperatingSystem:  Windows NT
Synopsis:  Tcl/Tk doesn't trap the logoff/reboot/shutdown events on
Windows

DesiredBehavior:
    With the current Tcl/Tk for Windows it is impossible to perform
some
    "cleanup" actions within your TK application (e.g. "Ask the user
to
save
    before going down") when a user triggered a logoff, reboot or
shutdown
    event.
    The WM_QUERYENDSESSION and WM_ENDSESSION events aren't handled.
    

Patch:
*** tkWinX.c	Sat Oct 10 02:30:37 1998
--- tkWinX.c_new	Fri Mar 12 11:25:00 1999
***************
*** 50,55 ****
--- 50,63 ----
  			    LPARAM lParam));
  static void 		GetTranslatedKey _ANSI_ARGS_((XKeyEvent
*xkey));
  

+ 
+ static void (*fd_tcl_exit_callback_function_ptr)() = NULL;
+ 
+ void fd_tcl_register_exit_callback(void (*func_ptr)())   
+ {   
+    fd_tcl_exit_callback_function_ptr = func_ptr;
+ }
+ 
  /*
  
*----------------------------------------------------------------------
   *
***************
*** 588,593 ****
--- 596,615 ----
  	    Tk_PointerEvent(hwnd, (short) LOWORD(lParam),
  		    (short) HIWORD(lParam));
  	    return 1;
+ 
+    case WM_QUERYENDSESSION: {       
+        *resultPtr = 1;
+        return 1;
+    }
+ 
+    case WM_ENDSESSION: {
+        if ( wParam == TRUE ) {
+            if ( fd_tcl_exit_callback_function_ptr != NULL ) {
+                (*fd_tcl_exit_callback_function_ptr)();
+            }
+        } 
+        return 1;
+    }
  
  	case WM_CLOSE:
  	case WM_SETFOCUS:


PatchFiles:
    win/tkWinX.c
    
    And, you have to modify 'winmain.c'
    
       extern void fd_tcl_register_exit_callback(void (*func_ptr)());
    
       static Tcl_Interp *YOUR_TCL_INTERPRETER;
    
       void fd_logoff_callback()
       {
          char * command = "YOUR_TCL_EXIT_PROC";
          Tcl_Obj * cmdObj = Tcl_NewStringObj(command, -1);
          if ( Tcl_GlobalEvalObj(YOUR_TCL_INTERPRETER, cmdObj) ==
TCL_ERROR
    ) {
             exit(1);
          }
       }
    
       ...
       /* Just before calling TkMain() */
       fd_tcl_register_exit_callback(fd_logoff_callback);
       ...
    
    YOUR_TCL_EXIT_PROC is the Tcl procedure that will get called when
a
    logout, restart or shutdown occurs.
    
    

Comments:
    To handle the WM_QUERYENDSESSION and WM_ENDSESSION events on
Windows, we
    added an 'fd_tcl_register_exit_callback()' function to Tk to 
    register a C callback function.  
    
    When receiving a WM_ENDSESSION event with LogOff == TRUE (which
will
    only
    happen when all running applications returned TRUE when receiving
the 
    WM_QUERYENDSESSION event) the registered function will be called.
    
    This patch currently fits our needs, but it should be generalized.
    We added a 'hook' to call the registered callback function, but a
better
    solution would be to provide a new Tcl builtin on Windows to
register a
    Tcl logout procedure.





More information about the Python-list mailing list