]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
reverted wxSocket changes of rev 48723 as they broke linking under non-Unix systems
[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
2bda0e17
KB
155}
156
b3daa5a3
VZ
157bool wxDialog::Create(wxWindow *parent,
158 wxWindowID id,
462e2437
VZ
159 const wxString& title,
160 const wxPoint& pos,
161 const wxSize& size,
162 long style,
163 const wxString& name)
2bda0e17 164{
82c9f85c 165 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 166
b225f659 167 // save focus before doing anything which can potentially change it
82c9f85c 168 m_oldFocus = FindFocus();
462e2437 169
706bb5f9 170 // All dialogs should really have this style
b225f659 171 style |= wxTAB_TRAVERSAL;
2bda0e17 172
b225f659 173 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 174 return false;
00233716
VZ
175
176 if ( !m_hasFont )
6f951810 177 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 178
3180bc0e 179#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
180 SetLeftMenu(wxID_OK, _("OK"));
181#endif
ec5f0c24
JS
182#if wxUSE_TOOLBAR && defined(__POCKETPC__)
183 CreateToolBar();
184#endif
ef6d716b 185
044fe836 186 return true;
2bda0e17
KB
187}
188
40ff126a
WS
189#if WXWIN_COMPATIBILITY_2_6
190
f46f4c86
VZ
191// deprecated ctor
192wxDialog::wxDialog(wxWindow *parent,
193 const wxString& title,
194 bool WXUNUSED(modal),
195 int x,
196 int y,
197 int w,
198 int h,
199 long style,
200 const wxString& name)
2bda0e17 201{
f46f4c86
VZ
202 Init();
203
d71cc120 204 Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name);
f46f4c86
VZ
205}
206
207void wxDialog::SetModal(bool WXUNUSED(flag))
208{
209 // nothing to do, obsolete method
2bda0e17
KB
210}
211
40ff126a
WS
212#endif // WXWIN_COMPATIBILITY_2_6
213
2bda0e17
KB
214wxDialog::~wxDialog()
215{
044fe836 216 m_isBeingDeleted = true;
2bda0e17 217
b0a6bb75 218 // this will also reenable all the other windows for a modal dialog
044fe836 219 Show(false);
2bda0e17
KB
220}
221
b6c588e1
VZ
222// ----------------------------------------------------------------------------
223// showing the dialogs
224// ----------------------------------------------------------------------------
22cf5fec 225
40ff126a
WS
226#if WXWIN_COMPATIBILITY_2_6
227
b6c588e1 228bool wxDialog::IsModalShowing() const
2bda0e17 229{
f46f4c86 230 return IsModal();
2bda0e17
KB
231}
232
40ff126a
WS
233#endif // WXWIN_COMPATIBILITY_2_6
234
a543e3ce
VZ
235wxWindow *wxDialog::FindSuitableParent() const
236{
237 // first try to use the currently active window
238 HWND hwndFg = ::GetForegroundWindow();
239 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
240 : NULL;
241 if ( !parent )
242 {
243 // next try the main app window
244 parent = wxTheApp->GetTopWindow();
245 }
246
247 // finally, check if the parent we found is really suitable
248 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
249 {
250 // don't use this one
251 parent = NULL;
252 }
253
254 return parent;
255}
256
b6c588e1 257bool wxDialog::Show(bool show)
2bda0e17 258{
044fe836
VZ
259 if ( show == IsShown() )
260 return false;
261
6757b5e3 262 if ( !show && m_modalData )
86ad564e 263 {
6757b5e3
VZ
264 // we need to do this before calling wxDialogBase version because if we
265 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 266 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
267 // which is enabled, anyhow) when we're hidden in the base class Show()
268 // and we will lose activation
269 m_modalData->ExitLoop();
86ad564e
JS
270 }
271
044fe836 272 if ( show )
b6c588e1 273 {
044fe836
VZ
274 // this usually will result in TransferDataToWindow() being called
275 // which will change the controls values so do it before showing as
276 // otherwise we could have some flicker
277 InitDialog();
b6c588e1 278 }
2bda0e17 279
044fe836
VZ
280 wxDialogBase::Show(show);
281
b6c588e1
VZ
282 if ( show )
283 {
2b5f62a0
VZ
284 // dialogs don't get WM_SIZE message after creation unlike most (all?)
285 // other windows and so could start their life non laid out correctly
286 // if we didn't call Layout() from here
287 //
288 // NB: normally we should call it just the first time but doing it
289 // every time is simpler than keeping a flag
290 Layout();
b6c588e1 291 }
2bda0e17 292
044fe836 293 return true;
2bda0e17
KB
294}
295
313901f3
JS
296void wxDialog::Raise()
297{
298 ::SetForegroundWindow(GetHwnd());
299}
300
f46f4c86 301// show dialog modally
a23fd0e1 302int wxDialog::ShowModal()
2bda0e17 303{
f46f4c86
VZ
304 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
305
044fe836 306 m_endModalCalled = false;
f46f4c86
VZ
307
308 Show();
309
310 // EndModal may have been called from InitDialog handler (called from
311 // inside Show()), which would cause an infinite loop if we didn't take it
312 // into account
313 if ( !m_endModalCalled )
5e1febfa 314 {
f46f4c86
VZ
315 // modal dialog needs a parent window, so try to find one
316 wxWindow *parent = GetParent();
317 if ( !parent )
318 {
319 parent = FindSuitableParent();
320 }
5e1febfa 321
f46f4c86
VZ
322 // remember where the focus was
323 wxWindow *oldFocus = m_oldFocus;
324 if ( !oldFocus )
325 {
326 // VZ: do we really want to do this?
327 oldFocus = parent;
328 }
329
330 // We have to remember the HWND because we need to check
331 // the HWND still exists (oldFocus can be garbage when the dialog
332 // exits, if it has been destroyed)
333 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
334
335
336 // enter and run the modal loop
337 {
338 wxDialogModalDataTiedPtr modalData(&m_modalData,
339 new wxDialogModalData(this));
340 modalData->RunLoop();
341 }
342
343
344 // and restore focus
345 // Note that this code MUST NOT access the dialog object's data
346 // in case the object has been deleted (which will be the case
347 // for a modal dialog that has been destroyed before calling EndModal).
348 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
349 {
350 // This is likely to prove that the object still exists
351 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
352 oldFocus->SetFocus();
353 }
354 }
5e1febfa 355
b6c588e1 356 return GetReturnCode();
2bda0e17
KB
357}
358
359void wxDialog::EndModal(int retCode)
360{
f46f4c86
VZ
361 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
362
044fe836 363 m_endModalCalled = true;
b6c588e1 364 SetReturnCode(retCode);
6a088435 365
12b58624
VZ
366 Hide();
367}
368
b6c588e1
VZ
369// ----------------------------------------------------------------------------
370// wxWin event handlers
371// ----------------------------------------------------------------------------
2bda0e17 372
9ceeecb9
JS
373#ifdef __POCKETPC__
374// Responds to the OK button in a PocketPC titlebar. This
375// can be overridden, or you can change the id used for
376// sending the event, by calling SetAffirmativeId.
377bool wxDialog::DoOK()
378{
f55fee08
VZ
379 const int idOk = GetAffirmativeId();
380 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 381 return true;
9ceeecb9 382
f55fee08
VZ
383 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
384 event.SetEventObject(this);
385
386 return GetEventHandler()->ProcessEvent(event);
9ceeecb9 387}
f55fee08 388#endif // __POCKETPC__
9ceeecb9 389
ec5f0c24
JS
390#if wxUSE_TOOLBAR && defined(__POCKETPC__)
391// create main toolbar by calling OnCreateToolBar()
392wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
393{
394 m_dialogToolBar = OnCreateToolBar(style, winid, name);
395
396 return m_dialogToolBar;
397}
398
399// return a new toolbar
400wxToolBar *wxDialog::OnCreateToolBar(long style,
401 wxWindowID winid,
402 const wxString& name)
403{
404 return new wxToolMenuBar(this, winid,
405 wxDefaultPosition, wxDefaultSize,
406 style, name);
660296aa 407}
ec5f0c24 408#endif
9ceeecb9 409
42e69d6b 410// ---------------------------------------------------------------------------
f55fee08 411// dialog Windows messages processing
42e69d6b
VZ
412// ---------------------------------------------------------------------------
413
c140b7e7 414WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 415{
c140b7e7 416 WXLRESULT rc = 0;
044fe836 417 bool processed = false;
42e69d6b
VZ
418
419 switch ( message )
420 {
0fc58b86
RR
421#ifdef __WXWINCE__
422 // react to pressing the OK button in the title
423 case WM_COMMAND:
ef6d716b
WS
424 {
425 switch ( LOWORD(wParam) )
0fc58b86 426 {
9ceeecb9 427#ifdef __POCKETPC__
ef6d716b 428 case IDOK:
9ceeecb9
JS
429 processed = DoOK();
430 if (!processed)
b554cf63 431 processed = !Close();
9ceeecb9
JS
432#endif
433#ifdef __SMARTPHONE__
ef6d716b
WS
434 case IDM_LEFT:
435 case IDM_RIGHT:
436 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 437 break;
ef6d716b 438#endif // __SMARTPHONE__
0fc58b86
RR
439 }
440 break;
ef6d716b 441 }
044fe836 442#endif
42e69d6b
VZ
443 case WM_CLOSE:
444 // if we can't close, tell the system that we processed the
445 // message - otherwise it would close us
446 processed = !Close();
447 break;
abceee76 448
f73f67be
VZ
449 case WM_SIZE:
450 // the Windows dialogs unfortunately are not meant to be resizeable
451 // at all and their standard class doesn't include CS_[VH]REDRAW
452 // styles which means that the window is not refreshed properly
453 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
454 // help with it - so we have to refresh it manually which certainly
455 // creates flicker but at least doesn't show garbage on the screen
456 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 457 processed = true;
e441e1f4 458 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 459 {
044fe836 460 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
461 }
462 break;
463
04ef50df 464#ifndef __WXMICROWIN__
abceee76
VZ
465 case WM_SETCURSOR:
466 // we want to override the busy cursor for modal dialogs:
467 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 468 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 469 if ( IsModal() && wxIsBusy() )
abceee76 470 {
bfbd6dc1
VZ
471 // set our cursor for all windows (but see below)
472 wxCursor cursor = m_cursor;
473 if ( !cursor.Ok() )
474 cursor = wxCURSOR_ARROW;
abceee76 475
bfbd6dc1
VZ
476 ::SetCursor(GetHcursorOf(cursor));
477
478 // in any case, stop here and don't let wxWindow process this
479 // message (it would set the busy cursor)
044fe836 480 processed = true;
bfbd6dc1 481
044fe836 482 // but return false to tell the child window (if the event
bfbd6dc1
VZ
483 // comes from one of them and not from ourselves) that it can
484 // set its own cursor if it has one: thus, standard controls
485 // (e.g. text ctrl) still have correct cursors in a dialog
486 // invoked while wxIsBusy()
044fe836 487 rc = false;
abceee76 488 }
bfbd6dc1 489 break;
82c9f85c 490#endif // __WXMICROWIN__
42e69d6b
VZ
491 }
492
493 if ( !processed )
494 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
495
496 return rc;
497}