Fix bug with using uninitialized flags in GetParentForModalDialog().

GetParentForModalDialog() was called from the ctor initialized list before
m_windowStyle could be initialized by the base class ctor in several different
places, meaning that the check for wxDIALOG_NO_PARENT in this function was
using uninitialized variable.

Fix this by passing the style parameter explicitly to this function to allow
using it from derived class ctors. Still keep an overload which uses the
actual window parent and flags which is simpler to use for later calls to this
function.

Thanks valgrind for finding this one.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64019 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-04-18 00:05:37 +00:00
parent 852febd80e
commit cdc48273b4
24 changed files with 49 additions and 42 deletions

View File

@@ -98,8 +98,19 @@ public:
// but fall back to the current active window or main application window as // but fall back to the current active window or main application window as
// last resort if it is unsuitable. // last resort if it is unsuitable.
// //
// As this function is often called from the ctor, the window style may be
// not set yet and hence must be passed explicitly to it so that we could
// check whether it contains wxDIALOG_NO_PARENT bit.
//
// This function always returns a valid top level window or NULL. // This function always returns a valid top level window or NULL.
wxWindow *GetParentForModalDialog(wxWindow *parent = NULL) const; wxWindow *GetParentForModalDialog(wxWindow *parent, long style) const;
// This overload can only be used for already initialized windows, i.e. not
// from the ctor. It uses the current window parent and style.
wxWindow *GetParentForModalDialog() const
{
return GetParentForModalDialog(GetParent(), GetWindowStyle());
}
#if wxUSE_STATTEXT // && wxUSE_TEXTCTRL #if wxUSE_STATTEXT // && wxUSE_TEXTCTRL
// splits text up at newlines and places the // splits text up at newlines and places the

View File

@@ -116,12 +116,13 @@ wxWindow *wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow *parent) const
return parent; return parent;
} }
wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const wxWindow *
wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const
{ {
// creating a parent-less modal dialog will result (under e.g. wxGTK2) // 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 unless we // in an unfocused dialog, so try to find a valid parent for it unless we
// were explicitly asked not to // were explicitly asked not to
if ( HasFlag(wxDIALOG_NO_PARENT) ) if ( style & wxDIALOG_NO_PARENT )
return NULL; return NULL;
// first try the given parent // first try the given parent

View File

@@ -140,7 +140,7 @@ void wxGenericColourDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data) bool wxGenericColourDialog::Create(wxWindow *parent, wxColourData *data)
{ {
if ( !wxDialog::Create(GetParentForModalDialog(parent), wxID_ANY, if ( !wxDialog::Create(GetParentForModalDialog(parent, 0), wxID_ANY,
_("Choose colour"), _("Choose colour"),
wxPoint(0, 0), wxSize(900, 900)) ) wxPoint(0, 0), wxSize(900, 900)) )
return false; return false;

View File

@@ -79,7 +79,7 @@ bool wxGenericDirDialog::Create(wxWindow* parent,
{ {
wxBusyCursor cursor; wxBusyCursor cursor;
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if (!wxDirDialogBase::Create(parent, title, defaultPath, style, pos, sz, name)) if (!wxDirDialogBase::Create(parent, title, defaultPath, style, pos, sz, name))
return false; return false;

View File

@@ -87,7 +87,7 @@ bool wxGenericFindReplaceDialog::Create(wxWindow *parent,
const wxString& title, const wxString& title,
int style) int style)
{ {
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if ( !wxDialog::Create(parent, wxID_ANY, title, if ( !wxDialog::Create(parent, wxID_ANY, title,
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,

View File

@@ -164,7 +164,7 @@ bool wxGenericFileDialog::Create( wxWindow *parent,
{ {
m_bypassGenericImpl = bypassGenericImpl; m_bypassGenericImpl = bypassGenericImpl;
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile, if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile,
wildCard, style, pos, sz, name)) wildCard, style, pos, sz, name))

View File

@@ -282,7 +282,7 @@ void wxGenericFontDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
bool wxGenericFontDialog::DoCreate(wxWindow *parent) bool wxGenericFontDialog::DoCreate(wxWindow *parent)
{ {
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, 0);
if ( !wxDialog::Create( parent , wxID_ANY , wxT("Choose Font") , if ( !wxDialog::Create( parent , wxID_ANY , wxT("Choose Font") ,
wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,

View File

@@ -60,7 +60,7 @@ wxGenericMessageDialog::wxGenericMessageDialog( wxWindow *parent,
const wxString& caption, const wxString& caption,
long style, long style,
const wxPoint& pos) const wxPoint& pos)
: wxMessageDialogBase(GetParentForModalDialog(parent), : wxMessageDialogBase(GetParentForModalDialog(parent, style),
message, message,
caption, caption,
style), style),

View File

@@ -77,7 +77,7 @@ wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
long min, long min,
long max, long max,
const wxPoint& pos) const wxPoint& pos)
: wxDialog(GetParentForModalDialog(parent), : wxDialog(GetParentForModalDialog(parent, 0),
wxID_ANY, caption, wxID_ANY, caption,
pos, wxDefaultSize) pos, wxDefaultSize)
{ {

View File

@@ -145,7 +145,7 @@ END_EVENT_TABLE()
wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent, wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
wxPrintDialogData* data) wxPrintDialogData* data)
: wxPrintDialogBase(GetParentForModalDialog(parent), : wxPrintDialogBase(GetParentForModalDialog(parent, 0),
wxID_ANY, _("Print"), wxID_ANY, _("Print"),
wxPoint(0,0), wxSize(600, 600), wxPoint(0,0), wxSize(600, 600),
wxDEFAULT_DIALOG_STYLE | wxDEFAULT_DIALOG_STYLE |
@@ -159,7 +159,7 @@ wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent, wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
wxPrintData* data) wxPrintData* data)
: wxPrintDialogBase(GetParentForModalDialog(parent), : wxPrintDialogBase(GetParentForModalDialog(parent, 0),
wxID_ANY, _("Print"), wxID_ANY, _("Print"),
wxPoint(0,0), wxSize(600, 600), wxPoint(0,0), wxSize(600, 600),
wxDEFAULT_DIALOG_STYLE | wxDEFAULT_DIALOG_STYLE |

View File

@@ -99,7 +99,7 @@ wxProgressDialog::wxProgressDialog(const wxString& title,
int maximum, int maximum,
wxWindow *parent, wxWindow *parent,
int style) int style)
: wxDialog(GetParentForModalDialog(parent), wxID_ANY, title), : wxDialog(GetParentForModalDialog(parent, style), wxID_ANY, title),
m_skip(false), m_skip(false),
m_delay(3), m_delay(3),
m_hasAbortButton(false), m_hasAbortButton(false),

View File

@@ -62,7 +62,7 @@ bool wxPropertySheetDialog::Create(wxWindow* parent, wxWindowID id, const wxStri
const wxPoint& pos, const wxSize& sz, long style, const wxPoint& pos, const wxSize& sz, long style,
const wxString& name) const wxString& name)
{ {
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if (!wxDialog::Create(parent, id, title, pos, sz, style|wxCLIP_CHILDREN, name)) if (!wxDialog::Create(parent, id, title, pos, sz, style|wxCLIP_CHILDREN, name))
return false; return false;

View File

@@ -71,7 +71,7 @@ wxTextEntryDialog::wxTextEntryDialog(wxWindow *parent,
const wxString& value, const wxString& value,
long style, long style,
const wxPoint& pos) const wxPoint& pos)
: wxDialog(GetParentForModalDialog(parent), : wxDialog(GetParentForModalDialog(parent, style),
wxID_ANY, caption, pos, wxDefaultSize, wxID_ANY, caption, pos, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE), wxDEFAULT_DIALOG_STYLE),
m_value(value) m_value(value)

View File

@@ -218,7 +218,7 @@ END_EVENT_TABLE()
wxTipDialog::wxTipDialog(wxWindow *parent, wxTipDialog::wxTipDialog(wxWindow *parent,
wxTipProvider *tipProvider, wxTipProvider *tipProvider,
bool showAtStartup) bool showAtStartup)
: wxDialog(GetParentForModalDialog(parent), wxID_ANY, _("Tip of the Day"), : wxDialog(GetParentForModalDialog(parent, 0), wxID_ANY, _("Tip of the Day"),
wxDefaultPosition, wxDefaultSize, wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
) )

View File

@@ -48,7 +48,7 @@ bool wxColourDialog::Create(wxWindow *parent, wxColourData *data)
if (data) if (data)
m_data = *data; m_data = *data;
m_parent = GetParentForModalDialog(parent); m_parent = GetParentForModalDialog(parent, 0);
GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget) GtkWindow * const parentGTK = m_parent ? GTK_WINDOW(m_parent->m_widget)
: NULL; : NULL;

View File

@@ -111,7 +111,7 @@ int wxDialog::ShowModal()
if ( win ) if ( win )
win->GTKReleaseMouseAndNotify(); win->GTKReleaseMouseAndNotify();
wxWindow * const parent = GetParentForModalDialog(GetParent()); wxWindow * const parent = GetParentForModalDialog();
if ( parent ) if ( parent )
{ {
gtk_window_set_transient_for( GTK_WINDOW(m_widget), gtk_window_set_transient_for( GTK_WINDOW(m_widget),

View File

@@ -99,7 +99,7 @@ wxDirDialog::wxDirDialog(wxWindow* parent,
{ {
m_message = title; m_message = title;
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if (!PreCreation(parent, pos, wxDefaultSize) || if (!PreCreation(parent, pos, wxDefaultSize) ||
!CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style, !CreateBase(parent, wxID_ANY, pos, wxDefaultSize, style,

View File

@@ -173,7 +173,7 @@ wxFileDialog::wxFileDialog(wxWindow *parent, const wxString& message,
const wxString& name) const wxString& name)
: wxFileDialogBase() : wxFileDialogBase()
{ {
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, style);
if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName, if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFileName,
wildCard, style, pos, sz, name)) wildCard, style, pos, sz, name))

View File

@@ -86,7 +86,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxFontDialog, wxDialog)
bool wxFontDialog::DoCreate(wxWindow *parent) bool wxFontDialog::DoCreate(wxWindow *parent)
{ {
parent = GetParentForModalDialog(parent); parent = GetParentForModalDialog(parent, 0);
if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) || if (!PreCreation( parent, wxDefaultPosition, wxDefaultSize ) ||
!CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, !CreateBase( parent, -1, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE,

View File

@@ -44,10 +44,13 @@ wxMessageDialog::wxMessageDialog(wxWindow *parent,
const wxString& caption, const wxString& caption,
long style, long style,
const wxPoint& WXUNUSED(pos)) const wxPoint& WXUNUSED(pos))
: wxMessageDialogWithCustomLabels(GetParentForModalDialog(parent), : wxMessageDialogWithCustomLabels
message, (
caption, GetParentForModalDialog(parent, style),
style) message,
caption,
style
)
{ {
} }

View File

@@ -192,14 +192,11 @@ int wxDialog::ShowModal()
// use the apps top level window as parent if none given unless explicitly // use the apps top level window as parent if none given unless explicitly
// forbidden // forbidden
if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) ) wxWindow * const parent = GetParentForModalDialog();
if ( parent )
{ {
wxWindow * const parent = GetParentForModalDialog(); m_parent = parent;
if ( parent ) gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
{
m_parent = parent;
gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
}
} }
wxBusyCursorSuspender cs; // temporarily suppress the busy cursor wxBusyCursorSuspender cs; // temporarily suppress the busy cursor

View File

@@ -443,8 +443,7 @@ int wxMessageDialog::ShowModal()
} }
// use the top level window as parent if none specified // use the top level window as parent if none specified
if ( !m_parent ) m_parent = GetParentForModalDialog();
m_parent = GetParentForModalDialog();
HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL; HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
#if wxUSE_INTL #if wxUSE_INTL

View File

@@ -375,8 +375,7 @@ bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
#else // !__WXMICROWIN__ #else // !__WXMICROWIN__
// static cast is valid as we're only ever called for dialogs // static cast is valid as we're only ever called for dialogs
wxWindow * const wxWindow * const
parent = static_cast<wxDialog *>(this)-> parent = static_cast<wxDialog *>(this)->GetParentForModalDialog();
GetParentForModalDialog(GetParent());
m_hWnd = (WXHWND)::CreateDialogIndirect m_hWnd = (WXHWND)::CreateDialogIndirect
( (

View File

@@ -179,13 +179,10 @@ int wxDialog::ShowModal()
// use the apps top level window as parent if none given unless explicitly // use the apps top level window as parent if none given unless explicitly
// forbidden // forbidden
if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) ) wxWindow * const parent = GetParentForModalDialog();
if ( parent && parent != this )
{ {
wxWindow * const parent = GetParentForModalDialog(); m_parent = parent;
if ( parent && parent != this )
{
m_parent = parent;
}
} }
Show(true); Show(true);