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
// 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.
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
// splits text up at newlines and places the

View File

@@ -116,12 +116,13 @@ wxWindow *wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow *parent) const
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)
// in an unfocused dialog, so try to find a valid parent for it unless we
// were explicitly asked not to
if ( HasFlag(wxDIALOG_NO_PARENT) )
if ( style & wxDIALOG_NO_PARENT )
return NULL;
// first try the given parent

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -99,7 +99,7 @@ wxProgressDialog::wxProgressDialog(const wxString& title,
int maximum,
wxWindow *parent,
int style)
: wxDialog(GetParentForModalDialog(parent), wxID_ANY, title),
: wxDialog(GetParentForModalDialog(parent, style), wxID_ANY, title),
m_skip(false),
m_delay(3),
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 wxString& name)
{
parent = GetParentForModalDialog(parent);
parent = GetParentForModalDialog(parent, style);
if (!wxDialog::Create(parent, id, title, pos, sz, style|wxCLIP_CHILDREN, name))
return false;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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