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"
34 #if wxUSE_COMMON_DIALOGS
38 #define wxDIALOG_DEFAULT_X 300
39 #define wxDIALOG_DEFAULT_Y 300
41 // Lists to keep track of windows, so we can disable/enable them
43 wxList wxModalDialogs
;
44 wxList wxModelessWindows
; // Frames and modeless dialogs
45 extern wxList WXDLLEXPORT wxPendingDelete
;
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
50 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
51 EVT_SIZE(wxDialog::OnSize
)
52 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
53 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
54 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
55 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
56 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
57 EVT_CLOSE(wxDialog::OnCloseWindow
)
61 bool wxDialog::MSWOnClose(void)
66 wxDialog::wxDialog(void)
69 m_modalShowing
= FALSE
;
71 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
74 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
75 const wxString
& title
,
81 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
85 wxTopLevelWindows
.Append(this);
87 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
89 if (parent
) parent
->AddChild(this);
92 m_windowId
= (int)NewControlId();
101 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
102 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
104 m_windowStyle
= style
;
107 m_modalShowing
= FALSE
;
114 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
115 if (m_windowStyle
& wxSTAY_ON_TOP
)
116 extendedStyle
|= WS_EX_TOPMOST
;
118 // Allows creation of dialogs with & without captions under MSWindows,
119 // resizeable or not (but a resizeable dialog always has caption -
120 // otherwise it would look too strange)
122 if ( style
& wxTHICK_FRAME
)
123 dlg
= "wxResizeableDialog";
124 else if ( style
& wxCAPTION
)
125 dlg
= "wxCaptionDialog";
127 dlg
= "wxNoCaptionDialog";
128 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
130 0, // style is not used if we have dlg template
134 HWND hwnd
= (HWND
)GetHWND();
138 wxLogError(_("Failed to created dialog."));
143 SubclassWin(GetHWND());
145 SetWindowText(hwnd
, title
);
146 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
151 void wxDialog::SetModal(bool flag
)
154 m_windowStyle
|= wxDIALOG_MODAL
;
156 if ( m_windowStyle
& wxDIALOG_MODAL
)
157 m_windowStyle
-= wxDIALOG_MODAL
;
159 wxModelessWindows
.DeleteObject(this);
161 wxModelessWindows
.Append(this);
164 wxDialog::~wxDialog()
166 m_isBeingDeleted
= TRUE
;
168 wxTopLevelWindows
.DeleteObject(this);
173 // For some reason, wxWindows can activate another task altogether
174 // when a frame is destroyed after a modal dialog has been invoked.
175 // Try to bring the parent to the top.
176 // dfgg: I moved this following line from end of function -
177 // must not call if another window is on top!!
178 // This can often happen with Close() and delayed deleting
179 if (GetParent() && GetParent()->GetHWND())
180 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
183 m_modalShowing
= FALSE
;
185 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
187 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
188 wxModelessWindows
.DeleteObject(this);
192 // If this is the last top-level window, exit.
193 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
195 wxTheApp
->SetTopWindow(NULL
);
197 if (wxTheApp
->GetExitOnFrameDelete())
204 // By default, pressing escape cancels the dialog
205 void wxDialog::OnCharHook(wxKeyEvent
& event
)
209 if (event
.m_keyCode
== WXK_ESCAPE
)
211 // Behaviour changed in 2.0: we'll send a Cancel message
212 // to the dialog instead of Close.
213 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
214 cancelEvent
.SetEventObject( this );
215 GetEventHandler()->ProcessEvent(cancelEvent
);
220 // We didn't process this event.
224 void wxDialog::OnPaint(wxPaintEvent
& event
)
226 // No: if you call the default procedure, it makes
227 // the following painting code not work.
228 // wxWindow::OnPaint(event);
231 void wxDialog::Fit(void)
236 void wxDialog::Iconize(bool WXUNUSED(iconize
))
238 // Windows dialog boxes can't be iconized
241 bool wxDialog::IsIconized(void) const
246 void wxDialog::DoSetClientSize(int width
, int height
)
248 HWND hWnd
= (HWND
) GetHWND();
250 ::GetClientRect(hWnd
, &rect
);
253 GetWindowRect(hWnd
, &rect2
);
255 // Find the difference between the entire window (title bar and all)
256 // and the client area; add this to the new client size to move the
258 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
259 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
261 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
263 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
264 event
.SetEventObject( this );
265 GetEventHandler()->ProcessEvent(event
);
268 void wxDialog::GetPosition(int *x
, int *y
) const
270 HWND hWnd
= (HWND
) GetHWND();
272 GetWindowRect(hWnd
, &rect
);
278 bool wxDialog::IsShown(void) const
283 bool wxDialog::Show(bool show
)
290 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
292 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
295 if (!wxModelessWindows
.Member(this))
296 wxModelessWindows
.Append(this);
298 wxModelessWindows
.DeleteObject(this);
301 if (!wxTopLevelWindows
.Member(this))
302 wxTopLevelWindows
.Append(this);
304 wxTopLevelWindows
.DeleteObject(this);
311 m_hwndOldFocus
= (WXHWND
)::GetFocus();
315 BringWindowToTop((HWND
) GetHWND());
319 m_modalShowing
= TRUE
;
320 wxNode
*node
= wxModalDialogs
.First();
323 wxDialog
*box
= (wxDialog
*)node
->Data();
325 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
329 // if we don't do it, some window might be deleted while we have pointers
330 // to them in our disabledWindows list and the program will crash when it
331 // will try to reenable them after the modal dialog end
332 wxTheApp
->DeletePendingObjects();
333 wxList disabledWindows
;
335 node
= wxModelessWindows
.First();
338 wxWindow
*win
= (wxWindow
*)node
->Data();
339 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
341 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
342 disabledWindows
.Append(win
);
347 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
348 EnableWindow((HWND
) GetHWND(), TRUE
);
349 BringWindowToTop((HWND
) GetHWND());
351 if (!wxModalDialogs
.Member(this))
352 wxModalDialogs
.Append(this);
355 // Must test whether this dialog still exists: we may not process
356 // a message before the deletion.
357 while (wxModalDialogs
.Member(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
359 if ( m_acceleratorTable
.Ok() &&
360 ::TranslateAccelerator((HWND
)GetHWND(),
361 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
364 // Have processed the message
366 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&msg
) )
368 TranslateMessage(&msg
);
369 DispatchMessage(&msg
);
372 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
373 // we should try removing the m_modalShowing test
375 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
376 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
377 // a Show(FALSE) in the mean time!!!
378 // Without the test, we might delete the dialog before the end of modal showing.
380 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
382 // Keep going until we decide we've done enough
386 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
387 node
=disabledWindows
.First();
389 wxWindow
* win
= (wxWindow
*) node
->Data();
390 if (wxModalDialogs
.Member(win
) || wxModelessWindows
.Member(win
))
392 HWND hWnd
= (HWND
) win
->GetHWND();
393 if (::IsWindow(hWnd
))
394 ::EnableWindow(hWnd
,TRUE
);
401 ::SetFocus((HWND
)m_hwndOldFocus
);
403 wxModalDialogs
.DeleteObject(this);
405 wxNode
*last
= wxModalDialogs
.Last();
407 // If there's still a modal dialog active, we
408 // enable it, else we enable all modeless windows
411 wxDialog
*box
= (wxDialog
*)last
->Data();
412 HWND hwnd
= (HWND
) box
->GetHWND();
413 if (box
->m_winEnabled
)
414 EnableWindow(hwnd
, TRUE
);
415 BringWindowToTop(hwnd
);
419 wxNode
*node
= wxModelessWindows
.First();
422 wxWindow
*win
= (wxWindow
*)node
->Data();
423 HWND hwnd
= (HWND
) win
->GetHWND();
424 // Only enable again if not user-disabled.
425 if (win
->IsUserEnabled())
426 EnableWindow(hwnd
, TRUE
);
430 // Try to highlight the correct window (the parent)
434 hWndParent
= (HWND
) GetParent()->GetHWND();
436 ::BringWindowToTop(hWndParent
);
438 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
439 m_modalShowing
= FALSE
;
446 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
447 BringWindowToTop((HWND
) GetHWND());
451 // Try to highlight the correct window (the parent)
455 hWndParent
= (HWND
) GetParent()->GetHWND();
457 ::BringWindowToTop(hWndParent
);
459 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
465 void wxDialog::SetTitle(const wxString
& title
)
467 SetWindowText((HWND
) GetHWND(), (const char *)title
);
470 wxString
wxDialog::GetTitle(void) const
472 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
473 return wxString(wxBuffer
);
476 void wxDialog::Centre(int direction
)
478 int x_offset
,y_offset
;
479 int display_width
, display_height
;
480 int width
, height
, x
, y
;
481 wxWindow
*parent
= GetParent();
482 if ((direction
& wxCENTER_FRAME
) && parent
)
484 parent
->GetPosition(&x_offset
,&y_offset
) ;
485 parent
->GetSize(&display_width
,&display_height
) ;
489 wxDisplaySize(&display_width
, &display_height
);
494 GetSize(&width
, &height
);
497 if (direction
& wxHORIZONTAL
)
498 x
= (int)((display_width
- width
)/2);
499 if (direction
& wxVERTICAL
)
500 y
= (int)((display_height
- height
)/2);
502 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
505 // Replacement for Show(TRUE) for modal dialogs - returns return code
506 int wxDialog::ShowModal(void)
508 m_windowStyle
|= wxDIALOG_MODAL
;
510 return GetReturnCode();
513 void wxDialog::EndModal(int retCode
)
515 SetReturnCode(retCode
);
519 // Define for each class of dialog and control
520 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
521 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
524 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
525 return (WXHBRUSH
) hbrush
;
532 void wxDialog::OnOK(wxCommandEvent
& event
)
534 if ( Validate() && TransferDataFromWindow() )
540 SetReturnCode(wxID_OK
);
546 void wxDialog::OnApply(wxCommandEvent
& event
)
549 TransferDataFromWindow();
550 // TODO probably need to disable the Apply button until things change again
553 void wxDialog::OnCancel(wxCommandEvent
& event
)
556 EndModal(wxID_CANCEL
);
559 SetReturnCode(wxID_CANCEL
);
564 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
566 // We'll send a Cancel message by default,
567 // which may close the dialog.
568 // Check for looping if the Cancel event handler calls Close().
570 // Note that if a cancel button and handler aren't present in the dialog,
571 // nothing will happen when you close the dialog via the window manager, or
573 // We wouldn't want to destroy the dialog by default, since the dialog may have been
574 // created on the stack.
575 // However, this does mean that calling dialog->Close() won't delete the dialog
576 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
577 // sure to destroy the dialog.
578 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
580 static wxList closing
;
582 if ( closing
.Member(this) )
585 closing
.Append(this);
587 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
588 cancelEvent
.SetEventObject( this );
589 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
591 closing
.DeleteObject(this);
594 // Destroy the window (delayed, if a managed window)
595 bool wxDialog::Destroy(void)
597 if (!wxPendingDelete
.Member(this))
598 wxPendingDelete
.Append(this);
602 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
604 // if we're using constraints - do use them
605 #if wxUSE_CONSTRAINTS
606 if ( GetAutoLayout() ) {
612 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
617 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));