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