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
);
216 // ensure that there is another message for this window so the
217 // ShowModal loop will exit and won't get stuck in GetMessage().
218 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
222 // We didn't process this event.
226 void wxDialog::OnPaint(wxPaintEvent
& event
)
228 // No: if you call the default procedure, it makes
229 // the following painting code not work.
230 // wxWindow::OnPaint(event);
238 void wxDialog::Iconize(bool WXUNUSED(iconize
))
240 // Windows dialog boxes can't be iconized
243 bool wxDialog::IsIconized() const
248 void wxDialog::DoSetClientSize(int width
, int height
)
250 HWND hWnd
= (HWND
) GetHWND();
252 ::GetClientRect(hWnd
, &rect
);
255 GetWindowRect(hWnd
, &rect2
);
257 // Find the difference between the entire window (title bar and all)
258 // and the client area; add this to the new client size to move the
260 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
261 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
263 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
265 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
266 event
.SetEventObject( this );
267 GetEventHandler()->ProcessEvent(event
);
270 void wxDialog::GetPosition(int *x
, int *y
) const
272 HWND hWnd
= (HWND
) GetHWND();
274 GetWindowRect(hWnd
, &rect
);
280 bool wxDialog::IsShown() const
285 bool wxDialog::IsModal() const
287 return wxModalDialogs
.Find((wxDialog
*)this) != 0; // const_cast
290 bool wxDialog::Show(bool show
)
297 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
299 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
302 if (!wxModelessWindows
.Find(this))
303 wxModelessWindows
.Append(this);
305 wxModelessWindows
.DeleteObject(this);
308 if (!wxTopLevelWindows
.Find(this))
309 wxTopLevelWindows
.Append(this);
311 wxTopLevelWindows
.DeleteObject(this);
318 // find the top level window which had focus before - we will restore
321 for ( HWND hwnd
= ::GetFocus(); hwnd
; hwnd
= ::GetParent(hwnd
) )
323 m_hwndOldFocus
= (WXHWND
)hwnd
;
328 BringWindowToTop((HWND
) GetHWND());
332 m_modalShowing
= TRUE
;
333 wxNode
*node
= wxModalDialogs
.First();
336 wxDialog
*box
= (wxDialog
*)node
->Data();
338 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
342 // if we don't do it, some window might be deleted while we have pointers
343 // to them in our disabledWindows list and the program will crash when it
344 // will try to reenable them after the modal dialog end
345 wxTheApp
->DeletePendingObjects();
346 wxList disabledWindows
;
348 node
= wxModelessWindows
.First();
351 wxWindow
*win
= (wxWindow
*)node
->Data();
352 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
354 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
355 disabledWindows
.Append(win
);
360 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
361 EnableWindow((HWND
) GetHWND(), TRUE
);
362 BringWindowToTop((HWND
) GetHWND());
364 if ( !wxModalDialogs
.Find(this) )
365 wxModalDialogs
.Append(this);
368 // Must test whether this dialog still exists: we may not process
369 // a message before the deletion.
370 while (wxModalDialogs
.Find(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
372 if ( m_acceleratorTable
.Ok() &&
373 ::TranslateAccelerator((HWND
)GetHWND(),
374 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
377 // Have processed the message
379 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&msg
) )
381 TranslateMessage(&msg
);
382 DispatchMessage(&msg
);
385 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
386 // we should try removing the m_modalShowing test
388 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
389 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
390 // a Show(FALSE) in the mean time!!!
391 // Without the test, we might delete the dialog before the end of modal showing.
393 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
395 // Keep going until we decide we've done enough
399 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
400 node
=disabledWindows
.First();
402 wxWindow
* win
= (wxWindow
*) node
->Data();
403 if (wxModalDialogs
.Find(win
) || wxModelessWindows
.Find(win
))
405 HWND hWnd
= (HWND
) win
->GetHWND();
406 if (::IsWindow(hWnd
))
407 ::EnableWindow(hWnd
,TRUE
);
414 ::SetFocus((HWND
)m_hwndOldFocus
);
416 wxModalDialogs
.DeleteObject(this);
418 wxNode
*last
= wxModalDialogs
.Last();
420 // If there's still a modal dialog active, we
421 // enable it, else we enable all modeless windows
424 // VZ: I don't understand what this is supposed to do, so I'll leave
425 // it out for now and look for horrible consequences
426 wxDialog
*box
= (wxDialog
*)last
->Data();
427 HWND hwnd
= (HWND
) box
->GetHWND();
429 if (box
->IsUserEnabled())
431 EnableWindow(hwnd
, TRUE
);
432 BringWindowToTop(hwnd
);
436 wxNode
*node
= wxModelessWindows
.First();
439 wxWindow
*win
= (wxWindow
*)node
->Data();
440 HWND hwnd
= (HWND
) win
->GetHWND();
441 // Only enable again if not user-disabled.
443 if (win
->IsUserEnabled())
445 EnableWindow(hwnd
, TRUE
);
449 // Try to highlight the correct window (the parent)
453 hWndParent
= (HWND
) GetParent()->GetHWND();
455 ::BringWindowToTop(hWndParent
);
457 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
458 m_modalShowing
= FALSE
;
465 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
466 BringWindowToTop((HWND
) GetHWND());
470 // Try to highlight the correct window (the parent)
474 hWndParent
= (HWND
) GetParent()->GetHWND();
476 ::BringWindowToTop(hWndParent
);
480 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
486 void wxDialog::SetTitle(const wxString
& title
)
488 SetWindowText((HWND
) GetHWND(), title
.c_str());
491 wxString
wxDialog::GetTitle() const
493 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
494 return wxString(wxBuffer
);
497 void wxDialog::Centre(int direction
)
499 int x_offset
,y_offset
;
500 int display_width
, display_height
;
501 int width
, height
, x
, y
;
502 wxWindow
*parent
= GetParent();
503 if ((direction
& wxCENTER_FRAME
) && parent
)
505 parent
->GetPosition(&x_offset
,&y_offset
) ;
506 parent
->GetSize(&display_width
,&display_height
) ;
510 wxDisplaySize(&display_width
, &display_height
);
515 GetSize(&width
, &height
);
518 if (direction
& wxHORIZONTAL
)
519 x
= (int)((display_width
- width
)/2);
520 if (direction
& wxVERTICAL
)
521 y
= (int)((display_height
- height
)/2);
523 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
526 // Replacement for Show(TRUE) for modal dialogs - returns return code
527 int wxDialog::ShowModal()
529 m_windowStyle
|= wxDIALOG_MODAL
;
531 return GetReturnCode();
534 void wxDialog::EndModal(int retCode
)
536 SetReturnCode(retCode
);
540 // Define for each class of dialog and control
541 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
542 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
545 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
546 return (WXHBRUSH
) hbrush
;
553 void wxDialog::OnOK(wxCommandEvent
& event
)
555 if ( Validate() && TransferDataFromWindow() )
561 SetReturnCode(wxID_OK
);
567 void wxDialog::OnApply(wxCommandEvent
& event
)
570 TransferDataFromWindow();
571 // TODO probably need to disable the Apply button until things change again
574 void wxDialog::OnCancel(wxCommandEvent
& event
)
577 EndModal(wxID_CANCEL
);
580 SetReturnCode(wxID_CANCEL
);
585 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
587 // We'll send a Cancel message by default,
588 // which may close the dialog.
589 // Check for looping if the Cancel event handler calls Close().
591 // Note that if a cancel button and handler aren't present in the dialog,
592 // nothing will happen when you close the dialog via the window manager, or
594 // We wouldn't want to destroy the dialog by default, since the dialog may have been
595 // created on the stack.
596 // However, this does mean that calling dialog->Close() won't delete the dialog
597 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
598 // sure to destroy the dialog.
599 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
601 static wxList closing
;
603 if ( closing
.Member(this) )
606 closing
.Append(this);
608 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
609 cancelEvent
.SetEventObject( this );
610 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
612 closing
.DeleteObject(this);
615 // Destroy the window (delayed, if a managed window)
616 bool wxDialog::Destroy()
618 if (!wxPendingDelete
.Member(this))
619 wxPendingDelete
.Append(this);
623 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
625 // if we're using constraints - do use them
626 #if wxUSE_CONSTRAINTS
627 if ( GetAutoLayout() )
634 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
639 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
644 // ---------------------------------------------------------------------------
645 // dialog window proc
646 // ---------------------------------------------------------------------------
648 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
651 bool processed
= FALSE
;
656 // if we can't close, tell the system that we processed the
657 // message - otherwise it would close us
658 processed
= !Close();
663 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);