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 wxWindowList wxModalDialogs
;
44 wxWindowList 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
)
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
& wxTHICK_FRAME
)
125 dlg
= "wxResizeableDialog";
126 else if ( style
& wxCAPTION
)
127 dlg
= "wxCaptionDialog";
129 dlg
= "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);
175 // For some reason, wxWindows can activate another task altogether
176 // when a frame is destroyed after a modal dialog has been invoked.
177 // Try to bring the parent to the top.
178 // dfgg: I moved this following line from end of function -
179 // must not call if another window is on top!!
180 // This can often happen with Close() and delayed deleting
181 if (GetParent() && GetParent()->GetHWND())
182 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
185 m_modalShowing
= FALSE
;
187 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
189 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
190 wxModelessWindows
.DeleteObject(this);
194 // If this is the last top-level window, exit.
195 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
197 wxTheApp
->SetTopWindow(NULL
);
199 if (wxTheApp
->GetExitOnFrameDelete())
206 // By default, pressing escape cancels the dialog
207 void wxDialog::OnCharHook(wxKeyEvent
& event
)
211 if (event
.m_keyCode
== WXK_ESCAPE
)
213 // Behaviour changed in 2.0: we'll send a Cancel message
214 // to the dialog instead of Close.
215 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
216 cancelEvent
.SetEventObject( this );
217 GetEventHandler()->ProcessEvent(cancelEvent
);
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::Show(bool show
)
292 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
294 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
297 if (!wxModelessWindows
.Find(this))
298 wxModelessWindows
.Append(this);
300 wxModelessWindows
.DeleteObject(this);
303 if (!wxTopLevelWindows
.Find(this))
304 wxTopLevelWindows
.Append(this);
306 wxTopLevelWindows
.DeleteObject(this);
313 m_hwndOldFocus
= (WXHWND
)::GetFocus();
317 BringWindowToTop((HWND
) GetHWND());
321 m_modalShowing
= TRUE
;
322 wxNode
*node
= wxModalDialogs
.First();
325 wxDialog
*box
= (wxDialog
*)node
->Data();
327 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
331 // if we don't do it, some window might be deleted while we have pointers
332 // to them in our disabledWindows list and the program will crash when it
333 // will try to reenable them after the modal dialog end
334 wxTheApp
->DeletePendingObjects();
335 wxList disabledWindows
;
337 node
= wxModelessWindows
.First();
340 wxWindow
*win
= (wxWindow
*)node
->Data();
341 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
343 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
344 disabledWindows
.Append(win
);
349 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
350 EnableWindow((HWND
) GetHWND(), TRUE
);
351 BringWindowToTop((HWND
) GetHWND());
353 if ( !wxModalDialogs
.Find(this) )
354 wxModalDialogs
.Append(this);
357 // Must test whether this dialog still exists: we may not process
358 // a message before the deletion.
359 while (wxModalDialogs
.Find(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
361 if ( m_acceleratorTable
.Ok() &&
362 ::TranslateAccelerator((HWND
)GetHWND(),
363 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
366 // Have processed the message
368 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&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
.Find(win
) || wxModelessWindows
.Find(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 // VZ: I don't understand what this is supposed to do, so I'll leave
414 // it out for now and look for horrible consequences
415 wxDialog
*box
= (wxDialog
*)last
->Data();
416 HWND hwnd
= (HWND
) box
->GetHWND();
418 if (box
->IsUserEnabled())
420 EnableWindow(hwnd
, TRUE
);
421 BringWindowToTop(hwnd
);
425 wxNode
*node
= wxModelessWindows
.First();
428 wxWindow
*win
= (wxWindow
*)node
->Data();
429 HWND hwnd
= (HWND
) win
->GetHWND();
430 // Only enable again if not user-disabled.
432 if (win
->IsUserEnabled())
434 EnableWindow(hwnd
, TRUE
);
438 // Try to highlight the correct window (the parent)
442 hWndParent
= (HWND
) GetParent()->GetHWND();
444 ::BringWindowToTop(hWndParent
);
446 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
447 m_modalShowing
= FALSE
;
454 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
455 BringWindowToTop((HWND
) GetHWND());
459 // Try to highlight the correct window (the parent)
463 hWndParent
= (HWND
) GetParent()->GetHWND();
465 ::BringWindowToTop(hWndParent
);
467 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
473 void wxDialog::SetTitle(const wxString
& title
)
475 SetWindowText((HWND
) GetHWND(), (const char *)title
);
478 wxString
wxDialog::GetTitle() const
480 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
481 return wxString(wxBuffer
);
484 void wxDialog::Centre(int direction
)
486 int x_offset
,y_offset
;
487 int display_width
, display_height
;
488 int width
, height
, x
, y
;
489 wxWindow
*parent
= GetParent();
490 if ((direction
& wxCENTER_FRAME
) && parent
)
492 parent
->GetPosition(&x_offset
,&y_offset
) ;
493 parent
->GetSize(&display_width
,&display_height
) ;
497 wxDisplaySize(&display_width
, &display_height
);
502 GetSize(&width
, &height
);
505 if (direction
& wxHORIZONTAL
)
506 x
= (int)((display_width
- width
)/2);
507 if (direction
& wxVERTICAL
)
508 y
= (int)((display_height
- height
)/2);
510 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
513 // Replacement for Show(TRUE) for modal dialogs - returns return code
514 int wxDialog::ShowModal()
516 m_windowStyle
|= wxDIALOG_MODAL
;
518 return GetReturnCode();
521 void wxDialog::EndModal(int retCode
)
523 SetReturnCode(retCode
);
527 // Define for each class of dialog and control
528 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
529 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
532 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
533 return (WXHBRUSH
) hbrush
;
540 void wxDialog::OnOK(wxCommandEvent
& event
)
542 if ( Validate() && TransferDataFromWindow() )
548 SetReturnCode(wxID_OK
);
554 void wxDialog::OnApply(wxCommandEvent
& event
)
557 TransferDataFromWindow();
558 // TODO probably need to disable the Apply button until things change again
561 void wxDialog::OnCancel(wxCommandEvent
& event
)
564 EndModal(wxID_CANCEL
);
567 SetReturnCode(wxID_CANCEL
);
572 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
574 // We'll send a Cancel message by default,
575 // which may close the dialog.
576 // Check for looping if the Cancel event handler calls Close().
578 // Note that if a cancel button and handler aren't present in the dialog,
579 // nothing will happen when you close the dialog via the window manager, or
581 // We wouldn't want to destroy the dialog by default, since the dialog may have been
582 // created on the stack.
583 // However, this does mean that calling dialog->Close() won't delete the dialog
584 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
585 // sure to destroy the dialog.
586 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
588 static wxList closing
;
590 if ( closing
.Member(this) )
593 closing
.Append(this);
595 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
596 cancelEvent
.SetEventObject( this );
597 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
599 closing
.DeleteObject(this);
602 // Destroy the window (delayed, if a managed window)
603 bool wxDialog::Destroy()
605 if (!wxPendingDelete
.Member(this))
606 wxPendingDelete
.Append(this);
610 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
612 // if we're using constraints - do use them
613 #if wxUSE_CONSTRAINTS
614 if ( GetAutoLayout() )
621 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
626 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
631 // ---------------------------------------------------------------------------
632 // dialog window proc
633 // ---------------------------------------------------------------------------
635 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
638 bool processed
= FALSE
;
643 // if we can't close, tell the system that we processed the
644 // message - otherwise it would close us
645 processed
= !Close();
650 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);