]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
corrected path format for wxMac compiled with Apple DevTools
[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;
325 }
326 }
327
b6c588e1
VZ
328 DoShowModal();
329 }
330 else // end of modal dialog
331 {
332 // this will cause IsModalShowing() return FALSE and our local
333 // message loop will terminate
334 wxModalDialogs.DeleteObject(this);
335 }
336 }
2bda0e17 337
b6c588e1 338 return TRUE;
2bda0e17
KB
339}
340
f6bcfd97 341// a special version for Show(TRUE) for modal dialogs which returns return code
a23fd0e1 342int wxDialog::ShowModal()
2bda0e17 343{
f6bcfd97 344 if ( !IsModal() )
5e1febfa 345 {
f6bcfd97 346 SetModal(TRUE);
5e1febfa
VZ
347 }
348
b6c588e1 349 Show(TRUE);
5e1febfa 350
b6c588e1 351 return GetReturnCode();
2bda0e17
KB
352}
353
b6c588e1
VZ
354// NB: this function (surprizingly) may be called for both modal and modeless
355// dialogs and should work for both of them
2bda0e17
KB
356void wxDialog::EndModal(int retCode)
357{
b6c588e1 358 SetReturnCode(retCode);
6a088435 359
b6c588e1 360 Show(FALSE);
2bda0e17
KB
361}
362
b6c588e1
VZ
363// ----------------------------------------------------------------------------
364// wxWin event handlers
365// ----------------------------------------------------------------------------
2bda0e17
KB
366
367// Standard buttons
33ac7e6f 368void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
2bda0e17 369{
dc1c4b62
VZ
370 if ( Validate() && TransferDataFromWindow() )
371 {
b6c588e1 372 EndModal(wxID_OK);
dc1c4b62 373 }
2bda0e17
KB
374}
375
33ac7e6f 376void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
2bda0e17 377{
b6c588e1
VZ
378 if ( Validate() )
379 TransferDataFromWindow();
380
381 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
382}
383
33ac7e6f 384void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
2bda0e17 385{
b6c588e1 386 EndModal(wxID_CANCEL);
2bda0e17
KB
387}
388
33ac7e6f 389void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
2bda0e17 390{
b6c588e1 391 // We'll send a Cancel message by default, which may close the dialog.
e3065973
JS
392 // Check for looping if the Cancel event handler calls Close().
393
394 // Note that if a cancel button and handler aren't present in the dialog,
395 // nothing will happen when you close the dialog via the window manager, or
b6c588e1
VZ
396 // via Close(). We wouldn't want to destroy the dialog by default, since
397 // the dialog may have been created on the stack. However, this does mean
398 // that calling dialog->Close() won't delete the dialog unless the handler
399 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
400 // destroy the dialog. The default OnCancel (above) simply ends a modal
401 // dialog, and hides a modeless dialog.
402
403 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
404 // lists here? don't dare to change it now, but should be done later!
2bda0e17 405 static wxList closing;
bd9d76cb 406
2bda0e17 407 if ( closing.Member(this) )
e3065973 408 return;
bd9d76cb 409
2bda0e17 410 closing.Append(this);
bd9d76cb 411
387a3b02
JS
412 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
413 cancelEvent.SetEventObject( this );
e3065973 414 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
2bda0e17
KB
415
416 closing.DeleteObject(this);
e3065973 417}
2bda0e17 418
33ac7e6f 419void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
2bda0e17 420{
1f112209 421#if wxUSE_CTL3D
b6c588e1 422 Ctl3dColorChange();
2bda0e17 423#else
b6c588e1
VZ
424 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
425 Refresh();
2bda0e17 426#endif
68ad65f8 427}
42e69d6b
VZ
428
429// ---------------------------------------------------------------------------
430// dialog window proc
431// ---------------------------------------------------------------------------
432
433long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
434{
435 long rc = 0;
436 bool processed = FALSE;
437
438 switch ( message )
439 {
b0a6bb75 440#if 0 // now that we got owner window right it doesn't seem to be needed
5e1febfa
VZ
441 case WM_ACTIVATE:
442 switch ( LOWORD(wParam) )
443 {
444 case WA_ACTIVE:
445 case WA_CLICKACTIVE:
446 if ( IsModalShowing() && GetParent() )
447 {
448 // bring the owner window to top as the standard dialog
449 // boxes do
450 if ( !::SetWindowPos
451 (
452 GetHwndOf(GetParent()),
453 GetHwnd(),
454 0, 0,
455 0, 0,
456 SWP_NOACTIVATE |
457 SWP_NOMOVE |
458 SWP_NOSIZE
459 ) )
460 {
f6bcfd97 461 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
5e1febfa
VZ
462 }
463 }
464 // fall through to process it normally as well
465 }
466 break;
b0a6bb75 467#endif // 0
5e1febfa 468
42e69d6b
VZ
469 case WM_CLOSE:
470 // if we can't close, tell the system that we processed the
471 // message - otherwise it would close us
472 processed = !Close();
473 break;
abceee76 474
04ef50df 475#ifndef __WXMICROWIN__
abceee76
VZ
476 case WM_SETCURSOR:
477 // we want to override the busy cursor for modal dialogs:
478 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 479 // is shown, but the modal dialog shouldn't have hourglass cursor
d1477745 480 if ( IsModalShowing() && wxIsBusy() )
abceee76 481 {
bfbd6dc1
VZ
482 // set our cursor for all windows (but see below)
483 wxCursor cursor = m_cursor;
484 if ( !cursor.Ok() )
485 cursor = wxCURSOR_ARROW;
abceee76 486
bfbd6dc1
VZ
487 ::SetCursor(GetHcursorOf(cursor));
488
489 // in any case, stop here and don't let wxWindow process this
490 // message (it would set the busy cursor)
abceee76 491 processed = TRUE;
bfbd6dc1
VZ
492
493 // but return FALSE to tell the child window (if the event
494 // comes from one of them and not from ourselves) that it can
495 // set its own cursor if it has one: thus, standard controls
496 // (e.g. text ctrl) still have correct cursors in a dialog
497 // invoked while wxIsBusy()
498 rc = FALSE;
abceee76 499 }
bfbd6dc1 500 break;
82c9f85c 501#endif // __WXMICROWIN__
42e69d6b
VZ
502 }
503
504 if ( !processed )
505 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
506
507 return rc;
508}
b6c588e1
VZ
509
510#if wxUSE_CTL3D
511
512// Define for each class of dialog and control
513WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
514 WXHWND WXUNUSED(pWnd),
515 WXUINT WXUNUSED(nCtlColor),
516 WXUINT message,
517 WXWPARAM wParam,
518 WXLPARAM lParam)
519{
520 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
521}
522
523#endif // wxUSE_CTL3D
524