delay gripper positioning until the dialog is shown to ensure that it doesn't overwri...
[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 CreateGripper();
189
190 return true;
191 }
192
193 wxDialog::~wxDialog()
194 {
195 m_isBeingDeleted = true;
196
197 // this will also reenable all the other windows for a modal dialog
198 Show(false);
199
200 DestroyGripper();
201 }
202
203 // ----------------------------------------------------------------------------
204 // showing the dialogs
205 // ----------------------------------------------------------------------------
206
207 wxWindow *wxDialog::FindSuitableParent() const
208 {
209 // first try to use the currently active window
210 HWND hwndFg = ::GetForegroundWindow();
211 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
212 : NULL;
213 if ( !parent )
214 {
215 // next try the main app window
216 parent = wxTheApp->GetTopWindow();
217 }
218
219 // finally, check if the parent we found is really suitable
220 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
221 {
222 // don't use this one
223 parent = NULL;
224 }
225
226 return parent;
227 }
228
229 bool wxDialog::Show(bool show)
230 {
231 if ( show == IsShown() )
232 return false;
233
234 if ( !show && m_modalData )
235 {
236 // we need to do this before calling wxDialogBase version because if we
237 // had disabled other app windows, they must be reenabled right now as
238 // if they stay disabled Windows will activate another window (one
239 // which is enabled, anyhow) when we're hidden in the base class Show()
240 // and we will lose activation
241 m_modalData->ExitLoop();
242 }
243
244 if ( show )
245 {
246 if (CanDoLayoutAdaptation())
247 DoLayoutAdaptation();
248
249 // this usually will result in TransferDataToWindow() being called
250 // which will change the controls values so do it before showing as
251 // otherwise we could have some flicker
252 InitDialog();
253 }
254
255 wxDialogBase::Show(show);
256
257 if ( show )
258 {
259 // dialogs don't get WM_SIZE message after creation unlike most (all?)
260 // other windows and so could start their life non laid out correctly
261 // if we didn't call Layout() from here
262 //
263 // NB: normally we should call it just the first time but doing it
264 // every time is simpler than keeping a flag
265 Layout();
266 }
267
268 return true;
269 }
270
271 void wxDialog::Raise()
272 {
273 ::SetForegroundWindow(GetHwnd());
274 }
275
276 // show dialog modally
277 int wxDialog::ShowModal()
278 {
279 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
280
281 m_endModalCalled = false;
282
283 Show();
284
285 // EndModal may have been called from InitDialog handler (called from
286 // inside Show()), which would cause an infinite loop if we didn't take it
287 // into account
288 if ( !m_endModalCalled )
289 {
290 // modal dialog needs a parent window, so try to find one
291 wxWindow *parent = GetParent();
292 if ( !parent )
293 {
294 parent = FindSuitableParent();
295 }
296
297 // remember where the focus was
298 wxWindow *oldFocus = m_oldFocus;
299 if ( !oldFocus )
300 {
301 // VZ: do we really want to do this?
302 oldFocus = parent;
303 }
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 = oldFocus ? GetHwndOf(oldFocus) : NULL;
309
310
311 // enter and run the modal loop
312 {
313 wxDialogModalDataTiedPtr modalData(&m_modalData,
314 new wxDialogModalData(this));
315 modalData->RunLoop();
316 }
317
318
319 // and restore focus
320 // Note that this code MUST NOT access the dialog object's data
321 // in case the object has been deleted (which will be the case
322 // for a modal dialog that has been destroyed before calling EndModal).
323 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
324 {
325 // This is likely to prove that the object still exists
326 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
327 oldFocus->SetFocus();
328 }
329 }
330
331 return GetReturnCode();
332 }
333
334 void wxDialog::EndModal(int retCode)
335 {
336 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
337
338 m_endModalCalled = true;
339 SetReturnCode(retCode);
340
341 Hide();
342 }
343
344 // ----------------------------------------------------------------------------
345 // wxDialog gripper handling
346 // ----------------------------------------------------------------------------
347
348 void wxDialog::SetWindowStyleFlag(long style)
349 {
350 wxDialogBase::SetWindowStyleFlag(style);
351
352 if ( HasFlag(wxRESIZE_BORDER) )
353 CreateGripper();
354 else
355 DestroyGripper();
356 }
357
358 void wxDialog::CreateGripper()
359 {
360 if ( !m_hGripper )
361 {
362 // just create it here, it will be positioned and shown later
363 m_hGripper = (WXHWND)::CreateWindow
364 (
365 wxT("SCROLLBAR"),
366 wxT(""),
367 WS_CHILD |
368 WS_CLIPSIBLINGS |
369 SBS_SIZEGRIP |
370 SBS_SIZEBOX |
371 SBS_SIZEBOXBOTTOMRIGHTALIGN,
372 0, 0, 0, 0,
373 GetHwnd(),
374 0,
375 wxGetInstance(),
376 NULL
377 );
378 }
379 }
380
381 void wxDialog::DestroyGripper()
382 {
383 if ( m_hGripper )
384 {
385 ::DestroyWindow((HWND) m_hGripper);
386 m_hGripper = 0;
387 }
388 }
389
390 void wxDialog::ShowGripper(bool show)
391 {
392 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
393
394 if ( show )
395 ResizeGripper();
396
397 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
398 }
399
400 void wxDialog::ResizeGripper()
401 {
402 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
403
404 HWND hwndGripper = (HWND)m_hGripper;
405
406 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
407 const wxSize size = GetClientSize() - rectGripper.GetSize();
408
409 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
410 size.x, size.y,
411 rectGripper.width, rectGripper.height,
412 SWP_NOACTIVATE);
413 }
414
415 // ----------------------------------------------------------------------------
416 // wxWin event handlers
417 // ----------------------------------------------------------------------------
418
419 #ifdef __POCKETPC__
420 // Responds to the OK button in a PocketPC titlebar. This
421 // can be overridden, or you can change the id used for
422 // sending the event, by calling SetAffirmativeId.
423 bool wxDialog::DoOK()
424 {
425 const int idOk = GetAffirmativeId();
426 if ( EmulateButtonClickIfPresent(idOk) )
427 return true;
428
429 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
430 event.SetEventObject(this);
431
432 return HandleWindowEvent(event);
433 }
434 #endif // __POCKETPC__
435
436 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
437 // create main toolbar by calling OnCreateToolBar()
438 wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
439 {
440 m_dialogToolBar = OnCreateToolBar(style, winid, name);
441
442 return m_dialogToolBar;
443 }
444
445 // return a new toolbar
446 wxToolBar *wxDialog::OnCreateToolBar(long style,
447 wxWindowID winid,
448 const wxString& name)
449 {
450 return new wxToolMenuBar(this, winid,
451 wxDefaultPosition, wxDefaultSize,
452 style, name);
453 }
454 #endif
455
456 // ---------------------------------------------------------------------------
457 // dialog Windows messages processing
458 // ---------------------------------------------------------------------------
459
460 WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
461 {
462 WXLRESULT rc = 0;
463 bool processed = false;
464
465 switch ( message )
466 {
467 #ifdef __WXWINCE__
468 // react to pressing the OK button in the title
469 case WM_COMMAND:
470 {
471 switch ( LOWORD(wParam) )
472 {
473 #ifdef __POCKETPC__
474 case IDOK:
475 processed = DoOK();
476 if (!processed)
477 processed = !Close();
478 #endif
479 #ifdef __SMARTPHONE__
480 case IDM_LEFT:
481 case IDM_RIGHT:
482 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
483 break;
484 #endif // __SMARTPHONE__
485 }
486 break;
487 }
488 #endif
489 case WM_CLOSE:
490 // if we can't close, tell the system that we processed the
491 // message - otherwise it would close us
492 processed = !Close();
493 break;
494
495 case WM_SIZE:
496 if ( m_hGripper )
497 {
498 switch ( wParam )
499 {
500 case SIZE_MAXIMIZED:
501 ShowGripper(false);
502 break;
503
504 case SIZE_RESTORED:
505 ShowGripper(true);
506 }
507 }
508
509 // the Windows dialogs unfortunately are not meant to be resizeable
510 // at all and their standard class doesn't include CS_[VH]REDRAW
511 // styles which means that the window is not refreshed properly
512 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
513 // help with it - so we have to refresh it manually which certainly
514 // creates flicker but at least doesn't show garbage on the screen
515 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
516 processed = true;
517 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
518 {
519 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
520 }
521 break;
522
523 case WM_WINDOWPOSCHANGED:
524 {
525 WINDOWPOS * const wp = wx_reinterpret_cast(WINDOWPOS *, lParam);
526 if ( wp->flags & SWP_SHOWWINDOW )
527 {
528 // we should only show it now to ensure that it's really
529 // positioned underneath under all the other controls in
530 // the dialog, if we showed it before it could overlap them
531 if ( m_hGripper )
532 ShowGripper(true);
533 }
534 }
535 break;
536
537 #ifndef __WXMICROWIN__
538 case WM_SETCURSOR:
539 // we want to override the busy cursor for modal dialogs:
540 // typically, wxBeginBusyCursor() is called and then a modal dialog
541 // is shown, but the modal dialog shouldn't have hourglass cursor
542 if ( IsModal() && wxIsBusy() )
543 {
544 // set our cursor for all windows (but see below)
545 wxCursor cursor = m_cursor;
546 if ( !cursor.Ok() )
547 cursor = wxCURSOR_ARROW;
548
549 ::SetCursor(GetHcursorOf(cursor));
550
551 // in any case, stop here and don't let wxWindow process this
552 // message (it would set the busy cursor)
553 processed = true;
554
555 // but return false to tell the child window (if the event
556 // comes from one of them and not from ourselves) that it can
557 // set its own cursor if it has one: thus, standard controls
558 // (e.g. text ctrl) still have correct cursors in a dialog
559 // invoked while wxIsBusy()
560 rc = false;
561 }
562 break;
563 #endif // __WXMICROWIN__
564 }
565
566 if ( !processed )
567 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
568
569 return rc;
570 }