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