]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Just a teeny change -- addition of wxFrame::IsMaximized. For wxMDIChildFrame
[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::SetClientSize(int width, int height)
2bda0e17
KB
241{
242 HWND hWnd = (HWND) GetHWND();
243 RECT rect;
244 GetClientRect(hWnd, &rect);
245
246 RECT rect2;
247 GetWindowRect(hWnd, &rect2);
248
249 // Find the difference between the entire window (title bar and all)
250 // and the client area; add this to the new client size to move the
251 // window
252 int actual_width = rect2.right - rect2.left - rect.right + width;
253 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
254
255 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
debe6624 256
2bda0e17 257 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
debe6624 258 event.SetEventObject( this );
2bda0e17 259 GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
260}
261
262void wxDialog::GetPosition(int *x, int *y) const
263{
264 HWND hWnd = (HWND) GetHWND();
265 RECT rect;
266 GetWindowRect(hWnd, &rect);
267
268 *x = rect.left;
269 *y = rect.top;
270}
271
272bool wxDialog::IsShown(void) const
273{
274 return m_isShown;
275}
276
debe6624 277bool wxDialog::Show(bool show)
2bda0e17
KB
278{
279 m_isShown = show;
280
281 if (show)
282 InitDialog();
283
284 bool modal = ((GetWindowStyleFlag() & wxDIALOG_MODAL) == wxDIALOG_MODAL) ;
285
286#if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
287 if (!modal) {
288 if (show) {
289 if (!wxModelessWindows.Member(this))
dc1c4b62 290 wxModelessWindows.Append(this);
2bda0e17
KB
291 } else
292 wxModelessWindows.DeleteObject(this);
293 }
294 if (show) {
295 if (!wxTopLevelWindows.Member(this))
296 wxTopLevelWindows.Append(this);
297 } else
298 wxTopLevelWindows.DeleteObject(this);
299#endif
300
301 if (modal)
302 {
303 if (show)
304 {
dc1c4b62
VZ
305 m_hwndOldFocus = (WXHWND)::GetFocus();
306
2bda0e17
KB
307 if (m_modalShowing)
308 {
309 BringWindowToTop((HWND) GetHWND());
310 return TRUE;
311 }
312
313 m_modalShowing = TRUE;
314 wxNode *node = wxModalDialogs.First();
315 while (node)
316 {
317 wxDialog *box = (wxDialog *)node->Data();
318 if (box != this)
319 ::EnableWindow((HWND) box->GetHWND(), FALSE);
320 node = node->Next();
321 }
9c9cff0b
VZ
322
323 // if we don't do it, some window might be deleted while we have pointers
324 // to them in our disabledWindows list and the program will crash when it
325 // will try to reenable them after the modal dialog end
326 wxTheApp->DeletePendingObjects();
327 wxList disabledWindows;
328
2bda0e17
KB
329 node = wxModelessWindows.First();
330 while (node)
331 {
332 wxWindow *win = (wxWindow *)node->Data();
dc1c4b62
VZ
333 if (::IsWindowEnabled((HWND) win->GetHWND()))
334 {
2bda0e17 335 ::EnableWindow((HWND) win->GetHWND(), FALSE);
9c9cff0b 336 disabledWindows.Append(win);
dc1c4b62 337 }
2bda0e17
KB
338 node = node->Next();
339 }
340
341 ShowWindow((HWND) GetHWND(), SW_SHOW);
342 EnableWindow((HWND) GetHWND(), TRUE);
343 BringWindowToTop((HWND) GetHWND());
344
345 if (!wxModalDialogs.Member(this))
346 wxModalDialogs.Append(this);
347
348 MSG msg;
349 // Must test whether this dialog still exists: we may not process
350 // a message before the deletion.
351 while (wxModalDialogs.Member(this) && m_modalShowing && GetMessage(&msg, NULL, 0, 0))
352 {
567da5c6
JS
353 if (m_acceleratorTable.Ok() &&
354 ::TranslateAccelerator((HWND) GetHWND(), (HACCEL) m_acceleratorTable.GetHACCEL(), &msg))
355 {
356 // Have processed the message
357 }
358 else if (!IsDialogMessage((HWND) GetHWND(), &msg))
2bda0e17
KB
359 {
360 TranslateMessage(&msg);
361 DispatchMessage(&msg);
362 }
363 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
dc1c4b62
VZ
364 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
365 // a Show(FALSE) in the mean time!!!
366 // Without the test, we might delete the dialog before the end of modal showing.
2bda0e17
KB
367 {
368 while (wxTheApp->ProcessIdle() && m_modalShowing)
369 {
370 // Keep going until we decide we've done enough
371 }
372 }
373 }
374 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
9c9cff0b 375 node=disabledWindows.First();
2bda0e17 376 while(node) {
dc1c4b62 377 wxWindow* win = (wxWindow*) node->Data();
2bda0e17
KB
378 HWND hWnd = (HWND) win->GetHWND();
379 if (::IsWindow(hWnd) && (wxModalDialogs.Member(win) || wxModelessWindows.Member(win) ))
380 ::EnableWindow(hWnd,TRUE);
381 node=node->Next();
382 }
383 }
dc1c4b62 384 else // !show
2bda0e17 385 {
dc1c4b62
VZ
386 ::SetFocus((HWND)m_hwndOldFocus);
387
2bda0e17
KB
388 wxModalDialogs.DeleteObject(this);
389
390 wxNode *last = wxModalDialogs.Last();
391
392 // If there's still a modal dialog active, we
393 // enable it, else we enable all modeless windows
394 if (last)
395 {
396 wxDialog *box = (wxDialog *)last->Data();
397 HWND hwnd = (HWND) box->GetHWND();
398 if (box->m_winEnabled)
399 EnableWindow(hwnd, TRUE);
400 BringWindowToTop(hwnd);
401 }
402 else
403 {
404 wxNode *node = wxModelessWindows.First();
405 while (node)
406 {
407 wxWindow *win = (wxWindow *)node->Data();
408 HWND hwnd = (HWND) win->GetHWND();
409 // Only enable again if not user-disabled.
410 if (win->IsUserEnabled())
411 EnableWindow(hwnd, TRUE);
412 node = node->Next();
413 }
414 }
415 // Try to highlight the correct window (the parent)
416 HWND hWndParent = 0;
417 if (GetParent())
418 {
419 hWndParent = (HWND) GetParent()->GetHWND();
420 if (hWndParent)
421 ::BringWindowToTop(hWndParent);
422 }
423 ShowWindow((HWND) GetHWND(), SW_HIDE);
424 m_modalShowing = FALSE;
425 }
426 }
dc1c4b62 427 else // !modal
2bda0e17
KB
428 {
429 if (show)
430 {
431 ShowWindow((HWND) GetHWND(), SW_SHOW);
432 BringWindowToTop((HWND) GetHWND());
433 }
434 else
435 {
436 // Try to highlight the correct window (the parent)
437 HWND hWndParent = 0;
438 if (GetParent())
439 {
440 hWndParent = (HWND) GetParent()->GetHWND();
441 if (hWndParent)
442 ::BringWindowToTop(hWndParent);
443 }
444 ShowWindow((HWND) GetHWND(), SW_HIDE);
445 }
446 }
447 return TRUE;
448}
449
450void wxDialog::SetTitle(const wxString& title)
451{
452 SetWindowText((HWND) GetHWND(), (const char *)title);
453}
454
455wxString wxDialog::GetTitle(void) const
456{
457 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
458 return wxString(wxBuffer);
459}
460
debe6624 461void wxDialog::Centre(int direction)
2bda0e17
KB
462{
463 int x_offset,y_offset ;
464 int display_width, display_height;
465 int width, height, x, y;
dfc54541
JS
466 wxWindow *parent = GetParent();
467 if ((direction & wxCENTER_FRAME) && parent)
2bda0e17 468 {
dfc54541
JS
469 parent->GetPosition(&x_offset,&y_offset) ;
470 parent->GetSize(&display_width,&display_height) ;
2bda0e17
KB
471 }
472 else
2bda0e17
KB
473 {
474 wxDisplaySize(&display_width, &display_height);
475 x_offset = 0 ;
476 y_offset = 0 ;
477 }
478
2bda0e17
KB
479 GetSize(&width, &height);
480 GetPosition(&x, &y);
481
482 if (direction & wxHORIZONTAL)
483 x = (int)((display_width - width)/2);
484 if (direction & wxVERTICAL)
485 y = (int)((display_height - height)/2);
486
487 SetSize(x+x_offset, y+y_offset, width, height);
488}
489
490// Replacement for Show(TRUE) for modal dialogs - returns return code
491int wxDialog::ShowModal(void)
492{
dc1c4b62
VZ
493 m_windowStyle |= wxDIALOG_MODAL;
494 Show(TRUE);
495 return GetReturnCode();
2bda0e17
KB
496}
497
498void wxDialog::EndModal(int retCode)
499{
dc1c4b62
VZ
500 SetReturnCode(retCode);
501 Show(FALSE);
2bda0e17
KB
502}
503
504// Define for each class of dialog and control
debe6624 505WXHBRUSH wxDialog::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor,
dc1c4b62 506 WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2bda0e17
KB
507{
508#if CTL3D
509 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
510 return (WXHBRUSH) hbrush;
511#else
512 return 0;
513#endif
514}
515
516// Standard buttons
517void wxDialog::OnOK(wxCommandEvent& event)
518{
dc1c4b62
VZ
519 if ( Validate() && TransferDataFromWindow() )
520 {
2bda0e17
KB
521 if ( IsModal() )
522 EndModal(wxID_OK);
523 else
524 {
387a3b02
JS
525 SetReturnCode(wxID_OK);
526 this->Show(FALSE);
2bda0e17 527 }
dc1c4b62 528 }
2bda0e17
KB
529}
530
531void wxDialog::OnApply(wxCommandEvent& event)
532{
dc1c4b62
VZ
533 if (Validate())
534 TransferDataFromWindow();
535 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
536}
537
538void wxDialog::OnCancel(wxCommandEvent& event)
539{
540 if ( IsModal() )
541 EndModal(wxID_CANCEL);
542 else
543 {
544 SetReturnCode(wxID_CANCEL);
dc1c4b62 545 this->Show(FALSE);
2bda0e17
KB
546 }
547}
548
549bool wxDialog::OnClose(void)
550{
387a3b02 551 // Behaviour changed in 2.0: we'll send a Cancel message by default,
2bda0e17
KB
552 // which may close the dialog.
553 // Check for looping if the Cancel event handler calls Close()
554
555 static wxList closing;
556
557 if ( closing.Member(this) )
558 return FALSE;
559
560 closing.Append(this);
561
387a3b02
JS
562 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
563 cancelEvent.SetEventObject( this );
564 GetEventHandler()->ProcessEvent(cancelEvent);
2bda0e17
KB
565
566 closing.DeleteObject(this);
567
387a3b02 568 return FALSE;
2bda0e17
KB
569}
570
571void wxDialog::OnCloseWindow(wxCloseEvent& event)
572{
573 // Compatibility
574 if ( GetEventHandler()->OnClose() || event.GetForce())
575 {
576 this->Destroy();
577 }
387a3b02
JS
578 else
579 event.Veto(TRUE);
2bda0e17
KB
580}
581
582// Destroy the window (delayed, if a managed window)
583bool wxDialog::Destroy(void)
584{
585 if (!wxPendingDelete.Member(this))
586 wxPendingDelete.Append(this);
587 return TRUE;
588}
589
590void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
591{
592#if CTL3D
593 Ctl3dColorChange();
594#else
595 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
596 Refresh();
597#endif
598}
599
600long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
601{
dc1c4b62 602 return wxWindow::MSWWindowProc(message, wParam, lParam);
2bda0e17
KB
603}
604