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