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_BUTTON(wxID_OK
, wxDialog::OnOK
)
51 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
52 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
53 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
54 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
55 EVT_CLOSE(wxDialog::OnCloseWindow
)
60 long wxDialog::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
62 return ::CallWindowProc(CASTWNDPROC m_oldWndProc
, (HWND
) GetHWND(), (UINT
) nMsg
, (WPARAM
) wParam
, (LPARAM
) lParam
);
65 bool wxDialog::MSWProcessMessage(WXMSG
* pMsg
)
67 return (::IsDialogMessage((HWND
) GetHWND(), (MSG
*)pMsg
) != 0);
70 bool wxDialog::MSWOnClose(void)
75 wxDialog::wxDialog(void)
78 m_modalShowing
= FALSE
;
80 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
83 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
84 const wxString
& title
,
90 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
94 wxTopLevelWindows
.Append(this);
96 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
98 if (parent
) parent
->AddChild(this);
101 m_windowId
= (int)NewControlId();
110 if (x
< 0) x
= wxDIALOG_DEFAULT_X
;
111 if (y
< 0) y
= wxDIALOG_DEFAULT_Y
;
113 m_windowStyle
= style
;
116 m_modalShowing
= FALSE
;
123 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
124 if (m_windowStyle
& wxSTAY_ON_TOP
)
125 extendedStyle
|= WS_EX_TOPMOST
;
127 // Allows creation of dialogs with & without captions under MSWindows
128 if(style
& wxCAPTION
){
129 MSWCreate(m_windowId
, (wxWindow
*)parent
, NULL
, this, NULL
, x
, y
, width
, height
, 0, "wxCaptionDialog",
133 MSWCreate(m_windowId
, (wxWindow
*)parent
, NULL
, this, NULL
, x
, y
, width
, height
, 0, "wxNoCaptionDialog",
137 SubclassWin(GetHWND());
139 SetWindowText((HWND
) GetHWND(), (const char *)title
);
140 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
145 void wxDialog::SetModal(bool flag
)
148 m_windowStyle
|= wxDIALOG_MODAL
;
150 if ( m_windowStyle
& wxDIALOG_MODAL
)
151 m_windowStyle
-= wxDIALOG_MODAL
;
153 wxModelessWindows
.DeleteObject(this);
155 wxModelessWindows
.Append(this);
158 wxDialog::~wxDialog()
160 m_isBeingDeleted
= TRUE
;
162 wxTopLevelWindows
.DeleteObject(this);
167 // For some reason, wxWindows can activate another task altogether
168 // when a frame is destroyed after a modal dialog has been invoked.
169 // Try to bring the parent to the top.
170 // dfgg: I moved this following line from end of function -
171 // must not call if another window is on top!!
172 // This can often happen with Close() and delayed deleting
173 if (GetParent() && GetParent()->GetHWND())
174 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
177 m_modalShowing
= FALSE
;
179 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
181 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
182 wxModelessWindows
.DeleteObject(this);
186 // If this is the last top-level window, exit.
187 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
189 wxTheApp
->SetTopWindow(NULL
);
191 if (wxTheApp
->GetExitOnFrameDelete())
198 // By default, pressing escape cancels the dialog
199 void wxDialog::OnCharHook(wxKeyEvent
& event
)
203 if (event
.m_keyCode
== WXK_ESCAPE
)
205 // Behaviour changed in 2.0: we'll send a Cancel message
206 // to the dialog instead of Close.
207 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
208 cancelEvent
.SetEventObject( this );
209 GetEventHandler()->ProcessEvent(cancelEvent
);
214 // We didn't process this event.
218 void wxDialog::OnPaint(wxPaintEvent
& event
)
220 // No: if you call the default procedure, it makes
221 // the following painting code not work.
222 // wxWindow::OnPaint(event);
225 void wxDialog::Fit(void)
230 void wxDialog::Iconize(bool WXUNUSED(iconize
))
232 // Windows dialog boxes can't be iconized
235 bool wxDialog::IsIconized(void) const
240 void wxDialog::SetSize(int x
, int y
, int width
, int height
, int WXUNUSED(sizeFlags
))
242 wxWindow::SetSize(x
, y
, width
, height
);
245 void wxDialog::SetClientSize(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(void) const
282 bool wxDialog::Show(bool show
)
289 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
291 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
294 if (!wxModelessWindows
.Member(this))
295 wxModelessWindows
.Append(this);
297 wxModelessWindows
.DeleteObject(this);
300 if (!wxTopLevelWindows
.Member(this))
301 wxTopLevelWindows
.Append(this);
303 wxTopLevelWindows
.DeleteObject(this);
310 m_hwndOldFocus
= (WXHWND
)::GetFocus();
314 BringWindowToTop((HWND
) GetHWND());
318 m_modalShowing
= TRUE
;
319 wxNode
*node
= wxModalDialogs
.First();
322 wxDialog
*box
= (wxDialog
*)node
->Data();
324 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
328 // if we don't do it, some window might be deleted while we have pointers
329 // to them in our disabledWindows list and the program will crash when it
330 // will try to reenable them after the modal dialog end
331 wxTheApp
->DeletePendingObjects();
332 wxList disabledWindows
;
334 node
= wxModelessWindows
.First();
337 wxWindow
*win
= (wxWindow
*)node
->Data();
338 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
340 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
341 disabledWindows
.Append(win
);
346 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
347 EnableWindow((HWND
) GetHWND(), TRUE
);
348 BringWindowToTop((HWND
) GetHWND());
350 if (!wxModalDialogs
.Member(this))
351 wxModalDialogs
.Append(this);
354 // Must test whether this dialog still exists: we may not process
355 // a message before the deletion.
356 while (wxModalDialogs
.Member(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
358 if (m_acceleratorTable
.Ok() &&
359 ::TranslateAccelerator((HWND
) GetHWND(), (HACCEL
) m_acceleratorTable
.GetHACCEL(), &msg
))
361 // Have processed the message
363 else if (!IsDialogMessage((HWND
) GetHWND(), &msg
))
365 TranslateMessage(&msg
);
366 DispatchMessage(&msg
);
368 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
369 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
370 // a Show(FALSE) in the mean time!!!
371 // Without the test, we might delete the dialog before the end of modal showing.
373 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
375 // Keep going until we decide we've done enough
379 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
380 node
=disabledWindows
.First();
382 wxWindow
* win
= (wxWindow
*) node
->Data();
383 HWND hWnd
= (HWND
) win
->GetHWND();
384 if (::IsWindow(hWnd
) && (wxModalDialogs
.Member(win
) || wxModelessWindows
.Member(win
) ))
385 ::EnableWindow(hWnd
,TRUE
);
391 ::SetFocus((HWND
)m_hwndOldFocus
);
393 wxModalDialogs
.DeleteObject(this);
395 wxNode
*last
= wxModalDialogs
.Last();
397 // If there's still a modal dialog active, we
398 // enable it, else we enable all modeless windows
401 wxDialog
*box
= (wxDialog
*)last
->Data();
402 HWND hwnd
= (HWND
) box
->GetHWND();
403 if (box
->m_winEnabled
)
404 EnableWindow(hwnd
, TRUE
);
405 BringWindowToTop(hwnd
);
409 wxNode
*node
= wxModelessWindows
.First();
412 wxWindow
*win
= (wxWindow
*)node
->Data();
413 HWND hwnd
= (HWND
) win
->GetHWND();
414 // Only enable again if not user-disabled.
415 if (win
->IsUserEnabled())
416 EnableWindow(hwnd
, TRUE
);
420 // Try to highlight the correct window (the parent)
424 hWndParent
= (HWND
) GetParent()->GetHWND();
426 ::BringWindowToTop(hWndParent
);
428 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
429 m_modalShowing
= FALSE
;
436 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
437 BringWindowToTop((HWND
) GetHWND());
441 // Try to highlight the correct window (the parent)
445 hWndParent
= (HWND
) GetParent()->GetHWND();
447 ::BringWindowToTop(hWndParent
);
449 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
455 void wxDialog::SetTitle(const wxString
& title
)
457 SetWindowText((HWND
) GetHWND(), (const char *)title
);
460 wxString
wxDialog::GetTitle(void) const
462 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
463 return wxString(wxBuffer
);
466 void wxDialog::Centre(int direction
)
468 int x_offset
,y_offset
;
469 int display_width
, display_height
;
470 int width
, height
, x
, y
;
471 wxWindow
*parent
= GetParent();
472 if ((direction
& wxCENTER_FRAME
) && parent
)
474 parent
->GetPosition(&x_offset
,&y_offset
) ;
475 parent
->GetSize(&display_width
,&display_height
) ;
479 wxDisplaySize(&display_width
, &display_height
);
484 GetSize(&width
, &height
);
487 if (direction
& wxHORIZONTAL
)
488 x
= (int)((display_width
- width
)/2);
489 if (direction
& wxVERTICAL
)
490 y
= (int)((display_height
- height
)/2);
492 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
495 // Replacement for Show(TRUE) for modal dialogs - returns return code
496 int wxDialog::ShowModal(void)
498 m_windowStyle
|= wxDIALOG_MODAL
;
500 return GetReturnCode();
503 void wxDialog::EndModal(int retCode
)
505 SetReturnCode(retCode
);
509 // Define for each class of dialog and control
510 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
511 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
514 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
515 return (WXHBRUSH
) hbrush
;
522 void wxDialog::OnOK(wxCommandEvent
& event
)
524 if ( Validate() && TransferDataFromWindow() )
530 SetReturnCode(wxID_OK
);
536 void wxDialog::OnApply(wxCommandEvent
& event
)
539 TransferDataFromWindow();
540 // TODO probably need to disable the Apply button until things change again
543 void wxDialog::OnCancel(wxCommandEvent
& event
)
546 EndModal(wxID_CANCEL
);
549 SetReturnCode(wxID_CANCEL
);
554 bool wxDialog::OnClose(void)
556 // Behaviour changed in 2.0: we'll send a Cancel message by default,
557 // which may close the dialog.
558 // Check for looping if the Cancel event handler calls Close()
560 static wxList closing
;
562 if ( closing
.Member(this) )
565 closing
.Append(this);
567 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
568 cancelEvent
.SetEventObject( this );
569 GetEventHandler()->ProcessEvent(cancelEvent
);
571 closing
.DeleteObject(this);
576 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
579 if ( GetEventHandler()->OnClose() || event
.GetForce())
587 // Destroy the window (delayed, if a managed window)
588 bool wxDialog::Destroy(void)
590 if (!wxPendingDelete
.Member(this))
591 wxPendingDelete
.Append(this);
595 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
600 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
605 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
607 return wxWindow::MSWWindowProc(message
, wParam
, lParam
);