]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Corrected spelling of 'unrecognized'
[wxWidgets.git] / src / msw / dialog.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialog.cpp
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
12#ifdef __GNUG__
13#pragma implementation "dialog.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24#include "wx/dialog.h"
25#include "wx/utils.h"
26#include "wx/frame.h"
27#include "wx/app.h"
28#include "wx/settings.h"
29#endif
30
31#include "wx/msw/private.h"
dbda9e86 32#include "wx/log.h"
2bda0e17 33
47d67540 34#if wxUSE_COMMON_DIALOGS
2bda0e17
KB
35#include <commdlg.h>
36#endif
37
38#define wxDIALOG_DEFAULT_X 300
39#define wxDIALOG_DEFAULT_Y 300
40
41// Lists to keep track of windows, so we can disable/enable them
42// for modal dialogs
a23fd0e1
VZ
43wxWindowList wxModalDialogs;
44wxWindowList wxModelessWindows; // Frames and modeless dialogs
cde9f08e 45extern wxList WXDLLEXPORT wxPendingDelete;
2bda0e17
KB
46
47#if !USE_SHARED_LIBRARY
462e2437
VZ
48 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
49
50 BEGIN_EVENT_TABLE(wxDialog, wxPanel)
51 EVT_SIZE(wxDialog::OnSize)
52 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
53 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
54 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
55 EVT_CHAR_HOOK(wxDialog::OnCharHook)
56 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
57 EVT_CLOSE(wxDialog::OnCloseWindow)
58 END_EVENT_TABLE()
2bda0e17
KB
59#endif
60
a23fd0e1 61wxDialog::wxDialog()
2bda0e17
KB
62{
63 m_isShown = FALSE;
64 m_modalShowing = FALSE;
65
66 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
67}
68
debe6624 69bool wxDialog::Create(wxWindow *parent, wxWindowID id,
462e2437
VZ
70 const wxString& title,
71 const wxPoint& pos,
72 const wxSize& size,
73 long style,
74 const wxString& name)
2bda0e17 75{
bd9d76cb
VZ
76#if wxUSE_TOOLTIPS
77 m_hwndToolTip = 0;
78#endif
79
462e2437
VZ
80 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
81 SetName(name);
bd9d76cb 82
462e2437
VZ
83 if (!parent)
84 wxTopLevelWindows.Append(this);
2bda0e17 85
462e2437 86 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
2bda0e17 87
462e2437 88 if (parent) parent->AddChild(this);
2bda0e17 89
462e2437
VZ
90 if ( id == -1 )
91 m_windowId = (int)NewControlId();
92 else
93 m_windowId = id;
94
95 int x = pos.x;
96 int y = pos.y;
97 int width = size.x;
98 int height = size.y;
99
100 if (x < 0) x = wxDIALOG_DEFAULT_X;
101 if (y < 0) y = wxDIALOG_DEFAULT_Y;
102
103 m_windowStyle = style;
104
105 m_isShown = FALSE;
106 m_modalShowing = FALSE;
107
108 if (width < 0)
109 width = 500;
110 if (height < 0)
111 height = 500;
112
706bb5f9
JS
113 // All dialogs should really have this style
114 m_windowStyle |= wxTAB_TRAVERSAL;
115
462e2437
VZ
116 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
117 if (m_windowStyle & wxSTAY_ON_TOP)
118 extendedStyle |= WS_EX_TOPMOST;
119
120 // Allows creation of dialogs with & without captions under MSWindows,
121 // resizeable or not (but a resizeable dialog always has caption -
122 // otherwise it would look too strange)
123 const char *dlg;
124 if ( style & wxTHICK_FRAME )
125 dlg = "wxResizeableDialog";
126 else if ( style & wxCAPTION )
127 dlg = "wxCaptionDialog";
128 else
129 dlg = "wxNoCaptionDialog";
130 MSWCreate(m_windowId, parent, NULL, this, NULL,
131 x, y, width, height,
132 0, // style is not used if we have dlg template
133 dlg,
134 extendedStyle);
2bda0e17 135
462e2437 136 HWND hwnd = (HWND)GetHWND();
2bda0e17 137
462e2437
VZ
138 if ( !hwnd )
139 {
68ad65f8 140 wxLogError(_("Failed to create dialog."));
2bda0e17 141
462e2437
VZ
142 return FALSE;
143 }
2bda0e17 144
462e2437 145 SubclassWin(GetHWND());
bd9d76cb 146
462e2437
VZ
147 SetWindowText(hwnd, title);
148 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
2bda0e17 149
462e2437 150 return TRUE;
2bda0e17
KB
151}
152
debe6624 153void wxDialog::SetModal(bool flag)
2bda0e17 154{
dc1c4b62
VZ
155 if ( flag )
156 m_windowStyle |= wxDIALOG_MODAL ;
157 else
158 if ( m_windowStyle & wxDIALOG_MODAL )
159 m_windowStyle -= wxDIALOG_MODAL ;
bd9d76cb 160
2bda0e17
KB
161 wxModelessWindows.DeleteObject(this);
162 if (!flag)
163 wxModelessWindows.Append(this);
164}
165
166wxDialog::~wxDialog()
167{
168 m_isBeingDeleted = TRUE;
169
170 wxTopLevelWindows.DeleteObject(this);
171
172 if (m_modalShowing)
173 {
174 Show(FALSE);
175 // For some reason, wxWindows can activate another task altogether
176 // when a frame is destroyed after a modal dialog has been invoked.
177 // Try to bring the parent to the top.
178 // dfgg: I moved this following line from end of function -
179 // must not call if another window is on top!!
180 // This can often happen with Close() and delayed deleting
181 if (GetParent() && GetParent()->GetHWND())
182 ::BringWindowToTop((HWND) GetParent()->GetHWND());
183 }
184
185 m_modalShowing = FALSE;
186 if ( GetHWND() )
187 ShowWindow((HWND) GetHWND(), SW_HIDE);
188
189 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL) != wxDIALOG_MODAL )
190 wxModelessWindows.DeleteObject(this);
191
192 UnsubclassWin();
193
194 // If this is the last top-level window, exit.
195 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
196 {
197 wxTheApp->SetTopWindow(NULL);
198
199 if (wxTheApp->GetExitOnFrameDelete())
200 {
201 PostQuitMessage(0);
202 }
203 }
204}
205
206// By default, pressing escape cancels the dialog
207void wxDialog::OnCharHook(wxKeyEvent& event)
208{
209 if (GetHWND())
210 {
211 if (event.m_keyCode == WXK_ESCAPE)
212 {
2a47d3c1
JS
213 // Behaviour changed in 2.0: we'll send a Cancel message
214 // to the dialog instead of Close.
215 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
216 cancelEvent.SetEventObject( this );
217 GetEventHandler()->ProcessEvent(cancelEvent);
2bda0e17 218
2a47d3c1 219 return;
2bda0e17
KB
220 }
221 }
222 // We didn't process this event.
223 event.Skip();
224}
225
226void wxDialog::OnPaint(wxPaintEvent& event)
227{
dc1c4b62
VZ
228 // No: if you call the default procedure, it makes
229 // the following painting code not work.
230// wxWindow::OnPaint(event);
2bda0e17
KB
231}
232
a23fd0e1 233void wxDialog::Fit()
2bda0e17
KB
234{
235 wxWindow::Fit();
236}
237
debe6624 238void wxDialog::Iconize(bool WXUNUSED(iconize))
2bda0e17
KB
239{
240 // Windows dialog boxes can't be iconized
241}
242
a23fd0e1 243bool wxDialog::IsIconized() const
2bda0e17
KB
244{
245 return FALSE;
246}
247
721b32e0 248void wxDialog::DoSetClientSize(int width, int height)
2bda0e17
KB
249{
250 HWND hWnd = (HWND) GetHWND();
251 RECT rect;
2de8030d 252 ::GetClientRect(hWnd, &rect);
2bda0e17
KB
253
254 RECT rect2;
255 GetWindowRect(hWnd, &rect2);
256
257 // Find the difference between the entire window (title bar and all)
258 // and the client area; add this to the new client size to move the
259 // window
260 int actual_width = rect2.right - rect2.left - rect.right + width;
261 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
262
263 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
debe6624 264
2bda0e17 265 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
debe6624 266 event.SetEventObject( this );
2bda0e17 267 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
268}
269
270void wxDialog::GetPosition(int *x, int *y) const
271{
272 HWND hWnd = (HWND) GetHWND();
273 RECT rect;
274 GetWindowRect(hWnd, &rect);
275
276 *x = rect.left;
277 *y = rect.top;
278}
279
a23fd0e1 280bool wxDialog::IsShown() const
2bda0e17
KB
281{
282 return m_isShown;
283}
284
debe6624 285bool wxDialog::Show(bool show)
2bda0e17
KB
286{
287 m_isShown = show;
288
289 if (show)
290 InitDialog();
291
292 bool modal = ((GetWindowStyleFlag() & wxDIALOG_MODAL) == wxDIALOG_MODAL) ;
293
294#if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
295 if (!modal) {
296 if (show) {
a23fd0e1
VZ
297 if (!wxModelessWindows.Find(this))
298 wxModelessWindows.Append(this);
2bda0e17
KB
299 } else
300 wxModelessWindows.DeleteObject(this);
301 }
302 if (show) {
a23fd0e1 303 if (!wxTopLevelWindows.Find(this))
2bda0e17
KB
304 wxTopLevelWindows.Append(this);
305 } else
306 wxTopLevelWindows.DeleteObject(this);
307#endif
308
309 if (modal)
310 {
311 if (show)
312 {
7f68042c
VZ
313 // find the top level window which had focus before - we will restore
314 // focus to it later
315 m_hwndOldFocus = 0;
316 for ( HWND hwnd = ::GetFocus(); hwnd; hwnd = ::GetParent(hwnd) )
317 {
318 m_hwndOldFocus = (WXHWND)hwnd;
319 }
dc1c4b62 320
2bda0e17
KB
321 if (m_modalShowing)
322 {
323 BringWindowToTop((HWND) GetHWND());
324 return TRUE;
325 }
bd9d76cb 326
2bda0e17
KB
327 m_modalShowing = TRUE;
328 wxNode *node = wxModalDialogs.First();
329 while (node)
330 {
331 wxDialog *box = (wxDialog *)node->Data();
332 if (box != this)
333 ::EnableWindow((HWND) box->GetHWND(), FALSE);
334 node = node->Next();
335 }
9c9cff0b
VZ
336
337 // if we don't do it, some window might be deleted while we have pointers
338 // to them in our disabledWindows list and the program will crash when it
339 // will try to reenable them after the modal dialog end
340 wxTheApp->DeletePendingObjects();
341 wxList disabledWindows;
342
2bda0e17
KB
343 node = wxModelessWindows.First();
344 while (node)
345 {
346 wxWindow *win = (wxWindow *)node->Data();
dc1c4b62
VZ
347 if (::IsWindowEnabled((HWND) win->GetHWND()))
348 {
2bda0e17 349 ::EnableWindow((HWND) win->GetHWND(), FALSE);
9c9cff0b 350 disabledWindows.Append(win);
dc1c4b62 351 }
2bda0e17
KB
352 node = node->Next();
353 }
354
355 ShowWindow((HWND) GetHWND(), SW_SHOW);
356 EnableWindow((HWND) GetHWND(), TRUE);
357 BringWindowToTop((HWND) GetHWND());
358
a23fd0e1 359 if ( !wxModalDialogs.Find(this) )
2bda0e17
KB
360 wxModalDialogs.Append(this);
361
362 MSG msg;
363 // Must test whether this dialog still exists: we may not process
364 // a message before the deletion.
a23fd0e1 365 while (wxModalDialogs.Find(this) && m_modalShowing && GetMessage(&msg, NULL, 0, 0))
2bda0e17 366 {
0a54c4a8
VZ
367 if ( m_acceleratorTable.Ok() &&
368 ::TranslateAccelerator((HWND)GetHWND(),
369 (HACCEL)m_acceleratorTable.GetHACCEL(),
370 &msg) )
567da5c6
JS
371 {
372 // Have processed the message
373 }
0a54c4a8 374 else if ( !wxTheApp->ProcessMessage((WXMSG *)&msg) )
2bda0e17
KB
375 {
376 TranslateMessage(&msg);
377 DispatchMessage(&msg);
378 }
9838df2c
JS
379
380 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
381 // we should try removing the m_modalShowing test
382
2bda0e17 383 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
dc1c4b62
VZ
384 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
385 // a Show(FALSE) in the mean time!!!
386 // Without the test, we might delete the dialog before the end of modal showing.
2bda0e17
KB
387 {
388 while (wxTheApp->ProcessIdle() && m_modalShowing)
389 {
390 // Keep going until we decide we've done enough
391 }
392 }
393 }
394 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
9c9cff0b 395 node=disabledWindows.First();
2bda0e17 396 while(node) {
dc1c4b62 397 wxWindow* win = (wxWindow*) node->Data();
a23fd0e1 398 if (wxModalDialogs.Find(win) || wxModelessWindows.Find(win))
9838df2c
JS
399 {
400 HWND hWnd = (HWND) win->GetHWND();
401 if (::IsWindow(hWnd))
402 ::EnableWindow(hWnd,TRUE);
403 }
2bda0e17
KB
404 node=node->Next();
405 }
406 }
dc1c4b62 407 else // !show
2bda0e17 408 {
dc1c4b62
VZ
409 ::SetFocus((HWND)m_hwndOldFocus);
410
2bda0e17
KB
411 wxModalDialogs.DeleteObject(this);
412
413 wxNode *last = wxModalDialogs.Last();
414
415 // If there's still a modal dialog active, we
416 // enable it, else we enable all modeless windows
417 if (last)
418 {
cc2b7472
VZ
419 // VZ: I don't understand what this is supposed to do, so I'll leave
420 // it out for now and look for horrible consequences
2bda0e17
KB
421 wxDialog *box = (wxDialog *)last->Data();
422 HWND hwnd = (HWND) box->GetHWND();
cc2b7472
VZ
423#if 0
424 if (box->IsUserEnabled())
425#endif // 0
2bda0e17
KB
426 EnableWindow(hwnd, TRUE);
427 BringWindowToTop(hwnd);
428 }
429 else
430 {
431 wxNode *node = wxModelessWindows.First();
432 while (node)
433 {
434 wxWindow *win = (wxWindow *)node->Data();
435 HWND hwnd = (HWND) win->GetHWND();
436 // Only enable again if not user-disabled.
cc2b7472 437#if 0
2bda0e17 438 if (win->IsUserEnabled())
cc2b7472 439#endif // 0
2bda0e17
KB
440 EnableWindow(hwnd, TRUE);
441 node = node->Next();
442 }
443 }
444 // Try to highlight the correct window (the parent)
445 HWND hWndParent = 0;
446 if (GetParent())
447 {
448 hWndParent = (HWND) GetParent()->GetHWND();
449 if (hWndParent)
450 ::BringWindowToTop(hWndParent);
451 }
452 ShowWindow((HWND) GetHWND(), SW_HIDE);
453 m_modalShowing = FALSE;
454 }
455 }
dc1c4b62 456 else // !modal
2bda0e17
KB
457 {
458 if (show)
459 {
460 ShowWindow((HWND) GetHWND(), SW_SHOW);
461 BringWindowToTop((HWND) GetHWND());
462 }
463 else
464 {
465 // Try to highlight the correct window (the parent)
466 HWND hWndParent = 0;
467 if (GetParent())
468 {
469 hWndParent = (HWND) GetParent()->GetHWND();
470 if (hWndParent)
471 ::BringWindowToTop(hWndParent);
472 }
473 ShowWindow((HWND) GetHWND(), SW_HIDE);
474 }
475 }
476 return TRUE;
477}
478
479void wxDialog::SetTitle(const wxString& title)
480{
481 SetWindowText((HWND) GetHWND(), (const char *)title);
482}
483
a23fd0e1 484wxString wxDialog::GetTitle() const
2bda0e17
KB
485{
486 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
487 return wxString(wxBuffer);
488}
489
debe6624 490void wxDialog::Centre(int direction)
2bda0e17
KB
491{
492 int x_offset,y_offset ;
493 int display_width, display_height;
494 int width, height, x, y;
dfc54541
JS
495 wxWindow *parent = GetParent();
496 if ((direction & wxCENTER_FRAME) && parent)
2bda0e17 497 {
dfc54541
JS
498 parent->GetPosition(&x_offset,&y_offset) ;
499 parent->GetSize(&display_width,&display_height) ;
2bda0e17
KB
500 }
501 else
2bda0e17
KB
502 {
503 wxDisplaySize(&display_width, &display_height);
504 x_offset = 0 ;
505 y_offset = 0 ;
506 }
507
2bda0e17
KB
508 GetSize(&width, &height);
509 GetPosition(&x, &y);
510
511 if (direction & wxHORIZONTAL)
512 x = (int)((display_width - width)/2);
513 if (direction & wxVERTICAL)
514 y = (int)((display_height - height)/2);
515
516 SetSize(x+x_offset, y+y_offset, width, height);
517}
518
519// Replacement for Show(TRUE) for modal dialogs - returns return code
a23fd0e1 520int wxDialog::ShowModal()
2bda0e17 521{
dc1c4b62
VZ
522 m_windowStyle |= wxDIALOG_MODAL;
523 Show(TRUE);
524 return GetReturnCode();
2bda0e17
KB
525}
526
527void wxDialog::EndModal(int retCode)
528{
dc1c4b62
VZ
529 SetReturnCode(retCode);
530 Show(FALSE);
2bda0e17
KB
531}
532
533// Define for each class of dialog and control
debe6624 534WXHBRUSH wxDialog::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
dc1c4b62 535 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17 536{
1f112209 537#if wxUSE_CTL3D
2bda0e17
KB
538 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
539 return (WXHBRUSH) hbrush;
540#else
541 return 0;
542#endif
543}
544
545// Standard buttons
546void wxDialog::OnOK(wxCommandEvent& event)
547{
dc1c4b62
VZ
548 if ( Validate() && TransferDataFromWindow() )
549 {
2bda0e17
KB
550 if ( IsModal() )
551 EndModal(wxID_OK);
552 else
553 {
387a3b02
JS
554 SetReturnCode(wxID_OK);
555 this->Show(FALSE);
2bda0e17 556 }
dc1c4b62 557 }
2bda0e17
KB
558}
559
560void wxDialog::OnApply(wxCommandEvent& event)
561{
dc1c4b62
VZ
562 if (Validate())
563 TransferDataFromWindow();
564 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
565}
566
567void wxDialog::OnCancel(wxCommandEvent& event)
568{
569 if ( IsModal() )
570 EndModal(wxID_CANCEL);
571 else
572 {
573 SetReturnCode(wxID_CANCEL);
2a47d3c1 574 this->Show(FALSE);
2bda0e17
KB
575 }
576}
577
e3065973 578void wxDialog::OnCloseWindow(wxCloseEvent& event)
2bda0e17 579{
e3065973 580 // We'll send a Cancel message by default,
2bda0e17 581 // which may close the dialog.
e3065973
JS
582 // Check for looping if the Cancel event handler calls Close().
583
584 // Note that if a cancel button and handler aren't present in the dialog,
585 // nothing will happen when you close the dialog via the window manager, or
586 // via Close().
587 // We wouldn't want to destroy the dialog by default, since the dialog may have been
588 // created on the stack.
589 // However, this does mean that calling dialog->Close() won't delete the dialog
590 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
591 // sure to destroy the dialog.
592 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
2bda0e17
KB
593
594 static wxList closing;
bd9d76cb 595
2bda0e17 596 if ( closing.Member(this) )
e3065973 597 return;
bd9d76cb 598
2bda0e17 599 closing.Append(this);
bd9d76cb 600
387a3b02
JS
601 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
602 cancelEvent.SetEventObject( this );
e3065973 603 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
2bda0e17
KB
604
605 closing.DeleteObject(this);
e3065973 606}
2bda0e17 607
e3065973 608// Destroy the window (delayed, if a managed window)
a23fd0e1 609bool wxDialog::Destroy()
e3065973
JS
610{
611 if (!wxPendingDelete.Member(this))
612 wxPendingDelete.Append(this);
613 return TRUE;
2bda0e17
KB
614}
615
94b49b93
JS
616void wxDialog::OnSize(wxSizeEvent& WXUNUSED(event))
617{
618 // if we're using constraints - do use them
619 #if wxUSE_CONSTRAINTS
a23fd0e1
VZ
620 if ( GetAutoLayout() )
621 {
94b49b93
JS
622 Layout();
623 }
624 #endif
625}
626
2bda0e17
KB
627void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
628{
1f112209 629#if wxUSE_CTL3D
2bda0e17
KB
630 Ctl3dColorChange();
631#else
632 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
633 Refresh();
634#endif
68ad65f8 635}
42e69d6b
VZ
636
637// ---------------------------------------------------------------------------
638// dialog window proc
639// ---------------------------------------------------------------------------
640
641long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
642{
643 long rc = 0;
644 bool processed = FALSE;
645
646 switch ( message )
647 {
648 case WM_CLOSE:
649 // if we can't close, tell the system that we processed the
650 // message - otherwise it would close us
651 processed = !Close();
652 break;
653 }
654
655 if ( !processed )
656 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
657
658 return rc;
659}