Subject: bug#31440: Can't redefine parent-frame on NS

From: Alan Third <[email-masked]>
Date: Sun, 13 May 2018 10:40:54 +0100

from emacs -Q:

set up three frames, a, b and c:

    (set-frame-parameter c 'parent-frame a)

c becomes child of a.

(set-frame-parameter c 'parent-frame b)

expected: c becomes child of b and is no longer child of a.

actual: c remains child of a, and b now cannot be raised above a and b.

(set-frame-parameter c 'parent-frame nil)

expected: c ceases to be a child frame.

actual: emacs crashes.

Patch attached.

If this is too late for 26.1 I don’t think that will be a huge issue
as it seems redefining parent-frame is not something people want to
do, and it’s unlikely to be something a user does by accident.
-- 
Alan Third
>From a33d6248a7369fc18111e42b6157a6bf84c5578a Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Sun, 13 May 2018 10:33:44 +0100
Subject: [PATCH] Fix redefinition of child frames on NS

* src/nsterm.m (x_set_parent_frame): If the NSWindow has an existing
parent frame, remove it.
---
 src/nsterm.m | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/nsterm.m b/src/nsterm.m
index c8ae31abc0..df883346de 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1958,12 +1958,20 @@ so some key presses (TAB) are swallowed by the system. */
 
   if (p != FRAME_PARENT_FRAME (f))
     {
-      parent = [FRAME_NS_VIEW (p) window];
+      block_input ();
       child = [FRAME_NS_VIEW (f) window];
 
-      block_input ();
-      [parent addChildWindow: child
-                     ordered: NSWindowAbove];
+      if ([child parentWindow] != nil)
+        [[child parentWindow] removeChildWindow:child];
+
+      if (!NILP (new_value))
+        {
+          parent = [FRAME_NS_VIEW (p) window];
+
+          [parent addChildWindow: child
+                         ordered: NSWindowAbove];
+        }
+
       unblock_input ();
 
       fset_parent_frame (f, new_value);
-- 
2.16.1