c12ecfb
From ea6cfca48017b76bfeb8898e6d9e47b3011a3add Mon Sep 17 00:00:00 2001
9b8b810
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
9b8b810
Date: Sat, 6 Nov 2021 21:32:21 +0000
9b8b810
Subject: [PATCH] Resolves: tdf#145567 restore focus to the usual frame focus
9b8b810
 widget
9b8b810
9b8b810
when tearing down the start center. Don't leave the focus in an
9b8b810
arbitrary widget.
9b8b810
9b8b810
Change-Id: I82c30c94121dc43b2ea1b4fbc66a0a3e79f7e664
9b8b810
---
c12ecfb
 vcl/unx/gtk3/gtk3gtkinst.cxx | 28 ++++++++++++++++++++++++++++
c12ecfb
 1 file changed, 28 insertions(+)
9b8b810
9b8b810
diff --git a/vcl/unx/gtk3/gtk3gtkinst.cxx b/vcl/unx/gtk3/gtk3gtkinst.cxx
c12ecfb
index a5d233e2f95e..0fddb27aaa2d 100644
9b8b810
--- a/vcl/unx/gtk3/gtk3gtkinst.cxx
9b8b810
+++ b/vcl/unx/gtk3/gtk3gtkinst.cxx
c12ecfb
@@ -1899,6 +1899,26 @@ GdkDragAction VclToGdk(sal_Int8 dragOperation)
c12ecfb
     return eRet;
c12ecfb
 }
c12ecfb
 
c12ecfb
+GtkWindow* get_active_window()
c12ecfb
+{
c12ecfb
+    GtkWindow* pFocus = nullptr;
c12ecfb
+
c12ecfb
+    GList* pList = gtk_window_list_toplevels();
c12ecfb
+
c12ecfb
+    for (GList* pEntry = pList; pEntry; pEntry = pEntry->next)
c12ecfb
+    {
c12ecfb
+        if (gtk_window_has_toplevel_focus(GTK_WINDOW(pEntry->data)))
c12ecfb
+        {
c12ecfb
+            pFocus = GTK_WINDOW(pEntry->data);
c12ecfb
+            break;
c12ecfb
+        }
c12ecfb
+    }
c12ecfb
+
c12ecfb
+    g_list_free(pList);
c12ecfb
+
c12ecfb
+    return pFocus;
c12ecfb
+}
c12ecfb
+
c12ecfb
 class GtkInstanceWidget : public virtual weld::Widget
c12ecfb
 {
c12ecfb
 protected:
c12ecfb
@@ -16635,6 +16655,14 @@ private:
9b8b810
         // rehook handler and let vcl cycle its own way through this widget's
9b8b810
         // children
9b8b810
         pFrame->AllowCycleFocusOut();
9b8b810
+
9b8b810
+        // tdf#145567 if the focus is in this hierarchy then, now that we are tearing down,
9b8b810
+        // move focus to the usual focus candidate for the frame
9b8b810
+        GtkWindow* pFocusWin = get_active_window();
9b8b810
+        GtkWidget* pFocus = pFocusWin ? gtk_window_get_focus(pFocusWin) : nullptr;
9b8b810
+        bool bHasFocus = pFocus && gtk_widget_is_ancestor(pFocus, pTopLevel);
9b8b810
+        if (bHasFocus)
9b8b810
+            pFrame->GrabFocus();
9b8b810
     }
9b8b810
 
9b8b810
     static void signalUnmap(GtkWidget*, gpointer user_data)
9b8b810
-- 
9b8b810
2.33.1
9b8b810