Tk Source Code

View Ticket
Login
Ticket UUID: eb0393e7b7d0a3dcc08df25aff2986b5887282bb
Title: [grid forget] silently resets the forgotten window's own [grid propagate] setting
Type: Patch Version: trunk
Submitter: nab Created on: 2026-08-20 15:53:00
Subsystem: 49. [grid] Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-09-22 17:17:11
Resolution: Fixed Closed By: nab
    Closed on: 2026-09-22 17:17:11
Description:
Hi,
here's a little something found with Claude while working on my app.
i do not have enough skills to say it's good or bad, so I let you decide

**Description:**

When a window is both a grid *content* (slave) and a grid *container*
(master), forgetting it from its own container resets the `grid propagate`
setting of its container role:

```tcl
frame .p
frame .p.m
grid propagate .p.m 0        ;# .p.m must not request its content's size
frame .p.m.a
grid .p.m.a                  ;# .p.m is a container
grid .p.m -in .p             ;# ... and also a content of .p
puts [grid propagate .p.m]   ;# 0, as configured
grid forget .p.m
grid .p.m -in .p
puts [grid propagate .p.m]   ;# 1 — expected 0
```

After the re-grid, `.p.m` propagates its content's requested size again,
although `grid propagate .p.m 0` was never undone by the script. Nothing in
the `grid forget` documentation suggests it purges anything beyond the
window's *content* options (column/row/span/padding/sticky/-in). Notably,
row/column `-weight`/`-minsize` of the container role DO survive the forget,
which makes the loss of the propagate flag look even more accidental.

`grid remove` is not affected.

Real-world impact: a UI that temporarily un-grids fixed-size sub-containers
(propagate 0) and re-grids them later sees them silently start propagating,
changing the layout on the first hide/show cycle (this is how we found it —
all our fader strips grew by 1px on the first page switch).

**Cause:**

In `GridForgetRemoveCommand` (generic/tkGrid.c), the forget branch resets
the content options and then does:

```c
    if (contentPtr->flags & REQUESTED_RELAYOUT) {
        Tcl_CancelIdleCall(ArrangeGrid, contentPtr);
    }
    contentPtr->flags = 0;
```

`Gridder.flags` is shared between the window's two roles. The only defined
bits are `REQUESTED_RELAYOUT`, `DONT_PROPAGATE` and `ALLOCED_CONTAINER` —
all three belong to the *container* role. `contentPtr->flags = 0` therefore
wipes `DONT_PROPAGATE` (set by `grid propagate $w 0`) and
`ALLOCED_CONTAINER` as a side effect of a content-role operation.

(As an aside, the cancelled `ArrangeGrid` idle call also belongs to the
container role — it re-arranges the forgotten window's own content. It is
harmless in practice because the window is unmapped right after and
`MapNotify` schedules a new arrangement on re-map, and `DestroyGrid` /
the `DestroyNotify` handler already cancel the idle call at destruction.)

**Suggested patch (minimal):** clear only the relayout bit whose idle call
was just cancelled, and preserve the container-role bits:

```diff
--- a/generic/tkGrid.c
+++ b/generic/tkGrid.c
@@ GridForgetRemoveCommand (forget branch)
 		if (contentPtr->flags & REQUESTED_RELAYOUT) {
 		    Tcl_CancelIdleCall(ArrangeGrid, contentPtr);
 		}
-		contentPtr->flags = 0;
+		/*
+		 * The flags field is shared with this window's container
+		 * role: DONT_PROPAGATE ([grid propagate $w 0]) and
+		 * ALLOCED_CONTAINER must survive a [grid forget] of the
+		 * window as content, otherwise a container that is
+		 * forgotten from its own container silently starts
+		 * propagating its content's size again once re-gridded.
+		 * Only clear the relayout bit, whose idle call was
+		 * cancelled above.
+		 */
+		contentPtr->flags &= ~REQUESTED_RELAYOUT;
 		contentPtr->sticky = 0;
```

A candidate test (tests/grid.test style):

```tcl
test grid-forget-propagate {grid forget keeps the window's own propagate setting} -setup {
    frame .p
    frame .p.m
    frame .p.m.a
} -body {
    grid propagate .p.m 0
    grid .p.m.a
    grid .p.m -in .p
    grid forget .p.m
    grid .p.m -in .p
    grid propagate .p.m
} -cleanup {
    destroy .p
} -result 0
```
User Comments: nab added on 2026-09-22 17:17:11:
merci

jan.nijtmans added on 2026-09-22 16:20:07:

[46faf35ca534ae8d|fixed] now, in trunk and core-9-0-branch


nab added on 2026-09-03 05:58:03:
thanks Jan for taking care of this.

++

jan.nijtmans added on 2026-09-02 21:08:45:

Proposed [1ee98b20ff65bd8e|fix] and testcase now committed to a branch.

The provided testcase actually fails without applying the patch, and succeeds when the patch is applied. So, it looks like this really fixes some actual bug.

The analysis is very clear to understand. If there are no objections, I will merge this to core-9-0-branch and trunk (after Tk 9.1b1 is out).

Thanks for the report!