]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dialog.cpp
yet another attempt to wxFrame's last focus bug
[wxWidgets.git] / src / msw / dialog.cpp
... / ...
CommitLineData
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
65extern wxList WXDLLEXPORT wxPendingDelete;
66
67// all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68wxWindowList wxModelessWindows;
69
70// all modal dialogs currently shown
71static wxWindowList wxModalDialogs;
72
73// ----------------------------------------------------------------------------
74// wxWin macros
75// ----------------------------------------------------------------------------
76
77IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel)
78
79BEGIN_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)
89END_EVENT_TABLE()
90
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wxDialog construction
97// ----------------------------------------------------------------------------
98
99void wxDialog::Init()
100{
101 m_oldFocus = (wxWindow *)NULL;
102
103 m_isShown = FALSE;
104
105 m_windowDisabler = (wxWindowDisabler *)NULL;
106
107 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
108}
109
110bool wxDialog::Create(wxWindow *parent,
111 wxWindowID id,
112 const wxString& title,
113 const wxPoint& pos,
114 const wxSize& size,
115 long style,
116 const wxString& name)
117{
118 Init();
119
120 m_oldFocus = FindFocus();
121
122 SetName(name);
123
124 wxTopLevelWindows.Append(this);
125
126 if ( parent )
127 parent->AddChild(this);
128
129 if ( id == -1 )
130 m_windowId = (int)NewControlId();
131 else
132 m_windowId = id;
133
134 int x = pos.x;
135 int y = pos.y;
136 int width = size.x;
137 int height = size.y;
138
139 if (x < 0)
140 x = wxDIALOG_DEFAULT_X;
141 if (y < 0)
142 y = wxDIALOG_DEFAULT_Y;
143
144 m_windowStyle = style;
145
146 if (width < 0)
147 width = wxDIALOG_DEFAULT_WIDTH;
148 if (height < 0)
149 height = wxDIALOG_DEFAULT_HEIGHT;
150
151 // All dialogs should really have this style
152 m_windowStyle |= wxTAB_TRAVERSAL;
153
154 WXDWORD extendedStyle = MakeExtendedStyle(m_windowStyle);
155 if (m_windowStyle & wxSTAY_ON_TOP)
156 extendedStyle |= WS_EX_TOPMOST;
157
158#ifndef __WIN16__
159 if (m_exStyle & wxDIALOG_EX_CONTEXTHELP)
160 extendedStyle |= WS_EX_CONTEXTHELP;
161#endif
162
163 // Allows creation of dialogs with & without captions under MSWindows,
164 // resizeable or not (but a resizeable dialog always has caption -
165 // otherwise it would look too strange)
166 const wxChar *dlg;
167 if ( style & wxRESIZE_BORDER )
168 dlg = wxT("wxResizeableDialog");
169 else if ( style & wxCAPTION )
170 dlg = wxT("wxCaptionDialog");
171 else
172 dlg = wxT("wxNoCaptionDialog");
173 MSWCreate(m_windowId, parent, NULL, this, NULL,
174 x, y, width, height,
175 0, // style is not used if we have dlg template
176 dlg,
177 extendedStyle);
178
179 HWND hwnd = (HWND)GetHWND();
180
181 if ( !hwnd )
182 {
183 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
184
185 return FALSE;
186 }
187
188 SubclassWin(GetHWND());
189
190 SetWindowText(hwnd, title);
191
192 return TRUE;
193}
194
195void wxDialog::SetModal(bool flag)
196{
197 if ( flag )
198 {
199 m_windowStyle |= wxDIALOG_MODAL;
200
201 wxModelessWindows.DeleteObject(this);
202 }
203 else
204 {
205 m_windowStyle &= ~wxDIALOG_MODAL;
206
207 wxModelessWindows.Append(this);
208 }
209}
210
211wxDialog::~wxDialog()
212{
213 m_isBeingDeleted = TRUE;
214
215 wxTopLevelWindows.DeleteObject(this);
216
217 // this will also reenable all the other windows for a modal dialog
218 Show(FALSE);
219
220 if ( !IsModal() )
221 wxModelessWindows.DeleteObject(this);
222
223 // If this is the last top-level window, exit.
224 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
225 {
226 wxTheApp->SetTopWindow(NULL);
227
228 if ( wxTheApp->GetExitOnFrameDelete() )
229 {
230 ::PostQuitMessage(0);
231 }
232 }
233}
234
235// ----------------------------------------------------------------------------
236// kbd handling
237// ----------------------------------------------------------------------------
238
239// By default, pressing escape cancels the dialog
240void wxDialog::OnCharHook(wxKeyEvent& event)
241{
242 if (GetHWND())
243 {
244 // "Esc" works as an accelerator for the "Cancel" button, but it
245 // shouldn't close the dialog which doesn't have any cancel button
246 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
247 {
248 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
249 cancelEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent(cancelEvent);
251
252 // ensure that there is another message for this window so the
253 // ShowModal loop will exit and won't get stuck in GetMessage().
254 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
255
256 return;
257 }
258 }
259
260 // We didn't process this event.
261 event.Skip();
262}
263
264// ----------------------------------------------------------------------------
265// Windows dialog boxes can't be iconized
266// ----------------------------------------------------------------------------
267
268void wxDialog::Iconize(bool WXUNUSED(iconize))
269{
270}
271
272bool wxDialog::IsIconized() const
273{
274 return FALSE;
275}
276
277// ----------------------------------------------------------------------------
278// size/position handling
279// ----------------------------------------------------------------------------
280
281void wxDialog::DoSetClientSize(int width, int height)
282{
283 HWND hWnd = (HWND) GetHWND();
284 RECT rect;
285 ::GetClientRect(hWnd, &rect);
286
287 RECT rect2;
288 GetWindowRect(hWnd, &rect2);
289
290 // Find the difference between the entire window (title bar and all)
291 // and the client area; add this to the new client size to move the
292 // window
293 int actual_width = rect2.right - rect2.left - rect.right + width;
294 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
295
296 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
297
298 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
299 event.SetEventObject( this );
300 GetEventHandler()->ProcessEvent(event);
301}
302
303void wxDialog::DoGetPosition(int *x, int *y) const
304{
305 RECT rect;
306 GetWindowRect(GetHwnd(), &rect);
307
308 if ( x )
309 *x = rect.left;
310 if ( y )
311 *y = rect.top;
312}
313
314// ----------------------------------------------------------------------------
315// showing the dialogs
316// ----------------------------------------------------------------------------
317
318bool wxDialog::IsModal() const
319{
320 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
321}
322
323bool wxDialog::IsModalShowing() const
324{
325 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
326}
327
328void wxDialog::DoShowModal()
329{
330 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
331 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
332
333 wxModalDialogs.Append(this);
334
335 wxWindow *parent = GetParent();
336
337 wxWindow* oldFocus = m_oldFocus;
338
339 // We have to remember the HWND because we need to check
340 // the HWND still exists (oldFocus can be garbage when the dialog
341 // exits, if it has been destroyed)
342 HWND hwndOldFocus = 0;
343 if (oldFocus)
344 hwndOldFocus = (HWND) oldFocus->GetHWND();
345
346 // remember where the focus was
347 if ( !oldFocus )
348 {
349 oldFocus = parent;
350 if ( parent )
351 hwndOldFocus = GetHwndOf(parent);
352 }
353
354 // disable all other app windows
355 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
356
357 m_windowDisabler = new wxWindowDisabler(this);
358
359 // enter the modal loop
360 while ( IsModalShowing() )
361 {
362#if wxUSE_THREADS
363 wxMutexGuiLeaveOrEnter();
364#endif // wxUSE_THREADS
365
366 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
367 ;
368
369 // a message came or no more idle processing to do
370 wxTheApp->DoMessage();
371 }
372
373 // and restore focus
374 // Note that this code MUST NOT access the dialog object's data
375 // in case the object has been deleted (which will be the case
376 // for a modal dialog that has been destroyed before calling EndModal).
377 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
378 {
379 // This is likely to prove that the object still exists
380 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
381 oldFocus->SetFocus();
382 }
383}
384
385bool wxDialog::Show(bool show)
386{
387 if ( !show )
388 {
389 // if we had disabled other app windows, reenable them back now because
390 // if they stay disabled Windows will activate another window (one
391 // which is enabled, anyhow) and we will lose activation
392 if ( m_windowDisabler )
393 {
394 delete m_windowDisabler;
395 m_windowDisabler = NULL;
396 }
397 }
398
399 // ShowModal() may be called for already shown dialog
400 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
401 {
402 // nothing to do
403 return FALSE;
404 }
405
406 if ( show )
407 {
408 // usually will result in TransferDataToWindow() being called
409 InitDialog();
410 }
411
412 if ( IsModal() )
413 {
414 if ( show )
415 {
416 // modal dialog needs a parent window, so try to find one
417 if ( !GetParent() )
418 {
419 wxWindow *parent = wxTheApp->GetTopWindow();
420 if ( parent && parent != this && parent->IsShown() )
421 {
422 // use it
423 m_parent = parent;
424
425 // VZ: to make dialog behave properly we should reparent
426 // the dialog for Windows as well - unfortunately,
427 // following the docs for SetParent() results in this
428 // code which plainly doesn't work
429#if 0
430 long dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
431 dwStyle &= ~WS_POPUP;
432 dwStyle |= WS_CHILD;
433 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyle);
434 ::SetParent(GetHwnd(), GetHwndOf(parent));
435#endif // 0
436 }
437 }
438
439 DoShowModal();
440 }
441 else // end of modal dialog
442 {
443 // this will cause IsModalShowing() return FALSE and our local
444 // message loop will terminate
445 wxModalDialogs.DeleteObject(this);
446 }
447 }
448
449 return TRUE;
450}
451
452// a special version for Show(TRUE) for modal dialogs which returns return code
453int wxDialog::ShowModal()
454{
455 if ( !IsModal() )
456 {
457 SetModal(TRUE);
458 }
459
460 Show(TRUE);
461
462 return GetReturnCode();
463}
464
465// NB: this function (surprizingly) may be called for both modal and modeless
466// dialogs and should work for both of them
467void wxDialog::EndModal(int retCode)
468{
469 SetReturnCode(retCode);
470
471 Show(FALSE);
472}
473
474// ----------------------------------------------------------------------------
475// wxWin event handlers
476// ----------------------------------------------------------------------------
477
478// Standard buttons
479void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
480{
481 if ( Validate() && TransferDataFromWindow() )
482 {
483 EndModal(wxID_OK);
484 }
485}
486
487void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
488{
489 if ( Validate() )
490 TransferDataFromWindow();
491
492 // TODO probably need to disable the Apply button until things change again
493}
494
495void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
496{
497 EndModal(wxID_CANCEL);
498}
499
500void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
501{
502 // We'll send a Cancel message by default, which may close the dialog.
503 // Check for looping if the Cancel event handler calls Close().
504
505 // Note that if a cancel button and handler aren't present in the dialog,
506 // nothing will happen when you close the dialog via the window manager, or
507 // via Close(). We wouldn't want to destroy the dialog by default, since
508 // the dialog may have been created on the stack. However, this does mean
509 // that calling dialog->Close() won't delete the dialog unless the handler
510 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
511 // destroy the dialog. The default OnCancel (above) simply ends a modal
512 // dialog, and hides a modeless dialog.
513
514 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
515 // lists here? don't dare to change it now, but should be done later!
516 static wxList closing;
517
518 if ( closing.Member(this) )
519 return;
520
521 closing.Append(this);
522
523 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
524 cancelEvent.SetEventObject( this );
525 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
526
527 closing.DeleteObject(this);
528}
529
530// Destroy the window (delayed, if a managed window)
531bool wxDialog::Destroy()
532{
533 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
534 _T("wxDialog destroyed twice") );
535
536 wxPendingDelete.Append(this);
537
538 return TRUE;
539}
540
541void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
542{
543#if wxUSE_CTL3D
544 Ctl3dColorChange();
545#else
546 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
547 Refresh();
548#endif
549}
550
551// ---------------------------------------------------------------------------
552// dialog window proc
553// ---------------------------------------------------------------------------
554
555long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
556{
557 long rc = 0;
558 bool processed = FALSE;
559
560 switch ( message )
561 {
562#if 0 // now that we got owner window right it doesn't seem to be needed
563 case WM_ACTIVATE:
564 switch ( LOWORD(wParam) )
565 {
566 case WA_ACTIVE:
567 case WA_CLICKACTIVE:
568 if ( IsModalShowing() && GetParent() )
569 {
570 // bring the owner window to top as the standard dialog
571 // boxes do
572 if ( !::SetWindowPos
573 (
574 GetHwndOf(GetParent()),
575 GetHwnd(),
576 0, 0,
577 0, 0,
578 SWP_NOACTIVATE |
579 SWP_NOMOVE |
580 SWP_NOSIZE
581 ) )
582 {
583 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
584 }
585 }
586 // fall through to process it normally as well
587 }
588 break;
589#endif // 0
590
591 case WM_CLOSE:
592 // if we can't close, tell the system that we processed the
593 // message - otherwise it would close us
594 processed = !Close();
595 break;
596
597 case WM_SETCURSOR:
598 // we want to override the busy cursor for modal dialogs:
599 // typically, wxBeginBusyCursor() is called and then a modal dialog
600 // is shown, but the modal dialog shouldn't have hourglass cursor
601 if ( IsModalShowing() && wxIsBusy() )
602 {
603 // set our cursor for all windows (but see below)
604 wxCursor cursor = m_cursor;
605 if ( !cursor.Ok() )
606 cursor = wxCURSOR_ARROW;
607
608 ::SetCursor(GetHcursorOf(cursor));
609
610 // in any case, stop here and don't let wxWindow process this
611 // message (it would set the busy cursor)
612 processed = TRUE;
613
614 // but return FALSE to tell the child window (if the event
615 // comes from one of them and not from ourselves) that it can
616 // set its own cursor if it has one: thus, standard controls
617 // (e.g. text ctrl) still have correct cursors in a dialog
618 // invoked while wxIsBusy()
619 rc = FALSE;
620 }
621 break;
622 }
623
624 if ( !processed )
625 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
626
627 return rc;
628}
629
630#if wxUSE_CTL3D
631
632// Define for each class of dialog and control
633WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
634 WXHWND WXUNUSED(pWnd),
635 WXUINT WXUNUSED(nCtlColor),
636 WXUINT message,
637 WXWPARAM wParam,
638 WXLPARAM lParam)
639{
640 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
641}
642
643#endif // wxUSE_CTL3D
644