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