1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dialog.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dialog.h"
36 #include "wx/settings.h"
41 #include "wx/msw/private.h"
44 #if wxUSE_COMMON_DIALOGS
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // default dialog pos and size
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // all objects to be deleted during next idle processing - from window.cpp
65 extern wxList WXDLLEXPORT wxPendingDelete
;
67 // all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68 wxWindowList wxModelessWindows
;
70 // all modal dialogs currently shown
71 static wxWindowList wxModalDialogs
;
73 static wxWindow
*m_oldFocus
;
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
81 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
82 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
83 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
84 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
86 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
88 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
90 EVT_CLOSE(wxDialog::OnCloseWindow
)
93 // ============================================================================
95 // ============================================================================
97 // ----------------------------------------------------------------------------
98 // wxDialog construction
99 // ----------------------------------------------------------------------------
105 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
108 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
109 const wxString
& title
,
113 const wxString
& name
)
115 m_oldFocus
= FindFocus();
117 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
121 wxTopLevelWindows
.Append(this);
123 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
125 if (parent
) parent
->AddChild(this);
128 m_windowId
= (int)NewControlId();
138 x
= wxDIALOG_DEFAULT_X
;
140 y
= wxDIALOG_DEFAULT_Y
;
142 m_windowStyle
= style
;
147 width
= wxDIALOG_DEFAULT_WIDTH
;
149 height
= wxDIALOG_DEFAULT_HEIGHT
;
151 // All dialogs should really have this style
152 m_windowStyle
|= wxTAB_TRAVERSAL
;
154 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
155 if (m_windowStyle
& wxSTAY_ON_TOP
)
156 extendedStyle
|= WS_EX_TOPMOST
;
158 // Allows creation of dialogs with & without captions under MSWindows,
159 // resizeable or not (but a resizeable dialog always has caption -
160 // otherwise it would look too strange)
162 if ( style
& wxRESIZE_BORDER
)
163 dlg
= wxT("wxResizeableDialog");
164 else if ( style
& wxCAPTION
)
165 dlg
= wxT("wxCaptionDialog");
167 dlg
= wxT("wxNoCaptionDialog");
168 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
170 0, // style is not used if we have dlg template
174 HWND hwnd
= (HWND
)GetHWND();
178 wxLogError(_("Failed to create dialog."));
183 SubclassWin(GetHWND());
185 SetWindowText(hwnd
, title
);
186 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
191 void wxDialog::SetModal(bool flag
)
195 m_windowStyle
|= wxDIALOG_MODAL
;
197 wxModelessWindows
.DeleteObject(this);
201 m_windowStyle
&= ~wxDIALOG_MODAL
;
203 wxModelessWindows
.Append(this);
207 wxDialog::~wxDialog()
209 m_isBeingDeleted
= TRUE
;
211 wxTopLevelWindows
.DeleteObject(this);
213 // this will call BringWindowToTop() if necessary to bring back our parent
218 wxModelessWindows
.DeleteObject(this);
220 // If this is the last top-level window, exit.
221 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
223 wxTheApp
->SetTopWindow(NULL
);
225 if ( wxTheApp
->GetExitOnFrameDelete() )
227 ::PostQuitMessage(0);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 // By default, pressing escape cancels the dialog
237 void wxDialog::OnCharHook(wxKeyEvent
& event
)
241 if (event
.m_keyCode
== WXK_ESCAPE
)
243 // Behaviour changed in 2.0: we'll send a Cancel message
244 // to the dialog instead of Close.
245 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
246 cancelEvent
.SetEventObject( this );
247 GetEventHandler()->ProcessEvent(cancelEvent
);
249 // ensure that there is another message for this window so the
250 // ShowModal loop will exit and won't get stuck in GetMessage().
251 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
255 // We didn't process this event.
259 // ----------------------------------------------------------------------------
260 // Windows dialog boxes can't be iconized
261 // ----------------------------------------------------------------------------
263 void wxDialog::Iconize(bool WXUNUSED(iconize
))
267 bool wxDialog::IsIconized() const
272 // ----------------------------------------------------------------------------
273 // size/position handling
274 // ----------------------------------------------------------------------------
276 void wxDialog::DoSetClientSize(int width
, int height
)
278 HWND hWnd
= (HWND
) GetHWND();
280 ::GetClientRect(hWnd
, &rect
);
283 GetWindowRect(hWnd
, &rect2
);
285 // Find the difference between the entire window (title bar and all)
286 // and the client area; add this to the new client size to move the
288 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
289 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
291 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
293 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
294 event
.SetEventObject( this );
295 GetEventHandler()->ProcessEvent(event
);
298 void wxDialog::DoGetPosition(int *x
, int *y
) const
301 GetWindowRect(GetHwnd(), &rect
);
309 // ----------------------------------------------------------------------------
310 // showing the dialogs
311 // ----------------------------------------------------------------------------
313 bool wxDialog::IsModal() const
315 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
318 bool wxDialog::IsModalShowing() const
320 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
323 void wxDialog::DoShowModal()
325 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
327 wxModalDialogs
.Append(this);
329 wxWindow
*parent
= GetParent();
331 // remember where the focus was
338 m_oldFocus
= wxTheApp
->GetTopWindow();
341 // disable the parent window first
342 HWND hwndParent
= parent
? GetHwndOf(parent
) : (HWND
)NULL
;
345 ::EnableWindow(hwndParent
, FALSE
);
348 // enter the modal loop
349 while ( IsModalShowing() )
352 wxMutexGuiLeaveOrEnter();
353 #endif // wxUSE_THREADS
355 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
358 // a message came or no more idle processing to do
359 wxTheApp
->DoMessage();
362 // reenable the parent window if any
365 ::EnableWindow(hwndParent
, TRUE
);
369 if ( m_oldFocus
&& (m_oldFocus
!= this) )
371 m_oldFocus
->SetFocus();
375 bool wxDialog::Show(bool show
)
377 // The following is required when the parent has been disabled, (modal
378 // dialogs, or modeless dialogs with disabling such as wxProgressDialog).
379 // Otherwise the parent disappears behind other windows when the dialog is
383 wxWindow
*parent
= GetParent();
386 ::BringWindowToTop(GetHwndOf(parent
));
390 // ShowModal() may be called for already shown dialog
391 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
399 // usually will result in TransferDataToWindow() being called
409 else // end of modal dialog
411 // this will cause IsModalShowing() return FALSE and our local
412 // message loop will terminate
413 wxModalDialogs
.DeleteObject(this);
420 // Replacement for Show(TRUE) for modal dialogs - returns return code
421 int wxDialog::ShowModal()
423 m_windowStyle
|= wxDIALOG_MODAL
;
425 return GetReturnCode();
428 // NB: this function (surprizingly) may be called for both modal and modeless
429 // dialogs and should work for both of them
430 void wxDialog::EndModal(int retCode
)
432 SetReturnCode(retCode
);
437 // ----------------------------------------------------------------------------
438 // wxWin event handlers
439 // ----------------------------------------------------------------------------
442 void wxDialog::OnOK(wxCommandEvent
& event
)
444 if ( Validate() && TransferDataFromWindow() )
450 void wxDialog::OnApply(wxCommandEvent
& event
)
453 TransferDataFromWindow();
455 // TODO probably need to disable the Apply button until things change again
458 void wxDialog::OnCancel(wxCommandEvent
& event
)
460 EndModal(wxID_CANCEL
);
463 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
465 // We'll send a Cancel message by default, which may close the dialog.
466 // Check for looping if the Cancel event handler calls Close().
468 // Note that if a cancel button and handler aren't present in the dialog,
469 // nothing will happen when you close the dialog via the window manager, or
470 // via Close(). We wouldn't want to destroy the dialog by default, since
471 // the dialog may have been created on the stack. However, this does mean
472 // that calling dialog->Close() won't delete the dialog unless the handler
473 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
474 // destroy the dialog. The default OnCancel (above) simply ends a modal
475 // dialog, and hides a modeless dialog.
477 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
478 // lists here? don't dare to change it now, but should be done later!
479 static wxList closing
;
481 if ( closing
.Member(this) )
484 closing
.Append(this);
486 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
487 cancelEvent
.SetEventObject( this );
488 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
490 closing
.DeleteObject(this);
493 // Destroy the window (delayed, if a managed window)
494 bool wxDialog::Destroy()
496 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
497 _T("wxDialog destroyed twice") );
499 wxPendingDelete
.Append(this);
504 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
509 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
514 // ---------------------------------------------------------------------------
515 // dialog window proc
516 // ---------------------------------------------------------------------------
518 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
521 bool processed
= FALSE
;
526 // if we can't close, tell the system that we processed the
527 // message - otherwise it would close us
528 processed
= !Close();
532 // we want to override the busy cursor for modal dialogs:
533 // typically, wxBeginBusyCursor() is called and then a modal dialog
534 // is shown, but the modal dialog shouldn't have this cursor
544 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
551 // Define for each class of dialog and control
552 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
553 WXHWND
WXUNUSED(pWnd
),
554 WXUINT
WXUNUSED(nCtlColor
),
559 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
562 #endif // wxUSE_CTL3D