]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dialog.cpp
don't crash in wxGetWindowXXX() if HWND is 0
[wxWidgets.git] / src / msw / dialog.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/dialog.cpp
3// Purpose: wxDialog class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "dialog.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/dialog.h"
33 #include "wx/utils.h"
34 #include "wx/frame.h"
35 #include "wx/app.h"
36 #include "wx/settings.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39#endif
40
41#include "wx/msw/private.h"
42#include "wx/log.h"
43
44#if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
45 #include <commdlg.h>
46#endif
47
48// ----------------------------------------------------------------------------
49// constants
50// ----------------------------------------------------------------------------
51
52// default dialog pos and size
53
54#define wxDIALOG_DEFAULT_X 300
55#define wxDIALOG_DEFAULT_Y 300
56
57#define wxDIALOG_DEFAULT_WIDTH 500
58#define wxDIALOG_DEFAULT_HEIGHT 500
59
60// ----------------------------------------------------------------------------
61// globals
62// ----------------------------------------------------------------------------
63
64// all modal dialogs currently shown
65static wxWindowList wxModalDialogs;
66
67// ----------------------------------------------------------------------------
68// wxWin macros
69// ----------------------------------------------------------------------------
70
71IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
72
73BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
74 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
75 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
76 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
77
78 EVT_CHAR_HOOK(wxDialog::OnCharHook)
79
80 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
81
82 EVT_CLOSE(wxDialog::OnCloseWindow)
83END_EVENT_TABLE()
84
85// ============================================================================
86// implementation
87// ============================================================================
88
89// ----------------------------------------------------------------------------
90// wxDialog construction
91// ----------------------------------------------------------------------------
92
93void wxDialog::Init()
94{
95 m_oldFocus = (wxWindow *)NULL;
96
97 m_isShown = FALSE;
98
99 m_windowDisabler = (wxWindowDisabler *)NULL;
100
101 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
102}
103
104bool wxDialog::Create(wxWindow *parent,
105 wxWindowID id,
106 const wxString& title,
107 const wxPoint& pos,
108 const wxSize& size,
109 long style,
110 const wxString& name)
111{
112 Init();
113
114 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
115
116 // save focus before doing anything which can potentially change it
117 m_oldFocus = FindFocus();
118
119 // All dialogs should really have this style
120 style |= wxTAB_TRAVERSAL;
121
122 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
123 return FALSE;
124
125 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
126
127 return TRUE;
128}
129
130void wxDialog::SetModal(bool flag)
131{
132 if ( flag )
133 {
134 m_windowStyle |= wxDIALOG_MODAL;
135
136 wxModelessWindows.DeleteObject(this);
137 }
138 else
139 {
140 m_windowStyle &= ~wxDIALOG_MODAL;
141
142 wxModelessWindows.Append(this);
143 }
144}
145
146wxDialog::~wxDialog()
147{
148 m_isBeingDeleted = TRUE;
149
150 // this will also reenable all the other windows for a modal dialog
151 Show(FALSE);
152}
153
154// ----------------------------------------------------------------------------
155// kbd handling
156// ----------------------------------------------------------------------------
157
158// By default, pressing escape cancels the dialog
159void wxDialog::OnCharHook(wxKeyEvent& event)
160{
161 if (GetHWND())
162 {
163 // "Esc" works as an accelerator for the "Cancel" button, but it
164 // shouldn't close the dialog which doesn't have any cancel button
165 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
166 {
167 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
168 cancelEvent.SetEventObject( this );
169 GetEventHandler()->ProcessEvent(cancelEvent);
170
171 // ensure that there is another message for this window so the
172 // ShowModal loop will exit and won't get stuck in GetMessage().
173 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
174
175 return;
176 }
177 }
178
179 // We didn't process this event.
180 event.Skip();
181}
182
183// ----------------------------------------------------------------------------
184// showing the dialogs
185// ----------------------------------------------------------------------------
186
187bool wxDialog::IsModal() const
188{
189 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
190}
191
192bool wxDialog::IsModalShowing() const
193{
194 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
195}
196
197void wxDialog::DoShowModal()
198{
199 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
200 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
201
202 wxModalDialogs.Append(this);
203
204 wxWindow *parent = GetParent();
205
206 wxWindow* oldFocus = m_oldFocus;
207
208 // We have to remember the HWND because we need to check
209 // the HWND still exists (oldFocus can be garbage when the dialog
210 // exits, if it has been destroyed)
211 HWND hwndOldFocus = 0;
212 if (oldFocus)
213 hwndOldFocus = (HWND) oldFocus->GetHWND();
214
215 // remember where the focus was
216 if ( !oldFocus )
217 {
218 oldFocus = parent;
219 if ( parent )
220 hwndOldFocus = GetHwndOf(parent);
221 }
222
223 // disable all other app windows
224 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
225
226 m_windowDisabler = new wxWindowDisabler(this);
227
228 // enter the modal loop
229 while ( IsModalShowing() )
230 {
231#if wxUSE_THREADS
232 wxMutexGuiLeaveOrEnter();
233#endif // wxUSE_THREADS
234
235 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
236 ;
237
238 // a message came or no more idle processing to do
239 wxTheApp->DoMessage();
240 }
241
242 // and restore focus
243 // Note that this code MUST NOT access the dialog object's data
244 // in case the object has been deleted (which will be the case
245 // for a modal dialog that has been destroyed before calling EndModal).
246 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
247 {
248 // This is likely to prove that the object still exists
249 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
250 oldFocus->SetFocus();
251 }
252}
253
254bool wxDialog::Show(bool show)
255{
256 if ( !show )
257 {
258 // if we had disabled other app windows, reenable them back now because
259 // if they stay disabled Windows will activate another window (one
260 // which is enabled, anyhow) and we will lose activation
261 if ( m_windowDisabler )
262 {
263 delete m_windowDisabler;
264 m_windowDisabler = NULL;
265 }
266 }
267
268 // ShowModal() may be called for already shown dialog
269 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
270 {
271 // nothing to do
272 return FALSE;
273 }
274
275 if ( show )
276 {
277 // usually will result in TransferDataToWindow() being called
278 InitDialog();
279 }
280
281 if ( IsModal() )
282 {
283 if ( show )
284 {
285 // modal dialog needs a parent window, so try to find one
286 if ( !GetParent() )
287 {
288 wxWindow *parent = wxTheApp->GetTopWindow();
289 if ( parent && parent != this && parent->IsShown() )
290 {
291 // use it
292 m_parent = parent;
293 }
294 }
295
296 DoShowModal();
297 }
298 else // end of modal dialog
299 {
300 // this will cause IsModalShowing() return FALSE and our local
301 // message loop will terminate
302 wxModalDialogs.DeleteObject(this);
303 }
304 }
305
306 return TRUE;
307}
308
309// a special version for Show(TRUE) for modal dialogs which returns return code
310int wxDialog::ShowModal()
311{
312 if ( !IsModal() )
313 {
314 SetModal(TRUE);
315 }
316
317 Show(TRUE);
318
319 return GetReturnCode();
320}
321
322// NB: this function (surprizingly) may be called for both modal and modeless
323// dialogs and should work for both of them
324void wxDialog::EndModal(int retCode)
325{
326 SetReturnCode(retCode);
327
328 Show(FALSE);
329}
330
331// ----------------------------------------------------------------------------
332// wxWin event handlers
333// ----------------------------------------------------------------------------
334
335// Standard buttons
336void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
337{
338 if ( Validate() && TransferDataFromWindow() )
339 {
340 EndModal(wxID_OK);
341 }
342}
343
344void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
345{
346 if ( Validate() )
347 TransferDataFromWindow();
348
349 // TODO probably need to disable the Apply button until things change again
350}
351
352void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
353{
354 EndModal(wxID_CANCEL);
355}
356
357void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
358{
359 // We'll send a Cancel message by default, which may close the dialog.
360 // Check for looping if the Cancel event handler calls Close().
361
362 // Note that if a cancel button and handler aren't present in the dialog,
363 // nothing will happen when you close the dialog via the window manager, or
364 // via Close(). We wouldn't want to destroy the dialog by default, since
365 // the dialog may have been created on the stack. However, this does mean
366 // that calling dialog->Close() won't delete the dialog unless the handler
367 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
368 // destroy the dialog. The default OnCancel (above) simply ends a modal
369 // dialog, and hides a modeless dialog.
370
371 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
372 // lists here? don't dare to change it now, but should be done later!
373 static wxList closing;
374
375 if ( closing.Member(this) )
376 return;
377
378 closing.Append(this);
379
380 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
381 cancelEvent.SetEventObject( this );
382 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
383
384 closing.DeleteObject(this);
385}
386
387void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
388{
389#if wxUSE_CTL3D
390 Ctl3dColorChange();
391#else
392 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
393 Refresh();
394#endif
395}
396
397// ---------------------------------------------------------------------------
398// dialog window proc
399// ---------------------------------------------------------------------------
400
401long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
402{
403 long rc = 0;
404 bool processed = FALSE;
405
406 switch ( message )
407 {
408#if 0 // now that we got owner window right it doesn't seem to be needed
409 case WM_ACTIVATE:
410 switch ( LOWORD(wParam) )
411 {
412 case WA_ACTIVE:
413 case WA_CLICKACTIVE:
414 if ( IsModalShowing() && GetParent() )
415 {
416 // bring the owner window to top as the standard dialog
417 // boxes do
418 if ( !::SetWindowPos
419 (
420 GetHwndOf(GetParent()),
421 GetHwnd(),
422 0, 0,
423 0, 0,
424 SWP_NOACTIVATE |
425 SWP_NOMOVE |
426 SWP_NOSIZE
427 ) )
428 {
429 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
430 }
431 }
432 // fall through to process it normally as well
433 }
434 break;
435#endif // 0
436
437 case WM_CLOSE:
438 // if we can't close, tell the system that we processed the
439 // message - otherwise it would close us
440 processed = !Close();
441 break;
442
443#ifndef __WXMICROWIN__
444 case WM_SETCURSOR:
445 // we want to override the busy cursor for modal dialogs:
446 // typically, wxBeginBusyCursor() is called and then a modal dialog
447 // is shown, but the modal dialog shouldn't have hourglass cursor
448 if ( IsModalShowing() && wxIsBusy() )
449 {
450 // set our cursor for all windows (but see below)
451 wxCursor cursor = m_cursor;
452 if ( !cursor.Ok() )
453 cursor = wxCURSOR_ARROW;
454
455 ::SetCursor(GetHcursorOf(cursor));
456
457 // in any case, stop here and don't let wxWindow process this
458 // message (it would set the busy cursor)
459 processed = TRUE;
460
461 // but return FALSE to tell the child window (if the event
462 // comes from one of them and not from ourselves) that it can
463 // set its own cursor if it has one: thus, standard controls
464 // (e.g. text ctrl) still have correct cursors in a dialog
465 // invoked while wxIsBusy()
466 rc = FALSE;
467 }
468 break;
469#endif // __WXMICROWIN__
470 }
471
472 if ( !processed )
473 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
474
475 return rc;
476}
477
478#if wxUSE_CTL3D
479
480// Define for each class of dialog and control
481WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
482 WXHWND WXUNUSED(pWnd),
483 WXUINT WXUNUSED(nCtlColor),
484 WXUINT message,
485 WXWPARAM wParam,
486 WXLPARAM lParam)
487{
488 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
489}
490
491#endif // wxUSE_CTL3D
492