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