]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialog.cpp
fixed deleting pages from the notebook
[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 void 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
110 bool 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
195 bool wxDialog::EnableCloseButton(bool enable)
196 {
197 #ifndef __WXMICROWIN__
198 // get system (a.k.a. window) menu
199 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE /* get it */);
200 if ( !hmenu )
201 {
202 wxLogLastError(_T("GetSystemMenu"));
203
204 return FALSE;
205 }
206
207 // enabling/disabling the close item from it also automatically
208 // disables/enabling the close title bar button
209 if ( !::EnableMenuItem(hmenu, SC_CLOSE,
210 MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED)) )
211 {
212 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
213
214 return FALSE;
215 }
216
217 // update appearance immediately
218 if ( !::DrawMenuBar(GetHwnd()) )
219 {
220 wxLogLastError(_T("DrawMenuBar"));
221 }
222 #endif
223
224 return TRUE;
225 }
226
227 void wxDialog::SetModal(bool flag)
228 {
229 if ( flag )
230 {
231 m_windowStyle |= wxDIALOG_MODAL;
232
233 wxModelessWindows.DeleteObject(this);
234 }
235 else
236 {
237 m_windowStyle &= ~wxDIALOG_MODAL;
238
239 wxModelessWindows.Append(this);
240 }
241 }
242
243 wxDialog::~wxDialog()
244 {
245 m_isBeingDeleted = TRUE;
246
247 wxTopLevelWindows.DeleteObject(this);
248
249 // this will also reenable all the other windows for a modal dialog
250 Show(FALSE);
251
252 if ( !IsModal() )
253 wxModelessWindows.DeleteObject(this);
254
255 // If this is the last top-level window, exit.
256 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
257 {
258 wxTheApp->SetTopWindow(NULL);
259
260 if ( wxTheApp->GetExitOnFrameDelete() )
261 {
262 ::PostQuitMessage(0);
263 }
264 }
265 }
266
267 // ----------------------------------------------------------------------------
268 // kbd handling
269 // ----------------------------------------------------------------------------
270
271 // By default, pressing escape cancels the dialog
272 void wxDialog::OnCharHook(wxKeyEvent& event)
273 {
274 if (GetHWND())
275 {
276 // "Esc" works as an accelerator for the "Cancel" button, but it
277 // shouldn't close the dialog which doesn't have any cancel button
278 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
279 {
280 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
281 cancelEvent.SetEventObject( this );
282 GetEventHandler()->ProcessEvent(cancelEvent);
283
284 // ensure that there is another message for this window so the
285 // ShowModal loop will exit and won't get stuck in GetMessage().
286 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
287
288 return;
289 }
290 }
291
292 // We didn't process this event.
293 event.Skip();
294 }
295
296 // ----------------------------------------------------------------------------
297 // Windows dialog boxes can't be iconized
298 // ----------------------------------------------------------------------------
299
300 void wxDialog::Iconize(bool WXUNUSED(iconize))
301 {
302 }
303
304 bool wxDialog::IsIconized() const
305 {
306 return FALSE;
307 }
308
309 // ----------------------------------------------------------------------------
310 // size/position handling
311 // ----------------------------------------------------------------------------
312
313 void wxDialog::DoSetClientSize(int width, int height)
314 {
315 HWND hWnd = (HWND) GetHWND();
316 RECT rect;
317 ::GetClientRect(hWnd, &rect);
318
319 RECT rect2;
320 GetWindowRect(hWnd, &rect2);
321
322 // Find the difference between the entire window (title bar and all)
323 // and the client area; add this to the new client size to move the
324 // window
325 int actual_width = rect2.right - rect2.left - rect.right + width;
326 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
327
328 MoveWindow(hWnd, rect2.left, rect2.top, actual_width, actual_height, TRUE);
329
330 wxSizeEvent event(wxSize(actual_width, actual_height), m_windowId);
331 event.SetEventObject( this );
332 GetEventHandler()->ProcessEvent(event);
333 }
334
335 void wxDialog::DoGetPosition(int *x, int *y) const
336 {
337 RECT rect;
338 GetWindowRect(GetHwnd(), &rect);
339
340 if ( x )
341 *x = rect.left;
342 if ( y )
343 *y = rect.top;
344 }
345
346 // ----------------------------------------------------------------------------
347 // showing the dialogs
348 // ----------------------------------------------------------------------------
349
350 bool wxDialog::IsModal() const
351 {
352 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
353 }
354
355 bool wxDialog::IsModalShowing() const
356 {
357 return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast
358 }
359
360 void wxDialog::DoShowModal()
361 {
362 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
363 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
364
365 wxModalDialogs.Append(this);
366
367 wxWindow *parent = GetParent();
368
369 wxWindow* oldFocus = m_oldFocus;
370
371 // We have to remember the HWND because we need to check
372 // the HWND still exists (oldFocus can be garbage when the dialog
373 // exits, if it has been destroyed)
374 HWND hwndOldFocus = 0;
375 if (oldFocus)
376 hwndOldFocus = (HWND) oldFocus->GetHWND();
377
378 // remember where the focus was
379 if ( !oldFocus )
380 {
381 oldFocus = parent;
382 if ( parent )
383 hwndOldFocus = GetHwndOf(parent);
384 }
385
386 // disable all other app windows
387 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
388
389 m_windowDisabler = new wxWindowDisabler(this);
390
391 // enter the modal loop
392 while ( IsModalShowing() )
393 {
394 #if wxUSE_THREADS
395 wxMutexGuiLeaveOrEnter();
396 #endif // wxUSE_THREADS
397
398 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
399 ;
400
401 // a message came or no more idle processing to do
402 wxTheApp->DoMessage();
403 }
404
405 // and restore focus
406 // Note that this code MUST NOT access the dialog object's data
407 // in case the object has been deleted (which will be the case
408 // for a modal dialog that has been destroyed before calling EndModal).
409 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
410 {
411 // This is likely to prove that the object still exists
412 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
413 oldFocus->SetFocus();
414 }
415 }
416
417 bool wxDialog::Show(bool show)
418 {
419 if ( !show )
420 {
421 // if we had disabled other app windows, reenable them back now because
422 // if they stay disabled Windows will activate another window (one
423 // which is enabled, anyhow) and we will lose activation
424 if ( m_windowDisabler )
425 {
426 delete m_windowDisabler;
427 m_windowDisabler = NULL;
428 }
429 }
430
431 // ShowModal() may be called for already shown dialog
432 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
433 {
434 // nothing to do
435 return FALSE;
436 }
437
438 if ( show )
439 {
440 // usually will result in TransferDataToWindow() being called
441 InitDialog();
442 }
443
444 if ( IsModal() )
445 {
446 if ( show )
447 {
448 // modal dialog needs a parent window, so try to find one
449 if ( !GetParent() )
450 {
451 wxWindow *parent = wxTheApp->GetTopWindow();
452 if ( parent && parent != this && parent->IsShown() )
453 {
454 // use it
455 m_parent = parent;
456
457 // VZ: to make dialog behave properly we should reparent
458 // the dialog for Windows as well - unfortunately,
459 // following the docs for SetParent() results in this
460 // code which plainly doesn't work
461 #if 0
462 long dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
463 dwStyle &= ~WS_POPUP;
464 dwStyle |= WS_CHILD;
465 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyle);
466 ::SetParent(GetHwnd(), GetHwndOf(parent));
467 #endif // 0
468 }
469 }
470
471 DoShowModal();
472 }
473 else // end of modal dialog
474 {
475 // this will cause IsModalShowing() return FALSE and our local
476 // message loop will terminate
477 wxModalDialogs.DeleteObject(this);
478 }
479 }
480
481 return TRUE;
482 }
483
484 // a special version for Show(TRUE) for modal dialogs which returns return code
485 int wxDialog::ShowModal()
486 {
487 if ( !IsModal() )
488 {
489 SetModal(TRUE);
490 }
491
492 Show(TRUE);
493
494 return GetReturnCode();
495 }
496
497 // NB: this function (surprizingly) may be called for both modal and modeless
498 // dialogs and should work for both of them
499 void wxDialog::EndModal(int retCode)
500 {
501 SetReturnCode(retCode);
502
503 Show(FALSE);
504 }
505
506 // ----------------------------------------------------------------------------
507 // wxWin event handlers
508 // ----------------------------------------------------------------------------
509
510 // Standard buttons
511 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
512 {
513 if ( Validate() && TransferDataFromWindow() )
514 {
515 EndModal(wxID_OK);
516 }
517 }
518
519 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
520 {
521 if ( Validate() )
522 TransferDataFromWindow();
523
524 // TODO probably need to disable the Apply button until things change again
525 }
526
527 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
528 {
529 EndModal(wxID_CANCEL);
530 }
531
532 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
533 {
534 // We'll send a Cancel message by default, which may close the dialog.
535 // Check for looping if the Cancel event handler calls Close().
536
537 // Note that if a cancel button and handler aren't present in the dialog,
538 // nothing will happen when you close the dialog via the window manager, or
539 // via Close(). We wouldn't want to destroy the dialog by default, since
540 // the dialog may have been created on the stack. However, this does mean
541 // that calling dialog->Close() won't delete the dialog unless the handler
542 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
543 // destroy the dialog. The default OnCancel (above) simply ends a modal
544 // dialog, and hides a modeless dialog.
545
546 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
547 // lists here? don't dare to change it now, but should be done later!
548 static wxList closing;
549
550 if ( closing.Member(this) )
551 return;
552
553 closing.Append(this);
554
555 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
556 cancelEvent.SetEventObject( this );
557 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
558
559 closing.DeleteObject(this);
560 }
561
562 // Destroy the window (delayed, if a managed window)
563 bool wxDialog::Destroy()
564 {
565 wxCHECK_MSG( !wxPendingDelete.Member(this), FALSE,
566 _T("wxDialog destroyed twice") );
567
568 wxPendingDelete.Append(this);
569
570 return TRUE;
571 }
572
573 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
574 {
575 #if wxUSE_CTL3D
576 Ctl3dColorChange();
577 #else
578 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
579 Refresh();
580 #endif
581 }
582
583 // ---------------------------------------------------------------------------
584 // dialog window proc
585 // ---------------------------------------------------------------------------
586
587 long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
588 {
589 long rc = 0;
590 bool processed = FALSE;
591
592 switch ( message )
593 {
594 #if 0 // now that we got owner window right it doesn't seem to be needed
595 case WM_ACTIVATE:
596 switch ( LOWORD(wParam) )
597 {
598 case WA_ACTIVE:
599 case WA_CLICKACTIVE:
600 if ( IsModalShowing() && GetParent() )
601 {
602 // bring the owner window to top as the standard dialog
603 // boxes do
604 if ( !::SetWindowPos
605 (
606 GetHwndOf(GetParent()),
607 GetHwnd(),
608 0, 0,
609 0, 0,
610 SWP_NOACTIVATE |
611 SWP_NOMOVE |
612 SWP_NOSIZE
613 ) )
614 {
615 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
616 }
617 }
618 // fall through to process it normally as well
619 }
620 break;
621 #endif // 0
622
623 case WM_CLOSE:
624 // if we can't close, tell the system that we processed the
625 // message - otherwise it would close us
626 processed = !Close();
627 break;
628
629 #ifndef __WXMICROWIN__
630 case WM_SETCURSOR:
631 // we want to override the busy cursor for modal dialogs:
632 // typically, wxBeginBusyCursor() is called and then a modal dialog
633 // is shown, but the modal dialog shouldn't have hourglass cursor
634 if ( IsModalShowing() && wxIsBusy() )
635 {
636 // set our cursor for all windows (but see below)
637 wxCursor cursor = m_cursor;
638 if ( !cursor.Ok() )
639 cursor = wxCURSOR_ARROW;
640
641 ::SetCursor(GetHcursorOf(cursor));
642
643 // in any case, stop here and don't let wxWindow process this
644 // message (it would set the busy cursor)
645 processed = TRUE;
646
647 // but return FALSE to tell the child window (if the event
648 // comes from one of them and not from ourselves) that it can
649 // set its own cursor if it has one: thus, standard controls
650 // (e.g. text ctrl) still have correct cursors in a dialog
651 // invoked while wxIsBusy()
652 rc = FALSE;
653 }
654 break;
655 #endif
656 }
657
658 if ( !processed )
659 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
660
661 return rc;
662 }
663
664 #if wxUSE_CTL3D
665
666 // Define for each class of dialog and control
667 WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
668 WXHWND WXUNUSED(pWnd),
669 WXUINT WXUNUSED(nCtlColor),
670 WXUINT message,
671 WXWPARAM wParam,
672 WXLPARAM lParam)
673 {
674 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
675 }
676
677 #endif // wxUSE_CTL3D
678