From 2229243bdf17485b33c15786124ab99366b83975 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 22 Apr 2007 20:34:41 +0000
Subject: [PATCH] added wxDialog::GetParentForModalDialog() and use it to try
 to always create modal dialogs with a parent (slightly modified patch
 1702962)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45588 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 include/wx/dialog.h       |  4 ++++
 src/common/dlgcmn.cpp     | 19 +++++++++++++++++++
 src/generic/aboutdlgg.cpp |  4 ++--
 src/generic/colrdlgg.cpp  |  5 +++--
 src/generic/dirdlgg.cpp   |  2 ++
 src/generic/fdrepdlg.cpp  |  2 ++
 src/generic/filedlgg.cpp  |  2 ++
 src/generic/fontdlgg.cpp  |  5 ++++-
 src/generic/msgdlgg.cpp   |  2 ++
 src/generic/numdlgg.cpp   |  3 ++-
 src/generic/prntdlgg.cpp  |  6 ++++--
 src/generic/progdlgg.cpp  |  2 +-
 src/generic/propdlg.cpp   |  2 ++
 src/generic/textdlgg.cpp  |  3 ++-
 src/generic/tipdlg.cpp    |  2 +-
 src/gtk/colordlg.cpp      |  5 +++--
 src/gtk/dirdlg.cpp        |  2 ++
 src/gtk/filedlg.cpp       |  2 ++
 src/gtk/fontdlg.cpp       |  2 ++
 src/gtk/msgdlg.cpp        |  3 ++-
 20 files changed, 63 insertions(+), 14 deletions(-)

diff --git a/include/wx/dialog.h b/include/wx/dialog.h
index 766771c42d..58a30e2d0c 100644
--- a/include/wx/dialog.h
+++ b/include/wx/dialog.h
@@ -64,6 +64,10 @@ public:
     void SetEscapeId(int escapeId);
     int GetEscapeId() const { return m_escapeId; }
 
+    // Returns the parent to use for modal dialogs if the user did not specify it
+    // explicitly
+    wxWindow *GetParentForModalDialog(wxWindow *parent = NULL) const;
+
 #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
     // splits text up at newlines and places the
     // lines into a vertical wxBoxSizer
diff --git a/src/common/dlgcmn.cpp b/src/common/dlgcmn.cpp
index be6afab98e..ec5a528ad4 100644
--- a/src/common/dlgcmn.cpp
+++ b/src/common/dlgcmn.cpp
@@ -71,6 +71,25 @@ void wxDialogBase::Init()
     WX_INIT_CONTROL_CONTAINER();
 }
 
+wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const
+{
+    // creating a parent-less modal dialog will result (under e.g. wxGTK2)
+    // in an unfocused dialog, so try to find a valid parent for it:
+    if ( parent )
+        parent = wxGetTopLevelParent(parent);
+
+    if ( !parent || parent->HasExtraStyle(wxWS_EX_TRANSIENT) )
+        parent = wxTheApp->GetTopWindow();
+
+    if ( parent && parent->HasExtraStyle(wxWS_EX_TRANSIENT) )
+    {
+        // can't use this one, it's going to disappear
+        parent = NULL;
+    }
+
+    return parent;
+}
+
 #if wxUSE_STATTEXT
 
 class wxTextSizerWrapper : public wxTextWrapper
diff --git a/src/generic/aboutdlgg.cpp b/src/generic/aboutdlgg.cpp
index 33cd027719..77e5d513c2 100644
--- a/src/generic/aboutdlgg.cpp
+++ b/src/generic/aboutdlgg.cpp
@@ -102,8 +102,8 @@ wxIcon wxAboutDialogInfo::GetIcon() const
 
 bool wxGenericAboutDialog::Create(const wxAboutDialogInfo& info)
 {
-    // TODO: should we use main frame as parent by default here?
-    if ( !wxDialog::Create(NULL, wxID_ANY, _("About ") + info.GetName(),
+    // this is a modal dialog thus we'll use GetParentForModalDialog:
+    if ( !wxDialog::Create(GetParentForModalDialog(), wxID_ANY, _("About ") + info.GetName(),
                            wxDefaultPosition, wxDefaultSize, wxRESIZE_BORDER|wxDEFAULT_DIALOG_STYLE) )
         return false;
 
diff --git a/src/generic/colrdlgg.cpp b/src/generic/colrdlgg.cpp
index ec97500ac8..0f4bfd6bab 100644
--- a/src/generic/colrdlgg.cpp
+++ b/src/generic/colrdlgg.cpp
@@ -140,8 +140,9 @@ void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
 
 bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data)
 {
-    if ( !wxDialog::Create(parent, wxID_ANY, _("Choose colour"),
-                           wxPoint(0,0), wxSize(900, 900)) )
+    if ( !wxDialog::Create(GetParentForModalDialog(parent), wxID_ANY,
+                           _("Choose colour"),
+                           wxPoint(0, 0), wxSize(900, 900)) )
         return false;
 
     if (data)
diff --git a/src/generic/dirdlgg.cpp b/src/generic/dirdlgg.cpp
index 5b7b50b2c3..bfe97a9cf9 100644
--- a/src/generic/dirdlgg.cpp
+++ b/src/generic/dirdlgg.cpp
@@ -78,6 +78,8 @@ bool wxGenericDirDialog::Create(wxWindow* parent,
 {
     wxBusyCursor cursor;
 
+    parent = GetParentForModalDialog(parent);
+
     if (!wxDirDialogBase::Create(parent, title, defaultPath, style, pos, sz, name))
         return false;
 
diff --git a/src/generic/fdrepdlg.cpp b/src/generic/fdrepdlg.cpp
index 797ac68cbb..976ade0dec 100644
--- a/src/generic/fdrepdlg.cpp
+++ b/src/generic/fdrepdlg.cpp
@@ -87,6 +87,8 @@ bool wxGenericFindReplaceDialog::Create(wxWindow *parent,
                                         const wxString& title,
                                         int style)
 {
+    parent = GetParentForModalDialog(parent);
+
     if ( !wxDialog::Create(parent, wxID_ANY, title,
                            wxDefaultPosition, wxDefaultSize,
                            wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
diff --git a/src/generic/filedlgg.cpp b/src/generic/filedlgg.cpp
index 2f493b2e32..90c086fd97 100644
--- a/src/generic/filedlgg.cpp
+++ b/src/generic/filedlgg.cpp
@@ -996,6 +996,8 @@ bool wxGenericFileDialog::Create( wxWindow *parent,
 {
     m_bypassGenericImpl = bypassGenericImpl;
 
+    parent = GetParentForModalDialog(parent);
+
     if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile,
                                   wildCard, style, pos, sz, name))
     {
diff --git a/src/generic/fontdlgg.cpp b/src/generic/fontdlgg.cpp
index 06a23703c6..175954049b 100644
--- a/src/generic/fontdlgg.cpp
+++ b/src/generic/fontdlgg.cpp
@@ -190,7 +190,10 @@ void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
 
 bool wxGenericFontDialog::DoCreate(wxWindow *parent)
 {
-    if ( !wxDialog::Create( parent , wxID_ANY , _T("Choose Font") , wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
+    parent = GetParentForModalDialog(parent);
+
+    if ( !wxDialog::Create( parent , wxID_ANY , _T("Choose Font") ,
+                            wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
         _T("fontdialog") ) )
     {
         wxFAIL_MSG( wxT("wxFontDialog creation failed") );
diff --git a/src/generic/msgdlgg.cpp b/src/generic/msgdlgg.cpp
index e17387ba58..1565d88e4f 100644
--- a/src/generic/msgdlgg.cpp
+++ b/src/generic/msgdlgg.cpp
@@ -64,6 +64,8 @@ wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
 {
     SetMessageDialogStyle(style);
 
+    parent = GetParentForModalDialog(parent);
+
     bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
 
     wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
diff --git a/src/generic/numdlgg.cpp b/src/generic/numdlgg.cpp
index 615b64ba0f..a7099e1c19 100644
--- a/src/generic/numdlgg.cpp
+++ b/src/generic/numdlgg.cpp
@@ -77,7 +77,8 @@ wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
                                          long min,
                                          long max,
                                          const wxPoint& pos)
-                   : wxDialog(parent, wxID_ANY, caption,
+                   : wxDialog(GetParentForModalDialog(parent),
+                              wxID_ANY, caption,
                               pos, wxDefaultSize)
 {
     m_value = value;
diff --git a/src/generic/prntdlgg.cpp b/src/generic/prntdlgg.cpp
index 3b0fb9a59a..18e5c56675 100644
--- a/src/generic/prntdlgg.cpp
+++ b/src/generic/prntdlgg.cpp
@@ -136,7 +136,8 @@ END_EVENT_TABLE()
 
 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
                                            wxPrintDialogData* data)
-                    : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
+                    : wxPrintDialogBase(GetParentForModalDialog(parent), 
+                               wxID_ANY, _("Print"),
                                wxPoint(0,0), wxSize(600, 600),
                                wxDEFAULT_DIALOG_STYLE |
                                wxTAB_TRAVERSAL)
@@ -149,7 +150,8 @@ wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
 
 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
                                            wxPrintData* data)
-                    : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
+                    : wxPrintDialogBase(GetParentForModalDialog(parent), 
+                                wxID_ANY, _("Print"),
                                wxPoint(0,0), wxSize(600, 600),
                                wxDEFAULT_DIALOG_STYLE |
                                wxTAB_TRAVERSAL)
diff --git a/src/generic/progdlgg.cpp b/src/generic/progdlgg.cpp
index ec7f57c951..f230c37458 100644
--- a/src/generic/progdlgg.cpp
+++ b/src/generic/progdlgg.cpp
@@ -97,7 +97,7 @@ wxProgressDialog::wxProgressDialog(wxString const &title,
                                    int maximum,
                                    wxWindow *parent,
                                    int style)
-                : wxDialog(parent, wxID_ANY, title),
+                : wxDialog(GetParentForModalDialog(parent), wxID_ANY, title),
                   m_skip(false),
                   m_delay(3),
                   m_hasAbortButton(false),
diff --git a/src/generic/propdlg.cpp b/src/generic/propdlg.cpp
index 15cd495c34..fa41bed4a8 100644
--- a/src/generic/propdlg.cpp
+++ b/src/generic/propdlg.cpp
@@ -62,6 +62,8 @@ bool wxPropertySheetDialog::Create(wxWindow* parent, wxWindowID id, const wxStri
                                        const wxPoint& pos, const wxSize& sz, long style,
                                        const wxString& name)
 {
+    parent = GetParentForModalDialog(parent);
+
     if (!wxDialog::Create(parent, id, title, pos, sz, style|wxCLIP_CHILDREN, name))
         return false;
 
diff --git a/src/generic/textdlgg.cpp b/src/generic/textdlgg.cpp
index de16dbbd5c..e7b809eb60 100644
--- a/src/generic/textdlgg.cpp
+++ b/src/generic/textdlgg.cpp
@@ -71,7 +71,8 @@ wxTextEntryDialog::wxTextEntryDialog(wxWindow *parent,
                                      const wxString& value,
                                      long style,
                                      const wxPoint& pos)
-                 : wxDialog(parent, wxID_ANY, caption, pos, wxDefaultSize,
+                 : wxDialog(GetParentForModalDialog(parent),
+                            wxID_ANY, caption, pos, wxDefaultSize,
                             wxDEFAULT_DIALOG_STYLE),
                    m_value(value)
 {
diff --git a/src/generic/tipdlg.cpp b/src/generic/tipdlg.cpp
index e8fbb90d0c..491a0ba700 100644
--- a/src/generic/tipdlg.cpp
+++ b/src/generic/tipdlg.cpp
@@ -215,7 +215,7 @@ END_EVENT_TABLE()
 wxTipDialog::wxTipDialog(wxWindow *parent,
                          wxTipProvider *tipProvider,
                          bool showAtStartup)
-           : wxDialog(parent, wxID_ANY, _("Tip of the Day"),
+           : wxDialog(GetParentForModalDialog(parent), wxID_ANY, _("Tip of the Day"),
                       wxDefaultPosition, wxDefaultSize,
                       wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
                       )
diff --git a/src/gtk/colordlg.cpp b/src/gtk/colordlg.cpp
index 34e9c118fe..20bacbb9e2 100644
--- a/src/gtk/colordlg.cpp
+++ b/src/gtk/colordlg.cpp
@@ -41,9 +41,10 @@ bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
     wxString title(_("Choose colour"));
     m_widget = gtk_color_selection_dialog_new(wxGTK_CONV(title));
 
-    if (parent)
+    m_parent = GetParentForModalDialog(parent);
+    if ( m_parent )
     {
-        GtkWindow* gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(parent->m_widget) );
+        GtkWindow* gtk_parent = GTK_WINDOW( gtk_widget_get_toplevel(m_parent->m_widget) );
         gtk_window_set_transient_for(GTK_WINDOW(m_widget),
                                      gtk_parent);
     }
diff --git a/src/gtk/dirdlg.cpp b/src/gtk/dirdlg.cpp
index 699f568fcb..f555dcdf17 100644
--- a/src/gtk/dirdlg.cpp
+++ b/src/gtk/dirdlg.cpp
@@ -99,6 +99,8 @@ wxDirDialog::wxDirDialog(wxWindow* parent, const wxString& title,
         m_message = title;
         m_needParent = false;
 
+        parent = GetParentForModalDialog(parent);
+
         if (!PreCreation(parent, pos, wxDefaultSize) ||
             !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
                     wxDefaultValidator, wxT("dirdialog")))
diff --git a/src/gtk/filedlg.cpp b/src/gtk/filedlg.cpp
index 65e530ab25..60fc46cdb8 100644
--- a/src/gtk/filedlg.cpp
+++ b/src/gtk/filedlg.cpp
@@ -154,6 +154,8 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
 
     m_needParent = false;
 
+    parent = GetParentForModalDialog(parent);
+
     if (!PreCreation(parent, pos, wxDefaultSize) ||
         !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,
                 wxDefaultValidator, wxT("filedialog")))
diff --git a/src/gtk/fontdlg.cpp b/src/gtk/fontdlg.cpp
index c95411e370..c20b8b55ab 100644
--- a/src/gtk/fontdlg.cpp
+++ b/src/gtk/fontdlg.cpp
@@ -88,6 +88,8 @@ bool wxFontDialog::DoCreate(wxWindow *parent)
 {
     m_needParent = false;
 
+    parent = GetParentForModalDialog(parent);
+
     if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
         !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,
                      wxDefaultValidator, wxT("fontdialog") ))
diff --git a/src/gtk/msgdlg.cpp b/src/gtk/msgdlg.cpp
index e2b0a4ef26..ddbecfa54a 100644
--- a/src/gtk/msgdlg.cpp
+++ b/src/gtk/msgdlg.cpp
@@ -38,7 +38,8 @@ wxMessageDialog::wxMessageDialog(wxWindow *parent,
     m_caption = caption;
     m_message = message;
     SetMessageDialogStyle(style);
-    m_parent = wxGetTopLevelParent(parent);
+
+    m_parent = GetParentForModalDialog(parent);
 
     GtkMessageType type = GTK_MESSAGE_ERROR;
     GtkButtonsType buttons = GTK_BUTTONS_OK;
-- 
2.47.2