ignore hidden buttons when emulating clicks on them
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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/button.h"
37 #include "wx/settings.h"
38 #include "wx/intl.h"
39 #include "wx/log.h"
40 #endif
41
42 #include "wx/msw/private.h"
43 #include "wx/log.h"
44 #include "wx/evtloop.h"
45 #include "wx/ptr_scpd.h"
46
47 #include "wx/msw/wrapcdlg.h"
48
49 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
50 #include "wx/msw/wince/resources.h"
51 #endif // __SMARTPHONE__ && __WXWINCE__
52
53 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
54 #include "wx/toolbar.h"
55 #endif
56
57 // ----------------------------------------------------------------------------
58 // wxWin macros
59 // ----------------------------------------------------------------------------
60
61 #if wxUSE_EXTENDED_RTTI
62 WX_DEFINE_FLAGS( wxDialogStyle )
63
64 wxBEGIN_FLAGS( wxDialogStyle )
65 // new style border flags, we put them first to
66 // use them for streaming out
67 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
68 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
69 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
70 wxFLAGS_MEMBER(wxBORDER_RAISED)
71 wxFLAGS_MEMBER(wxBORDER_STATIC)
72 wxFLAGS_MEMBER(wxBORDER_NONE)
73
74 // old style border flags
75 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
76 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
77 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
78 wxFLAGS_MEMBER(wxRAISED_BORDER)
79 wxFLAGS_MEMBER(wxSTATIC_BORDER)
80 wxFLAGS_MEMBER(wxNO_BORDER)
81
82 // standard window styles
83 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
84 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
85
86 // dialog styles
87 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
88 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
89 wxFLAGS_MEMBER(wxCAPTION)
90 wxFLAGS_MEMBER(wxTHICK_FRAME)
91 wxFLAGS_MEMBER(wxSYSTEM_MENU)
92 wxFLAGS_MEMBER(wxRESIZE_BORDER)
93 wxFLAGS_MEMBER(wxRESIZE_BOX)
94 wxFLAGS_MEMBER(wxCLOSE_BOX)
95 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
96 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
97 wxEND_FLAGS( wxDialogStyle )
98
99 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
100
101 wxBEGIN_PROPERTIES_TABLE(wxDialog)
102 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
103 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
104 wxEND_PROPERTIES_TABLE()
105
106 wxBEGIN_HANDLERS_TABLE(wxDialog)
107 wxEND_HANDLERS_TABLE()
108
109 wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
110
111 #else
112 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
113 #endif
114
115 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
116 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
117 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
118 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
119
120 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
121
122 EVT_CLOSE(wxDialog::OnCloseWindow)
123 END_EVENT_TABLE()
124
125 // ----------------------------------------------------------------------------
126 // wxDialogModalData
127 // ----------------------------------------------------------------------------
128
129 // this is simply a container for any data we need to implement modality which
130 // allows us to avoid changing wxDialog each time the implementation changes
131 class wxDialogModalData
132 {
133 public:
134 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
135
136 void RunLoop()
137 {
138 m_evtLoop.Run();
139 }
140
141 void ExitLoop()
142 {
143 m_evtLoop.Exit();
144 }
145
146 private:
147 wxModalEventLoop m_evtLoop;
148 };
149
150 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData);
151
152 // ============================================================================
153 // implementation
154 // ============================================================================
155
156 // ----------------------------------------------------------------------------
157 // wxDialog construction
158 // ----------------------------------------------------------------------------
159
160 void wxDialog::Init()
161 {
162 m_oldFocus = (wxWindow *)NULL;
163 m_isShown = false;
164 m_modalData = NULL;
165 m_endModalCalled = false;
166 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
167 m_dialogToolBar = NULL;
168 #endif
169 }
170
171 bool wxDialog::Create(wxWindow *parent,
172 wxWindowID id,
173 const wxString& title,
174 const wxPoint& pos,
175 const wxSize& size,
176 long style,
177 const wxString& name)
178 {
179 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
180
181 // save focus before doing anything which can potentially change it
182 m_oldFocus = FindFocus();
183
184 // All dialogs should really have this style
185 style |= wxTAB_TRAVERSAL;
186
187 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
188 return false;
189
190 if ( !m_hasFont )
191 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
192
193 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
194 SetLeftMenu(wxID_OK, _("OK"));
195 #endif
196 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
197 CreateToolBar();
198 #endif
199
200 return true;
201 }
202
203 // deprecated ctor
204 wxDialog::wxDialog(wxWindow *parent,
205 const wxString& title,
206 bool WXUNUSED(modal),
207 int x,
208 int y,
209 int w,
210 int h,
211 long style,
212 const wxString& name)
213 {
214 Init();
215
216 Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name);
217 }
218
219 void wxDialog::SetModal(bool WXUNUSED(flag))
220 {
221 // nothing to do, obsolete method
222 }
223
224 wxDialog::~wxDialog()
225 {
226 m_isBeingDeleted = true;
227
228 // this will also reenable all the other windows for a modal dialog
229 Show(false);
230 }
231
232 // ----------------------------------------------------------------------------
233 // showing the dialogs
234 // ----------------------------------------------------------------------------
235
236 bool wxDialog::IsModalShowing() const
237 {
238 return IsModal();
239 }
240
241 wxWindow *wxDialog::FindSuitableParent() const
242 {
243 // first try to use the currently active window
244 HWND hwndFg = ::GetForegroundWindow();
245 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
246 : NULL;
247 if ( !parent )
248 {
249 // next try the main app window
250 parent = wxTheApp->GetTopWindow();
251 }
252
253 // finally, check if the parent we found is really suitable
254 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
255 {
256 // don't use this one
257 parent = NULL;
258 }
259
260 return parent;
261 }
262
263 bool wxDialog::Show(bool show)
264 {
265 if ( show == IsShown() )
266 return false;
267
268 if ( !show && m_modalData )
269 {
270 // we need to do this before calling wxDialogBase version because if we
271 // had disabled other app windows, they must be reenabled right now as
272 // if they stay disabled Windows will activate another window (one
273 // which is enabled, anyhow) when we're hidden in the base class Show()
274 // and we will lose activation
275 m_modalData->ExitLoop();
276 }
277
278 if ( show )
279 {
280 // this usually will result in TransferDataToWindow() being called
281 // which will change the controls values so do it before showing as
282 // otherwise we could have some flicker
283 InitDialog();
284 }
285
286 wxDialogBase::Show(show);
287
288 if ( show )
289 {
290 // dialogs don't get WM_SIZE message after creation unlike most (all?)
291 // other windows and so could start their life non laid out correctly
292 // if we didn't call Layout() from here
293 //
294 // NB: normally we should call it just the first time but doing it
295 // every time is simpler than keeping a flag
296 Layout();
297 }
298
299 return true;
300 }
301
302 void wxDialog::Raise()
303 {
304 ::SetForegroundWindow(GetHwnd());
305 }
306
307 // show dialog modally
308 int wxDialog::ShowModal()
309 {
310 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
311
312 m_endModalCalled = false;
313
314 Show();
315
316 // EndModal may have been called from InitDialog handler (called from
317 // inside Show()), which would cause an infinite loop if we didn't take it
318 // into account
319 if ( !m_endModalCalled )
320 {
321 // modal dialog needs a parent window, so try to find one
322 wxWindow *parent = GetParent();
323 if ( !parent )
324 {
325 parent = FindSuitableParent();
326 }
327
328 // remember where the focus was
329 wxWindow *oldFocus = m_oldFocus;
330 if ( !oldFocus )
331 {
332 // VZ: do we really want to do this?
333 oldFocus = parent;
334 }
335
336 // We have to remember the HWND because we need to check
337 // the HWND still exists (oldFocus can be garbage when the dialog
338 // exits, if it has been destroyed)
339 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
340
341
342 // enter and run the modal loop
343 {
344 wxDialogModalDataTiedPtr modalData(&m_modalData,
345 new wxDialogModalData(this));
346 modalData->RunLoop();
347 }
348
349
350 // and restore focus
351 // Note that this code MUST NOT access the dialog object's data
352 // in case the object has been deleted (which will be the case
353 // for a modal dialog that has been destroyed before calling EndModal).
354 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
355 {
356 // This is likely to prove that the object still exists
357 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
358 oldFocus->SetFocus();
359 }
360 }
361
362 return GetReturnCode();
363 }
364
365 void wxDialog::EndModal(int retCode)
366 {
367 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
368
369 m_endModalCalled = true;
370 SetReturnCode(retCode);
371
372 Hide();
373 }
374
375 void wxDialog::EndDialog(int rc)
376 {
377 if ( IsModal() )
378 EndModal(rc);
379 else
380 Hide();
381 }
382
383 // ----------------------------------------------------------------------------
384 // wxWin event handlers
385 // ----------------------------------------------------------------------------
386
387 bool wxDialog::EmulateButtonClickIfPresent(int id)
388 {
389 wxButton *btn = wxDynamicCast(FindWindow(id), wxButton);
390
391 if ( !btn || !btn->IsEnabled() || !btn->IsShown() )
392 return false;
393
394 btn->MSWCommand(BN_CLICKED, 0 /* unused */);
395 return true;
396 }
397
398 // Standard buttons
399 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
400 {
401 if ( Validate() && TransferDataFromWindow() )
402 {
403 EndDialog(wxID_OK);
404 }
405 }
406
407 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
408 {
409 if ( Validate() )
410 TransferDataFromWindow();
411
412 // TODO probably need to disable the Apply button until things change again
413 }
414
415 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
416 {
417 EndDialog(wxID_CANCEL);
418 }
419
420 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
421 {
422 // We'll send a Cancel message by default, which may close the dialog.
423 // Check for looping if the Cancel event handler calls Close().
424
425 // Note that if a cancel button and handler aren't present in the dialog,
426 // nothing will happen when you close the dialog via the window manager, or
427 // via Close(). We wouldn't want to destroy the dialog by default, since
428 // the dialog may have been created on the stack. However, this does mean
429 // that calling dialog->Close() won't delete the dialog unless the handler
430 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
431 // destroy the dialog. The default OnCancel (above) simply ends a modal
432 // dialog, and hides a modeless dialog.
433
434 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
435 // lists here? don't dare to change it now, but should be done later!
436 static wxList closing;
437
438 if ( closing.Member(this) )
439 return;
440
441 closing.Append(this);
442
443 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
444 cancelEvent.SetEventObject( this );
445 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
446
447 closing.DeleteObject(this);
448 }
449
450 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
451 {
452 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
453 Refresh();
454 }
455
456 #ifdef __POCKETPC__
457 // Responds to the OK button in a PocketPC titlebar. This
458 // can be overridden, or you can change the id used for
459 // sending the event, by calling SetAffirmativeId.
460 bool wxDialog::DoOK()
461 {
462 const int idOk = GetAffirmativeId();
463 if ( EmulateButtonClickIfPresent(idOk) )
464 return true;
465
466 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
467 event.SetEventObject(this);
468
469 return GetEventHandler()->ProcessEvent(event);
470 }
471 #endif // __POCKETPC__
472
473 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
474 // create main toolbar by calling OnCreateToolBar()
475 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
476 {
477 m_dialogToolBar = OnCreateToolBar(style, winid, name);
478
479 return m_dialogToolBar;
480 }
481
482 // return a new toolbar
483 wxToolBar *wxDialog::OnCreateToolBar(long style,
484 wxWindowID winid,
485 const wxString& name)
486 {
487 return new wxToolMenuBar(this, winid,
488 wxDefaultPosition, wxDefaultSize,
489 style, name);
490 }
491 #endif
492
493 // ---------------------------------------------------------------------------
494 // dialog Windows messages processing
495 // ---------------------------------------------------------------------------
496
497 bool wxDialog::MSWProcessMessage(WXMSG* pMsg)
498 {
499 const MSG * const msg = wx_reinterpret_cast(MSG *, pMsg);
500 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_ESCAPE )
501 {
502 int idCancel = GetEscapeId();
503 switch ( idCancel )
504 {
505 case wxID_NONE:
506 // don't handle Esc specially at all
507 break;
508
509 case wxID_ANY:
510 // this value is special: it means translate Esc to wxID_CANCEL
511 // but if there is no such button, then fall back to wxID_OK
512 if ( EmulateButtonClickIfPresent(wxID_CANCEL) )
513 return true;
514 idCancel = wxID_OK;
515 // fall through
516
517 default:
518 // translate Esc to button press for the button with given id
519 if ( EmulateButtonClickIfPresent(idCancel) )
520 return true;
521 }
522 }
523
524 return wxDialogBase::MSWProcessMessage(pMsg);
525 }
526
527 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
528 {
529 WXLRESULT rc = 0;
530 bool processed = false;
531
532 switch ( message )
533 {
534 #ifdef __WXWINCE__
535 // react to pressing the OK button in the title
536 case WM_COMMAND:
537 {
538 switch ( LOWORD(wParam) )
539 {
540 #ifdef __POCKETPC__
541 case IDOK:
542 processed = DoOK();
543 if (!processed)
544 processed = !Close();
545 #endif
546 #ifdef __SMARTPHONE__
547 case IDM_LEFT:
548 case IDM_RIGHT:
549 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
550 break;
551 #endif // __SMARTPHONE__
552 }
553 break;
554 }
555 #endif
556 case WM_CLOSE:
557 // if we can't close, tell the system that we processed the
558 // message - otherwise it would close us
559 processed = !Close();
560 break;
561
562 case WM_SIZE:
563 // the Windows dialogs unfortunately are not meant to be resizeable
564 // at all and their standard class doesn't include CS_[VH]REDRAW
565 // styles which means that the window is not refreshed properly
566 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
567 // help with it - so we have to refresh it manually which certainly
568 // creates flicker but at least doesn't show garbage on the screen
569 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
570 processed = true;
571 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
572 {
573 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
574 }
575 break;
576
577 #ifndef __WXMICROWIN__
578 case WM_SETCURSOR:
579 // we want to override the busy cursor for modal dialogs:
580 // typically, wxBeginBusyCursor() is called and then a modal dialog
581 // is shown, but the modal dialog shouldn't have hourglass cursor
582 if ( IsModal() && wxIsBusy() )
583 {
584 // set our cursor for all windows (but see below)
585 wxCursor cursor = m_cursor;
586 if ( !cursor.Ok() )
587 cursor = wxCURSOR_ARROW;
588
589 ::SetCursor(GetHcursorOf(cursor));
590
591 // in any case, stop here and don't let wxWindow process this
592 // message (it would set the busy cursor)
593 processed = true;
594
595 // but return false to tell the child window (if the event
596 // comes from one of them and not from ourselves) that it can
597 // set its own cursor if it has one: thus, standard controls
598 // (e.g. text ctrl) still have correct cursors in a dialog
599 // invoked while wxIsBusy()
600 rc = false;
601 }
602 break;
603 #endif // __WXMICROWIN__
604 }
605
606 if ( !processed )
607 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
608
609 return rc;
610 }
611