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 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
51 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
52 EVT_SIZE(wxDialog::OnSize
)
53 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
54 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
55 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
56 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
57 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
58 EVT_CLOSE(wxDialog::OnCloseWindow
)
64 m_modalShowing
= FALSE
;
66 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
69 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
70 const wxString
& title
,
80 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
84 wxTopLevelWindows
.Append(this);
86 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
88 if (parent
) parent
->AddChild(this);
91 m_windowId
= (int)NewControlId();
100 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
101 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
103 m_windowStyle
= style
;
106 m_modalShowing
= FALSE
;
113 // All dialogs should really have this style
114 m_windowStyle
|= wxTAB_TRAVERSAL
;
116 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
117 if (m_windowStyle
& wxSTAY_ON_TOP
)
118 extendedStyle
|= WS_EX_TOPMOST
;
120 // Allows creation of dialogs with & without captions under MSWindows,
121 // resizeable or not (but a resizeable dialog always has caption -
122 // otherwise it would look too strange)
124 if ( style
& wxRESIZE_BORDER
)
125 dlg
= wxT("wxResizeableDialog");
126 else if ( style
& wxCAPTION
)
127 dlg
= wxT("wxCaptionDialog");
129 dlg
= wxT("wxNoCaptionDialog");
130 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
132 0, // style is not used if we have dlg template
136 HWND hwnd
= (HWND
)GetHWND();
140 wxLogError(_("Failed to create dialog."));
145 SubclassWin(GetHWND());
147 SetWindowText(hwnd
, title
);
148 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
153 void wxDialog::SetModal(bool flag
)
156 m_windowStyle
|= wxDIALOG_MODAL
;
158 if ( m_windowStyle
& wxDIALOG_MODAL
)
159 m_windowStyle
-= wxDIALOG_MODAL
;
161 wxModelessWindows
.DeleteObject(this);
163 wxModelessWindows
.Append(this);
166 wxDialog::~wxDialog()
168 m_isBeingDeleted
= TRUE
;
170 wxTopLevelWindows
.DeleteObject(this);
176 // For some reason, wxWindows can activate another task altogether
177 // when a frame is destroyed after a modal dialog has been invoked.
178 // Try to bring the parent to the top.
179 // dfgg: I moved this following line from end of function -
180 // must not call if another window is on top!!
181 // This can often happen with Close() and delayed deleting
182 if (GetParent() && GetParent()->GetHWND())
183 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
186 m_modalShowing
= FALSE
;
188 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
189 wxModelessWindows
.DeleteObject(this);
191 // If this is the last top-level window, exit.
192 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
194 wxTheApp
->SetTopWindow(NULL
);
196 if (wxTheApp
->GetExitOnFrameDelete())
203 // By default, pressing escape cancels the dialog
204 void wxDialog::OnCharHook(wxKeyEvent
& event
)
208 if (event
.m_keyCode
== WXK_ESCAPE
)
210 // Behaviour changed in 2.0: we'll send a Cancel message
211 // to the dialog instead of Close.
212 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
213 cancelEvent
.SetEventObject( this );
214 GetEventHandler()->ProcessEvent(cancelEvent
);
219 // We didn't process this event.
223 void wxDialog::OnPaint(wxPaintEvent
& event
)
225 // No: if you call the default procedure, it makes
226 // the following painting code not work.
227 // wxWindow::OnPaint(event);
235 void wxDialog::Iconize(bool WXUNUSED(iconize
))
237 // Windows dialog boxes can't be iconized
240 bool wxDialog::IsIconized() const
245 void wxDialog::DoSetClientSize(int width
, int height
)
247 HWND hWnd
= (HWND
) GetHWND();
249 ::GetClientRect(hWnd
, &rect
);
252 GetWindowRect(hWnd
, &rect2
);
254 // Find the difference between the entire window (title bar and all)
255 // and the client area; add this to the new client size to move the
257 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
258 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
260 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
262 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
263 event
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event
);
267 void wxDialog::GetPosition(int *x
, int *y
) const
269 HWND hWnd
= (HWND
) GetHWND();
271 GetWindowRect(hWnd
, &rect
);
277 bool wxDialog::IsShown() const
282 bool wxDialog::IsModal() const
284 return wxModalDialogs
.Find((wxDialog
*)this) != 0; // const_cast
287 bool wxDialog::Show(bool show
)
294 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
296 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
299 if (!wxModelessWindows
.Find(this))
300 wxModelessWindows
.Append(this);
302 wxModelessWindows
.DeleteObject(this);
305 if (!wxTopLevelWindows
.Find(this))
306 wxTopLevelWindows
.Append(this);
308 wxTopLevelWindows
.DeleteObject(this);
315 // find the top level window which had focus before - we will restore
318 for ( HWND hwnd
= ::GetFocus(); hwnd
; hwnd
= ::GetParent(hwnd
) )
320 m_hwndOldFocus
= (WXHWND
)hwnd
;
325 BringWindowToTop((HWND
) GetHWND());
329 m_modalShowing
= TRUE
;
330 wxNode
*node
= wxModalDialogs
.First();
333 wxDialog
*box
= (wxDialog
*)node
->Data();
335 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
339 // if we don't do it, some window might be deleted while we have pointers
340 // to them in our disabledWindows list and the program will crash when it
341 // will try to reenable them after the modal dialog end
342 wxTheApp
->DeletePendingObjects();
343 wxList disabledWindows
;
345 node
= wxModelessWindows
.First();
348 wxWindow
*win
= (wxWindow
*)node
->Data();
349 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
351 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
352 disabledWindows
.Append(win
);
357 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
358 EnableWindow((HWND
) GetHWND(), TRUE
);
359 BringWindowToTop((HWND
) GetHWND());
361 if ( !wxModalDialogs
.Find(this) )
362 wxModalDialogs
.Append(this);
365 // Must test whether this dialog still exists: we may not process
366 // a message before the deletion.
367 while (wxModalDialogs
.Find(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
369 if ( m_acceleratorTable
.Ok() &&
370 ::TranslateAccelerator((HWND
)GetHWND(),
371 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
374 // Have processed the message
376 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&msg
) )
378 TranslateMessage(&msg
);
379 DispatchMessage(&msg
);
382 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
383 // we should try removing the m_modalShowing test
385 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
386 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
387 // a Show(FALSE) in the mean time!!!
388 // Without the test, we might delete the dialog before the end of modal showing.
390 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
392 // Keep going until we decide we've done enough
396 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
397 node
=disabledWindows
.First();
399 wxWindow
* win
= (wxWindow
*) node
->Data();
400 if (wxModalDialogs
.Find(win
) || wxModelessWindows
.Find(win
))
402 HWND hWnd
= (HWND
) win
->GetHWND();
403 if (::IsWindow(hWnd
))
404 ::EnableWindow(hWnd
,TRUE
);
411 ::SetFocus((HWND
)m_hwndOldFocus
);
413 wxModalDialogs
.DeleteObject(this);
415 wxNode
*last
= wxModalDialogs
.Last();
417 // If there's still a modal dialog active, we
418 // enable it, else we enable all modeless windows
421 // VZ: I don't understand what this is supposed to do, so I'll leave
422 // it out for now and look for horrible consequences
423 wxDialog
*box
= (wxDialog
*)last
->Data();
424 HWND hwnd
= (HWND
) box
->GetHWND();
426 if (box
->IsUserEnabled())
428 EnableWindow(hwnd
, TRUE
);
429 BringWindowToTop(hwnd
);
433 wxNode
*node
= wxModelessWindows
.First();
436 wxWindow
*win
= (wxWindow
*)node
->Data();
437 HWND hwnd
= (HWND
) win
->GetHWND();
438 // Only enable again if not user-disabled.
440 if (win
->IsUserEnabled())
442 EnableWindow(hwnd
, TRUE
);
446 // Try to highlight the correct window (the parent)
450 hWndParent
= (HWND
) GetParent()->GetHWND();
452 ::BringWindowToTop(hWndParent
);
454 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
455 m_modalShowing
= FALSE
;
462 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
463 BringWindowToTop((HWND
) GetHWND());
467 // Try to highlight the correct window (the parent)
471 hWndParent
= (HWND
) GetParent()->GetHWND();
473 ::BringWindowToTop(hWndParent
);
477 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
483 void wxDialog::SetTitle(const wxString
& title
)
485 SetWindowText((HWND
) GetHWND(), title
.c_str());
488 wxString
wxDialog::GetTitle() const
490 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
491 return wxString(wxBuffer
);
494 void wxDialog::Centre(int direction
)
496 int x_offset
,y_offset
;
497 int display_width
, display_height
;
498 int width
, height
, x
, y
;
499 wxWindow
*parent
= GetParent();
500 if ((direction
& wxCENTER_FRAME
) && parent
)
502 parent
->GetPosition(&x_offset
,&y_offset
) ;
503 parent
->GetSize(&display_width
,&display_height
) ;
507 wxDisplaySize(&display_width
, &display_height
);
512 GetSize(&width
, &height
);
515 if (direction
& wxHORIZONTAL
)
516 x
= (int)((display_width
- width
)/2);
517 if (direction
& wxVERTICAL
)
518 y
= (int)((display_height
- height
)/2);
520 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
523 // Replacement for Show(TRUE) for modal dialogs - returns return code
524 int wxDialog::ShowModal()
526 m_windowStyle
|= wxDIALOG_MODAL
;
528 return GetReturnCode();
531 void wxDialog::EndModal(int retCode
)
533 SetReturnCode(retCode
);
537 // Define for each class of dialog and control
538 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
539 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
542 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
543 return (WXHBRUSH
) hbrush
;
550 void wxDialog::OnOK(wxCommandEvent
& event
)
552 if ( Validate() && TransferDataFromWindow() )
558 SetReturnCode(wxID_OK
);
564 void wxDialog::OnApply(wxCommandEvent
& event
)
567 TransferDataFromWindow();
568 // TODO probably need to disable the Apply button until things change again
571 void wxDialog::OnCancel(wxCommandEvent
& event
)
574 EndModal(wxID_CANCEL
);
577 SetReturnCode(wxID_CANCEL
);
582 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
584 // We'll send a Cancel message by default,
585 // which may close the dialog.
586 // Check for looping if the Cancel event handler calls Close().
588 // Note that if a cancel button and handler aren't present in the dialog,
589 // nothing will happen when you close the dialog via the window manager, or
591 // We wouldn't want to destroy the dialog by default, since the dialog may have been
592 // created on the stack.
593 // However, this does mean that calling dialog->Close() won't delete the dialog
594 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
595 // sure to destroy the dialog.
596 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
598 static wxList closing
;
600 if ( closing
.Member(this) )
603 closing
.Append(this);
605 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
606 cancelEvent
.SetEventObject( this );
607 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
609 closing
.DeleteObject(this);
612 // Destroy the window (delayed, if a managed window)
613 bool wxDialog::Destroy()
615 if (!wxPendingDelete
.Member(this))
616 wxPendingDelete
.Append(this);
620 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
622 // if we're using constraints - do use them
623 #if wxUSE_CONSTRAINTS
624 if ( GetAutoLayout() )
631 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
636 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
641 // ---------------------------------------------------------------------------
642 // dialog window proc
643 // ---------------------------------------------------------------------------
645 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
648 bool processed
= FALSE
;
653 // if we can't close, tell the system that we processed the
654 // message - otherwise it would close us
655 processed
= !Close();
660 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);