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 WXDLLEXPORT 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
)
60 bool wxDialog::MSWProcessMessage(WXMSG
* pMsg
)
62 return (::IsDialogMessage((HWND
) GetHWND(), (MSG
*)pMsg
) != 0);
65 bool wxDialog::MSWOnClose(void)
70 wxDialog::wxDialog(void)
73 m_modalShowing
= FALSE
;
75 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
78 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
79 const wxString
& title
,
85 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
89 wxTopLevelWindows
.Append(this);
91 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
93 if (parent
) parent
->AddChild(this);
96 m_windowId
= (int)NewControlId();
105 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
106 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
108 m_windowStyle
= style
;
111 m_modalShowing
= FALSE
;
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
& wxTHICK_FRAME
)
127 dlg
= "wxResizeableDialog";
128 else if ( style
& wxCAPTION
)
129 dlg
= "wxCaptionDialog";
131 dlg
= "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 created 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);
177 // For some reason, wxWindows can activate another task altogether
178 // when a frame is destroyed after a modal dialog has been invoked.
179 // Try to bring the parent to the top.
180 // dfgg: I moved this following line from end of function -
181 // must not call if another window is on top!!
182 // This can often happen with Close() and delayed deleting
183 if (GetParent() && GetParent()->GetHWND())
184 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
187 m_modalShowing
= FALSE
;
189 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
191 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
192 wxModelessWindows
.DeleteObject(this);
196 // If this is the last top-level window, exit.
197 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
199 wxTheApp
->SetTopWindow(NULL
);
201 if (wxTheApp
->GetExitOnFrameDelete())
208 // By default, pressing escape cancels the dialog
209 void wxDialog::OnCharHook(wxKeyEvent
& event
)
213 if (event
.m_keyCode
== WXK_ESCAPE
)
215 // Behaviour changed in 2.0: we'll send a Cancel message
216 // to the dialog instead of Close.
217 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
218 cancelEvent
.SetEventObject( this );
219 GetEventHandler()->ProcessEvent(cancelEvent
);
224 // We didn't process this event.
228 void wxDialog::OnPaint(wxPaintEvent
& event
)
230 // No: if you call the default procedure, it makes
231 // the following painting code not work.
232 // wxWindow::OnPaint(event);
235 void wxDialog::Fit(void)
240 void wxDialog::Iconize(bool WXUNUSED(iconize
))
242 // Windows dialog boxes can't be iconized
245 bool wxDialog::IsIconized(void) const
250 void wxDialog::SetClientSize(int width
, int height
)
252 HWND hWnd
= (HWND
) GetHWND();
254 GetClientRect(hWnd
, &rect
);
257 GetWindowRect(hWnd
, &rect2
);
259 // Find the difference between the entire window (title bar and all)
260 // and the client area; add this to the new client size to move the
262 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
263 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
265 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
267 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
268 event
.SetEventObject( this );
269 GetEventHandler()->ProcessEvent(event
);
272 void wxDialog::GetPosition(int *x
, int *y
) const
274 HWND hWnd
= (HWND
) GetHWND();
276 GetWindowRect(hWnd
, &rect
);
282 bool wxDialog::IsShown(void) const
287 bool wxDialog::Show(bool show
)
294 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
296 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
299 if (!wxModelessWindows
.Member(this))
300 wxModelessWindows
.Append(this);
302 wxModelessWindows
.DeleteObject(this);
305 if (!wxTopLevelWindows
.Member(this))
306 wxTopLevelWindows
.Append(this);
308 wxTopLevelWindows
.DeleteObject(this);
315 m_hwndOldFocus
= (WXHWND
)::GetFocus();
319 BringWindowToTop((HWND
) GetHWND());
323 m_modalShowing
= TRUE
;
324 wxNode
*node
= wxModalDialogs
.First();
327 wxDialog
*box
= (wxDialog
*)node
->Data();
329 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
333 // if we don't do it, some window might be deleted while we have pointers
334 // to them in our disabledWindows list and the program will crash when it
335 // will try to reenable them after the modal dialog end
336 wxTheApp
->DeletePendingObjects();
337 wxList disabledWindows
;
339 node
= wxModelessWindows
.First();
342 wxWindow
*win
= (wxWindow
*)node
->Data();
343 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
345 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
346 disabledWindows
.Append(win
);
351 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
352 EnableWindow((HWND
) GetHWND(), TRUE
);
353 BringWindowToTop((HWND
) GetHWND());
355 if (!wxModalDialogs
.Member(this))
356 wxModalDialogs
.Append(this);
359 // Must test whether this dialog still exists: we may not process
360 // a message before the deletion.
361 while (wxModalDialogs
.Member(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
363 if (m_acceleratorTable
.Ok() &&
364 ::TranslateAccelerator((HWND
) GetHWND(), (HACCEL
) m_acceleratorTable
.GetHACCEL(), &msg
))
366 // Have processed the message
368 else if (!IsDialogMessage((HWND
) GetHWND(), &msg
))
370 TranslateMessage(&msg
);
371 DispatchMessage(&msg
);
374 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
375 // we should try removing the m_modalShowing test
377 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
378 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
379 // a Show(FALSE) in the mean time!!!
380 // Without the test, we might delete the dialog before the end of modal showing.
382 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
384 // Keep going until we decide we've done enough
388 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
389 node
=disabledWindows
.First();
391 wxWindow
* win
= (wxWindow
*) node
->Data();
392 if (wxModalDialogs
.Member(win
) || wxModelessWindows
.Member(win
))
394 HWND hWnd
= (HWND
) win
->GetHWND();
395 if (::IsWindow(hWnd
))
396 ::EnableWindow(hWnd
,TRUE
);
403 ::SetFocus((HWND
)m_hwndOldFocus
);
405 wxModalDialogs
.DeleteObject(this);
407 wxNode
*last
= wxModalDialogs
.Last();
409 // If there's still a modal dialog active, we
410 // enable it, else we enable all modeless windows
413 wxDialog
*box
= (wxDialog
*)last
->Data();
414 HWND hwnd
= (HWND
) box
->GetHWND();
415 if (box
->m_winEnabled
)
416 EnableWindow(hwnd
, TRUE
);
417 BringWindowToTop(hwnd
);
421 wxNode
*node
= wxModelessWindows
.First();
424 wxWindow
*win
= (wxWindow
*)node
->Data();
425 HWND hwnd
= (HWND
) win
->GetHWND();
426 // Only enable again if not user-disabled.
427 if (win
->IsUserEnabled())
428 EnableWindow(hwnd
, TRUE
);
432 // Try to highlight the correct window (the parent)
436 hWndParent
= (HWND
) GetParent()->GetHWND();
438 ::BringWindowToTop(hWndParent
);
440 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
441 m_modalShowing
= FALSE
;
448 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
449 BringWindowToTop((HWND
) GetHWND());
453 // Try to highlight the correct window (the parent)
457 hWndParent
= (HWND
) GetParent()->GetHWND();
459 ::BringWindowToTop(hWndParent
);
461 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
467 void wxDialog::SetTitle(const wxString
& title
)
469 SetWindowText((HWND
) GetHWND(), (const char *)title
);
472 wxString
wxDialog::GetTitle(void) const
474 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
475 return wxString(wxBuffer
);
478 void wxDialog::Centre(int direction
)
480 int x_offset
,y_offset
;
481 int display_width
, display_height
;
482 int width
, height
, x
, y
;
483 wxWindow
*parent
= GetParent();
484 if ((direction
& wxCENTER_FRAME
) && parent
)
486 parent
->GetPosition(&x_offset
,&y_offset
) ;
487 parent
->GetSize(&display_width
,&display_height
) ;
491 wxDisplaySize(&display_width
, &display_height
);
496 GetSize(&width
, &height
);
499 if (direction
& wxHORIZONTAL
)
500 x
= (int)((display_width
- width
)/2);
501 if (direction
& wxVERTICAL
)
502 y
= (int)((display_height
- height
)/2);
504 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
507 // Replacement for Show(TRUE) for modal dialogs - returns return code
508 int wxDialog::ShowModal(void)
510 m_windowStyle
|= wxDIALOG_MODAL
;
512 return GetReturnCode();
515 void wxDialog::EndModal(int retCode
)
517 SetReturnCode(retCode
);
521 // Define for each class of dialog and control
522 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
523 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
526 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
527 return (WXHBRUSH
) hbrush
;
534 void wxDialog::OnOK(wxCommandEvent
& event
)
536 if ( Validate() && TransferDataFromWindow() )
542 SetReturnCode(wxID_OK
);
548 void wxDialog::OnApply(wxCommandEvent
& event
)
551 TransferDataFromWindow();
552 // TODO probably need to disable the Apply button until things change again
555 void wxDialog::OnCancel(wxCommandEvent
& event
)
558 EndModal(wxID_CANCEL
);
561 SetReturnCode(wxID_CANCEL
);
566 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
568 // We'll send a Cancel message by default,
569 // which may close the dialog.
570 // Check for looping if the Cancel event handler calls Close().
572 // Note that if a cancel button and handler aren't present in the dialog,
573 // nothing will happen when you close the dialog via the window manager, or
575 // We wouldn't want to destroy the dialog by default, since the dialog may have been
576 // created on the stack.
577 // However, this does mean that calling dialog->Close() won't delete the dialog
578 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
579 // sure to destroy the dialog.
580 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
582 static wxList closing
;
584 if ( closing
.Member(this) )
587 closing
.Append(this);
589 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
590 cancelEvent
.SetEventObject( this );
591 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
593 closing
.DeleteObject(this);
596 // Destroy the window (delayed, if a managed window)
597 bool wxDialog::Destroy(void)
599 if (!wxPendingDelete
.Member(this))
600 wxPendingDelete
.Append(this);
604 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
606 // if we're using constraints - do use them
607 #if wxUSE_CONSTRAINTS
608 if ( GetAutoLayout() ) {
614 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
619 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));