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