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