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
,
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 // All dialogs should really have this style
119 m_windowStyle
|= wxTAB_TRAVERSAL
;
121 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
122 if (m_windowStyle
& wxSTAY_ON_TOP
)
123 extendedStyle
|= WS_EX_TOPMOST
;
125 // Allows creation of dialogs with & without captions under MSWindows,
126 // resizeable or not (but a resizeable dialog always has caption -
127 // otherwise it would look too strange)
129 if ( style
& wxTHICK_FRAME
)
130 dlg
= "wxResizeableDialog";
131 else if ( style
& wxCAPTION
)
132 dlg
= "wxCaptionDialog";
134 dlg
= "wxNoCaptionDialog";
135 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
137 0, // style is not used if we have dlg template
141 HWND hwnd
= (HWND
)GetHWND();
145 wxLogError(_("Failed to create dialog."));
150 SubclassWin(GetHWND());
152 SetWindowText(hwnd
, title
);
153 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
158 void wxDialog::SetModal(bool flag
)
161 m_windowStyle
|= wxDIALOG_MODAL
;
163 if ( m_windowStyle
& wxDIALOG_MODAL
)
164 m_windowStyle
-= wxDIALOG_MODAL
;
166 wxModelessWindows
.DeleteObject(this);
168 wxModelessWindows
.Append(this);
171 wxDialog::~wxDialog()
173 m_isBeingDeleted
= TRUE
;
175 wxTopLevelWindows
.DeleteObject(this);
180 // For some reason, wxWindows can activate another task altogether
181 // when a frame is destroyed after a modal dialog has been invoked.
182 // Try to bring the parent to the top.
183 // dfgg: I moved this following line from end of function -
184 // must not call if another window is on top!!
185 // This can often happen with Close() and delayed deleting
186 if (GetParent() && GetParent()->GetHWND())
187 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
190 m_modalShowing
= FALSE
;
192 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
194 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
195 wxModelessWindows
.DeleteObject(this);
199 // If this is the last top-level window, exit.
200 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
202 wxTheApp
->SetTopWindow(NULL
);
204 if (wxTheApp
->GetExitOnFrameDelete())
211 // By default, pressing escape cancels the dialog
212 void wxDialog::OnCharHook(wxKeyEvent
& event
)
216 if (event
.m_keyCode
== WXK_ESCAPE
)
218 // Behaviour changed in 2.0: we'll send a Cancel message
219 // to the dialog instead of Close.
220 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
221 cancelEvent
.SetEventObject( this );
222 GetEventHandler()->ProcessEvent(cancelEvent
);
227 // We didn't process this event.
231 void wxDialog::OnPaint(wxPaintEvent
& event
)
233 // No: if you call the default procedure, it makes
234 // the following painting code not work.
235 // wxWindow::OnPaint(event);
238 void wxDialog::Fit(void)
243 void wxDialog::Iconize(bool WXUNUSED(iconize
))
245 // Windows dialog boxes can't be iconized
248 bool wxDialog::IsIconized(void) const
253 void wxDialog::DoSetClientSize(int width
, int height
)
255 HWND hWnd
= (HWND
) GetHWND();
257 ::GetClientRect(hWnd
, &rect
);
260 GetWindowRect(hWnd
, &rect2
);
262 // Find the difference between the entire window (title bar and all)
263 // and the client area; add this to the new client size to move the
265 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
266 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
268 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
270 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
271 event
.SetEventObject( this );
272 GetEventHandler()->ProcessEvent(event
);
275 void wxDialog::GetPosition(int *x
, int *y
) const
277 HWND hWnd
= (HWND
) GetHWND();
279 GetWindowRect(hWnd
, &rect
);
285 bool wxDialog::IsShown(void) const
290 bool wxDialog::Show(bool show
)
297 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
299 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
302 if (!wxModelessWindows
.Member(this))
303 wxModelessWindows
.Append(this);
305 wxModelessWindows
.DeleteObject(this);
308 if (!wxTopLevelWindows
.Member(this))
309 wxTopLevelWindows
.Append(this);
311 wxTopLevelWindows
.DeleteObject(this);
318 m_hwndOldFocus
= (WXHWND
)::GetFocus();
322 BringWindowToTop((HWND
) GetHWND());
326 m_modalShowing
= TRUE
;
327 wxNode
*node
= wxModalDialogs
.First();
330 wxDialog
*box
= (wxDialog
*)node
->Data();
332 ::EnableWindow((HWND
) box
->GetHWND(), FALSE
);
336 // if we don't do it, some window might be deleted while we have pointers
337 // to them in our disabledWindows list and the program will crash when it
338 // will try to reenable them after the modal dialog end
339 wxTheApp
->DeletePendingObjects();
340 wxList disabledWindows
;
342 node
= wxModelessWindows
.First();
345 wxWindow
*win
= (wxWindow
*)node
->Data();
346 if (::IsWindowEnabled((HWND
) win
->GetHWND()))
348 ::EnableWindow((HWND
) win
->GetHWND(), FALSE
);
349 disabledWindows
.Append(win
);
354 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
355 EnableWindow((HWND
) GetHWND(), TRUE
);
356 BringWindowToTop((HWND
) GetHWND());
358 if (!wxModalDialogs
.Member(this))
359 wxModalDialogs
.Append(this);
362 // Must test whether this dialog still exists: we may not process
363 // a message before the deletion.
364 while (wxModalDialogs
.Member(this) && m_modalShowing
&& GetMessage(&msg
, NULL
, 0, 0))
366 if ( m_acceleratorTable
.Ok() &&
367 ::TranslateAccelerator((HWND
)GetHWND(),
368 (HACCEL
)m_acceleratorTable
.GetHACCEL(),
371 // Have processed the message
373 else if ( !wxTheApp
->ProcessMessage((WXMSG
*)&msg
) )
375 TranslateMessage(&msg
);
376 DispatchMessage(&msg
);
379 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
380 // we should try removing the m_modalShowing test
382 if (m_modalShowing
&& !::PeekMessage(&msg
, 0, 0, 0, PM_NOREMOVE
))
383 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
384 // a Show(FALSE) in the mean time!!!
385 // Without the test, we might delete the dialog before the end of modal showing.
387 while (wxTheApp
->ProcessIdle() && m_modalShowing
)
389 // Keep going until we decide we've done enough
393 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
394 node
=disabledWindows
.First();
396 wxWindow
* win
= (wxWindow
*) node
->Data();
397 if (wxModalDialogs
.Member(win
) || wxModelessWindows
.Member(win
))
399 HWND hWnd
= (HWND
) win
->GetHWND();
400 if (::IsWindow(hWnd
))
401 ::EnableWindow(hWnd
,TRUE
);
408 ::SetFocus((HWND
)m_hwndOldFocus
);
410 wxModalDialogs
.DeleteObject(this);
412 wxNode
*last
= wxModalDialogs
.Last();
414 // If there's still a modal dialog active, we
415 // enable it, else we enable all modeless windows
418 // VZ: I don't understand what this is supposed to do, so I'll leave
419 // it out for now and look for horrible consequences
420 wxDialog
*box
= (wxDialog
*)last
->Data();
421 HWND hwnd
= (HWND
) box
->GetHWND();
423 if (box
->IsUserEnabled())
425 EnableWindow(hwnd
, TRUE
);
426 BringWindowToTop(hwnd
);
430 wxNode
*node
= wxModelessWindows
.First();
433 wxWindow
*win
= (wxWindow
*)node
->Data();
434 HWND hwnd
= (HWND
) win
->GetHWND();
435 // Only enable again if not user-disabled.
437 if (win
->IsUserEnabled())
439 EnableWindow(hwnd
, TRUE
);
443 // Try to highlight the correct window (the parent)
447 hWndParent
= (HWND
) GetParent()->GetHWND();
449 ::BringWindowToTop(hWndParent
);
451 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
452 m_modalShowing
= FALSE
;
459 ShowWindow((HWND
) GetHWND(), SW_SHOW
);
460 BringWindowToTop((HWND
) GetHWND());
464 // Try to highlight the correct window (the parent)
468 hWndParent
= (HWND
) GetParent()->GetHWND();
470 ::BringWindowToTop(hWndParent
);
472 ShowWindow((HWND
) GetHWND(), SW_HIDE
);
478 void wxDialog::SetTitle(const wxString
& title
)
480 SetWindowText((HWND
) GetHWND(), (const char *)title
);
483 wxString
wxDialog::GetTitle(void) const
485 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
486 return wxString(wxBuffer
);
489 void wxDialog::Centre(int direction
)
491 int x_offset
,y_offset
;
492 int display_width
, display_height
;
493 int width
, height
, x
, y
;
494 wxWindow
*parent
= GetParent();
495 if ((direction
& wxCENTER_FRAME
) && parent
)
497 parent
->GetPosition(&x_offset
,&y_offset
) ;
498 parent
->GetSize(&display_width
,&display_height
) ;
502 wxDisplaySize(&display_width
, &display_height
);
507 GetSize(&width
, &height
);
510 if (direction
& wxHORIZONTAL
)
511 x
= (int)((display_width
- width
)/2);
512 if (direction
& wxVERTICAL
)
513 y
= (int)((display_height
- height
)/2);
515 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
518 // Replacement for Show(TRUE) for modal dialogs - returns return code
519 int wxDialog::ShowModal(void)
521 m_windowStyle
|= wxDIALOG_MODAL
;
523 return GetReturnCode();
526 void wxDialog::EndModal(int retCode
)
528 SetReturnCode(retCode
);
532 // Define for each class of dialog and control
533 WXHBRUSH
wxDialog::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
534 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
537 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
538 return (WXHBRUSH
) hbrush
;
545 void wxDialog::OnOK(wxCommandEvent
& event
)
547 if ( Validate() && TransferDataFromWindow() )
553 SetReturnCode(wxID_OK
);
559 void wxDialog::OnApply(wxCommandEvent
& event
)
562 TransferDataFromWindow();
563 // TODO probably need to disable the Apply button until things change again
566 void wxDialog::OnCancel(wxCommandEvent
& event
)
569 EndModal(wxID_CANCEL
);
572 SetReturnCode(wxID_CANCEL
);
577 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
579 // We'll send a Cancel message by default,
580 // which may close the dialog.
581 // Check for looping if the Cancel event handler calls Close().
583 // Note that if a cancel button and handler aren't present in the dialog,
584 // nothing will happen when you close the dialog via the window manager, or
586 // We wouldn't want to destroy the dialog by default, since the dialog may have been
587 // created on the stack.
588 // However, this does mean that calling dialog->Close() won't delete the dialog
589 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
590 // sure to destroy the dialog.
591 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
593 static wxList closing
;
595 if ( closing
.Member(this) )
598 closing
.Append(this);
600 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
601 cancelEvent
.SetEventObject( this );
602 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
604 closing
.DeleteObject(this);
607 // Destroy the window (delayed, if a managed window)
608 bool wxDialog::Destroy(void)
610 if (!wxPendingDelete
.Member(this))
611 wxPendingDelete
.Append(this);
615 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
617 // if we're using constraints - do use them
618 #if wxUSE_CONSTRAINTS
619 if ( GetAutoLayout() ) {
625 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
630 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));