1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dialog.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/dialog.h"
28 #include "wx/settings.h"
33 #include "wx/msw/private.h"
36 #if wxUSE_COMMON_DIALOGS
40 #define wxDIALOG_DEFAULT_X 300
41 #define wxDIALOG_DEFAULT_Y 300
43 // Lists to keep track of windows, so we can disable/enable them
45 wxWindowList wxModalDialogs
;
46 wxWindowList wxModelessWindows
; // Frames and modeless dialogs
47 extern wxList WXDLLEXPORT wxPendingDelete
;
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
52 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
53 EVT_SIZE(wxDialog::OnSize
)
54 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
55 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
56 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
57 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
58 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
59 EVT_CLOSE(wxDialog::OnCloseWindow
)
66 m_modalShowing
= FALSE
;
68 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
71 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxString
& title
,
82 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
86 wxTopLevelWindows
.Append(this);
88 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
90 if (parent
) parent
->AddChild(this);
93 m_windowId
= (int)NewControlId();
102 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
103 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
105 m_windowStyle
= style
;
108 m_modalShowing
= FALSE
;
115 // All dialogs should really have this style
116 m_windowStyle
|= wxTAB_TRAVERSAL
;
118 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
119 if (m_windowStyle
& wxSTAY_ON_TOP
)
120 extendedStyle
|= WS_EX_TOPMOST
;
122 // Allows creation of dialogs with & without captions under MSWindows,
123 // resizeable or not (but a resizeable dialog always has caption -
124 // otherwise it would look too strange)
126 if ( style
& wxRESIZE_BORDER
)
127 dlg
= wxT("wxResizeableDialog");
128 else if ( style
& wxCAPTION
)
129 dlg
= wxT("wxCaptionDialog");
131 dlg
= wxT("wxNoCaptionDialog");
132 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
134 0, // style is not used if we have dlg template
138 HWND hwnd
= (HWND
)GetHWND();
142 wxLogError(_("Failed to create dialog."));
147 SubclassWin(GetHWND());
149 SetWindowText(hwnd
, title
);
150 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
155 void wxDialog::SetModal(bool flag
)
158 m_windowStyle
|= wxDIALOG_MODAL
;
160 if ( m_windowStyle
& wxDIALOG_MODAL
)
161 m_windowStyle
-= wxDIALOG_MODAL
;
163 wxModelessWindows
.DeleteObject(this);
165 wxModelessWindows
.Append(this);
168 wxDialog::~wxDialog()
170 m_isBeingDeleted
= TRUE
;
172 wxTopLevelWindows
.DeleteObject(this);
178 // For some reason, wxWindows can activate another task altogether
179 // when a frame is destroyed after a modal dialog has been invoked.
180 // Try to bring the parent to the top.
181 // dfgg: I moved this following line from end of function -
182 // must not call if another window is on top!!
183 // This can often happen with Close() and delayed deleting
184 if (GetParent() && GetParent()->GetHWND())
185 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
188 m_modalShowing
= FALSE
;
190 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
191 wxModelessWindows
.DeleteObject(this);
193 // If this is the last top-level window, exit.
194 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
196 wxTheApp
->SetTopWindow(NULL
);
198 if (wxTheApp
->GetExitOnFrameDelete())
205 // By default, pressing escape cancels the dialog
206 void wxDialog::OnCharHook(wxKeyEvent
& event
)
210 if (event
.m_keyCode
== WXK_ESCAPE
)
212 // Behaviour changed in 2.0: we'll send a Cancel message
213 // to the dialog instead of Close.
214 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
215 cancelEvent
.SetEventObject( this );
216 GetEventHandler()->ProcessEvent(cancelEvent
);
221 // We didn't process this event.
225 void wxDialog::OnPaint(wxPaintEvent
& event
)
227 // No: if you call the default procedure, it makes
228 // the following painting code not work.
229 // wxWindow::OnPaint(event);
237 void wxDialog::Iconize(bool WXUNUSED(iconize
))
239 // Windows dialog boxes can't be iconized
242 bool wxDialog::IsIconized() const
247 void wxDialog::DoSetClientSize(int width
, int height
)
249 HWND hWnd
= (HWND
) GetHWND();
251 ::GetClientRect(hWnd
, &rect
);
254 GetWindowRect(hWnd
, &rect2
);
256 // Find the difference between the entire window (title bar and all)
257 // and the client area; add this to the new client size to move the
259 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
260 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
262 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
264 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
265 event
.SetEventObject( this );
266 GetEventHandler()->ProcessEvent(event
);
269 void wxDialog::GetPosition(int *x
, int *y
) const
271 HWND hWnd
= (HWND
) GetHWND();
273 GetWindowRect(hWnd
, &rect
);
279 bool wxDialog::IsShown() const
284 bool wxDialog::IsModal() const
286 return wxModalDialogs
.Find((wxDialog
*)this) != 0; // const_cast
289 bool wxDialog::Show(bool show
)
296 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
298 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
301 if (!wxModelessWindows
.Find(this))
302 wxModelessWindows
.Append(this);
304 wxModelessWindows
.DeleteObject(this);
307 if (!wxTopLevelWindows
.Find(this))
308 wxTopLevelWindows
.Append(this);
310 wxTopLevelWindows
.DeleteObject(this);
317 // find the top level window which had focus before - we will restore
320 for ( HWND hwnd
= ::GetFocus(); hwnd
; hwnd
= ::GetParent(hwnd
) )
322 m_hwndOldFocus
= (WXHWND
)hwnd
;
327 BringWindowToTop((HWND
) GetHWND());
331 m_modalShowing
= TRUE
;
332 wxNode
*node
= wxModalDialogs
.First();
335 wxDialog
*box
= (wxDialog
*)node
->Data();
337 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
341 // if we don't do it, some window might be deleted while we have pointers
342 // to them in our disabledWindows list and the program will crash when it
343 // will try to reenable them after the modal dialog end
344 wxTheApp
->DeletePendingObjects();
345 wxList disabledWindows
;
347 node
= wxModelessWindows
.First();
350 wxWindow
*win
= (wxWindow
*)node
->Data();
351 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
353 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
354 disabledWindows
.Append(win
);
359 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
360 EnableWindow((HWND
) GetHWND(), TRUE
);
361 BringWindowToTop((HWND
) GetHWND());
363 if ( !wxModalDialogs
.Find(this) )
364 wxModalDialogs
.Append(this);
367 // Must test whether this dialog still exists: we may not process
368 // a message before the deletion.
369 while (wxModalDialogs
.Find(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
371 if ( m_acceleratorTable
.Ok() &&
372 ::TranslateAccelerator((HWND
)GetHWND(),
373 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
376 // Have processed the message
378 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&msg
) )
380 TranslateMessage(&msg
);
381 DispatchMessage(&msg
);
384 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
385 // we should try removing the m_modalShowing test
387 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
388 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
389 // a Show(FALSE) in the mean time!!!
390 // Without the test, we might delete the dialog before the end of modal showing.
392 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
394 // Keep going until we decide we've done enough
398 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
399 node
=disabledWindows
.First();
401 wxWindow
* win
= (wxWindow
*) node
->Data();
402 if (wxModalDialogs
.Find(win
) || wxModelessWindows
.Find(win
))
404 HWND hWnd
= (HWND
) win
->GetHWND();
405 if (::IsWindow(hWnd
))
406 ::EnableWindow(hWnd
,TRUE
);
413 ::SetFocus((HWND
)m_hwndOldFocus
);
415 wxModalDialogs
.DeleteObject(this);
417 wxNode
*last
= wxModalDialogs
.Last();
419 // If there's still a modal dialog active, we
420 // enable it, else we enable all modeless windows
423 // VZ: I don't understand what this is supposed to do, so I'll leave
424 // it out for now and look for horrible consequences
425 wxDialog
*box
= (wxDialog
*)last
->Data();
426 HWND hwnd
= (HWND
) box
->GetHWND();
428 if (box
->IsUserEnabled())
430 EnableWindow(hwnd
, TRUE
);
431 BringWindowToTop(hwnd
);
435 wxNode
*node
= wxModelessWindows
.First();
438 wxWindow
*win
= (wxWindow
*)node
->Data();
439 HWND hwnd
= (HWND
) win
->GetHWND();
440 // Only enable again if not user-disabled.
442 if (win
->IsUserEnabled())
444 EnableWindow(hwnd
, TRUE
);
448 // Try to highlight the correct window (the parent)
452 hWndParent
= (HWND
) GetParent()->GetHWND();
454 ::BringWindowToTop(hWndParent
);
456 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
457 m_modalShowing
= FALSE
;
464 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
465 BringWindowToTop((HWND
) GetHWND());
469 // Try to highlight the correct window (the parent)
473 hWndParent
= (HWND
) GetParent()->GetHWND();
475 ::BringWindowToTop(hWndParent
);
479 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
485 void wxDialog::SetTitle(const wxString
& title
)
487 SetWindowText((HWND
) GetHWND(), title
.c_str());
490 wxString
wxDialog::GetTitle() const
492 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
493 return wxString(wxBuffer
);
496 void wxDialog::Centre(int direction
)
498 int x_offset
,y_offset
;
499 int display_width
, display_height
;
500 int width
, height
, x
, y
;
501 wxWindow
*parent
= GetParent();
502 if ((direction
& wxCENTER_FRAME
) && parent
)
504 parent
->GetPosition(&x_offset
,&y_offset
) ;
505 parent
->GetSize(&display_width
,&display_height
) ;
509 wxDisplaySize(&display_width
, &display_height
);
514 GetSize(&width
, &height
);
517 if (direction
& wxHORIZONTAL
)
518 x
= (int)((display_width
- width
)/2);
519 if (direction
& wxVERTICAL
)
520 y
= (int)((display_height
- height
)/2);
522 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
525 // Replacement for Show(TRUE) for modal dialogs - returns return code
526 int wxDialog::ShowModal()
528 m_windowStyle
|= wxDIALOG_MODAL
;
530 return GetReturnCode();
533 void wxDialog::EndModal(int retCode
)
535 SetReturnCode(retCode
);
539 // Define for each class of dialog and control
540 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
541 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
544 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
545 return (WXHBRUSH
) hbrush
;
552 void wxDialog::OnOK(wxCommandEvent
& event
)
554 if ( Validate() && TransferDataFromWindow() )
560 SetReturnCode(wxID_OK
);
566 void wxDialog::OnApply(wxCommandEvent
& event
)
569 TransferDataFromWindow();
570 // TODO probably need to disable the Apply button until things change again
573 void wxDialog::OnCancel(wxCommandEvent
& event
)
576 EndModal(wxID_CANCEL
);
579 SetReturnCode(wxID_CANCEL
);
584 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
586 // We'll send a Cancel message by default,
587 // which may close the dialog.
588 // Check for looping if the Cancel event handler calls Close().
590 // Note that if a cancel button and handler aren't present in the dialog,
591 // nothing will happen when you close the dialog via the window manager, or
593 // We wouldn't want to destroy the dialog by default, since the dialog may have been
594 // created on the stack.
595 // However, this does mean that calling dialog->Close() won't delete the dialog
596 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
597 // sure to destroy the dialog.
598 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
600 static wxList closing
;
602 if ( closing
.Member(this) )
605 closing
.Append(this);
607 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
608 cancelEvent
.SetEventObject( this );
609 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
611 closing
.DeleteObject(this);
614 // Destroy the window (delayed, if a managed window)
615 bool wxDialog::Destroy()
617 if (!wxPendingDelete
.Member(this))
618 wxPendingDelete
.Append(this);
622 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
624 // if we're using constraints - do use them
625 #if wxUSE_CONSTRAINTS
626 if ( GetAutoLayout() )
633 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
638 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
643 // ---------------------------------------------------------------------------
644 // dialog window proc
645 // ---------------------------------------------------------------------------
647 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
650 bool processed
= FALSE
;
655 // if we can't close, tell the system that we processed the
656 // message - otherwise it would close us
657 processed
= !Close();
662 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);