]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
Whole lotta stuff for wxPlotWindow,
[wxWidgets.git] / src / msw / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dialog.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/dialog.h"
33 #include "wx/utils.h"
34 #include "wx/frame.h"
35 #include "wx/app.h"
36 #include "wx/settings.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
39 #endif
40
41 #include "wx/msw/private.h"
42 #include "wx/log.h"
43
44 #if wxUSE_COMMON_DIALOGS
45 #include <commdlg.h>
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // default dialog pos and size
53
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
56
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
59
60 // ----------------------------------------------------------------------------
61 // globals
62 // ----------------------------------------------------------------------------
63
64 // all objects to be deleted during next idle processing - from window.cpp
65 extern wxList WXDLLEXPORT wxPendingDelete;
66
67 // all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68 wxWindowList wxModelessWindows;
69
70 // all modal dialogs currently shown
71 static wxWindowList wxModalDialogs;
72
73 // ----------------------------------------------------------------------------
74 // wxWin macros
75 // ----------------------------------------------------------------------------
76
77 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
78
79 BEGIN_EVENT_TABLE(wxDialog, wxPanel)
80 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
81 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
82 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
83
84 EVT_CHAR_HOOK(wxDialog::OnCharHook)
85
86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
87
88 EVT_CLOSE(wxDialog::OnCloseWindow)
89 END_EVENT_TABLE()
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxDialog construction
97 // ----------------------------------------------------------------------------
98
99 wxDialog::wxDialog()
100 {
101 m_isShown = FALSE;
102
103 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
104 }
105
106 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
107 const wxString& title,
108 const wxPoint& pos,
109 const wxSize& size,
110 long style,
111 const wxString& name)
112 {
113 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
114 SetName(name);
115
116 if (!parent)
117 wxTopLevelWindows.Append(this);
118
119 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
120
121 if (parent) parent->AddChild(this);
122
123 if ( id == -1 )
124 m_windowId = (int)NewControlId();
125 else
126 m_windowId = id;
127
128 int x = pos.x;
129 int y = pos.y;
130 int width = size.x;
131 int height = size.y;
132
133 if (x < 0)
134 x = wxDIALOG_DEFAULT_X;
135 if (y < 0)
136 y = wxDIALOG_DEFAULT_Y;
137
138 m_windowStyle = style;
139
140 m_isShown = FALSE;
141
142 if (width < 0)
143 width = wxDIALOG_DEFAULT_WIDTH;
144 if (height < 0)
145 height = wxDIALOG_DEFAULT_HEIGHT;
146
147 // All dialogs should really have this style
148 m_windowStyle |= wxTAB_TRAVERSAL;
149
150 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
151 if (m_windowStyle & wxSTAY_ON_TOP)
152 extendedStyle |= WS_EX_TOPMOST;
153
154 // Allows creation of dialogs with & without captions under MSWindows,
155 // resizeable or not (but a resizeable dialog always has caption -
156 // otherwise it would look too strange)
157 const wxChar *dlg;
158 if ( style & wxRESIZE_BORDER )
159 dlg = wxT("wxResizeableDialog");
160 else if ( style & wxCAPTION )
161 dlg = wxT("wxCaptionDialog");
162 else
163 dlg = wxT("wxNoCaptionDialog");
164 MSWCreate(m_windowId, parent, NULL, this, NULL,
165 x, y, width, height,
166 0, // style is not used if we have dlg template
167 dlg,
168 extendedStyle);
169
170 HWND hwnd = (HWND)GetHWND();
171
172 if ( !hwnd )
173 {
174 wxLogError(_("Failed to create dialog."));
175
176 return FALSE;
177 }
178
179 SubclassWin(GetHWND());
180
181 SetWindowText(hwnd, title);
182 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
183
184 return TRUE;
185 }
186
187 void wxDialog::SetModal(bool flag)
188 {
189 if ( flag )
190 {
191 m_windowStyle |= wxDIALOG_MODAL;
192
193 wxModelessWindows.DeleteObject(this);
194 }
195 else
196 {
197 m_windowStyle &= ~wxDIALOG_MODAL;
198
199 wxModelessWindows.Append(this);
200 }
201 }
202
203 wxDialog::~wxDialog()
204 {
205 m_isBeingDeleted = TRUE;
206
207 wxTopLevelWindows.DeleteObject(this);
208
209 Show(FALSE);
210
211 // VZ: this is bogus and breaks focus handling - it won't be returned to
212 // the window which had it previosuly if we do this
213 #if 0
214 if (m_modalShowing)
215 {
216 // For some reason, wxWindows can activate another task altogether
217 // when a frame is destroyed after a modal dialog has been invoked.
218 // Try to bring the parent to the top.
219 // dfgg: I moved this following line from end of function -
220 // must not call if another window is on top!!
221 // This can often happen with Close() and delayed deleting
222 if (GetParent() && GetParent()->GetHWND())
223 ::BringWindowToTop((HWND) GetParent()->GetHWND());
224 }
225
226 m_modalShowing = FALSE;
227 #endif // 0
228
229 if ( !IsModal() )
230 wxModelessWindows.DeleteObject(this);
231
232 // If this is the last top-level window, exit.
233 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
234 {
235 wxTheApp->SetTopWindow(NULL);
236
237 if ( wxTheApp->GetExitOnFrameDelete() )
238 {
239 ::PostQuitMessage(0);
240 }
241 }
242 }
243
244 // ----------------------------------------------------------------------------
245 // kbd handling
246 // ----------------------------------------------------------------------------
247
248 // By default, pressing escape cancels the dialog
249 void wxDialog::OnCharHook(wxKeyEvent& event)
250 {
251 if (GetHWND())
252 {
253 if (event.m_keyCode == WXK_ESCAPE)
254 {
255 // Behaviour changed in 2.0: we'll send a Cancel message
256 // to the dialog instead of Close.
257 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
258 cancelEvent.SetEventObject( this );
259 GetEventHandler()->ProcessEvent(cancelEvent);
260
261 // ensure that there is another message for this window so the
262 // ShowModal loop will exit and won't get stuck in GetMessage().
263 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
264 return;
265 }
266 }
267 // We didn't process this event.
268 event.Skip();
269 }
270
271 // ----------------------------------------------------------------------------
272 // Windows dialog boxes can't be iconized
273 // ----------------------------------------------------------------------------
274
275 void wxDialog::Iconize(bool WXUNUSED(iconize))
276 {
277 }
278
279 bool wxDialog::IsIconized() const
280 {
281 return FALSE;
282 }
283
284 // ----------------------------------------------------------------------------
285 // size/position handling
286 // ----------------------------------------------------------------------------
287
288 void wxDialog::DoSetClientSize(int width, int height)
289 {
290 HWND hWnd = (HWND) GetHWND();
291 RECT rect;
292 ::GetClientRect(hWnd, &rect);
293
294 RECT rect2;
295 GetWindowRect(hWnd, &rect2);
296
297 // Find the difference between the entire window (title bar and all)
298 // and the client area; add this to the new client size to move the
299 // window
300 int actual_width = rect2.right - rect2.left - rect.right + width;
301 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
302
303 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
304
305 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
306 event.SetEventObject( this );
307 GetEventHandler()->ProcessEvent(event);
308 }
309
310 void wxDialog::DoGetPosition(int *x, int *y) const
311 {
312 RECT rect;
313 GetWindowRect(GetHwnd(), &rect);
314
315 if ( x )
316 *x = rect.left;
317 if ( y )
318 *y = rect.top;
319 }
320
321 // ----------------------------------------------------------------------------
322 // showing the dialogs
323 // ----------------------------------------------------------------------------
324
325 bool wxDialog::IsModal() const
326 {
327 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
328 }
329
330 // VZ: this is the old version unchanged (reindented only), it will be removed
331 // as soon as we're sure the new one works correctly
332 #if 0
333
334 bool wxDialog::Show(bool show)
335 {
336 m_isShown = show;
337
338 if (show)
339 InitDialog();
340
341 bool modal = IsModal();
342
343 if (modal)
344 {
345 if (show)
346 {
347 // find the top level window which had focus before - we will restore
348 // focus to it later
349 m_hwndOldFocus = 0;
350 for ( HWND hwnd = ::GetFocus(); hwnd; hwnd = ::GetParent(hwnd) )
351 {
352 m_hwndOldFocus = (WXHWND)hwnd;
353 }
354
355 if (m_modalShowing)
356 {
357 BringWindowToTop((HWND) GetHWND());
358 return TRUE;
359 }
360
361 m_modalShowing = TRUE;
362 wxNode *node = wxModalDialogs.First();
363 while (node)
364 {
365 wxDialog *box = (wxDialog *)node->Data();
366 if (box != this)
367 ::EnableWindow((HWND) box->GetHWND(), FALSE);
368 node = node->Next();
369 }
370
371 // if we don't do it, some window might be deleted while we have pointers
372 // to them in our disabledWindows list and the program will crash when it
373 // will try to reenable them after the modal dialog end
374 wxTheApp->DeletePendingObjects();
375 wxList disabledWindows;
376
377 node = wxModelessWindows.First();
378 while (node)
379 {
380 wxWindow *win = (wxWindow *)node->Data();
381 if (::IsWindowEnabled((HWND) win->GetHWND()))
382 {
383 ::EnableWindow((HWND) win->GetHWND(), FALSE);
384 disabledWindows.Append(win);
385 }
386 node = node->Next();
387 }
388
389 ShowWindow((HWND) GetHWND(), SW_SHOW);
390 EnableWindow((HWND) GetHWND(), TRUE);
391 BringWindowToTop((HWND) GetHWND());
392
393 if ( !wxModalDialogs.Find(this) )
394 wxModalDialogs.Append(this);
395
396 MSG msg;
397 // Must test whether this dialog still exists: we may not process
398 // a message before the deletion.
399 while (wxModalDialogs.Find(this) && m_modalShowing && GetMessage(&msg, NULL, 0, 0))
400 {
401 if ( m_acceleratorTable.Ok() &&
402 ::TranslateAccelerator((HWND)GetHWND(),
403 (HACCEL)m_acceleratorTable.GetHACCEL(),
404 &msg) )
405 {
406 // Have processed the message
407 }
408 else if ( !wxTheApp->ProcessMessage((WXMSG *)&msg) )
409 {
410 TranslateMessage(&msg);
411 DispatchMessage(&msg);
412 }
413
414 // If we get crashes (as per George Tasker's message) with nested modal dialogs,
415 // we should try removing the m_modalShowing test
416
417 if (m_modalShowing && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE))
418 // dfgg: NB MUST test m_modalShowing again as the message loop could have triggered
419 // a Show(FALSE) in the mean time!!!
420 // Without the test, we might delete the dialog before the end of modal showing.
421 {
422 while (wxTheApp->ProcessIdle() && m_modalShowing)
423 {
424 // Keep going until we decide we've done enough
425 }
426 }
427 }
428 // dfgg: now must specifically re-enable all other app windows that we disabled earlier
429 node=disabledWindows.First();
430 while(node) {
431 wxWindow* win = (wxWindow*) node->Data();
432 if (wxModalDialogs.Find(win) || wxModelessWindows.Find(win))
433 {
434 HWND hWnd = (HWND) win->GetHWND();
435 if (::IsWindow(hWnd))
436 ::EnableWindow(hWnd,TRUE);
437 }
438 node=node->Next();
439 }
440 }
441 else // !show
442 {
443 ::SetFocus((HWND)m_hwndOldFocus);
444
445 wxModalDialogs.DeleteObject(this);
446
447 wxNode *last = wxModalDialogs.Last();
448
449 // If there's still a modal dialog active, we
450 // enable it, else we enable all modeless windows
451 if (last)
452 {
453 // VZ: I don't understand what this is supposed to do, so I'll leave
454 // it out for now and look for horrible consequences
455 wxDialog *box = (wxDialog *)last->Data();
456 HWND hwnd = (HWND) box->GetHWND();
457 #if 0
458 if (box->IsUserEnabled())
459 #endif // 0
460 EnableWindow(hwnd, TRUE);
461 BringWindowToTop(hwnd);
462 }
463 else
464 {
465 wxNode *node = wxModelessWindows.First();
466 while (node)
467 {
468 wxWindow *win = (wxWindow *)node->Data();
469 HWND hwnd = (HWND) win->GetHWND();
470 // Only enable again if not user-disabled.
471 #if 0
472 if (win->IsUserEnabled())
473 #endif // 0
474 EnableWindow(hwnd, TRUE);
475 node = node->Next();
476 }
477 }
478 // Try to highlight the correct window (the parent)
479 HWND hWndParent = 0;
480 if (GetParent())
481 {
482 hWndParent = (HWND) GetParent()->GetHWND();
483 if (hWndParent)
484 ::BringWindowToTop(hWndParent);
485 }
486 ShowWindow((HWND) GetHWND(), SW_HIDE);
487 m_modalShowing = FALSE;
488 }
489 }
490 else // !modal
491 {
492 if (show)
493 {
494 ShowWindow((HWND) GetHWND(), SW_SHOW);
495 BringWindowToTop((HWND) GetHWND());
496 }
497 else
498 {
499 // Try to highlight the correct window (the parent)
500 HWND hWndParent = 0;
501 if (GetParent())
502 {
503 hWndParent = (HWND) GetParent()->GetHWND();
504 if (hWndParent)
505 ::BringWindowToTop(hWndParent);
506 }
507
508 if ( m_hWnd )
509 ShowWindow((HWND) GetHWND(), SW_HIDE);
510 }
511 }
512
513 return TRUE;
514 }
515
516 #else // 1
517
518 bool wxDialog::IsModalShowing() const
519 {
520 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
521 }
522
523 void wxDialog::DoShowModal()
524 {
525 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
526
527 wxModalDialogs.Append(this);
528
529 wxWindow *parent = GetParent();
530
531 // remember where the focus was
532 wxWindow *winFocus = FindFocus();
533 if ( !winFocus )
534 {
535 winFocus = parent;
536 }
537 if ( !winFocus )
538 {
539 winFocus = wxTheApp->GetTopWindow();
540 }
541
542 // disable the parent window first
543 HWND hwndParent = parent ? GetHwndOf(parent) : (HWND)NULL;
544 if ( hwndParent )
545 {
546 ::EnableWindow(hwndParent, FALSE);
547 }
548
549 // enter the modal loop
550 while ( IsModalShowing() )
551 {
552 #if wxUSE_THREADS
553 wxMutexGuiLeaveOrEnter();
554 #endif // wxUSE_THREADS
555
556 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
557 ;
558
559 // a message came or no more idle processing to do
560 wxTheApp->DoMessage();
561 }
562
563 // reenable the parent window if any
564 if ( hwndParent )
565 {
566 ::EnableWindow(hwndParent, TRUE);
567 }
568
569 // and restore focus
570 if ( winFocus )
571 {
572 winFocus->SetFocus();
573 }
574 }
575
576 bool wxDialog::Show(bool show)
577 {
578 if ( !wxDialogBase::Show(show) )
579 {
580 // nothing to do
581 return FALSE;
582 }
583
584 if ( show )
585 {
586 // usually will result in TransferDataToWindow() being called
587 InitDialog();
588 }
589
590 if ( IsModal() )
591 {
592 if ( show )
593 {
594 DoShowModal();
595 }
596 else // end of modal dialog
597 {
598 // this will cause IsModalShowing() return FALSE and our local
599 // message loop will terminate
600 wxModalDialogs.DeleteObject(this);
601 }
602 }
603
604 return TRUE;
605 }
606
607 #endif // 0/1
608
609 // Replacement for Show(TRUE) for modal dialogs - returns return code
610 int wxDialog::ShowModal()
611 {
612 m_windowStyle |= wxDIALOG_MODAL;
613 Show(TRUE);
614 return GetReturnCode();
615 }
616
617 // NB: this function (surprizingly) may be called for both modal and modeless
618 // dialogs and should work for both of them
619 void wxDialog::EndModal(int retCode)
620 {
621 SetReturnCode(retCode);
622 Show(FALSE);
623 }
624
625 // ----------------------------------------------------------------------------
626 // wxWin event handlers
627 // ----------------------------------------------------------------------------
628
629 // Standard buttons
630 void wxDialog::OnOK(wxCommandEvent& event)
631 {
632 if ( Validate() && TransferDataFromWindow() )
633 {
634 EndModal(wxID_OK);
635 }
636 }
637
638 void wxDialog::OnApply(wxCommandEvent& event)
639 {
640 if ( Validate() )
641 TransferDataFromWindow();
642
643 // TODO probably need to disable the Apply button until things change again
644 }
645
646 void wxDialog::OnCancel(wxCommandEvent& event)
647 {
648 EndModal(wxID_CANCEL);
649 }
650
651 void wxDialog::OnCloseWindow(wxCloseEvent& event)
652 {
653 // We'll send a Cancel message by default, which may close the dialog.
654 // Check for looping if the Cancel event handler calls Close().
655
656 // Note that if a cancel button and handler aren't present in the dialog,
657 // nothing will happen when you close the dialog via the window manager, or
658 // via Close(). We wouldn't want to destroy the dialog by default, since
659 // the dialog may have been created on the stack. However, this does mean
660 // that calling dialog->Close() won't delete the dialog unless the handler
661 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
662 // destroy the dialog. The default OnCancel (above) simply ends a modal
663 // dialog, and hides a modeless dialog.
664
665 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
666 // lists here? don't dare to change it now, but should be done later!
667 static wxList closing;
668
669 if ( closing.Member(this) )
670 return;
671
672 closing.Append(this);
673
674 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
675 cancelEvent.SetEventObject( this );
676 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
677
678 closing.DeleteObject(this);
679 }
680
681 // Destroy the window (delayed, if a managed window)
682 bool wxDialog::Destroy()
683 {
684 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
685 _T("wxDialog destroyed twice") );
686
687 wxPendingDelete.Append(this);
688
689 return TRUE;
690 }
691
692 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event)
693 {
694 #if wxUSE_CTL3D
695 Ctl3dColorChange();
696 #else
697 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
698 Refresh();
699 #endif
700 }
701
702 // ---------------------------------------------------------------------------
703 // dialog window proc
704 // ---------------------------------------------------------------------------
705
706 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
707 {
708 long rc = 0;
709 bool processed = FALSE;
710
711 switch ( message )
712 {
713 case WM_CLOSE:
714 // if we can't close, tell the system that we processed the
715 // message - otherwise it would close us
716 processed = !Close();
717 break;
718 }
719
720 if ( !processed )
721 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
722
723 return rc;
724 }
725
726 #if wxUSE_CTL3D
727
728 // Define for each class of dialog and control
729 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
730 WXHWND WXUNUSED(pWnd),
731 WXUINT WXUNUSED(nCtlColor),
732 WXUINT message,
733 WXWPARAM wParam,
734 WXLPARAM lParam)
735 {
736 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
737 }
738
739 #endif // wxUSE_CTL3D
740