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