]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Dont't finish any editing if there is no control
[wxWidgets.git] / src / msw / dialog.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
b6c588e1 2// Name: src/msw/dialog.cpp
2bda0e17
KB
3// Purpose: wxDialog class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b6c588e1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
b6c588e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
fdf565fe
WS
27#include "wx/dialog.h"
28
2bda0e17 29#ifndef WX_PRECOMP
57bd4c60 30 #include "wx/msw/wrapcdlg.h"
b6c588e1
VZ
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
95e92d90 34 #include "wx/button.h"
b6c588e1
VZ
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
4e3e485b 38 #include "wx/toolbar.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
ac8d0c11 42#include "wx/evtloop.h"
8c7f5f03 43#include "wx/ptr_scpd.h"
2bda0e17 44
3180bc0e 45#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b 46 #include "wx/msw/wince/resources.h"
3180bc0e 47#endif // __SMARTPHONE__ && __WXWINCE__
ef6d716b 48
b6c588e1
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
ffca575c
SC
53#if wxUSE_EXTENDED_RTTI
54WX_DEFINE_FLAGS( wxDialogStyle )
55
3ff066a4 56wxBEGIN_FLAGS( wxDialogStyle )
ffca575c
SC
57 // new style border flags, we put them first to
58 // use them for streaming out
3ff066a4
SC
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)
044fe836 65
ffca575c 66 // old style border flags
3ff066a4
SC
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)
ffca575c
SC
73
74 // standard window styles
3ff066a4
SC
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
ffca575c
SC
77
78 // dialog styles
3ff066a4
SC
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
81 wxFLAGS_MEMBER(wxCAPTION)
1c067fe3 82#if WXWIN_COMPATIBILITY_2_6
3ff066a4 83 wxFLAGS_MEMBER(wxTHICK_FRAME)
1c067fe3 84#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
85 wxFLAGS_MEMBER(wxSYSTEM_MENU)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER)
1c067fe3 87#if WXWIN_COMPATIBILITY_2_6
3ff066a4 88 wxFLAGS_MEMBER(wxRESIZE_BOX)
1c067fe3 89#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
90 wxFLAGS_MEMBER(wxCLOSE_BOX)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
93wxEND_FLAGS( wxDialogStyle )
ffca575c
SC
94
95IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
96
3ff066a4 97wxBEGIN_PROPERTIES_TABLE(wxDialog)
af498247
VZ
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
3ff066a4 100wxEND_PROPERTIES_TABLE()
ffca575c 101
3ff066a4
SC
102wxBEGIN_HANDLERS_TABLE(wxDialog)
103wxEND_HANDLERS_TABLE()
ffca575c 104
044fe836 105wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
ffca575c
SC
106
107#else
82c9f85c 108IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
ffca575c 109#endif
066f1b7a 110
6757b5e3
VZ
111// ----------------------------------------------------------------------------
112// wxDialogModalData
113// ----------------------------------------------------------------------------
114
8c7f5f03
VZ
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
6757b5e3
VZ
117class wxDialogModalData
118{
119public:
8c7f5f03 120 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 121
8c7f5f03 122 void RunLoop()
6757b5e3 123 {
6757b5e3
VZ
124 m_evtLoop.Run();
125 }
126
127 void ExitLoop()
128 {
6757b5e3
VZ
129 m_evtLoop.Exit();
130 }
131
6757b5e3 132private:
8c7f5f03 133 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
134};
135
259c43f6 136wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 137
b6c588e1
VZ
138// ============================================================================
139// implementation
140// ============================================================================
141
142// ----------------------------------------------------------------------------
143// wxDialog construction
144// ----------------------------------------------------------------------------
145
b0a6bb75 146void wxDialog::Init()
2bda0e17 147{
52a07708 148 m_oldFocus = (wxWindow *)NULL;
044fe836 149 m_isShown = false;
6757b5e3 150 m_modalData = NULL;
044fe836 151 m_endModalCalled = false;
ec5f0c24
JS
152#if wxUSE_TOOLBAR && defined(__POCKETPC__)
153 m_dialogToolBar = NULL;
154#endif
2c66581e 155 m_hGripper = 0;
2bda0e17
KB
156}
157
b3daa5a3
VZ
158bool wxDialog::Create(wxWindow *parent,
159 wxWindowID id,
462e2437
VZ
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
2bda0e17 165{
82c9f85c 166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 167
b225f659 168 // save focus before doing anything which can potentially change it
82c9f85c 169 m_oldFocus = FindFocus();
462e2437 170
706bb5f9 171 // All dialogs should really have this style
b225f659 172 style |= wxTAB_TRAVERSAL;
2bda0e17 173
b225f659 174 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 175 return false;
00233716
VZ
176
177 if ( !m_hasFont )
6f951810 178 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 179
3180bc0e 180#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
181 SetLeftMenu(wxID_OK, _("OK"));
182#endif
ec5f0c24
JS
183#if wxUSE_TOOLBAR && defined(__POCKETPC__)
184 CreateToolBar();
185#endif
ef6d716b 186
952f8d3c 187 if ( HasFlag(wxRESIZE_BORDER) )
eddc468e 188 {
2c66581e
VZ
189 CreateGripper();
190
eddc468e
VZ
191 Connect(wxEVT_CREATE,
192 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
193 }
194
044fe836 195 return true;
2bda0e17
KB
196}
197
2bda0e17
KB
198wxDialog::~wxDialog()
199{
044fe836 200 m_isBeingDeleted = true;
2bda0e17 201
b0a6bb75 202 // this will also reenable all the other windows for a modal dialog
044fe836 203 Show(false);
2c66581e
VZ
204
205 DestroyGripper();
2bda0e17
KB
206}
207
b6c588e1
VZ
208// ----------------------------------------------------------------------------
209// showing the dialogs
210// ----------------------------------------------------------------------------
22cf5fec 211
a543e3ce
VZ
212wxWindow *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
b6c588e1 234bool wxDialog::Show(bool show)
2bda0e17 235{
044fe836
VZ
236 if ( show == IsShown() )
237 return false;
238
6757b5e3 239 if ( !show && m_modalData )
86ad564e 240 {
6757b5e3
VZ
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
b0a6bb75 243 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
244 // which is enabled, anyhow) when we're hidden in the base class Show()
245 // and we will lose activation
246 m_modalData->ExitLoop();
86ad564e
JS
247 }
248
044fe836 249 if ( show )
b6c588e1 250 {
3aa8e4ea
JS
251 if (CanDoLayoutAdaptation())
252 DoLayoutAdaptation();
253
044fe836
VZ
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();
b6c588e1 258 }
2bda0e17 259
044fe836
VZ
260 wxDialogBase::Show(show);
261
b6c588e1
VZ
262 if ( show )
263 {
2bc44d62
VZ
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
2b5f62a0
VZ
267 //
268 // NB: normally we should call it just the first time but doing it
269 // every time is simpler than keeping a flag
2bc44d62
VZ
270 const wxSize size = GetClientSize();
271 ::SendMessage(GetHwnd(), WM_SIZE,
272 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
b6c588e1 273 }
2bda0e17 274
044fe836 275 return true;
2bda0e17
KB
276}
277
313901f3
JS
278void wxDialog::Raise()
279{
280 ::SetForegroundWindow(GetHwnd());
281}
282
f46f4c86 283// show dialog modally
a23fd0e1 284int wxDialog::ShowModal()
2bda0e17 285{
dc28b856 286 wxASSERT_MSG( !IsModal(), _T("ShowModal() can't be called twice") );
f46f4c86 287
044fe836 288 m_endModalCalled = false;
f46f4c86
VZ
289
290 Show();
291
292 // EndModal may have been called from InitDialog handler (called from
293 // inside Show()), which would cause an infinite loop if we didn't take it
294 // into account
295 if ( !m_endModalCalled )
5e1febfa 296 {
f46f4c86
VZ
297 // modal dialog needs a parent window, so try to find one
298 wxWindow *parent = GetParent();
299 if ( !parent )
300 {
301 parent = FindSuitableParent();
302 }
5e1febfa 303
f46f4c86
VZ
304 // remember where the focus was
305 wxWindow *oldFocus = m_oldFocus;
306 if ( !oldFocus )
307 {
308 // VZ: do we really want to do this?
309 oldFocus = parent;
310 }
311
312 // We have to remember the HWND because we need to check
313 // the HWND still exists (oldFocus can be garbage when the dialog
314 // exits, if it has been destroyed)
315 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
316
317
318 // enter and run the modal loop
319 {
320 wxDialogModalDataTiedPtr modalData(&m_modalData,
321 new wxDialogModalData(this));
322 modalData->RunLoop();
323 }
324
325
326 // and restore focus
327 // Note that this code MUST NOT access the dialog object's data
328 // in case the object has been deleted (which will be the case
329 // for a modal dialog that has been destroyed before calling EndModal).
330 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
331 {
332 // This is likely to prove that the object still exists
333 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
334 oldFocus->SetFocus();
335 }
336 }
5e1febfa 337
b6c588e1 338 return GetReturnCode();
2bda0e17
KB
339}
340
341void wxDialog::EndModal(int retCode)
342{
f46f4c86
VZ
343 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
344
044fe836 345 m_endModalCalled = true;
b6c588e1 346 SetReturnCode(retCode);
6a088435 347
12b58624
VZ
348 Hide();
349}
350
2c66581e
VZ
351// ----------------------------------------------------------------------------
352// wxDialog gripper handling
353// ----------------------------------------------------------------------------
354
355void wxDialog::SetWindowStyleFlag(long style)
356{
357 wxDialogBase::SetWindowStyleFlag(style);
358
952f8d3c 359 if ( HasFlag(wxRESIZE_BORDER) )
2c66581e
VZ
360 CreateGripper();
361 else
362 DestroyGripper();
363}
364
365void wxDialog::CreateGripper()
366{
952f8d3c 367 if ( !m_hGripper )
2c66581e 368 {
952f8d3c 369 // just create it here, it will be positioned and shown later
2c66581e
VZ
370 m_hGripper = (WXHWND)::CreateWindow
371 (
372 wxT("SCROLLBAR"),
373 wxT(""),
952f8d3c
VZ
374 WS_CHILD |
375 WS_CLIPSIBLINGS |
2c66581e
VZ
376 SBS_SIZEGRIP |
377 SBS_SIZEBOX |
378 SBS_SIZEBOXBOTTOMRIGHTALIGN,
379 0, 0, 0, 0,
380 GetHwnd(),
381 0,
382 wxGetInstance(),
383 NULL
384 );
2c66581e
VZ
385 }
386}
387
388void wxDialog::DestroyGripper()
389{
390 if ( m_hGripper )
391 {
eddc468e
VZ
392 // we used to have trouble with gripper appearing on top (and hence
393 // overdrawing) the other, real, dialog children -- check that this
f78c70f3
VZ
394 // isn't the case automatically (but notice that this could be false if
395 // we're not shown at all as in this case ResizeGripper() might not
396 // have been called yet)
397 wxASSERT_MSG( !IsShown() ||
398 ::GetNextWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
eddc468e 399 _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
2c66581e
VZ
400 ::DestroyWindow((HWND) m_hGripper);
401 m_hGripper = 0;
402 }
403}
404
405void wxDialog::ShowGripper(bool show)
406{
407 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
408
952f8d3c
VZ
409 if ( show )
410 ResizeGripper();
411
2c66581e
VZ
412 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
413}
414
415void wxDialog::ResizeGripper()
416{
417 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
418
419 HWND hwndGripper = (HWND)m_hGripper;
420
421 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
422 const wxSize size = GetClientSize() - rectGripper.GetSize();
423
424 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
425 size.x, size.y,
426 rectGripper.width, rectGripper.height,
427 SWP_NOACTIVATE);
428}
429
eddc468e
VZ
430void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
431{
432 if ( m_hGripper && IsShown() &&
433 event.GetWindow() && event.GetWindow()->GetParent() == this )
434 {
435 // Put gripper below the newly created child window
436 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
437 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
438 }
439
440 event.Skip();
441}
442
b6c588e1
VZ
443// ----------------------------------------------------------------------------
444// wxWin event handlers
445// ----------------------------------------------------------------------------
2bda0e17 446
9ceeecb9
JS
447#ifdef __POCKETPC__
448// Responds to the OK button in a PocketPC titlebar. This
449// can be overridden, or you can change the id used for
450// sending the event, by calling SetAffirmativeId.
451bool wxDialog::DoOK()
452{
f55fee08
VZ
453 const int idOk = GetAffirmativeId();
454 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 455 return true;
9ceeecb9 456
f55fee08
VZ
457 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
458 event.SetEventObject(this);
459
937013e0 460 return HandleWindowEvent(event);
9ceeecb9 461}
f55fee08 462#endif // __POCKETPC__
9ceeecb9 463
ec5f0c24
JS
464#if wxUSE_TOOLBAR && defined(__POCKETPC__)
465// create main toolbar by calling OnCreateToolBar()
466wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
467{
468 m_dialogToolBar = OnCreateToolBar(style, winid, name);
469
470 return m_dialogToolBar;
471}
472
473// return a new toolbar
474wxToolBar *wxDialog::OnCreateToolBar(long style,
475 wxWindowID winid,
476 const wxString& name)
477{
478 return new wxToolMenuBar(this, winid,
479 wxDefaultPosition, wxDefaultSize,
480 style, name);
660296aa 481}
ec5f0c24 482#endif
9ceeecb9 483
42e69d6b 484// ---------------------------------------------------------------------------
f55fee08 485// dialog Windows messages processing
42e69d6b
VZ
486// ---------------------------------------------------------------------------
487
c140b7e7 488WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 489{
c140b7e7 490 WXLRESULT rc = 0;
044fe836 491 bool processed = false;
42e69d6b
VZ
492
493 switch ( message )
494 {
0fc58b86
RR
495#ifdef __WXWINCE__
496 // react to pressing the OK button in the title
497 case WM_COMMAND:
ef6d716b
WS
498 {
499 switch ( LOWORD(wParam) )
0fc58b86 500 {
9ceeecb9 501#ifdef __POCKETPC__
ef6d716b 502 case IDOK:
9ceeecb9
JS
503 processed = DoOK();
504 if (!processed)
b554cf63 505 processed = !Close();
9ceeecb9
JS
506#endif
507#ifdef __SMARTPHONE__
ef6d716b
WS
508 case IDM_LEFT:
509 case IDM_RIGHT:
510 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 511 break;
ef6d716b 512#endif // __SMARTPHONE__
0fc58b86
RR
513 }
514 break;
ef6d716b 515 }
044fe836 516#endif
42e69d6b
VZ
517 case WM_CLOSE:
518 // if we can't close, tell the system that we processed the
519 // message - otherwise it would close us
520 processed = !Close();
521 break;
abceee76 522
f73f67be 523 case WM_SIZE:
2c66581e
VZ
524 if ( m_hGripper )
525 {
526 switch ( wParam )
527 {
528 case SIZE_MAXIMIZED:
529 ShowGripper(false);
530 break;
531
532 case SIZE_RESTORED:
533 ShowGripper(true);
2c66581e
VZ
534 }
535 }
536
f73f67be
VZ
537 // the Windows dialogs unfortunately are not meant to be resizeable
538 // at all and their standard class doesn't include CS_[VH]REDRAW
539 // styles which means that the window is not refreshed properly
540 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
541 // help with it - so we have to refresh it manually which certainly
542 // creates flicker but at least doesn't show garbage on the screen
543 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 544 processed = true;
e441e1f4 545 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 546 {
044fe836 547 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
548 }
549 break;
550
04ef50df 551#ifndef __WXMICROWIN__
abceee76
VZ
552 case WM_SETCURSOR:
553 // we want to override the busy cursor for modal dialogs:
554 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 555 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 556 if ( IsModal() && wxIsBusy() )
abceee76 557 {
bfbd6dc1
VZ
558 // set our cursor for all windows (but see below)
559 wxCursor cursor = m_cursor;
560 if ( !cursor.Ok() )
561 cursor = wxCURSOR_ARROW;
abceee76 562
bfbd6dc1
VZ
563 ::SetCursor(GetHcursorOf(cursor));
564
565 // in any case, stop here and don't let wxWindow process this
566 // message (it would set the busy cursor)
044fe836 567 processed = true;
bfbd6dc1 568
044fe836 569 // but return false to tell the child window (if the event
bfbd6dc1
VZ
570 // comes from one of them and not from ourselves) that it can
571 // set its own cursor if it has one: thus, standard controls
572 // (e.g. text ctrl) still have correct cursors in a dialog
573 // invoked while wxIsBusy()
044fe836 574 rc = false;
abceee76 575 }
bfbd6dc1 576 break;
82c9f85c 577#endif // __WXMICROWIN__
42e69d6b
VZ
578 }
579
580 if ( !processed )
581 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
582
583 return rc;
584}