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