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"
31 #include "wx/msw/private.h"
33 #if wxUSE_COMMON_DIALOGS
37 #define wxDIALOG_DEFAULT_X 300
38 #define wxDIALOG_DEFAULT_Y 300
40 // Lists to keep track of windows, so we can disable/enable them
42 wxList wxModalDialogs
;
43 wxList wxModelessWindows
; // Frames and modeless dialogs
44 extern wxList wxPendingDelete
;
46 #if !USE_SHARED_LIBRARY
47 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
49 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
50 EVT_SIZE(wxDialog::OnSize
)
51 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
52 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
53 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
54 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
55 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
56 EVT_CLOSE(wxDialog::OnCloseWindow
)
61 long wxDialog::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
63 return ::CallWindowProc(CASTWNDPROC m_oldWndProc
, (HWND
) GetHWND(), (UINT
) nMsg
, (WPARAM
) wParam
, (LPARAM
) lParam
);
66 bool wxDialog::MSWProcessMessage(WXMSG
* pMsg
)
68 return (::IsDialogMessage((HWND
) GetHWND(), (MSG
*)pMsg
) != 0);
71 bool wxDialog::MSWOnClose(void)
76 wxDialog::wxDialog(void)
79 m_modalShowing
= FALSE
;
81 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
84 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
85 const wxString
& title
,
91 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
95 wxTopLevelWindows
.Append(this);
97 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
99 if (parent
) parent
->AddChild(this);
102 m_windowId
= (int)NewControlId();
111 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
112 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
114 m_windowStyle
= style
;
117 m_modalShowing
= FALSE
;
124 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
125 if (m_windowStyle
& wxSTAY_ON_TOP
)
126 extendedStyle
|= WS_EX_TOPMOST
;
128 // Allows creation of dialogs with & without captions under MSWindows
129 if(style
& wxCAPTION
){
130 MSWCreate(m_windowId
, (wxWindow
*)parent
, NULL
, this, NULL
, x
, y
, width
, height
, 0, "wxCaptionDialog",
134 MSWCreate(m_windowId
, (wxWindow
*)parent
, NULL
, this, NULL
, x
, y
, width
, height
, 0, "wxNoCaptionDialog",
138 SubclassWin(GetHWND());
140 SetWindowText((HWND
) GetHWND(), (const char *)title
);
141 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
146 void wxDialog::SetModal(bool flag
)
149 m_windowStyle
|= wxDIALOG_MODAL
;
151 if ( m_windowStyle
& wxDIALOG_MODAL
)
152 m_windowStyle
-= wxDIALOG_MODAL
;
154 wxModelessWindows
.DeleteObject(this);
156 wxModelessWindows
.Append(this);
159 wxDialog::~wxDialog()
161 m_isBeingDeleted
= TRUE
;
163 wxTopLevelWindows
.DeleteObject(this);
168 // For some reason, wxWindows can activate another task altogether
169 // when a frame is destroyed after a modal dialog has been invoked.
170 // Try to bring the parent to the top.
171 // dfgg: I moved this following line from end of function -
172 // must not call if another window is on top!!
173 // This can often happen with Close() and delayed deleting
174 if (GetParent() && GetParent()->GetHWND())
175 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
178 m_modalShowing
= FALSE
;
180 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
182 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
183 wxModelessWindows
.DeleteObject(this);
187 // If this is the last top-level window, exit.
188 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
190 wxTheApp
->SetTopWindow(NULL
);
192 if (wxTheApp
->GetExitOnFrameDelete())
199 // By default, pressing escape cancels the dialog
200 void wxDialog::OnCharHook(wxKeyEvent
& event
)
204 if (event
.m_keyCode
== WXK_ESCAPE
)
206 // Behaviour changed in 2.0: we'll send a Cancel message
207 // to the dialog instead of Close.
208 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
209 cancelEvent
.SetEventObject( this );
210 GetEventHandler()->ProcessEvent(cancelEvent
);
215 // We didn't process this event.
219 void wxDialog::OnPaint(wxPaintEvent
& event
)
221 // No: if you call the default procedure, it makes
222 // the following painting code not work.
223 // wxWindow::OnPaint(event);
226 void wxDialog::Fit(void)
231 void wxDialog::Iconize(bool WXUNUSED(iconize
))
233 // Windows dialog boxes can't be iconized
236 bool wxDialog::IsIconized(void) const
241 void wxDialog::SetClientSize(int width
, int height
)
243 HWND hWnd
= (HWND
) GetHWND();
245 GetClientRect(hWnd
, &rect
);
248 GetWindowRect(hWnd
, &rect2
);
250 // Find the difference between the entire window (title bar and all)
251 // and the client area; add this to the new client size to move the
253 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
254 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
256 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
258 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
259 event
.SetEventObject( this );
260 GetEventHandler()->ProcessEvent(event
);
263 void wxDialog::GetPosition(int *x
, int *y
) const
265 HWND hWnd
= (HWND
) GetHWND();
267 GetWindowRect(hWnd
, &rect
);
273 bool wxDialog::IsShown(void) const
278 bool wxDialog::Show(bool show
)
285 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
287 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
290 if (!wxModelessWindows
.Member(this))
291 wxModelessWindows
.Append(this);
293 wxModelessWindows
.DeleteObject(this);
296 if (!wxTopLevelWindows
.Member(this))
297 wxTopLevelWindows
.Append(this);
299 wxTopLevelWindows
.DeleteObject(this);
306 m_hwndOldFocus
= (WXHWND
)::GetFocus();
310 BringWindowToTop((HWND
) GetHWND());
314 m_modalShowing
= TRUE
;
315 wxNode
*node
= wxModalDialogs
.First();
318 wxDialog
*box
= (wxDialog
*)node
->Data();
320 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
324 // if we don't do it, some window might be deleted while we have pointers
325 // to them in our disabledWindows list and the program will crash when it
326 // will try to reenable them after the modal dialog end
327 wxTheApp
->DeletePendingObjects();
328 wxList disabledWindows
;
330 node
= wxModelessWindows
.First();
333 wxWindow
*win
= (wxWindow
*)node
->Data();
334 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
336 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
337 disabledWindows
.Append(win
);
342 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
343 EnableWindow((HWND
) GetHWND(), TRUE
);
344 BringWindowToTop((HWND
) GetHWND());
346 if (!wxModalDialogs
.Member(this))
347 wxModalDialogs
.Append(this);
350 // Must test whether this dialog still exists: we may not process
351 // a message before the deletion.
352 while (wxModalDialogs
.Member(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
354 if (m_acceleratorTable
.Ok() &&
355 ::TranslateAccelerator((HWND
) GetHWND(), (HACCEL
) m_acceleratorTable
.GetHACCEL(), &msg
))
357 // Have processed the message
359 else if (!IsDialogMessage((HWND
) GetHWND(), &msg
))
361 TranslateMessage(&msg
);
362 DispatchMessage(&msg
);
364 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
365 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
366 // a Show(FALSE) in the mean time!!!
367 // Without the test, we might delete the dialog before the end of modal showing.
369 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
371 // Keep going until we decide we've done enough
375 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
376 node
=disabledWindows
.First();
378 wxWindow
* win
= (wxWindow
*) node
->Data();
379 HWND hWnd
= (HWND
) win
->GetHWND();
380 if (::IsWindow(hWnd
) && (wxModalDialogs
.Member(win
) || wxModelessWindows
.Member(win
) ))
381 ::EnableWindow(hWnd
,TRUE
);
387 ::SetFocus((HWND
)m_hwndOldFocus
);
389 wxModalDialogs
.DeleteObject(this);
391 wxNode
*last
= wxModalDialogs
.Last();
393 // If there's still a modal dialog active, we
394 // enable it, else we enable all modeless windows
397 wxDialog
*box
= (wxDialog
*)last
->Data();
398 HWND hwnd
= (HWND
) box
->GetHWND();
399 if (box
->m_winEnabled
)
400 EnableWindow(hwnd
, TRUE
);
401 BringWindowToTop(hwnd
);
405 wxNode
*node
= wxModelessWindows
.First();
408 wxWindow
*win
= (wxWindow
*)node
->Data();
409 HWND hwnd
= (HWND
) win
->GetHWND();
410 // Only enable again if not user-disabled.
411 if (win
->IsUserEnabled())
412 EnableWindow(hwnd
, TRUE
);
416 // Try to highlight the correct window (the parent)
420 hWndParent
= (HWND
) GetParent()->GetHWND();
422 ::BringWindowToTop(hWndParent
);
424 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
425 m_modalShowing
= FALSE
;
432 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
433 BringWindowToTop((HWND
) GetHWND());
437 // Try to highlight the correct window (the parent)
441 hWndParent
= (HWND
) GetParent()->GetHWND();
443 ::BringWindowToTop(hWndParent
);
445 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
451 void wxDialog::SetTitle(const wxString
& title
)
453 SetWindowText((HWND
) GetHWND(), (const char *)title
);
456 wxString
wxDialog::GetTitle(void) const
458 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
459 return wxString(wxBuffer
);
462 void wxDialog::Centre(int direction
)
464 int x_offset
,y_offset
;
465 int display_width
, display_height
;
466 int width
, height
, x
, y
;
467 wxWindow
*parent
= GetParent();
468 if ((direction
& wxCENTER_FRAME
) && parent
)
470 parent
->GetPosition(&x_offset
,&y_offset
) ;
471 parent
->GetSize(&display_width
,&display_height
) ;
475 wxDisplaySize(&display_width
, &display_height
);
480 GetSize(&width
, &height
);
483 if (direction
& wxHORIZONTAL
)
484 x
= (int)((display_width
- width
)/2);
485 if (direction
& wxVERTICAL
)
486 y
= (int)((display_height
- height
)/2);
488 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
491 // Replacement for Show(TRUE) for modal dialogs - returns return code
492 int wxDialog::ShowModal(void)
494 m_windowStyle
|= wxDIALOG_MODAL
;
496 return GetReturnCode();
499 void wxDialog::EndModal(int retCode
)
501 SetReturnCode(retCode
);
505 // Define for each class of dialog and control
506 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
507 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
510 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
511 return (WXHBRUSH
) hbrush
;
518 void wxDialog::OnOK(wxCommandEvent
& event
)
520 if ( Validate() && TransferDataFromWindow() )
526 SetReturnCode(wxID_OK
);
532 void wxDialog::OnApply(wxCommandEvent
& event
)
535 TransferDataFromWindow();
536 // TODO probably need to disable the Apply button until things change again
539 void wxDialog::OnCancel(wxCommandEvent
& event
)
542 EndModal(wxID_CANCEL
);
545 SetReturnCode(wxID_CANCEL
);
550 bool wxDialog::OnClose(void)
552 // Behaviour changed in 2.0: we'll send a Cancel message by default,
553 // which may close the dialog.
554 // Check for looping if the Cancel event handler calls Close()
556 static wxList closing
;
558 if ( closing
.Member(this) )
561 closing
.Append(this);
563 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
564 cancelEvent
.SetEventObject( this );
565 GetEventHandler()->ProcessEvent(cancelEvent
);
567 closing
.DeleteObject(this);
572 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
574 // if we're using constraints - do use them
575 #if wxUSE_CONSTRAINTS
576 if ( GetAutoLayout() ) {
582 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
585 if ( GetEventHandler()->OnClose() || event
.GetForce())
593 // Destroy the window (delayed, if a managed window)
594 bool wxDialog::Destroy(void)
596 if (!wxPendingDelete
.Member(this))
597 wxPendingDelete
.Append(this);
601 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
606 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
611 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
613 return wxWindow::MSWWindowProc(message
, wParam
, lParam
);