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 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
79 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
80 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
81 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
82 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
84 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
88 EVT_CLOSE(wxDialog::OnCloseWindow
)
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxDialog construction
97 // ----------------------------------------------------------------------------
101 m_oldFocus
= (wxWindow
*)NULL
;
104 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
107 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
108 const wxString
& title
,
112 const wxString
& name
)
114 m_oldFocus
= FindFocus();
116 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
120 wxTopLevelWindows
.Append(this);
122 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
124 if (parent
) parent
->AddChild(this);
127 m_windowId
= (int)NewControlId();
137 x
= wxDIALOG_DEFAULT_X
;
139 y
= wxDIALOG_DEFAULT_Y
;
141 m_windowStyle
= style
;
146 width
= wxDIALOG_DEFAULT_WIDTH
;
148 height
= wxDIALOG_DEFAULT_HEIGHT
;
150 // All dialogs should really have this style
151 m_windowStyle
|= wxTAB_TRAVERSAL
;
153 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
154 if (m_windowStyle
& wxSTAY_ON_TOP
)
155 extendedStyle
|= WS_EX_TOPMOST
;
157 // Allows creation of dialogs with & without captions under MSWindows,
158 // resizeable or not (but a resizeable dialog always has caption -
159 // otherwise it would look too strange)
161 if ( style
& wxRESIZE_BORDER
)
162 dlg
= wxT("wxResizeableDialog");
163 else if ( style
& wxCAPTION
)
164 dlg
= wxT("wxCaptionDialog");
166 dlg
= wxT("wxNoCaptionDialog");
167 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
169 0, // style is not used if we have dlg template
173 HWND hwnd
= (HWND
)GetHWND();
177 wxLogError(_("Failed to create dialog."));
182 SubclassWin(GetHWND());
184 SetWindowText(hwnd
, title
);
185 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
190 void wxDialog::SetModal(bool flag
)
194 m_windowStyle
|= wxDIALOG_MODAL
;
196 wxModelessWindows
.DeleteObject(this);
200 m_windowStyle
&= ~wxDIALOG_MODAL
;
202 wxModelessWindows
.Append(this);
206 wxDialog::~wxDialog()
208 m_isBeingDeleted
= TRUE
;
210 wxTopLevelWindows
.DeleteObject(this);
212 // this will call BringWindowToTop() if necessary to bring back our parent
217 wxModelessWindows
.DeleteObject(this);
219 // If this is the last top-level window, exit.
220 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
222 wxTheApp
->SetTopWindow(NULL
);
224 if ( wxTheApp
->GetExitOnFrameDelete() )
226 ::PostQuitMessage(0);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 // By default, pressing escape cancels the dialog
236 void wxDialog::OnCharHook(wxKeyEvent
& event
)
240 if (event
.m_keyCode
== WXK_ESCAPE
)
242 // Behaviour changed in 2.0: we'll send a Cancel message
243 // to the dialog instead of Close.
244 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
245 cancelEvent
.SetEventObject( this );
246 GetEventHandler()->ProcessEvent(cancelEvent
);
248 // ensure that there is another message for this window so the
249 // ShowModal loop will exit and won't get stuck in GetMessage().
250 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
254 // We didn't process this event.
258 // ----------------------------------------------------------------------------
259 // Windows dialog boxes can't be iconized
260 // ----------------------------------------------------------------------------
262 void wxDialog::Iconize(bool WXUNUSED(iconize
))
266 bool wxDialog::IsIconized() const
271 // ----------------------------------------------------------------------------
272 // size/position handling
273 // ----------------------------------------------------------------------------
275 void wxDialog::DoSetClientSize(int width
, int height
)
277 HWND hWnd
= (HWND
) GetHWND();
279 ::GetClientRect(hWnd
, &rect
);
282 GetWindowRect(hWnd
, &rect2
);
284 // Find the difference between the entire window (title bar and all)
285 // and the client area; add this to the new client size to move the
287 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
288 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
290 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
292 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
293 event
.SetEventObject( this );
294 GetEventHandler()->ProcessEvent(event
);
297 void wxDialog::DoGetPosition(int *x
, int *y
) const
300 GetWindowRect(GetHwnd(), &rect
);
308 // ----------------------------------------------------------------------------
309 // showing the dialogs
310 // ----------------------------------------------------------------------------
312 bool wxDialog::IsModal() const
314 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
317 bool wxDialog::IsModalShowing() const
319 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
322 void wxDialog::DoShowModal()
324 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
326 wxModalDialogs
.Append(this);
328 wxWindow
*parent
= GetParent();
330 // remember where the focus was
337 m_oldFocus
= wxTheApp
->GetTopWindow();
340 // disable the parent window first
341 HWND hwndParent
= parent
? GetHwndOf(parent
) : (HWND
)NULL
;
344 ::EnableWindow(hwndParent
, FALSE
);
347 // enter the modal loop
348 while ( IsModalShowing() )
351 wxMutexGuiLeaveOrEnter();
352 #endif // wxUSE_THREADS
354 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
357 // a message came or no more idle processing to do
358 wxTheApp
->DoMessage();
361 // reenable the parent window if any
364 ::EnableWindow(hwndParent
, TRUE
);
368 if ( m_oldFocus
&& (m_oldFocus
!= this) )
370 m_oldFocus
->SetFocus();
374 bool wxDialog::Show(bool show
)
376 // The following is required when the parent has been disabled, (modal
377 // dialogs, or modeless dialogs with disabling such as wxProgressDialog).
378 // Otherwise the parent disappears behind other windows when the dialog is
382 wxWindow
*parent
= GetParent();
385 ::BringWindowToTop(GetHwndOf(parent
));
389 // ShowModal() may be called for already shown dialog
390 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
398 // usually will result in TransferDataToWindow() being called
408 else // end of modal dialog
410 // this will cause IsModalShowing() return FALSE and our local
411 // message loop will terminate
412 wxModalDialogs
.DeleteObject(this);
419 // Replacement for Show(TRUE) for modal dialogs - returns return code
420 int wxDialog::ShowModal()
422 m_windowStyle
|= wxDIALOG_MODAL
;
424 return GetReturnCode();
427 // NB: this function (surprizingly) may be called for both modal and modeless
428 // dialogs and should work for both of them
429 void wxDialog::EndModal(int retCode
)
431 SetReturnCode(retCode
);
436 // ----------------------------------------------------------------------------
437 // wxWin event handlers
438 // ----------------------------------------------------------------------------
441 void wxDialog::OnOK(wxCommandEvent
& event
)
443 if ( Validate() && TransferDataFromWindow() )
449 void wxDialog::OnApply(wxCommandEvent
& event
)
452 TransferDataFromWindow();
454 // TODO probably need to disable the Apply button until things change again
457 void wxDialog::OnCancel(wxCommandEvent
& event
)
459 EndModal(wxID_CANCEL
);
462 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
464 // We'll send a Cancel message by default, which may close the dialog.
465 // Check for looping if the Cancel event handler calls Close().
467 // Note that if a cancel button and handler aren't present in the dialog,
468 // nothing will happen when you close the dialog via the window manager, or
469 // via Close(). We wouldn't want to destroy the dialog by default, since
470 // the dialog may have been created on the stack. However, this does mean
471 // that calling dialog->Close() won't delete the dialog unless the handler
472 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
473 // destroy the dialog. The default OnCancel (above) simply ends a modal
474 // dialog, and hides a modeless dialog.
476 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
477 // lists here? don't dare to change it now, but should be done later!
478 static wxList closing
;
480 if ( closing
.Member(this) )
483 closing
.Append(this);
485 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
486 cancelEvent
.SetEventObject( this );
487 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
489 closing
.DeleteObject(this);
492 // Destroy the window (delayed, if a managed window)
493 bool wxDialog::Destroy()
495 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
496 _T("wxDialog destroyed twice") );
498 wxPendingDelete
.Append(this);
503 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
508 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
513 // ---------------------------------------------------------------------------
514 // dialog window proc
515 // ---------------------------------------------------------------------------
517 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
520 bool processed
= FALSE
;
525 // if we can't close, tell the system that we processed the
526 // message - otherwise it would close us
527 processed
= !Close();
531 // we want to override the busy cursor for modal dialogs:
532 // typically, wxBeginBusyCursor() is called and then a modal dialog
533 // is shown, but the modal dialog shouldn't have this cursor
543 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
550 // Define for each class of dialog and control
551 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
552 WXHWND
WXUNUSED(pWnd
),
553 WXUINT
WXUNUSED(nCtlColor
),
558 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
561 #endif // wxUSE_CTL3D