assert if ShowModal() is called after a previous Show() (#1416)
[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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/dialog.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapcdlg.h"
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/toolbar.h"
39 #endif
40
41 #include "wx/msw/private.h"
42 #include "wx/evtloop.h"
43 #include "wx/ptr_scpd.h"
44
45 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
46 #include "wx/msw/wince/resources.h"
47 #endif // __SMARTPHONE__ && __WXWINCE__
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxDialogStyle )
55
56 wxBEGIN_FLAGS( wxDialogStyle )
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62 wxFLAGS_MEMBER(wxBORDER_RAISED)
63 wxFLAGS_MEMBER(wxBORDER_STATIC)
64 wxFLAGS_MEMBER(wxBORDER_NONE)
65
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70 wxFLAGS_MEMBER(wxRAISED_BORDER)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER)
72 wxFLAGS_MEMBER(wxNO_BORDER)
73
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
77
78 // dialog styles
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
81 wxFLAGS_MEMBER(wxCAPTION)
82 #if WXWIN_COMPATIBILITY_2_6
83 wxFLAGS_MEMBER(wxTHICK_FRAME)
84 #endif // WXWIN_COMPATIBILITY_2_6
85 wxFLAGS_MEMBER(wxSYSTEM_MENU)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER)
87 #if WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxRESIZE_BOX)
89 #endif // WXWIN_COMPATIBILITY_2_6
90 wxFLAGS_MEMBER(wxCLOSE_BOX)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
93 wxEND_FLAGS( wxDialogStyle )
94
95 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
96
97 wxBEGIN_PROPERTIES_TABLE(wxDialog)
98 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100 wxEND_PROPERTIES_TABLE()
101
102 wxBEGIN_HANDLERS_TABLE(wxDialog)
103 wxEND_HANDLERS_TABLE()
104
105 wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
106
107 #else
108 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
109 #endif
110
111 // ----------------------------------------------------------------------------
112 // wxDialogModalData
113 // ----------------------------------------------------------------------------
114
115 // this is simply a container for any data we need to implement modality which
116 // allows us to avoid changing wxDialog each time the implementation changes
117 class wxDialogModalData
118 {
119 public:
120 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
121
122 void RunLoop()
123 {
124 m_evtLoop.Run();
125 }
126
127 void ExitLoop()
128 {
129 m_evtLoop.Exit();
130 }
131
132 private:
133 wxModalEventLoop m_evtLoop;
134 };
135
136 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
137
138 // ============================================================================
139 // implementation
140 // ============================================================================
141
142 // ----------------------------------------------------------------------------
143 // wxDialog construction
144 // ----------------------------------------------------------------------------
145
146 void wxDialog::Init()
147 {
148 m_oldFocus = (wxWindow *)NULL;
149 m_isShown = false;
150 m_modalData = NULL;
151 m_endModalCalled = false;
152 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
153 m_dialogToolBar = NULL;
154 #endif
155 m_hGripper = 0;
156 }
157
158 bool wxDialog::Create(wxWindow *parent,
159 wxWindowID id,
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
165 {
166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
167
168 // save focus before doing anything which can potentially change it
169 m_oldFocus = FindFocus();
170
171 // All dialogs should really have this style
172 style |= wxTAB_TRAVERSAL;
173
174 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
175 return false;
176
177 if ( !m_hasFont )
178 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
179
180 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
181 SetLeftMenu(wxID_OK, _("OK"));
182 #endif
183 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
184 CreateToolBar();
185 #endif
186
187 if ( HasFlag(wxRESIZE_BORDER) )
188 {
189 CreateGripper();
190
191 Connect(wxEVT_CREATE,
192 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
193 }
194
195 return true;
196 }
197
198 wxDialog::~wxDialog()
199 {
200 m_isBeingDeleted = true;
201
202 // this will also reenable all the other windows for a modal dialog
203 Show(false);
204
205 DestroyGripper();
206 }
207
208 // ----------------------------------------------------------------------------
209 // showing the dialogs
210 // ----------------------------------------------------------------------------
211
212 wxWindow *wxDialog::FindSuitableParent() const
213 {
214 // first try to use the currently active window
215 HWND hwndFg = ::GetForegroundWindow();
216 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
217 : NULL;
218 if ( !parent )
219 {
220 // next try the main app window
221 parent = wxTheApp->GetTopWindow();
222 }
223
224 // finally, check if the parent we found is really suitable
225 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
226 {
227 // don't use this one
228 parent = NULL;
229 }
230
231 return parent;
232 }
233
234 bool wxDialog::Show(bool show)
235 {
236 if ( show == IsShown() )
237 return false;
238
239 if ( !show && m_modalData )
240 {
241 // we need to do this before calling wxDialogBase version because if we
242 // had disabled other app windows, they must be reenabled right now as
243 // if they stay disabled Windows will activate another window (one
244 // which is enabled, anyhow) when we're hidden in the base class Show()
245 // and we will lose activation
246 m_modalData->ExitLoop();
247 }
248
249 if ( show )
250 {
251 if (CanDoLayoutAdaptation())
252 DoLayoutAdaptation();
253
254 // this usually will result in TransferDataToWindow() being called
255 // which will change the controls values so do it before showing as
256 // otherwise we could have some flicker
257 InitDialog();
258 }
259
260 wxDialogBase::Show(show);
261
262 if ( show )
263 {
264 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
265 // reason so generate it ourselves for consistency with frames and
266 // dialogs in other ports
267 //
268 // NB: normally we should call it just the first time but doing it
269 // every time is simpler than keeping a flag
270 const wxSize size = GetClientSize();
271 ::SendMessage(GetHwnd(), WM_SIZE,
272 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
273 }
274
275 return true;
276 }
277
278 void wxDialog::Raise()
279 {
280 ::SetForegroundWindow(GetHwnd());
281 }
282
283 // show dialog modally
284 int wxDialog::ShowModal()
285 {
286 wxASSERT_MSG(!IsShown(),
287 _T("ShowModal() can't be called for an already shown dialog"));
288
289 m_endModalCalled = false;
290
291 Show();
292
293 // EndModal may have been called from InitDialog handler (called from
294 // inside Show()), which would cause an infinite loop if we didn't take it
295 // into account
296 if ( !m_endModalCalled )
297 {
298 // modal dialog needs a parent window, so try to find one
299 wxWindow *parent = GetParent();
300 if ( !parent )
301 {
302 parent = FindSuitableParent();
303 }
304
305 // remember where the focus was
306 wxWindow *oldFocus = m_oldFocus;
307 if ( !oldFocus )
308 {
309 // VZ: do we really want to do this?
310 oldFocus = parent;
311 }
312
313 // We have to remember the HWND because we need to check
314 // the HWND still exists (oldFocus can be garbage when the dialog
315 // exits, if it has been destroyed)
316 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
317
318
319 // enter and run the modal loop
320 {
321 wxDialogModalDataTiedPtr modalData(&m_modalData,
322 new wxDialogModalData(this));
323 modalData->RunLoop();
324 }
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 return GetReturnCode();
340 }
341
342 void wxDialog::EndModal(int retCode)
343 {
344 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
345
346 m_endModalCalled = true;
347 SetReturnCode(retCode);
348
349 Hide();
350 }
351
352 // ----------------------------------------------------------------------------
353 // wxDialog gripper handling
354 // ----------------------------------------------------------------------------
355
356 void wxDialog::SetWindowStyleFlag(long style)
357 {
358 wxDialogBase::SetWindowStyleFlag(style);
359
360 if ( HasFlag(wxRESIZE_BORDER) )
361 CreateGripper();
362 else
363 DestroyGripper();
364 }
365
366 void wxDialog::CreateGripper()
367 {
368 if ( !m_hGripper )
369 {
370 // just create it here, it will be positioned and shown later
371 m_hGripper = (WXHWND)::CreateWindow
372 (
373 wxT("SCROLLBAR"),
374 wxT(""),
375 WS_CHILD |
376 WS_CLIPSIBLINGS |
377 SBS_SIZEGRIP |
378 SBS_SIZEBOX |
379 SBS_SIZEBOXBOTTOMRIGHTALIGN,
380 0, 0, 0, 0,
381 GetHwnd(),
382 0,
383 wxGetInstance(),
384 NULL
385 );
386 }
387 }
388
389 void wxDialog::DestroyGripper()
390 {
391 if ( m_hGripper )
392 {
393 // we used to have trouble with gripper appearing on top (and hence
394 // overdrawing) the other, real, dialog children -- check that this
395 // isn't the case automatically
396 wxASSERT_MSG( ::GetNextWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
397 _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
398 ::DestroyWindow((HWND) m_hGripper);
399 m_hGripper = 0;
400 }
401 }
402
403 void wxDialog::ShowGripper(bool show)
404 {
405 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
406
407 if ( show )
408 ResizeGripper();
409
410 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
411 }
412
413 void wxDialog::ResizeGripper()
414 {
415 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
416
417 HWND hwndGripper = (HWND)m_hGripper;
418
419 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
420 const wxSize size = GetClientSize() - rectGripper.GetSize();
421
422 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
423 size.x, size.y,
424 rectGripper.width, rectGripper.height,
425 SWP_NOACTIVATE);
426 }
427
428 void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
429 {
430 if ( m_hGripper && IsShown() &&
431 event.GetWindow() && event.GetWindow()->GetParent() == this )
432 {
433 // Put gripper below the newly created child window
434 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
435 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
436 }
437
438 event.Skip();
439 }
440
441 // ----------------------------------------------------------------------------
442 // wxWin event handlers
443 // ----------------------------------------------------------------------------
444
445 #ifdef __POCKETPC__
446 // Responds to the OK button in a PocketPC titlebar. This
447 // can be overridden, or you can change the id used for
448 // sending the event, by calling SetAffirmativeId.
449 bool wxDialog::DoOK()
450 {
451 const int idOk = GetAffirmativeId();
452 if ( EmulateButtonClickIfPresent(idOk) )
453 return true;
454
455 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
456 event.SetEventObject(this);
457
458 return HandleWindowEvent(event);
459 }
460 #endif // __POCKETPC__
461
462 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
463 // create main toolbar by calling OnCreateToolBar()
464 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
465 {
466 m_dialogToolBar = OnCreateToolBar(style, winid, name);
467
468 return m_dialogToolBar;
469 }
470
471 // return a new toolbar
472 wxToolBar *wxDialog::OnCreateToolBar(long style,
473 wxWindowID winid,
474 const wxString& name)
475 {
476 return new wxToolMenuBar(this, winid,
477 wxDefaultPosition, wxDefaultSize,
478 style, name);
479 }
480 #endif
481
482 // ---------------------------------------------------------------------------
483 // dialog Windows messages processing
484 // ---------------------------------------------------------------------------
485
486 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
487 {
488 WXLRESULT rc = 0;
489 bool processed = false;
490
491 switch ( message )
492 {
493 #ifdef __WXWINCE__
494 // react to pressing the OK button in the title
495 case WM_COMMAND:
496 {
497 switch ( LOWORD(wParam) )
498 {
499 #ifdef __POCKETPC__
500 case IDOK:
501 processed = DoOK();
502 if (!processed)
503 processed = !Close();
504 #endif
505 #ifdef __SMARTPHONE__
506 case IDM_LEFT:
507 case IDM_RIGHT:
508 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
509 break;
510 #endif // __SMARTPHONE__
511 }
512 break;
513 }
514 #endif
515 case WM_CLOSE:
516 // if we can't close, tell the system that we processed the
517 // message - otherwise it would close us
518 processed = !Close();
519 break;
520
521 case WM_SIZE:
522 if ( m_hGripper )
523 {
524 switch ( wParam )
525 {
526 case SIZE_MAXIMIZED:
527 ShowGripper(false);
528 break;
529
530 case SIZE_RESTORED:
531 ShowGripper(true);
532 }
533 }
534
535 // the Windows dialogs unfortunately are not meant to be resizeable
536 // at all and their standard class doesn't include CS_[VH]REDRAW
537 // styles which means that the window is not refreshed properly
538 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
539 // help with it - so we have to refresh it manually which certainly
540 // creates flicker but at least doesn't show garbage on the screen
541 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
542 processed = true;
543 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
544 {
545 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
546 }
547 break;
548
549 #ifndef __WXMICROWIN__
550 case WM_SETCURSOR:
551 // we want to override the busy cursor for modal dialogs:
552 // typically, wxBeginBusyCursor() is called and then a modal dialog
553 // is shown, but the modal dialog shouldn't have hourglass cursor
554 if ( IsModal() && wxIsBusy() )
555 {
556 // set our cursor for all windows (but see below)
557 wxCursor cursor = m_cursor;
558 if ( !cursor.Ok() )
559 cursor = wxCURSOR_ARROW;
560
561 ::SetCursor(GetHcursorOf(cursor));
562
563 // in any case, stop here and don't let wxWindow process this
564 // message (it would set the busy cursor)
565 processed = true;
566
567 // but return false to tell the child window (if the event
568 // comes from one of them and not from ourselves) that it can
569 // set its own cursor if it has one: thus, standard controls
570 // (e.g. text ctrl) still have correct cursors in a dialog
571 // invoked while wxIsBusy()
572 rc = false;
573 }
574 break;
575 #endif // __WXMICROWIN__
576 }
577
578 if ( !processed )
579 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
580
581 return rc;
582 }