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