]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
warnings in wxCHeCK_MSG fixed
[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
64// all objects to be deleted during next idle processing - from window.cpp
cde9f08e 65extern wxList WXDLLEXPORT wxPendingDelete;
2bda0e17 66
b6c588e1
VZ
67// all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68wxWindowList wxModelessWindows;
69
70// all modal dialogs currently shown
71static wxWindowList wxModalDialogs;
72
73// ----------------------------------------------------------------------------
74// wxWin macros
75// ----------------------------------------------------------------------------
76
1b6452df
VZ
77IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
78
79BEGIN_EVENT_TABLE(wxDialog, wxPanel)
1b6452df
VZ
80 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
81 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
82 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
b6c588e1 83
1b6452df 84 EVT_CHAR_HOOK(wxDialog::OnCharHook)
b6c588e1 85
1b6452df 86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
b6c588e1 87
1b6452df
VZ
88 EVT_CLOSE(wxDialog::OnCloseWindow)
89END_EVENT_TABLE()
2bda0e17 90
b6c588e1
VZ
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wxDialog construction
97// ----------------------------------------------------------------------------
98
a23fd0e1 99wxDialog::wxDialog()
2bda0e17 100{
52a07708 101 m_oldFocus = (wxWindow *)NULL;
b6c588e1 102 m_isShown = FALSE;
2bda0e17 103
b6c588e1 104 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
105}
106
debe6624 107bool wxDialog::Create(wxWindow *parent, wxWindowID id,
462e2437
VZ
108 const wxString& title,
109 const wxPoint& pos,
110 const wxSize& size,
111 long style,
112 const wxString& name)
2bda0e17 113{
abceee76
VZ
114 m_oldFocus = FindFocus();
115
462e2437
VZ
116 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
117 SetName(name);
bd9d76cb 118
462e2437
VZ
119 if (!parent)
120 wxTopLevelWindows.Append(this);
2bda0e17 121
462e2437 122 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
2bda0e17 123
462e2437 124 if (parent) parent->AddChild(this);
2bda0e17 125
462e2437
VZ
126 if ( id == -1 )
127 m_windowId = (int)NewControlId();
128 else
129 m_windowId = id;
130
131 int x = pos.x;
132 int y = pos.y;
133 int width = size.x;
134 int height = size.y;
135
b6c588e1
VZ
136 if (x < 0)
137 x = wxDIALOG_DEFAULT_X;
138 if (y < 0)
139 y = wxDIALOG_DEFAULT_Y;
462e2437
VZ
140
141 m_windowStyle = style;
142
143 m_isShown = FALSE;
462e2437
VZ
144
145 if (width < 0)
b6c588e1 146 width = wxDIALOG_DEFAULT_WIDTH;
462e2437 147 if (height < 0)
b6c588e1 148 height = wxDIALOG_DEFAULT_HEIGHT;
462e2437 149
706bb5f9
JS
150 // All dialogs should really have this style
151 m_windowStyle |= wxTAB_TRAVERSAL;
152
462e2437
VZ
153 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
154 if (m_windowStyle & wxSTAY_ON_TOP)
155 extendedStyle |= WS_EX_TOPMOST;
156
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)
837e5743 160 const wxChar *dlg;
257bf510 161 if ( style & wxRESIZE_BORDER )
223d09f6 162 dlg = wxT("wxResizeableDialog");
462e2437 163 else if ( style & wxCAPTION )
223d09f6 164 dlg = wxT("wxCaptionDialog");
462e2437 165 else
223d09f6 166 dlg = wxT("wxNoCaptionDialog");
462e2437
VZ
167 MSWCreate(m_windowId, parent, NULL, this, NULL,
168 x, y, width, height,
169 0, // style is not used if we have dlg template
170 dlg,
171 extendedStyle);
2bda0e17 172
462e2437 173 HWND hwnd = (HWND)GetHWND();
2bda0e17 174
462e2437
VZ
175 if ( !hwnd )
176 {
68ad65f8 177 wxLogError(_("Failed to create dialog."));
2bda0e17 178
462e2437
VZ
179 return FALSE;
180 }
2bda0e17 181
462e2437 182 SubclassWin(GetHWND());
bd9d76cb 183
462e2437
VZ
184 SetWindowText(hwnd, title);
185 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 186
462e2437 187 return TRUE;
2bda0e17
KB
188}
189
debe6624 190void wxDialog::SetModal(bool flag)
2bda0e17 191{
b6c588e1
VZ
192 if ( flag )
193 {
194 m_windowStyle |= wxDIALOG_MODAL;
195
196 wxModelessWindows.DeleteObject(this);
197 }
198 else
199 {
200 m_windowStyle &= ~wxDIALOG_MODAL;
201
202 wxModelessWindows.Append(this);
203 }
2bda0e17
KB
204}
205
206wxDialog::~wxDialog()
207{
b6c588e1 208 m_isBeingDeleted = TRUE;
2bda0e17 209
b6c588e1 210 wxTopLevelWindows.DeleteObject(this);
2bda0e17 211
abceee76
VZ
212 // this will call BringWindowToTop() if necessary to bring back our parent
213 // window to top
b6c588e1 214 Show(FALSE);
edccf428 215
b6c588e1
VZ
216 if ( !IsModal() )
217 wxModelessWindows.DeleteObject(this);
2bda0e17 218
b6c588e1
VZ
219 // If this is the last top-level window, exit.
220 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
2bda0e17 221 {
b6c588e1
VZ
222 wxTheApp->SetTopWindow(NULL);
223
224 if ( wxTheApp->GetExitOnFrameDelete() )
225 {
226 ::PostQuitMessage(0);
227 }
2bda0e17 228 }
2bda0e17
KB
229}
230
b6c588e1
VZ
231// ----------------------------------------------------------------------------
232// kbd handling
233// ----------------------------------------------------------------------------
234
2bda0e17
KB
235// By default, pressing escape cancels the dialog
236void wxDialog::OnCharHook(wxKeyEvent& event)
237{
238 if (GetHWND())
239 {
240 if (event.m_keyCode == WXK_ESCAPE)
241 {
2a47d3c1
JS
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);
2bda0e17 247
995a594a
RD
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);
2a47d3c1 251 return;
2bda0e17
KB
252 }
253 }
254 // We didn't process this event.
255 event.Skip();
256}
257
b6c588e1
VZ
258// ----------------------------------------------------------------------------
259// Windows dialog boxes can't be iconized
260// ----------------------------------------------------------------------------
2bda0e17 261
debe6624 262void wxDialog::Iconize(bool WXUNUSED(iconize))
2bda0e17 263{
2bda0e17
KB
264}
265
a23fd0e1 266bool wxDialog::IsIconized() const
2bda0e17 267{
b6c588e1 268 return FALSE;
2bda0e17
KB
269}
270
b6c588e1
VZ
271// ----------------------------------------------------------------------------
272// size/position handling
273// ----------------------------------------------------------------------------
274
721b32e0 275void wxDialog::DoSetClientSize(int width, int height)
2bda0e17 276{
b6c588e1
VZ
277 HWND hWnd = (HWND) GetHWND();
278 RECT rect;
279 ::GetClientRect(hWnd, &rect);
2bda0e17 280
b6c588e1
VZ
281 RECT rect2;
282 GetWindowRect(hWnd, &rect2);
2bda0e17 283
b6c588e1
VZ
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
286 // window
287 int actual_width = rect2.right - rect2.left - rect.right + width;
288 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
2bda0e17 289
b6c588e1 290 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
debe6624 291
b6c588e1
VZ
292 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
293 event.SetEventObject( this );
294 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
295}
296
b6c588e1 297void wxDialog::DoGetPosition(int *x, int *y) const
2bda0e17 298{
b6c588e1
VZ
299 RECT rect;
300 GetWindowRect(GetHwnd(), &rect);
2bda0e17 301
b6c588e1
VZ
302 if ( x )
303 *x = rect.left;
304 if ( y )
305 *y = rect.top;
2bda0e17
KB
306}
307
b6c588e1
VZ
308// ----------------------------------------------------------------------------
309// showing the dialogs
310// ----------------------------------------------------------------------------
22cf5fec
VZ
311
312bool wxDialog::IsModal() const
313{
b6c588e1 314 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
2bda0e17
KB
315}
316
b6c588e1 317bool wxDialog::IsModalShowing() const
2bda0e17 318{
b6c588e1 319 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
2bda0e17
KB
320}
321
b6c588e1 322void wxDialog::DoShowModal()
2bda0e17 323{
b6c588e1
VZ
324 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
325
326 wxModalDialogs.Append(this);
327
328 wxWindow *parent = GetParent();
329
330 // remember where the focus was
abceee76 331 if ( !m_oldFocus )
b6c588e1 332 {
abceee76 333 m_oldFocus = parent;
b6c588e1 334 }
abceee76 335 if ( !m_oldFocus )
b6c588e1 336 {
abceee76 337 m_oldFocus = wxTheApp->GetTopWindow();
b6c588e1
VZ
338 }
339
340 // disable the parent window first
341 HWND hwndParent = parent ? GetHwndOf(parent) : (HWND)NULL;
342 if ( hwndParent )
343 {
344 ::EnableWindow(hwndParent, FALSE);
345 }
346
347 // enter the modal loop
348 while ( IsModalShowing() )
349 {
350#if wxUSE_THREADS
351 wxMutexGuiLeaveOrEnter();
352#endif // wxUSE_THREADS
353
354 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
355 ;
356
357 // a message came or no more idle processing to do
358 wxTheApp->DoMessage();
359 }
360
361 // reenable the parent window if any
362 if ( hwndParent )
363 {
364 ::EnableWindow(hwndParent, TRUE);
365 }
366
367 // and restore focus
abceee76 368 if ( m_oldFocus && (m_oldFocus != this) )
b6c588e1 369 {
abceee76 370 m_oldFocus->SetFocus();
b6c588e1 371 }
2bda0e17
KB
372}
373
b6c588e1 374bool wxDialog::Show(bool show)
2bda0e17 375{
abceee76
VZ
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
379 // hidden.
380 if ( !show )
86ad564e 381 {
abceee76
VZ
382 wxWindow *parent = GetParent();
383 if ( parent )
86ad564e 384 {
abceee76 385 ::BringWindowToTop(GetHwndOf(parent));
86ad564e
JS
386 }
387 }
388
abceee76
VZ
389 // ShowModal() may be called for already shown dialog
390 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
b6c588e1
VZ
391 {
392 // nothing to do
393 return FALSE;
394 }
2bda0e17 395
b6c588e1
VZ
396 if ( show )
397 {
398 // usually will result in TransferDataToWindow() being called
399 InitDialog();
400 }
2bda0e17 401
b6c588e1
VZ
402 if ( IsModal() )
403 {
404 if ( show )
405 {
406 DoShowModal();
407 }
408 else // end of modal dialog
409 {
410 // this will cause IsModalShowing() return FALSE and our local
411 // message loop will terminate
412 wxModalDialogs.DeleteObject(this);
413 }
414 }
2bda0e17 415
b6c588e1 416 return TRUE;
2bda0e17
KB
417}
418
419// Replacement for Show(TRUE) for modal dialogs - returns return code
a23fd0e1 420int wxDialog::ShowModal()
2bda0e17 421{
5e1febfa
VZ
422 // modal dialog needs a parent window, so try to find one
423 if ( !GetParent() )
424 {
425 wxWindow *parent = wxTheApp->GetTopWindow();
426 if ( parent && parent != this )
427 {
428 // use it
429 m_parent = parent;
430 }
431 }
432
433 wxWindowDisabler *wd = (wxWindowDisabler *)NULL;
434 if ( !GetParent() )
435 {
436 // still no parent? make the dialog app modal by disabling all windows
437 wd = new wxWindowDisabler(this);
438 }
439
b6c588e1
VZ
440 m_windowStyle |= wxDIALOG_MODAL;
441 Show(TRUE);
5e1febfa
VZ
442
443 delete wd;
444
b6c588e1 445 return GetReturnCode();
2bda0e17
KB
446}
447
b6c588e1
VZ
448// NB: this function (surprizingly) may be called for both modal and modeless
449// dialogs and should work for both of them
2bda0e17
KB
450void wxDialog::EndModal(int retCode)
451{
b6c588e1 452 SetReturnCode(retCode);
6a088435 453
b6c588e1 454 Show(FALSE);
2bda0e17
KB
455}
456
b6c588e1
VZ
457// ----------------------------------------------------------------------------
458// wxWin event handlers
459// ----------------------------------------------------------------------------
2bda0e17
KB
460
461// Standard buttons
462void wxDialog::OnOK(wxCommandEvent& event)
463{
dc1c4b62
VZ
464 if ( Validate() && TransferDataFromWindow() )
465 {
b6c588e1 466 EndModal(wxID_OK);
dc1c4b62 467 }
2bda0e17
KB
468}
469
470void wxDialog::OnApply(wxCommandEvent& event)
471{
b6c588e1
VZ
472 if ( Validate() )
473 TransferDataFromWindow();
474
475 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
476}
477
478void wxDialog::OnCancel(wxCommandEvent& event)
479{
b6c588e1 480 EndModal(wxID_CANCEL);
2bda0e17
KB
481}
482
e3065973 483void wxDialog::OnCloseWindow(wxCloseEvent& event)
2bda0e17 484{
b6c588e1 485 // We'll send a Cancel message by default, which may close the dialog.
e3065973
JS
486 // Check for looping if the Cancel event handler calls Close().
487
488 // Note that if a cancel button and handler aren't present in the dialog,
489 // nothing will happen when you close the dialog via the window manager, or
b6c588e1
VZ
490 // via Close(). We wouldn't want to destroy the dialog by default, since
491 // the dialog may have been created on the stack. However, this does mean
492 // that calling dialog->Close() won't delete the dialog unless the handler
493 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
494 // destroy the dialog. The default OnCancel (above) simply ends a modal
495 // dialog, and hides a modeless dialog.
496
497 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
498 // lists here? don't dare to change it now, but should be done later!
2bda0e17 499 static wxList closing;
bd9d76cb 500
2bda0e17 501 if ( closing.Member(this) )
e3065973 502 return;
bd9d76cb 503
2bda0e17 504 closing.Append(this);
bd9d76cb 505
387a3b02
JS
506 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
507 cancelEvent.SetEventObject( this );
e3065973 508 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
2bda0e17
KB
509
510 closing.DeleteObject(this);
e3065973 511}
2bda0e17 512
e3065973 513// Destroy the window (delayed, if a managed window)
a23fd0e1 514bool wxDialog::Destroy()
e3065973 515{
b6c588e1
VZ
516 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
517 _T("wxDialog destroyed twice") );
518
e3065973 519 wxPendingDelete.Append(this);
2bda0e17 520
b6c588e1 521 return TRUE;
94b49b93
JS
522}
523
2bda0e17
KB
524void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
525{
1f112209 526#if wxUSE_CTL3D
b6c588e1 527 Ctl3dColorChange();
2bda0e17 528#else
b6c588e1
VZ
529 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
530 Refresh();
2bda0e17 531#endif
68ad65f8 532}
42e69d6b
VZ
533
534// ---------------------------------------------------------------------------
535// dialog window proc
536// ---------------------------------------------------------------------------
537
538long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
539{
540 long rc = 0;
541 bool processed = FALSE;
542
543 switch ( message )
544 {
5e1febfa
VZ
545 case WM_ACTIVATE:
546 switch ( LOWORD(wParam) )
547 {
548 case WA_ACTIVE:
549 case WA_CLICKACTIVE:
550 if ( IsModalShowing() && GetParent() )
551 {
552 // bring the owner window to top as the standard dialog
553 // boxes do
554 if ( !::SetWindowPos
555 (
556 GetHwndOf(GetParent()),
557 GetHwnd(),
558 0, 0,
559 0, 0,
560 SWP_NOACTIVATE |
561 SWP_NOMOVE |
562 SWP_NOSIZE
563 ) )
564 {
565 wxLogLastError("SetWindowPos(SWP_NOACTIVATE)");
566 }
567 }
568 // fall through to process it normally as well
569 }
570 break;
571
42e69d6b
VZ
572 case WM_CLOSE:
573 // if we can't close, tell the system that we processed the
574 // message - otherwise it would close us
575 processed = !Close();
576 break;
abceee76
VZ
577
578 case WM_SETCURSOR:
579 // we want to override the busy cursor for modal dialogs:
580 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 581 // is shown, but the modal dialog shouldn't have hourglass cursor
d1477745 582 if ( IsModalShowing() && wxIsBusy() )
abceee76 583 {
bfbd6dc1
VZ
584 // set our cursor for all windows (but see below)
585 wxCursor cursor = m_cursor;
586 if ( !cursor.Ok() )
587 cursor = wxCURSOR_ARROW;
abceee76 588
bfbd6dc1
VZ
589 ::SetCursor(GetHcursorOf(cursor));
590
591 // in any case, stop here and don't let wxWindow process this
592 // message (it would set the busy cursor)
abceee76 593 processed = TRUE;
bfbd6dc1
VZ
594
595 // but return FALSE to tell the child window (if the event
596 // comes from one of them and not from ourselves) that it can
597 // set its own cursor if it has one: thus, standard controls
598 // (e.g. text ctrl) still have correct cursors in a dialog
599 // invoked while wxIsBusy()
600 rc = FALSE;
abceee76 601 }
bfbd6dc1 602 break;
42e69d6b
VZ
603 }
604
605 if ( !processed )
606 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
607
608 return rc;
609}
b6c588e1
VZ
610
611#if wxUSE_CTL3D
612
613// Define for each class of dialog and control
614WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
615 WXHWND WXUNUSED(pWnd),
616 WXUINT WXUNUSED(nCtlColor),
617 WXUINT message,
618 WXWPARAM wParam,
619 WXLPARAM lParam)
620{
621 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
622}
623
624#endif // wxUSE_CTL3D
625