Tcl Source Code

View Ticket
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to
or submit via the online form by Aug 20.
Ticket UUID: 883155fb98a9d7a426f24cac27c71a9b353a3f7
Title: tkDisplayIfNeeded is unnecessary
Type: Patch Version: trunk
Submitter: anonymous Created on: 2014-01-21 10:04:29
Subsystem: 02. Event Loops Assigned To: kevin_walzer
Priority: 5 Medium Severity: Minor
Status: Pending Last Modified: 2015-05-15 13:27:16
Resolution: Invalid Closed By: nobody
    Closed on:
In the nextEventMatchingMask method in macosx/tkMacOSXNotify.c, the tkDisplayIfNeeded method is applied to all windows each time there is an event by calling [NSApp makeWindowsPerform:@selector(tkDisplayIfNeeded) inOrder:NO].

The tkDisplayIfNeeded method is the following:

@implementation NSWindow(TKNotify)
- (id) tkDisplayIfNeeded
    if (![self isAutodisplay]) {
       [self displayIfNeeded];
    return nil;

In the code creating the window (TkMacOSXMakeRealWindowExist in tkMacOSXWm.c), we call [window setAutodisplay:NO], so [self isAutodisplay] is false, and tkDisplayIfNeeded calls [self displayIfNeeded]. Thus, in each window auto-displaying is switched off, but then on each pass through the event loop we ask the window to display itself if needed anyway.

This suggests that this piece of code may not be needed, and moreover the Cocoa documentation seems to discourage the use of [NSWindow displayIfNeeded]. According to the discussion on the MacTcl mailing list starting on January 18, 2014, the [window setAutodisplay:NO] line is needed to address some issues with drawing Tk windows under Cocoa, though the details are no longer readily available.

The attached patch removed the [window setAutodisplay:NO] line from tkMacOSXWm.c (by default, windows do auto-display), and removes the tkDisplayIfNeeded code. This seems to work perfectly fine; after applying this patch, the Tk test suite actually reports one test failure less. However, if it turns out that removing [window setAutodisplay:NO] causes some issues with Cocoa to reappear, then it provides us a chance to investigate the bug in detail and find an appropriate solution for it (preferably without having to tinker with the innards of the event loop).

While this patch helps to keep the Tcl/Tk code clean, it is particularly relevant for people who are running Tcl/Tk using an external event loop (using the notifier approach, as explained at; see the mailing list for details).
User Comments: dkf added on 2015-05-15 13:27:16:

Belongs in Tk.

dgp added on 2015-05-11 17:10:14:
This might be out of date now, but the code is still
in tkMacOSXNotify.c.

anonymous added on 2014-10-31 00:50:18:
I submitted this patch back in January, but since then I have been looking at the Tcl/Tk event loop code in much more detail, and it's clear to me that much more extensive changes are needed to solve the event loop issues. I would suggest to close this ticket for now, and wait for a comprehensive solution of the event loop (which may take a few more months).