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