vcl/inc/jsdialog/jsdialogbuilder.hxx |    5 +++++
 vcl/jsdialog/jsdialogbuilder.cxx     |   32 +++++++++++++++++++++++---------
 2 files changed, 28 insertions(+), 9 deletions(-)

New commits:
commit ff4bd9dc63146f3338ecb1fcee4dbe1606454cad
Author:     Szymon Kłos <szymon.k...@collabora.com>
AuthorDate: Tue Nov 23 09:55:00 2021 +0100
Commit:     Jan Holesovsky <ke...@collabora.com>
CommitDate: Tue Nov 23 19:52:10 2021 +0100

    jsdialog: correctly clean statically created message dialogs
    
    - remember WindowId for statically created message dialogs
    - remember _DIALOG_ handle for message dialogs so response
      action can be done
    
    Change-Id: I1f6c9877e20dcbed4855b32d1e89d9ce3ff12111
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125686
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Jan Holesovsky <ke...@collabora.com>

diff --git a/vcl/inc/jsdialog/jsdialogbuilder.hxx 
b/vcl/inc/jsdialog/jsdialogbuilder.hxx
index 12305d05d424..b7cec478dc37 100644
--- a/vcl/inc/jsdialog/jsdialogbuilder.hxx
+++ b/vcl/inc/jsdialog/jsdialogbuilder.hxx
@@ -217,6 +217,8 @@ class JSInstanceBuilder : public SalInstanceBuilder, public 
JSDialogSender
     static std::map<std::string, WidgetMap>& GetLOKWeldWidgetsMap();
     static void InsertWindowToMap(const std::string& nWindowId);
     void RememberWidget(const OString& id, weld::Widget* pWidget);
+    static void RememberWidget(const std::string& nWindowId, const OString& id,
+                               weld::Widget* pWidget);
     static weld::Widget* FindWeldWidgetsMap(const std::string& nWindowId, 
const OString& rWidget);
 
     std::string getMapIdFromWindowId() const;
@@ -526,6 +528,9 @@ class JSMessageDialog : public 
JSWidget<SalInstanceMessageDialog, ::MessageDialo
     std::unique_ptr<JSButton> m_pOK;
     std::unique_ptr<JSButton> m_pCancel;
 
+    // used for message dialogs created using static functions
+    std::string m_sWindowId;
+
     DECL_LINK(OKHdl, weld::Button&, void);
     DECL_LINK(CancelHdl, weld::Button&, void);
 
diff --git a/vcl/jsdialog/jsdialogbuilder.cxx b/vcl/jsdialog/jsdialogbuilder.cxx
index 8268b3a6b524..e63f6e861a6f 100644
--- a/vcl/jsdialog/jsdialogbuilder.cxx
+++ b/vcl/jsdialog/jsdialogbuilder.cxx
@@ -709,12 +709,18 @@ void JSInstanceBuilder::InsertWindowToMap(const 
std::string& nWindowId)
 
 void JSInstanceBuilder::RememberWidget(const OString& id, weld::Widget* 
pWidget)
 {
-    auto it = GetLOKWeldWidgetsMap().find(getMapIdFromWindowId());
+    RememberWidget(getMapIdFromWindowId(), id, pWidget);
+    m_aRememberedWidgets.push_back(id.getStr());
+}
+
+void JSInstanceBuilder::RememberWidget(const std::string& nWindowId, const 
OString& id,
+                                       weld::Widget* pWidget)
+{
+    auto it = GetLOKWeldWidgetsMap().find(nWindowId);
     if (it != GetLOKWeldWidgetsMap().end())
     {
         it->second.erase(id);
         it->second.insert(WidgetMap::value_type(id, pWidget));
-        m_aRememberedWidgets.push_back(id.getStr());
     }
 }
 
@@ -1108,8 +1114,15 @@ weld::MessageDialog* 
JSInstanceBuilder::CreateMessageDialog(weld::Widget* pParen
     }
 
     xMessageDialog->SetLOKTunnelingState(false);
-    InsertWindowToMap(std::to_string(xMessageDialog->GetLOKWindowId()));
-    return new JSMessageDialog(xMessageDialog, nullptr, true);
+    std::string sWindowId = std::to_string(xMessageDialog->GetLOKWindowId());
+    InsertWindowToMap(sWindowId);
+
+    weld::MessageDialog* pRet = new JSMessageDialog(xMessageDialog, nullptr, 
true);
+
+    if (pRet)
+        RememberWidget(sWindowId, "__DIALOG__", pRet);
+
+    return pRet;
 }
 
 JSDialog::JSDialog(JSDialogSender* pSender, ::Dialog* pDialog, 
SalInstanceBuilder* pBuilder,
@@ -1313,12 +1326,13 @@ JSMessageDialog::JSMessageDialog(::MessageDialog* 
pDialog, SalInstanceBuilder* p
 
     if (!pBuilder)
     {
+        m_sWindowId = std::to_string(m_xMessageDialog->GetLOKWindowId());
+
         if (::OKButton* pOKBtn
             = 
dynamic_cast<::OKButton*>(m_xMessageDialog->get_widget_for_response(RET_OK)))
         {
             m_pOK.reset(new JSButton(m_pSender, pOKBtn, nullptr, false));
-            
JSInstanceBuilder::AddChildWidget(std::to_string(m_xMessageDialog->GetLOKWindowId()),
-                                              pOKBtn->get_id().toUtf8(), 
m_pOK.get());
+            JSInstanceBuilder::AddChildWidget(m_sWindowId, 
pOKBtn->get_id().toUtf8(), m_pOK.get());
             m_pOK->connect_clicked(LINK(this, JSMessageDialog, OKHdl));
         }
 
@@ -1326,8 +1340,8 @@ JSMessageDialog::JSMessageDialog(::MessageDialog* 
pDialog, SalInstanceBuilder* p
             = 
dynamic_cast<::CancelButton*>(m_xMessageDialog->get_widget_for_response(RET_CANCEL)))
         {
             m_pCancel.reset(new JSButton(m_pSender, pCancelBtn, nullptr, 
false));
-            
JSInstanceBuilder::AddChildWidget(std::to_string(m_xMessageDialog->GetLOKWindowId()),
-                                              pCancelBtn->get_id().toUtf8(), 
m_pCancel.get());
+            JSInstanceBuilder::AddChildWidget(m_sWindowId, 
pCancelBtn->get_id().toUtf8(),
+                                              m_pCancel.get());
             m_pCancel->connect_clicked(LINK(this, JSMessageDialog, CancelHdl));
         }
     }
@@ -1336,7 +1350,7 @@ JSMessageDialog::JSMessageDialog(::MessageDialog* 
pDialog, SalInstanceBuilder* p
 JSMessageDialog::~JSMessageDialog()
 {
     if (m_pOK || m_pCancel)
-        
JSInstanceBuilder::RemoveWindowWidget(std::to_string(m_xMessageDialog->GetLOKWindowId()));
+        JSInstanceBuilder::RemoveWindowWidget(m_sWindowId);
 }
 
 IMPL_LINK_NOARG(JSMessageDialog, OKHdl, weld::Button&, void) { 
response(RET_OK); }

Reply via email to